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> 4429c91e99STejun Heo #include <linux/jhash.h> 4542f8570fSSasha Levin #include <linux/hashtable.h> 4676af4d93STejun Heo #include <linux/rculist.h> 47e22bee78STejun Heo 48ea138446STejun Heo #include "workqueue_internal.h" 491da177e4SLinus Torvalds 50c8e55f36STejun Heo enum { 51bc2ae0f5STejun Heo /* 5224647570STejun Heo * worker_pool flags 53bc2ae0f5STejun Heo * 5424647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 55bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 56bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 57bc2ae0f5STejun Heo * is in effect. 58bc2ae0f5STejun Heo * 59bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 60bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6124647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 62bc2ae0f5STejun Heo * 63bc2ae0f5STejun Heo * Note that DISASSOCIATED can be flipped only while holding 6424647570STejun Heo * assoc_mutex to avoid changing binding state while 6524647570STejun Heo * create_worker() is in progress. 66bc2ae0f5STejun Heo */ 6711ebea50STejun Heo POOL_MANAGE_WORKERS = 1 << 0, /* need to manage workers */ 6824647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 6935b6bb63STejun Heo POOL_FREEZING = 1 << 3, /* freeze in progress */ 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 */ 76fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 77f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 78e22bee78STejun Heo 795f7dabfdSLai Jiangshan WORKER_NOT_RUNNING = WORKER_PREP | WORKER_UNBOUND | 80403c821dSTejun Heo WORKER_CPU_INTENSIVE, 81db7bccf4STejun Heo 82e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 834ce62e9eSTejun Heo 8429c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 85c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 86db7bccf4STejun Heo 87e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 88e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 89e22bee78STejun Heo 903233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 913233cdbdSTejun Heo /* call for help after 10ms 923233cdbdSTejun Heo (min two ticks) */ 93e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 94e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 97e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 98e22bee78STejun Heo * all cpus. Give -20. 99e22bee78STejun Heo */ 100e22bee78STejun Heo RESCUER_NICE_LEVEL = -20, 1013270476aSTejun Heo HIGHPRI_NICE_LEVEL = -20, 102c8e55f36STejun Heo }; 103c8e55f36STejun Heo 1041da177e4SLinus Torvalds /* 1054690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1064690c4abSTejun Heo * 107e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 108e41e704bSTejun Heo * everyone else. 1094690c4abSTejun Heo * 110e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 111e22bee78STejun Heo * only be modified and accessed from the local cpu. 112e22bee78STejun Heo * 113d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1144690c4abSTejun Heo * 115d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 116d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 117d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 118d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 119e22bee78STejun Heo * 12073f53c4aSTejun Heo * F: wq->flush_mutex protected. 12173f53c4aSTejun Heo * 1224690c4abSTejun Heo * W: workqueue_lock protected. 12376af4d93STejun Heo * 12476af4d93STejun Heo * R: workqueue_lock protected for writes. Sched-RCU protected for reads. 1254690c4abSTejun Heo */ 1264690c4abSTejun Heo 1272eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 128c34056a3STejun Heo 129bd7bdd43STejun Heo struct worker_pool { 130d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 131d84ff051STejun Heo int cpu; /* I: the associated cpu */ 1329daf9e67STejun Heo int id; /* I: pool ID */ 13311ebea50STejun Heo unsigned int flags; /* X: flags */ 134bd7bdd43STejun Heo 135bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 136bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 137ea1abd61SLai Jiangshan 138ea1abd61SLai Jiangshan /* nr_idle includes the ones off idle_list for rebinding */ 139bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 140bd7bdd43STejun Heo 141bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 142bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 143bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 144bd7bdd43STejun Heo 145c9e7cf27STejun Heo /* workers are chained either in busy_hash or idle_list */ 146c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 147c9e7cf27STejun Heo /* L: hash of busy workers */ 148c9e7cf27STejun Heo 14934a06bd6STejun Heo struct mutex manager_arb; /* manager arbitration */ 15024647570STejun Heo struct mutex assoc_mutex; /* protect POOL_DISASSOCIATED */ 151bd7bdd43STejun Heo struct ida worker_ida; /* L: for worker IDs */ 152e19e397aSTejun Heo 1537a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 15429c91e99STejun Heo struct hlist_node hash_node; /* R: unbound_pool_hash node */ 15529c91e99STejun Heo int refcnt; /* refcnt for unbound pools */ 1567a4e344cSTejun Heo 157e19e397aSTejun Heo /* 158e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 159e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 160e19e397aSTejun Heo * cacheline. 161e19e397aSTejun Heo */ 162e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 16329c91e99STejun Heo 16429c91e99STejun Heo /* 16529c91e99STejun Heo * Destruction of pool is sched-RCU protected to allow dereferences 16629c91e99STejun Heo * from get_work_pool(). 16729c91e99STejun Heo */ 16829c91e99STejun Heo struct rcu_head rcu; 1698b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1708b03ae3cSTejun Heo 1718b03ae3cSTejun Heo /* 172112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 173112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 174112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 175112202d9STejun Heo * number of flag bits. 1761da177e4SLinus Torvalds */ 177112202d9STejun Heo struct pool_workqueue { 178bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 1794690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 18073f53c4aSTejun Heo int work_color; /* L: current color */ 18173f53c4aSTejun Heo int flush_color; /* L: flushing color */ 18273f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 18373f53c4aSTejun Heo /* L: nr of in_flight works */ 1841e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 185a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 1861e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 18776af4d93STejun Heo struct list_head pwqs_node; /* R: node on wq->pwqs */ 188493a1724STejun Heo struct list_head mayday_node; /* W: node on wq->maydays */ 189e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* 19273f53c4aSTejun Heo * Structure used to wait for workqueue flush. 19373f53c4aSTejun Heo */ 19473f53c4aSTejun Heo struct wq_flusher { 19573f53c4aSTejun Heo struct list_head list; /* F: list of flushers */ 19673f53c4aSTejun Heo int flush_color; /* F: flush color waiting for */ 19773f53c4aSTejun Heo struct completion done; /* flush completion */ 19873f53c4aSTejun Heo }; 1991da177e4SLinus Torvalds 20073f53c4aSTejun Heo /* 2011da177e4SLinus Torvalds * The externally visible workqueue abstraction is an array of 2021da177e4SLinus Torvalds * per-CPU workqueues: 2031da177e4SLinus Torvalds */ 2041da177e4SLinus Torvalds struct workqueue_struct { 2059c5a2ba7STejun Heo unsigned int flags; /* W: WQ_* flags */ 206420c0ddbSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwq's */ 20776af4d93STejun Heo struct list_head pwqs; /* R: all pwqs of this wq */ 2084690c4abSTejun Heo struct list_head list; /* W: list of all workqueues */ 20973f53c4aSTejun Heo 21073f53c4aSTejun Heo struct mutex flush_mutex; /* protects wq flushing */ 21173f53c4aSTejun Heo int work_color; /* F: current work color */ 21273f53c4aSTejun Heo int flush_color; /* F: current flush color */ 213112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 21473f53c4aSTejun Heo struct wq_flusher *first_flusher; /* F: first flusher */ 21573f53c4aSTejun Heo struct list_head flusher_queue; /* F: flush waiters */ 21673f53c4aSTejun Heo struct list_head flusher_overflow; /* F: flush overflow list */ 21773f53c4aSTejun Heo 218493a1724STejun Heo struct list_head maydays; /* W: pwqs requesting rescue */ 219e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 220e22bee78STejun Heo 2219c5a2ba7STejun Heo int nr_drainers; /* W: drain in progress */ 222112202d9STejun Heo int saved_max_active; /* W: saved pwq max_active */ 2234e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2244e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2254e6045f1SJohannes Berg #endif 226b196be89STejun Heo char name[]; /* I: workqueue name */ 2271da177e4SLinus Torvalds }; 2281da177e4SLinus Torvalds 229e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 230e904e6c2STejun Heo 23129c91e99STejun Heo /* hash of all unbound pools keyed by pool->attrs */ 23229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 23329c91e99STejun Heo 23429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 23529c91e99STejun Heo 236d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 237d320c038STejun Heo EXPORT_SYMBOL_GPL(system_wq); 238044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 2391aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 240044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 241d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 242044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 243f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 244044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 24524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 246d320c038STejun Heo 24797bd2347STejun Heo #define CREATE_TRACE_POINTS 24897bd2347STejun Heo #include <trace/events/workqueue.h> 24997bd2347STejun Heo 25076af4d93STejun Heo #define assert_rcu_or_wq_lock() \ 25176af4d93STejun Heo rcu_lockdep_assert(rcu_read_lock_sched_held() || \ 25276af4d93STejun Heo lockdep_is_held(&workqueue_lock), \ 25376af4d93STejun Heo "sched RCU or workqueue lock should be held") 25476af4d93STejun Heo 255f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 256f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 257f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 2587a62c2c8STejun Heo (pool)++) 2594ce62e9eSTejun Heo 260b67bfe0dSSasha Levin #define for_each_busy_worker(worker, i, pool) \ 261b67bfe0dSSasha Levin hash_for_each(pool->busy_hash, i, worker, hentry) 262db7bccf4STejun Heo 26349e3cf44STejun Heo /** 26417116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 26517116969STejun Heo * @pool: iteration cursor 26617116969STejun Heo * @id: integer used for iteration 267fa1b54e6STejun Heo * 268fa1b54e6STejun Heo * This must be called either with workqueue_lock held or sched RCU read 269fa1b54e6STejun Heo * locked. If the pool needs to be used beyond the locking in effect, the 270fa1b54e6STejun Heo * caller is responsible for guaranteeing that the pool stays online. 271fa1b54e6STejun Heo * 272fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 273fa1b54e6STejun Heo * ignored. 27417116969STejun Heo */ 27517116969STejun Heo #define for_each_pool(pool, id) \ 276fa1b54e6STejun Heo idr_for_each_entry(&worker_pool_idr, pool, id) \ 277fa1b54e6STejun Heo if (({ assert_rcu_or_wq_lock(); false; })) { } \ 278fa1b54e6STejun Heo else 27917116969STejun Heo 28017116969STejun Heo /** 28149e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 28249e3cf44STejun Heo * @pwq: iteration cursor 28349e3cf44STejun Heo * @wq: the target workqueue 28476af4d93STejun Heo * 28576af4d93STejun Heo * This must be called either with workqueue_lock held or sched RCU read 28676af4d93STejun Heo * locked. If the pwq needs to be used beyond the locking in effect, the 28776af4d93STejun Heo * caller is responsible for guaranteeing that the pwq stays online. 28876af4d93STejun Heo * 28976af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 29076af4d93STejun Heo * ignored. 29149e3cf44STejun Heo */ 29249e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 29376af4d93STejun Heo list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ 29476af4d93STejun Heo if (({ assert_rcu_or_wq_lock(); false; })) { } \ 29576af4d93STejun Heo else 296f3421797STejun Heo 297dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 298dc186ad7SThomas Gleixner 299dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 300dc186ad7SThomas Gleixner 30199777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 30299777288SStanislaw Gruszka { 30399777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 30499777288SStanislaw Gruszka } 30599777288SStanislaw Gruszka 306dc186ad7SThomas Gleixner /* 307dc186ad7SThomas Gleixner * fixup_init is called when: 308dc186ad7SThomas Gleixner * - an active object is initialized 309dc186ad7SThomas Gleixner */ 310dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 311dc186ad7SThomas Gleixner { 312dc186ad7SThomas Gleixner struct work_struct *work = addr; 313dc186ad7SThomas Gleixner 314dc186ad7SThomas Gleixner switch (state) { 315dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 316dc186ad7SThomas Gleixner cancel_work_sync(work); 317dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 318dc186ad7SThomas Gleixner return 1; 319dc186ad7SThomas Gleixner default: 320dc186ad7SThomas Gleixner return 0; 321dc186ad7SThomas Gleixner } 322dc186ad7SThomas Gleixner } 323dc186ad7SThomas Gleixner 324dc186ad7SThomas Gleixner /* 325dc186ad7SThomas Gleixner * fixup_activate is called when: 326dc186ad7SThomas Gleixner * - an active object is activated 327dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 328dc186ad7SThomas Gleixner */ 329dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 330dc186ad7SThomas Gleixner { 331dc186ad7SThomas Gleixner struct work_struct *work = addr; 332dc186ad7SThomas Gleixner 333dc186ad7SThomas Gleixner switch (state) { 334dc186ad7SThomas Gleixner 335dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 336dc186ad7SThomas Gleixner /* 337dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 338dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 339dc186ad7SThomas Gleixner * is tracked in the object tracker. 340dc186ad7SThomas Gleixner */ 34122df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 342dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 343dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 344dc186ad7SThomas Gleixner return 0; 345dc186ad7SThomas Gleixner } 346dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 347dc186ad7SThomas Gleixner return 0; 348dc186ad7SThomas Gleixner 349dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 350dc186ad7SThomas Gleixner WARN_ON(1); 351dc186ad7SThomas Gleixner 352dc186ad7SThomas Gleixner default: 353dc186ad7SThomas Gleixner return 0; 354dc186ad7SThomas Gleixner } 355dc186ad7SThomas Gleixner } 356dc186ad7SThomas Gleixner 357dc186ad7SThomas Gleixner /* 358dc186ad7SThomas Gleixner * fixup_free is called when: 359dc186ad7SThomas Gleixner * - an active object is freed 360dc186ad7SThomas Gleixner */ 361dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 362dc186ad7SThomas Gleixner { 363dc186ad7SThomas Gleixner struct work_struct *work = addr; 364dc186ad7SThomas Gleixner 365dc186ad7SThomas Gleixner switch (state) { 366dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 367dc186ad7SThomas Gleixner cancel_work_sync(work); 368dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 369dc186ad7SThomas Gleixner return 1; 370dc186ad7SThomas Gleixner default: 371dc186ad7SThomas Gleixner return 0; 372dc186ad7SThomas Gleixner } 373dc186ad7SThomas Gleixner } 374dc186ad7SThomas Gleixner 375dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 376dc186ad7SThomas Gleixner .name = "work_struct", 37799777288SStanislaw Gruszka .debug_hint = work_debug_hint, 378dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 379dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 380dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 381dc186ad7SThomas Gleixner }; 382dc186ad7SThomas Gleixner 383dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 384dc186ad7SThomas Gleixner { 385dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 386dc186ad7SThomas Gleixner } 387dc186ad7SThomas Gleixner 388dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 389dc186ad7SThomas Gleixner { 390dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 391dc186ad7SThomas Gleixner } 392dc186ad7SThomas Gleixner 393dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 394dc186ad7SThomas Gleixner { 395dc186ad7SThomas Gleixner if (onstack) 396dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 397dc186ad7SThomas Gleixner else 398dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 399dc186ad7SThomas Gleixner } 400dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 401dc186ad7SThomas Gleixner 402dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 403dc186ad7SThomas Gleixner { 404dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 405dc186ad7SThomas Gleixner } 406dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 407dc186ad7SThomas Gleixner 408dc186ad7SThomas Gleixner #else 409dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 410dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 411dc186ad7SThomas Gleixner #endif 412dc186ad7SThomas Gleixner 41395402b38SGautham R Shenoy /* Serializes the accesses to the list of workqueues. */ 41495402b38SGautham R Shenoy static DEFINE_SPINLOCK(workqueue_lock); 4151da177e4SLinus Torvalds static LIST_HEAD(workqueues); 416a0a1a5fdSTejun Heo static bool workqueue_freezing; /* W: have wqs started freezing? */ 4171da177e4SLinus Torvalds 41814441960SOleg Nesterov /* 419e19e397aSTejun Heo * The CPU and unbound standard worker pools. The unbound ones have 420e19e397aSTejun Heo * POOL_DISASSOCIATED set, and their workers have WORKER_UNBOUND set. 42114441960SOleg Nesterov */ 422e19e397aSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 423f02ae73aSTejun Heo cpu_worker_pools); 424f3421797STejun Heo 425fa1b54e6STejun Heo /* 426fa1b54e6STejun Heo * idr of all pools. Modifications are protected by workqueue_lock. Read 427fa1b54e6STejun Heo * accesses are protected by sched-RCU protected. 428fa1b54e6STejun Heo */ 4299daf9e67STejun Heo static DEFINE_IDR(worker_pool_idr); 4309daf9e67STejun Heo 431c34056a3STejun Heo static int worker_thread(void *__worker); 4321da177e4SLinus Torvalds 4339daf9e67STejun Heo /* allocate ID and assign it to @pool */ 4349daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 4359daf9e67STejun Heo { 4369daf9e67STejun Heo int ret; 4379daf9e67STejun Heo 438fa1b54e6STejun Heo do { 439fa1b54e6STejun Heo if (!idr_pre_get(&worker_pool_idr, GFP_KERNEL)) 440fa1b54e6STejun Heo return -ENOMEM; 441fa1b54e6STejun Heo 442fa1b54e6STejun Heo spin_lock_irq(&workqueue_lock); 4439daf9e67STejun Heo ret = idr_get_new(&worker_pool_idr, pool, &pool->id); 444fa1b54e6STejun Heo spin_unlock_irq(&workqueue_lock); 445fa1b54e6STejun Heo } while (ret == -EAGAIN); 4469daf9e67STejun Heo 4479daf9e67STejun Heo return ret; 4489daf9e67STejun Heo } 4499daf9e67STejun Heo 45076af4d93STejun Heo /** 45176af4d93STejun Heo * first_pwq - return the first pool_workqueue of the specified workqueue 45276af4d93STejun Heo * @wq: the target workqueue 45376af4d93STejun Heo * 45476af4d93STejun Heo * This must be called either with workqueue_lock held or sched RCU read 45576af4d93STejun Heo * locked. If the pwq needs to be used beyond the locking in effect, the 45676af4d93STejun Heo * caller is responsible for guaranteeing that the pwq stays online. 45776af4d93STejun Heo */ 4587fb98ea7STejun Heo static struct pool_workqueue *first_pwq(struct workqueue_struct *wq) 459a848e3b6SOleg Nesterov { 46076af4d93STejun Heo assert_rcu_or_wq_lock(); 46176af4d93STejun Heo return list_first_or_null_rcu(&wq->pwqs, struct pool_workqueue, 46276af4d93STejun Heo pwqs_node); 463f3421797STejun Heo } 464a848e3b6SOleg Nesterov 46573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 46673f53c4aSTejun Heo { 46773f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 46873f53c4aSTejun Heo } 46973f53c4aSTejun Heo 47073f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 47173f53c4aSTejun Heo { 47273f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 47373f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 47473f53c4aSTejun Heo } 47573f53c4aSTejun Heo 47673f53c4aSTejun Heo static int work_next_color(int color) 47773f53c4aSTejun Heo { 47873f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 479b1f4ec17SOleg Nesterov } 480b1f4ec17SOleg Nesterov 4814594bf15SDavid Howells /* 482112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 483112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 4847c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 4857a22ad75STejun Heo * 486112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 487112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 488bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 489bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 4907a22ad75STejun Heo * 491112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 4927c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 493112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 4947c3eed5cSTejun Heo * available only while the work item is queued. 495bbb68dfaSTejun Heo * 496bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 497bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 498bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 499bbb68dfaSTejun Heo * try to steal the PENDING bit. 5004594bf15SDavid Howells */ 5017a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5027a22ad75STejun Heo unsigned long flags) 5037a22ad75STejun Heo { 5046183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 5057a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 5067a22ad75STejun Heo } 5077a22ad75STejun Heo 508112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 5094690c4abSTejun Heo unsigned long extra_flags) 510365970a1SDavid Howells { 511112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 512112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 513365970a1SDavid Howells } 514365970a1SDavid Howells 5154468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 5164468a00fSLai Jiangshan int pool_id) 5174468a00fSLai Jiangshan { 5184468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 5194468a00fSLai Jiangshan WORK_STRUCT_PENDING); 5204468a00fSLai Jiangshan } 5214468a00fSLai Jiangshan 5227c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 5237c3eed5cSTejun Heo int pool_id) 5244d707b9fSOleg Nesterov { 52523657bb1STejun Heo /* 52623657bb1STejun Heo * The following wmb is paired with the implied mb in 52723657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 52823657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 52923657bb1STejun Heo * owner. 53023657bb1STejun Heo */ 53123657bb1STejun Heo smp_wmb(); 5327c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 5334d707b9fSOleg Nesterov } 5344d707b9fSOleg Nesterov 5357a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 536365970a1SDavid Howells { 5377c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 5387c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 5397a22ad75STejun Heo } 5407a22ad75STejun Heo 541112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 5427a22ad75STejun Heo { 543e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5447a22ad75STejun Heo 545112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 546e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 547e120153dSTejun Heo else 548e120153dSTejun Heo return NULL; 5497a22ad75STejun Heo } 5507a22ad75STejun Heo 5517c3eed5cSTejun Heo /** 5527c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 5537c3eed5cSTejun Heo * @work: the work item of interest 5547c3eed5cSTejun Heo * 5557c3eed5cSTejun Heo * Return the worker_pool @work was last associated with. %NULL if none. 556fa1b54e6STejun Heo * 557fa1b54e6STejun Heo * Pools are created and destroyed under workqueue_lock, and allows read 558fa1b54e6STejun Heo * access under sched-RCU read lock. As such, this function should be 559fa1b54e6STejun Heo * called under workqueue_lock or with preemption disabled. 560fa1b54e6STejun Heo * 561fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 562fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 563fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 564fa1b54e6STejun Heo * returned pool is and stays online. 5657c3eed5cSTejun Heo */ 5667c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 5677a22ad75STejun Heo { 568e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 5697c3eed5cSTejun Heo int pool_id; 5707a22ad75STejun Heo 571fa1b54e6STejun Heo assert_rcu_or_wq_lock(); 572fa1b54e6STejun Heo 573112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 574112202d9STejun Heo return ((struct pool_workqueue *) 5757c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 5767a22ad75STejun Heo 5777c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 5787c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 5797a22ad75STejun Heo return NULL; 5807a22ad75STejun Heo 581fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 5827c3eed5cSTejun Heo } 5837c3eed5cSTejun Heo 5847c3eed5cSTejun Heo /** 5857c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 5867c3eed5cSTejun Heo * @work: the work item of interest 5877c3eed5cSTejun Heo * 5887c3eed5cSTejun Heo * Return the worker_pool ID @work was last associated with. 5897c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 5907c3eed5cSTejun Heo */ 5917c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 5927c3eed5cSTejun Heo { 59354d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 5947c3eed5cSTejun Heo 595112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 596112202d9STejun Heo return ((struct pool_workqueue *) 59754d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 59854d5b7d0SLai Jiangshan 59954d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 6007c3eed5cSTejun Heo } 6017c3eed5cSTejun Heo 602bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 603bbb68dfaSTejun Heo { 6047c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 605bbb68dfaSTejun Heo 6067c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 6077c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 608bbb68dfaSTejun Heo } 609bbb68dfaSTejun Heo 610bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 611bbb68dfaSTejun Heo { 612bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 613bbb68dfaSTejun Heo 614112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 615bbb68dfaSTejun Heo } 616bbb68dfaSTejun Heo 617e22bee78STejun Heo /* 6183270476aSTejun Heo * Policy functions. These define the policies on how the global worker 6193270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 620d565ed63STejun Heo * they're being called with pool->lock held. 621e22bee78STejun Heo */ 622e22bee78STejun Heo 62363d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 624649027d7STejun Heo { 625e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 626649027d7STejun Heo } 627649027d7STejun Heo 628e22bee78STejun Heo /* 629e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 630e22bee78STejun Heo * running workers. 631974271c4STejun Heo * 632974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 633706026c2STejun Heo * function will always return %true for unbound pools as long as the 634974271c4STejun Heo * worklist isn't empty. 635e22bee78STejun Heo */ 63663d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 637e22bee78STejun Heo { 63863d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 639e22bee78STejun Heo } 640e22bee78STejun Heo 641e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 64263d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 643e22bee78STejun Heo { 64463d95a91STejun Heo return pool->nr_idle; 645e22bee78STejun Heo } 646e22bee78STejun Heo 647e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 64863d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 649e22bee78STejun Heo { 650e19e397aSTejun Heo return !list_empty(&pool->worklist) && 651e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 652e22bee78STejun Heo } 653e22bee78STejun Heo 654e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 65563d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 656e22bee78STejun Heo { 65763d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 658e22bee78STejun Heo } 659e22bee78STejun Heo 660e22bee78STejun Heo /* Do I need to be the manager? */ 66163d95a91STejun Heo static bool need_to_manage_workers(struct worker_pool *pool) 662e22bee78STejun Heo { 66363d95a91STejun Heo return need_to_create_worker(pool) || 66411ebea50STejun Heo (pool->flags & POOL_MANAGE_WORKERS); 665e22bee78STejun Heo } 666e22bee78STejun Heo 667e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 66863d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 669e22bee78STejun Heo { 67034a06bd6STejun Heo bool managing = mutex_is_locked(&pool->manager_arb); 67163d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 67263d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 673e22bee78STejun Heo 674ea1abd61SLai Jiangshan /* 675ea1abd61SLai Jiangshan * nr_idle and idle_list may disagree if idle rebinding is in 676ea1abd61SLai Jiangshan * progress. Never return %true if idle_list is empty. 677ea1abd61SLai Jiangshan */ 678ea1abd61SLai Jiangshan if (list_empty(&pool->idle_list)) 679ea1abd61SLai Jiangshan return false; 680ea1abd61SLai Jiangshan 681e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 682e22bee78STejun Heo } 683e22bee78STejun Heo 684e22bee78STejun Heo /* 685e22bee78STejun Heo * Wake up functions. 686e22bee78STejun Heo */ 687e22bee78STejun Heo 6887e11629dSTejun Heo /* Return the first worker. Safe with preemption disabled */ 68963d95a91STejun Heo static struct worker *first_worker(struct worker_pool *pool) 6907e11629dSTejun Heo { 69163d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 6927e11629dSTejun Heo return NULL; 6937e11629dSTejun Heo 69463d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 6957e11629dSTejun Heo } 6967e11629dSTejun Heo 6977e11629dSTejun Heo /** 6987e11629dSTejun Heo * wake_up_worker - wake up an idle worker 69963d95a91STejun Heo * @pool: worker pool to wake worker from 7007e11629dSTejun Heo * 70163d95a91STejun Heo * Wake up the first idle worker of @pool. 7027e11629dSTejun Heo * 7037e11629dSTejun Heo * CONTEXT: 704d565ed63STejun Heo * spin_lock_irq(pool->lock). 7057e11629dSTejun Heo */ 70663d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 7077e11629dSTejun Heo { 70863d95a91STejun Heo struct worker *worker = first_worker(pool); 7097e11629dSTejun Heo 7107e11629dSTejun Heo if (likely(worker)) 7117e11629dSTejun Heo wake_up_process(worker->task); 7127e11629dSTejun Heo } 7137e11629dSTejun Heo 7144690c4abSTejun Heo /** 715e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 716e22bee78STejun Heo * @task: task waking up 717e22bee78STejun Heo * @cpu: CPU @task is waking up to 718e22bee78STejun Heo * 719e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 720e22bee78STejun Heo * being awoken. 721e22bee78STejun Heo * 722e22bee78STejun Heo * CONTEXT: 723e22bee78STejun Heo * spin_lock_irq(rq->lock) 724e22bee78STejun Heo */ 725d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu) 726e22bee78STejun Heo { 727e22bee78STejun Heo struct worker *worker = kthread_data(task); 728e22bee78STejun Heo 72936576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 730ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 731e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 732e22bee78STejun Heo } 73336576000SJoonsoo Kim } 734e22bee78STejun Heo 735e22bee78STejun Heo /** 736e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 737e22bee78STejun Heo * @task: task going to sleep 738e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 739e22bee78STejun Heo * 740e22bee78STejun Heo * This function is called during schedule() when a busy worker is 741e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 742e22bee78STejun Heo * returning pointer to its task. 743e22bee78STejun Heo * 744e22bee78STejun Heo * CONTEXT: 745e22bee78STejun Heo * spin_lock_irq(rq->lock) 746e22bee78STejun Heo * 747e22bee78STejun Heo * RETURNS: 748e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 749e22bee78STejun Heo */ 750d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu) 751e22bee78STejun Heo { 752e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 753111c225aSTejun Heo struct worker_pool *pool; 754e22bee78STejun Heo 755111c225aSTejun Heo /* 756111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 757111c225aSTejun Heo * workers, also reach here, let's not access anything before 758111c225aSTejun Heo * checking NOT_RUNNING. 759111c225aSTejun Heo */ 7602d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 761e22bee78STejun Heo return NULL; 762e22bee78STejun Heo 763111c225aSTejun Heo pool = worker->pool; 764111c225aSTejun Heo 765e22bee78STejun Heo /* this can only happen on the local cpu */ 7666183c009STejun Heo if (WARN_ON_ONCE(cpu != raw_smp_processor_id())) 7676183c009STejun Heo return NULL; 768e22bee78STejun Heo 769e22bee78STejun Heo /* 770e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 771e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 772e22bee78STejun Heo * Please read comment there. 773e22bee78STejun Heo * 774628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 775628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 776628c78e7STejun Heo * disabled, which in turn means that none else could be 777d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 778628c78e7STejun Heo * lock is safe. 779e22bee78STejun Heo */ 780e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 781e19e397aSTejun Heo !list_empty(&pool->worklist)) 78263d95a91STejun Heo to_wakeup = first_worker(pool); 783e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 784e22bee78STejun Heo } 785e22bee78STejun Heo 786e22bee78STejun Heo /** 787e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 788cb444766STejun Heo * @worker: self 789d302f017STejun Heo * @flags: flags to set 790d302f017STejun Heo * @wakeup: wakeup an idle worker if necessary 791d302f017STejun Heo * 792e22bee78STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. If 793e22bee78STejun Heo * nr_running becomes zero and @wakeup is %true, an idle worker is 794e22bee78STejun Heo * woken up. 795d302f017STejun Heo * 796cb444766STejun Heo * CONTEXT: 797d565ed63STejun Heo * spin_lock_irq(pool->lock) 798d302f017STejun Heo */ 799d302f017STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags, 800d302f017STejun Heo bool wakeup) 801d302f017STejun Heo { 802bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 803e22bee78STejun Heo 804cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 805cb444766STejun Heo 806e22bee78STejun Heo /* 807e22bee78STejun Heo * If transitioning into NOT_RUNNING, adjust nr_running and 808e22bee78STejun Heo * wake up an idle worker as necessary if requested by 809e22bee78STejun Heo * @wakeup. 810e22bee78STejun Heo */ 811e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 812e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 813e22bee78STejun Heo if (wakeup) { 814e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 815bd7bdd43STejun Heo !list_empty(&pool->worklist)) 81663d95a91STejun Heo wake_up_worker(pool); 817e22bee78STejun Heo } else 818e19e397aSTejun Heo atomic_dec(&pool->nr_running); 819e22bee78STejun Heo } 820e22bee78STejun Heo 821d302f017STejun Heo worker->flags |= flags; 822d302f017STejun Heo } 823d302f017STejun Heo 824d302f017STejun Heo /** 825e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 826cb444766STejun Heo * @worker: self 827d302f017STejun Heo * @flags: flags to clear 828d302f017STejun Heo * 829e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 830d302f017STejun Heo * 831cb444766STejun Heo * CONTEXT: 832d565ed63STejun Heo * spin_lock_irq(pool->lock) 833d302f017STejun Heo */ 834d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 835d302f017STejun Heo { 83663d95a91STejun Heo struct worker_pool *pool = worker->pool; 837e22bee78STejun Heo unsigned int oflags = worker->flags; 838e22bee78STejun Heo 839cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 840cb444766STejun Heo 841d302f017STejun Heo worker->flags &= ~flags; 842e22bee78STejun Heo 84342c025f3STejun Heo /* 84442c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 84542c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 84642c025f3STejun Heo * of multiple flags, not a single flag. 84742c025f3STejun Heo */ 848e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 849e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 850e19e397aSTejun Heo atomic_inc(&pool->nr_running); 851d302f017STejun Heo } 852d302f017STejun Heo 853d302f017STejun Heo /** 8548cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 855c9e7cf27STejun Heo * @pool: pool of interest 8568cca0eeaSTejun Heo * @work: work to find worker for 8578cca0eeaSTejun Heo * 858c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 859c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 860a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 861a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 862a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 863a2c1c57bSTejun Heo * being executed. 864a2c1c57bSTejun Heo * 865a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 866a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 867a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 868a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 869a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 870a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 871a2c1c57bSTejun Heo * 872a2c1c57bSTejun Heo * This function checks the work item address, work function and workqueue 873a2c1c57bSTejun Heo * to avoid false positives. Note that this isn't complete as one may 874a2c1c57bSTejun Heo * construct a work function which can introduce dependency onto itself 875a2c1c57bSTejun Heo * through a recycled work item. Well, if somebody wants to shoot oneself 876a2c1c57bSTejun Heo * in the foot that badly, there's only so much we can do, and if such 877a2c1c57bSTejun Heo * deadlock actually occurs, it should be easy to locate the culprit work 878a2c1c57bSTejun Heo * function. 8798cca0eeaSTejun Heo * 8808cca0eeaSTejun Heo * CONTEXT: 881d565ed63STejun Heo * spin_lock_irq(pool->lock). 8828cca0eeaSTejun Heo * 8838cca0eeaSTejun Heo * RETURNS: 8848cca0eeaSTejun Heo * Pointer to worker which is executing @work if found, NULL 8858cca0eeaSTejun Heo * otherwise. 8868cca0eeaSTejun Heo */ 887c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 8888cca0eeaSTejun Heo struct work_struct *work) 8898cca0eeaSTejun Heo { 89042f8570fSSasha Levin struct worker *worker; 89142f8570fSSasha Levin 892b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 893a2c1c57bSTejun Heo (unsigned long)work) 894a2c1c57bSTejun Heo if (worker->current_work == work && 895a2c1c57bSTejun Heo worker->current_func == work->func) 89642f8570fSSasha Levin return worker; 89742f8570fSSasha Levin 89842f8570fSSasha Levin return NULL; 8998cca0eeaSTejun Heo } 9008cca0eeaSTejun Heo 9018cca0eeaSTejun Heo /** 902bf4ede01STejun Heo * move_linked_works - move linked works to a list 903bf4ede01STejun Heo * @work: start of series of works to be scheduled 904bf4ede01STejun Heo * @head: target list to append @work to 905bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 906bf4ede01STejun Heo * 907bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 908bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 909bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 910bf4ede01STejun Heo * 911bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 912bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 913bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 914bf4ede01STejun Heo * 915bf4ede01STejun Heo * CONTEXT: 916d565ed63STejun Heo * spin_lock_irq(pool->lock). 917bf4ede01STejun Heo */ 918bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 919bf4ede01STejun Heo struct work_struct **nextp) 920bf4ede01STejun Heo { 921bf4ede01STejun Heo struct work_struct *n; 922bf4ede01STejun Heo 923bf4ede01STejun Heo /* 924bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 925bf4ede01STejun Heo * use NULL for list head. 926bf4ede01STejun Heo */ 927bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 928bf4ede01STejun Heo list_move_tail(&work->entry, head); 929bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 930bf4ede01STejun Heo break; 931bf4ede01STejun Heo } 932bf4ede01STejun Heo 933bf4ede01STejun Heo /* 934bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 935bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 936bf4ede01STejun Heo * needs to be updated. 937bf4ede01STejun Heo */ 938bf4ede01STejun Heo if (nextp) 939bf4ede01STejun Heo *nextp = n; 940bf4ede01STejun Heo } 941bf4ede01STejun Heo 942112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 943bf4ede01STejun Heo { 944112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 945bf4ede01STejun Heo 946bf4ede01STejun Heo trace_workqueue_activate_work(work); 947112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 948bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 949112202d9STejun Heo pwq->nr_active++; 950bf4ede01STejun Heo } 951bf4ede01STejun Heo 952112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 9533aa62497SLai Jiangshan { 954112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 9553aa62497SLai Jiangshan struct work_struct, entry); 9563aa62497SLai Jiangshan 957112202d9STejun Heo pwq_activate_delayed_work(work); 9583aa62497SLai Jiangshan } 9593aa62497SLai Jiangshan 960bf4ede01STejun Heo /** 961112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 962112202d9STejun Heo * @pwq: pwq of interest 963bf4ede01STejun Heo * @color: color of work which left the queue 964bf4ede01STejun Heo * 965bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 966112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 967bf4ede01STejun Heo * 968bf4ede01STejun Heo * CONTEXT: 969d565ed63STejun Heo * spin_lock_irq(pool->lock). 970bf4ede01STejun Heo */ 971112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 972bf4ede01STejun Heo { 973bf4ede01STejun Heo /* ignore uncolored works */ 974bf4ede01STejun Heo if (color == WORK_NO_COLOR) 975bf4ede01STejun Heo return; 976bf4ede01STejun Heo 977112202d9STejun Heo pwq->nr_in_flight[color]--; 978bf4ede01STejun Heo 979112202d9STejun Heo pwq->nr_active--; 980112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 981bf4ede01STejun Heo /* one down, submit a delayed one */ 982112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 983112202d9STejun Heo pwq_activate_first_delayed(pwq); 984bf4ede01STejun Heo } 985bf4ede01STejun Heo 986bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 987112202d9STejun Heo if (likely(pwq->flush_color != color)) 988bf4ede01STejun Heo return; 989bf4ede01STejun Heo 990bf4ede01STejun Heo /* are there still in-flight works? */ 991112202d9STejun Heo if (pwq->nr_in_flight[color]) 992bf4ede01STejun Heo return; 993bf4ede01STejun Heo 994112202d9STejun Heo /* this pwq is done, clear flush_color */ 995112202d9STejun Heo pwq->flush_color = -1; 996bf4ede01STejun Heo 997bf4ede01STejun Heo /* 998112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 999bf4ede01STejun Heo * will handle the rest. 1000bf4ede01STejun Heo */ 1001112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1002112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 1003bf4ede01STejun Heo } 1004bf4ede01STejun Heo 100536e227d2STejun Heo /** 1006bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 100736e227d2STejun Heo * @work: work item to steal 100836e227d2STejun Heo * @is_dwork: @work is a delayed_work 1009bbb68dfaSTejun Heo * @flags: place to store irq state 101036e227d2STejun Heo * 101136e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 101236e227d2STejun Heo * stable state - idle, on timer or on worklist. Return values are 101336e227d2STejun Heo * 101436e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 101536e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 101636e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1017bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1018bbb68dfaSTejun Heo * for arbitrarily long 101936e227d2STejun Heo * 1020bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1021e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1022e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1023e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1024bbb68dfaSTejun Heo * 1025bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1026bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1027bbb68dfaSTejun Heo * 1028e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1029bf4ede01STejun Heo */ 1030bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1031bbb68dfaSTejun Heo unsigned long *flags) 1032bf4ede01STejun Heo { 1033d565ed63STejun Heo struct worker_pool *pool; 1034112202d9STejun Heo struct pool_workqueue *pwq; 1035bf4ede01STejun Heo 1036bbb68dfaSTejun Heo local_irq_save(*flags); 1037bbb68dfaSTejun Heo 103836e227d2STejun Heo /* try to steal the timer if it exists */ 103936e227d2STejun Heo if (is_dwork) { 104036e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 104136e227d2STejun Heo 1042e0aecdd8STejun Heo /* 1043e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1044e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1045e0aecdd8STejun Heo * running on the local CPU. 1046e0aecdd8STejun Heo */ 104736e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 104836e227d2STejun Heo return 1; 104936e227d2STejun Heo } 105036e227d2STejun Heo 105136e227d2STejun Heo /* try to claim PENDING the normal way */ 1052bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1053bf4ede01STejun Heo return 0; 1054bf4ede01STejun Heo 1055bf4ede01STejun Heo /* 1056bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1057bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1058bf4ede01STejun Heo */ 1059d565ed63STejun Heo pool = get_work_pool(work); 1060d565ed63STejun Heo if (!pool) 1061bbb68dfaSTejun Heo goto fail; 1062bf4ede01STejun Heo 1063d565ed63STejun Heo spin_lock(&pool->lock); 1064bf4ede01STejun Heo /* 1065112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1066112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1067112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1068112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1069112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 10700b3dae68SLai Jiangshan * item is currently queued on that pool. 1071bf4ede01STejun Heo */ 1072112202d9STejun Heo pwq = get_work_pwq(work); 1073112202d9STejun Heo if (pwq && pwq->pool == pool) { 1074bf4ede01STejun Heo debug_work_deactivate(work); 10753aa62497SLai Jiangshan 10763aa62497SLai Jiangshan /* 107716062836STejun Heo * A delayed work item cannot be grabbed directly because 107816062836STejun Heo * it might have linked NO_COLOR work items which, if left 1079112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 108016062836STejun Heo * management later on and cause stall. Make sure the work 108116062836STejun Heo * item is activated before grabbing. 10823aa62497SLai Jiangshan */ 10833aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1084112202d9STejun Heo pwq_activate_delayed_work(work); 10853aa62497SLai Jiangshan 1086bf4ede01STejun Heo list_del_init(&work->entry); 1087112202d9STejun Heo pwq_dec_nr_in_flight(get_work_pwq(work), get_work_color(work)); 108836e227d2STejun Heo 1089112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 10904468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 10914468a00fSLai Jiangshan 1092d565ed63STejun Heo spin_unlock(&pool->lock); 109336e227d2STejun Heo return 1; 1094bf4ede01STejun Heo } 1095d565ed63STejun Heo spin_unlock(&pool->lock); 1096bbb68dfaSTejun Heo fail: 1097bbb68dfaSTejun Heo local_irq_restore(*flags); 1098bbb68dfaSTejun Heo if (work_is_canceling(work)) 1099bbb68dfaSTejun Heo return -ENOENT; 1100bbb68dfaSTejun Heo cpu_relax(); 110136e227d2STejun Heo return -EAGAIN; 1102bf4ede01STejun Heo } 1103bf4ede01STejun Heo 1104bf4ede01STejun Heo /** 1105706026c2STejun Heo * insert_work - insert a work into a pool 1106112202d9STejun Heo * @pwq: pwq @work belongs to 11074690c4abSTejun Heo * @work: work to insert 11084690c4abSTejun Heo * @head: insertion point 11094690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 11104690c4abSTejun Heo * 1111112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1112706026c2STejun Heo * work_struct flags. 11134690c4abSTejun Heo * 11144690c4abSTejun Heo * CONTEXT: 1115d565ed63STejun Heo * spin_lock_irq(pool->lock). 1116365970a1SDavid Howells */ 1117112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1118112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1119b89deed3SOleg Nesterov { 1120112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1121e1d8aa9fSFrederic Weisbecker 11224690c4abSTejun Heo /* we own @work, set data and link */ 1123112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 11241a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 1125e22bee78STejun Heo 1126e22bee78STejun Heo /* 1127e22bee78STejun Heo * Ensure either worker_sched_deactivated() sees the above 1128e22bee78STejun Heo * list_add_tail() or we see zero nr_running to avoid workers 1129e22bee78STejun Heo * lying around lazily while there are works to be processed. 1130e22bee78STejun Heo */ 1131e22bee78STejun Heo smp_mb(); 1132e22bee78STejun Heo 113363d95a91STejun Heo if (__need_more_worker(pool)) 113463d95a91STejun Heo wake_up_worker(pool); 1135b89deed3SOleg Nesterov } 1136b89deed3SOleg Nesterov 1137c8efcc25STejun Heo /* 1138c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 11398d03ecfeSTejun Heo * same workqueue. 1140c8efcc25STejun Heo */ 1141c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1142c8efcc25STejun Heo { 1143c8efcc25STejun Heo struct worker *worker; 1144c8efcc25STejun Heo 11458d03ecfeSTejun Heo worker = current_wq_worker(); 1146c8efcc25STejun Heo /* 11478d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 11488d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1149c8efcc25STejun Heo */ 1150112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1151c8efcc25STejun Heo } 1152c8efcc25STejun Heo 1153d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 11541da177e4SLinus Torvalds struct work_struct *work) 11551da177e4SLinus Torvalds { 1156112202d9STejun Heo struct pool_workqueue *pwq; 11571e19ffc6STejun Heo struct list_head *worklist; 11588a2e8e5dSTejun Heo unsigned int work_flags; 1159b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 11608930cabaSTejun Heo 11618930cabaSTejun Heo /* 11628930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 11638930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 11648930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 11658930cabaSTejun Heo * happen with IRQ disabled. 11668930cabaSTejun Heo */ 11678930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 11681da177e4SLinus Torvalds 1169dc186ad7SThomas Gleixner debug_work_activate(work); 11701e19ffc6STejun Heo 1171c8efcc25STejun Heo /* if dying, only works from the same workqueue are allowed */ 11729c5a2ba7STejun Heo if (unlikely(wq->flags & WQ_DRAINING) && 1173c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1174e41e704bSTejun Heo return; 1175e41e704bSTejun Heo 1176112202d9STejun Heo /* determine the pwq to use */ 1177c7fc77f7STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 1178c9e7cf27STejun Heo struct worker_pool *last_pool; 1179c7fc77f7STejun Heo 118057469821STejun Heo if (cpu == WORK_CPU_UNBOUND) 1181f3421797STejun Heo cpu = raw_smp_processor_id(); 1182f3421797STejun Heo 118318aa9effSTejun Heo /* 1184dbf2576eSTejun Heo * It's multi cpu. If @work was previously on a different 1185dbf2576eSTejun Heo * cpu, it might still be running there, in which case the 1186dbf2576eSTejun Heo * work needs to be queued on that cpu to guarantee 1187dbf2576eSTejun Heo * non-reentrancy. 118818aa9effSTejun Heo */ 11897fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1190c9e7cf27STejun Heo last_pool = get_work_pool(work); 1191dbf2576eSTejun Heo 1192112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 119318aa9effSTejun Heo struct worker *worker; 119418aa9effSTejun Heo 1195d565ed63STejun Heo spin_lock(&last_pool->lock); 119618aa9effSTejun Heo 1197c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 119818aa9effSTejun Heo 1199112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 12007fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, last_pool->cpu); 12018594fadeSLai Jiangshan } else { 120218aa9effSTejun Heo /* meh... not running there, queue here */ 1203d565ed63STejun Heo spin_unlock(&last_pool->lock); 1204112202d9STejun Heo spin_lock(&pwq->pool->lock); 120518aa9effSTejun Heo } 12068930cabaSTejun Heo } else { 1207112202d9STejun Heo spin_lock(&pwq->pool->lock); 12088930cabaSTejun Heo } 1209f3421797STejun Heo } else { 12107fb98ea7STejun Heo pwq = first_pwq(wq); 1211112202d9STejun Heo spin_lock(&pwq->pool->lock); 1212502ca9d8STejun Heo } 1213502ca9d8STejun Heo 1214112202d9STejun Heo /* pwq determined, queue */ 1215112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1216502ca9d8STejun Heo 1217f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1218112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1219f5b2552bSDan Carpenter return; 1220f5b2552bSDan Carpenter } 12211e19ffc6STejun Heo 1222112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1223112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 12241e19ffc6STejun Heo 1225112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1226cdadf009STejun Heo trace_workqueue_activate_work(work); 1227112202d9STejun Heo pwq->nr_active++; 1228112202d9STejun Heo worklist = &pwq->pool->worklist; 12298a2e8e5dSTejun Heo } else { 12308a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1231112202d9STejun Heo worklist = &pwq->delayed_works; 12328a2e8e5dSTejun Heo } 12331e19ffc6STejun Heo 1234112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 12351e19ffc6STejun Heo 1236112202d9STejun Heo spin_unlock(&pwq->pool->lock); 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 12390fcb78c2SRolf Eike Beer /** 1240c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1241c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1242c1a220e7SZhang Rui * @wq: workqueue to use 1243c1a220e7SZhang Rui * @work: work to queue 1244c1a220e7SZhang Rui * 1245d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 1246c1a220e7SZhang Rui * 1247c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1248c1a220e7SZhang Rui * can't go away. 1249c1a220e7SZhang Rui */ 1250d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1251d4283e93STejun Heo struct work_struct *work) 1252c1a220e7SZhang Rui { 1253d4283e93STejun Heo bool ret = false; 12548930cabaSTejun Heo unsigned long flags; 12558930cabaSTejun Heo 12568930cabaSTejun Heo local_irq_save(flags); 1257c1a220e7SZhang Rui 125822df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 12594690c4abSTejun Heo __queue_work(cpu, wq, work); 1260d4283e93STejun Heo ret = true; 1261c1a220e7SZhang Rui } 12628930cabaSTejun Heo 12638930cabaSTejun Heo local_irq_restore(flags); 1264c1a220e7SZhang Rui return ret; 1265c1a220e7SZhang Rui } 1266c1a220e7SZhang Rui EXPORT_SYMBOL_GPL(queue_work_on); 1267c1a220e7SZhang Rui 12680a13c00eSTejun Heo /** 12690a13c00eSTejun Heo * queue_work - queue work on a workqueue 12700a13c00eSTejun Heo * @wq: workqueue to use 12710a13c00eSTejun Heo * @work: work to queue 12720a13c00eSTejun Heo * 1273d4283e93STejun Heo * Returns %false if @work was already on a queue, %true otherwise. 12740a13c00eSTejun Heo * 12750a13c00eSTejun Heo * We queue the work to the CPU on which it was submitted, but if the CPU dies 12760a13c00eSTejun Heo * it can be processed by another CPU. 12770a13c00eSTejun Heo */ 1278d4283e93STejun Heo bool queue_work(struct workqueue_struct *wq, struct work_struct *work) 12790a13c00eSTejun Heo { 128057469821STejun Heo return queue_work_on(WORK_CPU_UNBOUND, wq, work); 12810a13c00eSTejun Heo } 12820a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_work); 12830a13c00eSTejun Heo 1284d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 12851da177e4SLinus Torvalds { 128652bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 12871da177e4SLinus Torvalds 1288e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 128960c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 12901da177e4SLinus Torvalds } 12911438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 12921da177e4SLinus Torvalds 12937beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 129452bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 12951da177e4SLinus Torvalds { 12967beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 12977beb2edfSTejun Heo struct work_struct *work = &dwork->work; 12981da177e4SLinus Torvalds 12997beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 13007beb2edfSTejun Heo timer->data != (unsigned long)dwork); 1301fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1302fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 13037beb2edfSTejun Heo 13048852aac2STejun Heo /* 13058852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 13068852aac2STejun Heo * both optimization and correctness. The earliest @timer can 13078852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 13088852aac2STejun Heo * on that there's no such delay when @delay is 0. 13098852aac2STejun Heo */ 13108852aac2STejun Heo if (!delay) { 13118852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 13128852aac2STejun Heo return; 13138852aac2STejun Heo } 13148852aac2STejun Heo 13157beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 13167beb2edfSTejun Heo 131760c057bcSLai Jiangshan dwork->wq = wq; 13181265057fSTejun Heo dwork->cpu = cpu; 13197beb2edfSTejun Heo timer->expires = jiffies + delay; 13207beb2edfSTejun Heo 13217beb2edfSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 13227beb2edfSTejun Heo add_timer_on(timer, cpu); 13237beb2edfSTejun Heo else 13247beb2edfSTejun Heo add_timer(timer); 13257beb2edfSTejun Heo } 13261da177e4SLinus Torvalds 13270fcb78c2SRolf Eike Beer /** 13280fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 13290fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 13300fcb78c2SRolf Eike Beer * @wq: workqueue to use 1331af9997e4SRandy Dunlap * @dwork: work to queue 13320fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 13330fcb78c2SRolf Eike Beer * 1334715f1300STejun Heo * Returns %false if @work was already on a queue, %true otherwise. If 1335715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1336715f1300STejun Heo * execution. 13370fcb78c2SRolf Eike Beer */ 1338d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 133952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 13407a6bc1cdSVenkatesh Pallipadi { 134152bad64dSDavid Howells struct work_struct *work = &dwork->work; 1342d4283e93STejun Heo bool ret = false; 13438930cabaSTejun Heo unsigned long flags; 13448930cabaSTejun Heo 13458930cabaSTejun Heo /* read the comment in __queue_work() */ 13468930cabaSTejun Heo local_irq_save(flags); 13477a6bc1cdSVenkatesh Pallipadi 134822df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 13497beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1350d4283e93STejun Heo ret = true; 13517a6bc1cdSVenkatesh Pallipadi } 13528930cabaSTejun Heo 13538930cabaSTejun Heo local_irq_restore(flags); 13547a6bc1cdSVenkatesh Pallipadi return ret; 13557a6bc1cdSVenkatesh Pallipadi } 1356ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(queue_delayed_work_on); 13571da177e4SLinus Torvalds 1358c8e55f36STejun Heo /** 13590a13c00eSTejun Heo * queue_delayed_work - queue work on a workqueue after delay 13600a13c00eSTejun Heo * @wq: workqueue to use 13610a13c00eSTejun Heo * @dwork: delayable work to queue 13620a13c00eSTejun Heo * @delay: number of jiffies to wait before queueing 13630a13c00eSTejun Heo * 1364715f1300STejun Heo * Equivalent to queue_delayed_work_on() but tries to use the local CPU. 13650a13c00eSTejun Heo */ 1366d4283e93STejun Heo bool queue_delayed_work(struct workqueue_struct *wq, 13670a13c00eSTejun Heo struct delayed_work *dwork, unsigned long delay) 13680a13c00eSTejun Heo { 136957469821STejun Heo return queue_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 13700a13c00eSTejun Heo } 13710a13c00eSTejun Heo EXPORT_SYMBOL_GPL(queue_delayed_work); 13720a13c00eSTejun Heo 13730a13c00eSTejun Heo /** 13748376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 13758376fe22STejun Heo * @cpu: CPU number to execute work on 13768376fe22STejun Heo * @wq: workqueue to use 13778376fe22STejun Heo * @dwork: work to queue 13788376fe22STejun Heo * @delay: number of jiffies to wait before queueing 13798376fe22STejun Heo * 13808376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 13818376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 13828376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 13838376fe22STejun Heo * current state. 13848376fe22STejun Heo * 13858376fe22STejun Heo * Returns %false if @dwork was idle and queued, %true if @dwork was 13868376fe22STejun Heo * pending and its timer was modified. 13878376fe22STejun Heo * 1388e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 13898376fe22STejun Heo * See try_to_grab_pending() for details. 13908376fe22STejun Heo */ 13918376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 13928376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 13938376fe22STejun Heo { 13948376fe22STejun Heo unsigned long flags; 13958376fe22STejun Heo int ret; 13968376fe22STejun Heo 13978376fe22STejun Heo do { 13988376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 13998376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 14008376fe22STejun Heo 14018376fe22STejun Heo if (likely(ret >= 0)) { 14028376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 14038376fe22STejun Heo local_irq_restore(flags); 14048376fe22STejun Heo } 14058376fe22STejun Heo 14068376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 14078376fe22STejun Heo return ret; 14088376fe22STejun Heo } 14098376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 14108376fe22STejun Heo 14118376fe22STejun Heo /** 14128376fe22STejun Heo * mod_delayed_work - modify delay of or queue a delayed work 14138376fe22STejun Heo * @wq: workqueue to use 14148376fe22STejun Heo * @dwork: work to queue 14158376fe22STejun Heo * @delay: number of jiffies to wait before queueing 14168376fe22STejun Heo * 14178376fe22STejun Heo * mod_delayed_work_on() on local CPU. 14188376fe22STejun Heo */ 14198376fe22STejun Heo bool mod_delayed_work(struct workqueue_struct *wq, struct delayed_work *dwork, 14208376fe22STejun Heo unsigned long delay) 14218376fe22STejun Heo { 14228376fe22STejun Heo return mod_delayed_work_on(WORK_CPU_UNBOUND, wq, dwork, delay); 14238376fe22STejun Heo } 14248376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work); 14258376fe22STejun Heo 14268376fe22STejun Heo /** 1427c8e55f36STejun Heo * worker_enter_idle - enter idle state 1428c8e55f36STejun Heo * @worker: worker which is entering idle state 1429c8e55f36STejun Heo * 1430c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1431c8e55f36STejun Heo * necessary. 1432c8e55f36STejun Heo * 1433c8e55f36STejun Heo * LOCKING: 1434d565ed63STejun Heo * spin_lock_irq(pool->lock). 1435c8e55f36STejun Heo */ 1436c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 14371da177e4SLinus Torvalds { 1438bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1439c8e55f36STejun Heo 14406183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 14416183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 14426183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 14436183c009STejun Heo return; 1444c8e55f36STejun Heo 1445cb444766STejun Heo /* can't use worker_set_flags(), also called from start_worker() */ 1446cb444766STejun Heo worker->flags |= WORKER_IDLE; 1447bd7bdd43STejun Heo pool->nr_idle++; 1448e22bee78STejun Heo worker->last_active = jiffies; 1449c8e55f36STejun Heo 1450c8e55f36STejun Heo /* idle_list is LIFO */ 1451bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1452db7bccf4STejun Heo 145363d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1454628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1455cb444766STejun Heo 1456544ecf31STejun Heo /* 1457706026c2STejun Heo * Sanity check nr_running. Because wq_unbind_fn() releases 1458d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1459628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1460628c78e7STejun Heo * unbind is not in progress. 1461544ecf31STejun Heo */ 146224647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1463bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1464e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1465c8e55f36STejun Heo } 1466c8e55f36STejun Heo 1467c8e55f36STejun Heo /** 1468c8e55f36STejun Heo * worker_leave_idle - leave idle state 1469c8e55f36STejun Heo * @worker: worker which is leaving idle state 1470c8e55f36STejun Heo * 1471c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1472c8e55f36STejun Heo * 1473c8e55f36STejun Heo * LOCKING: 1474d565ed63STejun Heo * spin_lock_irq(pool->lock). 1475c8e55f36STejun Heo */ 1476c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1477c8e55f36STejun Heo { 1478bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1479c8e55f36STejun Heo 14806183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 14816183c009STejun Heo return; 1482d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1483bd7bdd43STejun Heo pool->nr_idle--; 1484c8e55f36STejun Heo list_del_init(&worker->entry); 1485c8e55f36STejun Heo } 1486c8e55f36STejun Heo 1487e22bee78STejun Heo /** 1488f36dc67bSLai Jiangshan * worker_maybe_bind_and_lock - try to bind %current to worker_pool and lock it 1489f36dc67bSLai Jiangshan * @pool: target worker_pool 1490f36dc67bSLai Jiangshan * 1491f36dc67bSLai Jiangshan * Bind %current to the cpu of @pool if it is associated and lock @pool. 1492e22bee78STejun Heo * 1493e22bee78STejun Heo * Works which are scheduled while the cpu is online must at least be 1494e22bee78STejun Heo * scheduled to a worker which is bound to the cpu so that if they are 1495e22bee78STejun Heo * flushed from cpu callbacks while cpu is going down, they are 1496e22bee78STejun Heo * guaranteed to execute on the cpu. 1497e22bee78STejun Heo * 1498f5faa077SLai Jiangshan * This function is to be used by unbound workers and rescuers to bind 1499e22bee78STejun Heo * themselves to the target cpu and may race with cpu going down or 1500e22bee78STejun Heo * coming online. kthread_bind() can't be used because it may put the 1501e22bee78STejun Heo * worker to already dead cpu and set_cpus_allowed_ptr() can't be used 1502706026c2STejun Heo * verbatim as it's best effort and blocking and pool may be 1503e22bee78STejun Heo * [dis]associated in the meantime. 1504e22bee78STejun Heo * 1505706026c2STejun Heo * This function tries set_cpus_allowed() and locks pool and verifies the 150624647570STejun Heo * binding against %POOL_DISASSOCIATED which is set during 1507f2d5a0eeSTejun Heo * %CPU_DOWN_PREPARE and cleared during %CPU_ONLINE, so if the worker 1508f2d5a0eeSTejun Heo * enters idle state or fetches works without dropping lock, it can 1509f2d5a0eeSTejun Heo * guarantee the scheduling requirement described in the first paragraph. 1510e22bee78STejun Heo * 1511e22bee78STejun Heo * CONTEXT: 1512d565ed63STejun Heo * Might sleep. Called without any lock but returns with pool->lock 1513e22bee78STejun Heo * held. 1514e22bee78STejun Heo * 1515e22bee78STejun Heo * RETURNS: 1516706026c2STejun Heo * %true if the associated pool is online (@worker is successfully 1517e22bee78STejun Heo * bound), %false if offline. 1518e22bee78STejun Heo */ 1519f36dc67bSLai Jiangshan static bool worker_maybe_bind_and_lock(struct worker_pool *pool) 1520d565ed63STejun Heo __acquires(&pool->lock) 1521e22bee78STejun Heo { 1522e22bee78STejun Heo while (true) { 1523e22bee78STejun Heo /* 1524e22bee78STejun Heo * The following call may fail, succeed or succeed 1525e22bee78STejun Heo * without actually migrating the task to the cpu if 1526e22bee78STejun Heo * it races with cpu hotunplug operation. Verify 152724647570STejun Heo * against POOL_DISASSOCIATED. 1528e22bee78STejun Heo */ 152924647570STejun Heo if (!(pool->flags & POOL_DISASSOCIATED)) 15307a4e344cSTejun Heo set_cpus_allowed_ptr(current, pool->attrs->cpumask); 1531e22bee78STejun Heo 1532d565ed63STejun Heo spin_lock_irq(&pool->lock); 153324647570STejun Heo if (pool->flags & POOL_DISASSOCIATED) 1534e22bee78STejun Heo return false; 1535f5faa077SLai Jiangshan if (task_cpu(current) == pool->cpu && 15367a4e344cSTejun Heo cpumask_equal(¤t->cpus_allowed, pool->attrs->cpumask)) 1537e22bee78STejun Heo return true; 1538d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1539e22bee78STejun Heo 15405035b20fSTejun Heo /* 15415035b20fSTejun Heo * We've raced with CPU hot[un]plug. Give it a breather 15425035b20fSTejun Heo * and retry migration. cond_resched() is required here; 15435035b20fSTejun Heo * otherwise, we might deadlock against cpu_stop trying to 15445035b20fSTejun Heo * bring down the CPU on non-preemptive kernel. 15455035b20fSTejun Heo */ 1546e22bee78STejun Heo cpu_relax(); 15475035b20fSTejun Heo cond_resched(); 1548e22bee78STejun Heo } 1549e22bee78STejun Heo } 1550e22bee78STejun Heo 1551e22bee78STejun Heo /* 1552ea1abd61SLai Jiangshan * Rebind an idle @worker to its CPU. worker_thread() will test 15535f7dabfdSLai Jiangshan * list_empty(@worker->entry) before leaving idle and call this function. 155425511a47STejun Heo */ 155525511a47STejun Heo static void idle_worker_rebind(struct worker *worker) 155625511a47STejun Heo { 15575f7dabfdSLai Jiangshan /* CPU may go down again inbetween, clear UNBOUND only on success */ 1558f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(worker->pool)) 15595f7dabfdSLai Jiangshan worker_clr_flags(worker, WORKER_UNBOUND); 156025511a47STejun Heo 1561ea1abd61SLai Jiangshan /* rebind complete, become available again */ 1562ea1abd61SLai Jiangshan list_add(&worker->entry, &worker->pool->idle_list); 1563d565ed63STejun Heo spin_unlock_irq(&worker->pool->lock); 156425511a47STejun Heo } 156525511a47STejun Heo 156625511a47STejun Heo /* 156725511a47STejun Heo * Function for @worker->rebind.work used to rebind unbound busy workers to 1568403c821dSTejun Heo * the associated cpu which is coming back online. This is scheduled by 1569403c821dSTejun Heo * cpu up but can race with other cpu hotplug operations and may be 1570403c821dSTejun Heo * executed twice without intervening cpu down. 1571e22bee78STejun Heo */ 157225511a47STejun Heo static void busy_worker_rebind_fn(struct work_struct *work) 1573e22bee78STejun Heo { 1574e22bee78STejun Heo struct worker *worker = container_of(work, struct worker, rebind_work); 1575e22bee78STejun Heo 1576f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(worker->pool)) 1577eab6d828SLai Jiangshan worker_clr_flags(worker, WORKER_UNBOUND); 1578e22bee78STejun Heo 1579d565ed63STejun Heo spin_unlock_irq(&worker->pool->lock); 1580e22bee78STejun Heo } 1581e22bee78STejun Heo 158225511a47STejun Heo /** 158394cf58bbSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 158494cf58bbSTejun Heo * @pool: pool of interest 158525511a47STejun Heo * 158694cf58bbSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. Rebinding 158725511a47STejun Heo * is different for idle and busy ones. 158825511a47STejun Heo * 1589ea1abd61SLai Jiangshan * Idle ones will be removed from the idle_list and woken up. They will 1590ea1abd61SLai Jiangshan * add themselves back after completing rebind. This ensures that the 1591ea1abd61SLai Jiangshan * idle_list doesn't contain any unbound workers when re-bound busy workers 1592ea1abd61SLai Jiangshan * try to perform local wake-ups for concurrency management. 159325511a47STejun Heo * 1594ea1abd61SLai Jiangshan * Busy workers can rebind after they finish their current work items. 1595ea1abd61SLai Jiangshan * Queueing the rebind work item at the head of the scheduled list is 1596ea1abd61SLai Jiangshan * enough. Note that nr_running will be properly bumped as busy workers 1597ea1abd61SLai Jiangshan * rebind. 159825511a47STejun Heo * 1599ea1abd61SLai Jiangshan * On return, all non-manager workers are scheduled for rebind - see 1600ea1abd61SLai Jiangshan * manage_workers() for the manager special case. Any idle worker 1601ea1abd61SLai Jiangshan * including the manager will not appear on @idle_list until rebind is 1602ea1abd61SLai Jiangshan * complete, making local wake-ups safe. 160325511a47STejun Heo */ 160494cf58bbSTejun Heo static void rebind_workers(struct worker_pool *pool) 160525511a47STejun Heo { 1606ea1abd61SLai Jiangshan struct worker *worker, *n; 160725511a47STejun Heo int i; 160825511a47STejun Heo 1609b2eb83d1SLai Jiangshan lockdep_assert_held(&pool->assoc_mutex); 1610d565ed63STejun Heo lockdep_assert_held(&pool->lock); 161125511a47STejun Heo 16125f7dabfdSLai Jiangshan /* dequeue and kick idle ones */ 1613ea1abd61SLai Jiangshan list_for_each_entry_safe(worker, n, &pool->idle_list, entry) { 1614ea1abd61SLai Jiangshan /* 161594cf58bbSTejun Heo * idle workers should be off @pool->idle_list until rebind 161694cf58bbSTejun Heo * is complete to avoid receiving premature local wake-ups. 1617ea1abd61SLai Jiangshan */ 1618ea1abd61SLai Jiangshan list_del_init(&worker->entry); 161925511a47STejun Heo 162025511a47STejun Heo /* 162194cf58bbSTejun Heo * worker_thread() will see the above dequeuing and call 162294cf58bbSTejun Heo * idle_worker_rebind(). 162325511a47STejun Heo */ 162425511a47STejun Heo wake_up_process(worker->task); 162525511a47STejun Heo } 162625511a47STejun Heo 1627ea1abd61SLai Jiangshan /* rebind busy workers */ 1628b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) { 162925511a47STejun Heo struct work_struct *rebind_work = &worker->rebind_work; 1630e2b6a6d5SJoonsoo Kim struct workqueue_struct *wq; 163125511a47STejun Heo 163225511a47STejun Heo if (test_and_set_bit(WORK_STRUCT_PENDING_BIT, 163325511a47STejun Heo work_data_bits(rebind_work))) 163425511a47STejun Heo continue; 163525511a47STejun Heo 163625511a47STejun Heo debug_work_activate(rebind_work); 163790beca5dSTejun Heo 163890beca5dSTejun Heo /* 163994cf58bbSTejun Heo * wq doesn't really matter but let's keep @worker->pool 1640112202d9STejun Heo * and @pwq->pool consistent for sanity. 164190beca5dSTejun Heo */ 16427a4e344cSTejun Heo if (worker->pool->attrs->nice < 0) 1643e2b6a6d5SJoonsoo Kim wq = system_highpri_wq; 1644e2b6a6d5SJoonsoo Kim else 1645e2b6a6d5SJoonsoo Kim wq = system_wq; 1646ec58815aSTejun Heo 16477fb98ea7STejun Heo insert_work(per_cpu_ptr(wq->cpu_pwqs, pool->cpu), rebind_work, 164825511a47STejun Heo worker->scheduled.next, 164925511a47STejun Heo work_color_to_flags(WORK_NO_COLOR)); 1650ec58815aSTejun Heo } 165125511a47STejun Heo } 165225511a47STejun Heo 1653c34056a3STejun Heo static struct worker *alloc_worker(void) 1654c34056a3STejun Heo { 1655c34056a3STejun Heo struct worker *worker; 1656c34056a3STejun Heo 1657c34056a3STejun Heo worker = kzalloc(sizeof(*worker), GFP_KERNEL); 1658c8e55f36STejun Heo if (worker) { 1659c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1660affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 166125511a47STejun Heo INIT_WORK(&worker->rebind_work, busy_worker_rebind_fn); 1662e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1663e22bee78STejun Heo worker->flags = WORKER_PREP; 1664c8e55f36STejun Heo } 1665c34056a3STejun Heo return worker; 1666c34056a3STejun Heo } 1667c34056a3STejun Heo 1668c34056a3STejun Heo /** 1669c34056a3STejun Heo * create_worker - create a new workqueue worker 167063d95a91STejun Heo * @pool: pool the new worker will belong to 1671c34056a3STejun Heo * 167263d95a91STejun Heo * Create a new worker which is bound to @pool. The returned worker 1673c34056a3STejun Heo * can be started by calling start_worker() or destroyed using 1674c34056a3STejun Heo * destroy_worker(). 1675c34056a3STejun Heo * 1676c34056a3STejun Heo * CONTEXT: 1677c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1678c34056a3STejun Heo * 1679c34056a3STejun Heo * RETURNS: 1680c34056a3STejun Heo * Pointer to the newly created worker. 1681c34056a3STejun Heo */ 1682bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1683c34056a3STejun Heo { 16847a4e344cSTejun Heo const char *pri = pool->attrs->nice < 0 ? "H" : ""; 1685c34056a3STejun Heo struct worker *worker = NULL; 1686f3421797STejun Heo int id = -1; 1687c34056a3STejun Heo 1688d565ed63STejun Heo spin_lock_irq(&pool->lock); 1689bd7bdd43STejun Heo while (ida_get_new(&pool->worker_ida, &id)) { 1690d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1691bd7bdd43STejun Heo if (!ida_pre_get(&pool->worker_ida, GFP_KERNEL)) 1692c34056a3STejun Heo goto fail; 1693d565ed63STejun Heo spin_lock_irq(&pool->lock); 1694c34056a3STejun Heo } 1695d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1696c34056a3STejun Heo 1697c34056a3STejun Heo worker = alloc_worker(); 1698c34056a3STejun Heo if (!worker) 1699c34056a3STejun Heo goto fail; 1700c34056a3STejun Heo 1701bd7bdd43STejun Heo worker->pool = pool; 1702c34056a3STejun Heo worker->id = id; 1703c34056a3STejun Heo 170429c91e99STejun Heo if (pool->cpu >= 0) 170594dcf29aSEric Dumazet worker->task = kthread_create_on_node(worker_thread, 1706ec22ca5eSTejun Heo worker, cpu_to_node(pool->cpu), 1707d84ff051STejun Heo "kworker/%d:%d%s", pool->cpu, id, pri); 1708f3421797STejun Heo else 1709f3421797STejun Heo worker->task = kthread_create(worker_thread, worker, 1710*ac6104cdSTejun Heo "kworker/u%d:%d%s", 1711*ac6104cdSTejun Heo pool->id, id, pri); 1712c34056a3STejun Heo if (IS_ERR(worker->task)) 1713c34056a3STejun Heo goto fail; 1714c34056a3STejun Heo 17157a4e344cSTejun Heo set_user_nice(worker->task, pool->attrs->nice); 17167a4e344cSTejun Heo set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 17173270476aSTejun Heo 1718db7bccf4STejun Heo /* 17197a4e344cSTejun Heo * %PF_THREAD_BOUND is used to prevent userland from meddling with 17207a4e344cSTejun Heo * cpumask of workqueue workers. This is an abuse. We need 17217a4e344cSTejun Heo * %PF_NO_SETAFFINITY. 1722db7bccf4STejun Heo */ 1723db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 17247a4e344cSTejun Heo 17257a4e344cSTejun Heo /* 17267a4e344cSTejun Heo * The caller is responsible for ensuring %POOL_DISASSOCIATED 17277a4e344cSTejun Heo * remains stable across this function. See the comments above the 17287a4e344cSTejun Heo * flag definition for details. 17297a4e344cSTejun Heo */ 17307a4e344cSTejun Heo if (pool->flags & POOL_DISASSOCIATED) 1731f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1732c34056a3STejun Heo 1733c34056a3STejun Heo return worker; 1734c34056a3STejun Heo fail: 1735c34056a3STejun Heo if (id >= 0) { 1736d565ed63STejun Heo spin_lock_irq(&pool->lock); 1737bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1738d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1739c34056a3STejun Heo } 1740c34056a3STejun Heo kfree(worker); 1741c34056a3STejun Heo return NULL; 1742c34056a3STejun Heo } 1743c34056a3STejun Heo 1744c34056a3STejun Heo /** 1745c34056a3STejun Heo * start_worker - start a newly created worker 1746c34056a3STejun Heo * @worker: worker to start 1747c34056a3STejun Heo * 1748706026c2STejun Heo * Make the pool aware of @worker and start it. 1749c34056a3STejun Heo * 1750c34056a3STejun Heo * CONTEXT: 1751d565ed63STejun Heo * spin_lock_irq(pool->lock). 1752c34056a3STejun Heo */ 1753c34056a3STejun Heo static void start_worker(struct worker *worker) 1754c34056a3STejun Heo { 1755cb444766STejun Heo worker->flags |= WORKER_STARTED; 1756bd7bdd43STejun Heo worker->pool->nr_workers++; 1757c8e55f36STejun Heo worker_enter_idle(worker); 1758c34056a3STejun Heo wake_up_process(worker->task); 1759c34056a3STejun Heo } 1760c34056a3STejun Heo 1761c34056a3STejun Heo /** 1762c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1763c34056a3STejun Heo * @worker: worker to be destroyed 1764c34056a3STejun Heo * 1765706026c2STejun Heo * Destroy @worker and adjust @pool stats accordingly. 1766c8e55f36STejun Heo * 1767c8e55f36STejun Heo * CONTEXT: 1768d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 1769c34056a3STejun Heo */ 1770c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1771c34056a3STejun Heo { 1772bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1773c34056a3STejun Heo int id = worker->id; 1774c34056a3STejun Heo 1775c34056a3STejun Heo /* sanity check frenzy */ 17766183c009STejun Heo if (WARN_ON(worker->current_work) || 17776183c009STejun Heo WARN_ON(!list_empty(&worker->scheduled))) 17786183c009STejun Heo return; 1779c34056a3STejun Heo 1780c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1781bd7bdd43STejun Heo pool->nr_workers--; 1782c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1783bd7bdd43STejun Heo pool->nr_idle--; 1784c8e55f36STejun Heo 1785c8e55f36STejun Heo list_del_init(&worker->entry); 1786cb444766STejun Heo worker->flags |= WORKER_DIE; 1787c8e55f36STejun Heo 1788d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1789c8e55f36STejun Heo 1790c34056a3STejun Heo kthread_stop(worker->task); 1791c34056a3STejun Heo kfree(worker); 1792c34056a3STejun Heo 1793d565ed63STejun Heo spin_lock_irq(&pool->lock); 1794bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1795c34056a3STejun Heo } 1796c34056a3STejun Heo 179763d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1798e22bee78STejun Heo { 179963d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1800e22bee78STejun Heo 1801d565ed63STejun Heo spin_lock_irq(&pool->lock); 1802e22bee78STejun Heo 180363d95a91STejun Heo if (too_many_workers(pool)) { 1804e22bee78STejun Heo struct worker *worker; 1805e22bee78STejun Heo unsigned long expires; 1806e22bee78STejun Heo 1807e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 180863d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1809e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1810e22bee78STejun Heo 1811e22bee78STejun Heo if (time_before(jiffies, expires)) 181263d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1813e22bee78STejun Heo else { 1814e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 181511ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 181663d95a91STejun Heo wake_up_worker(pool); 1817e22bee78STejun Heo } 1818e22bee78STejun Heo } 1819e22bee78STejun Heo 1820d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1821e22bee78STejun Heo } 1822e22bee78STejun Heo 1823493a1724STejun Heo static void send_mayday(struct work_struct *work) 1824e22bee78STejun Heo { 1825112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1826112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1827493a1724STejun Heo 1828493a1724STejun Heo lockdep_assert_held(&workqueue_lock); 1829e22bee78STejun Heo 1830e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1831493a1724STejun Heo return; 1832e22bee78STejun Heo 1833e22bee78STejun Heo /* mayday mayday mayday */ 1834493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 1835493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1836e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1837493a1724STejun Heo } 1838e22bee78STejun Heo } 1839e22bee78STejun Heo 1840706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool) 1841e22bee78STejun Heo { 184263d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1843e22bee78STejun Heo struct work_struct *work; 1844e22bee78STejun Heo 1845493a1724STejun Heo spin_lock_irq(&workqueue_lock); /* for wq->maydays */ 1846493a1724STejun Heo spin_lock(&pool->lock); 1847e22bee78STejun Heo 184863d95a91STejun Heo if (need_to_create_worker(pool)) { 1849e22bee78STejun Heo /* 1850e22bee78STejun Heo * We've been trying to create a new worker but 1851e22bee78STejun Heo * haven't been successful. We might be hitting an 1852e22bee78STejun Heo * allocation deadlock. Send distress signals to 1853e22bee78STejun Heo * rescuers. 1854e22bee78STejun Heo */ 185563d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1856e22bee78STejun Heo send_mayday(work); 1857e22bee78STejun Heo } 1858e22bee78STejun Heo 1859493a1724STejun Heo spin_unlock(&pool->lock); 1860493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 1861e22bee78STejun Heo 186263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1863e22bee78STejun Heo } 1864e22bee78STejun Heo 1865e22bee78STejun Heo /** 1866e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 186763d95a91STejun Heo * @pool: pool to create a new worker for 1868e22bee78STejun Heo * 186963d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1870e22bee78STejun Heo * have at least one idle worker on return from this function. If 1871e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 187263d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1873e22bee78STejun Heo * possible allocation deadlock. 1874e22bee78STejun Heo * 1875e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1876e22bee78STejun Heo * may_start_working() true. 1877e22bee78STejun Heo * 1878e22bee78STejun Heo * LOCKING: 1879d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1880e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1881e22bee78STejun Heo * manager. 1882e22bee78STejun Heo * 1883e22bee78STejun Heo * RETURNS: 1884d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1885e22bee78STejun Heo * otherwise. 1886e22bee78STejun Heo */ 188763d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 1888d565ed63STejun Heo __releases(&pool->lock) 1889d565ed63STejun Heo __acquires(&pool->lock) 1890e22bee78STejun Heo { 189163d95a91STejun Heo if (!need_to_create_worker(pool)) 1892e22bee78STejun Heo return false; 1893e22bee78STejun Heo restart: 1894d565ed63STejun Heo spin_unlock_irq(&pool->lock); 18959f9c2364STejun Heo 1896e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 189763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1898e22bee78STejun Heo 1899e22bee78STejun Heo while (true) { 1900e22bee78STejun Heo struct worker *worker; 1901e22bee78STejun Heo 1902bc2ae0f5STejun Heo worker = create_worker(pool); 1903e22bee78STejun Heo if (worker) { 190463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1905d565ed63STejun Heo spin_lock_irq(&pool->lock); 1906e22bee78STejun Heo start_worker(worker); 19076183c009STejun Heo if (WARN_ON_ONCE(need_to_create_worker(pool))) 19086183c009STejun Heo goto restart; 1909e22bee78STejun Heo return true; 1910e22bee78STejun Heo } 1911e22bee78STejun Heo 191263d95a91STejun Heo if (!need_to_create_worker(pool)) 1913e22bee78STejun Heo break; 1914e22bee78STejun Heo 1915e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1916e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 19179f9c2364STejun Heo 191863d95a91STejun Heo if (!need_to_create_worker(pool)) 1919e22bee78STejun Heo break; 1920e22bee78STejun Heo } 1921e22bee78STejun Heo 192263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1923d565ed63STejun Heo spin_lock_irq(&pool->lock); 192463d95a91STejun Heo if (need_to_create_worker(pool)) 1925e22bee78STejun Heo goto restart; 1926e22bee78STejun Heo return true; 1927e22bee78STejun Heo } 1928e22bee78STejun Heo 1929e22bee78STejun Heo /** 1930e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 193163d95a91STejun Heo * @pool: pool to destroy workers for 1932e22bee78STejun Heo * 193363d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1934e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1935e22bee78STejun Heo * 1936e22bee78STejun Heo * LOCKING: 1937d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1938e22bee78STejun Heo * multiple times. Called only from manager. 1939e22bee78STejun Heo * 1940e22bee78STejun Heo * RETURNS: 1941d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1942e22bee78STejun Heo * otherwise. 1943e22bee78STejun Heo */ 194463d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1945e22bee78STejun Heo { 1946e22bee78STejun Heo bool ret = false; 1947e22bee78STejun Heo 194863d95a91STejun Heo while (too_many_workers(pool)) { 1949e22bee78STejun Heo struct worker *worker; 1950e22bee78STejun Heo unsigned long expires; 1951e22bee78STejun Heo 195263d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1953e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1954e22bee78STejun Heo 1955e22bee78STejun Heo if (time_before(jiffies, expires)) { 195663d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1957e22bee78STejun Heo break; 1958e22bee78STejun Heo } 1959e22bee78STejun Heo 1960e22bee78STejun Heo destroy_worker(worker); 1961e22bee78STejun Heo ret = true; 1962e22bee78STejun Heo } 1963e22bee78STejun Heo 1964e22bee78STejun Heo return ret; 1965e22bee78STejun Heo } 1966e22bee78STejun Heo 1967e22bee78STejun Heo /** 1968e22bee78STejun Heo * manage_workers - manage worker pool 1969e22bee78STejun Heo * @worker: self 1970e22bee78STejun Heo * 1971706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1972e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1973706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1974e22bee78STejun Heo * 1975e22bee78STejun Heo * The caller can safely start processing works on false return. On 1976e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1977e22bee78STejun Heo * and may_start_working() is true. 1978e22bee78STejun Heo * 1979e22bee78STejun Heo * CONTEXT: 1980d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1981e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1982e22bee78STejun Heo * 1983e22bee78STejun Heo * RETURNS: 1984d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1985d565ed63STejun Heo * multiple times. Does GFP_KERNEL allocations. 1986e22bee78STejun Heo */ 1987e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1988e22bee78STejun Heo { 198963d95a91STejun Heo struct worker_pool *pool = worker->pool; 1990e22bee78STejun Heo bool ret = false; 1991e22bee78STejun Heo 199234a06bd6STejun Heo if (!mutex_trylock(&pool->manager_arb)) 1993e22bee78STejun Heo return ret; 1994e22bee78STejun Heo 1995ee378aa4SLai Jiangshan /* 1996ee378aa4SLai Jiangshan * To simplify both worker management and CPU hotplug, hold off 1997ee378aa4SLai Jiangshan * management while hotplug is in progress. CPU hotplug path can't 199834a06bd6STejun Heo * grab @pool->manager_arb to achieve this because that can lead to 199934a06bd6STejun Heo * idle worker depletion (all become busy thinking someone else is 200034a06bd6STejun Heo * managing) which in turn can result in deadlock under extreme 200134a06bd6STejun Heo * circumstances. Use @pool->assoc_mutex to synchronize manager 200234a06bd6STejun Heo * against CPU hotplug. 2003ee378aa4SLai Jiangshan * 2004b2eb83d1SLai Jiangshan * assoc_mutex would always be free unless CPU hotplug is in 2005d565ed63STejun Heo * progress. trylock first without dropping @pool->lock. 2006ee378aa4SLai Jiangshan */ 2007b2eb83d1SLai Jiangshan if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { 2008d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2009b2eb83d1SLai Jiangshan mutex_lock(&pool->assoc_mutex); 2010ee378aa4SLai Jiangshan /* 2011ee378aa4SLai Jiangshan * CPU hotplug could have happened while we were waiting 2012b2eb83d1SLai Jiangshan * for assoc_mutex. Hotplug itself can't handle us 2013ee378aa4SLai Jiangshan * because manager isn't either on idle or busy list, and 2014706026c2STejun Heo * @pool's state and ours could have deviated. 2015ee378aa4SLai Jiangshan * 2016b2eb83d1SLai Jiangshan * As hotplug is now excluded via assoc_mutex, we can 2017ee378aa4SLai Jiangshan * simply try to bind. It will succeed or fail depending 2018706026c2STejun Heo * on @pool's current state. Try it and adjust 2019ee378aa4SLai Jiangshan * %WORKER_UNBOUND accordingly. 2020ee378aa4SLai Jiangshan */ 2021f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(pool)) 2022ee378aa4SLai Jiangshan worker->flags &= ~WORKER_UNBOUND; 2023ee378aa4SLai Jiangshan else 2024ee378aa4SLai Jiangshan worker->flags |= WORKER_UNBOUND; 2025ee378aa4SLai Jiangshan 2026ee378aa4SLai Jiangshan ret = true; 2027ee378aa4SLai Jiangshan } 2028ee378aa4SLai Jiangshan 202911ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 2030e22bee78STejun Heo 2031e22bee78STejun Heo /* 2032e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 2033e22bee78STejun Heo * on return. 2034e22bee78STejun Heo */ 203563d95a91STejun Heo ret |= maybe_destroy_workers(pool); 203663d95a91STejun Heo ret |= maybe_create_worker(pool); 2037e22bee78STejun Heo 2038b2eb83d1SLai Jiangshan mutex_unlock(&pool->assoc_mutex); 203934a06bd6STejun Heo mutex_unlock(&pool->manager_arb); 2040e22bee78STejun Heo return ret; 2041e22bee78STejun Heo } 2042e22bee78STejun Heo 2043a62428c0STejun Heo /** 2044a62428c0STejun Heo * process_one_work - process single work 2045c34056a3STejun Heo * @worker: self 2046a62428c0STejun Heo * @work: work to process 2047a62428c0STejun Heo * 2048a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2049a62428c0STejun Heo * process a single work including synchronization against and 2050a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2051a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2052a62428c0STejun Heo * call this function to process a work. 2053a62428c0STejun Heo * 2054a62428c0STejun Heo * CONTEXT: 2055d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2056a62428c0STejun Heo */ 2057c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2058d565ed63STejun Heo __releases(&pool->lock) 2059d565ed63STejun Heo __acquires(&pool->lock) 20601da177e4SLinus Torvalds { 2061112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2062bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2063112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 206473f53c4aSTejun Heo int work_color; 20657e11629dSTejun Heo struct worker *collision; 20664e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20674e6045f1SJohannes Berg /* 2068a62428c0STejun Heo * It is permissible to free the struct work_struct from 2069a62428c0STejun Heo * inside the function that is called from it, this we need to 2070a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2071a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2072a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20734e6045f1SJohannes Berg */ 20744d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20754d82a1deSPeter Zijlstra 20764d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20774e6045f1SJohannes Berg #endif 20786fec10a1STejun Heo /* 20796fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 20806fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 208124647570STejun Heo * unbound or a disassociated pool. 20826fec10a1STejun Heo */ 20835f7dabfdSLai Jiangshan WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) && 208424647570STejun Heo !(pool->flags & POOL_DISASSOCIATED) && 2085ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 208625511a47STejun Heo 20877e11629dSTejun Heo /* 20887e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20897e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20907e11629dSTejun Heo * already processing the work. If so, defer the work to the 20917e11629dSTejun Heo * currently executing one. 20927e11629dSTejun Heo */ 2093c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 20947e11629dSTejun Heo if (unlikely(collision)) { 20957e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20967e11629dSTejun Heo return; 20977e11629dSTejun Heo } 20981da177e4SLinus Torvalds 20998930cabaSTejun Heo /* claim and dequeue */ 21001da177e4SLinus Torvalds debug_work_deactivate(work); 2101c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2102c34056a3STejun Heo worker->current_work = work; 2103a2c1c57bSTejun Heo worker->current_func = work->func; 2104112202d9STejun Heo worker->current_pwq = pwq; 210573f53c4aSTejun Heo work_color = get_work_color(work); 21067a22ad75STejun Heo 2107a62428c0STejun Heo list_del_init(&work->entry); 2108a62428c0STejun Heo 2109649027d7STejun Heo /* 2110fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2111fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2112fb0e7bebSTejun Heo */ 2113fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2114fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2115fb0e7bebSTejun Heo 2116974271c4STejun Heo /* 2117d565ed63STejun Heo * Unbound pool isn't concurrency managed and work items should be 2118974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2119974271c4STejun Heo */ 212063d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 212163d95a91STejun Heo wake_up_worker(pool); 2122974271c4STejun Heo 21238930cabaSTejun Heo /* 21247c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2125d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 212623657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 212723657bb1STejun Heo * disabled. 21288930cabaSTejun Heo */ 21297c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 21301da177e4SLinus Torvalds 2131d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2132365970a1SDavid Howells 2133112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 21343295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2135e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2136a2c1c57bSTejun Heo worker->current_func(work); 2137e36c886aSArjan van de Ven /* 2138e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2139e36c886aSArjan van de Ven * point will only record its address. 2140e36c886aSArjan van de Ven */ 2141e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21423295f0efSIngo Molnar lock_map_release(&lockdep_map); 2143112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 21441da177e4SLinus Torvalds 2145d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2146044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2147044c782cSValentin Ilie " last function: %pf\n", 2148a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2149a2c1c57bSTejun Heo worker->current_func); 2150d5abe669SPeter Zijlstra debug_show_held_locks(current); 2151d5abe669SPeter Zijlstra dump_stack(); 2152d5abe669SPeter Zijlstra } 2153d5abe669SPeter Zijlstra 2154d565ed63STejun Heo spin_lock_irq(&pool->lock); 2155a62428c0STejun Heo 2156fb0e7bebSTejun Heo /* clear cpu intensive status */ 2157fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2158fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2159fb0e7bebSTejun Heo 2160a62428c0STejun Heo /* we're done with it, release */ 216142f8570fSSasha Levin hash_del(&worker->hentry); 2162c34056a3STejun Heo worker->current_work = NULL; 2163a2c1c57bSTejun Heo worker->current_func = NULL; 2164112202d9STejun Heo worker->current_pwq = NULL; 2165112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 21661da177e4SLinus Torvalds } 21671da177e4SLinus Torvalds 2168affee4b2STejun Heo /** 2169affee4b2STejun Heo * process_scheduled_works - process scheduled works 2170affee4b2STejun Heo * @worker: self 2171affee4b2STejun Heo * 2172affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2173affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2174affee4b2STejun Heo * fetches a work from the top and executes it. 2175affee4b2STejun Heo * 2176affee4b2STejun Heo * CONTEXT: 2177d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2178affee4b2STejun Heo * multiple times. 2179affee4b2STejun Heo */ 2180affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21811da177e4SLinus Torvalds { 2182affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2183affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2184a62428c0STejun Heo struct work_struct, entry); 2185c34056a3STejun Heo process_one_work(worker, work); 2186a62428c0STejun Heo } 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds 21894690c4abSTejun Heo /** 21904690c4abSTejun Heo * worker_thread - the worker thread function 2191c34056a3STejun Heo * @__worker: self 21924690c4abSTejun Heo * 2193706026c2STejun Heo * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools 2194706026c2STejun Heo * of these per each cpu. These workers process all works regardless of 2195e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2196e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2197e22bee78STejun Heo * rescuer_thread(). 21984690c4abSTejun Heo */ 2199c34056a3STejun Heo static int worker_thread(void *__worker) 22001da177e4SLinus Torvalds { 2201c34056a3STejun Heo struct worker *worker = __worker; 2202bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 22031da177e4SLinus Torvalds 2204e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2205e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2206c8e55f36STejun Heo woke_up: 2207d565ed63STejun Heo spin_lock_irq(&pool->lock); 2208affee4b2STejun Heo 22095f7dabfdSLai Jiangshan /* we are off idle list if destruction or rebind is requested */ 22105f7dabfdSLai Jiangshan if (unlikely(list_empty(&worker->entry))) { 2211d565ed63STejun Heo spin_unlock_irq(&pool->lock); 221225511a47STejun Heo 22135f7dabfdSLai Jiangshan /* if DIE is set, destruction is requested */ 221425511a47STejun Heo if (worker->flags & WORKER_DIE) { 2215e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2216c8e55f36STejun Heo return 0; 2217c8e55f36STejun Heo } 2218c8e55f36STejun Heo 22195f7dabfdSLai Jiangshan /* otherwise, rebind */ 222025511a47STejun Heo idle_worker_rebind(worker); 222125511a47STejun Heo goto woke_up; 222225511a47STejun Heo } 222325511a47STejun Heo 2224c8e55f36STejun Heo worker_leave_idle(worker); 2225db7bccf4STejun Heo recheck: 2226e22bee78STejun Heo /* no more worker necessary? */ 222763d95a91STejun Heo if (!need_more_worker(pool)) 2228e22bee78STejun Heo goto sleep; 2229e22bee78STejun Heo 2230e22bee78STejun Heo /* do we need to manage? */ 223163d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2232e22bee78STejun Heo goto recheck; 2233e22bee78STejun Heo 2234c8e55f36STejun Heo /* 2235c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2236c8e55f36STejun Heo * preparing to process a work or actually processing it. 2237c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2238c8e55f36STejun Heo */ 22396183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2240c8e55f36STejun Heo 2241e22bee78STejun Heo /* 2242e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2243e22bee78STejun Heo * at least one idle worker or that someone else has already 2244e22bee78STejun Heo * assumed the manager role. 2245e22bee78STejun Heo */ 2246e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2247e22bee78STejun Heo 2248e22bee78STejun Heo do { 2249affee4b2STejun Heo struct work_struct *work = 2250bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2251affee4b2STejun Heo struct work_struct, entry); 2252affee4b2STejun Heo 2253c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2254affee4b2STejun Heo /* optimization path, not strictly necessary */ 2255affee4b2STejun Heo process_one_work(worker, work); 2256affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2257affee4b2STejun Heo process_scheduled_works(worker); 2258affee4b2STejun Heo } else { 2259c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2260affee4b2STejun Heo process_scheduled_works(worker); 2261affee4b2STejun Heo } 226263d95a91STejun Heo } while (keep_working(pool)); 2263affee4b2STejun Heo 2264e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2265d313dd85STejun Heo sleep: 226663d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2267e22bee78STejun Heo goto recheck; 2268d313dd85STejun Heo 2269c8e55f36STejun Heo /* 2270d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2271d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2272d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2273d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2274d565ed63STejun Heo * event. 2275c8e55f36STejun Heo */ 2276c8e55f36STejun Heo worker_enter_idle(worker); 2277c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 2278d565ed63STejun Heo spin_unlock_irq(&pool->lock); 22791da177e4SLinus Torvalds schedule(); 2280c8e55f36STejun Heo goto woke_up; 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 2283e22bee78STejun Heo /** 2284e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2285111c225aSTejun Heo * @__rescuer: self 2286e22bee78STejun Heo * 2287e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2288e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2289e22bee78STejun Heo * 2290706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2291e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2292e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2293e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2294e22bee78STejun Heo * the problem rescuer solves. 2295e22bee78STejun Heo * 2296706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2297706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2298e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2299e22bee78STejun Heo * 2300e22bee78STejun Heo * This should happen rarely. 2301e22bee78STejun Heo */ 2302111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2303e22bee78STejun Heo { 2304111c225aSTejun Heo struct worker *rescuer = __rescuer; 2305111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2306e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2307e22bee78STejun Heo 2308e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2309111c225aSTejun Heo 2310111c225aSTejun Heo /* 2311111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2312111c225aSTejun Heo * doesn't participate in concurrency management. 2313111c225aSTejun Heo */ 2314111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2315e22bee78STejun Heo repeat: 2316e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 23171da177e4SLinus Torvalds 2318412d32e6SMike Galbraith if (kthread_should_stop()) { 2319412d32e6SMike Galbraith __set_current_state(TASK_RUNNING); 2320111c225aSTejun Heo rescuer->task->flags &= ~PF_WQ_WORKER; 2321e22bee78STejun Heo return 0; 2322412d32e6SMike Galbraith } 23231da177e4SLinus Torvalds 2324493a1724STejun Heo /* see whether any pwq is asking for help */ 2325493a1724STejun Heo spin_lock_irq(&workqueue_lock); 2326493a1724STejun Heo 2327493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2328493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2329493a1724STejun Heo struct pool_workqueue, mayday_node); 2330112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2331e22bee78STejun Heo struct work_struct *work, *n; 2332e22bee78STejun Heo 2333e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2334493a1724STejun Heo list_del_init(&pwq->mayday_node); 2335493a1724STejun Heo 2336493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 2337e22bee78STejun Heo 2338e22bee78STejun Heo /* migrate to the target cpu if possible */ 2339f36dc67bSLai Jiangshan worker_maybe_bind_and_lock(pool); 2340b3104104SLai Jiangshan rescuer->pool = pool; 2341e22bee78STejun Heo 2342e22bee78STejun Heo /* 2343e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2344e22bee78STejun Heo * process'em. 2345e22bee78STejun Heo */ 23466183c009STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 2347bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2348112202d9STejun Heo if (get_work_pwq(work) == pwq) 2349e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2350e22bee78STejun Heo 2351e22bee78STejun Heo process_scheduled_works(rescuer); 23527576958aSTejun Heo 23537576958aSTejun Heo /* 2354d565ed63STejun Heo * Leave this pool. If keep_working() is %true, notify a 23557576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23567576958aSTejun Heo * and stalling the execution. 23577576958aSTejun Heo */ 235863d95a91STejun Heo if (keep_working(pool)) 235963d95a91STejun Heo wake_up_worker(pool); 23607576958aSTejun Heo 2361b3104104SLai Jiangshan rescuer->pool = NULL; 2362493a1724STejun Heo spin_unlock(&pool->lock); 2363493a1724STejun Heo spin_lock(&workqueue_lock); 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 2366493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 2367493a1724STejun Heo 2368111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2369111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2370e22bee78STejun Heo schedule(); 2371e22bee78STejun Heo goto repeat; 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds 2374fc2e4d70SOleg Nesterov struct wq_barrier { 2375fc2e4d70SOleg Nesterov struct work_struct work; 2376fc2e4d70SOleg Nesterov struct completion done; 2377fc2e4d70SOleg Nesterov }; 2378fc2e4d70SOleg Nesterov 2379fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2380fc2e4d70SOleg Nesterov { 2381fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2382fc2e4d70SOleg Nesterov complete(&barr->done); 2383fc2e4d70SOleg Nesterov } 2384fc2e4d70SOleg Nesterov 23854690c4abSTejun Heo /** 23864690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2387112202d9STejun Heo * @pwq: pwq to insert barrier into 23884690c4abSTejun Heo * @barr: wq_barrier to insert 2389affee4b2STejun Heo * @target: target work to attach @barr to 2390affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23914690c4abSTejun Heo * 2392affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2393affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2394affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2395affee4b2STejun Heo * cpu. 2396affee4b2STejun Heo * 2397affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2398affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2399affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2400affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2401affee4b2STejun Heo * after a work with LINKED flag set. 2402affee4b2STejun Heo * 2403affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2404112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 24054690c4abSTejun Heo * 24064690c4abSTejun Heo * CONTEXT: 2407d565ed63STejun Heo * spin_lock_irq(pool->lock). 24084690c4abSTejun Heo */ 2409112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2410affee4b2STejun Heo struct wq_barrier *barr, 2411affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2412fc2e4d70SOleg Nesterov { 2413affee4b2STejun Heo struct list_head *head; 2414affee4b2STejun Heo unsigned int linked = 0; 2415affee4b2STejun Heo 2416dc186ad7SThomas Gleixner /* 2417d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2418dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2419dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2420dc186ad7SThomas Gleixner * might deadlock. 2421dc186ad7SThomas Gleixner */ 2422ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 242322df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2424fc2e4d70SOleg Nesterov init_completion(&barr->done); 242583c22520SOleg Nesterov 2426affee4b2STejun Heo /* 2427affee4b2STejun Heo * If @target is currently being executed, schedule the 2428affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2429affee4b2STejun Heo */ 2430affee4b2STejun Heo if (worker) 2431affee4b2STejun Heo head = worker->scheduled.next; 2432affee4b2STejun Heo else { 2433affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2434affee4b2STejun Heo 2435affee4b2STejun Heo head = target->entry.next; 2436affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2437affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2438affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2439affee4b2STejun Heo } 2440affee4b2STejun Heo 2441dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2442112202d9STejun Heo insert_work(pwq, &barr->work, head, 2443affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2444fc2e4d70SOleg Nesterov } 2445fc2e4d70SOleg Nesterov 244673f53c4aSTejun Heo /** 2447112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 244873f53c4aSTejun Heo * @wq: workqueue being flushed 244973f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 245073f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 245173f53c4aSTejun Heo * 2452112202d9STejun Heo * Prepare pwqs for workqueue flushing. 245373f53c4aSTejun Heo * 2454112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2455112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2456112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2457112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2458112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 245973f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 246073f53c4aSTejun Heo * 246173f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 246273f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 246373f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 246473f53c4aSTejun Heo * is returned. 246573f53c4aSTejun Heo * 2466112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 246773f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 246873f53c4aSTejun Heo * advanced to @work_color. 246973f53c4aSTejun Heo * 247073f53c4aSTejun Heo * CONTEXT: 247173f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 247273f53c4aSTejun Heo * 247373f53c4aSTejun Heo * RETURNS: 247473f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 247573f53c4aSTejun Heo * otherwise. 247673f53c4aSTejun Heo */ 2477112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 247873f53c4aSTejun Heo int flush_color, int work_color) 24791da177e4SLinus Torvalds { 248073f53c4aSTejun Heo bool wait = false; 248149e3cf44STejun Heo struct pool_workqueue *pwq; 24821da177e4SLinus Torvalds 248373f53c4aSTejun Heo if (flush_color >= 0) { 24846183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2485112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2486dc186ad7SThomas Gleixner } 248714441960SOleg Nesterov 248876af4d93STejun Heo local_irq_disable(); 248976af4d93STejun Heo 249049e3cf44STejun Heo for_each_pwq(pwq, wq) { 2491112202d9STejun Heo struct worker_pool *pool = pwq->pool; 24921da177e4SLinus Torvalds 249376af4d93STejun Heo spin_lock(&pool->lock); 249473f53c4aSTejun Heo 249573f53c4aSTejun Heo if (flush_color >= 0) { 24966183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 249773f53c4aSTejun Heo 2498112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2499112202d9STejun Heo pwq->flush_color = flush_color; 2500112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 250173f53c4aSTejun Heo wait = true; 25021da177e4SLinus Torvalds } 250373f53c4aSTejun Heo } 250473f53c4aSTejun Heo 250573f53c4aSTejun Heo if (work_color >= 0) { 25066183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2507112202d9STejun Heo pwq->work_color = work_color; 250873f53c4aSTejun Heo } 250973f53c4aSTejun Heo 251076af4d93STejun Heo spin_unlock(&pool->lock); 25111da177e4SLinus Torvalds } 25121da177e4SLinus Torvalds 251376af4d93STejun Heo local_irq_enable(); 251476af4d93STejun Heo 2515112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 251673f53c4aSTejun Heo complete(&wq->first_flusher->done); 251773f53c4aSTejun Heo 251873f53c4aSTejun Heo return wait; 251983c22520SOleg Nesterov } 25201da177e4SLinus Torvalds 25210fcb78c2SRolf Eike Beer /** 25221da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25230fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25241da177e4SLinus Torvalds * 25251da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 25261da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 25271da177e4SLinus Torvalds * 2528fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2529fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 25301da177e4SLinus Torvalds */ 25317ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25321da177e4SLinus Torvalds { 253373f53c4aSTejun Heo struct wq_flusher this_flusher = { 253473f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 253573f53c4aSTejun Heo .flush_color = -1, 253673f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 253773f53c4aSTejun Heo }; 253873f53c4aSTejun Heo int next_color; 2539b1f4ec17SOleg Nesterov 25403295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 25413295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 254273f53c4aSTejun Heo 254373f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 254473f53c4aSTejun Heo 254573f53c4aSTejun Heo /* 254673f53c4aSTejun Heo * Start-to-wait phase 254773f53c4aSTejun Heo */ 254873f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 254973f53c4aSTejun Heo 255073f53c4aSTejun Heo if (next_color != wq->flush_color) { 255173f53c4aSTejun Heo /* 255273f53c4aSTejun Heo * Color space is not full. The current work_color 255373f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 255473f53c4aSTejun Heo * by one. 255573f53c4aSTejun Heo */ 25566183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 255773f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 255873f53c4aSTejun Heo wq->work_color = next_color; 255973f53c4aSTejun Heo 256073f53c4aSTejun Heo if (!wq->first_flusher) { 256173f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 25626183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 256373f53c4aSTejun Heo 256473f53c4aSTejun Heo wq->first_flusher = &this_flusher; 256573f53c4aSTejun Heo 2566112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 256773f53c4aSTejun Heo wq->work_color)) { 256873f53c4aSTejun Heo /* nothing to flush, done */ 256973f53c4aSTejun Heo wq->flush_color = next_color; 257073f53c4aSTejun Heo wq->first_flusher = NULL; 257173f53c4aSTejun Heo goto out_unlock; 257273f53c4aSTejun Heo } 257373f53c4aSTejun Heo } else { 257473f53c4aSTejun Heo /* wait in queue */ 25756183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 257673f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2577112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 257873f53c4aSTejun Heo } 257973f53c4aSTejun Heo } else { 258073f53c4aSTejun Heo /* 258173f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 258273f53c4aSTejun Heo * The next flush completion will assign us 258373f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 258473f53c4aSTejun Heo */ 258573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 258673f53c4aSTejun Heo } 258773f53c4aSTejun Heo 258873f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 258973f53c4aSTejun Heo 259073f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 259173f53c4aSTejun Heo 259273f53c4aSTejun Heo /* 259373f53c4aSTejun Heo * Wake-up-and-cascade phase 259473f53c4aSTejun Heo * 259573f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 259673f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 259773f53c4aSTejun Heo */ 259873f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 259973f53c4aSTejun Heo return; 260073f53c4aSTejun Heo 260173f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 260273f53c4aSTejun Heo 26034ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26044ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26054ce48b37STejun Heo goto out_unlock; 26064ce48b37STejun Heo 260773f53c4aSTejun Heo wq->first_flusher = NULL; 260873f53c4aSTejun Heo 26096183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 26106183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 261173f53c4aSTejun Heo 261273f53c4aSTejun Heo while (true) { 261373f53c4aSTejun Heo struct wq_flusher *next, *tmp; 261473f53c4aSTejun Heo 261573f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 261673f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 261773f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 261873f53c4aSTejun Heo break; 261973f53c4aSTejun Heo list_del_init(&next->list); 262073f53c4aSTejun Heo complete(&next->done); 262173f53c4aSTejun Heo } 262273f53c4aSTejun Heo 26236183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 262473f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 262573f53c4aSTejun Heo 262673f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 262773f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 262873f53c4aSTejun Heo 262973f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 263073f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 263173f53c4aSTejun Heo /* 263273f53c4aSTejun Heo * Assign the same color to all overflowed 263373f53c4aSTejun Heo * flushers, advance work_color and append to 263473f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 263573f53c4aSTejun Heo * phase for these overflowed flushers. 263673f53c4aSTejun Heo */ 263773f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 263873f53c4aSTejun Heo tmp->flush_color = wq->work_color; 263973f53c4aSTejun Heo 264073f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 264173f53c4aSTejun Heo 264273f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 264373f53c4aSTejun Heo &wq->flusher_queue); 2644112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 264573f53c4aSTejun Heo } 264673f53c4aSTejun Heo 264773f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 26486183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 264973f53c4aSTejun Heo break; 265073f53c4aSTejun Heo } 265173f53c4aSTejun Heo 265273f53c4aSTejun Heo /* 265373f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2654112202d9STejun Heo * the new first flusher and arm pwqs. 265573f53c4aSTejun Heo */ 26566183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 26576183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 265873f53c4aSTejun Heo 265973f53c4aSTejun Heo list_del_init(&next->list); 266073f53c4aSTejun Heo wq->first_flusher = next; 266173f53c4aSTejun Heo 2662112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 266373f53c4aSTejun Heo break; 266473f53c4aSTejun Heo 266573f53c4aSTejun Heo /* 266673f53c4aSTejun Heo * Meh... this color is already done, clear first 266773f53c4aSTejun Heo * flusher and repeat cascading. 266873f53c4aSTejun Heo */ 266973f53c4aSTejun Heo wq->first_flusher = NULL; 267073f53c4aSTejun Heo } 267173f53c4aSTejun Heo 267273f53c4aSTejun Heo out_unlock: 267373f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 26741da177e4SLinus Torvalds } 2675ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26761da177e4SLinus Torvalds 26779c5a2ba7STejun Heo /** 26789c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26799c5a2ba7STejun Heo * @wq: workqueue to drain 26809c5a2ba7STejun Heo * 26819c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26829c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26839c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26849c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26859c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26869c5a2ba7STejun Heo * takes too long. 26879c5a2ba7STejun Heo */ 26889c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26899c5a2ba7STejun Heo { 26909c5a2ba7STejun Heo unsigned int flush_cnt = 0; 269149e3cf44STejun Heo struct pool_workqueue *pwq; 26929c5a2ba7STejun Heo 26939c5a2ba7STejun Heo /* 26949c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26959c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 26969c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 26979c5a2ba7STejun Heo */ 2698e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 26999c5a2ba7STejun Heo if (!wq->nr_drainers++) 27009c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 2701e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 27029c5a2ba7STejun Heo reflush: 27039c5a2ba7STejun Heo flush_workqueue(wq); 27049c5a2ba7STejun Heo 270576af4d93STejun Heo local_irq_disable(); 270676af4d93STejun Heo 270749e3cf44STejun Heo for_each_pwq(pwq, wq) { 2708fa2563e4SThomas Tuttle bool drained; 27099c5a2ba7STejun Heo 271076af4d93STejun Heo spin_lock(&pwq->pool->lock); 2711112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 271276af4d93STejun Heo spin_unlock(&pwq->pool->lock); 2713fa2563e4SThomas Tuttle 2714fa2563e4SThomas Tuttle if (drained) 27159c5a2ba7STejun Heo continue; 27169c5a2ba7STejun Heo 27179c5a2ba7STejun Heo if (++flush_cnt == 10 || 27189c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2719044c782cSValentin Ilie pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", 27209c5a2ba7STejun Heo wq->name, flush_cnt); 272176af4d93STejun Heo 272276af4d93STejun Heo local_irq_enable(); 27239c5a2ba7STejun Heo goto reflush; 27249c5a2ba7STejun Heo } 27259c5a2ba7STejun Heo 272676af4d93STejun Heo spin_lock(&workqueue_lock); 27279c5a2ba7STejun Heo if (!--wq->nr_drainers) 27289c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 272976af4d93STejun Heo spin_unlock(&workqueue_lock); 273076af4d93STejun Heo 273176af4d93STejun Heo local_irq_enable(); 27329c5a2ba7STejun Heo } 27339c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27349c5a2ba7STejun Heo 2735606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2736baf59022STejun Heo { 2737baf59022STejun Heo struct worker *worker = NULL; 2738c9e7cf27STejun Heo struct worker_pool *pool; 2739112202d9STejun Heo struct pool_workqueue *pwq; 2740baf59022STejun Heo 2741baf59022STejun Heo might_sleep(); 2742baf59022STejun Heo 2743fa1b54e6STejun Heo local_irq_disable(); 2744fa1b54e6STejun Heo pool = get_work_pool(work); 2745fa1b54e6STejun Heo if (!pool) { 2746fa1b54e6STejun Heo local_irq_enable(); 2747fa1b54e6STejun Heo return false; 2748fa1b54e6STejun Heo } 2749fa1b54e6STejun Heo 2750fa1b54e6STejun Heo spin_lock(&pool->lock); 27510b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2752112202d9STejun Heo pwq = get_work_pwq(work); 2753112202d9STejun Heo if (pwq) { 2754112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2755baf59022STejun Heo goto already_gone; 2756606a5020STejun Heo } else { 2757c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2758baf59022STejun Heo if (!worker) 2759baf59022STejun Heo goto already_gone; 2760112202d9STejun Heo pwq = worker->current_pwq; 2761606a5020STejun Heo } 2762baf59022STejun Heo 2763112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2764d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2765baf59022STejun Heo 2766e159489bSTejun Heo /* 2767e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2768e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2769e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2770e159489bSTejun Heo * access. 2771e159489bSTejun Heo */ 2772112202d9STejun Heo if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER) 2773112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2774e159489bSTejun Heo else 2775112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 2776112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2777e159489bSTejun Heo 2778baf59022STejun Heo return true; 2779baf59022STejun Heo already_gone: 2780d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2781baf59022STejun Heo return false; 2782baf59022STejun Heo } 2783baf59022STejun Heo 2784db700897SOleg Nesterov /** 2785401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2786401a8d04STejun Heo * @work: the work to flush 2787db700897SOleg Nesterov * 2788606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2789606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2790401a8d04STejun Heo * 2791401a8d04STejun Heo * RETURNS: 2792401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2793401a8d04STejun Heo * %false if it was already idle. 2794db700897SOleg Nesterov */ 2795401a8d04STejun Heo bool flush_work(struct work_struct *work) 2796db700897SOleg Nesterov { 2797db700897SOleg Nesterov struct wq_barrier barr; 2798db700897SOleg Nesterov 27990976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 28000976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 28010976dfc1SStephen Boyd 2802606a5020STejun Heo if (start_flush_work(work, &barr)) { 2803db700897SOleg Nesterov wait_for_completion(&barr.done); 2804dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2805401a8d04STejun Heo return true; 2806606a5020STejun Heo } else { 2807401a8d04STejun Heo return false; 2808db700897SOleg Nesterov } 2809606a5020STejun Heo } 2810db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2811db700897SOleg Nesterov 281236e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2813401a8d04STejun Heo { 2814bbb68dfaSTejun Heo unsigned long flags; 28151f1f642eSOleg Nesterov int ret; 28161f1f642eSOleg Nesterov 28171f1f642eSOleg Nesterov do { 2818bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2819bbb68dfaSTejun Heo /* 2820bbb68dfaSTejun Heo * If someone else is canceling, wait for the same event it 2821bbb68dfaSTejun Heo * would be waiting for before retrying. 2822bbb68dfaSTejun Heo */ 2823bbb68dfaSTejun Heo if (unlikely(ret == -ENOENT)) 2824606a5020STejun Heo flush_work(work); 28251f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28261f1f642eSOleg Nesterov 2827bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2828bbb68dfaSTejun Heo mark_work_canceling(work); 2829bbb68dfaSTejun Heo local_irq_restore(flags); 2830bbb68dfaSTejun Heo 2831606a5020STejun Heo flush_work(work); 28327a22ad75STejun Heo clear_work_data(work); 28331f1f642eSOleg Nesterov return ret; 28341f1f642eSOleg Nesterov } 28351f1f642eSOleg Nesterov 28366e84d644SOleg Nesterov /** 2837401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2838401a8d04STejun Heo * @work: the work to cancel 28396e84d644SOleg Nesterov * 2840401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2841401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2842401a8d04STejun Heo * another workqueue. On return from this function, @work is 2843401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28441f1f642eSOleg Nesterov * 2845401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2846401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28476e84d644SOleg Nesterov * 2848401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28496e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2850401a8d04STejun Heo * 2851401a8d04STejun Heo * RETURNS: 2852401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28536e84d644SOleg Nesterov */ 2854401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28556e84d644SOleg Nesterov { 285636e227d2STejun Heo return __cancel_work_timer(work, false); 2857b89deed3SOleg Nesterov } 285828e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2859b89deed3SOleg Nesterov 28606e84d644SOleg Nesterov /** 2861401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2862401a8d04STejun Heo * @dwork: the delayed work to flush 28636e84d644SOleg Nesterov * 2864401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2865401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2866401a8d04STejun Heo * considers the last queueing instance of @dwork. 28671f1f642eSOleg Nesterov * 2868401a8d04STejun Heo * RETURNS: 2869401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2870401a8d04STejun Heo * %false if it was already idle. 28716e84d644SOleg Nesterov */ 2872401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2873401a8d04STejun Heo { 28748930cabaSTejun Heo local_irq_disable(); 2875401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 287660c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 28778930cabaSTejun Heo local_irq_enable(); 2878401a8d04STejun Heo return flush_work(&dwork->work); 2879401a8d04STejun Heo } 2880401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2881401a8d04STejun Heo 2882401a8d04STejun Heo /** 288357b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 288457b30ae7STejun Heo * @dwork: delayed_work to cancel 288509383498STejun Heo * 288657b30ae7STejun Heo * Kill off a pending delayed_work. Returns %true if @dwork was pending 288757b30ae7STejun Heo * and canceled; %false if wasn't pending. Note that the work callback 288857b30ae7STejun Heo * function may still be running on return, unless it returns %true and the 288957b30ae7STejun Heo * work doesn't re-arm itself. Explicitly flush or use 289057b30ae7STejun Heo * cancel_delayed_work_sync() to wait on it. 289109383498STejun Heo * 289257b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 289309383498STejun Heo */ 289457b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 289509383498STejun Heo { 289657b30ae7STejun Heo unsigned long flags; 289757b30ae7STejun Heo int ret; 289857b30ae7STejun Heo 289957b30ae7STejun Heo do { 290057b30ae7STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 290157b30ae7STejun Heo } while (unlikely(ret == -EAGAIN)); 290257b30ae7STejun Heo 290357b30ae7STejun Heo if (unlikely(ret < 0)) 290457b30ae7STejun Heo return false; 290557b30ae7STejun Heo 29067c3eed5cSTejun Heo set_work_pool_and_clear_pending(&dwork->work, 29077c3eed5cSTejun Heo get_work_pool_id(&dwork->work)); 290857b30ae7STejun Heo local_irq_restore(flags); 2909c0158ca6SDan Magenheimer return ret; 291009383498STejun Heo } 291157b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 291209383498STejun Heo 291309383498STejun Heo /** 2914401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2915401a8d04STejun Heo * @dwork: the delayed work cancel 2916401a8d04STejun Heo * 2917401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2918401a8d04STejun Heo * 2919401a8d04STejun Heo * RETURNS: 2920401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2921401a8d04STejun Heo */ 2922401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29236e84d644SOleg Nesterov { 292436e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29256e84d644SOleg Nesterov } 2926f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29271da177e4SLinus Torvalds 29280fcb78c2SRolf Eike Beer /** 2929c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2930c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2931c1a220e7SZhang Rui * @work: job to be done 2932c1a220e7SZhang Rui * 2933c1a220e7SZhang Rui * This puts a job on a specific cpu 2934c1a220e7SZhang Rui */ 2935d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 2936c1a220e7SZhang Rui { 2937d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2938c1a220e7SZhang Rui } 2939c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2940c1a220e7SZhang Rui 29410fcb78c2SRolf Eike Beer /** 2942ae90dd5dSDave Jones * schedule_work - put work task in global workqueue 29431da177e4SLinus Torvalds * @work: job to be done 29441da177e4SLinus Torvalds * 2945d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2946d4283e93STejun Heo * %true otherwise. 294752bad64dSDavid Howells * 29480fcb78c2SRolf Eike Beer * This puts a job in the kernel-global workqueue if it was not already 29490fcb78c2SRolf Eike Beer * queued and leaves it in the same position on the kernel-global 29500fcb78c2SRolf Eike Beer * workqueue otherwise. 29511da177e4SLinus Torvalds */ 2952d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29531da177e4SLinus Torvalds { 29540fcb78c2SRolf Eike Beer return queue_work(system_wq, work); 29551da177e4SLinus Torvalds } 29560fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_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 29610fcb78c2SRolf Eike Beer * @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 */ 2967d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2968d4283e93STejun Heo 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 /** 29750a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29760a13c00eSTejun Heo * @dwork: job to be done 29770a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29780a13c00eSTejun Heo * 29790a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29800a13c00eSTejun Heo * workqueue. 29810a13c00eSTejun Heo */ 2982d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29830a13c00eSTejun Heo { 29840a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29850a13c00eSTejun Heo } 29860a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29870a13c00eSTejun Heo 29880a13c00eSTejun Heo /** 298931ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2990b6136773SAndrew Morton * @func: the function to call 2991b6136773SAndrew Morton * 299231ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 299331ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2994b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 299531ddd871STejun Heo * 299631ddd871STejun Heo * RETURNS: 299731ddd871STejun Heo * 0 on success, -errno on failure. 2998b6136773SAndrew Morton */ 299965f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 300015316ba8SChristoph Lameter { 300115316ba8SChristoph Lameter int cpu; 300238f51568SNamhyung Kim struct work_struct __percpu *works; 300315316ba8SChristoph Lameter 3004b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3005b6136773SAndrew Morton if (!works) 300615316ba8SChristoph Lameter return -ENOMEM; 3007b6136773SAndrew Morton 300895402b38SGautham R Shenoy get_online_cpus(); 300993981800STejun Heo 301015316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30119bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30129bfb1839SIngo Molnar 30139bfb1839SIngo Molnar INIT_WORK(work, func); 30148de6d308SOleg Nesterov schedule_work_on(cpu, work); 301515316ba8SChristoph Lameter } 301693981800STejun Heo 301793981800STejun Heo for_each_online_cpu(cpu) 30188616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 301993981800STejun Heo 302095402b38SGautham R Shenoy put_online_cpus(); 3021b6136773SAndrew Morton free_percpu(works); 302215316ba8SChristoph Lameter return 0; 302315316ba8SChristoph Lameter } 302415316ba8SChristoph Lameter 3025eef6a7d5SAlan Stern /** 3026eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3027eef6a7d5SAlan Stern * 3028eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3029eef6a7d5SAlan Stern * completion. 3030eef6a7d5SAlan Stern * 3031eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3032eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3033eef6a7d5SAlan Stern * will lead to deadlock: 3034eef6a7d5SAlan Stern * 3035eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3036eef6a7d5SAlan Stern * a lock held by your code or its caller. 3037eef6a7d5SAlan Stern * 3038eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3039eef6a7d5SAlan Stern * 3040eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3041eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3042eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3043eef6a7d5SAlan Stern * 3044eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3045eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3046eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3047eef6a7d5SAlan Stern * cancel_work_sync() instead. 3048eef6a7d5SAlan Stern */ 30491da177e4SLinus Torvalds void flush_scheduled_work(void) 30501da177e4SLinus Torvalds { 3051d320c038STejun Heo flush_workqueue(system_wq); 30521da177e4SLinus Torvalds } 3053ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30541da177e4SLinus Torvalds 30551da177e4SLinus Torvalds /** 30561fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30571fa44ecaSJames Bottomley * @fn: the function to execute 30581fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30591fa44ecaSJames Bottomley * be available when the work executes) 30601fa44ecaSJames Bottomley * 30611fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30621fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30631fa44ecaSJames Bottomley * 30641fa44ecaSJames Bottomley * Returns: 0 - function was executed 30651fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30661fa44ecaSJames Bottomley */ 306765f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30681fa44ecaSJames Bottomley { 30691fa44ecaSJames Bottomley if (!in_interrupt()) { 307065f27f38SDavid Howells fn(&ew->work); 30711fa44ecaSJames Bottomley return 0; 30721fa44ecaSJames Bottomley } 30731fa44ecaSJames Bottomley 307465f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30751fa44ecaSJames Bottomley schedule_work(&ew->work); 30761fa44ecaSJames Bottomley 30771fa44ecaSJames Bottomley return 1; 30781fa44ecaSJames Bottomley } 30791fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30801fa44ecaSJames Bottomley 30811da177e4SLinus Torvalds int keventd_up(void) 30821da177e4SLinus Torvalds { 3083d320c038STejun Heo return system_wq != NULL; 30841da177e4SLinus Torvalds } 30851da177e4SLinus Torvalds 30867a4e344cSTejun Heo /** 30877a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 30887a4e344cSTejun Heo * @attrs: workqueue_attrs to free 30897a4e344cSTejun Heo * 30907a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 30917a4e344cSTejun Heo */ 30927a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs) 30937a4e344cSTejun Heo { 30947a4e344cSTejun Heo if (attrs) { 30957a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 30967a4e344cSTejun Heo kfree(attrs); 30977a4e344cSTejun Heo } 30987a4e344cSTejun Heo } 30997a4e344cSTejun Heo 31007a4e344cSTejun Heo /** 31017a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 31027a4e344cSTejun Heo * @gfp_mask: allocation mask to use 31037a4e344cSTejun Heo * 31047a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 31057a4e344cSTejun Heo * return it. Returns NULL on failure. 31067a4e344cSTejun Heo */ 31077a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) 31087a4e344cSTejun Heo { 31097a4e344cSTejun Heo struct workqueue_attrs *attrs; 31107a4e344cSTejun Heo 31117a4e344cSTejun Heo attrs = kzalloc(sizeof(*attrs), gfp_mask); 31127a4e344cSTejun Heo if (!attrs) 31137a4e344cSTejun Heo goto fail; 31147a4e344cSTejun Heo if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) 31157a4e344cSTejun Heo goto fail; 31167a4e344cSTejun Heo 31177a4e344cSTejun Heo cpumask_setall(attrs->cpumask); 31187a4e344cSTejun Heo return attrs; 31197a4e344cSTejun Heo fail: 31207a4e344cSTejun Heo free_workqueue_attrs(attrs); 31217a4e344cSTejun Heo return NULL; 31227a4e344cSTejun Heo } 31237a4e344cSTejun Heo 312429c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 312529c91e99STejun Heo const struct workqueue_attrs *from) 312629c91e99STejun Heo { 312729c91e99STejun Heo to->nice = from->nice; 312829c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 312929c91e99STejun Heo } 313029c91e99STejun Heo 313129c91e99STejun Heo /* 313229c91e99STejun Heo * Hacky implementation of jhash of bitmaps which only considers the 313329c91e99STejun Heo * specified number of bits. We probably want a proper implementation in 313429c91e99STejun Heo * include/linux/jhash.h. 313529c91e99STejun Heo */ 313629c91e99STejun Heo static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash) 313729c91e99STejun Heo { 313829c91e99STejun Heo int nr_longs = bits / BITS_PER_LONG; 313929c91e99STejun Heo int nr_leftover = bits % BITS_PER_LONG; 314029c91e99STejun Heo unsigned long leftover = 0; 314129c91e99STejun Heo 314229c91e99STejun Heo if (nr_longs) 314329c91e99STejun Heo hash = jhash(bitmap, nr_longs * sizeof(long), hash); 314429c91e99STejun Heo if (nr_leftover) { 314529c91e99STejun Heo bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover); 314629c91e99STejun Heo hash = jhash(&leftover, sizeof(long), hash); 314729c91e99STejun Heo } 314829c91e99STejun Heo return hash; 314929c91e99STejun Heo } 315029c91e99STejun Heo 315129c91e99STejun Heo /* hash value of the content of @attr */ 315229c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 315329c91e99STejun Heo { 315429c91e99STejun Heo u32 hash = 0; 315529c91e99STejun Heo 315629c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 315729c91e99STejun Heo hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash); 315829c91e99STejun Heo return hash; 315929c91e99STejun Heo } 316029c91e99STejun Heo 316129c91e99STejun Heo /* content equality test */ 316229c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 316329c91e99STejun Heo const struct workqueue_attrs *b) 316429c91e99STejun Heo { 316529c91e99STejun Heo if (a->nice != b->nice) 316629c91e99STejun Heo return false; 316729c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 316829c91e99STejun Heo return false; 316929c91e99STejun Heo return true; 317029c91e99STejun Heo } 317129c91e99STejun Heo 31727a4e344cSTejun Heo /** 31737a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 31747a4e344cSTejun Heo * @pool: worker_pool to initialize 31757a4e344cSTejun Heo * 31767a4e344cSTejun Heo * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs. 317729c91e99STejun Heo * Returns 0 on success, -errno on failure. Even on failure, all fields 317829c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 317929c91e99STejun Heo * on @pool safely to release it. 31807a4e344cSTejun Heo */ 31817a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 31824e1a1f9aSTejun Heo { 31834e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 318429c91e99STejun Heo pool->id = -1; 318529c91e99STejun Heo pool->cpu = -1; 31864e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 31874e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 31884e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 31894e1a1f9aSTejun Heo hash_init(pool->busy_hash); 31904e1a1f9aSTejun Heo 31914e1a1f9aSTejun Heo init_timer_deferrable(&pool->idle_timer); 31924e1a1f9aSTejun Heo pool->idle_timer.function = idle_worker_timeout; 31934e1a1f9aSTejun Heo pool->idle_timer.data = (unsigned long)pool; 31944e1a1f9aSTejun Heo 31954e1a1f9aSTejun Heo setup_timer(&pool->mayday_timer, pool_mayday_timeout, 31964e1a1f9aSTejun Heo (unsigned long)pool); 31974e1a1f9aSTejun Heo 31984e1a1f9aSTejun Heo mutex_init(&pool->manager_arb); 31994e1a1f9aSTejun Heo mutex_init(&pool->assoc_mutex); 32004e1a1f9aSTejun Heo ida_init(&pool->worker_ida); 32017a4e344cSTejun Heo 320229c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 320329c91e99STejun Heo pool->refcnt = 1; 320429c91e99STejun Heo 320529c91e99STejun Heo /* shouldn't fail above this point */ 32067a4e344cSTejun Heo pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); 32077a4e344cSTejun Heo if (!pool->attrs) 32087a4e344cSTejun Heo return -ENOMEM; 32097a4e344cSTejun Heo return 0; 32104e1a1f9aSTejun Heo } 32114e1a1f9aSTejun Heo 321229c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 321329c91e99STejun Heo { 321429c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 321529c91e99STejun Heo 321629c91e99STejun Heo ida_destroy(&pool->worker_ida); 321729c91e99STejun Heo free_workqueue_attrs(pool->attrs); 321829c91e99STejun Heo kfree(pool); 321929c91e99STejun Heo } 322029c91e99STejun Heo 322129c91e99STejun Heo /** 322229c91e99STejun Heo * put_unbound_pool - put a worker_pool 322329c91e99STejun Heo * @pool: worker_pool to put 322429c91e99STejun Heo * 322529c91e99STejun Heo * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU 322629c91e99STejun Heo * safe manner. 322729c91e99STejun Heo */ 322829c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 322929c91e99STejun Heo { 323029c91e99STejun Heo struct worker *worker; 323129c91e99STejun Heo 323229c91e99STejun Heo spin_lock_irq(&workqueue_lock); 323329c91e99STejun Heo if (--pool->refcnt) { 323429c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 323529c91e99STejun Heo return; 323629c91e99STejun Heo } 323729c91e99STejun Heo 323829c91e99STejun Heo /* sanity checks */ 323929c91e99STejun Heo if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) || 324029c91e99STejun Heo WARN_ON(!list_empty(&pool->worklist))) { 324129c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 324229c91e99STejun Heo return; 324329c91e99STejun Heo } 324429c91e99STejun Heo 324529c91e99STejun Heo /* release id and unhash */ 324629c91e99STejun Heo if (pool->id >= 0) 324729c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 324829c91e99STejun Heo hash_del(&pool->hash_node); 324929c91e99STejun Heo 325029c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 325129c91e99STejun Heo 325229c91e99STejun Heo /* lock out manager and destroy all workers */ 325329c91e99STejun Heo mutex_lock(&pool->manager_arb); 325429c91e99STejun Heo spin_lock_irq(&pool->lock); 325529c91e99STejun Heo 325629c91e99STejun Heo while ((worker = first_worker(pool))) 325729c91e99STejun Heo destroy_worker(worker); 325829c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 325929c91e99STejun Heo 326029c91e99STejun Heo spin_unlock_irq(&pool->lock); 326129c91e99STejun Heo mutex_unlock(&pool->manager_arb); 326229c91e99STejun Heo 326329c91e99STejun Heo /* shut down the timers */ 326429c91e99STejun Heo del_timer_sync(&pool->idle_timer); 326529c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 326629c91e99STejun Heo 326729c91e99STejun Heo /* sched-RCU protected to allow dereferences from get_work_pool() */ 326829c91e99STejun Heo call_rcu_sched(&pool->rcu, rcu_free_pool); 326929c91e99STejun Heo } 327029c91e99STejun Heo 327129c91e99STejun Heo /** 327229c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 327329c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 327429c91e99STejun Heo * 327529c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 327629c91e99STejun Heo * reference count and return it. If there already is a matching 327729c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 327829c91e99STejun Heo * create a new one. On failure, returns NULL. 327929c91e99STejun Heo */ 328029c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 328129c91e99STejun Heo { 328229c91e99STejun Heo static DEFINE_MUTEX(create_mutex); 328329c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 328429c91e99STejun Heo struct worker_pool *pool; 328529c91e99STejun Heo struct worker *worker; 328629c91e99STejun Heo 328729c91e99STejun Heo mutex_lock(&create_mutex); 328829c91e99STejun Heo 328929c91e99STejun Heo /* do we already have a matching pool? */ 329029c91e99STejun Heo spin_lock_irq(&workqueue_lock); 329129c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 329229c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 329329c91e99STejun Heo pool->refcnt++; 329429c91e99STejun Heo goto out_unlock; 329529c91e99STejun Heo } 329629c91e99STejun Heo } 329729c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 329829c91e99STejun Heo 329929c91e99STejun Heo /* nope, create a new one */ 330029c91e99STejun Heo pool = kzalloc(sizeof(*pool), GFP_KERNEL); 330129c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 330229c91e99STejun Heo goto fail; 330329c91e99STejun Heo 330429c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 330529c91e99STejun Heo 330629c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 330729c91e99STejun Heo goto fail; 330829c91e99STejun Heo 330929c91e99STejun Heo /* create and start the initial worker */ 331029c91e99STejun Heo worker = create_worker(pool); 331129c91e99STejun Heo if (!worker) 331229c91e99STejun Heo goto fail; 331329c91e99STejun Heo 331429c91e99STejun Heo spin_lock_irq(&pool->lock); 331529c91e99STejun Heo start_worker(worker); 331629c91e99STejun Heo spin_unlock_irq(&pool->lock); 331729c91e99STejun Heo 331829c91e99STejun Heo /* install */ 331929c91e99STejun Heo spin_lock_irq(&workqueue_lock); 332029c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 332129c91e99STejun Heo out_unlock: 332229c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 332329c91e99STejun Heo mutex_unlock(&create_mutex); 332429c91e99STejun Heo return pool; 332529c91e99STejun Heo fail: 332629c91e99STejun Heo mutex_unlock(&create_mutex); 332729c91e99STejun Heo if (pool) 332829c91e99STejun Heo put_unbound_pool(pool); 332929c91e99STejun Heo return NULL; 333029c91e99STejun Heo } 333129c91e99STejun Heo 333230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 33331da177e4SLinus Torvalds { 333449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 333530cdf249STejun Heo int cpu; 3336e1d8aa9fSFrederic Weisbecker 333730cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3338420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3339420c0ddbSTejun Heo if (!wq->cpu_pwqs) 334030cdf249STejun Heo return -ENOMEM; 334130cdf249STejun Heo 334230cdf249STejun Heo for_each_possible_cpu(cpu) { 33437fb98ea7STejun Heo struct pool_workqueue *pwq = 33447fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 33457a62c2c8STejun Heo struct worker_pool *cpu_pools = 3346f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 334730cdf249STejun Heo 33487a62c2c8STejun Heo pwq->pool = &cpu_pools[highpri]; 334976af4d93STejun Heo list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 335030cdf249STejun Heo } 335130cdf249STejun Heo } else { 335230cdf249STejun Heo struct pool_workqueue *pwq; 335330cdf249STejun Heo 335430cdf249STejun Heo pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL); 335530cdf249STejun Heo if (!pwq) 335630cdf249STejun Heo return -ENOMEM; 335730cdf249STejun Heo 335829c91e99STejun Heo pwq->pool = get_unbound_pool(unbound_std_wq_attrs[highpri]); 335929c91e99STejun Heo if (!pwq->pool) { 336029c91e99STejun Heo kmem_cache_free(pwq_cache, pwq); 336129c91e99STejun Heo return -ENOMEM; 336229c91e99STejun Heo } 336329c91e99STejun Heo 336476af4d93STejun Heo list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 336530cdf249STejun Heo } 336630cdf249STejun Heo 336730cdf249STejun Heo return 0; 33680f900049STejun Heo } 33690f900049STejun Heo 3370112202d9STejun Heo static void free_pwqs(struct workqueue_struct *wq) 337106ba38a9SOleg Nesterov { 3372e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3373420c0ddbSTejun Heo free_percpu(wq->cpu_pwqs); 3374420c0ddbSTejun Heo else if (!list_empty(&wq->pwqs)) 3375420c0ddbSTejun Heo kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs, 3376420c0ddbSTejun Heo struct pool_workqueue, pwqs_node)); 337706ba38a9SOleg Nesterov } 337806ba38a9SOleg Nesterov 3379f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3380f3421797STejun Heo const char *name) 3381b71ab8c2STejun Heo { 3382f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3383f3421797STejun Heo 3384f3421797STejun Heo if (max_active < 1 || max_active > lim) 3385044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3386f3421797STejun Heo max_active, name, 1, lim); 3387b71ab8c2STejun Heo 3388f3421797STejun Heo return clamp_val(max_active, 1, lim); 3389b71ab8c2STejun Heo } 3390b71ab8c2STejun Heo 3391b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 339297e37d7bSTejun Heo unsigned int flags, 33931e19ffc6STejun Heo int max_active, 3394eb13ba87SJohannes Berg struct lock_class_key *key, 3395b196be89STejun Heo const char *lock_name, ...) 33963af24433SOleg Nesterov { 3397b196be89STejun Heo va_list args, args1; 33983af24433SOleg Nesterov struct workqueue_struct *wq; 339949e3cf44STejun Heo struct pool_workqueue *pwq; 3400b196be89STejun Heo size_t namelen; 3401b196be89STejun Heo 3402b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3403b196be89STejun Heo va_start(args, lock_name); 3404b196be89STejun Heo va_copy(args1, args); 3405b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3406b196be89STejun Heo 3407b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3408b196be89STejun Heo if (!wq) 3409b196be89STejun Heo goto err; 3410b196be89STejun Heo 3411b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3412b196be89STejun Heo va_end(args); 3413b196be89STejun Heo va_end(args1); 34143af24433SOleg Nesterov 3415f3421797STejun Heo /* 34166370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 34176370a6adSTejun Heo * have a rescuer to guarantee forward progress. 34186370a6adSTejun Heo */ 34196370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 34206370a6adSTejun Heo flags |= WQ_RESCUER; 34216370a6adSTejun Heo 3422d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3423b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 34243af24433SOleg Nesterov 3425b196be89STejun Heo /* init wq */ 342697e37d7bSTejun Heo wq->flags = flags; 3427a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 342873f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 3429112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 343030cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 343173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 343273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 3433493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 34343af24433SOleg Nesterov 3435eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3436cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 34373af24433SOleg Nesterov 343830cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 3439bdbc5dd7STejun Heo goto err; 3440bdbc5dd7STejun Heo 344176af4d93STejun Heo local_irq_disable(); 344249e3cf44STejun Heo for_each_pwq(pwq, wq) { 3443112202d9STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3444112202d9STejun Heo pwq->wq = wq; 3445112202d9STejun Heo pwq->flush_color = -1; 3446112202d9STejun Heo pwq->max_active = max_active; 3447112202d9STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 3448493a1724STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 3449e22bee78STejun Heo } 345076af4d93STejun Heo local_irq_enable(); 34511537663fSTejun Heo 3452e22bee78STejun Heo if (flags & WQ_RESCUER) { 3453e22bee78STejun Heo struct worker *rescuer; 3454e22bee78STejun Heo 3455e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3456e22bee78STejun Heo if (!rescuer) 3457e22bee78STejun Heo goto err; 3458e22bee78STejun Heo 3459111c225aSTejun Heo rescuer->rescue_wq = wq; 3460111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 3461b196be89STejun Heo wq->name); 3462e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3463e22bee78STejun Heo goto err; 3464e22bee78STejun Heo 3465e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3466e22bee78STejun Heo wake_up_process(rescuer->task); 34673af24433SOleg Nesterov } 34681537663fSTejun Heo 34693af24433SOleg Nesterov /* 3470a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3471a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3472a0a1a5fdSTejun Heo * workqueue to workqueues list. 34733af24433SOleg Nesterov */ 3474e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3475a0a1a5fdSTejun Heo 347658a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 347749e3cf44STejun Heo for_each_pwq(pwq, wq) 347849e3cf44STejun Heo pwq->max_active = 0; 3479a0a1a5fdSTejun Heo 34803af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3481a0a1a5fdSTejun Heo 3482e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 34833af24433SOleg Nesterov 34843af24433SOleg Nesterov return wq; 34854690c4abSTejun Heo err: 34864690c4abSTejun Heo if (wq) { 3487112202d9STejun Heo free_pwqs(wq); 3488e22bee78STejun Heo kfree(wq->rescuer); 34894690c4abSTejun Heo kfree(wq); 34903af24433SOleg Nesterov } 34914690c4abSTejun Heo return NULL; 34921da177e4SLinus Torvalds } 3493d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 34941da177e4SLinus Torvalds 34953af24433SOleg Nesterov /** 34963af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 34973af24433SOleg Nesterov * @wq: target workqueue 34983af24433SOleg Nesterov * 34993af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 35003af24433SOleg Nesterov */ 35013af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 35023af24433SOleg Nesterov { 350349e3cf44STejun Heo struct pool_workqueue *pwq; 35043af24433SOleg Nesterov 35059c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 35069c5a2ba7STejun Heo drain_workqueue(wq); 3507c8efcc25STejun Heo 350876af4d93STejun Heo spin_lock_irq(&workqueue_lock); 350976af4d93STejun Heo 35106183c009STejun Heo /* sanity checks */ 351149e3cf44STejun Heo for_each_pwq(pwq, wq) { 35126183c009STejun Heo int i; 35136183c009STejun Heo 351476af4d93STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) { 351576af4d93STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) { 351676af4d93STejun Heo spin_unlock_irq(&workqueue_lock); 35176183c009STejun Heo return; 351876af4d93STejun Heo } 351976af4d93STejun Heo } 352076af4d93STejun Heo 35216183c009STejun Heo if (WARN_ON(pwq->nr_active) || 352276af4d93STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) { 352376af4d93STejun Heo spin_unlock_irq(&workqueue_lock); 35246183c009STejun Heo return; 35256183c009STejun Heo } 352676af4d93STejun Heo } 35276183c009STejun Heo 3528a0a1a5fdSTejun Heo /* 3529a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3530a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3531a0a1a5fdSTejun Heo */ 35323af24433SOleg Nesterov list_del(&wq->list); 353376af4d93STejun Heo 3534e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 35353af24433SOleg Nesterov 3536e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3537e22bee78STejun Heo kthread_stop(wq->rescuer->task); 35388d9df9f0SXiaotian Feng kfree(wq->rescuer); 3539e22bee78STejun Heo } 3540e22bee78STejun Heo 354129c91e99STejun Heo /* 354229c91e99STejun Heo * We're the sole accessor of @wq at this point. Directly access 354329c91e99STejun Heo * the first pwq and put its pool. 354429c91e99STejun Heo */ 354529c91e99STejun Heo if (wq->flags & WQ_UNBOUND) { 354629c91e99STejun Heo pwq = list_first_entry(&wq->pwqs, struct pool_workqueue, 354729c91e99STejun Heo pwqs_node); 354829c91e99STejun Heo put_unbound_pool(pwq->pool); 354929c91e99STejun Heo } 3550112202d9STejun Heo free_pwqs(wq); 35513af24433SOleg Nesterov kfree(wq); 35523af24433SOleg Nesterov } 35533af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 35543af24433SOleg Nesterov 3555dcd989cbSTejun Heo /** 3556112202d9STejun Heo * pwq_set_max_active - adjust max_active of a pwq 3557112202d9STejun Heo * @pwq: target pool_workqueue 35589f4bd4cdSLai Jiangshan * @max_active: new max_active value. 35599f4bd4cdSLai Jiangshan * 3560112202d9STejun Heo * Set @pwq->max_active to @max_active and activate delayed works if 35619f4bd4cdSLai Jiangshan * increased. 35629f4bd4cdSLai Jiangshan * 35639f4bd4cdSLai Jiangshan * CONTEXT: 3564d565ed63STejun Heo * spin_lock_irq(pool->lock). 35659f4bd4cdSLai Jiangshan */ 3566112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active) 35679f4bd4cdSLai Jiangshan { 3568112202d9STejun Heo pwq->max_active = max_active; 35699f4bd4cdSLai Jiangshan 3570112202d9STejun Heo while (!list_empty(&pwq->delayed_works) && 3571112202d9STejun Heo pwq->nr_active < pwq->max_active) 3572112202d9STejun Heo pwq_activate_first_delayed(pwq); 35739f4bd4cdSLai Jiangshan } 35749f4bd4cdSLai Jiangshan 35759f4bd4cdSLai Jiangshan /** 3576dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3577dcd989cbSTejun Heo * @wq: target workqueue 3578dcd989cbSTejun Heo * @max_active: new max_active value. 3579dcd989cbSTejun Heo * 3580dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3581dcd989cbSTejun Heo * 3582dcd989cbSTejun Heo * CONTEXT: 3583dcd989cbSTejun Heo * Don't call from IRQ context. 3584dcd989cbSTejun Heo */ 3585dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3586dcd989cbSTejun Heo { 358749e3cf44STejun Heo struct pool_workqueue *pwq; 3588dcd989cbSTejun Heo 3589f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3590dcd989cbSTejun Heo 3591e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3592dcd989cbSTejun Heo 3593dcd989cbSTejun Heo wq->saved_max_active = max_active; 3594dcd989cbSTejun Heo 359549e3cf44STejun Heo for_each_pwq(pwq, wq) { 3596112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3597dcd989cbSTejun Heo 3598e98d5b16STejun Heo spin_lock(&pool->lock); 3599dcd989cbSTejun Heo 360058a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 360135b6bb63STejun Heo !(pool->flags & POOL_FREEZING)) 3602112202d9STejun Heo pwq_set_max_active(pwq, max_active); 3603dcd989cbSTejun Heo 3604e98d5b16STejun Heo spin_unlock(&pool->lock); 3605dcd989cbSTejun Heo } 3606dcd989cbSTejun Heo 3607e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3608dcd989cbSTejun Heo } 3609dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3610dcd989cbSTejun Heo 3611dcd989cbSTejun Heo /** 3612dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3613dcd989cbSTejun Heo * @cpu: CPU in question 3614dcd989cbSTejun Heo * @wq: target workqueue 3615dcd989cbSTejun Heo * 3616dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3617dcd989cbSTejun Heo * no synchronization around this function and the test result is 3618dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3619dcd989cbSTejun Heo * 3620dcd989cbSTejun Heo * RETURNS: 3621dcd989cbSTejun Heo * %true if congested, %false otherwise. 3622dcd989cbSTejun Heo */ 3623d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 3624dcd989cbSTejun Heo { 36257fb98ea7STejun Heo struct pool_workqueue *pwq; 362676af4d93STejun Heo bool ret; 362776af4d93STejun Heo 362876af4d93STejun Heo preempt_disable(); 36297fb98ea7STejun Heo 36307fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 36317fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 36327fb98ea7STejun Heo else 36337fb98ea7STejun Heo pwq = first_pwq(wq); 3634dcd989cbSTejun Heo 363576af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 363676af4d93STejun Heo preempt_enable(); 363776af4d93STejun Heo 363876af4d93STejun Heo return ret; 3639dcd989cbSTejun Heo } 3640dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3641dcd989cbSTejun Heo 3642dcd989cbSTejun Heo /** 3643dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3644dcd989cbSTejun Heo * @work: the work to be tested 3645dcd989cbSTejun Heo * 3646dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3647dcd989cbSTejun Heo * synchronization around this function and the test result is 3648dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3649dcd989cbSTejun Heo * 3650dcd989cbSTejun Heo * RETURNS: 3651dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3652dcd989cbSTejun Heo */ 3653dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3654dcd989cbSTejun Heo { 3655fa1b54e6STejun Heo struct worker_pool *pool; 3656dcd989cbSTejun Heo unsigned long flags; 3657dcd989cbSTejun Heo unsigned int ret = 0; 3658dcd989cbSTejun Heo 3659dcd989cbSTejun Heo if (work_pending(work)) 3660dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3661038366c5SLai Jiangshan 3662fa1b54e6STejun Heo local_irq_save(flags); 3663fa1b54e6STejun Heo pool = get_work_pool(work); 3664038366c5SLai Jiangshan if (pool) { 3665fa1b54e6STejun Heo spin_lock(&pool->lock); 3666c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 3667dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3668fa1b54e6STejun Heo spin_unlock(&pool->lock); 3669038366c5SLai Jiangshan } 3670fa1b54e6STejun Heo local_irq_restore(flags); 3671dcd989cbSTejun Heo 3672dcd989cbSTejun Heo return ret; 3673dcd989cbSTejun Heo } 3674dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3675dcd989cbSTejun Heo 3676db7bccf4STejun Heo /* 3677db7bccf4STejun Heo * CPU hotplug. 3678db7bccf4STejun Heo * 3679e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3680112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 3681706026c2STejun Heo * pool which make migrating pending and scheduled works very 3682e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 368394cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 3684e22bee78STejun Heo * blocked draining impractical. 3685e22bee78STejun Heo * 368624647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 3687628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3688628c78e7STejun Heo * cpu comes back online. 3689db7bccf4STejun Heo */ 3690db7bccf4STejun Heo 3691706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 3692db7bccf4STejun Heo { 369338db41d9STejun Heo int cpu = smp_processor_id(); 36944ce62e9eSTejun Heo struct worker_pool *pool; 3695db7bccf4STejun Heo struct worker *worker; 3696db7bccf4STejun Heo int i; 3697db7bccf4STejun Heo 3698f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 36996183c009STejun Heo WARN_ON_ONCE(cpu != smp_processor_id()); 3700db7bccf4STejun Heo 370194cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 370294cf58bbSTejun Heo spin_lock_irq(&pool->lock); 3703e22bee78STejun Heo 3704f2d5a0eeSTejun Heo /* 370594cf58bbSTejun Heo * We've claimed all manager positions. Make all workers 370694cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 370794cf58bbSTejun Heo * except for the ones which are still executing works from 370894cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 370994cf58bbSTejun Heo * this, they may become diasporas. 3710f2d5a0eeSTejun Heo */ 37114ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3712403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3713db7bccf4STejun Heo 3714b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) 3715403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3716db7bccf4STejun Heo 371724647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 3718f2d5a0eeSTejun Heo 371994cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 372094cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 372194cf58bbSTejun Heo } 3722e22bee78STejun Heo 3723e22bee78STejun Heo /* 3724628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3725628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3726628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3727628c78e7STejun Heo */ 3728628c78e7STejun Heo schedule(); 3729628c78e7STejun Heo 3730628c78e7STejun Heo /* 3731628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3732628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 373338db41d9STejun Heo * are always true as long as the worklist is not empty. Pools on 373438db41d9STejun Heo * @cpu now behave as unbound (in terms of concurrency management) 373538db41d9STejun Heo * pools which are served by workers tied to the CPU. 3736628c78e7STejun Heo * 3737628c78e7STejun Heo * On return from this function, the current worker would trigger 3738628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3739628c78e7STejun Heo * didn't already. 3740e22bee78STejun Heo */ 3741f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) 3742e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 3743db7bccf4STejun Heo } 3744db7bccf4STejun Heo 37458db25e78STejun Heo /* 37468db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 37478db25e78STejun Heo * This will be registered high priority CPU notifier. 37488db25e78STejun Heo */ 37499fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb, 37501da177e4SLinus Torvalds unsigned long action, 37511da177e4SLinus Torvalds void *hcpu) 37521da177e4SLinus Torvalds { 3753d84ff051STejun Heo int cpu = (unsigned long)hcpu; 37544ce62e9eSTejun Heo struct worker_pool *pool; 37551da177e4SLinus Torvalds 37568db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 37573af24433SOleg Nesterov case CPU_UP_PREPARE: 3758f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 37593ce63377STejun Heo struct worker *worker; 37603ce63377STejun Heo 37613ce63377STejun Heo if (pool->nr_workers) 37623ce63377STejun Heo continue; 37633ce63377STejun Heo 37643ce63377STejun Heo worker = create_worker(pool); 37653ce63377STejun Heo if (!worker) 37663ce63377STejun Heo return NOTIFY_BAD; 37673ce63377STejun Heo 3768d565ed63STejun Heo spin_lock_irq(&pool->lock); 37693ce63377STejun Heo start_worker(worker); 3770d565ed63STejun Heo spin_unlock_irq(&pool->lock); 37713af24433SOleg Nesterov } 37721da177e4SLinus Torvalds break; 37731da177e4SLinus Torvalds 377465758202STejun Heo case CPU_DOWN_FAILED: 377565758202STejun Heo case CPU_ONLINE: 3776f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 377794cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 377894cf58bbSTejun Heo spin_lock_irq(&pool->lock); 377994cf58bbSTejun Heo 378024647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 378194cf58bbSTejun Heo rebind_workers(pool); 378294cf58bbSTejun Heo 378394cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 378494cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 378594cf58bbSTejun Heo } 37868db25e78STejun Heo break; 378765758202STejun Heo } 378865758202STejun Heo return NOTIFY_OK; 378965758202STejun Heo } 379065758202STejun Heo 379165758202STejun Heo /* 379265758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 379365758202STejun Heo * This will be registered as low priority CPU notifier. 379465758202STejun Heo */ 37959fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb, 379665758202STejun Heo unsigned long action, 379765758202STejun Heo void *hcpu) 379865758202STejun Heo { 3799d84ff051STejun Heo int cpu = (unsigned long)hcpu; 38008db25e78STejun Heo struct work_struct unbind_work; 38018db25e78STejun Heo 380265758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 380365758202STejun Heo case CPU_DOWN_PREPARE: 38048db25e78STejun Heo /* unbinding should happen on the local CPU */ 3805706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 38067635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 38078db25e78STejun Heo flush_work(&unbind_work); 38088db25e78STejun Heo break; 380965758202STejun Heo } 381065758202STejun Heo return NOTIFY_OK; 381165758202STejun Heo } 381265758202STejun Heo 38132d3854a3SRusty Russell #ifdef CONFIG_SMP 38148ccad40dSRusty Russell 38152d3854a3SRusty Russell struct work_for_cpu { 3816ed48ece2STejun Heo struct work_struct work; 38172d3854a3SRusty Russell long (*fn)(void *); 38182d3854a3SRusty Russell void *arg; 38192d3854a3SRusty Russell long ret; 38202d3854a3SRusty Russell }; 38212d3854a3SRusty Russell 3822ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 38232d3854a3SRusty Russell { 3824ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 3825ed48ece2STejun Heo 38262d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 38272d3854a3SRusty Russell } 38282d3854a3SRusty Russell 38292d3854a3SRusty Russell /** 38302d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 38312d3854a3SRusty Russell * @cpu: the cpu to run on 38322d3854a3SRusty Russell * @fn: the function to run 38332d3854a3SRusty Russell * @arg: the function arg 38342d3854a3SRusty Russell * 383531ad9081SRusty Russell * This will return the value @fn returns. 383631ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 38376b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 38382d3854a3SRusty Russell */ 3839d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 38402d3854a3SRusty Russell { 3841ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 38422d3854a3SRusty Russell 3843ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 3844ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 3845ed48ece2STejun Heo flush_work(&wfc.work); 38462d3854a3SRusty Russell return wfc.ret; 38472d3854a3SRusty Russell } 38482d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 38492d3854a3SRusty Russell #endif /* CONFIG_SMP */ 38502d3854a3SRusty Russell 3851a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3852e7577c50SRusty Russell 3853a0a1a5fdSTejun Heo /** 3854a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3855a0a1a5fdSTejun Heo * 385658a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 385758a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 3858706026c2STejun Heo * pool->worklist. 3859a0a1a5fdSTejun Heo * 3860a0a1a5fdSTejun Heo * CONTEXT: 3861d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3862a0a1a5fdSTejun Heo */ 3863a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3864a0a1a5fdSTejun Heo { 386517116969STejun Heo struct worker_pool *pool; 386624b8a847STejun Heo struct workqueue_struct *wq; 386724b8a847STejun Heo struct pool_workqueue *pwq; 386817116969STejun Heo int id; 3869a0a1a5fdSTejun Heo 3870e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3871a0a1a5fdSTejun Heo 38726183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 3873a0a1a5fdSTejun Heo workqueue_freezing = true; 3874a0a1a5fdSTejun Heo 387524b8a847STejun Heo /* set FREEZING */ 387617116969STejun Heo for_each_pool(pool, id) { 3877e98d5b16STejun Heo spin_lock(&pool->lock); 387835b6bb63STejun Heo WARN_ON_ONCE(pool->flags & POOL_FREEZING); 387935b6bb63STejun Heo pool->flags |= POOL_FREEZING; 388024b8a847STejun Heo spin_unlock(&pool->lock); 38811da177e4SLinus Torvalds } 38828b03ae3cSTejun Heo 388324b8a847STejun Heo /* suppress further executions by setting max_active to zero */ 388424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 388524b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 388624b8a847STejun Heo continue; 388724b8a847STejun Heo 388824b8a847STejun Heo for_each_pwq(pwq, wq) { 388924b8a847STejun Heo spin_lock(&pwq->pool->lock); 389024b8a847STejun Heo pwq->max_active = 0; 389124b8a847STejun Heo spin_unlock(&pwq->pool->lock); 389224b8a847STejun Heo } 3893a1056305STejun Heo } 3894a0a1a5fdSTejun Heo 3895e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3896a0a1a5fdSTejun Heo } 3897a0a1a5fdSTejun Heo 3898a0a1a5fdSTejun Heo /** 389958a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3900a0a1a5fdSTejun Heo * 3901a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3902a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3903a0a1a5fdSTejun Heo * 3904a0a1a5fdSTejun Heo * CONTEXT: 3905a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3906a0a1a5fdSTejun Heo * 3907a0a1a5fdSTejun Heo * RETURNS: 390858a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 390958a69cb4STejun Heo * is complete. 3910a0a1a5fdSTejun Heo */ 3911a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3912a0a1a5fdSTejun Heo { 3913a0a1a5fdSTejun Heo bool busy = false; 391424b8a847STejun Heo struct workqueue_struct *wq; 391524b8a847STejun Heo struct pool_workqueue *pwq; 3916a0a1a5fdSTejun Heo 3917e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3918a0a1a5fdSTejun Heo 39196183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 3920a0a1a5fdSTejun Heo 392124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 392224b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 392324b8a847STejun Heo continue; 3924a0a1a5fdSTejun Heo /* 3925a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3926a0a1a5fdSTejun Heo * to peek without lock. 3927a0a1a5fdSTejun Heo */ 392824b8a847STejun Heo for_each_pwq(pwq, wq) { 39296183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 3930112202d9STejun Heo if (pwq->nr_active) { 3931a0a1a5fdSTejun Heo busy = true; 3932a0a1a5fdSTejun Heo goto out_unlock; 3933a0a1a5fdSTejun Heo } 3934a0a1a5fdSTejun Heo } 3935a0a1a5fdSTejun Heo } 3936a0a1a5fdSTejun Heo out_unlock: 3937e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3938a0a1a5fdSTejun Heo return busy; 3939a0a1a5fdSTejun Heo } 3940a0a1a5fdSTejun Heo 3941a0a1a5fdSTejun Heo /** 3942a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3943a0a1a5fdSTejun Heo * 3944a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 3945706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 3946a0a1a5fdSTejun Heo * 3947a0a1a5fdSTejun Heo * CONTEXT: 3948d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3949a0a1a5fdSTejun Heo */ 3950a0a1a5fdSTejun Heo void thaw_workqueues(void) 3951a0a1a5fdSTejun Heo { 395224b8a847STejun Heo struct workqueue_struct *wq; 395324b8a847STejun Heo struct pool_workqueue *pwq; 395424b8a847STejun Heo struct worker_pool *pool; 395524b8a847STejun Heo int id; 3956a0a1a5fdSTejun Heo 3957e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3958a0a1a5fdSTejun Heo 3959a0a1a5fdSTejun Heo if (!workqueue_freezing) 3960a0a1a5fdSTejun Heo goto out_unlock; 3961a0a1a5fdSTejun Heo 396224b8a847STejun Heo /* clear FREEZING */ 396324b8a847STejun Heo for_each_pool(pool, id) { 3964e98d5b16STejun Heo spin_lock(&pool->lock); 396535b6bb63STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); 396635b6bb63STejun Heo pool->flags &= ~POOL_FREEZING; 3967e98d5b16STejun Heo spin_unlock(&pool->lock); 3968d565ed63STejun Heo } 396924b8a847STejun Heo 397024b8a847STejun Heo /* restore max_active and repopulate worklist */ 397124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 397224b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 397324b8a847STejun Heo continue; 397424b8a847STejun Heo 397524b8a847STejun Heo for_each_pwq(pwq, wq) { 397624b8a847STejun Heo spin_lock(&pwq->pool->lock); 397724b8a847STejun Heo pwq_set_max_active(pwq, wq->saved_max_active); 397824b8a847STejun Heo spin_unlock(&pwq->pool->lock); 397924b8a847STejun Heo } 398024b8a847STejun Heo } 398124b8a847STejun Heo 398224b8a847STejun Heo /* kick workers */ 398324b8a847STejun Heo for_each_pool(pool, id) { 398424b8a847STejun Heo spin_lock(&pool->lock); 398524b8a847STejun Heo wake_up_worker(pool); 398624b8a847STejun Heo spin_unlock(&pool->lock); 3987a0a1a5fdSTejun Heo } 3988a0a1a5fdSTejun Heo 3989a0a1a5fdSTejun Heo workqueue_freezing = false; 3990a0a1a5fdSTejun Heo out_unlock: 3991e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3992a0a1a5fdSTejun Heo } 3993a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3994a0a1a5fdSTejun Heo 39956ee0578bSSuresh Siddha static int __init init_workqueues(void) 39961da177e4SLinus Torvalds { 39977a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 39987a4e344cSTejun Heo int i, cpu; 3999c34056a3STejun Heo 40007c3eed5cSTejun Heo /* make sure we have enough bits for OFFQ pool ID */ 40017c3eed5cSTejun Heo BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < 40026be19588SLai Jiangshan WORK_CPU_END * NR_STD_WORKER_POOLS); 4003b5490077STejun Heo 4004e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 4005e904e6c2STejun Heo 4006e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 4007e904e6c2STejun Heo 400865758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 4009a5b4e57dSLai Jiangshan hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 40108b03ae3cSTejun Heo 4011706026c2STejun Heo /* initialize CPU pools */ 401229c91e99STejun Heo for_each_possible_cpu(cpu) { 40134ce62e9eSTejun Heo struct worker_pool *pool; 40148b03ae3cSTejun Heo 40157a4e344cSTejun Heo i = 0; 4016f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 40177a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 4018ec22ca5eSTejun Heo pool->cpu = cpu; 40197a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 40207a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 40217a4e344cSTejun Heo 40229daf9e67STejun Heo /* alloc pool ID */ 40239daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 40244ce62e9eSTejun Heo } 40258b03ae3cSTejun Heo } 40268b03ae3cSTejun Heo 4027e22bee78STejun Heo /* create the initial worker */ 402829c91e99STejun Heo for_each_online_cpu(cpu) { 40294ce62e9eSTejun Heo struct worker_pool *pool; 4030e22bee78STejun Heo 4031f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 40324ce62e9eSTejun Heo struct worker *worker; 40334ce62e9eSTejun Heo 403424647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 403524647570STejun Heo 4036bc2ae0f5STejun Heo worker = create_worker(pool); 4037e22bee78STejun Heo BUG_ON(!worker); 4038d565ed63STejun Heo spin_lock_irq(&pool->lock); 4039e22bee78STejun Heo start_worker(worker); 4040d565ed63STejun Heo spin_unlock_irq(&pool->lock); 4041e22bee78STejun Heo } 40424ce62e9eSTejun Heo } 4043e22bee78STejun Heo 404429c91e99STejun Heo /* create default unbound wq attrs */ 404529c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 404629c91e99STejun Heo struct workqueue_attrs *attrs; 404729c91e99STejun Heo 404829c91e99STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 404929c91e99STejun Heo 405029c91e99STejun Heo attrs->nice = std_nice[i]; 405129c91e99STejun Heo cpumask_setall(attrs->cpumask); 405229c91e99STejun Heo 405329c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 405429c91e99STejun Heo } 405529c91e99STejun Heo 4056d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 40571aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 4058d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 4059f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 4060f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 406124d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 406224d51addSTejun Heo WQ_FREEZABLE, 0); 40631aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 4064ae930e0fSTejun Heo !system_unbound_wq || !system_freezable_wq); 40656ee0578bSSuresh Siddha return 0; 40661da177e4SLinus Torvalds } 40676ee0578bSSuresh Siddha early_initcall(init_workqueues); 4068