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 */ 69*552a37e9SLai Jiangshan POOL_MANAGING_WORKERS = 1 << 1, /* managing workers */ 70db7bccf4STejun Heo 71c8e55f36STejun Heo /* worker flags */ 72c8e55f36STejun Heo WORKER_STARTED = 1 << 0, /* started */ 73c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 74c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 75e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 76e22bee78STejun Heo WORKER_REBIND = 1 << 5, /* mom is home, come back */ 77fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 78f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 79e22bee78STejun Heo 80403c821dSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_REBIND | WORKER_UNBOUND | 81403c821dSTejun Heo WORKER_CPU_INTENSIVE, 82db7bccf4STejun Heo 833270476aSTejun Heo NR_WORKER_POOLS = 2, /* # worker pools per gcwq */ 844ce62e9eSTejun Heo 85c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 86c8e55f36STejun Heo BUSY_WORKER_HASH_SIZE = 1 << BUSY_WORKER_HASH_ORDER, 87c8e55f36STejun Heo BUSY_WORKER_HASH_MASK = BUSY_WORKER_HASH_SIZE - 1, 88db7bccf4STejun Heo 89e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 90e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 91e22bee78STejun Heo 923233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 933233cdbdSTejun Heo /* call for help after 10ms 943233cdbdSTejun Heo (min two ticks) */ 95e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 96e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 99e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 100e22bee78STejun Heo * all cpus. Give -20. 101e22bee78STejun Heo */ 102e22bee78STejun Heo RESCUER_NICE_LEVEL = -20, 1033270476aSTejun Heo HIGHPRI_NICE_LEVEL = -20, 104c8e55f36STejun Heo }; 105c8e55f36STejun Heo 1061da177e4SLinus Torvalds /* 1074690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1084690c4abSTejun Heo * 109e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 110e41e704bSTejun Heo * everyone else. 1114690c4abSTejun Heo * 112e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 113e22bee78STejun Heo * only be modified and accessed from the local cpu. 114e22bee78STejun Heo * 1158b03ae3cSTejun Heo * L: gcwq->lock protected. Access with gcwq->lock held. 1164690c4abSTejun Heo * 117e22bee78STejun Heo * X: During normal operation, modification requires gcwq->lock and 118e22bee78STejun Heo * should be done only from local cpu. Either disabling preemption 119e22bee78STejun Heo * on local cpu or grabbing gcwq->lock is enough for read access. 120f3421797STejun Heo * If GCWQ_DISASSOCIATED is set, it's identical to L. 121e22bee78STejun Heo * 12273f53c4aSTejun Heo * F: wq->flush_mutex protected. 12373f53c4aSTejun Heo * 1244690c4abSTejun Heo * W: workqueue_lock protected. 1254690c4abSTejun Heo */ 1264690c4abSTejun Heo 1278b03ae3cSTejun Heo struct global_cwq; 128bd7bdd43STejun Heo struct worker_pool; 12925511a47STejun Heo struct idle_rebind; 130c34056a3STejun Heo 131e22bee78STejun Heo /* 132e22bee78STejun Heo * The poor guys doing the actual heavy lifting. All on-duty workers 133e22bee78STejun Heo * are either serving the manager role, on idle list or on busy hash. 134e22bee78STejun Heo */ 135c34056a3STejun Heo struct worker { 136c8e55f36STejun Heo /* on idle list while idle, on busy hash table while busy */ 137c8e55f36STejun Heo union { 138c8e55f36STejun Heo struct list_head entry; /* L: while idle */ 139c8e55f36STejun Heo struct hlist_node hentry; /* L: while busy */ 140c8e55f36STejun Heo }; 141c8e55f36STejun Heo 142c34056a3STejun Heo struct work_struct *current_work; /* L: work being processed */ 1438cca0eeaSTejun Heo struct cpu_workqueue_struct *current_cwq; /* L: current_work's cwq */ 144affee4b2STejun Heo struct list_head scheduled; /* L: scheduled works */ 145c34056a3STejun Heo struct task_struct *task; /* I: worker task */ 146bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 147e22bee78STejun Heo /* 64 bytes boundary on 64bit, 32 on 32bit */ 148e22bee78STejun Heo unsigned long last_active; /* L: last active timestamp */ 149e22bee78STejun Heo unsigned int flags; /* X: flags */ 150c34056a3STejun Heo int id; /* I: worker id */ 15125511a47STejun Heo 15225511a47STejun Heo /* for rebinding worker to CPU */ 15325511a47STejun Heo struct idle_rebind *idle_rebind; /* L: for idle worker */ 15425511a47STejun Heo struct work_struct rebind_work; /* L: for busy worker */ 155c34056a3STejun Heo }; 156c34056a3STejun Heo 157bd7bdd43STejun Heo struct worker_pool { 158bd7bdd43STejun Heo struct global_cwq *gcwq; /* I: the owning gcwq */ 15911ebea50STejun Heo unsigned int flags; /* X: flags */ 160bd7bdd43STejun Heo 161bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 162bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 163bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 164bd7bdd43STejun Heo 165bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 166bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 167bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 168bd7bdd43STejun Heo 16960373152STejun Heo struct mutex manager_mutex; /* mutex manager should hold */ 170bd7bdd43STejun Heo struct ida worker_ida; /* L: for worker IDs */ 171bd7bdd43STejun Heo }; 172bd7bdd43STejun Heo 1734690c4abSTejun Heo /* 174e22bee78STejun Heo * Global per-cpu workqueue. There's one and only one for each cpu 175e22bee78STejun Heo * and all works are queued and processed here regardless of their 176e22bee78STejun Heo * target workqueues. 1778b03ae3cSTejun Heo */ 1788b03ae3cSTejun Heo struct global_cwq { 1798b03ae3cSTejun Heo spinlock_t lock; /* the gcwq lock */ 1808b03ae3cSTejun Heo unsigned int cpu; /* I: the associated cpu */ 181db7bccf4STejun Heo unsigned int flags; /* L: GCWQ_* flags */ 182c8e55f36STejun Heo 183bd7bdd43STejun Heo /* workers are chained either in busy_hash or pool idle_list */ 184c8e55f36STejun Heo struct hlist_head busy_hash[BUSY_WORKER_HASH_SIZE]; 185c8e55f36STejun Heo /* L: hash of busy workers */ 186c8e55f36STejun Heo 1873270476aSTejun Heo struct worker_pool pools[2]; /* normal and highpri pools */ 188db7bccf4STejun Heo 18925511a47STejun Heo wait_queue_head_t rebind_hold; /* rebind hold wait */ 1908b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1918b03ae3cSTejun Heo 1928b03ae3cSTejun Heo /* 193502ca9d8STejun Heo * The per-CPU workqueue. The lower WORK_STRUCT_FLAG_BITS of 1940f900049STejun Heo * work_struct->data are used for flags and thus cwqs need to be 1950f900049STejun Heo * aligned at two's power of the number of flag bits. 1961da177e4SLinus Torvalds */ 1971da177e4SLinus Torvalds struct cpu_workqueue_struct { 198bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 1994690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 20073f53c4aSTejun Heo int work_color; /* L: current color */ 20173f53c4aSTejun Heo int flush_color; /* L: flushing color */ 20273f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20373f53c4aSTejun Heo /* L: nr of in_flight works */ 2041e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 205a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2061e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2070f900049STejun Heo }; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds /* 21073f53c4aSTejun Heo * Structure used to wait for workqueue flush. 21173f53c4aSTejun Heo */ 21273f53c4aSTejun Heo struct wq_flusher { 21373f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 21473f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 21573f53c4aSTejun Heo struct completion done; /* flush completion */ 21673f53c4aSTejun Heo }; 2171da177e4SLinus Torvalds 21873f53c4aSTejun Heo /* 219f2e005aaSTejun Heo * All cpumasks are assumed to be always set on UP and thus can't be 220f2e005aaSTejun Heo * used to determine whether there's something to be done. 221f2e005aaSTejun Heo */ 222f2e005aaSTejun Heo #ifdef CONFIG_SMP 223f2e005aaSTejun Heo typedef cpumask_var_t mayday_mask_t; 224f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) \ 225f2e005aaSTejun Heo cpumask_test_and_set_cpu((cpu), (mask)) 226f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) cpumask_clear_cpu((cpu), (mask)) 227f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) for_each_cpu((cpu), (mask)) 2289c37547aSTejun Heo #define alloc_mayday_mask(maskp, gfp) zalloc_cpumask_var((maskp), (gfp)) 229f2e005aaSTejun Heo #define free_mayday_mask(mask) free_cpumask_var((mask)) 230f2e005aaSTejun Heo #else 231f2e005aaSTejun Heo typedef unsigned long mayday_mask_t; 232f2e005aaSTejun Heo #define mayday_test_and_set_cpu(cpu, mask) test_and_set_bit(0, &(mask)) 233f2e005aaSTejun Heo #define mayday_clear_cpu(cpu, mask) clear_bit(0, &(mask)) 234f2e005aaSTejun Heo #define for_each_mayday_cpu(cpu, mask) if ((cpu) = 0, (mask)) 235f2e005aaSTejun Heo #define alloc_mayday_mask(maskp, gfp) true 236f2e005aaSTejun Heo #define free_mayday_mask(mask) do { } while (0) 237f2e005aaSTejun Heo #endif 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /* 2401da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 2411da177e4SLinus Torvalds * per-CPU workqueues: 2421da177e4SLinus Torvalds */ 2431da177e4SLinus Torvalds struct workqueue_struct { 2449c5a2ba7STejun Heo unsigned int flags; /* W: WQ_* flags */ 245bdbc5dd7STejun Heo union { 246bdbc5dd7STejun Heo struct cpu_workqueue_struct __percpu *pcpu; 247bdbc5dd7STejun Heo struct cpu_workqueue_struct *single; 248bdbc5dd7STejun Heo unsigned long v; 249bdbc5dd7STejun Heo } cpu_wq; /* I: cwq's */ 2504690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 25173f53c4aSTejun Heo 25273f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 25373f53c4aSTejun Heo int work_color; /* F: current work color */ 25473f53c4aSTejun Heo int flush_color; /* F: current flush color */ 25573f53c4aSTejun Heo atomic_t nr_cwqs_to_flush; /* flush in progress */ 25673f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 25773f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 25873f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 25973f53c4aSTejun Heo 260f2e005aaSTejun Heo mayday_mask_t mayday_mask; /* cpus requesting rescue */ 261e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 262e22bee78STejun Heo 2639c5a2ba7STejun Heo int nr_drainers; /* W: drain in progress */ 264dcd989cbSTejun Heo int saved_max_active; /* W: saved cwq max_active */ 2654e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2664e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2674e6045f1SJohannes Berg #endif 268b196be89STejun Heo char name[]; /* I: workqueue name */ 2691da177e4SLinus Torvalds }; 2701da177e4SLinus Torvalds 271d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 272d320c038STejun Heo struct workqueue_struct *system_long_wq __read_mostly; 273d320c038STejun Heo struct workqueue_struct *system_nrt_wq __read_mostly; 274f3421797STejun Heo struct workqueue_struct *system_unbound_wq __read_mostly; 27524d51addSTejun Heo struct workqueue_struct *system_freezable_wq __read_mostly; 27662d3c543SAlan Stern struct workqueue_struct *system_nrt_freezable_wq __read_mostly; 277d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq); 278d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 279d320c038STejun Heo EXPORT_SYMBOL_GPL(system_nrt_wq); 280f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 28124d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 28262d3c543SAlan Stern EXPORT_SYMBOL_GPL(system_nrt_freezable_wq); 283d320c038STejun Heo 28497bd2347STejun Heo #define CREATE_TRACE_POINTS 28597bd2347STejun Heo #include <trace/events/workqueue.h> 28697bd2347STejun Heo 2874ce62e9eSTejun Heo #define for_each_worker_pool(pool, gcwq) \ 2883270476aSTejun Heo for ((pool) = &(gcwq)->pools[0]; \ 2893270476aSTejun Heo (pool) < &(gcwq)->pools[NR_WORKER_POOLS]; (pool)++) 2904ce62e9eSTejun Heo 291db7bccf4STejun Heo #define for_each_busy_worker(worker, i, pos, gcwq) \ 292db7bccf4STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) \ 293db7bccf4STejun Heo hlist_for_each_entry(worker, pos, &gcwq->busy_hash[i], hentry) 294db7bccf4STejun Heo 295f3421797STejun Heo static inline int __next_gcwq_cpu(int cpu, const struct cpumask *mask, 296f3421797STejun Heo unsigned int sw) 297f3421797STejun Heo { 298f3421797STejun Heo if (cpu < nr_cpu_ids) { 299f3421797STejun Heo if (sw & 1) { 300f3421797STejun Heo cpu = cpumask_next(cpu, mask); 301f3421797STejun Heo if (cpu < nr_cpu_ids) 302f3421797STejun Heo return cpu; 303f3421797STejun Heo } 304f3421797STejun Heo if (sw & 2) 305f3421797STejun Heo return WORK_CPU_UNBOUND; 306f3421797STejun Heo } 307f3421797STejun Heo return WORK_CPU_NONE; 308f3421797STejun Heo } 309f3421797STejun Heo 310f3421797STejun Heo static inline int __next_wq_cpu(int cpu, const struct cpumask *mask, 311f3421797STejun Heo struct workqueue_struct *wq) 312f3421797STejun Heo { 313f3421797STejun Heo return __next_gcwq_cpu(cpu, mask, !(wq->flags & WQ_UNBOUND) ? 1 : 2); 314f3421797STejun Heo } 315f3421797STejun Heo 31609884951STejun Heo /* 31709884951STejun Heo * CPU iterators 31809884951STejun Heo * 31909884951STejun Heo * An extra gcwq is defined for an invalid cpu number 32009884951STejun Heo * (WORK_CPU_UNBOUND) to host workqueues which are not bound to any 32109884951STejun Heo * specific CPU. The following iterators are similar to 32209884951STejun Heo * for_each_*_cpu() iterators but also considers the unbound gcwq. 32309884951STejun Heo * 32409884951STejun Heo * for_each_gcwq_cpu() : possible CPUs + WORK_CPU_UNBOUND 32509884951STejun Heo * for_each_online_gcwq_cpu() : online CPUs + WORK_CPU_UNBOUND 32609884951STejun Heo * for_each_cwq_cpu() : possible CPUs for bound workqueues, 32709884951STejun Heo * WORK_CPU_UNBOUND for unbound workqueues 32809884951STejun Heo */ 329f3421797STejun Heo #define for_each_gcwq_cpu(cpu) \ 330f3421797STejun Heo for ((cpu) = __next_gcwq_cpu(-1, cpu_possible_mask, 3); \ 331f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 332f3421797STejun Heo (cpu) = __next_gcwq_cpu((cpu), cpu_possible_mask, 3)) 333f3421797STejun Heo 334f3421797STejun Heo #define for_each_online_gcwq_cpu(cpu) \ 335f3421797STejun Heo for ((cpu) = __next_gcwq_cpu(-1, cpu_online_mask, 3); \ 336f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 337f3421797STejun Heo (cpu) = __next_gcwq_cpu((cpu), cpu_online_mask, 3)) 338f3421797STejun Heo 339f3421797STejun Heo #define for_each_cwq_cpu(cpu, wq) \ 340f3421797STejun Heo for ((cpu) = __next_wq_cpu(-1, cpu_possible_mask, (wq)); \ 341f3421797STejun Heo (cpu) < WORK_CPU_NONE; \ 342f3421797STejun Heo (cpu) = __next_wq_cpu((cpu), cpu_possible_mask, (wq))) 343f3421797STejun Heo 344dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 345dc186ad7SThomas Gleixner 346dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 347dc186ad7SThomas Gleixner 34899777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 34999777288SStanislaw Gruszka { 35099777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 35199777288SStanislaw Gruszka } 35299777288SStanislaw Gruszka 353dc186ad7SThomas Gleixner /* 354dc186ad7SThomas Gleixner * fixup_init is called when: 355dc186ad7SThomas Gleixner * - an active object is initialized 356dc186ad7SThomas Gleixner */ 357dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 358dc186ad7SThomas Gleixner { 359dc186ad7SThomas Gleixner struct work_struct *work = addr; 360dc186ad7SThomas Gleixner 361dc186ad7SThomas Gleixner switch (state) { 362dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 363dc186ad7SThomas Gleixner cancel_work_sync(work); 364dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 365dc186ad7SThomas Gleixner return 1; 366dc186ad7SThomas Gleixner default: 367dc186ad7SThomas Gleixner return 0; 368dc186ad7SThomas Gleixner } 369dc186ad7SThomas Gleixner } 370dc186ad7SThomas Gleixner 371dc186ad7SThomas Gleixner /* 372dc186ad7SThomas Gleixner * fixup_activate is called when: 373dc186ad7SThomas Gleixner * - an active object is activated 374dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 375dc186ad7SThomas Gleixner */ 376dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 377dc186ad7SThomas Gleixner { 378dc186ad7SThomas Gleixner struct work_struct *work = addr; 379dc186ad7SThomas Gleixner 380dc186ad7SThomas Gleixner switch (state) { 381dc186ad7SThomas Gleixner 382dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 383dc186ad7SThomas Gleixner /* 384dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 385dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 386dc186ad7SThomas Gleixner * is tracked in the object tracker. 387dc186ad7SThomas Gleixner */ 38822df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 389dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 390dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 391dc186ad7SThomas Gleixner return 0; 392dc186ad7SThomas Gleixner } 393dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 394dc186ad7SThomas Gleixner return 0; 395dc186ad7SThomas Gleixner 396dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 397dc186ad7SThomas Gleixner WARN_ON(1); 398dc186ad7SThomas Gleixner 399dc186ad7SThomas Gleixner default: 400dc186ad7SThomas Gleixner return 0; 401dc186ad7SThomas Gleixner } 402dc186ad7SThomas Gleixner } 403dc186ad7SThomas Gleixner 404dc186ad7SThomas Gleixner /* 405dc186ad7SThomas Gleixner * fixup_free is called when: 406dc186ad7SThomas Gleixner * - an active object is freed 407dc186ad7SThomas Gleixner */ 408dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 409dc186ad7SThomas Gleixner { 410dc186ad7SThomas Gleixner struct work_struct *work = addr; 411dc186ad7SThomas Gleixner 412dc186ad7SThomas Gleixner switch (state) { 413dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 414dc186ad7SThomas Gleixner cancel_work_sync(work); 415dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 416dc186ad7SThomas Gleixner return 1; 417dc186ad7SThomas Gleixner default: 418dc186ad7SThomas Gleixner return 0; 419dc186ad7SThomas Gleixner } 420dc186ad7SThomas Gleixner } 421dc186ad7SThomas Gleixner 422dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 423dc186ad7SThomas Gleixner .name = "work_struct", 42499777288SStanislaw Gruszka .debug_hint = work_debug_hint, 425dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 426dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 427dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 428dc186ad7SThomas Gleixner }; 429dc186ad7SThomas Gleixner 430dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 431dc186ad7SThomas Gleixner { 432dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 433dc186ad7SThomas Gleixner } 434dc186ad7SThomas Gleixner 435dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 436dc186ad7SThomas Gleixner { 437dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 438dc186ad7SThomas Gleixner } 439dc186ad7SThomas Gleixner 440dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 441dc186ad7SThomas Gleixner { 442dc186ad7SThomas Gleixner if (onstack) 443dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 444dc186ad7SThomas Gleixner else 445dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 446dc186ad7SThomas Gleixner } 447dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 448dc186ad7SThomas Gleixner 449dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 450dc186ad7SThomas Gleixner { 451dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 452dc186ad7SThomas Gleixner } 453dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 454dc186ad7SThomas Gleixner 455dc186ad7SThomas Gleixner #else 456dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 457dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 458dc186ad7SThomas Gleixner #endif 459dc186ad7SThomas Gleixner 46095402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 46195402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 4621da177e4SLinus Torvalds static LIST_HEAD(workqueues); 463a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 4641da177e4SLinus Torvalds 46514441960SOleg Nesterov /* 466e22bee78STejun Heo * The almighty global cpu workqueues. nr_running is the only field 467e22bee78STejun Heo * which is expected to be used frequently by other cpus via 468e22bee78STejun Heo * try_to_wake_up(). Put it in a separate cacheline. 46914441960SOleg Nesterov */ 4708b03ae3cSTejun Heo static DEFINE_PER_CPU(struct global_cwq, global_cwq); 4714ce62e9eSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, pool_nr_running[NR_WORKER_POOLS]); 472f756d5e2SNathan Lynch 473f3421797STejun Heo /* 474f3421797STejun Heo * Global cpu workqueue and nr_running counter for unbound gcwq. The 475f3421797STejun Heo * gcwq is always online, has GCWQ_DISASSOCIATED set, and all its 476f3421797STejun Heo * workers have WORKER_UNBOUND set. 477f3421797STejun Heo */ 478f3421797STejun Heo static struct global_cwq unbound_global_cwq; 4794ce62e9eSTejun Heo static atomic_t unbound_pool_nr_running[NR_WORKER_POOLS] = { 4804ce62e9eSTejun Heo [0 ... NR_WORKER_POOLS - 1] = ATOMIC_INIT(0), /* always 0 */ 4814ce62e9eSTejun Heo }; 482f3421797STejun Heo 483c34056a3STejun Heo static int worker_thread(void *__worker); 4841da177e4SLinus Torvalds 4853270476aSTejun Heo static int worker_pool_pri(struct worker_pool *pool) 4863270476aSTejun Heo { 4873270476aSTejun Heo return pool - pool->gcwq->pools; 4883270476aSTejun Heo } 4893270476aSTejun Heo 4908b03ae3cSTejun Heo static struct global_cwq *get_gcwq(unsigned int cpu) 4911da177e4SLinus Torvalds { 492f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 4938b03ae3cSTejun Heo return &per_cpu(global_cwq, cpu); 494f3421797STejun Heo else 495f3421797STejun Heo return &unbound_global_cwq; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 49863d95a91STejun Heo static atomic_t *get_pool_nr_running(struct worker_pool *pool) 499b1f4ec17SOleg Nesterov { 50063d95a91STejun Heo int cpu = pool->gcwq->cpu; 5013270476aSTejun Heo int idx = worker_pool_pri(pool); 50263d95a91STejun Heo 503f3421797STejun Heo if (cpu != WORK_CPU_UNBOUND) 5044ce62e9eSTejun Heo return &per_cpu(pool_nr_running, cpu)[idx]; 505f3421797STejun Heo else 5064ce62e9eSTejun Heo return &unbound_pool_nr_running[idx]; 507b1f4ec17SOleg Nesterov } 508b1f4ec17SOleg Nesterov 5094690c4abSTejun Heo static struct cpu_workqueue_struct *get_cwq(unsigned int cpu, 5104690c4abSTejun Heo struct workqueue_struct *wq) 511a848e3b6SOleg Nesterov { 512f3421797STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 513e06ffa1eSLai Jiangshan if (likely(cpu < nr_cpu_ids)) 514bdbc5dd7STejun Heo return per_cpu_ptr(wq->cpu_wq.pcpu, cpu); 515f3421797STejun Heo } else if (likely(cpu == WORK_CPU_UNBOUND)) 516f3421797STejun Heo return wq->cpu_wq.single; 517f3421797STejun Heo return NULL; 518f3421797STejun Heo } 519a848e3b6SOleg Nesterov 52073f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 52173f53c4aSTejun Heo { 52273f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 52373f53c4aSTejun Heo } 52473f53c4aSTejun Heo 52573f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 52673f53c4aSTejun Heo { 52773f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 52873f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 52973f53c4aSTejun Heo } 53073f53c4aSTejun Heo 53173f53c4aSTejun Heo static int work_next_color(int color) 53273f53c4aSTejun Heo { 53373f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5364594bf15SDavid Howells /* 537e120153dSTejun Heo * A work's data points to the cwq with WORK_STRUCT_CWQ set while the 538e120153dSTejun Heo * work is on queue. Once execution starts, WORK_STRUCT_CWQ is 539e120153dSTejun Heo * cleared and the work data contains the cpu number it was last on. 5407a22ad75STejun Heo * 5417a22ad75STejun Heo * set_work_{cwq|cpu}() and clear_work_data() can be used to set the 5427a22ad75STejun Heo * cwq, cpu or clear work->data. These functions should only be 5437a22ad75STejun Heo * called while the work is owned - ie. while the PENDING bit is set. 5447a22ad75STejun Heo * 5457a22ad75STejun Heo * get_work_[g]cwq() can be used to obtain the gcwq or cwq 5467a22ad75STejun Heo * corresponding to a work. gcwq is available once the work has been 5477a22ad75STejun Heo * queued anywhere after initialization. cwq is available only from 5487a22ad75STejun Heo * queueing until execution starts. 5494594bf15SDavid Howells */ 5507a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5517a22ad75STejun Heo unsigned long flags) 5527a22ad75STejun Heo { 5537a22ad75STejun Heo BUG_ON(!work_pending(work)); 5547a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5557a22ad75STejun Heo } 5567a22ad75STejun Heo 5577a22ad75STejun Heo static void set_work_cwq(struct work_struct *work, 5584690c4abSTejun Heo struct cpu_workqueue_struct *cwq, 5594690c4abSTejun Heo unsigned long extra_flags) 560365970a1SDavid Howells { 5617a22ad75STejun Heo set_work_data(work, (unsigned long)cwq, 562e120153dSTejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_CWQ | extra_flags); 563365970a1SDavid Howells } 564365970a1SDavid Howells 5657a22ad75STejun Heo static void set_work_cpu(struct work_struct *work, unsigned int cpu) 5664d707b9fSOleg Nesterov { 5677a22ad75STejun Heo set_work_data(work, cpu << WORK_STRUCT_FLAG_BITS, WORK_STRUCT_PENDING); 5684d707b9fSOleg Nesterov } 5694d707b9fSOleg Nesterov 5707a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 571365970a1SDavid Howells { 5727a22ad75STejun Heo set_work_data(work, WORK_STRUCT_NO_CPU, 0); 5737a22ad75STejun Heo } 5747a22ad75STejun Heo 5757a22ad75STejun Heo static struct cpu_workqueue_struct *get_work_cwq(struct work_struct *work) 5767a22ad75STejun Heo { 577e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5787a22ad75STejun Heo 579e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 580e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 581e120153dSTejun Heo else 582e120153dSTejun Heo return NULL; 5837a22ad75STejun Heo } 5847a22ad75STejun Heo 5857a22ad75STejun Heo static struct global_cwq *get_work_gcwq(struct work_struct *work) 5867a22ad75STejun Heo { 587e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5887a22ad75STejun Heo unsigned int cpu; 5897a22ad75STejun Heo 590e120153dSTejun Heo if (data & WORK_STRUCT_CWQ) 591e120153dSTejun Heo return ((struct cpu_workqueue_struct *) 592bd7bdd43STejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool->gcwq; 5937a22ad75STejun Heo 5947a22ad75STejun Heo cpu = data >> WORK_STRUCT_FLAG_BITS; 595bdbc5dd7STejun Heo if (cpu == WORK_CPU_NONE) 5967a22ad75STejun Heo return NULL; 5977a22ad75STejun Heo 598f3421797STejun Heo BUG_ON(cpu >= nr_cpu_ids && cpu != WORK_CPU_UNBOUND); 5997a22ad75STejun Heo return get_gcwq(cpu); 600365970a1SDavid Howells } 601365970a1SDavid Howells 602e22bee78STejun Heo /* 6033270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6043270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 6053270476aSTejun Heo * they're being called with gcwq->lock held. 606e22bee78STejun Heo */ 607e22bee78STejun Heo 60863d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 609649027d7STejun Heo { 6103270476aSTejun Heo return !atomic_read(get_pool_nr_running(pool)); 611649027d7STejun Heo } 612649027d7STejun Heo 613e22bee78STejun Heo /* 614e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 615e22bee78STejun Heo * running workers. 616974271c4STejun Heo * 617974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 618974271c4STejun Heo * function will always return %true for unbound gcwq as long as the 619974271c4STejun Heo * worklist isn't empty. 620e22bee78STejun Heo */ 62163d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 622e22bee78STejun Heo { 62363d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 624e22bee78STejun Heo } 625e22bee78STejun Heo 626e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 62763d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 628e22bee78STejun Heo { 62963d95a91STejun Heo return pool->nr_idle; 630e22bee78STejun Heo } 631e22bee78STejun Heo 632e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 63363d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 634e22bee78STejun Heo { 63563d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 636e22bee78STejun Heo 6373270476aSTejun Heo return !list_empty(&pool->worklist) && atomic_read(nr_running) <= 1; 638e22bee78STejun Heo } 639e22bee78STejun Heo 640e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 64163d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 642e22bee78STejun Heo { 64363d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 644e22bee78STejun Heo } 645e22bee78STejun Heo 646e22bee78STejun Heo /* Do I need to be the manager? */ 64763d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 648e22bee78STejun Heo { 64963d95a91STejun Heo return need_to_create_worker(pool) || 65011ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 651e22bee78STejun Heo } 652e22bee78STejun Heo 653e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 65463d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 655e22bee78STejun Heo { 656*552a37e9SLai Jiangshan bool managing = pool->flags & POOL_MANAGING_WORKERS; 65763d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 65863d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 659e22bee78STejun Heo 660e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 661e22bee78STejun Heo } 662e22bee78STejun Heo 663e22bee78STejun Heo /* 664e22bee78STejun Heo * Wake up functions. 665e22bee78STejun Heo */ 666e22bee78STejun Heo 6677e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 66863d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6697e11629dSTejun Heo { 67063d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6717e11629dSTejun Heo return NULL; 6727e11629dSTejun Heo 67363d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6747e11629dSTejun Heo } 6757e11629dSTejun Heo 6767e11629dSTejun Heo /** 6777e11629dSTejun Heo * wake_up_worker - wake up an idle worker 67863d95a91STejun Heo * @pool: worker pool to wake worker from 6797e11629dSTejun Heo * 68063d95a91STejun Heo * Wake up the first idle worker of @pool. 6817e11629dSTejun Heo * 6827e11629dSTejun Heo * CONTEXT: 6837e11629dSTejun Heo * spin_lock_irq(gcwq->lock). 6847e11629dSTejun Heo */ 68563d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 6867e11629dSTejun Heo { 68763d95a91STejun Heo struct worker *worker = first_worker(pool); 6887e11629dSTejun Heo 6897e11629dSTejun Heo if (likely(worker)) 6907e11629dSTejun Heo wake_up_process(worker->task); 6917e11629dSTejun Heo } 6927e11629dSTejun Heo 6934690c4abSTejun Heo /** 694e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 695e22bee78STejun Heo * @task: task waking up 696e22bee78STejun Heo * @cpu: CPU @task is waking up to 697e22bee78STejun Heo * 698e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 699e22bee78STejun Heo * being awoken. 700e22bee78STejun Heo * 701e22bee78STejun Heo * CONTEXT: 702e22bee78STejun Heo * spin_lock_irq(rq->lock) 703e22bee78STejun Heo */ 704e22bee78STejun Heo void wq_worker_waking_up(struct task_struct *task, unsigned int cpu) 705e22bee78STejun Heo { 706e22bee78STejun Heo struct worker *worker = kthread_data(task); 707e22bee78STejun Heo 7082d64672eSSteven Rostedt if (!(worker->flags & WORKER_NOT_RUNNING)) 70963d95a91STejun Heo atomic_inc(get_pool_nr_running(worker->pool)); 710e22bee78STejun Heo } 711e22bee78STejun Heo 712e22bee78STejun Heo /** 713e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 714e22bee78STejun Heo * @task: task going to sleep 715e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 716e22bee78STejun Heo * 717e22bee78STejun Heo * This function is called during schedule() when a busy worker is 718e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 719e22bee78STejun Heo * returning pointer to its task. 720e22bee78STejun Heo * 721e22bee78STejun Heo * CONTEXT: 722e22bee78STejun Heo * spin_lock_irq(rq->lock) 723e22bee78STejun Heo * 724e22bee78STejun Heo * RETURNS: 725e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 726e22bee78STejun Heo */ 727e22bee78STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, 728e22bee78STejun Heo unsigned int cpu) 729e22bee78STejun Heo { 730e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 731bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 73263d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 733e22bee78STejun Heo 7342d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 735e22bee78STejun Heo return NULL; 736e22bee78STejun Heo 737e22bee78STejun Heo /* this can only happen on the local cpu */ 738e22bee78STejun Heo BUG_ON(cpu != raw_smp_processor_id()); 739e22bee78STejun Heo 740e22bee78STejun Heo /* 741e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 742e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 743e22bee78STejun Heo * Please read comment there. 744e22bee78STejun Heo * 745628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 746628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 747628c78e7STejun Heo * disabled, which in turn means that none else could be 748628c78e7STejun Heo * manipulating idle_list, so dereferencing idle_list without gcwq 749628c78e7STejun Heo * lock is safe. 750e22bee78STejun Heo */ 751bd7bdd43STejun Heo if (atomic_dec_and_test(nr_running) && !list_empty(&pool->worklist)) 75263d95a91STejun Heo to_wakeup = first_worker(pool); 753e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 754e22bee78STejun Heo } 755e22bee78STejun Heo 756e22bee78STejun Heo /** 757e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 758cb444766STejun Heo * @worker: self 759d302f017STejun Heo * @flags: flags to set 760d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 761d302f017STejun Heo * 762e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 763e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 764e22bee78STejun Heo * woken up. 765d302f017STejun Heo * 766cb444766STejun Heo * CONTEXT: 767cb444766STejun Heo * spin_lock_irq(gcwq->lock) 768d302f017STejun Heo */ 769d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 770d302f017STejun Heo bool wakeup) 771d302f017STejun Heo { 772bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 773e22bee78STejun Heo 774cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 775cb444766STejun Heo 776e22bee78STejun Heo /* 777e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 778e22bee78STejun Heo * wake up an idle worker as necessary if requested by 779e22bee78STejun Heo * @wakeup. 780e22bee78STejun Heo */ 781e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 782e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 78363d95a91STejun Heo atomic_t *nr_running = get_pool_nr_running(pool); 784e22bee78STejun Heo 785e22bee78STejun Heo if (wakeup) { 786e22bee78STejun Heo if (atomic_dec_and_test(nr_running) && 787bd7bdd43STejun Heo !list_empty(&pool->worklist)) 78863d95a91STejun Heo wake_up_worker(pool); 789e22bee78STejun Heo } else 790e22bee78STejun Heo atomic_dec(nr_running); 791e22bee78STejun Heo } 792e22bee78STejun Heo 793d302f017STejun Heo worker->flags |= flags; 794d302f017STejun Heo } 795d302f017STejun Heo 796d302f017STejun Heo /** 797e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 798cb444766STejun Heo * @worker: self 799d302f017STejun Heo * @flags: flags to clear 800d302f017STejun Heo * 801e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 802d302f017STejun Heo * 803cb444766STejun Heo * CONTEXT: 804cb444766STejun Heo * spin_lock_irq(gcwq->lock) 805d302f017STejun Heo */ 806d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 807d302f017STejun Heo { 80863d95a91STejun Heo struct worker_pool *pool = worker->pool; 809e22bee78STejun Heo unsigned int oflags = worker->flags; 810e22bee78STejun Heo 811cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 812cb444766STejun Heo 813d302f017STejun Heo worker->flags &= ~flags; 814e22bee78STejun Heo 81542c025f3STejun Heo /* 81642c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 81742c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 81842c025f3STejun Heo * of multiple flags, not a single flag. 81942c025f3STejun Heo */ 820e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 821e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 82263d95a91STejun Heo atomic_inc(get_pool_nr_running(pool)); 823d302f017STejun Heo } 824d302f017STejun Heo 825d302f017STejun Heo /** 826c8e55f36STejun Heo * busy_worker_head - return the busy hash head for a work 827c8e55f36STejun Heo * @gcwq: gcwq of interest 828c8e55f36STejun Heo * @work: work to be hashed 829c8e55f36STejun Heo * 830c8e55f36STejun Heo * Return hash head of @gcwq for @work. 831c8e55f36STejun Heo * 832c8e55f36STejun Heo * CONTEXT: 833c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 834c8e55f36STejun Heo * 835c8e55f36STejun Heo * RETURNS: 836c8e55f36STejun Heo * Pointer to the hash head. 837c8e55f36STejun Heo */ 838c8e55f36STejun Heo static struct hlist_head *busy_worker_head(struct global_cwq *gcwq, 839c8e55f36STejun Heo struct work_struct *work) 840c8e55f36STejun Heo { 841c8e55f36STejun Heo const int base_shift = ilog2(sizeof(struct work_struct)); 842c8e55f36STejun Heo unsigned long v = (unsigned long)work; 843c8e55f36STejun Heo 844c8e55f36STejun Heo /* simple shift and fold hash, do we need something better? */ 845c8e55f36STejun Heo v >>= base_shift; 846c8e55f36STejun Heo v += v >> BUSY_WORKER_HASH_ORDER; 847c8e55f36STejun Heo v &= BUSY_WORKER_HASH_MASK; 848c8e55f36STejun Heo 849c8e55f36STejun Heo return &gcwq->busy_hash[v]; 850c8e55f36STejun Heo } 851c8e55f36STejun Heo 852c8e55f36STejun Heo /** 8538cca0eeaSTejun Heo * __find_worker_executing_work - find worker which is executing a work 8548cca0eeaSTejun Heo * @gcwq: gcwq of interest 8558cca0eeaSTejun Heo * @bwh: hash head as returned by busy_worker_head() 8568cca0eeaSTejun Heo * @work: work to find worker for 8578cca0eeaSTejun Heo * 8588cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. @bwh should be 8598cca0eeaSTejun Heo * the hash head obtained by calling busy_worker_head() with the same 8608cca0eeaSTejun Heo * work. 8618cca0eeaSTejun Heo * 8628cca0eeaSTejun Heo * CONTEXT: 8638cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8648cca0eeaSTejun Heo * 8658cca0eeaSTejun Heo * RETURNS: 8668cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8678cca0eeaSTejun Heo * otherwise. 8688cca0eeaSTejun Heo */ 8698cca0eeaSTejun Heo static struct worker *__find_worker_executing_work(struct global_cwq *gcwq, 8708cca0eeaSTejun Heo struct hlist_head *bwh, 8718cca0eeaSTejun Heo struct work_struct *work) 8728cca0eeaSTejun Heo { 8738cca0eeaSTejun Heo struct worker *worker; 8748cca0eeaSTejun Heo struct hlist_node *tmp; 8758cca0eeaSTejun Heo 8768cca0eeaSTejun Heo hlist_for_each_entry(worker, tmp, bwh, hentry) 8778cca0eeaSTejun Heo if (worker->current_work == work) 8788cca0eeaSTejun Heo return worker; 8798cca0eeaSTejun Heo return NULL; 8808cca0eeaSTejun Heo } 8818cca0eeaSTejun Heo 8828cca0eeaSTejun Heo /** 8838cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 8848cca0eeaSTejun Heo * @gcwq: gcwq of interest 8858cca0eeaSTejun Heo * @work: work to find worker for 8868cca0eeaSTejun Heo * 8878cca0eeaSTejun Heo * Find a worker which is executing @work on @gcwq. This function is 8888cca0eeaSTejun Heo * identical to __find_worker_executing_work() except that this 8898cca0eeaSTejun Heo * function calculates @bwh itself. 8908cca0eeaSTejun Heo * 8918cca0eeaSTejun Heo * CONTEXT: 8928cca0eeaSTejun Heo * spin_lock_irq(gcwq->lock). 8938cca0eeaSTejun Heo * 8948cca0eeaSTejun Heo * RETURNS: 8958cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8968cca0eeaSTejun Heo * otherwise. 8978cca0eeaSTejun Heo */ 8988cca0eeaSTejun Heo static struct worker *find_worker_executing_work(struct global_cwq *gcwq, 8998cca0eeaSTejun Heo struct work_struct *work) 9008cca0eeaSTejun Heo { 9018cca0eeaSTejun Heo return __find_worker_executing_work(gcwq, busy_worker_head(gcwq, work), 9028cca0eeaSTejun Heo work); 9038cca0eeaSTejun Heo } 9048cca0eeaSTejun Heo 9058cca0eeaSTejun Heo /** 9067e11629dSTejun Heo * insert_work - insert a work into gcwq 9074690c4abSTejun Heo * @cwq: cwq @work belongs to 9084690c4abSTejun Heo * @work: work to insert 9094690c4abSTejun Heo * @head: insertion point 9104690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 9114690c4abSTejun Heo * 9127e11629dSTejun Heo * Insert @work which belongs to @cwq into @gcwq after @head. 9137e11629dSTejun Heo * @extra_flags is or'd to work_struct flags. 9144690c4abSTejun Heo * 9154690c4abSTejun Heo * CONTEXT: 9168b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 9171da177e4SLinus Torvalds */ 918b89deed3SOleg Nesterov static void insert_work(struct cpu_workqueue_struct *cwq, 9194690c4abSTejun Heo struct work_struct *work, struct list_head *head, 9204690c4abSTejun Heo unsigned int extra_flags) 921b89deed3SOleg Nesterov { 92263d95a91STejun Heo struct worker_pool *pool = cwq->pool; 923e1d8aa9fSFrederic Weisbecker 9244690c4abSTejun Heo /* we own @work, set data and link */ 9257a22ad75STejun Heo set_work_cwq(work, cwq, extra_flags); 9264690c4abSTejun Heo 9276e84d644SOleg Nesterov /* 9286e84d644SOleg Nesterov * Ensure that we get the right work->data if we see the 9296e84d644SOleg Nesterov * result of list_add() below, see try_to_grab_pending(). 9306e84d644SOleg Nesterov */ 9316e84d644SOleg Nesterov smp_wmb(); 9324690c4abSTejun Heo 9331a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 934e22bee78STejun Heo 935e22bee78STejun Heo /* 936e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 937e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 938e22bee78STejun Heo * lying around lazily while there are works to be processed. 939e22bee78STejun Heo */ 940e22bee78STejun Heo smp_mb(); 941e22bee78STejun Heo 94263d95a91STejun Heo if (__need_more_worker(pool)) 94363d95a91STejun Heo wake_up_worker(pool); 944b89deed3SOleg Nesterov } 945b89deed3SOleg Nesterov 946c8efcc25STejun Heo /* 947c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 948c8efcc25STejun Heo * same workqueue. This is rather expensive and should only be used from 949c8efcc25STejun Heo * cold paths. 950c8efcc25STejun Heo */ 951c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 952c8efcc25STejun Heo { 953c8efcc25STejun Heo unsigned long flags; 954c8efcc25STejun Heo unsigned int cpu; 955c8efcc25STejun Heo 956c8efcc25STejun Heo for_each_gcwq_cpu(cpu) { 957c8efcc25STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 958c8efcc25STejun Heo struct worker *worker; 959c8efcc25STejun Heo struct hlist_node *pos; 960c8efcc25STejun Heo int i; 961c8efcc25STejun Heo 962c8efcc25STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 963c8efcc25STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 964c8efcc25STejun Heo if (worker->task != current) 965c8efcc25STejun Heo continue; 966c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 967c8efcc25STejun Heo /* 968c8efcc25STejun Heo * I'm @worker, no locking necessary. See if @work 969c8efcc25STejun Heo * is headed to the same workqueue. 970c8efcc25STejun Heo */ 971c8efcc25STejun Heo return worker->current_cwq->wq == wq; 972c8efcc25STejun Heo } 973c8efcc25STejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 974c8efcc25STejun Heo } 975c8efcc25STejun Heo return false; 976c8efcc25STejun Heo } 977c8efcc25STejun Heo 9784690c4abSTejun Heo static void __queue_work(unsigned int cpu, struct workqueue_struct *wq, 9791da177e4SLinus Torvalds struct work_struct *work) 9801da177e4SLinus Torvalds { 981502ca9d8STejun Heo struct global_cwq *gcwq; 982502ca9d8STejun Heo struct cpu_workqueue_struct *cwq; 9831e19ffc6STejun Heo struct list_head *worklist; 9848a2e8e5dSTejun Heo unsigned int work_flags; 9851da177e4SLinus Torvalds unsigned long flags; 9861da177e4SLinus Torvalds 987dc186ad7SThomas Gleixner debug_work_activate(work); 9881e19ffc6STejun Heo 989c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 9909c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 991c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 992e41e704bSTejun Heo return; 993e41e704bSTejun Heo 994c7fc77f7STejun Heo /* determine gcwq to use */ 995c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 996c7fc77f7STejun Heo struct global_cwq *last_gcwq; 997c7fc77f7STejun Heo 998f3421797STejun Heo if (unlikely(cpu == WORK_CPU_UNBOUND)) 999f3421797STejun Heo cpu = raw_smp_processor_id(); 1000f3421797STejun Heo 100118aa9effSTejun Heo /* 100218aa9effSTejun Heo * It's multi cpu. If @wq is non-reentrant and @work 100318aa9effSTejun Heo * was previously on a different cpu, it might still 100418aa9effSTejun Heo * be running there, in which case the work needs to 100518aa9effSTejun Heo * be queued on that cpu to guarantee non-reentrance. 100618aa9effSTejun Heo */ 1007502ca9d8STejun Heo gcwq = get_gcwq(cpu); 100818aa9effSTejun Heo if (wq->flags & WQ_NON_REENTRANT && 100918aa9effSTejun Heo (last_gcwq = get_work_gcwq(work)) && last_gcwq != gcwq) { 101018aa9effSTejun Heo struct worker *worker; 101118aa9effSTejun Heo 101218aa9effSTejun Heo spin_lock_irqsave(&last_gcwq->lock, flags); 101318aa9effSTejun Heo 101418aa9effSTejun Heo worker = find_worker_executing_work(last_gcwq, work); 101518aa9effSTejun Heo 101618aa9effSTejun Heo if (worker && worker->current_cwq->wq == wq) 101718aa9effSTejun Heo gcwq = last_gcwq; 101818aa9effSTejun Heo else { 101918aa9effSTejun Heo /* meh... not running there, queue here */ 102018aa9effSTejun Heo spin_unlock_irqrestore(&last_gcwq->lock, flags); 102118aa9effSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 102218aa9effSTejun Heo } 102318aa9effSTejun Heo } else 10248b03ae3cSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1025f3421797STejun Heo } else { 1026f3421797STejun Heo gcwq = get_gcwq(WORK_CPU_UNBOUND); 1027f3421797STejun Heo spin_lock_irqsave(&gcwq->lock, flags); 1028502ca9d8STejun Heo } 1029502ca9d8STejun Heo 1030502ca9d8STejun Heo /* gcwq determined, get cwq and queue */ 1031502ca9d8STejun Heo cwq = get_cwq(gcwq->cpu, wq); 1032cdadf009STejun Heo trace_workqueue_queue_work(cpu, cwq, work); 1033502ca9d8STejun Heo 1034f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1035f5b2552bSDan Carpenter spin_unlock_irqrestore(&gcwq->lock, flags); 1036f5b2552bSDan Carpenter return; 1037f5b2552bSDan Carpenter } 10381e19ffc6STejun Heo 103973f53c4aSTejun Heo cwq->nr_in_flight[cwq->work_color]++; 10408a2e8e5dSTejun Heo work_flags = work_color_to_flags(cwq->work_color); 10411e19ffc6STejun Heo 10421e19ffc6STejun Heo if (likely(cwq->nr_active < cwq->max_active)) { 1043cdadf009STejun Heo trace_workqueue_activate_work(work); 10441e19ffc6STejun Heo cwq->nr_active++; 10453270476aSTejun Heo worklist = &cwq->pool->worklist; 10468a2e8e5dSTejun Heo } else { 10478a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 10481e19ffc6STejun Heo worklist = &cwq->delayed_works; 10498a2e8e5dSTejun Heo } 10501e19ffc6STejun Heo 10518a2e8e5dSTejun Heo insert_work(cwq, work, worklist, work_flags); 10521e19ffc6STejun Heo 10538b03ae3cSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 10541da177e4SLinus Torvalds } 10551da177e4SLinus Torvalds 10560fcb78c2SRolf Eike Beer /** 10570fcb78c2SRolf Eike Beer * queue_work - queue work on a workqueue 10580fcb78c2SRolf Eike Beer * @wq: workqueue to use 10590fcb78c2SRolf Eike Beer * @work: work to queue 10600fcb78c2SRolf Eike Beer * 1061057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 10621da177e4SLinus Torvalds * 106300dfcaf7SOleg Nesterov * We queue the work to the CPU on which it was submitted, but if the CPU dies 106400dfcaf7SOleg Nesterov * it can be processed by another CPU. 10651da177e4SLinus Torvalds */ 10667ad5b3a5SHarvey Harrison int queue_work(struct workqueue_struct *wq, struct work_struct *work) 10671da177e4SLinus Torvalds { 1068ef1ca236SOleg Nesterov int ret; 10691da177e4SLinus Torvalds 1070ef1ca236SOleg Nesterov ret = queue_work_on(get_cpu(), wq, work); 1071a848e3b6SOleg Nesterov put_cpu(); 1072ef1ca236SOleg Nesterov 10731da177e4SLinus Torvalds return ret; 10741da177e4SLinus Torvalds } 1075ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_work); 10761da177e4SLinus Torvalds 1077c1a220e7SZhang Rui /** 1078c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1079c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1080c1a220e7SZhang Rui * @wq: workqueue to use 1081c1a220e7SZhang Rui * @work: work to queue 1082c1a220e7SZhang Rui * 1083c1a220e7SZhang Rui * Returns 0 if @work was already on a queue, non-zero otherwise. 1084c1a220e7SZhang Rui * 1085c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1086c1a220e7SZhang Rui * can't go away. 1087c1a220e7SZhang Rui */ 1088c1a220e7SZhang Rui int 1089c1a220e7SZhang Rui queue_work_on(int cpu, struct workqueue_struct *wq, struct work_struct *work) 1090c1a220e7SZhang Rui { 1091c1a220e7SZhang Rui int ret = 0; 1092c1a220e7SZhang Rui 109322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 10944690c4abSTejun Heo __queue_work(cpu, wq, work); 1095c1a220e7SZhang Rui ret = 1; 1096c1a220e7SZhang Rui } 1097c1a220e7SZhang Rui return ret; 1098c1a220e7SZhang Rui } 1099c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1100c1a220e7SZhang Rui 11016d141c3fSLi Zefan static void delayed_work_timer_fn(unsigned long __data) 11021da177e4SLinus Torvalds { 110352bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 11047a22ad75STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(&dwork->work); 11051da177e4SLinus Torvalds 11064690c4abSTejun Heo __queue_work(smp_processor_id(), cwq->wq, &dwork->work); 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11090fcb78c2SRolf Eike Beer /** 11100fcb78c2SRolf Eike Beer * queue_delayed_work - queue work on a workqueue after delay 11110fcb78c2SRolf Eike Beer * @wq: workqueue to use 1112af9997e4SRandy Dunlap * @dwork: delayable work to queue 11130fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11140fcb78c2SRolf Eike Beer * 1115057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 11160fcb78c2SRolf Eike Beer */ 11177ad5b3a5SHarvey Harrison int queue_delayed_work(struct workqueue_struct *wq, 111852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11191da177e4SLinus Torvalds { 112052bad64dSDavid Howells if (delay == 0) 112163bc0362SOleg Nesterov return queue_work(wq, &dwork->work); 11221da177e4SLinus Torvalds 112363bc0362SOleg Nesterov return queue_delayed_work_on(-1, wq, dwork, delay); 11241da177e4SLinus Torvalds } 1125ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work); 11261da177e4SLinus Torvalds 11270fcb78c2SRolf Eike Beer /** 11280fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 11290fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 11300fcb78c2SRolf Eike Beer * @wq: workqueue to use 1131af9997e4SRandy Dunlap * @dwork: work to queue 11320fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 11330fcb78c2SRolf Eike Beer * 1134057647fcSAlan Stern * Returns 0 if @work was already on a queue, non-zero otherwise. 11350fcb78c2SRolf Eike Beer */ 11367a6bc1cdSVenkatesh Pallipadi int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 113752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 11387a6bc1cdSVenkatesh Pallipadi { 11397a6bc1cdSVenkatesh Pallipadi int ret = 0; 114052bad64dSDavid Howells struct timer_list *timer = &dwork->timer; 114152bad64dSDavid Howells struct work_struct *work = &dwork->work; 11427a6bc1cdSVenkatesh Pallipadi 114322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1144c7fc77f7STejun Heo unsigned int lcpu; 11457a22ad75STejun Heo 11467a6bc1cdSVenkatesh Pallipadi BUG_ON(timer_pending(timer)); 11477a6bc1cdSVenkatesh Pallipadi BUG_ON(!list_empty(&work->entry)); 11487a6bc1cdSVenkatesh Pallipadi 11498a3e77ccSAndrew Liu timer_stats_timer_set_start_info(&dwork->timer); 11508a3e77ccSAndrew Liu 11517a22ad75STejun Heo /* 11527a22ad75STejun Heo * This stores cwq for the moment, for the timer_fn. 11537a22ad75STejun Heo * Note that the work's gcwq is preserved to allow 11547a22ad75STejun Heo * reentrance detection for delayed works. 11557a22ad75STejun Heo */ 1156c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1157c7fc77f7STejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 1158c7fc77f7STejun Heo 1159c7fc77f7STejun Heo if (gcwq && gcwq->cpu != WORK_CPU_UNBOUND) 1160c7fc77f7STejun Heo lcpu = gcwq->cpu; 1161c7fc77f7STejun Heo else 1162c7fc77f7STejun Heo lcpu = raw_smp_processor_id(); 1163c7fc77f7STejun Heo } else 1164c7fc77f7STejun Heo lcpu = WORK_CPU_UNBOUND; 1165c7fc77f7STejun Heo 11667a22ad75STejun Heo set_work_cwq(work, get_cwq(lcpu, wq), 0); 1167c7fc77f7STejun Heo 11687a6bc1cdSVenkatesh Pallipadi timer->expires = jiffies + delay; 116952bad64dSDavid Howells timer->data = (unsigned long)dwork; 11707a6bc1cdSVenkatesh Pallipadi timer->function = delayed_work_timer_fn; 117163bc0362SOleg Nesterov 117263bc0362SOleg Nesterov if (unlikely(cpu >= 0)) 11737a6bc1cdSVenkatesh Pallipadi add_timer_on(timer, cpu); 117463bc0362SOleg Nesterov else 117563bc0362SOleg Nesterov add_timer(timer); 11767a6bc1cdSVenkatesh Pallipadi ret = 1; 11777a6bc1cdSVenkatesh Pallipadi } 11787a6bc1cdSVenkatesh Pallipadi return ret; 11797a6bc1cdSVenkatesh Pallipadi } 1180ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 11811da177e4SLinus Torvalds 1182c8e55f36STejun Heo /** 1183c8e55f36STejun Heo * worker_enter_idle - enter idle state 1184c8e55f36STejun Heo * @worker: worker which is entering idle state 1185c8e55f36STejun Heo * 1186c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1187c8e55f36STejun Heo * necessary. 1188c8e55f36STejun Heo * 1189c8e55f36STejun Heo * LOCKING: 1190c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1191c8e55f36STejun Heo */ 1192c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 11931da177e4SLinus Torvalds { 1194bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1195bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1196c8e55f36STejun Heo 1197c8e55f36STejun Heo BUG_ON(worker->flags & WORKER_IDLE); 1198c8e55f36STejun Heo BUG_ON(!list_empty(&worker->entry) && 1199c8e55f36STejun Heo (worker->hentry.next || worker->hentry.pprev)); 1200c8e55f36STejun Heo 1201cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1202cb444766STejun Heo worker->flags |= WORKER_IDLE; 1203bd7bdd43STejun Heo pool->nr_idle++; 1204e22bee78STejun Heo worker->last_active = jiffies; 1205c8e55f36STejun Heo 1206c8e55f36STejun Heo /* idle_list is LIFO */ 1207bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1208db7bccf4STejun Heo 120963d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1210628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1211cb444766STejun Heo 1212544ecf31STejun Heo /* 1213628c78e7STejun Heo * Sanity check nr_running. Because gcwq_unbind_fn() releases 1214628c78e7STejun Heo * gcwq->lock between setting %WORKER_UNBOUND and zapping 1215628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1216628c78e7STejun Heo * unbind is not in progress. 1217544ecf31STejun Heo */ 1218628c78e7STejun Heo WARN_ON_ONCE(!(gcwq->flags & GCWQ_DISASSOCIATED) && 1219bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 122063d95a91STejun Heo atomic_read(get_pool_nr_running(pool))); 1221c8e55f36STejun Heo } 1222c8e55f36STejun Heo 1223c8e55f36STejun Heo /** 1224c8e55f36STejun Heo * worker_leave_idle - leave idle state 1225c8e55f36STejun Heo * @worker: worker which is leaving idle state 1226c8e55f36STejun Heo * 1227c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1228c8e55f36STejun Heo * 1229c8e55f36STejun Heo * LOCKING: 1230c8e55f36STejun Heo * spin_lock_irq(gcwq->lock). 1231c8e55f36STejun Heo */ 1232c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1233c8e55f36STejun Heo { 1234bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1235c8e55f36STejun Heo 1236c8e55f36STejun Heo BUG_ON(!(worker->flags & WORKER_IDLE)); 1237d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1238bd7bdd43STejun Heo pool->nr_idle--; 1239c8e55f36STejun Heo list_del_init(&worker->entry); 1240c8e55f36STejun Heo } 1241c8e55f36STejun Heo 1242e22bee78STejun Heo /** 1243e22bee78STejun Heo * worker_maybe_bind_and_lock - bind worker to its cpu if possible and lock gcwq 1244e22bee78STejun Heo * @worker: self 1245e22bee78STejun Heo * 1246e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1247e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1248e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1249e22bee78STejun Heo * guaranteed to execute on the cpu. 1250e22bee78STejun Heo * 1251e22bee78STejun Heo * This function is to be used by rogue workers and rescuers to bind 1252e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1253e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1254e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1255e22bee78STejun Heo * verbatim as it's best effort and blocking and gcwq may be 1256e22bee78STejun Heo * [dis]associated in the meantime. 1257e22bee78STejun Heo * 1258f2d5a0eeSTejun Heo * This function tries set_cpus_allowed() and locks gcwq and verifies the 1259f2d5a0eeSTejun Heo * binding against %GCWQ_DISASSOCIATED which is set during 1260f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1261f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1262f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1263e22bee78STejun Heo * 1264e22bee78STejun Heo * CONTEXT: 1265e22bee78STejun Heo * Might sleep. Called without any lock but returns with gcwq->lock 1266e22bee78STejun Heo * held. 1267e22bee78STejun Heo * 1268e22bee78STejun Heo * RETURNS: 1269e22bee78STejun Heo * %true if the associated gcwq is online (@worker is successfully 1270e22bee78STejun Heo * bound), %false if offline. 1271e22bee78STejun Heo */ 1272e22bee78STejun Heo static bool worker_maybe_bind_and_lock(struct worker *worker) 1273972fa1c5SNamhyung Kim __acquires(&gcwq->lock) 1274e22bee78STejun Heo { 1275bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1276e22bee78STejun Heo struct task_struct *task = worker->task; 1277e22bee78STejun Heo 1278e22bee78STejun Heo while (true) { 1279e22bee78STejun Heo /* 1280e22bee78STejun Heo * The following call may fail, succeed or succeed 1281e22bee78STejun Heo * without actually migrating the task to the cpu if 1282e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 1283e22bee78STejun Heo * against GCWQ_DISASSOCIATED. 1284e22bee78STejun Heo */ 1285f3421797STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) 1286e22bee78STejun Heo set_cpus_allowed_ptr(task, get_cpu_mask(gcwq->cpu)); 1287e22bee78STejun Heo 1288e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1289e22bee78STejun Heo if (gcwq->flags & GCWQ_DISASSOCIATED) 1290e22bee78STejun Heo return false; 1291e22bee78STejun Heo if (task_cpu(task) == gcwq->cpu && 1292e22bee78STejun Heo cpumask_equal(¤t->cpus_allowed, 1293e22bee78STejun Heo get_cpu_mask(gcwq->cpu))) 1294e22bee78STejun Heo return true; 1295e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1296e22bee78STejun Heo 12975035b20fSTejun Heo /* 12985035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 12995035b20fSTejun Heo * and retry migration. cond_resched() is required here; 13005035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 13015035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 13025035b20fSTejun Heo */ 1303e22bee78STejun Heo cpu_relax(); 13045035b20fSTejun Heo cond_resched(); 1305e22bee78STejun Heo } 1306e22bee78STejun Heo } 1307e22bee78STejun Heo 130825511a47STejun Heo struct idle_rebind { 130925511a47STejun Heo int cnt; /* # workers to be rebound */ 131025511a47STejun Heo struct completion done; /* all workers rebound */ 131125511a47STejun Heo }; 131225511a47STejun Heo 1313e22bee78STejun Heo /* 131425511a47STejun Heo * Rebind an idle @worker to its CPU. During CPU onlining, this has to 131525511a47STejun Heo * happen synchronously for idle workers. worker_thread() will test 131625511a47STejun Heo * %WORKER_REBIND before leaving idle and call this function. 131725511a47STejun Heo */ 131825511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 131925511a47STejun Heo { 132025511a47STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 132125511a47STejun Heo 132225511a47STejun Heo /* CPU must be online at this point */ 132325511a47STejun Heo WARN_ON(!worker_maybe_bind_and_lock(worker)); 132425511a47STejun Heo if (!--worker->idle_rebind->cnt) 132525511a47STejun Heo complete(&worker->idle_rebind->done); 132625511a47STejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 132725511a47STejun Heo 132825511a47STejun Heo /* we did our part, wait for rebind_workers() to finish up */ 132925511a47STejun Heo wait_event(gcwq->rebind_hold, !(worker->flags & WORKER_REBIND)); 1330ec58815aSTejun Heo 1331ec58815aSTejun Heo /* 1332ec58815aSTejun Heo * rebind_workers() shouldn't finish until all workers passed the 1333ec58815aSTejun Heo * above WORKER_REBIND wait. Tell it when done. 1334ec58815aSTejun Heo */ 1335ec58815aSTejun Heo spin_lock_irq(&worker->pool->gcwq->lock); 1336ec58815aSTejun Heo if (!--worker->idle_rebind->cnt) 1337ec58815aSTejun Heo complete(&worker->idle_rebind->done); 1338ec58815aSTejun Heo spin_unlock_irq(&worker->pool->gcwq->lock); 133925511a47STejun Heo } 134025511a47STejun Heo 134125511a47STejun Heo /* 134225511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1343403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1344403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1345403c821dSTejun Heo * executed twice without intervening cpu down. 1346e22bee78STejun Heo */ 134725511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1348e22bee78STejun Heo { 1349e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1350bd7bdd43STejun Heo struct global_cwq *gcwq = worker->pool->gcwq; 1351e22bee78STejun Heo 1352e22bee78STejun Heo if (worker_maybe_bind_and_lock(worker)) 1353e22bee78STejun Heo worker_clr_flags(worker, WORKER_REBIND); 1354e22bee78STejun Heo 1355e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1356e22bee78STejun Heo } 1357e22bee78STejun Heo 135825511a47STejun Heo /** 135925511a47STejun Heo * rebind_workers - rebind all workers of a gcwq to the associated CPU 136025511a47STejun Heo * @gcwq: gcwq of interest 136125511a47STejun Heo * 136225511a47STejun Heo * @gcwq->cpu is coming online. Rebind all workers to the CPU. Rebinding 136325511a47STejun Heo * is different for idle and busy ones. 136425511a47STejun Heo * 136525511a47STejun Heo * The idle ones should be rebound synchronously and idle rebinding should 136625511a47STejun Heo * be complete before any worker starts executing work items with 136725511a47STejun Heo * concurrency management enabled; otherwise, scheduler may oops trying to 136825511a47STejun Heo * wake up non-local idle worker from wq_worker_sleeping(). 136925511a47STejun Heo * 137025511a47STejun Heo * This is achieved by repeatedly requesting rebinding until all idle 137125511a47STejun Heo * workers are known to have been rebound under @gcwq->lock and holding all 137225511a47STejun Heo * idle workers from becoming busy until idle rebinding is complete. 137325511a47STejun Heo * 137425511a47STejun Heo * Once idle workers are rebound, busy workers can be rebound as they 137525511a47STejun Heo * finish executing their current work items. Queueing the rebind work at 137625511a47STejun Heo * the head of their scheduled lists is enough. Note that nr_running will 137725511a47STejun Heo * be properbly bumped as busy workers rebind. 137825511a47STejun Heo * 137925511a47STejun Heo * On return, all workers are guaranteed to either be bound or have rebind 138025511a47STejun Heo * work item scheduled. 138125511a47STejun Heo */ 138225511a47STejun Heo static void rebind_workers(struct global_cwq *gcwq) 138325511a47STejun Heo __releases(&gcwq->lock) __acquires(&gcwq->lock) 138425511a47STejun Heo { 138525511a47STejun Heo struct idle_rebind idle_rebind; 138625511a47STejun Heo struct worker_pool *pool; 138725511a47STejun Heo struct worker *worker; 138825511a47STejun Heo struct hlist_node *pos; 138925511a47STejun Heo int i; 139025511a47STejun Heo 139125511a47STejun Heo lockdep_assert_held(&gcwq->lock); 139225511a47STejun Heo 139325511a47STejun Heo for_each_worker_pool(pool, gcwq) 139425511a47STejun Heo lockdep_assert_held(&pool->manager_mutex); 139525511a47STejun Heo 139625511a47STejun Heo /* 139725511a47STejun Heo * Rebind idle workers. Interlocked both ways. We wait for 139825511a47STejun Heo * workers to rebind via @idle_rebind.done. Workers will wait for 139925511a47STejun Heo * us to finish up by watching %WORKER_REBIND. 140025511a47STejun Heo */ 140125511a47STejun Heo init_completion(&idle_rebind.done); 140225511a47STejun Heo retry: 140325511a47STejun Heo idle_rebind.cnt = 1; 140425511a47STejun Heo INIT_COMPLETION(idle_rebind.done); 140525511a47STejun Heo 140625511a47STejun Heo /* set REBIND and kick idle ones, we'll wait for these later */ 140725511a47STejun Heo for_each_worker_pool(pool, gcwq) { 140825511a47STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 140996e65306SLai Jiangshan unsigned long worker_flags = worker->flags; 141096e65306SLai Jiangshan 141125511a47STejun Heo if (worker->flags & WORKER_REBIND) 141225511a47STejun Heo continue; 141325511a47STejun Heo 141496e65306SLai Jiangshan /* morph UNBOUND to REBIND atomically */ 141596e65306SLai Jiangshan worker_flags &= ~WORKER_UNBOUND; 141696e65306SLai Jiangshan worker_flags |= WORKER_REBIND; 141796e65306SLai Jiangshan ACCESS_ONCE(worker->flags) = worker_flags; 141825511a47STejun Heo 141925511a47STejun Heo idle_rebind.cnt++; 142025511a47STejun Heo worker->idle_rebind = &idle_rebind; 142125511a47STejun Heo 142225511a47STejun Heo /* worker_thread() will call idle_worker_rebind() */ 142325511a47STejun Heo wake_up_process(worker->task); 142425511a47STejun Heo } 142525511a47STejun Heo } 142625511a47STejun Heo 142725511a47STejun Heo if (--idle_rebind.cnt) { 142825511a47STejun Heo spin_unlock_irq(&gcwq->lock); 142925511a47STejun Heo wait_for_completion(&idle_rebind.done); 143025511a47STejun Heo spin_lock_irq(&gcwq->lock); 143125511a47STejun Heo /* busy ones might have become idle while waiting, retry */ 143225511a47STejun Heo goto retry; 143325511a47STejun Heo } 143425511a47STejun Heo 143590beca5dSTejun Heo /* all idle workers are rebound, rebind busy workers */ 143625511a47STejun Heo for_each_busy_worker(worker, i, pos, gcwq) { 143725511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 143896e65306SLai Jiangshan unsigned long worker_flags = worker->flags; 143925511a47STejun Heo 144096e65306SLai Jiangshan /* morph UNBOUND to REBIND atomically */ 144196e65306SLai Jiangshan worker_flags &= ~WORKER_UNBOUND; 144296e65306SLai Jiangshan worker_flags |= WORKER_REBIND; 144396e65306SLai Jiangshan ACCESS_ONCE(worker->flags) = worker_flags; 144425511a47STejun Heo 144525511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 144625511a47STejun Heo work_data_bits(rebind_work))) 144725511a47STejun Heo continue; 144825511a47STejun Heo 144925511a47STejun Heo /* wq doesn't matter, use the default one */ 145025511a47STejun Heo debug_work_activate(rebind_work); 145125511a47STejun Heo insert_work(get_cwq(gcwq->cpu, system_wq), rebind_work, 145225511a47STejun Heo worker->scheduled.next, 145325511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 145425511a47STejun Heo } 145590beca5dSTejun Heo 145690beca5dSTejun Heo /* 145790beca5dSTejun Heo * All idle workers are rebound and waiting for %WORKER_REBIND to 145890beca5dSTejun Heo * be cleared inside idle_worker_rebind(). Clear and release. 145990beca5dSTejun Heo * Clearing %WORKER_REBIND from this foreign context is safe 146090beca5dSTejun Heo * because these workers are still guaranteed to be idle. 1461ec58815aSTejun Heo * 1462ec58815aSTejun Heo * We need to make sure all idle workers passed WORKER_REBIND wait 1463ec58815aSTejun Heo * in idle_worker_rebind() before returning; otherwise, workers can 1464ec58815aSTejun Heo * get stuck at the wait if hotplug cycle repeats. 146590beca5dSTejun Heo */ 1466ec58815aSTejun Heo idle_rebind.cnt = 1; 1467ec58815aSTejun Heo INIT_COMPLETION(idle_rebind.done); 1468ec58815aSTejun Heo 1469ec58815aSTejun Heo for_each_worker_pool(pool, gcwq) { 1470ec58815aSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 147190beca5dSTejun Heo worker->flags &= ~WORKER_REBIND; 1472ec58815aSTejun Heo idle_rebind.cnt++; 1473ec58815aSTejun Heo } 1474ec58815aSTejun Heo } 147590beca5dSTejun Heo 147690beca5dSTejun Heo wake_up_all(&gcwq->rebind_hold); 1477ec58815aSTejun Heo 1478ec58815aSTejun Heo if (--idle_rebind.cnt) { 1479ec58815aSTejun Heo spin_unlock_irq(&gcwq->lock); 1480ec58815aSTejun Heo wait_for_completion(&idle_rebind.done); 1481ec58815aSTejun Heo spin_lock_irq(&gcwq->lock); 1482ec58815aSTejun Heo } 148325511a47STejun Heo } 148425511a47STejun Heo 1485c34056a3STejun Heo static struct worker *alloc_worker(void) 1486c34056a3STejun Heo { 1487c34056a3STejun Heo struct worker *worker; 1488c34056a3STejun Heo 1489c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1490c8e55f36STejun Heo if (worker) { 1491c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1492affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 149325511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1494e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1495e22bee78STejun Heo worker->flags = WORKER_PREP; 1496c8e55f36STejun Heo } 1497c34056a3STejun Heo return worker; 1498c34056a3STejun Heo } 1499c34056a3STejun Heo 1500c34056a3STejun Heo /** 1501c34056a3STejun Heo * create_worker - create a new workqueue worker 150263d95a91STejun Heo * @pool: pool the new worker will belong to 1503c34056a3STejun Heo * 150463d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1505c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1506c34056a3STejun Heo * destroy_worker(). 1507c34056a3STejun Heo * 1508c34056a3STejun Heo * CONTEXT: 1509c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1510c34056a3STejun Heo * 1511c34056a3STejun Heo * RETURNS: 1512c34056a3STejun Heo * Pointer to the newly created worker. 1513c34056a3STejun Heo */ 1514bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1515c34056a3STejun Heo { 151663d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 15173270476aSTejun Heo const char *pri = worker_pool_pri(pool) ? "H" : ""; 1518c34056a3STejun Heo struct worker *worker = NULL; 1519f3421797STejun Heo int id = -1; 1520c34056a3STejun Heo 15218b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1522bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 15238b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1524bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1525c34056a3STejun Heo goto fail; 15268b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1527c34056a3STejun Heo } 15288b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1529c34056a3STejun Heo 1530c34056a3STejun Heo worker = alloc_worker(); 1531c34056a3STejun Heo if (!worker) 1532c34056a3STejun Heo goto fail; 1533c34056a3STejun Heo 1534bd7bdd43STejun Heo worker->pool = pool; 1535c34056a3STejun Heo worker->id = id; 1536c34056a3STejun Heo 1537bc2ae0f5STejun Heo if (gcwq->cpu != WORK_CPU_UNBOUND) 153894dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 15393270476aSTejun Heo worker, cpu_to_node(gcwq->cpu), 15403270476aSTejun Heo "kworker/%u:%d%s", gcwq->cpu, id, pri); 1541f3421797STejun Heo else 1542f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 15433270476aSTejun Heo "kworker/u:%d%s", id, pri); 1544c34056a3STejun Heo if (IS_ERR(worker->task)) 1545c34056a3STejun Heo goto fail; 1546c34056a3STejun Heo 15473270476aSTejun Heo if (worker_pool_pri(pool)) 15483270476aSTejun Heo set_user_nice(worker->task, HIGHPRI_NICE_LEVEL); 15493270476aSTejun Heo 1550db7bccf4STejun Heo /* 1551bc2ae0f5STejun Heo * Determine CPU binding of the new worker depending on 1552bc2ae0f5STejun Heo * %GCWQ_DISASSOCIATED. The caller is responsible for ensuring the 1553bc2ae0f5STejun Heo * flag remains stable across this function. See the comments 1554bc2ae0f5STejun Heo * above the flag definition for details. 1555bc2ae0f5STejun Heo * 1556bc2ae0f5STejun Heo * As an unbound worker may later become a regular one if CPU comes 1557bc2ae0f5STejun Heo * online, make sure every worker has %PF_THREAD_BOUND set. 1558db7bccf4STejun Heo */ 1559bc2ae0f5STejun Heo if (!(gcwq->flags & GCWQ_DISASSOCIATED)) { 15608b03ae3cSTejun Heo kthread_bind(worker->task, gcwq->cpu); 1561bc2ae0f5STejun Heo } else { 1562db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 1563f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1564f3421797STejun Heo } 1565c34056a3STejun Heo 1566c34056a3STejun Heo return worker; 1567c34056a3STejun Heo fail: 1568c34056a3STejun Heo if (id >= 0) { 15698b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1570bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 15718b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 1572c34056a3STejun Heo } 1573c34056a3STejun Heo kfree(worker); 1574c34056a3STejun Heo return NULL; 1575c34056a3STejun Heo } 1576c34056a3STejun Heo 1577c34056a3STejun Heo /** 1578c34056a3STejun Heo * start_worker - start a newly created worker 1579c34056a3STejun Heo * @worker: worker to start 1580c34056a3STejun Heo * 1581c8e55f36STejun Heo * Make the gcwq aware of @worker and start it. 1582c34056a3STejun Heo * 1583c34056a3STejun Heo * CONTEXT: 15848b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1585c34056a3STejun Heo */ 1586c34056a3STejun Heo static void start_worker(struct worker *worker) 1587c34056a3STejun Heo { 1588cb444766STejun Heo worker->flags |= WORKER_STARTED; 1589bd7bdd43STejun Heo worker->pool->nr_workers++; 1590c8e55f36STejun Heo worker_enter_idle(worker); 1591c34056a3STejun Heo wake_up_process(worker->task); 1592c34056a3STejun Heo } 1593c34056a3STejun Heo 1594c34056a3STejun Heo /** 1595c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1596c34056a3STejun Heo * @worker: worker to be destroyed 1597c34056a3STejun Heo * 1598c8e55f36STejun Heo * Destroy @worker and adjust @gcwq stats accordingly. 1599c8e55f36STejun Heo * 1600c8e55f36STejun Heo * CONTEXT: 1601c8e55f36STejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1602c34056a3STejun Heo */ 1603c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1604c34056a3STejun Heo { 1605bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1606bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1607c34056a3STejun Heo int id = worker->id; 1608c34056a3STejun Heo 1609c34056a3STejun Heo /* sanity check frenzy */ 1610c34056a3STejun Heo BUG_ON(worker->current_work); 1611affee4b2STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 1612c34056a3STejun Heo 1613c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1614bd7bdd43STejun Heo pool->nr_workers--; 1615c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1616bd7bdd43STejun Heo pool->nr_idle--; 1617c8e55f36STejun Heo 1618c8e55f36STejun Heo list_del_init(&worker->entry); 1619cb444766STejun Heo worker->flags |= WORKER_DIE; 1620c8e55f36STejun Heo 1621c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 1622c8e55f36STejun Heo 1623c34056a3STejun Heo kthread_stop(worker->task); 1624c34056a3STejun Heo kfree(worker); 1625c34056a3STejun Heo 16268b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 1627bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1628c34056a3STejun Heo } 1629c34056a3STejun Heo 163063d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1631e22bee78STejun Heo { 163263d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 163363d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1634e22bee78STejun Heo 1635e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1636e22bee78STejun Heo 163763d95a91STejun Heo if (too_many_workers(pool)) { 1638e22bee78STejun Heo struct worker *worker; 1639e22bee78STejun Heo unsigned long expires; 1640e22bee78STejun Heo 1641e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 164263d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1643e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1644e22bee78STejun Heo 1645e22bee78STejun Heo if (time_before(jiffies, expires)) 164663d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1647e22bee78STejun Heo else { 1648e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 164911ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 165063d95a91STejun Heo wake_up_worker(pool); 1651e22bee78STejun Heo } 1652e22bee78STejun Heo } 1653e22bee78STejun Heo 1654e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1655e22bee78STejun Heo } 1656e22bee78STejun Heo 1657e22bee78STejun Heo static bool send_mayday(struct work_struct *work) 1658e22bee78STejun Heo { 1659e22bee78STejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1660e22bee78STejun Heo struct workqueue_struct *wq = cwq->wq; 1661f3421797STejun Heo unsigned int cpu; 1662e22bee78STejun Heo 1663e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1664e22bee78STejun Heo return false; 1665e22bee78STejun Heo 1666e22bee78STejun Heo /* mayday mayday mayday */ 1667bd7bdd43STejun Heo cpu = cwq->pool->gcwq->cpu; 1668f3421797STejun Heo /* WORK_CPU_UNBOUND can't be set in cpumask, use cpu 0 instead */ 1669f3421797STejun Heo if (cpu == WORK_CPU_UNBOUND) 1670f3421797STejun Heo cpu = 0; 1671f2e005aaSTejun Heo if (!mayday_test_and_set_cpu(cpu, wq->mayday_mask)) 1672e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1673e22bee78STejun Heo return true; 1674e22bee78STejun Heo } 1675e22bee78STejun Heo 167663d95a91STejun Heo static void gcwq_mayday_timeout(unsigned long __pool) 1677e22bee78STejun Heo { 167863d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 167963d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 1680e22bee78STejun Heo struct work_struct *work; 1681e22bee78STejun Heo 1682e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1683e22bee78STejun Heo 168463d95a91STejun Heo if (need_to_create_worker(pool)) { 1685e22bee78STejun Heo /* 1686e22bee78STejun Heo * We've been trying to create a new worker but 1687e22bee78STejun Heo * haven't been successful. We might be hitting an 1688e22bee78STejun Heo * allocation deadlock. Send distress signals to 1689e22bee78STejun Heo * rescuers. 1690e22bee78STejun Heo */ 169163d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1692e22bee78STejun Heo send_mayday(work); 1693e22bee78STejun Heo } 1694e22bee78STejun Heo 1695e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 1696e22bee78STejun Heo 169763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1698e22bee78STejun Heo } 1699e22bee78STejun Heo 1700e22bee78STejun Heo /** 1701e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 170263d95a91STejun Heo * @pool: pool to create a new worker for 1703e22bee78STejun Heo * 170463d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1705e22bee78STejun Heo * have at least one idle worker on return from this function. If 1706e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 170763d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1708e22bee78STejun Heo * possible allocation deadlock. 1709e22bee78STejun Heo * 1710e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1711e22bee78STejun Heo * may_start_working() true. 1712e22bee78STejun Heo * 1713e22bee78STejun Heo * LOCKING: 1714e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1715e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1716e22bee78STejun Heo * manager. 1717e22bee78STejun Heo * 1718e22bee78STejun Heo * RETURNS: 1719e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1720e22bee78STejun Heo * otherwise. 1721e22bee78STejun Heo */ 172263d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 172306bd6ebfSNamhyung Kim __releases(&gcwq->lock) 172406bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 1725e22bee78STejun Heo { 172663d95a91STejun Heo struct global_cwq *gcwq = pool->gcwq; 172763d95a91STejun Heo 172863d95a91STejun Heo if (!need_to_create_worker(pool)) 1729e22bee78STejun Heo return false; 1730e22bee78STejun Heo restart: 17319f9c2364STejun Heo spin_unlock_irq(&gcwq->lock); 17329f9c2364STejun Heo 1733e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 173463d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1735e22bee78STejun Heo 1736e22bee78STejun Heo while (true) { 1737e22bee78STejun Heo struct worker *worker; 1738e22bee78STejun Heo 1739bc2ae0f5STejun Heo worker = create_worker(pool); 1740e22bee78STejun Heo if (worker) { 174163d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1742e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 1743e22bee78STejun Heo start_worker(worker); 174463d95a91STejun Heo BUG_ON(need_to_create_worker(pool)); 1745e22bee78STejun Heo return true; 1746e22bee78STejun Heo } 1747e22bee78STejun Heo 174863d95a91STejun Heo if (!need_to_create_worker(pool)) 1749e22bee78STejun Heo break; 1750e22bee78STejun Heo 1751e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1752e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 17539f9c2364STejun Heo 175463d95a91STejun Heo if (!need_to_create_worker(pool)) 1755e22bee78STejun Heo break; 1756e22bee78STejun Heo } 1757e22bee78STejun Heo 175863d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1759e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 176063d95a91STejun Heo if (need_to_create_worker(pool)) 1761e22bee78STejun Heo goto restart; 1762e22bee78STejun Heo return true; 1763e22bee78STejun Heo } 1764e22bee78STejun Heo 1765e22bee78STejun Heo /** 1766e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 176763d95a91STejun Heo * @pool: pool to destroy workers for 1768e22bee78STejun Heo * 176963d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1770e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1771e22bee78STejun Heo * 1772e22bee78STejun Heo * LOCKING: 1773e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1774e22bee78STejun Heo * multiple times. Called only from manager. 1775e22bee78STejun Heo * 1776e22bee78STejun Heo * RETURNS: 1777e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true 1778e22bee78STejun Heo * otherwise. 1779e22bee78STejun Heo */ 178063d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1781e22bee78STejun Heo { 1782e22bee78STejun Heo bool ret = false; 1783e22bee78STejun Heo 178463d95a91STejun Heo while (too_many_workers(pool)) { 1785e22bee78STejun Heo struct worker *worker; 1786e22bee78STejun Heo unsigned long expires; 1787e22bee78STejun Heo 178863d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1789e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1790e22bee78STejun Heo 1791e22bee78STejun Heo if (time_before(jiffies, expires)) { 179263d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1793e22bee78STejun Heo break; 1794e22bee78STejun Heo } 1795e22bee78STejun Heo 1796e22bee78STejun Heo destroy_worker(worker); 1797e22bee78STejun Heo ret = true; 1798e22bee78STejun Heo } 1799e22bee78STejun Heo 1800e22bee78STejun Heo return ret; 1801e22bee78STejun Heo } 1802e22bee78STejun Heo 1803e22bee78STejun Heo /** 1804e22bee78STejun Heo * manage_workers - manage worker pool 1805e22bee78STejun Heo * @worker: self 1806e22bee78STejun Heo * 1807e22bee78STejun Heo * Assume the manager role and manage gcwq worker pool @worker belongs 1808e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1809e22bee78STejun Heo * gcwq. The exclusion is handled automatically by this function. 1810e22bee78STejun Heo * 1811e22bee78STejun Heo * The caller can safely start processing works on false return. On 1812e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1813e22bee78STejun Heo * and may_start_working() is true. 1814e22bee78STejun Heo * 1815e22bee78STejun Heo * CONTEXT: 1816e22bee78STejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 1817e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1818e22bee78STejun Heo * 1819e22bee78STejun Heo * RETURNS: 1820e22bee78STejun Heo * false if no action was taken and gcwq->lock stayed locked, true if 1821e22bee78STejun Heo * some action was taken. 1822e22bee78STejun Heo */ 1823e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1824e22bee78STejun Heo { 182563d95a91STejun Heo struct worker_pool *pool = worker->pool; 1826e22bee78STejun Heo bool ret = false; 1827e22bee78STejun Heo 182860373152STejun Heo if (!mutex_trylock(&pool->manager_mutex)) 1829e22bee78STejun Heo return ret; 1830e22bee78STejun Heo 1831*552a37e9SLai Jiangshan pool->flags |= POOL_MANAGING_WORKERS; 183211ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 1833e22bee78STejun Heo 1834e22bee78STejun Heo /* 1835e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 1836e22bee78STejun Heo * on return. 1837e22bee78STejun Heo */ 183863d95a91STejun Heo ret |= maybe_destroy_workers(pool); 183963d95a91STejun Heo ret |= maybe_create_worker(pool); 1840e22bee78STejun Heo 1841*552a37e9SLai Jiangshan pool->flags &= ~POOL_MANAGING_WORKERS; 184260373152STejun Heo mutex_unlock(&pool->manager_mutex); 1843e22bee78STejun Heo return ret; 1844e22bee78STejun Heo } 1845e22bee78STejun Heo 1846a62428c0STejun Heo /** 1847affee4b2STejun Heo * move_linked_works - move linked works to a list 1848affee4b2STejun Heo * @work: start of series of works to be scheduled 1849affee4b2STejun Heo * @head: target list to append @work to 1850affee4b2STejun Heo * @nextp: out paramter for nested worklist walking 1851affee4b2STejun Heo * 1852affee4b2STejun Heo * Schedule linked works starting from @work to @head. Work series to 1853affee4b2STejun Heo * be scheduled starts at @work and includes any consecutive work with 1854affee4b2STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1855affee4b2STejun Heo * 1856affee4b2STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1857affee4b2STejun Heo * the last scheduled work. This allows move_linked_works() to be 1858affee4b2STejun Heo * nested inside outer list_for_each_entry_safe(). 1859affee4b2STejun Heo * 1860affee4b2STejun Heo * CONTEXT: 18618b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 1862affee4b2STejun Heo */ 1863affee4b2STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1864affee4b2STejun Heo struct work_struct **nextp) 1865affee4b2STejun Heo { 1866affee4b2STejun Heo struct work_struct *n; 1867affee4b2STejun Heo 1868affee4b2STejun Heo /* 1869affee4b2STejun Heo * Linked worklist will always end before the end of the list, 1870affee4b2STejun Heo * use NULL for list head. 1871affee4b2STejun Heo */ 1872affee4b2STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1873affee4b2STejun Heo list_move_tail(&work->entry, head); 1874affee4b2STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1875affee4b2STejun Heo break; 1876affee4b2STejun Heo } 1877affee4b2STejun Heo 1878affee4b2STejun Heo /* 1879affee4b2STejun Heo * If we're already inside safe list traversal and have moved 1880affee4b2STejun Heo * multiple works to the scheduled queue, the next position 1881affee4b2STejun Heo * needs to be updated. 1882affee4b2STejun Heo */ 1883affee4b2STejun Heo if (nextp) 1884affee4b2STejun Heo *nextp = n; 1885affee4b2STejun Heo } 1886affee4b2STejun Heo 18871e19ffc6STejun Heo static void cwq_activate_first_delayed(struct cpu_workqueue_struct *cwq) 18881e19ffc6STejun Heo { 18891e19ffc6STejun Heo struct work_struct *work = list_first_entry(&cwq->delayed_works, 18901da177e4SLinus Torvalds struct work_struct, entry); 18911e19ffc6STejun Heo 1892cdadf009STejun Heo trace_workqueue_activate_work(work); 18933270476aSTejun Heo move_linked_works(work, &cwq->pool->worklist, NULL); 18948a2e8e5dSTejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 18951e19ffc6STejun Heo cwq->nr_active++; 18961e19ffc6STejun Heo } 18971e19ffc6STejun Heo 1898affee4b2STejun Heo /** 189973f53c4aSTejun Heo * cwq_dec_nr_in_flight - decrement cwq's nr_in_flight 190073f53c4aSTejun Heo * @cwq: cwq of interest 190173f53c4aSTejun Heo * @color: color of work which left the queue 19028a2e8e5dSTejun Heo * @delayed: for a delayed work 190373f53c4aSTejun Heo * 190473f53c4aSTejun Heo * A work either has completed or is removed from pending queue, 190573f53c4aSTejun Heo * decrement nr_in_flight of its cwq and handle workqueue flushing. 190673f53c4aSTejun Heo * 190773f53c4aSTejun Heo * CONTEXT: 19088b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 190973f53c4aSTejun Heo */ 19108a2e8e5dSTejun Heo static void cwq_dec_nr_in_flight(struct cpu_workqueue_struct *cwq, int color, 19118a2e8e5dSTejun Heo bool delayed) 191273f53c4aSTejun Heo { 191373f53c4aSTejun Heo /* ignore uncolored works */ 191473f53c4aSTejun Heo if (color == WORK_NO_COLOR) 191573f53c4aSTejun Heo return; 191673f53c4aSTejun Heo 191773f53c4aSTejun Heo cwq->nr_in_flight[color]--; 19181e19ffc6STejun Heo 19198a2e8e5dSTejun Heo if (!delayed) { 19208a2e8e5dSTejun Heo cwq->nr_active--; 1921502ca9d8STejun Heo if (!list_empty(&cwq->delayed_works)) { 19221e19ffc6STejun Heo /* one down, submit a delayed one */ 1923502ca9d8STejun Heo if (cwq->nr_active < cwq->max_active) 19241e19ffc6STejun Heo cwq_activate_first_delayed(cwq); 1925502ca9d8STejun Heo } 19268a2e8e5dSTejun Heo } 192773f53c4aSTejun Heo 192873f53c4aSTejun Heo /* is flush in progress and are we at the flushing tip? */ 192973f53c4aSTejun Heo if (likely(cwq->flush_color != color)) 193073f53c4aSTejun Heo return; 193173f53c4aSTejun Heo 193273f53c4aSTejun Heo /* are there still in-flight works? */ 193373f53c4aSTejun Heo if (cwq->nr_in_flight[color]) 193473f53c4aSTejun Heo return; 193573f53c4aSTejun Heo 193673f53c4aSTejun Heo /* this cwq is done, clear flush_color */ 193773f53c4aSTejun Heo cwq->flush_color = -1; 193873f53c4aSTejun Heo 193973f53c4aSTejun Heo /* 194073f53c4aSTejun Heo * If this was the last cwq, wake up the first flusher. It 194173f53c4aSTejun Heo * will handle the rest. 194273f53c4aSTejun Heo */ 194373f53c4aSTejun Heo if (atomic_dec_and_test(&cwq->wq->nr_cwqs_to_flush)) 194473f53c4aSTejun Heo complete(&cwq->wq->first_flusher->done); 194573f53c4aSTejun Heo } 194673f53c4aSTejun Heo 194773f53c4aSTejun Heo /** 1948a62428c0STejun Heo * process_one_work - process single work 1949c34056a3STejun Heo * @worker: self 1950a62428c0STejun Heo * @work: work to process 1951a62428c0STejun Heo * 1952a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1953a62428c0STejun Heo * process a single work including synchronization against and 1954a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1955a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1956a62428c0STejun Heo * call this function to process a work. 1957a62428c0STejun Heo * 1958a62428c0STejun Heo * CONTEXT: 19598b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which is released and regrabbed. 1960a62428c0STejun Heo */ 1961c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 196206bd6ebfSNamhyung Kim __releases(&gcwq->lock) 196306bd6ebfSNamhyung Kim __acquires(&gcwq->lock) 19641da177e4SLinus Torvalds { 19657e11629dSTejun Heo struct cpu_workqueue_struct *cwq = get_work_cwq(work); 1966bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1967bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 1968c8e55f36STejun Heo struct hlist_head *bwh = busy_worker_head(gcwq, work); 1969fb0e7bebSTejun Heo bool cpu_intensive = cwq->wq->flags & WQ_CPU_INTENSIVE; 19706bb49e59SDavid Howells work_func_t f = work->func; 197173f53c4aSTejun Heo int work_color; 19727e11629dSTejun Heo struct worker *collision; 19734e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 19744e6045f1SJohannes Berg /* 1975a62428c0STejun Heo * It is permissible to free the struct work_struct from 1976a62428c0STejun Heo * inside the function that is called from it, this we need to 1977a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 1978a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 1979a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 19804e6045f1SJohannes Berg */ 19814d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 19824d82a1deSPeter Zijlstra 19834d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 19844e6045f1SJohannes Berg #endif 19856fec10a1STejun Heo /* 19866fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 19876fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 19886fec10a1STejun Heo * unbound or a disassociated gcwq. 19896fec10a1STejun Heo */ 199025511a47STejun Heo WARN_ON_ONCE(!(worker->flags & (WORKER_UNBOUND | WORKER_REBIND)) && 19916fec10a1STejun Heo !(gcwq->flags & GCWQ_DISASSOCIATED) && 199225511a47STejun Heo raw_smp_processor_id() != gcwq->cpu); 199325511a47STejun Heo 19947e11629dSTejun Heo /* 19957e11629dSTejun Heo * A single work shouldn't be executed concurrently by 19967e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 19977e11629dSTejun Heo * already processing the work. If so, defer the work to the 19987e11629dSTejun Heo * currently executing one. 19997e11629dSTejun Heo */ 20007e11629dSTejun Heo collision = __find_worker_executing_work(gcwq, bwh, work); 20017e11629dSTejun Heo if (unlikely(collision)) { 20027e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20037e11629dSTejun Heo return; 20047e11629dSTejun Heo } 20051da177e4SLinus Torvalds 2006a62428c0STejun Heo /* claim and process */ 20071da177e4SLinus Torvalds debug_work_deactivate(work); 2008c8e55f36STejun Heo hlist_add_head(&worker->hentry, bwh); 2009c34056a3STejun Heo worker->current_work = work; 20108cca0eeaSTejun Heo worker->current_cwq = cwq; 201173f53c4aSTejun Heo work_color = get_work_color(work); 20127a22ad75STejun Heo 20137a22ad75STejun Heo /* record the current cpu number in the work data and dequeue */ 20147a22ad75STejun Heo set_work_cpu(work, gcwq->cpu); 2015a62428c0STejun Heo list_del_init(&work->entry); 2016a62428c0STejun Heo 2017649027d7STejun Heo /* 2018fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2019fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2020fb0e7bebSTejun Heo */ 2021fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2022fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2023fb0e7bebSTejun Heo 2024974271c4STejun Heo /* 2025974271c4STejun Heo * Unbound gcwq isn't concurrency managed and work items should be 2026974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2027974271c4STejun Heo */ 202863d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 202963d95a91STejun Heo wake_up_worker(pool); 2030974271c4STejun Heo 20318b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 20321da177e4SLinus Torvalds 203323b2e599SOleg Nesterov work_clear_pending(work); 2034e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 20353295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2036e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 203765f27f38SDavid Howells f(work); 2038e36c886aSArjan van de Ven /* 2039e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2040e36c886aSArjan van de Ven * point will only record its address. 2041e36c886aSArjan van de Ven */ 2042e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20433295f0efSIngo Molnar lock_map_release(&lockdep_map); 20443295f0efSIngo Molnar lock_map_release(&cwq->wq->lockdep_map); 20451da177e4SLinus Torvalds 2046d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2047d5abe669SPeter Zijlstra printk(KERN_ERR "BUG: workqueue leaked lock or atomic: " 2048d5abe669SPeter Zijlstra "%s/0x%08x/%d\n", 2049a62428c0STejun Heo current->comm, preempt_count(), task_pid_nr(current)); 2050d5abe669SPeter Zijlstra printk(KERN_ERR " last function: "); 2051d5abe669SPeter Zijlstra print_symbol("%s\n", (unsigned long)f); 2052d5abe669SPeter Zijlstra debug_show_held_locks(current); 2053d5abe669SPeter Zijlstra dump_stack(); 2054d5abe669SPeter Zijlstra } 2055d5abe669SPeter Zijlstra 20568b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2057a62428c0STejun Heo 2058fb0e7bebSTejun Heo /* clear cpu intensive status */ 2059fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2060fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2061fb0e7bebSTejun Heo 2062a62428c0STejun Heo /* we're done with it, release */ 2063c8e55f36STejun Heo hlist_del_init(&worker->hentry); 2064c34056a3STejun Heo worker->current_work = NULL; 20658cca0eeaSTejun Heo worker->current_cwq = NULL; 20668a2e8e5dSTejun Heo cwq_dec_nr_in_flight(cwq, work_color, false); 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069affee4b2STejun Heo /** 2070affee4b2STejun Heo * process_scheduled_works - process scheduled works 2071affee4b2STejun Heo * @worker: self 2072affee4b2STejun Heo * 2073affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2074affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2075affee4b2STejun Heo * fetches a work from the top and executes it. 2076affee4b2STejun Heo * 2077affee4b2STejun Heo * CONTEXT: 20788b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock) which may be released and regrabbed 2079affee4b2STejun Heo * multiple times. 2080affee4b2STejun Heo */ 2081affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 20821da177e4SLinus Torvalds { 2083affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2084affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2085a62428c0STejun Heo struct work_struct, entry); 2086c34056a3STejun Heo process_one_work(worker, work); 2087a62428c0STejun Heo } 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds 20904690c4abSTejun Heo /** 20914690c4abSTejun Heo * worker_thread - the worker thread function 2092c34056a3STejun Heo * @__worker: self 20934690c4abSTejun Heo * 2094e22bee78STejun Heo * The gcwq worker thread function. There's a single dynamic pool of 2095e22bee78STejun Heo * these per each cpu. These workers process all works regardless of 2096e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2097e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2098e22bee78STejun Heo * rescuer_thread(). 20994690c4abSTejun Heo */ 2100c34056a3STejun Heo static int worker_thread(void *__worker) 21011da177e4SLinus Torvalds { 2102c34056a3STejun Heo struct worker *worker = __worker; 2103bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2104bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 21051da177e4SLinus Torvalds 2106e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2107e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2108c8e55f36STejun Heo woke_up: 21098b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 2110affee4b2STejun Heo 211125511a47STejun Heo /* 211225511a47STejun Heo * DIE can be set only while idle and REBIND set while busy has 211325511a47STejun Heo * @worker->rebind_work scheduled. Checking here is enough. 211425511a47STejun Heo */ 211525511a47STejun Heo if (unlikely(worker->flags & (WORKER_REBIND | WORKER_DIE))) { 2116c8e55f36STejun Heo spin_unlock_irq(&gcwq->lock); 211725511a47STejun Heo 211825511a47STejun Heo if (worker->flags & WORKER_DIE) { 2119e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2120c8e55f36STejun Heo return 0; 2121c8e55f36STejun Heo } 2122c8e55f36STejun Heo 212325511a47STejun Heo idle_worker_rebind(worker); 212425511a47STejun Heo goto woke_up; 212525511a47STejun Heo } 212625511a47STejun Heo 2127c8e55f36STejun Heo worker_leave_idle(worker); 2128db7bccf4STejun Heo recheck: 2129e22bee78STejun Heo /* no more worker necessary? */ 213063d95a91STejun Heo if (!need_more_worker(pool)) 2131e22bee78STejun Heo goto sleep; 2132e22bee78STejun Heo 2133e22bee78STejun Heo /* do we need to manage? */ 213463d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2135e22bee78STejun Heo goto recheck; 2136e22bee78STejun Heo 2137c8e55f36STejun Heo /* 2138c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2139c8e55f36STejun Heo * preparing to process a work or actually processing it. 2140c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2141c8e55f36STejun Heo */ 2142c8e55f36STejun Heo BUG_ON(!list_empty(&worker->scheduled)); 2143c8e55f36STejun Heo 2144e22bee78STejun Heo /* 2145e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2146e22bee78STejun Heo * at least one idle worker or that someone else has already 2147e22bee78STejun Heo * assumed the manager role. 2148e22bee78STejun Heo */ 2149e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2150e22bee78STejun Heo 2151e22bee78STejun Heo do { 2152affee4b2STejun Heo struct work_struct *work = 2153bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2154affee4b2STejun Heo struct work_struct, entry); 2155affee4b2STejun Heo 2156c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2157affee4b2STejun Heo /* optimization path, not strictly necessary */ 2158affee4b2STejun Heo process_one_work(worker, work); 2159affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2160affee4b2STejun Heo process_scheduled_works(worker); 2161affee4b2STejun Heo } else { 2162c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2163affee4b2STejun Heo process_scheduled_works(worker); 2164affee4b2STejun Heo } 216563d95a91STejun Heo } while (keep_working(pool)); 2166affee4b2STejun Heo 2167e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2168d313dd85STejun Heo sleep: 216963d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2170e22bee78STejun Heo goto recheck; 2171d313dd85STejun Heo 2172c8e55f36STejun Heo /* 2173e22bee78STejun Heo * gcwq->lock is held and there's no work to process and no 2174e22bee78STejun Heo * need to manage, sleep. Workers are woken up only while 2175e22bee78STejun Heo * holding gcwq->lock or from local cpu, so setting the 2176e22bee78STejun Heo * current state before releasing gcwq->lock is enough to 2177e22bee78STejun Heo * prevent losing any event. 2178c8e55f36STejun Heo */ 2179c8e55f36STejun Heo worker_enter_idle(worker); 2180c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 21818b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 21821da177e4SLinus Torvalds schedule(); 2183c8e55f36STejun Heo goto woke_up; 21841da177e4SLinus Torvalds } 21851da177e4SLinus Torvalds 2186e22bee78STejun Heo /** 2187e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2188e22bee78STejun Heo * @__wq: the associated workqueue 2189e22bee78STejun Heo * 2190e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2191e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2192e22bee78STejun Heo * 2193e22bee78STejun Heo * Regular work processing on a gcwq may block trying to create a new 2194e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2195e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2196e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2197e22bee78STejun Heo * the problem rescuer solves. 2198e22bee78STejun Heo * 2199e22bee78STejun Heo * When such condition is possible, the gcwq summons rescuers of all 2200e22bee78STejun Heo * workqueues which have works queued on the gcwq and let them process 2201e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2202e22bee78STejun Heo * 2203e22bee78STejun Heo * This should happen rarely. 2204e22bee78STejun Heo */ 2205e22bee78STejun Heo static int rescuer_thread(void *__wq) 2206e22bee78STejun Heo { 2207e22bee78STejun Heo struct workqueue_struct *wq = __wq; 2208e22bee78STejun Heo struct worker *rescuer = wq->rescuer; 2209e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2210f3421797STejun Heo bool is_unbound = wq->flags & WQ_UNBOUND; 2211e22bee78STejun Heo unsigned int cpu; 2212e22bee78STejun Heo 2213e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2214e22bee78STejun Heo repeat: 2215e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds if (kthread_should_stop()) 2218e22bee78STejun Heo return 0; 22191da177e4SLinus Torvalds 2220f3421797STejun Heo /* 2221f3421797STejun Heo * See whether any cpu is asking for help. Unbounded 2222f3421797STejun Heo * workqueues use cpu 0 in mayday_mask for CPU_UNBOUND. 2223f3421797STejun Heo */ 2224f2e005aaSTejun Heo for_each_mayday_cpu(cpu, wq->mayday_mask) { 2225f3421797STejun Heo unsigned int tcpu = is_unbound ? WORK_CPU_UNBOUND : cpu; 2226f3421797STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(tcpu, wq); 2227bd7bdd43STejun Heo struct worker_pool *pool = cwq->pool; 2228bd7bdd43STejun Heo struct global_cwq *gcwq = pool->gcwq; 2229e22bee78STejun Heo struct work_struct *work, *n; 2230e22bee78STejun Heo 2231e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2232f2e005aaSTejun Heo mayday_clear_cpu(cpu, wq->mayday_mask); 2233e22bee78STejun Heo 2234e22bee78STejun Heo /* migrate to the target cpu if possible */ 2235bd7bdd43STejun Heo rescuer->pool = pool; 2236e22bee78STejun Heo worker_maybe_bind_and_lock(rescuer); 2237e22bee78STejun Heo 2238e22bee78STejun Heo /* 2239e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2240e22bee78STejun Heo * process'em. 2241e22bee78STejun Heo */ 2242e22bee78STejun Heo BUG_ON(!list_empty(&rescuer->scheduled)); 2243bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2244e22bee78STejun Heo if (get_work_cwq(work) == cwq) 2245e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2246e22bee78STejun Heo 2247e22bee78STejun Heo process_scheduled_works(rescuer); 22487576958aSTejun Heo 22497576958aSTejun Heo /* 22507576958aSTejun Heo * Leave this gcwq. If keep_working() is %true, notify a 22517576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 22527576958aSTejun Heo * and stalling the execution. 22537576958aSTejun Heo */ 225463d95a91STejun Heo if (keep_working(pool)) 225563d95a91STejun Heo wake_up_worker(pool); 22567576958aSTejun Heo 2257e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds 2260e22bee78STejun Heo schedule(); 2261e22bee78STejun Heo goto repeat; 22621da177e4SLinus Torvalds } 22631da177e4SLinus Torvalds 2264fc2e4d70SOleg Nesterov struct wq_barrier { 2265fc2e4d70SOleg Nesterov struct work_struct work; 2266fc2e4d70SOleg Nesterov struct completion done; 2267fc2e4d70SOleg Nesterov }; 2268fc2e4d70SOleg Nesterov 2269fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2270fc2e4d70SOleg Nesterov { 2271fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2272fc2e4d70SOleg Nesterov complete(&barr->done); 2273fc2e4d70SOleg Nesterov } 2274fc2e4d70SOleg Nesterov 22754690c4abSTejun Heo /** 22764690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 22774690c4abSTejun Heo * @cwq: cwq to insert barrier into 22784690c4abSTejun Heo * @barr: wq_barrier to insert 2279affee4b2STejun Heo * @target: target work to attach @barr to 2280affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 22814690c4abSTejun Heo * 2282affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2283affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2284affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2285affee4b2STejun Heo * cpu. 2286affee4b2STejun Heo * 2287affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2288affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2289affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2290affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2291affee4b2STejun Heo * after a work with LINKED flag set. 2292affee4b2STejun Heo * 2293affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2294affee4b2STejun Heo * underneath us, so we can't reliably determine cwq from @target. 22954690c4abSTejun Heo * 22964690c4abSTejun Heo * CONTEXT: 22978b03ae3cSTejun Heo * spin_lock_irq(gcwq->lock). 22984690c4abSTejun Heo */ 229983c22520SOleg Nesterov static void insert_wq_barrier(struct cpu_workqueue_struct *cwq, 2300affee4b2STejun Heo struct wq_barrier *barr, 2301affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2302fc2e4d70SOleg Nesterov { 2303affee4b2STejun Heo struct list_head *head; 2304affee4b2STejun Heo unsigned int linked = 0; 2305affee4b2STejun Heo 2306dc186ad7SThomas Gleixner /* 23078b03ae3cSTejun Heo * debugobject calls are safe here even with gcwq->lock locked 2308dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2309dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2310dc186ad7SThomas Gleixner * might deadlock. 2311dc186ad7SThomas Gleixner */ 2312ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 231322df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2314fc2e4d70SOleg Nesterov init_completion(&barr->done); 231583c22520SOleg Nesterov 2316affee4b2STejun Heo /* 2317affee4b2STejun Heo * If @target is currently being executed, schedule the 2318affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2319affee4b2STejun Heo */ 2320affee4b2STejun Heo if (worker) 2321affee4b2STejun Heo head = worker->scheduled.next; 2322affee4b2STejun Heo else { 2323affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2324affee4b2STejun Heo 2325affee4b2STejun Heo head = target->entry.next; 2326affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2327affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2328affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2329affee4b2STejun Heo } 2330affee4b2STejun Heo 2331dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2332affee4b2STejun Heo insert_work(cwq, &barr->work, head, 2333affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2334fc2e4d70SOleg Nesterov } 2335fc2e4d70SOleg Nesterov 233673f53c4aSTejun Heo /** 233773f53c4aSTejun Heo * flush_workqueue_prep_cwqs - prepare cwqs for workqueue flushing 233873f53c4aSTejun Heo * @wq: workqueue being flushed 233973f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 234073f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 234173f53c4aSTejun Heo * 234273f53c4aSTejun Heo * Prepare cwqs for workqueue flushing. 234373f53c4aSTejun Heo * 234473f53c4aSTejun Heo * If @flush_color is non-negative, flush_color on all cwqs should be 234573f53c4aSTejun Heo * -1. If no cwq has in-flight commands at the specified color, all 234673f53c4aSTejun Heo * cwq->flush_color's stay at -1 and %false is returned. If any cwq 234773f53c4aSTejun Heo * has in flight commands, its cwq->flush_color is set to 234873f53c4aSTejun Heo * @flush_color, @wq->nr_cwqs_to_flush is updated accordingly, cwq 234973f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 235073f53c4aSTejun Heo * 235173f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 235273f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 235373f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 235473f53c4aSTejun Heo * is returned. 235573f53c4aSTejun Heo * 235673f53c4aSTejun Heo * If @work_color is non-negative, all cwqs should have the same 235773f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 235873f53c4aSTejun Heo * advanced to @work_color. 235973f53c4aSTejun Heo * 236073f53c4aSTejun Heo * CONTEXT: 236173f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 236273f53c4aSTejun Heo * 236373f53c4aSTejun Heo * RETURNS: 236473f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 236573f53c4aSTejun Heo * otherwise. 236673f53c4aSTejun Heo */ 236773f53c4aSTejun Heo static bool flush_workqueue_prep_cwqs(struct workqueue_struct *wq, 236873f53c4aSTejun Heo int flush_color, int work_color) 23691da177e4SLinus Torvalds { 237073f53c4aSTejun Heo bool wait = false; 237173f53c4aSTejun Heo unsigned int cpu; 23721da177e4SLinus Torvalds 237373f53c4aSTejun Heo if (flush_color >= 0) { 237473f53c4aSTejun Heo BUG_ON(atomic_read(&wq->nr_cwqs_to_flush)); 237573f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 1); 2376dc186ad7SThomas Gleixner } 237714441960SOleg Nesterov 2378f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 237973f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2380bd7bdd43STejun Heo struct global_cwq *gcwq = cwq->pool->gcwq; 23811da177e4SLinus Torvalds 23828b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 238373f53c4aSTejun Heo 238473f53c4aSTejun Heo if (flush_color >= 0) { 238573f53c4aSTejun Heo BUG_ON(cwq->flush_color != -1); 238673f53c4aSTejun Heo 238773f53c4aSTejun Heo if (cwq->nr_in_flight[flush_color]) { 238873f53c4aSTejun Heo cwq->flush_color = flush_color; 238973f53c4aSTejun Heo atomic_inc(&wq->nr_cwqs_to_flush); 239073f53c4aSTejun Heo wait = true; 23911da177e4SLinus Torvalds } 239273f53c4aSTejun Heo } 239373f53c4aSTejun Heo 239473f53c4aSTejun Heo if (work_color >= 0) { 239573f53c4aSTejun Heo BUG_ON(work_color != work_next_color(cwq->work_color)); 239673f53c4aSTejun Heo cwq->work_color = work_color; 239773f53c4aSTejun Heo } 239873f53c4aSTejun Heo 23998b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 24001da177e4SLinus Torvalds } 24011da177e4SLinus Torvalds 240273f53c4aSTejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_cwqs_to_flush)) 240373f53c4aSTejun Heo complete(&wq->first_flusher->done); 240473f53c4aSTejun Heo 240573f53c4aSTejun Heo return wait; 240683c22520SOleg Nesterov } 24071da177e4SLinus Torvalds 24080fcb78c2SRolf Eike Beer /** 24091da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 24100fcb78c2SRolf Eike Beer * @wq: workqueue to flush 24111da177e4SLinus Torvalds * 24121da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 24131da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 24141da177e4SLinus Torvalds * 2415fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2416fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 24171da177e4SLinus Torvalds */ 24187ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 24191da177e4SLinus Torvalds { 242073f53c4aSTejun Heo struct wq_flusher this_flusher = { 242173f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 242273f53c4aSTejun Heo .flush_color = -1, 242373f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 242473f53c4aSTejun Heo }; 242573f53c4aSTejun Heo int next_color; 2426b1f4ec17SOleg Nesterov 24273295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 24283295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 242973f53c4aSTejun Heo 243073f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 243173f53c4aSTejun Heo 243273f53c4aSTejun Heo /* 243373f53c4aSTejun Heo * Start-to-wait phase 243473f53c4aSTejun Heo */ 243573f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 243673f53c4aSTejun Heo 243773f53c4aSTejun Heo if (next_color != wq->flush_color) { 243873f53c4aSTejun Heo /* 243973f53c4aSTejun Heo * Color space is not full. The current work_color 244073f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 244173f53c4aSTejun Heo * by one. 244273f53c4aSTejun Heo */ 244373f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow)); 244473f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 244573f53c4aSTejun Heo wq->work_color = next_color; 244673f53c4aSTejun Heo 244773f53c4aSTejun Heo if (!wq->first_flusher) { 244873f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 244973f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 245073f53c4aSTejun Heo 245173f53c4aSTejun Heo wq->first_flusher = &this_flusher; 245273f53c4aSTejun Heo 245373f53c4aSTejun Heo if (!flush_workqueue_prep_cwqs(wq, wq->flush_color, 245473f53c4aSTejun Heo wq->work_color)) { 245573f53c4aSTejun Heo /* nothing to flush, done */ 245673f53c4aSTejun Heo wq->flush_color = next_color; 245773f53c4aSTejun Heo wq->first_flusher = NULL; 245873f53c4aSTejun Heo goto out_unlock; 245973f53c4aSTejun Heo } 246073f53c4aSTejun Heo } else { 246173f53c4aSTejun Heo /* wait in queue */ 246273f53c4aSTejun Heo BUG_ON(wq->flush_color == this_flusher.flush_color); 246373f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 246473f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 246573f53c4aSTejun Heo } 246673f53c4aSTejun Heo } else { 246773f53c4aSTejun Heo /* 246873f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 246973f53c4aSTejun Heo * The next flush completion will assign us 247073f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 247173f53c4aSTejun Heo */ 247273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 247373f53c4aSTejun Heo } 247473f53c4aSTejun Heo 247573f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 247673f53c4aSTejun Heo 247773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 247873f53c4aSTejun Heo 247973f53c4aSTejun Heo /* 248073f53c4aSTejun Heo * Wake-up-and-cascade phase 248173f53c4aSTejun Heo * 248273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 248373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 248473f53c4aSTejun Heo */ 248573f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 248673f53c4aSTejun Heo return; 248773f53c4aSTejun Heo 248873f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 248973f53c4aSTejun Heo 24904ce48b37STejun Heo /* we might have raced, check again with mutex held */ 24914ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 24924ce48b37STejun Heo goto out_unlock; 24934ce48b37STejun Heo 249473f53c4aSTejun Heo wq->first_flusher = NULL; 249573f53c4aSTejun Heo 249673f53c4aSTejun Heo BUG_ON(!list_empty(&this_flusher.list)); 249773f53c4aSTejun Heo BUG_ON(wq->flush_color != this_flusher.flush_color); 249873f53c4aSTejun Heo 249973f53c4aSTejun Heo while (true) { 250073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 250173f53c4aSTejun Heo 250273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 250373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 250473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 250573f53c4aSTejun Heo break; 250673f53c4aSTejun Heo list_del_init(&next->list); 250773f53c4aSTejun Heo complete(&next->done); 250873f53c4aSTejun Heo } 250973f53c4aSTejun Heo 251073f53c4aSTejun Heo BUG_ON(!list_empty(&wq->flusher_overflow) && 251173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 251273f53c4aSTejun Heo 251373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 251473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 251573f53c4aSTejun Heo 251673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 251773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 251873f53c4aSTejun Heo /* 251973f53c4aSTejun Heo * Assign the same color to all overflowed 252073f53c4aSTejun Heo * flushers, advance work_color and append to 252173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 252273f53c4aSTejun Heo * phase for these overflowed flushers. 252373f53c4aSTejun Heo */ 252473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 252573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 252673f53c4aSTejun Heo 252773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 252873f53c4aSTejun Heo 252973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 253073f53c4aSTejun Heo &wq->flusher_queue); 253173f53c4aSTejun Heo flush_workqueue_prep_cwqs(wq, -1, wq->work_color); 253273f53c4aSTejun Heo } 253373f53c4aSTejun Heo 253473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 253573f53c4aSTejun Heo BUG_ON(wq->flush_color != wq->work_color); 253673f53c4aSTejun Heo break; 253773f53c4aSTejun Heo } 253873f53c4aSTejun Heo 253973f53c4aSTejun Heo /* 254073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 254173f53c4aSTejun Heo * the new first flusher and arm cwqs. 254273f53c4aSTejun Heo */ 254373f53c4aSTejun Heo BUG_ON(wq->flush_color == wq->work_color); 254473f53c4aSTejun Heo BUG_ON(wq->flush_color != next->flush_color); 254573f53c4aSTejun Heo 254673f53c4aSTejun Heo list_del_init(&next->list); 254773f53c4aSTejun Heo wq->first_flusher = next; 254873f53c4aSTejun Heo 254973f53c4aSTejun Heo if (flush_workqueue_prep_cwqs(wq, wq->flush_color, -1)) 255073f53c4aSTejun Heo break; 255173f53c4aSTejun Heo 255273f53c4aSTejun Heo /* 255373f53c4aSTejun Heo * Meh... this color is already done, clear first 255473f53c4aSTejun Heo * flusher and repeat cascading. 255573f53c4aSTejun Heo */ 255673f53c4aSTejun Heo wq->first_flusher = NULL; 255773f53c4aSTejun Heo } 255873f53c4aSTejun Heo 255973f53c4aSTejun Heo out_unlock: 256073f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 25611da177e4SLinus Torvalds } 2562ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 25631da177e4SLinus Torvalds 25649c5a2ba7STejun Heo /** 25659c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 25669c5a2ba7STejun Heo * @wq: workqueue to drain 25679c5a2ba7STejun Heo * 25689c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 25699c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 25709c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 25719c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 25729c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 25739c5a2ba7STejun Heo * takes too long. 25749c5a2ba7STejun Heo */ 25759c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 25769c5a2ba7STejun Heo { 25779c5a2ba7STejun Heo unsigned int flush_cnt = 0; 25789c5a2ba7STejun Heo unsigned int cpu; 25799c5a2ba7STejun Heo 25809c5a2ba7STejun Heo /* 25819c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 25829c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 25839c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 25849c5a2ba7STejun Heo */ 25859c5a2ba7STejun Heo spin_lock(&workqueue_lock); 25869c5a2ba7STejun Heo if (!wq->nr_drainers++) 25879c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 25889c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 25899c5a2ba7STejun Heo reflush: 25909c5a2ba7STejun Heo flush_workqueue(wq); 25919c5a2ba7STejun Heo 25929c5a2ba7STejun Heo for_each_cwq_cpu(cpu, wq) { 25939c5a2ba7STejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 2594fa2563e4SThomas Tuttle bool drained; 25959c5a2ba7STejun Heo 2596bd7bdd43STejun Heo spin_lock_irq(&cwq->pool->gcwq->lock); 2597fa2563e4SThomas Tuttle drained = !cwq->nr_active && list_empty(&cwq->delayed_works); 2598bd7bdd43STejun Heo spin_unlock_irq(&cwq->pool->gcwq->lock); 2599fa2563e4SThomas Tuttle 2600fa2563e4SThomas Tuttle if (drained) 26019c5a2ba7STejun Heo continue; 26029c5a2ba7STejun Heo 26039c5a2ba7STejun Heo if (++flush_cnt == 10 || 26049c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 26059c5a2ba7STejun Heo pr_warning("workqueue %s: flush on destruction isn't complete after %u tries\n", 26069c5a2ba7STejun Heo wq->name, flush_cnt); 26079c5a2ba7STejun Heo goto reflush; 26089c5a2ba7STejun Heo } 26099c5a2ba7STejun Heo 26109c5a2ba7STejun Heo spin_lock(&workqueue_lock); 26119c5a2ba7STejun Heo if (!--wq->nr_drainers) 26129c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 26139c5a2ba7STejun Heo spin_unlock(&workqueue_lock); 26149c5a2ba7STejun Heo } 26159c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 26169c5a2ba7STejun Heo 2617baf59022STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2618baf59022STejun Heo bool wait_executing) 2619baf59022STejun Heo { 2620baf59022STejun Heo struct worker *worker = NULL; 2621baf59022STejun Heo struct global_cwq *gcwq; 2622baf59022STejun Heo struct cpu_workqueue_struct *cwq; 2623baf59022STejun Heo 2624baf59022STejun Heo might_sleep(); 2625baf59022STejun Heo gcwq = get_work_gcwq(work); 2626baf59022STejun Heo if (!gcwq) 2627baf59022STejun Heo return false; 2628baf59022STejun Heo 2629baf59022STejun Heo spin_lock_irq(&gcwq->lock); 2630baf59022STejun Heo if (!list_empty(&work->entry)) { 2631baf59022STejun Heo /* 2632baf59022STejun Heo * See the comment near try_to_grab_pending()->smp_rmb(). 2633baf59022STejun Heo * If it was re-queued to a different gcwq under us, we 2634baf59022STejun Heo * are not going to wait. 2635baf59022STejun Heo */ 2636baf59022STejun Heo smp_rmb(); 2637baf59022STejun Heo cwq = get_work_cwq(work); 2638bd7bdd43STejun Heo if (unlikely(!cwq || gcwq != cwq->pool->gcwq)) 2639baf59022STejun Heo goto already_gone; 2640baf59022STejun Heo } else if (wait_executing) { 2641baf59022STejun Heo worker = find_worker_executing_work(gcwq, work); 2642baf59022STejun Heo if (!worker) 2643baf59022STejun Heo goto already_gone; 2644baf59022STejun Heo cwq = worker->current_cwq; 2645baf59022STejun Heo } else 2646baf59022STejun Heo goto already_gone; 2647baf59022STejun Heo 2648baf59022STejun Heo insert_wq_barrier(cwq, barr, work, worker); 2649baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2650baf59022STejun Heo 2651e159489bSTejun Heo /* 2652e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2653e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2654e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2655e159489bSTejun Heo * access. 2656e159489bSTejun Heo */ 2657e159489bSTejun Heo if (cwq->wq->saved_max_active == 1 || cwq->wq->flags & WQ_RESCUER) 2658baf59022STejun Heo lock_map_acquire(&cwq->wq->lockdep_map); 2659e159489bSTejun Heo else 2660e159489bSTejun Heo lock_map_acquire_read(&cwq->wq->lockdep_map); 2661baf59022STejun Heo lock_map_release(&cwq->wq->lockdep_map); 2662e159489bSTejun Heo 2663baf59022STejun Heo return true; 2664baf59022STejun Heo already_gone: 2665baf59022STejun Heo spin_unlock_irq(&gcwq->lock); 2666baf59022STejun Heo return false; 2667baf59022STejun Heo } 2668baf59022STejun Heo 2669db700897SOleg Nesterov /** 2670401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2671401a8d04STejun Heo * @work: the work to flush 2672db700897SOleg Nesterov * 2673401a8d04STejun Heo * Wait until @work has finished execution. This function considers 2674401a8d04STejun Heo * only the last queueing instance of @work. If @work has been 2675401a8d04STejun Heo * enqueued across different CPUs on a non-reentrant workqueue or on 2676401a8d04STejun Heo * multiple workqueues, @work might still be executing on return on 2677401a8d04STejun Heo * some of the CPUs from earlier queueing. 2678a67da70dSOleg Nesterov * 2679401a8d04STejun Heo * If @work was queued only on a non-reentrant, ordered or unbound 2680401a8d04STejun Heo * workqueue, @work is guaranteed to be idle on return if it hasn't 2681401a8d04STejun Heo * been requeued since flush started. 2682401a8d04STejun Heo * 2683401a8d04STejun Heo * RETURNS: 2684401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2685401a8d04STejun Heo * %false if it was already idle. 2686db700897SOleg Nesterov */ 2687401a8d04STejun Heo bool flush_work(struct work_struct *work) 2688db700897SOleg Nesterov { 2689db700897SOleg Nesterov struct wq_barrier barr; 2690db700897SOleg Nesterov 26910976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 26920976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 26930976dfc1SStephen Boyd 2694baf59022STejun Heo if (start_flush_work(work, &barr, true)) { 2695db700897SOleg Nesterov wait_for_completion(&barr.done); 2696dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2697401a8d04STejun Heo return true; 2698baf59022STejun Heo } else 2699401a8d04STejun Heo return false; 2700db700897SOleg Nesterov } 2701db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2702db700897SOleg Nesterov 2703401a8d04STejun Heo static bool wait_on_cpu_work(struct global_cwq *gcwq, struct work_struct *work) 2704401a8d04STejun Heo { 2705401a8d04STejun Heo struct wq_barrier barr; 2706401a8d04STejun Heo struct worker *worker; 2707401a8d04STejun Heo 2708401a8d04STejun Heo spin_lock_irq(&gcwq->lock); 2709401a8d04STejun Heo 2710401a8d04STejun Heo worker = find_worker_executing_work(gcwq, work); 2711401a8d04STejun Heo if (unlikely(worker)) 2712401a8d04STejun Heo insert_wq_barrier(worker->current_cwq, &barr, work, worker); 2713401a8d04STejun Heo 2714401a8d04STejun Heo spin_unlock_irq(&gcwq->lock); 2715401a8d04STejun Heo 2716401a8d04STejun Heo if (unlikely(worker)) { 2717401a8d04STejun Heo wait_for_completion(&barr.done); 2718401a8d04STejun Heo destroy_work_on_stack(&barr.work); 2719401a8d04STejun Heo return true; 2720401a8d04STejun Heo } else 2721401a8d04STejun Heo return false; 2722401a8d04STejun Heo } 2723401a8d04STejun Heo 2724401a8d04STejun Heo static bool wait_on_work(struct work_struct *work) 2725401a8d04STejun Heo { 2726401a8d04STejun Heo bool ret = false; 2727401a8d04STejun Heo int cpu; 2728401a8d04STejun Heo 2729401a8d04STejun Heo might_sleep(); 2730401a8d04STejun Heo 2731401a8d04STejun Heo lock_map_acquire(&work->lockdep_map); 2732401a8d04STejun Heo lock_map_release(&work->lockdep_map); 2733401a8d04STejun Heo 2734401a8d04STejun Heo for_each_gcwq_cpu(cpu) 2735401a8d04STejun Heo ret |= wait_on_cpu_work(get_gcwq(cpu), work); 2736401a8d04STejun Heo return ret; 2737401a8d04STejun Heo } 2738401a8d04STejun Heo 273909383498STejun Heo /** 274009383498STejun Heo * flush_work_sync - wait until a work has finished execution 274109383498STejun Heo * @work: the work to flush 274209383498STejun Heo * 274309383498STejun Heo * Wait until @work has finished execution. On return, it's 274409383498STejun Heo * guaranteed that all queueing instances of @work which happened 274509383498STejun Heo * before this function is called are finished. In other words, if 274609383498STejun Heo * @work hasn't been requeued since this function was called, @work is 274709383498STejun Heo * guaranteed to be idle on return. 274809383498STejun Heo * 274909383498STejun Heo * RETURNS: 275009383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 275109383498STejun Heo * %false if it was already idle. 275209383498STejun Heo */ 275309383498STejun Heo bool flush_work_sync(struct work_struct *work) 275409383498STejun Heo { 275509383498STejun Heo struct wq_barrier barr; 275609383498STejun Heo bool pending, waited; 275709383498STejun Heo 275809383498STejun Heo /* we'll wait for executions separately, queue barr only if pending */ 275909383498STejun Heo pending = start_flush_work(work, &barr, false); 276009383498STejun Heo 276109383498STejun Heo /* wait for executions to finish */ 276209383498STejun Heo waited = wait_on_work(work); 276309383498STejun Heo 276409383498STejun Heo /* wait for the pending one */ 276509383498STejun Heo if (pending) { 276609383498STejun Heo wait_for_completion(&barr.done); 276709383498STejun Heo destroy_work_on_stack(&barr.work); 276809383498STejun Heo } 276909383498STejun Heo 277009383498STejun Heo return pending || waited; 277109383498STejun Heo } 277209383498STejun Heo EXPORT_SYMBOL_GPL(flush_work_sync); 277309383498STejun Heo 27746e84d644SOleg Nesterov /* 27751f1f642eSOleg Nesterov * Upon a successful return (>= 0), the caller "owns" WORK_STRUCT_PENDING bit, 27766e84d644SOleg Nesterov * so this work can't be re-armed in any way. 27776e84d644SOleg Nesterov */ 27786e84d644SOleg Nesterov static int try_to_grab_pending(struct work_struct *work) 27796e84d644SOleg Nesterov { 27808b03ae3cSTejun Heo struct global_cwq *gcwq; 27811f1f642eSOleg Nesterov int ret = -1; 27826e84d644SOleg Nesterov 278322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 27841f1f642eSOleg Nesterov return 0; 27856e84d644SOleg Nesterov 27866e84d644SOleg Nesterov /* 27876e84d644SOleg Nesterov * The queueing is in progress, or it is already queued. Try to 27886e84d644SOleg Nesterov * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 27896e84d644SOleg Nesterov */ 27907a22ad75STejun Heo gcwq = get_work_gcwq(work); 27917a22ad75STejun Heo if (!gcwq) 27926e84d644SOleg Nesterov return ret; 27936e84d644SOleg Nesterov 27948b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 27956e84d644SOleg Nesterov if (!list_empty(&work->entry)) { 27966e84d644SOleg Nesterov /* 27977a22ad75STejun Heo * This work is queued, but perhaps we locked the wrong gcwq. 27986e84d644SOleg Nesterov * In that case we must see the new value after rmb(), see 27996e84d644SOleg Nesterov * insert_work()->wmb(). 28006e84d644SOleg Nesterov */ 28016e84d644SOleg Nesterov smp_rmb(); 28027a22ad75STejun Heo if (gcwq == get_work_gcwq(work)) { 2803dc186ad7SThomas Gleixner debug_work_deactivate(work); 28046e84d644SOleg Nesterov list_del_init(&work->entry); 28057a22ad75STejun Heo cwq_dec_nr_in_flight(get_work_cwq(work), 28068a2e8e5dSTejun Heo get_work_color(work), 28078a2e8e5dSTejun Heo *work_data_bits(work) & WORK_STRUCT_DELAYED); 28086e84d644SOleg Nesterov ret = 1; 28096e84d644SOleg Nesterov } 28106e84d644SOleg Nesterov } 28118b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 28126e84d644SOleg Nesterov 28136e84d644SOleg Nesterov return ret; 28146e84d644SOleg Nesterov } 28156e84d644SOleg Nesterov 2816401a8d04STejun Heo static bool __cancel_work_timer(struct work_struct *work, 28171f1f642eSOleg Nesterov struct timer_list* timer) 28181f1f642eSOleg Nesterov { 28191f1f642eSOleg Nesterov int ret; 28201f1f642eSOleg Nesterov 28211f1f642eSOleg Nesterov do { 28221f1f642eSOleg Nesterov ret = (timer && likely(del_timer(timer))); 28231f1f642eSOleg Nesterov if (!ret) 28241f1f642eSOleg Nesterov ret = try_to_grab_pending(work); 28251f1f642eSOleg Nesterov wait_on_work(work); 28261f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28271f1f642eSOleg Nesterov 28287a22ad75STejun Heo clear_work_data(work); 28291f1f642eSOleg Nesterov return ret; 28301f1f642eSOleg Nesterov } 28311f1f642eSOleg Nesterov 28326e84d644SOleg Nesterov /** 2833401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2834401a8d04STejun Heo * @work: the work to cancel 28356e84d644SOleg Nesterov * 2836401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2837401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2838401a8d04STejun Heo * another workqueue. On return from this function, @work is 2839401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28401f1f642eSOleg Nesterov * 2841401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2842401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28436e84d644SOleg Nesterov * 2844401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28456e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2846401a8d04STejun Heo * 2847401a8d04STejun Heo * RETURNS: 2848401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28496e84d644SOleg Nesterov */ 2850401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28516e84d644SOleg Nesterov { 28521f1f642eSOleg Nesterov return __cancel_work_timer(work, NULL); 2853b89deed3SOleg Nesterov } 285428e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2855b89deed3SOleg Nesterov 28566e84d644SOleg Nesterov /** 2857401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2858401a8d04STejun Heo * @dwork: the delayed work to flush 28596e84d644SOleg Nesterov * 2860401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2861401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2862401a8d04STejun Heo * considers the last queueing instance of @dwork. 28631f1f642eSOleg Nesterov * 2864401a8d04STejun Heo * RETURNS: 2865401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2866401a8d04STejun Heo * %false if it was already idle. 28676e84d644SOleg Nesterov */ 2868401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2869401a8d04STejun Heo { 2870401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 2871401a8d04STejun Heo __queue_work(raw_smp_processor_id(), 2872401a8d04STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 2873401a8d04STejun Heo return flush_work(&dwork->work); 2874401a8d04STejun Heo } 2875401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2876401a8d04STejun Heo 2877401a8d04STejun Heo /** 287809383498STejun Heo * flush_delayed_work_sync - wait for a dwork to finish 287909383498STejun Heo * @dwork: the delayed work to flush 288009383498STejun Heo * 288109383498STejun Heo * Delayed timer is cancelled and the pending work is queued for 288209383498STejun Heo * execution immediately. Other than timer handling, its behavior 288309383498STejun Heo * is identical to flush_work_sync(). 288409383498STejun Heo * 288509383498STejun Heo * RETURNS: 288609383498STejun Heo * %true if flush_work_sync() waited for the work to finish execution, 288709383498STejun Heo * %false if it was already idle. 288809383498STejun Heo */ 288909383498STejun Heo bool flush_delayed_work_sync(struct delayed_work *dwork) 289009383498STejun Heo { 289109383498STejun Heo if (del_timer_sync(&dwork->timer)) 289209383498STejun Heo __queue_work(raw_smp_processor_id(), 289309383498STejun Heo get_work_cwq(&dwork->work)->wq, &dwork->work); 289409383498STejun Heo return flush_work_sync(&dwork->work); 289509383498STejun Heo } 289609383498STejun Heo EXPORT_SYMBOL(flush_delayed_work_sync); 289709383498STejun Heo 289809383498STejun Heo /** 2899401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2900401a8d04STejun Heo * @dwork: the delayed work cancel 2901401a8d04STejun Heo * 2902401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2903401a8d04STejun Heo * 2904401a8d04STejun Heo * RETURNS: 2905401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2906401a8d04STejun Heo */ 2907401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29086e84d644SOleg Nesterov { 29091f1f642eSOleg Nesterov return __cancel_work_timer(&dwork->work, &dwork->timer); 29106e84d644SOleg Nesterov } 2911f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29121da177e4SLinus Torvalds 29130fcb78c2SRolf Eike Beer /** 29140fcb78c2SRolf Eike Beer * schedule_work - put work task in global workqueue 29150fcb78c2SRolf Eike Beer * @work: job to be done 29160fcb78c2SRolf Eike Beer * 29175b0f437dSBart Van Assche * Returns zero if @work was already on the kernel-global workqueue and 29185b0f437dSBart Van Assche * non-zero otherwise. 29195b0f437dSBart Van Assche * 29205b0f437dSBart Van Assche * This puts a job in the kernel-global workqueue if it was not already 29215b0f437dSBart Van Assche * queued and leaves it in the same position on the kernel-global 29225b0f437dSBart Van Assche * workqueue otherwise. 29230fcb78c2SRolf Eike Beer */ 29247ad5b3a5SHarvey Harrison int schedule_work(struct work_struct *work) 29251da177e4SLinus Torvalds { 2926d320c038STejun Heo return queue_work(system_wq, work); 29271da177e4SLinus Torvalds } 2928ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_work); 29291da177e4SLinus Torvalds 2930c1a220e7SZhang Rui /* 2931c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2932c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2933c1a220e7SZhang Rui * @work: job to be done 2934c1a220e7SZhang Rui * 2935c1a220e7SZhang Rui * This puts a job on a specific cpu 2936c1a220e7SZhang Rui */ 2937c1a220e7SZhang Rui int schedule_work_on(int cpu, struct work_struct *work) 2938c1a220e7SZhang Rui { 2939d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2940c1a220e7SZhang Rui } 2941c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2942c1a220e7SZhang Rui 29430fcb78c2SRolf Eike Beer /** 29440fcb78c2SRolf Eike Beer * schedule_delayed_work - put work task in global workqueue after delay 294552bad64dSDavid Howells * @dwork: job to be done 294652bad64dSDavid Howells * @delay: number of jiffies to wait or 0 for immediate execution 29470fcb78c2SRolf Eike Beer * 29480fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29490fcb78c2SRolf Eike Beer * workqueue. 29500fcb78c2SRolf Eike Beer */ 29517ad5b3a5SHarvey Harrison int schedule_delayed_work(struct delayed_work *dwork, 295282f67cd9SIngo Molnar unsigned long delay) 29531da177e4SLinus Torvalds { 2954d320c038STejun Heo return queue_delayed_work(system_wq, dwork, delay); 29551da177e4SLinus Torvalds } 2956ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work); 29571da177e4SLinus Torvalds 29580fcb78c2SRolf Eike Beer /** 29590fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29600fcb78c2SRolf Eike Beer * @cpu: cpu to use 296152bad64dSDavid Howells * @dwork: job to be done 29620fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29630fcb78c2SRolf Eike Beer * 29640fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29650fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29660fcb78c2SRolf Eike Beer */ 29671da177e4SLinus Torvalds int schedule_delayed_work_on(int cpu, 296852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 29691da177e4SLinus Torvalds { 2970d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29711da177e4SLinus Torvalds } 2972ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29731da177e4SLinus Torvalds 2974b6136773SAndrew Morton /** 297531ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2976b6136773SAndrew Morton * @func: the function to call 2977b6136773SAndrew Morton * 297831ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 297931ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2980b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 298131ddd871STejun Heo * 298231ddd871STejun Heo * RETURNS: 298331ddd871STejun Heo * 0 on success, -errno on failure. 2984b6136773SAndrew Morton */ 298565f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 298615316ba8SChristoph Lameter { 298715316ba8SChristoph Lameter int cpu; 298838f51568SNamhyung Kim struct work_struct __percpu *works; 298915316ba8SChristoph Lameter 2990b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2991b6136773SAndrew Morton if (!works) 299215316ba8SChristoph Lameter return -ENOMEM; 2993b6136773SAndrew Morton 299495402b38SGautham R Shenoy get_online_cpus(); 299593981800STejun Heo 299615316ba8SChristoph Lameter for_each_online_cpu(cpu) { 29979bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 29989bfb1839SIngo Molnar 29999bfb1839SIngo Molnar INIT_WORK(work, func); 30008de6d308SOleg Nesterov schedule_work_on(cpu, work); 300115316ba8SChristoph Lameter } 300293981800STejun Heo 300393981800STejun Heo for_each_online_cpu(cpu) 30048616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 300593981800STejun Heo 300695402b38SGautham R Shenoy put_online_cpus(); 3007b6136773SAndrew Morton free_percpu(works); 300815316ba8SChristoph Lameter return 0; 300915316ba8SChristoph Lameter } 301015316ba8SChristoph Lameter 3011eef6a7d5SAlan Stern /** 3012eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3013eef6a7d5SAlan Stern * 3014eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3015eef6a7d5SAlan Stern * completion. 3016eef6a7d5SAlan Stern * 3017eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3018eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3019eef6a7d5SAlan Stern * will lead to deadlock: 3020eef6a7d5SAlan Stern * 3021eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3022eef6a7d5SAlan Stern * a lock held by your code or its caller. 3023eef6a7d5SAlan Stern * 3024eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3025eef6a7d5SAlan Stern * 3026eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3027eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3028eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3029eef6a7d5SAlan Stern * 3030eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3031eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3032eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3033eef6a7d5SAlan Stern * cancel_work_sync() instead. 3034eef6a7d5SAlan Stern */ 30351da177e4SLinus Torvalds void flush_scheduled_work(void) 30361da177e4SLinus Torvalds { 3037d320c038STejun Heo flush_workqueue(system_wq); 30381da177e4SLinus Torvalds } 3039ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds /** 30421fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30431fa44ecaSJames Bottomley * @fn: the function to execute 30441fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30451fa44ecaSJames Bottomley * be available when the work executes) 30461fa44ecaSJames Bottomley * 30471fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30481fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30491fa44ecaSJames Bottomley * 30501fa44ecaSJames Bottomley * Returns: 0 - function was executed 30511fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30521fa44ecaSJames Bottomley */ 305365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30541fa44ecaSJames Bottomley { 30551fa44ecaSJames Bottomley if (!in_interrupt()) { 305665f27f38SDavid Howells fn(&ew->work); 30571fa44ecaSJames Bottomley return 0; 30581fa44ecaSJames Bottomley } 30591fa44ecaSJames Bottomley 306065f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30611fa44ecaSJames Bottomley schedule_work(&ew->work); 30621fa44ecaSJames Bottomley 30631fa44ecaSJames Bottomley return 1; 30641fa44ecaSJames Bottomley } 30651fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30661fa44ecaSJames Bottomley 30671da177e4SLinus Torvalds int keventd_up(void) 30681da177e4SLinus Torvalds { 3069d320c038STejun Heo return system_wq != NULL; 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds 3072bdbc5dd7STejun Heo static int alloc_cwqs(struct workqueue_struct *wq) 30731da177e4SLinus Torvalds { 30743af24433SOleg Nesterov /* 30750f900049STejun Heo * cwqs are forced aligned according to WORK_STRUCT_FLAG_BITS. 30760f900049STejun Heo * Make sure that the alignment isn't lower than that of 30770f900049STejun Heo * unsigned long long. 30783af24433SOleg Nesterov */ 30790f900049STejun Heo const size_t size = sizeof(struct cpu_workqueue_struct); 30800f900049STejun Heo const size_t align = max_t(size_t, 1 << WORK_STRUCT_FLAG_BITS, 30810f900049STejun Heo __alignof__(unsigned long long)); 30823af24433SOleg Nesterov 3083e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3084f3421797STejun Heo wq->cpu_wq.pcpu = __alloc_percpu(size, align); 3085931ac77eSTejun Heo else { 30860f900049STejun Heo void *ptr; 3087e1d8aa9fSFrederic Weisbecker 30880f900049STejun Heo /* 3089f3421797STejun Heo * Allocate enough room to align cwq and put an extra 3090f3421797STejun Heo * pointer at the end pointing back to the originally 3091f3421797STejun Heo * allocated pointer which will be used for free. 30920f900049STejun Heo */ 3093bdbc5dd7STejun Heo ptr = kzalloc(size + align + sizeof(void *), GFP_KERNEL); 3094bdbc5dd7STejun Heo if (ptr) { 3095bdbc5dd7STejun Heo wq->cpu_wq.single = PTR_ALIGN(ptr, align); 3096bdbc5dd7STejun Heo *(void **)(wq->cpu_wq.single + 1) = ptr; 3097bdbc5dd7STejun Heo } 30983af24433SOleg Nesterov } 30993af24433SOleg Nesterov 31000415b00dSTejun Heo /* just in case, make sure it's actually aligned */ 3101bdbc5dd7STejun Heo BUG_ON(!IS_ALIGNED(wq->cpu_wq.v, align)); 3102bdbc5dd7STejun Heo return wq->cpu_wq.v ? 0 : -ENOMEM; 31030f900049STejun Heo } 31040f900049STejun Heo 3105bdbc5dd7STejun Heo static void free_cwqs(struct workqueue_struct *wq) 310606ba38a9SOleg Nesterov { 3107e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3108bdbc5dd7STejun Heo free_percpu(wq->cpu_wq.pcpu); 3109f3421797STejun Heo else if (wq->cpu_wq.single) { 3110f3421797STejun Heo /* the pointer to free is stored right after the cwq */ 3111f3421797STejun Heo kfree(*(void **)(wq->cpu_wq.single + 1)); 311206ba38a9SOleg Nesterov } 311306ba38a9SOleg Nesterov } 311406ba38a9SOleg Nesterov 3115f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3116f3421797STejun Heo const char *name) 3117b71ab8c2STejun Heo { 3118f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3119f3421797STejun Heo 3120f3421797STejun Heo if (max_active < 1 || max_active > lim) 3121b71ab8c2STejun Heo printk(KERN_WARNING "workqueue: max_active %d requested for %s " 3122b71ab8c2STejun Heo "is out of range, clamping between %d and %d\n", 3123f3421797STejun Heo max_active, name, 1, lim); 3124b71ab8c2STejun Heo 3125f3421797STejun Heo return clamp_val(max_active, 1, lim); 3126b71ab8c2STejun Heo } 3127b71ab8c2STejun Heo 3128b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 312997e37d7bSTejun Heo unsigned int flags, 31301e19ffc6STejun Heo int max_active, 3131eb13ba87SJohannes Berg struct lock_class_key *key, 3132b196be89STejun Heo const char *lock_name, ...) 31333af24433SOleg Nesterov { 3134b196be89STejun Heo va_list args, args1; 31353af24433SOleg Nesterov struct workqueue_struct *wq; 3136c34056a3STejun Heo unsigned int cpu; 3137b196be89STejun Heo size_t namelen; 3138b196be89STejun Heo 3139b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3140b196be89STejun Heo va_start(args, lock_name); 3141b196be89STejun Heo va_copy(args1, args); 3142b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3143b196be89STejun Heo 3144b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3145b196be89STejun Heo if (!wq) 3146b196be89STejun Heo goto err; 3147b196be89STejun Heo 3148b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3149b196be89STejun Heo va_end(args); 3150b196be89STejun Heo va_end(args1); 31513af24433SOleg Nesterov 3152f3421797STejun Heo /* 31536370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 31546370a6adSTejun Heo * have a rescuer to guarantee forward progress. 31556370a6adSTejun Heo */ 31566370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 31576370a6adSTejun Heo flags |= WQ_RESCUER; 31586370a6adSTejun Heo 3159d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3160b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 31613af24433SOleg Nesterov 3162b196be89STejun Heo /* init wq */ 316397e37d7bSTejun Heo wq->flags = flags; 3164a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 316573f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 316673f53c4aSTejun Heo atomic_set(&wq->nr_cwqs_to_flush, 0); 316773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 316873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 31693af24433SOleg Nesterov 3170eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3171cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 31723af24433SOleg Nesterov 3173bdbc5dd7STejun Heo if (alloc_cwqs(wq) < 0) 3174bdbc5dd7STejun Heo goto err; 3175bdbc5dd7STejun Heo 3176f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 31771537663fSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 31788b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 31793270476aSTejun Heo int pool_idx = (bool)(flags & WQ_HIGHPRI); 31801537663fSTejun Heo 31810f900049STejun Heo BUG_ON((unsigned long)cwq & WORK_STRUCT_FLAG_MASK); 31823270476aSTejun Heo cwq->pool = &gcwq->pools[pool_idx]; 3183c34056a3STejun Heo cwq->wq = wq; 318473f53c4aSTejun Heo cwq->flush_color = -1; 31851e19ffc6STejun Heo cwq->max_active = max_active; 31861e19ffc6STejun Heo INIT_LIST_HEAD(&cwq->delayed_works); 3187e22bee78STejun Heo } 31881537663fSTejun Heo 3189e22bee78STejun Heo if (flags & WQ_RESCUER) { 3190e22bee78STejun Heo struct worker *rescuer; 3191e22bee78STejun Heo 3192f2e005aaSTejun Heo if (!alloc_mayday_mask(&wq->mayday_mask, GFP_KERNEL)) 3193e22bee78STejun Heo goto err; 3194e22bee78STejun Heo 3195e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3196e22bee78STejun Heo if (!rescuer) 3197e22bee78STejun Heo goto err; 3198e22bee78STejun Heo 3199b196be89STejun Heo rescuer->task = kthread_create(rescuer_thread, wq, "%s", 3200b196be89STejun Heo wq->name); 3201e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3202e22bee78STejun Heo goto err; 3203e22bee78STejun Heo 3204e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3205e22bee78STejun Heo wake_up_process(rescuer->task); 32063af24433SOleg Nesterov } 32071537663fSTejun Heo 32083af24433SOleg Nesterov /* 3209a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3210a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3211a0a1a5fdSTejun Heo * workqueue to workqueues list. 32123af24433SOleg Nesterov */ 32133af24433SOleg Nesterov spin_lock(&workqueue_lock); 3214a0a1a5fdSTejun Heo 321558a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 3216f3421797STejun Heo for_each_cwq_cpu(cpu, wq) 3217a0a1a5fdSTejun Heo get_cwq(cpu, wq)->max_active = 0; 3218a0a1a5fdSTejun Heo 32193af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3220a0a1a5fdSTejun Heo 32213af24433SOleg Nesterov spin_unlock(&workqueue_lock); 32223af24433SOleg Nesterov 32233af24433SOleg Nesterov return wq; 32244690c4abSTejun Heo err: 32254690c4abSTejun Heo if (wq) { 3226bdbc5dd7STejun Heo free_cwqs(wq); 3227f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 3228e22bee78STejun Heo kfree(wq->rescuer); 32294690c4abSTejun Heo kfree(wq); 32303af24433SOleg Nesterov } 32314690c4abSTejun Heo return NULL; 32321da177e4SLinus Torvalds } 3233d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 32341da177e4SLinus Torvalds 32353af24433SOleg Nesterov /** 32363af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 32373af24433SOleg Nesterov * @wq: target workqueue 32383af24433SOleg Nesterov * 32393af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 32403af24433SOleg Nesterov */ 32413af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 32423af24433SOleg Nesterov { 3243c8e55f36STejun Heo unsigned int cpu; 32443af24433SOleg Nesterov 32459c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 32469c5a2ba7STejun Heo drain_workqueue(wq); 3247c8efcc25STejun Heo 3248a0a1a5fdSTejun Heo /* 3249a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3250a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3251a0a1a5fdSTejun Heo */ 325295402b38SGautham R Shenoy spin_lock(&workqueue_lock); 32533af24433SOleg Nesterov list_del(&wq->list); 325495402b38SGautham R Shenoy spin_unlock(&workqueue_lock); 32553af24433SOleg Nesterov 3256e22bee78STejun Heo /* sanity check */ 3257f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 325873f53c4aSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 325973f53c4aSTejun Heo int i; 32603af24433SOleg Nesterov 326173f53c4aSTejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 326273f53c4aSTejun Heo BUG_ON(cwq->nr_in_flight[i]); 32631e19ffc6STejun Heo BUG_ON(cwq->nr_active); 32641e19ffc6STejun Heo BUG_ON(!list_empty(&cwq->delayed_works)); 326573f53c4aSTejun Heo } 32661537663fSTejun Heo 3267e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3268e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3269f2e005aaSTejun Heo free_mayday_mask(wq->mayday_mask); 32708d9df9f0SXiaotian Feng kfree(wq->rescuer); 3271e22bee78STejun Heo } 3272e22bee78STejun Heo 3273bdbc5dd7STejun Heo free_cwqs(wq); 32743af24433SOleg Nesterov kfree(wq); 32753af24433SOleg Nesterov } 32763af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 32773af24433SOleg Nesterov 3278dcd989cbSTejun Heo /** 3279dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3280dcd989cbSTejun Heo * @wq: target workqueue 3281dcd989cbSTejun Heo * @max_active: new max_active value. 3282dcd989cbSTejun Heo * 3283dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3284dcd989cbSTejun Heo * 3285dcd989cbSTejun Heo * CONTEXT: 3286dcd989cbSTejun Heo * Don't call from IRQ context. 3287dcd989cbSTejun Heo */ 3288dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3289dcd989cbSTejun Heo { 3290dcd989cbSTejun Heo unsigned int cpu; 3291dcd989cbSTejun Heo 3292f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3293dcd989cbSTejun Heo 3294dcd989cbSTejun Heo spin_lock(&workqueue_lock); 3295dcd989cbSTejun Heo 3296dcd989cbSTejun Heo wq->saved_max_active = max_active; 3297dcd989cbSTejun Heo 3298f3421797STejun Heo for_each_cwq_cpu(cpu, wq) { 3299dcd989cbSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3300dcd989cbSTejun Heo 3301dcd989cbSTejun Heo spin_lock_irq(&gcwq->lock); 3302dcd989cbSTejun Heo 330358a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 3304dcd989cbSTejun Heo !(gcwq->flags & GCWQ_FREEZING)) 3305dcd989cbSTejun Heo get_cwq(gcwq->cpu, wq)->max_active = max_active; 3306dcd989cbSTejun Heo 3307dcd989cbSTejun Heo spin_unlock_irq(&gcwq->lock); 3308dcd989cbSTejun Heo } 3309dcd989cbSTejun Heo 3310dcd989cbSTejun Heo spin_unlock(&workqueue_lock); 3311dcd989cbSTejun Heo } 3312dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3313dcd989cbSTejun Heo 3314dcd989cbSTejun Heo /** 3315dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3316dcd989cbSTejun Heo * @cpu: CPU in question 3317dcd989cbSTejun Heo * @wq: target workqueue 3318dcd989cbSTejun Heo * 3319dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3320dcd989cbSTejun Heo * no synchronization around this function and the test result is 3321dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3322dcd989cbSTejun Heo * 3323dcd989cbSTejun Heo * RETURNS: 3324dcd989cbSTejun Heo * %true if congested, %false otherwise. 3325dcd989cbSTejun Heo */ 3326dcd989cbSTejun Heo bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq) 3327dcd989cbSTejun Heo { 3328dcd989cbSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3329dcd989cbSTejun Heo 3330dcd989cbSTejun Heo return !list_empty(&cwq->delayed_works); 3331dcd989cbSTejun Heo } 3332dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3333dcd989cbSTejun Heo 3334dcd989cbSTejun Heo /** 3335dcd989cbSTejun Heo * work_cpu - return the last known associated cpu for @work 3336dcd989cbSTejun Heo * @work: the work of interest 3337dcd989cbSTejun Heo * 3338dcd989cbSTejun Heo * RETURNS: 3339bdbc5dd7STejun Heo * CPU number if @work was ever queued. WORK_CPU_NONE otherwise. 3340dcd989cbSTejun Heo */ 3341dcd989cbSTejun Heo unsigned int work_cpu(struct work_struct *work) 3342dcd989cbSTejun Heo { 3343dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3344dcd989cbSTejun Heo 3345bdbc5dd7STejun Heo return gcwq ? gcwq->cpu : WORK_CPU_NONE; 3346dcd989cbSTejun Heo } 3347dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_cpu); 3348dcd989cbSTejun Heo 3349dcd989cbSTejun Heo /** 3350dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3351dcd989cbSTejun Heo * @work: the work to be tested 3352dcd989cbSTejun Heo * 3353dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3354dcd989cbSTejun Heo * synchronization around this function and the test result is 3355dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3356dcd989cbSTejun Heo * Especially for reentrant wqs, the pending state might hide the 3357dcd989cbSTejun Heo * running state. 3358dcd989cbSTejun Heo * 3359dcd989cbSTejun Heo * RETURNS: 3360dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3361dcd989cbSTejun Heo */ 3362dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3363dcd989cbSTejun Heo { 3364dcd989cbSTejun Heo struct global_cwq *gcwq = get_work_gcwq(work); 3365dcd989cbSTejun Heo unsigned long flags; 3366dcd989cbSTejun Heo unsigned int ret = 0; 3367dcd989cbSTejun Heo 3368dcd989cbSTejun Heo if (!gcwq) 3369dcd989cbSTejun Heo return false; 3370dcd989cbSTejun Heo 3371dcd989cbSTejun Heo spin_lock_irqsave(&gcwq->lock, flags); 3372dcd989cbSTejun Heo 3373dcd989cbSTejun Heo if (work_pending(work)) 3374dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3375dcd989cbSTejun Heo if (find_worker_executing_work(gcwq, work)) 3376dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3377dcd989cbSTejun Heo 3378dcd989cbSTejun Heo spin_unlock_irqrestore(&gcwq->lock, flags); 3379dcd989cbSTejun Heo 3380dcd989cbSTejun Heo return ret; 3381dcd989cbSTejun Heo } 3382dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3383dcd989cbSTejun Heo 3384db7bccf4STejun Heo /* 3385db7bccf4STejun Heo * CPU hotplug. 3386db7bccf4STejun Heo * 3387e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3388e22bee78STejun Heo * are a lot of assumptions on strong associations among work, cwq and 3389e22bee78STejun Heo * gcwq which make migrating pending and scheduled works very 3390e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 3391e22bee78STejun Heo * gcwqs serve mix of short, long and very long running works making 3392e22bee78STejun Heo * blocked draining impractical. 3393e22bee78STejun Heo * 3394628c78e7STejun Heo * This is solved by allowing a gcwq to be disassociated from the CPU 3395628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3396628c78e7STejun Heo * cpu comes back online. 3397db7bccf4STejun Heo */ 3398db7bccf4STejun Heo 339960373152STejun Heo /* claim manager positions of all pools */ 34008db25e78STejun Heo static void gcwq_claim_management_and_lock(struct global_cwq *gcwq) 340160373152STejun Heo { 340260373152STejun Heo struct worker_pool *pool; 340360373152STejun Heo 340460373152STejun Heo for_each_worker_pool(pool, gcwq) 340560373152STejun Heo mutex_lock_nested(&pool->manager_mutex, pool - gcwq->pools); 34068db25e78STejun Heo spin_lock_irq(&gcwq->lock); 340760373152STejun Heo } 340860373152STejun Heo 340960373152STejun Heo /* release manager positions */ 34108db25e78STejun Heo static void gcwq_release_management_and_unlock(struct global_cwq *gcwq) 341160373152STejun Heo { 341260373152STejun Heo struct worker_pool *pool; 341360373152STejun Heo 34148db25e78STejun Heo spin_unlock_irq(&gcwq->lock); 341560373152STejun Heo for_each_worker_pool(pool, gcwq) 341660373152STejun Heo mutex_unlock(&pool->manager_mutex); 341760373152STejun Heo } 341860373152STejun Heo 3419628c78e7STejun Heo static void gcwq_unbind_fn(struct work_struct *work) 3420db7bccf4STejun Heo { 3421628c78e7STejun Heo struct global_cwq *gcwq = get_gcwq(smp_processor_id()); 34224ce62e9eSTejun Heo struct worker_pool *pool; 3423db7bccf4STejun Heo struct worker *worker; 3424db7bccf4STejun Heo struct hlist_node *pos; 3425db7bccf4STejun Heo int i; 3426db7bccf4STejun Heo 3427db7bccf4STejun Heo BUG_ON(gcwq->cpu != smp_processor_id()); 3428db7bccf4STejun Heo 34298db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 3430e22bee78STejun Heo 3431f2d5a0eeSTejun Heo /* 3432f2d5a0eeSTejun Heo * We've claimed all manager positions. Make all workers unbound 3433f2d5a0eeSTejun Heo * and set DISASSOCIATED. Before this, all workers except for the 3434f2d5a0eeSTejun Heo * ones which are still executing works from before the last CPU 3435f2d5a0eeSTejun Heo * down must be on the cpu. After this, they may become diasporas. 3436f2d5a0eeSTejun Heo */ 343760373152STejun Heo for_each_worker_pool(pool, gcwq) 34384ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3439403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3440db7bccf4STejun Heo 3441db7bccf4STejun Heo for_each_busy_worker(worker, i, pos, gcwq) 3442403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3443db7bccf4STejun Heo 3444f2d5a0eeSTejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 3445f2d5a0eeSTejun Heo 34468db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 3447e22bee78STejun Heo 3448e22bee78STejun Heo /* 3449628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3450628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3451628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3452628c78e7STejun Heo */ 3453628c78e7STejun Heo schedule(); 3454628c78e7STejun Heo 3455628c78e7STejun Heo /* 3456628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3457628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 3458628c78e7STejun Heo * are always true as long as the worklist is not empty. @gcwq now 3459628c78e7STejun Heo * behaves as unbound (in terms of concurrency management) gcwq 3460628c78e7STejun Heo * which is served by workers tied to the CPU. 3461628c78e7STejun Heo * 3462628c78e7STejun Heo * On return from this function, the current worker would trigger 3463628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3464628c78e7STejun Heo * didn't already. 3465e22bee78STejun Heo */ 34664ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 34674ce62e9eSTejun Heo atomic_set(get_pool_nr_running(pool), 0); 3468db7bccf4STejun Heo } 3469db7bccf4STejun Heo 34708db25e78STejun Heo /* 34718db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 34728db25e78STejun Heo * This will be registered high priority CPU notifier. 34738db25e78STejun Heo */ 34748db25e78STejun Heo static int __devinit workqueue_cpu_up_callback(struct notifier_block *nfb, 34751da177e4SLinus Torvalds unsigned long action, 34761da177e4SLinus Torvalds void *hcpu) 34771da177e4SLinus Torvalds { 34783af24433SOleg Nesterov unsigned int cpu = (unsigned long)hcpu; 3479db7bccf4STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 34804ce62e9eSTejun Heo struct worker_pool *pool; 34811da177e4SLinus Torvalds 34828db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 34833af24433SOleg Nesterov case CPU_UP_PREPARE: 34844ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 34853ce63377STejun Heo struct worker *worker; 34863ce63377STejun Heo 34873ce63377STejun Heo if (pool->nr_workers) 34883ce63377STejun Heo continue; 34893ce63377STejun Heo 34903ce63377STejun Heo worker = create_worker(pool); 34913ce63377STejun Heo if (!worker) 34923ce63377STejun Heo return NOTIFY_BAD; 34933ce63377STejun Heo 34943ce63377STejun Heo spin_lock_irq(&gcwq->lock); 34953ce63377STejun Heo start_worker(worker); 34963ce63377STejun Heo spin_unlock_irq(&gcwq->lock); 34973af24433SOleg Nesterov } 34981da177e4SLinus Torvalds break; 34991da177e4SLinus Torvalds 350065758202STejun Heo case CPU_DOWN_FAILED: 350165758202STejun Heo case CPU_ONLINE: 35028db25e78STejun Heo gcwq_claim_management_and_lock(gcwq); 35038db25e78STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 35048db25e78STejun Heo rebind_workers(gcwq); 35058db25e78STejun Heo gcwq_release_management_and_unlock(gcwq); 35068db25e78STejun Heo break; 350765758202STejun Heo } 350865758202STejun Heo return NOTIFY_OK; 350965758202STejun Heo } 351065758202STejun Heo 351165758202STejun Heo /* 351265758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 351365758202STejun Heo * This will be registered as low priority CPU notifier. 351465758202STejun Heo */ 351565758202STejun Heo static int __devinit workqueue_cpu_down_callback(struct notifier_block *nfb, 351665758202STejun Heo unsigned long action, 351765758202STejun Heo void *hcpu) 351865758202STejun Heo { 35198db25e78STejun Heo unsigned int cpu = (unsigned long)hcpu; 35208db25e78STejun Heo struct work_struct unbind_work; 35218db25e78STejun Heo 352265758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 352365758202STejun Heo case CPU_DOWN_PREPARE: 35248db25e78STejun Heo /* unbinding should happen on the local CPU */ 35258db25e78STejun Heo INIT_WORK_ONSTACK(&unbind_work, gcwq_unbind_fn); 35268db25e78STejun Heo schedule_work_on(cpu, &unbind_work); 35278db25e78STejun Heo flush_work(&unbind_work); 35288db25e78STejun Heo break; 352965758202STejun Heo } 353065758202STejun Heo return NOTIFY_OK; 353165758202STejun Heo } 353265758202STejun Heo 35332d3854a3SRusty Russell #ifdef CONFIG_SMP 35348ccad40dSRusty Russell 35352d3854a3SRusty Russell struct work_for_cpu { 35366b44003eSAndrew Morton struct completion completion; 35372d3854a3SRusty Russell long (*fn)(void *); 35382d3854a3SRusty Russell void *arg; 35392d3854a3SRusty Russell long ret; 35402d3854a3SRusty Russell }; 35412d3854a3SRusty Russell 35426b44003eSAndrew Morton static int do_work_for_cpu(void *_wfc) 35432d3854a3SRusty Russell { 35446b44003eSAndrew Morton struct work_for_cpu *wfc = _wfc; 35452d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 35466b44003eSAndrew Morton complete(&wfc->completion); 35476b44003eSAndrew Morton return 0; 35482d3854a3SRusty Russell } 35492d3854a3SRusty Russell 35502d3854a3SRusty Russell /** 35512d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 35522d3854a3SRusty Russell * @cpu: the cpu to run on 35532d3854a3SRusty Russell * @fn: the function to run 35542d3854a3SRusty Russell * @arg: the function arg 35552d3854a3SRusty Russell * 355631ad9081SRusty Russell * This will return the value @fn returns. 355731ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 35586b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 35592d3854a3SRusty Russell */ 35602d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) 35612d3854a3SRusty Russell { 35626b44003eSAndrew Morton struct task_struct *sub_thread; 35636b44003eSAndrew Morton struct work_for_cpu wfc = { 35646b44003eSAndrew Morton .completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion), 35656b44003eSAndrew Morton .fn = fn, 35666b44003eSAndrew Morton .arg = arg, 35676b44003eSAndrew Morton }; 35682d3854a3SRusty Russell 35696b44003eSAndrew Morton sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu"); 35706b44003eSAndrew Morton if (IS_ERR(sub_thread)) 35716b44003eSAndrew Morton return PTR_ERR(sub_thread); 35726b44003eSAndrew Morton kthread_bind(sub_thread, cpu); 35736b44003eSAndrew Morton wake_up_process(sub_thread); 35746b44003eSAndrew Morton wait_for_completion(&wfc.completion); 35752d3854a3SRusty Russell return wfc.ret; 35762d3854a3SRusty Russell } 35772d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 35782d3854a3SRusty Russell #endif /* CONFIG_SMP */ 35792d3854a3SRusty Russell 3580a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3581e7577c50SRusty Russell 3582a0a1a5fdSTejun Heo /** 3583a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3584a0a1a5fdSTejun Heo * 358558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 358658a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 358758a69cb4STejun Heo * gcwq->worklist. 3588a0a1a5fdSTejun Heo * 3589a0a1a5fdSTejun Heo * CONTEXT: 35908b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3591a0a1a5fdSTejun Heo */ 3592a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3593a0a1a5fdSTejun Heo { 3594a0a1a5fdSTejun Heo unsigned int cpu; 3595a0a1a5fdSTejun Heo 3596a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3597a0a1a5fdSTejun Heo 3598a0a1a5fdSTejun Heo BUG_ON(workqueue_freezing); 3599a0a1a5fdSTejun Heo workqueue_freezing = true; 3600a0a1a5fdSTejun Heo 3601f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36028b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 3603bdbc5dd7STejun Heo struct workqueue_struct *wq; 36048b03ae3cSTejun Heo 36058b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36068b03ae3cSTejun Heo 3607db7bccf4STejun Heo BUG_ON(gcwq->flags & GCWQ_FREEZING); 3608db7bccf4STejun Heo gcwq->flags |= GCWQ_FREEZING; 3609db7bccf4STejun Heo 3610a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3611a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3612a0a1a5fdSTejun Heo 361358a69cb4STejun Heo if (cwq && wq->flags & WQ_FREEZABLE) 3614a0a1a5fdSTejun Heo cwq->max_active = 0; 36151da177e4SLinus Torvalds } 36168b03ae3cSTejun Heo 36178b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3618a0a1a5fdSTejun Heo } 3619a0a1a5fdSTejun Heo 3620a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3621a0a1a5fdSTejun Heo } 3622a0a1a5fdSTejun Heo 3623a0a1a5fdSTejun Heo /** 362458a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3625a0a1a5fdSTejun Heo * 3626a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3627a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3628a0a1a5fdSTejun Heo * 3629a0a1a5fdSTejun Heo * CONTEXT: 3630a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3631a0a1a5fdSTejun Heo * 3632a0a1a5fdSTejun Heo * RETURNS: 363358a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 363458a69cb4STejun Heo * is complete. 3635a0a1a5fdSTejun Heo */ 3636a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3637a0a1a5fdSTejun Heo { 3638a0a1a5fdSTejun Heo unsigned int cpu; 3639a0a1a5fdSTejun Heo bool busy = false; 3640a0a1a5fdSTejun Heo 3641a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3642a0a1a5fdSTejun Heo 3643a0a1a5fdSTejun Heo BUG_ON(!workqueue_freezing); 3644a0a1a5fdSTejun Heo 3645f3421797STejun Heo for_each_gcwq_cpu(cpu) { 3646bdbc5dd7STejun Heo struct workqueue_struct *wq; 3647a0a1a5fdSTejun Heo /* 3648a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3649a0a1a5fdSTejun Heo * to peek without lock. 3650a0a1a5fdSTejun Heo */ 3651a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3652a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3653a0a1a5fdSTejun Heo 365458a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3655a0a1a5fdSTejun Heo continue; 3656a0a1a5fdSTejun Heo 3657a0a1a5fdSTejun Heo BUG_ON(cwq->nr_active < 0); 3658a0a1a5fdSTejun Heo if (cwq->nr_active) { 3659a0a1a5fdSTejun Heo busy = true; 3660a0a1a5fdSTejun Heo goto out_unlock; 3661a0a1a5fdSTejun Heo } 3662a0a1a5fdSTejun Heo } 3663a0a1a5fdSTejun Heo } 3664a0a1a5fdSTejun Heo out_unlock: 3665a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3666a0a1a5fdSTejun Heo return busy; 3667a0a1a5fdSTejun Heo } 3668a0a1a5fdSTejun Heo 3669a0a1a5fdSTejun Heo /** 3670a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3671a0a1a5fdSTejun Heo * 3672a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 36737e11629dSTejun Heo * frozen works are transferred to their respective gcwq worklists. 3674a0a1a5fdSTejun Heo * 3675a0a1a5fdSTejun Heo * CONTEXT: 36768b03ae3cSTejun Heo * Grabs and releases workqueue_lock and gcwq->lock's. 3677a0a1a5fdSTejun Heo */ 3678a0a1a5fdSTejun Heo void thaw_workqueues(void) 3679a0a1a5fdSTejun Heo { 3680a0a1a5fdSTejun Heo unsigned int cpu; 3681a0a1a5fdSTejun Heo 3682a0a1a5fdSTejun Heo spin_lock(&workqueue_lock); 3683a0a1a5fdSTejun Heo 3684a0a1a5fdSTejun Heo if (!workqueue_freezing) 3685a0a1a5fdSTejun Heo goto out_unlock; 3686a0a1a5fdSTejun Heo 3687f3421797STejun Heo for_each_gcwq_cpu(cpu) { 36888b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 36894ce62e9eSTejun Heo struct worker_pool *pool; 3690bdbc5dd7STejun Heo struct workqueue_struct *wq; 36918b03ae3cSTejun Heo 36928b03ae3cSTejun Heo spin_lock_irq(&gcwq->lock); 36938b03ae3cSTejun Heo 3694db7bccf4STejun Heo BUG_ON(!(gcwq->flags & GCWQ_FREEZING)); 3695db7bccf4STejun Heo gcwq->flags &= ~GCWQ_FREEZING; 3696db7bccf4STejun Heo 3697a0a1a5fdSTejun Heo list_for_each_entry(wq, &workqueues, list) { 3698a0a1a5fdSTejun Heo struct cpu_workqueue_struct *cwq = get_cwq(cpu, wq); 3699a0a1a5fdSTejun Heo 370058a69cb4STejun Heo if (!cwq || !(wq->flags & WQ_FREEZABLE)) 3701a0a1a5fdSTejun Heo continue; 3702a0a1a5fdSTejun Heo 3703a0a1a5fdSTejun Heo /* restore max_active and repopulate worklist */ 3704a0a1a5fdSTejun Heo cwq->max_active = wq->saved_max_active; 3705a0a1a5fdSTejun Heo 3706a0a1a5fdSTejun Heo while (!list_empty(&cwq->delayed_works) && 3707a0a1a5fdSTejun Heo cwq->nr_active < cwq->max_active) 3708a0a1a5fdSTejun Heo cwq_activate_first_delayed(cwq); 3709a0a1a5fdSTejun Heo } 37108b03ae3cSTejun Heo 37114ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) 37124ce62e9eSTejun Heo wake_up_worker(pool); 3713e22bee78STejun Heo 37148b03ae3cSTejun Heo spin_unlock_irq(&gcwq->lock); 3715a0a1a5fdSTejun Heo } 3716a0a1a5fdSTejun Heo 3717a0a1a5fdSTejun Heo workqueue_freezing = false; 3718a0a1a5fdSTejun Heo out_unlock: 3719a0a1a5fdSTejun Heo spin_unlock(&workqueue_lock); 3720a0a1a5fdSTejun Heo } 3721a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3722a0a1a5fdSTejun Heo 37236ee0578bSSuresh Siddha static int __init init_workqueues(void) 37241da177e4SLinus Torvalds { 3725c34056a3STejun Heo unsigned int cpu; 3726c8e55f36STejun Heo int i; 3727c34056a3STejun Heo 372865758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 372965758202STejun Heo cpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 37308b03ae3cSTejun Heo 37318b03ae3cSTejun Heo /* initialize gcwqs */ 3732f3421797STejun Heo for_each_gcwq_cpu(cpu) { 37338b03ae3cSTejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37344ce62e9eSTejun Heo struct worker_pool *pool; 37358b03ae3cSTejun Heo 37368b03ae3cSTejun Heo spin_lock_init(&gcwq->lock); 37378b03ae3cSTejun Heo gcwq->cpu = cpu; 3738f3421797STejun Heo gcwq->flags |= GCWQ_DISASSOCIATED; 37398b03ae3cSTejun Heo 3740c8e55f36STejun Heo for (i = 0; i < BUSY_WORKER_HASH_SIZE; i++) 3741c8e55f36STejun Heo INIT_HLIST_HEAD(&gcwq->busy_hash[i]); 3742c8e55f36STejun Heo 37434ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37444ce62e9eSTejun Heo pool->gcwq = gcwq; 37454ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->worklist); 37464ce62e9eSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 3747e22bee78STejun Heo 37484ce62e9eSTejun Heo init_timer_deferrable(&pool->idle_timer); 37494ce62e9eSTejun Heo pool->idle_timer.function = idle_worker_timeout; 37504ce62e9eSTejun Heo pool->idle_timer.data = (unsigned long)pool; 3751e22bee78STejun Heo 37524ce62e9eSTejun Heo setup_timer(&pool->mayday_timer, gcwq_mayday_timeout, 37534ce62e9eSTejun Heo (unsigned long)pool); 37544ce62e9eSTejun Heo 375560373152STejun Heo mutex_init(&pool->manager_mutex); 37564ce62e9eSTejun Heo ida_init(&pool->worker_ida); 37574ce62e9eSTejun Heo } 3758db7bccf4STejun Heo 375925511a47STejun Heo init_waitqueue_head(&gcwq->rebind_hold); 37608b03ae3cSTejun Heo } 37618b03ae3cSTejun Heo 3762e22bee78STejun Heo /* create the initial worker */ 3763f3421797STejun Heo for_each_online_gcwq_cpu(cpu) { 3764e22bee78STejun Heo struct global_cwq *gcwq = get_gcwq(cpu); 37654ce62e9eSTejun Heo struct worker_pool *pool; 3766e22bee78STejun Heo 3767477a3c33STejun Heo if (cpu != WORK_CPU_UNBOUND) 3768477a3c33STejun Heo gcwq->flags &= ~GCWQ_DISASSOCIATED; 37694ce62e9eSTejun Heo 37704ce62e9eSTejun Heo for_each_worker_pool(pool, gcwq) { 37714ce62e9eSTejun Heo struct worker *worker; 37724ce62e9eSTejun Heo 3773bc2ae0f5STejun Heo worker = create_worker(pool); 3774e22bee78STejun Heo BUG_ON(!worker); 3775e22bee78STejun Heo spin_lock_irq(&gcwq->lock); 3776e22bee78STejun Heo start_worker(worker); 3777e22bee78STejun Heo spin_unlock_irq(&gcwq->lock); 3778e22bee78STejun Heo } 37794ce62e9eSTejun Heo } 3780e22bee78STejun Heo 3781d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 3782d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 3783d320c038STejun Heo system_nrt_wq = alloc_workqueue("events_nrt", WQ_NON_REENTRANT, 0); 3784f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 3785f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 378624d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 378724d51addSTejun Heo WQ_FREEZABLE, 0); 378862d3c543SAlan Stern system_nrt_freezable_wq = alloc_workqueue("events_nrt_freezable", 378962d3c543SAlan Stern WQ_NON_REENTRANT | WQ_FREEZABLE, 0); 3790e5cba24eSHitoshi Mitake BUG_ON(!system_wq || !system_long_wq || !system_nrt_wq || 379162d3c543SAlan Stern !system_unbound_wq || !system_freezable_wq || 379262d3c543SAlan Stern !system_nrt_freezable_wq); 37936ee0578bSSuresh Siddha return 0; 37941da177e4SLinus Torvalds } 37956ee0578bSSuresh Siddha early_initcall(init_workqueues); 3796