11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <dwmw2@infradead.org> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 101da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <tj@kernel.org> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19c54fce6eSTejun Heo * automatically managed. There is one worker pool for each CPU and 20c54fce6eSTejun Heo * one extra for works which are better served by workers which are 21c54fce6eSTejun Heo * not bound to any specific CPU. 22c54fce6eSTejun Heo * 23c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 241da177e4SLinus Torvalds */ 251da177e4SLinus Torvalds 269984de1aSPaul Gortmaker #include <linux/export.h> 271da177e4SLinus Torvalds #include <linux/kernel.h> 281da177e4SLinus Torvalds #include <linux/sched.h> 291da177e4SLinus Torvalds #include <linux/init.h> 301da177e4SLinus Torvalds #include <linux/signal.h> 311da177e4SLinus Torvalds #include <linux/completion.h> 321da177e4SLinus Torvalds #include <linux/workqueue.h> 331da177e4SLinus Torvalds #include <linux/slab.h> 341da177e4SLinus Torvalds #include <linux/cpu.h> 351da177e4SLinus Torvalds #include <linux/notifier.h> 361da177e4SLinus Torvalds #include <linux/kthread.h> 371fa44ecaSJames Bottomley #include <linux/hardirq.h> 3846934023SChristoph Lameter #include <linux/mempolicy.h> 39341a5958SRafael J. Wysocki #include <linux/freezer.h> 40d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 41d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 424e6045f1SJohannes Berg #include <linux/lockdep.h> 43c34056a3STejun Heo #include <linux/idr.h> 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, 17103270476aSTejun Heo "kworker/u:%d%s", id, pri); 1711c34056a3STejun Heo if (IS_ERR(worker->task)) 1712c34056a3STejun Heo goto fail; 1713c34056a3STejun Heo 17147a4e344cSTejun Heo set_user_nice(worker->task, pool->attrs->nice); 17157a4e344cSTejun Heo set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 17163270476aSTejun Heo 1717db7bccf4STejun Heo /* 17187a4e344cSTejun Heo * %PF_THREAD_BOUND is used to prevent userland from meddling with 17197a4e344cSTejun Heo * cpumask of workqueue workers. This is an abuse. We need 17207a4e344cSTejun Heo * %PF_NO_SETAFFINITY. 1721db7bccf4STejun Heo */ 1722db7bccf4STejun Heo worker->task->flags |= PF_THREAD_BOUND; 17237a4e344cSTejun Heo 17247a4e344cSTejun Heo /* 17257a4e344cSTejun Heo * The caller is responsible for ensuring %POOL_DISASSOCIATED 17267a4e344cSTejun Heo * remains stable across this function. See the comments above the 17277a4e344cSTejun Heo * flag definition for details. 17287a4e344cSTejun Heo */ 17297a4e344cSTejun Heo if (pool->flags & POOL_DISASSOCIATED) 1730f3421797STejun Heo worker->flags |= WORKER_UNBOUND; 1731c34056a3STejun Heo 1732c34056a3STejun Heo return worker; 1733c34056a3STejun Heo fail: 1734c34056a3STejun Heo if (id >= 0) { 1735d565ed63STejun Heo spin_lock_irq(&pool->lock); 1736bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1737d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1738c34056a3STejun Heo } 1739c34056a3STejun Heo kfree(worker); 1740c34056a3STejun Heo return NULL; 1741c34056a3STejun Heo } 1742c34056a3STejun Heo 1743c34056a3STejun Heo /** 1744c34056a3STejun Heo * start_worker - start a newly created worker 1745c34056a3STejun Heo * @worker: worker to start 1746c34056a3STejun Heo * 1747706026c2STejun Heo * Make the pool aware of @worker and start it. 1748c34056a3STejun Heo * 1749c34056a3STejun Heo * CONTEXT: 1750d565ed63STejun Heo * spin_lock_irq(pool->lock). 1751c34056a3STejun Heo */ 1752c34056a3STejun Heo static void start_worker(struct worker *worker) 1753c34056a3STejun Heo { 1754cb444766STejun Heo worker->flags |= WORKER_STARTED; 1755bd7bdd43STejun Heo worker->pool->nr_workers++; 1756c8e55f36STejun Heo worker_enter_idle(worker); 1757c34056a3STejun Heo wake_up_process(worker->task); 1758c34056a3STejun Heo } 1759c34056a3STejun Heo 1760c34056a3STejun Heo /** 1761c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1762c34056a3STejun Heo * @worker: worker to be destroyed 1763c34056a3STejun Heo * 1764706026c2STejun Heo * Destroy @worker and adjust @pool stats accordingly. 1765c8e55f36STejun Heo * 1766c8e55f36STejun Heo * CONTEXT: 1767d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 1768c34056a3STejun Heo */ 1769c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1770c34056a3STejun Heo { 1771bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1772c34056a3STejun Heo int id = worker->id; 1773c34056a3STejun Heo 1774c34056a3STejun Heo /* sanity check frenzy */ 17756183c009STejun Heo if (WARN_ON(worker->current_work) || 17766183c009STejun Heo WARN_ON(!list_empty(&worker->scheduled))) 17776183c009STejun Heo return; 1778c34056a3STejun Heo 1779c8e55f36STejun Heo if (worker->flags & WORKER_STARTED) 1780bd7bdd43STejun Heo pool->nr_workers--; 1781c8e55f36STejun Heo if (worker->flags & WORKER_IDLE) 1782bd7bdd43STejun Heo pool->nr_idle--; 1783c8e55f36STejun Heo 1784c8e55f36STejun Heo list_del_init(&worker->entry); 1785cb444766STejun Heo worker->flags |= WORKER_DIE; 1786c8e55f36STejun Heo 1787d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1788c8e55f36STejun Heo 1789c34056a3STejun Heo kthread_stop(worker->task); 1790c34056a3STejun Heo kfree(worker); 1791c34056a3STejun Heo 1792d565ed63STejun Heo spin_lock_irq(&pool->lock); 1793bd7bdd43STejun Heo ida_remove(&pool->worker_ida, id); 1794c34056a3STejun Heo } 1795c34056a3STejun Heo 179663d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1797e22bee78STejun Heo { 179863d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1799e22bee78STejun Heo 1800d565ed63STejun Heo spin_lock_irq(&pool->lock); 1801e22bee78STejun Heo 180263d95a91STejun Heo if (too_many_workers(pool)) { 1803e22bee78STejun Heo struct worker *worker; 1804e22bee78STejun Heo unsigned long expires; 1805e22bee78STejun Heo 1806e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 180763d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1808e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1809e22bee78STejun Heo 1810e22bee78STejun Heo if (time_before(jiffies, expires)) 181163d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1812e22bee78STejun Heo else { 1813e22bee78STejun Heo /* it's been idle for too long, wake up manager */ 181411ebea50STejun Heo pool->flags |= POOL_MANAGE_WORKERS; 181563d95a91STejun Heo wake_up_worker(pool); 1816e22bee78STejun Heo } 1817e22bee78STejun Heo } 1818e22bee78STejun Heo 1819d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1820e22bee78STejun Heo } 1821e22bee78STejun Heo 1822493a1724STejun Heo static void send_mayday(struct work_struct *work) 1823e22bee78STejun Heo { 1824112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1825112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1826493a1724STejun Heo 1827493a1724STejun Heo lockdep_assert_held(&workqueue_lock); 1828e22bee78STejun Heo 1829e22bee78STejun Heo if (!(wq->flags & WQ_RESCUER)) 1830493a1724STejun Heo return; 1831e22bee78STejun Heo 1832e22bee78STejun Heo /* mayday mayday mayday */ 1833493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 1834493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1835e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1836493a1724STejun Heo } 1837e22bee78STejun Heo } 1838e22bee78STejun Heo 1839706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool) 1840e22bee78STejun Heo { 184163d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1842e22bee78STejun Heo struct work_struct *work; 1843e22bee78STejun Heo 1844493a1724STejun Heo spin_lock_irq(&workqueue_lock); /* for wq->maydays */ 1845493a1724STejun Heo spin_lock(&pool->lock); 1846e22bee78STejun Heo 184763d95a91STejun Heo if (need_to_create_worker(pool)) { 1848e22bee78STejun Heo /* 1849e22bee78STejun Heo * We've been trying to create a new worker but 1850e22bee78STejun Heo * haven't been successful. We might be hitting an 1851e22bee78STejun Heo * allocation deadlock. Send distress signals to 1852e22bee78STejun Heo * rescuers. 1853e22bee78STejun Heo */ 185463d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1855e22bee78STejun Heo send_mayday(work); 1856e22bee78STejun Heo } 1857e22bee78STejun Heo 1858493a1724STejun Heo spin_unlock(&pool->lock); 1859493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 1860e22bee78STejun Heo 186163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1862e22bee78STejun Heo } 1863e22bee78STejun Heo 1864e22bee78STejun Heo /** 1865e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 186663d95a91STejun Heo * @pool: pool to create a new worker for 1867e22bee78STejun Heo * 186863d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1869e22bee78STejun Heo * have at least one idle worker on return from this function. If 1870e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 187163d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1872e22bee78STejun Heo * possible allocation deadlock. 1873e22bee78STejun Heo * 1874e22bee78STejun Heo * On return, need_to_create_worker() is guaranteed to be false and 1875e22bee78STejun Heo * may_start_working() true. 1876e22bee78STejun Heo * 1877e22bee78STejun Heo * LOCKING: 1878d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1879e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1880e22bee78STejun Heo * manager. 1881e22bee78STejun Heo * 1882e22bee78STejun Heo * RETURNS: 1883d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1884e22bee78STejun Heo * otherwise. 1885e22bee78STejun Heo */ 188663d95a91STejun Heo static bool maybe_create_worker(struct worker_pool *pool) 1887d565ed63STejun Heo __releases(&pool->lock) 1888d565ed63STejun Heo __acquires(&pool->lock) 1889e22bee78STejun Heo { 189063d95a91STejun Heo if (!need_to_create_worker(pool)) 1891e22bee78STejun Heo return false; 1892e22bee78STejun Heo restart: 1893d565ed63STejun Heo spin_unlock_irq(&pool->lock); 18949f9c2364STejun Heo 1895e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 189663d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1897e22bee78STejun Heo 1898e22bee78STejun Heo while (true) { 1899e22bee78STejun Heo struct worker *worker; 1900e22bee78STejun Heo 1901bc2ae0f5STejun Heo worker = create_worker(pool); 1902e22bee78STejun Heo if (worker) { 190363d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1904d565ed63STejun Heo spin_lock_irq(&pool->lock); 1905e22bee78STejun Heo start_worker(worker); 19066183c009STejun Heo if (WARN_ON_ONCE(need_to_create_worker(pool))) 19076183c009STejun Heo goto restart; 1908e22bee78STejun Heo return true; 1909e22bee78STejun Heo } 1910e22bee78STejun Heo 191163d95a91STejun Heo if (!need_to_create_worker(pool)) 1912e22bee78STejun Heo break; 1913e22bee78STejun Heo 1914e22bee78STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 1915e22bee78STejun Heo schedule_timeout(CREATE_COOLDOWN); 19169f9c2364STejun Heo 191763d95a91STejun Heo if (!need_to_create_worker(pool)) 1918e22bee78STejun Heo break; 1919e22bee78STejun Heo } 1920e22bee78STejun Heo 192163d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1922d565ed63STejun Heo spin_lock_irq(&pool->lock); 192363d95a91STejun Heo if (need_to_create_worker(pool)) 1924e22bee78STejun Heo goto restart; 1925e22bee78STejun Heo return true; 1926e22bee78STejun Heo } 1927e22bee78STejun Heo 1928e22bee78STejun Heo /** 1929e22bee78STejun Heo * maybe_destroy_worker - destroy workers which have been idle for a while 193063d95a91STejun Heo * @pool: pool to destroy workers for 1931e22bee78STejun Heo * 193263d95a91STejun Heo * Destroy @pool workers which have been idle for longer than 1933e22bee78STejun Heo * IDLE_WORKER_TIMEOUT. 1934e22bee78STejun Heo * 1935e22bee78STejun Heo * LOCKING: 1936d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1937e22bee78STejun Heo * multiple times. Called only from manager. 1938e22bee78STejun Heo * 1939e22bee78STejun Heo * RETURNS: 1940d565ed63STejun Heo * false if no action was taken and pool->lock stayed locked, true 1941e22bee78STejun Heo * otherwise. 1942e22bee78STejun Heo */ 194363d95a91STejun Heo static bool maybe_destroy_workers(struct worker_pool *pool) 1944e22bee78STejun Heo { 1945e22bee78STejun Heo bool ret = false; 1946e22bee78STejun Heo 194763d95a91STejun Heo while (too_many_workers(pool)) { 1948e22bee78STejun Heo struct worker *worker; 1949e22bee78STejun Heo unsigned long expires; 1950e22bee78STejun Heo 195163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1952e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1953e22bee78STejun Heo 1954e22bee78STejun Heo if (time_before(jiffies, expires)) { 195563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 1956e22bee78STejun Heo break; 1957e22bee78STejun Heo } 1958e22bee78STejun Heo 1959e22bee78STejun Heo destroy_worker(worker); 1960e22bee78STejun Heo ret = true; 1961e22bee78STejun Heo } 1962e22bee78STejun Heo 1963e22bee78STejun Heo return ret; 1964e22bee78STejun Heo } 1965e22bee78STejun Heo 1966e22bee78STejun Heo /** 1967e22bee78STejun Heo * manage_workers - manage worker pool 1968e22bee78STejun Heo * @worker: self 1969e22bee78STejun Heo * 1970706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1971e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1972706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1973e22bee78STejun Heo * 1974e22bee78STejun Heo * The caller can safely start processing works on false return. On 1975e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1976e22bee78STejun Heo * and may_start_working() is true. 1977e22bee78STejun Heo * 1978e22bee78STejun Heo * CONTEXT: 1979d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1980e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1981e22bee78STejun Heo * 1982e22bee78STejun Heo * RETURNS: 1983d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1984d565ed63STejun Heo * multiple times. Does GFP_KERNEL allocations. 1985e22bee78STejun Heo */ 1986e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1987e22bee78STejun Heo { 198863d95a91STejun Heo struct worker_pool *pool = worker->pool; 1989e22bee78STejun Heo bool ret = false; 1990e22bee78STejun Heo 199134a06bd6STejun Heo if (!mutex_trylock(&pool->manager_arb)) 1992e22bee78STejun Heo return ret; 1993e22bee78STejun Heo 1994ee378aa4SLai Jiangshan /* 1995ee378aa4SLai Jiangshan * To simplify both worker management and CPU hotplug, hold off 1996ee378aa4SLai Jiangshan * management while hotplug is in progress. CPU hotplug path can't 199734a06bd6STejun Heo * grab @pool->manager_arb to achieve this because that can lead to 199834a06bd6STejun Heo * idle worker depletion (all become busy thinking someone else is 199934a06bd6STejun Heo * managing) which in turn can result in deadlock under extreme 200034a06bd6STejun Heo * circumstances. Use @pool->assoc_mutex to synchronize manager 200134a06bd6STejun Heo * against CPU hotplug. 2002ee378aa4SLai Jiangshan * 2003b2eb83d1SLai Jiangshan * assoc_mutex would always be free unless CPU hotplug is in 2004d565ed63STejun Heo * progress. trylock first without dropping @pool->lock. 2005ee378aa4SLai Jiangshan */ 2006b2eb83d1SLai Jiangshan if (unlikely(!mutex_trylock(&pool->assoc_mutex))) { 2007d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2008b2eb83d1SLai Jiangshan mutex_lock(&pool->assoc_mutex); 2009ee378aa4SLai Jiangshan /* 2010ee378aa4SLai Jiangshan * CPU hotplug could have happened while we were waiting 2011b2eb83d1SLai Jiangshan * for assoc_mutex. Hotplug itself can't handle us 2012ee378aa4SLai Jiangshan * because manager isn't either on idle or busy list, and 2013706026c2STejun Heo * @pool's state and ours could have deviated. 2014ee378aa4SLai Jiangshan * 2015b2eb83d1SLai Jiangshan * As hotplug is now excluded via assoc_mutex, we can 2016ee378aa4SLai Jiangshan * simply try to bind. It will succeed or fail depending 2017706026c2STejun Heo * on @pool's current state. Try it and adjust 2018ee378aa4SLai Jiangshan * %WORKER_UNBOUND accordingly. 2019ee378aa4SLai Jiangshan */ 2020f36dc67bSLai Jiangshan if (worker_maybe_bind_and_lock(pool)) 2021ee378aa4SLai Jiangshan worker->flags &= ~WORKER_UNBOUND; 2022ee378aa4SLai Jiangshan else 2023ee378aa4SLai Jiangshan worker->flags |= WORKER_UNBOUND; 2024ee378aa4SLai Jiangshan 2025ee378aa4SLai Jiangshan ret = true; 2026ee378aa4SLai Jiangshan } 2027ee378aa4SLai Jiangshan 202811ebea50STejun Heo pool->flags &= ~POOL_MANAGE_WORKERS; 2029e22bee78STejun Heo 2030e22bee78STejun Heo /* 2031e22bee78STejun Heo * Destroy and then create so that may_start_working() is true 2032e22bee78STejun Heo * on return. 2033e22bee78STejun Heo */ 203463d95a91STejun Heo ret |= maybe_destroy_workers(pool); 203563d95a91STejun Heo ret |= maybe_create_worker(pool); 2036e22bee78STejun Heo 2037b2eb83d1SLai Jiangshan mutex_unlock(&pool->assoc_mutex); 203834a06bd6STejun Heo mutex_unlock(&pool->manager_arb); 2039e22bee78STejun Heo return ret; 2040e22bee78STejun Heo } 2041e22bee78STejun Heo 2042a62428c0STejun Heo /** 2043a62428c0STejun Heo * process_one_work - process single work 2044c34056a3STejun Heo * @worker: self 2045a62428c0STejun Heo * @work: work to process 2046a62428c0STejun Heo * 2047a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2048a62428c0STejun Heo * process a single work including synchronization against and 2049a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2050a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2051a62428c0STejun Heo * call this function to process a work. 2052a62428c0STejun Heo * 2053a62428c0STejun Heo * CONTEXT: 2054d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2055a62428c0STejun Heo */ 2056c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2057d565ed63STejun Heo __releases(&pool->lock) 2058d565ed63STejun Heo __acquires(&pool->lock) 20591da177e4SLinus Torvalds { 2060112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2061bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2062112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 206373f53c4aSTejun Heo int work_color; 20647e11629dSTejun Heo struct worker *collision; 20654e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20664e6045f1SJohannes Berg /* 2067a62428c0STejun Heo * It is permissible to free the struct work_struct from 2068a62428c0STejun Heo * inside the function that is called from it, this we need to 2069a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2070a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2071a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20724e6045f1SJohannes Berg */ 20734d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20744d82a1deSPeter Zijlstra 20754d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20764e6045f1SJohannes Berg #endif 20776fec10a1STejun Heo /* 20786fec10a1STejun Heo * Ensure we're on the correct CPU. DISASSOCIATED test is 20796fec10a1STejun Heo * necessary to avoid spurious warnings from rescuers servicing the 208024647570STejun Heo * unbound or a disassociated pool. 20816fec10a1STejun Heo */ 20825f7dabfdSLai Jiangshan WARN_ON_ONCE(!(worker->flags & WORKER_UNBOUND) && 208324647570STejun Heo !(pool->flags & POOL_DISASSOCIATED) && 2084ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 208525511a47STejun Heo 20867e11629dSTejun Heo /* 20877e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20887e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20897e11629dSTejun Heo * already processing the work. If so, defer the work to the 20907e11629dSTejun Heo * currently executing one. 20917e11629dSTejun Heo */ 2092c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 20937e11629dSTejun Heo if (unlikely(collision)) { 20947e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20957e11629dSTejun Heo return; 20967e11629dSTejun Heo } 20971da177e4SLinus Torvalds 20988930cabaSTejun Heo /* claim and dequeue */ 20991da177e4SLinus Torvalds debug_work_deactivate(work); 2100c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2101c34056a3STejun Heo worker->current_work = work; 2102a2c1c57bSTejun Heo worker->current_func = work->func; 2103112202d9STejun Heo worker->current_pwq = pwq; 210473f53c4aSTejun Heo work_color = get_work_color(work); 21057a22ad75STejun Heo 2106a62428c0STejun Heo list_del_init(&work->entry); 2107a62428c0STejun Heo 2108649027d7STejun Heo /* 2109fb0e7bebSTejun Heo * CPU intensive works don't participate in concurrency 2110fb0e7bebSTejun Heo * management. They're the scheduler's responsibility. 2111fb0e7bebSTejun Heo */ 2112fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2113fb0e7bebSTejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE, true); 2114fb0e7bebSTejun Heo 2115974271c4STejun Heo /* 2116d565ed63STejun Heo * Unbound pool isn't concurrency managed and work items should be 2117974271c4STejun Heo * executed ASAP. Wake up another worker if necessary. 2118974271c4STejun Heo */ 211963d95a91STejun Heo if ((worker->flags & WORKER_UNBOUND) && need_more_worker(pool)) 212063d95a91STejun Heo wake_up_worker(pool); 2121974271c4STejun Heo 21228930cabaSTejun Heo /* 21237c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2124d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 212523657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 212623657bb1STejun Heo * disabled. 21278930cabaSTejun Heo */ 21287c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 21291da177e4SLinus Torvalds 2130d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2131365970a1SDavid Howells 2132112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 21333295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2134e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2135a2c1c57bSTejun Heo worker->current_func(work); 2136e36c886aSArjan van de Ven /* 2137e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2138e36c886aSArjan van de Ven * point will only record its address. 2139e36c886aSArjan van de Ven */ 2140e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21413295f0efSIngo Molnar lock_map_release(&lockdep_map); 2142112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 21431da177e4SLinus Torvalds 2144d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2145044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2146044c782cSValentin Ilie " last function: %pf\n", 2147a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2148a2c1c57bSTejun Heo worker->current_func); 2149d5abe669SPeter Zijlstra debug_show_held_locks(current); 2150d5abe669SPeter Zijlstra dump_stack(); 2151d5abe669SPeter Zijlstra } 2152d5abe669SPeter Zijlstra 2153d565ed63STejun Heo spin_lock_irq(&pool->lock); 2154a62428c0STejun Heo 2155fb0e7bebSTejun Heo /* clear cpu intensive status */ 2156fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2157fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2158fb0e7bebSTejun Heo 2159a62428c0STejun Heo /* we're done with it, release */ 216042f8570fSSasha Levin hash_del(&worker->hentry); 2161c34056a3STejun Heo worker->current_work = NULL; 2162a2c1c57bSTejun Heo worker->current_func = NULL; 2163112202d9STejun Heo worker->current_pwq = NULL; 2164112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 21651da177e4SLinus Torvalds } 21661da177e4SLinus Torvalds 2167affee4b2STejun Heo /** 2168affee4b2STejun Heo * process_scheduled_works - process scheduled works 2169affee4b2STejun Heo * @worker: self 2170affee4b2STejun Heo * 2171affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2172affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2173affee4b2STejun Heo * fetches a work from the top and executes it. 2174affee4b2STejun Heo * 2175affee4b2STejun Heo * CONTEXT: 2176d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2177affee4b2STejun Heo * multiple times. 2178affee4b2STejun Heo */ 2179affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21801da177e4SLinus Torvalds { 2181affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2182affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2183a62428c0STejun Heo struct work_struct, entry); 2184c34056a3STejun Heo process_one_work(worker, work); 2185a62428c0STejun Heo } 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21884690c4abSTejun Heo /** 21894690c4abSTejun Heo * worker_thread - the worker thread function 2190c34056a3STejun Heo * @__worker: self 21914690c4abSTejun Heo * 2192706026c2STejun Heo * The worker thread function. There are NR_CPU_WORKER_POOLS dynamic pools 2193706026c2STejun Heo * of these per each cpu. These workers process all works regardless of 2194e22bee78STejun Heo * their specific target workqueue. The only exception is works which 2195e22bee78STejun Heo * belong to workqueues with a rescuer which will be explained in 2196e22bee78STejun Heo * rescuer_thread(). 21974690c4abSTejun Heo */ 2198c34056a3STejun Heo static int worker_thread(void *__worker) 21991da177e4SLinus Torvalds { 2200c34056a3STejun Heo struct worker *worker = __worker; 2201bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 22021da177e4SLinus Torvalds 2203e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2204e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2205c8e55f36STejun Heo woke_up: 2206d565ed63STejun Heo spin_lock_irq(&pool->lock); 2207affee4b2STejun Heo 22085f7dabfdSLai Jiangshan /* we are off idle list if destruction or rebind is requested */ 22095f7dabfdSLai Jiangshan if (unlikely(list_empty(&worker->entry))) { 2210d565ed63STejun Heo spin_unlock_irq(&pool->lock); 221125511a47STejun Heo 22125f7dabfdSLai Jiangshan /* if DIE is set, destruction is requested */ 221325511a47STejun Heo if (worker->flags & WORKER_DIE) { 2214e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 2215c8e55f36STejun Heo return 0; 2216c8e55f36STejun Heo } 2217c8e55f36STejun Heo 22185f7dabfdSLai Jiangshan /* otherwise, rebind */ 221925511a47STejun Heo idle_worker_rebind(worker); 222025511a47STejun Heo goto woke_up; 222125511a47STejun Heo } 222225511a47STejun Heo 2223c8e55f36STejun Heo worker_leave_idle(worker); 2224db7bccf4STejun Heo recheck: 2225e22bee78STejun Heo /* no more worker necessary? */ 222663d95a91STejun Heo if (!need_more_worker(pool)) 2227e22bee78STejun Heo goto sleep; 2228e22bee78STejun Heo 2229e22bee78STejun Heo /* do we need to manage? */ 223063d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2231e22bee78STejun Heo goto recheck; 2232e22bee78STejun Heo 2233c8e55f36STejun Heo /* 2234c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2235c8e55f36STejun Heo * preparing to process a work or actually processing it. 2236c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2237c8e55f36STejun Heo */ 22386183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2239c8e55f36STejun Heo 2240e22bee78STejun Heo /* 2241e22bee78STejun Heo * When control reaches this point, we're guaranteed to have 2242e22bee78STejun Heo * at least one idle worker or that someone else has already 2243e22bee78STejun Heo * assumed the manager role. 2244e22bee78STejun Heo */ 2245e22bee78STejun Heo worker_clr_flags(worker, WORKER_PREP); 2246e22bee78STejun Heo 2247e22bee78STejun Heo do { 2248affee4b2STejun Heo struct work_struct *work = 2249bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2250affee4b2STejun Heo struct work_struct, entry); 2251affee4b2STejun Heo 2252c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2253affee4b2STejun Heo /* optimization path, not strictly necessary */ 2254affee4b2STejun Heo process_one_work(worker, work); 2255affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2256affee4b2STejun Heo process_scheduled_works(worker); 2257affee4b2STejun Heo } else { 2258c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2259affee4b2STejun Heo process_scheduled_works(worker); 2260affee4b2STejun Heo } 226163d95a91STejun Heo } while (keep_working(pool)); 2262affee4b2STejun Heo 2263e22bee78STejun Heo worker_set_flags(worker, WORKER_PREP, false); 2264d313dd85STejun Heo sleep: 226563d95a91STejun Heo if (unlikely(need_to_manage_workers(pool)) && manage_workers(worker)) 2266e22bee78STejun Heo goto recheck; 2267d313dd85STejun Heo 2268c8e55f36STejun Heo /* 2269d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2270d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2271d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2272d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2273d565ed63STejun Heo * event. 2274c8e55f36STejun Heo */ 2275c8e55f36STejun Heo worker_enter_idle(worker); 2276c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 2277d565ed63STejun Heo spin_unlock_irq(&pool->lock); 22781da177e4SLinus Torvalds schedule(); 2279c8e55f36STejun Heo goto woke_up; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds 2282e22bee78STejun Heo /** 2283e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2284111c225aSTejun Heo * @__rescuer: self 2285e22bee78STejun Heo * 2286e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2287e22bee78STejun Heo * workqueue which has WQ_RESCUER set. 2288e22bee78STejun Heo * 2289706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2290e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2291e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2292e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2293e22bee78STejun Heo * the problem rescuer solves. 2294e22bee78STejun Heo * 2295706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2296706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2297e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2298e22bee78STejun Heo * 2299e22bee78STejun Heo * This should happen rarely. 2300e22bee78STejun Heo */ 2301111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2302e22bee78STejun Heo { 2303111c225aSTejun Heo struct worker *rescuer = __rescuer; 2304111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2305e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 2306e22bee78STejun Heo 2307e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2308111c225aSTejun Heo 2309111c225aSTejun Heo /* 2310111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2311111c225aSTejun Heo * doesn't participate in concurrency management. 2312111c225aSTejun Heo */ 2313111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2314e22bee78STejun Heo repeat: 2315e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 23161da177e4SLinus Torvalds 2317412d32e6SMike Galbraith if (kthread_should_stop()) { 2318412d32e6SMike Galbraith __set_current_state(TASK_RUNNING); 2319111c225aSTejun Heo rescuer->task->flags &= ~PF_WQ_WORKER; 2320e22bee78STejun Heo return 0; 2321412d32e6SMike Galbraith } 23221da177e4SLinus Torvalds 2323493a1724STejun Heo /* see whether any pwq is asking for help */ 2324493a1724STejun Heo spin_lock_irq(&workqueue_lock); 2325493a1724STejun Heo 2326493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2327493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2328493a1724STejun Heo struct pool_workqueue, mayday_node); 2329112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2330e22bee78STejun Heo struct work_struct *work, *n; 2331e22bee78STejun Heo 2332e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2333493a1724STejun Heo list_del_init(&pwq->mayday_node); 2334493a1724STejun Heo 2335493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 2336e22bee78STejun Heo 2337e22bee78STejun Heo /* migrate to the target cpu if possible */ 2338f36dc67bSLai Jiangshan worker_maybe_bind_and_lock(pool); 2339b3104104SLai Jiangshan rescuer->pool = pool; 2340e22bee78STejun Heo 2341e22bee78STejun Heo /* 2342e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2343e22bee78STejun Heo * process'em. 2344e22bee78STejun Heo */ 23456183c009STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 2346bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2347112202d9STejun Heo if (get_work_pwq(work) == pwq) 2348e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2349e22bee78STejun Heo 2350e22bee78STejun Heo process_scheduled_works(rescuer); 23517576958aSTejun Heo 23527576958aSTejun Heo /* 2353d565ed63STejun Heo * Leave this pool. If keep_working() is %true, notify a 23547576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23557576958aSTejun Heo * and stalling the execution. 23567576958aSTejun Heo */ 235763d95a91STejun Heo if (keep_working(pool)) 235863d95a91STejun Heo wake_up_worker(pool); 23597576958aSTejun Heo 2360b3104104SLai Jiangshan rescuer->pool = NULL; 2361493a1724STejun Heo spin_unlock(&pool->lock); 2362493a1724STejun Heo spin_lock(&workqueue_lock); 23631da177e4SLinus Torvalds } 23641da177e4SLinus Torvalds 2365493a1724STejun Heo spin_unlock_irq(&workqueue_lock); 2366493a1724STejun Heo 2367111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2368111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2369e22bee78STejun Heo schedule(); 2370e22bee78STejun Heo goto repeat; 23711da177e4SLinus Torvalds } 23721da177e4SLinus Torvalds 2373fc2e4d70SOleg Nesterov struct wq_barrier { 2374fc2e4d70SOleg Nesterov struct work_struct work; 2375fc2e4d70SOleg Nesterov struct completion done; 2376fc2e4d70SOleg Nesterov }; 2377fc2e4d70SOleg Nesterov 2378fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2379fc2e4d70SOleg Nesterov { 2380fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2381fc2e4d70SOleg Nesterov complete(&barr->done); 2382fc2e4d70SOleg Nesterov } 2383fc2e4d70SOleg Nesterov 23844690c4abSTejun Heo /** 23854690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2386112202d9STejun Heo * @pwq: pwq to insert barrier into 23874690c4abSTejun Heo * @barr: wq_barrier to insert 2388affee4b2STejun Heo * @target: target work to attach @barr to 2389affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23904690c4abSTejun Heo * 2391affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2392affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2393affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2394affee4b2STejun Heo * cpu. 2395affee4b2STejun Heo * 2396affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2397affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2398affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2399affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2400affee4b2STejun Heo * after a work with LINKED flag set. 2401affee4b2STejun Heo * 2402affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2403112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 24044690c4abSTejun Heo * 24054690c4abSTejun Heo * CONTEXT: 2406d565ed63STejun Heo * spin_lock_irq(pool->lock). 24074690c4abSTejun Heo */ 2408112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2409affee4b2STejun Heo struct wq_barrier *barr, 2410affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2411fc2e4d70SOleg Nesterov { 2412affee4b2STejun Heo struct list_head *head; 2413affee4b2STejun Heo unsigned int linked = 0; 2414affee4b2STejun Heo 2415dc186ad7SThomas Gleixner /* 2416d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2417dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2418dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2419dc186ad7SThomas Gleixner * might deadlock. 2420dc186ad7SThomas Gleixner */ 2421ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 242222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2423fc2e4d70SOleg Nesterov init_completion(&barr->done); 242483c22520SOleg Nesterov 2425affee4b2STejun Heo /* 2426affee4b2STejun Heo * If @target is currently being executed, schedule the 2427affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2428affee4b2STejun Heo */ 2429affee4b2STejun Heo if (worker) 2430affee4b2STejun Heo head = worker->scheduled.next; 2431affee4b2STejun Heo else { 2432affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2433affee4b2STejun Heo 2434affee4b2STejun Heo head = target->entry.next; 2435affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2436affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2437affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2438affee4b2STejun Heo } 2439affee4b2STejun Heo 2440dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2441112202d9STejun Heo insert_work(pwq, &barr->work, head, 2442affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2443fc2e4d70SOleg Nesterov } 2444fc2e4d70SOleg Nesterov 244573f53c4aSTejun Heo /** 2446112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 244773f53c4aSTejun Heo * @wq: workqueue being flushed 244873f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 244973f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 245073f53c4aSTejun Heo * 2451112202d9STejun Heo * Prepare pwqs for workqueue flushing. 245273f53c4aSTejun Heo * 2453112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2454112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2455112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2456112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2457112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 245873f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 245973f53c4aSTejun Heo * 246073f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 246173f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 246273f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 246373f53c4aSTejun Heo * is returned. 246473f53c4aSTejun Heo * 2465112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 246673f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 246773f53c4aSTejun Heo * advanced to @work_color. 246873f53c4aSTejun Heo * 246973f53c4aSTejun Heo * CONTEXT: 247073f53c4aSTejun Heo * mutex_lock(wq->flush_mutex). 247173f53c4aSTejun Heo * 247273f53c4aSTejun Heo * RETURNS: 247373f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 247473f53c4aSTejun Heo * otherwise. 247573f53c4aSTejun Heo */ 2476112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 247773f53c4aSTejun Heo int flush_color, int work_color) 24781da177e4SLinus Torvalds { 247973f53c4aSTejun Heo bool wait = false; 248049e3cf44STejun Heo struct pool_workqueue *pwq; 24811da177e4SLinus Torvalds 248273f53c4aSTejun Heo if (flush_color >= 0) { 24836183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2484112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2485dc186ad7SThomas Gleixner } 248614441960SOleg Nesterov 248776af4d93STejun Heo local_irq_disable(); 248876af4d93STejun Heo 248949e3cf44STejun Heo for_each_pwq(pwq, wq) { 2490112202d9STejun Heo struct worker_pool *pool = pwq->pool; 24911da177e4SLinus Torvalds 249276af4d93STejun Heo spin_lock(&pool->lock); 249373f53c4aSTejun Heo 249473f53c4aSTejun Heo if (flush_color >= 0) { 24956183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 249673f53c4aSTejun Heo 2497112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2498112202d9STejun Heo pwq->flush_color = flush_color; 2499112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 250073f53c4aSTejun Heo wait = true; 25011da177e4SLinus Torvalds } 250273f53c4aSTejun Heo } 250373f53c4aSTejun Heo 250473f53c4aSTejun Heo if (work_color >= 0) { 25056183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2506112202d9STejun Heo pwq->work_color = work_color; 250773f53c4aSTejun Heo } 250873f53c4aSTejun Heo 250976af4d93STejun Heo spin_unlock(&pool->lock); 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 251276af4d93STejun Heo local_irq_enable(); 251376af4d93STejun Heo 2514112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 251573f53c4aSTejun Heo complete(&wq->first_flusher->done); 251673f53c4aSTejun Heo 251773f53c4aSTejun Heo return wait; 251883c22520SOleg Nesterov } 25191da177e4SLinus Torvalds 25200fcb78c2SRolf Eike Beer /** 25211da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25220fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25231da177e4SLinus Torvalds * 25241da177e4SLinus Torvalds * Forces execution of the workqueue and blocks until its completion. 25251da177e4SLinus Torvalds * This is typically used in driver shutdown handlers. 25261da177e4SLinus Torvalds * 2527fc2e4d70SOleg Nesterov * We sleep until all works which were queued on entry have been handled, 2528fc2e4d70SOleg Nesterov * but we are not livelocked by new incoming ones. 25291da177e4SLinus Torvalds */ 25307ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25311da177e4SLinus Torvalds { 253273f53c4aSTejun Heo struct wq_flusher this_flusher = { 253373f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 253473f53c4aSTejun Heo .flush_color = -1, 253573f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 253673f53c4aSTejun Heo }; 253773f53c4aSTejun Heo int next_color; 2538b1f4ec17SOleg Nesterov 25393295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 25403295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 254173f53c4aSTejun Heo 254273f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 254373f53c4aSTejun Heo 254473f53c4aSTejun Heo /* 254573f53c4aSTejun Heo * Start-to-wait phase 254673f53c4aSTejun Heo */ 254773f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 254873f53c4aSTejun Heo 254973f53c4aSTejun Heo if (next_color != wq->flush_color) { 255073f53c4aSTejun Heo /* 255173f53c4aSTejun Heo * Color space is not full. The current work_color 255273f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 255373f53c4aSTejun Heo * by one. 255473f53c4aSTejun Heo */ 25556183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 255673f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 255773f53c4aSTejun Heo wq->work_color = next_color; 255873f53c4aSTejun Heo 255973f53c4aSTejun Heo if (!wq->first_flusher) { 256073f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 25616183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 256273f53c4aSTejun Heo 256373f53c4aSTejun Heo wq->first_flusher = &this_flusher; 256473f53c4aSTejun Heo 2565112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 256673f53c4aSTejun Heo wq->work_color)) { 256773f53c4aSTejun Heo /* nothing to flush, done */ 256873f53c4aSTejun Heo wq->flush_color = next_color; 256973f53c4aSTejun Heo wq->first_flusher = NULL; 257073f53c4aSTejun Heo goto out_unlock; 257173f53c4aSTejun Heo } 257273f53c4aSTejun Heo } else { 257373f53c4aSTejun Heo /* wait in queue */ 25746183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 257573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2576112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 257773f53c4aSTejun Heo } 257873f53c4aSTejun Heo } else { 257973f53c4aSTejun Heo /* 258073f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 258173f53c4aSTejun Heo * The next flush completion will assign us 258273f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 258373f53c4aSTejun Heo */ 258473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 258573f53c4aSTejun Heo } 258673f53c4aSTejun Heo 258773f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 258873f53c4aSTejun Heo 258973f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 259073f53c4aSTejun Heo 259173f53c4aSTejun Heo /* 259273f53c4aSTejun Heo * Wake-up-and-cascade phase 259373f53c4aSTejun Heo * 259473f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 259573f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 259673f53c4aSTejun Heo */ 259773f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 259873f53c4aSTejun Heo return; 259973f53c4aSTejun Heo 260073f53c4aSTejun Heo mutex_lock(&wq->flush_mutex); 260173f53c4aSTejun Heo 26024ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26034ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26044ce48b37STejun Heo goto out_unlock; 26054ce48b37STejun Heo 260673f53c4aSTejun Heo wq->first_flusher = NULL; 260773f53c4aSTejun Heo 26086183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 26096183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 261073f53c4aSTejun Heo 261173f53c4aSTejun Heo while (true) { 261273f53c4aSTejun Heo struct wq_flusher *next, *tmp; 261373f53c4aSTejun Heo 261473f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 261573f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 261673f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 261773f53c4aSTejun Heo break; 261873f53c4aSTejun Heo list_del_init(&next->list); 261973f53c4aSTejun Heo complete(&next->done); 262073f53c4aSTejun Heo } 262173f53c4aSTejun Heo 26226183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 262373f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 262473f53c4aSTejun Heo 262573f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 262673f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 262773f53c4aSTejun Heo 262873f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 262973f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 263073f53c4aSTejun Heo /* 263173f53c4aSTejun Heo * Assign the same color to all overflowed 263273f53c4aSTejun Heo * flushers, advance work_color and append to 263373f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 263473f53c4aSTejun Heo * phase for these overflowed flushers. 263573f53c4aSTejun Heo */ 263673f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 263773f53c4aSTejun Heo tmp->flush_color = wq->work_color; 263873f53c4aSTejun Heo 263973f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 264073f53c4aSTejun Heo 264173f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 264273f53c4aSTejun Heo &wq->flusher_queue); 2643112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 264473f53c4aSTejun Heo } 264573f53c4aSTejun Heo 264673f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 26476183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 264873f53c4aSTejun Heo break; 264973f53c4aSTejun Heo } 265073f53c4aSTejun Heo 265173f53c4aSTejun Heo /* 265273f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2653112202d9STejun Heo * the new first flusher and arm pwqs. 265473f53c4aSTejun Heo */ 26556183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 26566183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 265773f53c4aSTejun Heo 265873f53c4aSTejun Heo list_del_init(&next->list); 265973f53c4aSTejun Heo wq->first_flusher = next; 266073f53c4aSTejun Heo 2661112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 266273f53c4aSTejun Heo break; 266373f53c4aSTejun Heo 266473f53c4aSTejun Heo /* 266573f53c4aSTejun Heo * Meh... this color is already done, clear first 266673f53c4aSTejun Heo * flusher and repeat cascading. 266773f53c4aSTejun Heo */ 266873f53c4aSTejun Heo wq->first_flusher = NULL; 266973f53c4aSTejun Heo } 267073f53c4aSTejun Heo 267173f53c4aSTejun Heo out_unlock: 267273f53c4aSTejun Heo mutex_unlock(&wq->flush_mutex); 26731da177e4SLinus Torvalds } 2674ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26751da177e4SLinus Torvalds 26769c5a2ba7STejun Heo /** 26779c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26789c5a2ba7STejun Heo * @wq: workqueue to drain 26799c5a2ba7STejun Heo * 26809c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26819c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26829c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26839c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26849c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26859c5a2ba7STejun Heo * takes too long. 26869c5a2ba7STejun Heo */ 26879c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26889c5a2ba7STejun Heo { 26899c5a2ba7STejun Heo unsigned int flush_cnt = 0; 269049e3cf44STejun Heo struct pool_workqueue *pwq; 26919c5a2ba7STejun Heo 26929c5a2ba7STejun Heo /* 26939c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26949c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 26959c5a2ba7STejun Heo * Use WQ_DRAINING so that queue doesn't have to check nr_drainers. 26969c5a2ba7STejun Heo */ 2697e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 26989c5a2ba7STejun Heo if (!wq->nr_drainers++) 26999c5a2ba7STejun Heo wq->flags |= WQ_DRAINING; 2700e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 27019c5a2ba7STejun Heo reflush: 27029c5a2ba7STejun Heo flush_workqueue(wq); 27039c5a2ba7STejun Heo 270476af4d93STejun Heo local_irq_disable(); 270576af4d93STejun Heo 270649e3cf44STejun Heo for_each_pwq(pwq, wq) { 2707fa2563e4SThomas Tuttle bool drained; 27089c5a2ba7STejun Heo 270976af4d93STejun Heo spin_lock(&pwq->pool->lock); 2710112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 271176af4d93STejun Heo spin_unlock(&pwq->pool->lock); 2712fa2563e4SThomas Tuttle 2713fa2563e4SThomas Tuttle if (drained) 27149c5a2ba7STejun Heo continue; 27159c5a2ba7STejun Heo 27169c5a2ba7STejun Heo if (++flush_cnt == 10 || 27179c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2718044c782cSValentin Ilie pr_warn("workqueue %s: flush on destruction isn't complete after %u tries\n", 27199c5a2ba7STejun Heo wq->name, flush_cnt); 272076af4d93STejun Heo 272176af4d93STejun Heo local_irq_enable(); 27229c5a2ba7STejun Heo goto reflush; 27239c5a2ba7STejun Heo } 27249c5a2ba7STejun Heo 272576af4d93STejun Heo spin_lock(&workqueue_lock); 27269c5a2ba7STejun Heo if (!--wq->nr_drainers) 27279c5a2ba7STejun Heo wq->flags &= ~WQ_DRAINING; 272876af4d93STejun Heo spin_unlock(&workqueue_lock); 272976af4d93STejun Heo 273076af4d93STejun Heo local_irq_enable(); 27319c5a2ba7STejun Heo } 27329c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27339c5a2ba7STejun Heo 2734606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2735baf59022STejun Heo { 2736baf59022STejun Heo struct worker *worker = NULL; 2737c9e7cf27STejun Heo struct worker_pool *pool; 2738112202d9STejun Heo struct pool_workqueue *pwq; 2739baf59022STejun Heo 2740baf59022STejun Heo might_sleep(); 2741baf59022STejun Heo 2742fa1b54e6STejun Heo local_irq_disable(); 2743fa1b54e6STejun Heo pool = get_work_pool(work); 2744fa1b54e6STejun Heo if (!pool) { 2745fa1b54e6STejun Heo local_irq_enable(); 2746fa1b54e6STejun Heo return false; 2747fa1b54e6STejun Heo } 2748fa1b54e6STejun Heo 2749fa1b54e6STejun Heo spin_lock(&pool->lock); 27500b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2751112202d9STejun Heo pwq = get_work_pwq(work); 2752112202d9STejun Heo if (pwq) { 2753112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2754baf59022STejun Heo goto already_gone; 2755606a5020STejun Heo } else { 2756c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2757baf59022STejun Heo if (!worker) 2758baf59022STejun Heo goto already_gone; 2759112202d9STejun Heo pwq = worker->current_pwq; 2760606a5020STejun Heo } 2761baf59022STejun Heo 2762112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2763d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2764baf59022STejun Heo 2765e159489bSTejun Heo /* 2766e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2767e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2768e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2769e159489bSTejun Heo * access. 2770e159489bSTejun Heo */ 2771112202d9STejun Heo if (pwq->wq->saved_max_active == 1 || pwq->wq->flags & WQ_RESCUER) 2772112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2773e159489bSTejun Heo else 2774112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 2775112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2776e159489bSTejun Heo 2777baf59022STejun Heo return true; 2778baf59022STejun Heo already_gone: 2779d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2780baf59022STejun Heo return false; 2781baf59022STejun Heo } 2782baf59022STejun Heo 2783db700897SOleg Nesterov /** 2784401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2785401a8d04STejun Heo * @work: the work to flush 2786db700897SOleg Nesterov * 2787606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2788606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2789401a8d04STejun Heo * 2790401a8d04STejun Heo * RETURNS: 2791401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2792401a8d04STejun Heo * %false if it was already idle. 2793db700897SOleg Nesterov */ 2794401a8d04STejun Heo bool flush_work(struct work_struct *work) 2795db700897SOleg Nesterov { 2796db700897SOleg Nesterov struct wq_barrier barr; 2797db700897SOleg Nesterov 27980976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 27990976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 28000976dfc1SStephen Boyd 2801606a5020STejun Heo if (start_flush_work(work, &barr)) { 2802db700897SOleg Nesterov wait_for_completion(&barr.done); 2803dc186ad7SThomas Gleixner destroy_work_on_stack(&barr.work); 2804401a8d04STejun Heo return true; 2805606a5020STejun Heo } else { 2806401a8d04STejun Heo return false; 2807db700897SOleg Nesterov } 2808606a5020STejun Heo } 2809db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2810db700897SOleg Nesterov 281136e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2812401a8d04STejun Heo { 2813bbb68dfaSTejun Heo unsigned long flags; 28141f1f642eSOleg Nesterov int ret; 28151f1f642eSOleg Nesterov 28161f1f642eSOleg Nesterov do { 2817bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2818bbb68dfaSTejun Heo /* 2819bbb68dfaSTejun Heo * If someone else is canceling, wait for the same event it 2820bbb68dfaSTejun Heo * would be waiting for before retrying. 2821bbb68dfaSTejun Heo */ 2822bbb68dfaSTejun Heo if (unlikely(ret == -ENOENT)) 2823606a5020STejun Heo flush_work(work); 28241f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 28251f1f642eSOleg Nesterov 2826bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2827bbb68dfaSTejun Heo mark_work_canceling(work); 2828bbb68dfaSTejun Heo local_irq_restore(flags); 2829bbb68dfaSTejun Heo 2830606a5020STejun Heo flush_work(work); 28317a22ad75STejun Heo clear_work_data(work); 28321f1f642eSOleg Nesterov return ret; 28331f1f642eSOleg Nesterov } 28341f1f642eSOleg Nesterov 28356e84d644SOleg Nesterov /** 2836401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2837401a8d04STejun Heo * @work: the work to cancel 28386e84d644SOleg Nesterov * 2839401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2840401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2841401a8d04STejun Heo * another workqueue. On return from this function, @work is 2842401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28431f1f642eSOleg Nesterov * 2844401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2845401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28466e84d644SOleg Nesterov * 2847401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28486e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2849401a8d04STejun Heo * 2850401a8d04STejun Heo * RETURNS: 2851401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28526e84d644SOleg Nesterov */ 2853401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28546e84d644SOleg Nesterov { 285536e227d2STejun Heo return __cancel_work_timer(work, false); 2856b89deed3SOleg Nesterov } 285728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2858b89deed3SOleg Nesterov 28596e84d644SOleg Nesterov /** 2860401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2861401a8d04STejun Heo * @dwork: the delayed work to flush 28626e84d644SOleg Nesterov * 2863401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2864401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2865401a8d04STejun Heo * considers the last queueing instance of @dwork. 28661f1f642eSOleg Nesterov * 2867401a8d04STejun Heo * RETURNS: 2868401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2869401a8d04STejun Heo * %false if it was already idle. 28706e84d644SOleg Nesterov */ 2871401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2872401a8d04STejun Heo { 28738930cabaSTejun Heo local_irq_disable(); 2874401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 287560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 28768930cabaSTejun Heo local_irq_enable(); 2877401a8d04STejun Heo return flush_work(&dwork->work); 2878401a8d04STejun Heo } 2879401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2880401a8d04STejun Heo 2881401a8d04STejun Heo /** 288257b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 288357b30ae7STejun Heo * @dwork: delayed_work to cancel 288409383498STejun Heo * 288557b30ae7STejun Heo * Kill off a pending delayed_work. Returns %true if @dwork was pending 288657b30ae7STejun Heo * and canceled; %false if wasn't pending. Note that the work callback 288757b30ae7STejun Heo * function may still be running on return, unless it returns %true and the 288857b30ae7STejun Heo * work doesn't re-arm itself. Explicitly flush or use 288957b30ae7STejun Heo * cancel_delayed_work_sync() to wait on it. 289009383498STejun Heo * 289157b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 289209383498STejun Heo */ 289357b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 289409383498STejun Heo { 289557b30ae7STejun Heo unsigned long flags; 289657b30ae7STejun Heo int ret; 289757b30ae7STejun Heo 289857b30ae7STejun Heo do { 289957b30ae7STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 290057b30ae7STejun Heo } while (unlikely(ret == -EAGAIN)); 290157b30ae7STejun Heo 290257b30ae7STejun Heo if (unlikely(ret < 0)) 290357b30ae7STejun Heo return false; 290457b30ae7STejun Heo 29057c3eed5cSTejun Heo set_work_pool_and_clear_pending(&dwork->work, 29067c3eed5cSTejun Heo get_work_pool_id(&dwork->work)); 290757b30ae7STejun Heo local_irq_restore(flags); 2908c0158ca6SDan Magenheimer return ret; 290909383498STejun Heo } 291057b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 291109383498STejun Heo 291209383498STejun Heo /** 2913401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2914401a8d04STejun Heo * @dwork: the delayed work cancel 2915401a8d04STejun Heo * 2916401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2917401a8d04STejun Heo * 2918401a8d04STejun Heo * RETURNS: 2919401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2920401a8d04STejun Heo */ 2921401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29226e84d644SOleg Nesterov { 292336e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29246e84d644SOleg Nesterov } 2925f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29261da177e4SLinus Torvalds 29270fcb78c2SRolf Eike Beer /** 2928c1a220e7SZhang Rui * schedule_work_on - put work task on a specific cpu 2929c1a220e7SZhang Rui * @cpu: cpu to put the work task on 2930c1a220e7SZhang Rui * @work: job to be done 2931c1a220e7SZhang Rui * 2932c1a220e7SZhang Rui * This puts a job on a specific cpu 2933c1a220e7SZhang Rui */ 2934d4283e93STejun Heo bool schedule_work_on(int cpu, struct work_struct *work) 2935c1a220e7SZhang Rui { 2936d320c038STejun Heo return queue_work_on(cpu, system_wq, work); 2937c1a220e7SZhang Rui } 2938c1a220e7SZhang Rui EXPORT_SYMBOL(schedule_work_on); 2939c1a220e7SZhang Rui 29400fcb78c2SRolf Eike Beer /** 2941ae90dd5dSDave Jones * schedule_work - put work task in global workqueue 29421da177e4SLinus Torvalds * @work: job to be done 29431da177e4SLinus Torvalds * 2944d4283e93STejun Heo * Returns %false if @work was already on the kernel-global workqueue and 2945d4283e93STejun Heo * %true otherwise. 294652bad64dSDavid Howells * 29470fcb78c2SRolf Eike Beer * This puts a job in the kernel-global workqueue if it was not already 29480fcb78c2SRolf Eike Beer * queued and leaves it in the same position on the kernel-global 29490fcb78c2SRolf Eike Beer * workqueue otherwise. 29501da177e4SLinus Torvalds */ 2951d4283e93STejun Heo bool schedule_work(struct work_struct *work) 29521da177e4SLinus Torvalds { 29530fcb78c2SRolf Eike Beer return queue_work(system_wq, work); 29541da177e4SLinus Torvalds } 29550fcb78c2SRolf Eike Beer EXPORT_SYMBOL(schedule_work); 29561da177e4SLinus Torvalds 29570fcb78c2SRolf Eike Beer /** 29580fcb78c2SRolf Eike Beer * schedule_delayed_work_on - queue work in global workqueue on CPU after delay 29590fcb78c2SRolf Eike Beer * @cpu: cpu to use 29600fcb78c2SRolf Eike Beer * @dwork: job to be done 29610fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait 29620fcb78c2SRolf Eike Beer * 29630fcb78c2SRolf Eike Beer * After waiting for a given time this puts a job in the kernel-global 29640fcb78c2SRolf Eike Beer * workqueue on the specified CPU. 29650fcb78c2SRolf Eike Beer */ 2966d4283e93STejun Heo bool schedule_delayed_work_on(int cpu, struct delayed_work *dwork, 2967d4283e93STejun Heo unsigned long delay) 29681da177e4SLinus Torvalds { 2969d320c038STejun Heo return queue_delayed_work_on(cpu, system_wq, dwork, delay); 29701da177e4SLinus Torvalds } 2971ae90dd5dSDave Jones EXPORT_SYMBOL(schedule_delayed_work_on); 29721da177e4SLinus Torvalds 2973b6136773SAndrew Morton /** 29740a13c00eSTejun Heo * schedule_delayed_work - put work task in global workqueue after delay 29750a13c00eSTejun Heo * @dwork: job to be done 29760a13c00eSTejun Heo * @delay: number of jiffies to wait or 0 for immediate execution 29770a13c00eSTejun Heo * 29780a13c00eSTejun Heo * After waiting for a given time this puts a job in the kernel-global 29790a13c00eSTejun Heo * workqueue. 29800a13c00eSTejun Heo */ 2981d4283e93STejun Heo bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay) 29820a13c00eSTejun Heo { 29830a13c00eSTejun Heo return queue_delayed_work(system_wq, dwork, delay); 29840a13c00eSTejun Heo } 29850a13c00eSTejun Heo EXPORT_SYMBOL(schedule_delayed_work); 29860a13c00eSTejun Heo 29870a13c00eSTejun Heo /** 298831ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2989b6136773SAndrew Morton * @func: the function to call 2990b6136773SAndrew Morton * 299131ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 299231ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2993b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 299431ddd871STejun Heo * 299531ddd871STejun Heo * RETURNS: 299631ddd871STejun Heo * 0 on success, -errno on failure. 2997b6136773SAndrew Morton */ 299865f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 299915316ba8SChristoph Lameter { 300015316ba8SChristoph Lameter int cpu; 300138f51568SNamhyung Kim struct work_struct __percpu *works; 300215316ba8SChristoph Lameter 3003b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3004b6136773SAndrew Morton if (!works) 300515316ba8SChristoph Lameter return -ENOMEM; 3006b6136773SAndrew Morton 300795402b38SGautham R Shenoy get_online_cpus(); 300893981800STejun Heo 300915316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30109bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30119bfb1839SIngo Molnar 30129bfb1839SIngo Molnar INIT_WORK(work, func); 30138de6d308SOleg Nesterov schedule_work_on(cpu, work); 301415316ba8SChristoph Lameter } 301593981800STejun Heo 301693981800STejun Heo for_each_online_cpu(cpu) 30178616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 301893981800STejun Heo 301995402b38SGautham R Shenoy put_online_cpus(); 3020b6136773SAndrew Morton free_percpu(works); 302115316ba8SChristoph Lameter return 0; 302215316ba8SChristoph Lameter } 302315316ba8SChristoph Lameter 3024eef6a7d5SAlan Stern /** 3025eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 3026eef6a7d5SAlan Stern * 3027eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 3028eef6a7d5SAlan Stern * completion. 3029eef6a7d5SAlan Stern * 3030eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 3031eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 3032eef6a7d5SAlan Stern * will lead to deadlock: 3033eef6a7d5SAlan Stern * 3034eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 3035eef6a7d5SAlan Stern * a lock held by your code or its caller. 3036eef6a7d5SAlan Stern * 3037eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 3038eef6a7d5SAlan Stern * 3039eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 3040eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 3041eef6a7d5SAlan Stern * what locks they need, which you have no control over. 3042eef6a7d5SAlan Stern * 3043eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 3044eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 3045eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 3046eef6a7d5SAlan Stern * cancel_work_sync() instead. 3047eef6a7d5SAlan Stern */ 30481da177e4SLinus Torvalds void flush_scheduled_work(void) 30491da177e4SLinus Torvalds { 3050d320c038STejun Heo flush_workqueue(system_wq); 30511da177e4SLinus Torvalds } 3052ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 30531da177e4SLinus Torvalds 30541da177e4SLinus Torvalds /** 30551fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30561fa44ecaSJames Bottomley * @fn: the function to execute 30571fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30581fa44ecaSJames Bottomley * be available when the work executes) 30591fa44ecaSJames Bottomley * 30601fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30611fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30621fa44ecaSJames Bottomley * 30631fa44ecaSJames Bottomley * Returns: 0 - function was executed 30641fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30651fa44ecaSJames Bottomley */ 306665f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30671fa44ecaSJames Bottomley { 30681fa44ecaSJames Bottomley if (!in_interrupt()) { 306965f27f38SDavid Howells fn(&ew->work); 30701fa44ecaSJames Bottomley return 0; 30711fa44ecaSJames Bottomley } 30721fa44ecaSJames Bottomley 307365f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30741fa44ecaSJames Bottomley schedule_work(&ew->work); 30751fa44ecaSJames Bottomley 30761fa44ecaSJames Bottomley return 1; 30771fa44ecaSJames Bottomley } 30781fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30791fa44ecaSJames Bottomley 30801da177e4SLinus Torvalds int keventd_up(void) 30811da177e4SLinus Torvalds { 3082d320c038STejun Heo return system_wq != NULL; 30831da177e4SLinus Torvalds } 30841da177e4SLinus Torvalds 30857a4e344cSTejun Heo /** 30867a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 30877a4e344cSTejun Heo * @attrs: workqueue_attrs to free 30887a4e344cSTejun Heo * 30897a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 30907a4e344cSTejun Heo */ 30917a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs) 30927a4e344cSTejun Heo { 30937a4e344cSTejun Heo if (attrs) { 30947a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 30957a4e344cSTejun Heo kfree(attrs); 30967a4e344cSTejun Heo } 30977a4e344cSTejun Heo } 30987a4e344cSTejun Heo 30997a4e344cSTejun Heo /** 31007a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 31017a4e344cSTejun Heo * @gfp_mask: allocation mask to use 31027a4e344cSTejun Heo * 31037a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 31047a4e344cSTejun Heo * return it. Returns NULL on failure. 31057a4e344cSTejun Heo */ 31067a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) 31077a4e344cSTejun Heo { 31087a4e344cSTejun Heo struct workqueue_attrs *attrs; 31097a4e344cSTejun Heo 31107a4e344cSTejun Heo attrs = kzalloc(sizeof(*attrs), gfp_mask); 31117a4e344cSTejun Heo if (!attrs) 31127a4e344cSTejun Heo goto fail; 31137a4e344cSTejun Heo if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) 31147a4e344cSTejun Heo goto fail; 31157a4e344cSTejun Heo 31167a4e344cSTejun Heo cpumask_setall(attrs->cpumask); 31177a4e344cSTejun Heo return attrs; 31187a4e344cSTejun Heo fail: 31197a4e344cSTejun Heo free_workqueue_attrs(attrs); 31207a4e344cSTejun Heo return NULL; 31217a4e344cSTejun Heo } 31227a4e344cSTejun Heo 312329c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 312429c91e99STejun Heo const struct workqueue_attrs *from) 312529c91e99STejun Heo { 312629c91e99STejun Heo to->nice = from->nice; 312729c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 312829c91e99STejun Heo } 312929c91e99STejun Heo 313029c91e99STejun Heo /* 313129c91e99STejun Heo * Hacky implementation of jhash of bitmaps which only considers the 313229c91e99STejun Heo * specified number of bits. We probably want a proper implementation in 313329c91e99STejun Heo * include/linux/jhash.h. 313429c91e99STejun Heo */ 313529c91e99STejun Heo static u32 jhash_bitmap(const unsigned long *bitmap, int bits, u32 hash) 313629c91e99STejun Heo { 313729c91e99STejun Heo int nr_longs = bits / BITS_PER_LONG; 313829c91e99STejun Heo int nr_leftover = bits % BITS_PER_LONG; 313929c91e99STejun Heo unsigned long leftover = 0; 314029c91e99STejun Heo 314129c91e99STejun Heo if (nr_longs) 314229c91e99STejun Heo hash = jhash(bitmap, nr_longs * sizeof(long), hash); 314329c91e99STejun Heo if (nr_leftover) { 314429c91e99STejun Heo bitmap_copy(&leftover, bitmap + nr_longs, nr_leftover); 314529c91e99STejun Heo hash = jhash(&leftover, sizeof(long), hash); 314629c91e99STejun Heo } 314729c91e99STejun Heo return hash; 314829c91e99STejun Heo } 314929c91e99STejun Heo 315029c91e99STejun Heo /* hash value of the content of @attr */ 315129c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 315229c91e99STejun Heo { 315329c91e99STejun Heo u32 hash = 0; 315429c91e99STejun Heo 315529c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 315629c91e99STejun Heo hash = jhash_bitmap(cpumask_bits(attrs->cpumask), nr_cpu_ids, hash); 315729c91e99STejun Heo return hash; 315829c91e99STejun Heo } 315929c91e99STejun Heo 316029c91e99STejun Heo /* content equality test */ 316129c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 316229c91e99STejun Heo const struct workqueue_attrs *b) 316329c91e99STejun Heo { 316429c91e99STejun Heo if (a->nice != b->nice) 316529c91e99STejun Heo return false; 316629c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 316729c91e99STejun Heo return false; 316829c91e99STejun Heo return true; 316929c91e99STejun Heo } 317029c91e99STejun Heo 31717a4e344cSTejun Heo /** 31727a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 31737a4e344cSTejun Heo * @pool: worker_pool to initialize 31747a4e344cSTejun Heo * 31757a4e344cSTejun Heo * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs. 317629c91e99STejun Heo * Returns 0 on success, -errno on failure. Even on failure, all fields 317729c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 317829c91e99STejun Heo * on @pool safely to release it. 31797a4e344cSTejun Heo */ 31807a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 31814e1a1f9aSTejun Heo { 31824e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 318329c91e99STejun Heo pool->id = -1; 318429c91e99STejun Heo pool->cpu = -1; 31854e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 31864e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 31874e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 31884e1a1f9aSTejun Heo hash_init(pool->busy_hash); 31894e1a1f9aSTejun Heo 31904e1a1f9aSTejun Heo init_timer_deferrable(&pool->idle_timer); 31914e1a1f9aSTejun Heo pool->idle_timer.function = idle_worker_timeout; 31924e1a1f9aSTejun Heo pool->idle_timer.data = (unsigned long)pool; 31934e1a1f9aSTejun Heo 31944e1a1f9aSTejun Heo setup_timer(&pool->mayday_timer, pool_mayday_timeout, 31954e1a1f9aSTejun Heo (unsigned long)pool); 31964e1a1f9aSTejun Heo 31974e1a1f9aSTejun Heo mutex_init(&pool->manager_arb); 31984e1a1f9aSTejun Heo mutex_init(&pool->assoc_mutex); 31994e1a1f9aSTejun Heo ida_init(&pool->worker_ida); 32007a4e344cSTejun Heo 320129c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 320229c91e99STejun Heo pool->refcnt = 1; 320329c91e99STejun Heo 320429c91e99STejun Heo /* shouldn't fail above this point */ 32057a4e344cSTejun Heo pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); 32067a4e344cSTejun Heo if (!pool->attrs) 32077a4e344cSTejun Heo return -ENOMEM; 32087a4e344cSTejun Heo return 0; 32094e1a1f9aSTejun Heo } 32104e1a1f9aSTejun Heo 321129c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 321229c91e99STejun Heo { 321329c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 321429c91e99STejun Heo 321529c91e99STejun Heo ida_destroy(&pool->worker_ida); 321629c91e99STejun Heo free_workqueue_attrs(pool->attrs); 321729c91e99STejun Heo kfree(pool); 321829c91e99STejun Heo } 321929c91e99STejun Heo 322029c91e99STejun Heo /** 322129c91e99STejun Heo * put_unbound_pool - put a worker_pool 322229c91e99STejun Heo * @pool: worker_pool to put 322329c91e99STejun Heo * 322429c91e99STejun Heo * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU 322529c91e99STejun Heo * safe manner. 322629c91e99STejun Heo */ 322729c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 322829c91e99STejun Heo { 322929c91e99STejun Heo struct worker *worker; 323029c91e99STejun Heo 323129c91e99STejun Heo spin_lock_irq(&workqueue_lock); 323229c91e99STejun Heo if (--pool->refcnt) { 323329c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 323429c91e99STejun Heo return; 323529c91e99STejun Heo } 323629c91e99STejun Heo 323729c91e99STejun Heo /* sanity checks */ 323829c91e99STejun Heo if (WARN_ON(!(pool->flags & POOL_DISASSOCIATED)) || 323929c91e99STejun Heo WARN_ON(!list_empty(&pool->worklist))) { 324029c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 324129c91e99STejun Heo return; 324229c91e99STejun Heo } 324329c91e99STejun Heo 324429c91e99STejun Heo /* release id and unhash */ 324529c91e99STejun Heo if (pool->id >= 0) 324629c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 324729c91e99STejun Heo hash_del(&pool->hash_node); 324829c91e99STejun Heo 324929c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 325029c91e99STejun Heo 325129c91e99STejun Heo /* lock out manager and destroy all workers */ 325229c91e99STejun Heo mutex_lock(&pool->manager_arb); 325329c91e99STejun Heo spin_lock_irq(&pool->lock); 325429c91e99STejun Heo 325529c91e99STejun Heo while ((worker = first_worker(pool))) 325629c91e99STejun Heo destroy_worker(worker); 325729c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 325829c91e99STejun Heo 325929c91e99STejun Heo spin_unlock_irq(&pool->lock); 326029c91e99STejun Heo mutex_unlock(&pool->manager_arb); 326129c91e99STejun Heo 326229c91e99STejun Heo /* shut down the timers */ 326329c91e99STejun Heo del_timer_sync(&pool->idle_timer); 326429c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 326529c91e99STejun Heo 326629c91e99STejun Heo /* sched-RCU protected to allow dereferences from get_work_pool() */ 326729c91e99STejun Heo call_rcu_sched(&pool->rcu, rcu_free_pool); 326829c91e99STejun Heo } 326929c91e99STejun Heo 327029c91e99STejun Heo /** 327129c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 327229c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 327329c91e99STejun Heo * 327429c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 327529c91e99STejun Heo * reference count and return it. If there already is a matching 327629c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 327729c91e99STejun Heo * create a new one. On failure, returns NULL. 327829c91e99STejun Heo */ 327929c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 328029c91e99STejun Heo { 328129c91e99STejun Heo static DEFINE_MUTEX(create_mutex); 328229c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 328329c91e99STejun Heo struct worker_pool *pool; 328429c91e99STejun Heo struct worker *worker; 328529c91e99STejun Heo 328629c91e99STejun Heo mutex_lock(&create_mutex); 328729c91e99STejun Heo 328829c91e99STejun Heo /* do we already have a matching pool? */ 328929c91e99STejun Heo spin_lock_irq(&workqueue_lock); 329029c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 329129c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 329229c91e99STejun Heo pool->refcnt++; 329329c91e99STejun Heo goto out_unlock; 329429c91e99STejun Heo } 329529c91e99STejun Heo } 329629c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 329729c91e99STejun Heo 329829c91e99STejun Heo /* nope, create a new one */ 329929c91e99STejun Heo pool = kzalloc(sizeof(*pool), GFP_KERNEL); 330029c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 330129c91e99STejun Heo goto fail; 330229c91e99STejun Heo 330329c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 330429c91e99STejun Heo 330529c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 330629c91e99STejun Heo goto fail; 330729c91e99STejun Heo 330829c91e99STejun Heo /* create and start the initial worker */ 330929c91e99STejun Heo worker = create_worker(pool); 331029c91e99STejun Heo if (!worker) 331129c91e99STejun Heo goto fail; 331229c91e99STejun Heo 331329c91e99STejun Heo spin_lock_irq(&pool->lock); 331429c91e99STejun Heo start_worker(worker); 331529c91e99STejun Heo spin_unlock_irq(&pool->lock); 331629c91e99STejun Heo 331729c91e99STejun Heo /* install */ 331829c91e99STejun Heo spin_lock_irq(&workqueue_lock); 331929c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 332029c91e99STejun Heo out_unlock: 332129c91e99STejun Heo spin_unlock_irq(&workqueue_lock); 332229c91e99STejun Heo mutex_unlock(&create_mutex); 332329c91e99STejun Heo return pool; 332429c91e99STejun Heo fail: 332529c91e99STejun Heo mutex_unlock(&create_mutex); 332629c91e99STejun Heo if (pool) 332729c91e99STejun Heo put_unbound_pool(pool); 332829c91e99STejun Heo return NULL; 332929c91e99STejun Heo } 333029c91e99STejun Heo 333130cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 33321da177e4SLinus Torvalds { 333349e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 333430cdf249STejun Heo int cpu; 3335e1d8aa9fSFrederic Weisbecker 333630cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3337420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3338420c0ddbSTejun Heo if (!wq->cpu_pwqs) 333930cdf249STejun Heo return -ENOMEM; 334030cdf249STejun Heo 334130cdf249STejun Heo for_each_possible_cpu(cpu) { 33427fb98ea7STejun Heo struct pool_workqueue *pwq = 33437fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 33447a62c2c8STejun Heo struct worker_pool *cpu_pools = 3345f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 334630cdf249STejun Heo 33477a62c2c8STejun Heo pwq->pool = &cpu_pools[highpri]; 334876af4d93STejun Heo list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 334930cdf249STejun Heo } 335030cdf249STejun Heo } else { 335130cdf249STejun Heo struct pool_workqueue *pwq; 335230cdf249STejun Heo 335330cdf249STejun Heo pwq = kmem_cache_zalloc(pwq_cache, GFP_KERNEL); 335430cdf249STejun Heo if (!pwq) 335530cdf249STejun Heo return -ENOMEM; 335630cdf249STejun Heo 335729c91e99STejun Heo pwq->pool = get_unbound_pool(unbound_std_wq_attrs[highpri]); 335829c91e99STejun Heo if (!pwq->pool) { 335929c91e99STejun Heo kmem_cache_free(pwq_cache, pwq); 336029c91e99STejun Heo return -ENOMEM; 336129c91e99STejun Heo } 336229c91e99STejun Heo 336376af4d93STejun Heo list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 336430cdf249STejun Heo } 336530cdf249STejun Heo 336630cdf249STejun Heo return 0; 33670f900049STejun Heo } 33680f900049STejun Heo 3369112202d9STejun Heo static void free_pwqs(struct workqueue_struct *wq) 337006ba38a9SOleg Nesterov { 3371e06ffa1eSLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 3372420c0ddbSTejun Heo free_percpu(wq->cpu_pwqs); 3373420c0ddbSTejun Heo else if (!list_empty(&wq->pwqs)) 3374420c0ddbSTejun Heo kmem_cache_free(pwq_cache, list_first_entry(&wq->pwqs, 3375420c0ddbSTejun Heo struct pool_workqueue, pwqs_node)); 337606ba38a9SOleg Nesterov } 337706ba38a9SOleg Nesterov 3378f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3379f3421797STejun Heo const char *name) 3380b71ab8c2STejun Heo { 3381f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3382f3421797STejun Heo 3383f3421797STejun Heo if (max_active < 1 || max_active > lim) 3384044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3385f3421797STejun Heo max_active, name, 1, lim); 3386b71ab8c2STejun Heo 3387f3421797STejun Heo return clamp_val(max_active, 1, lim); 3388b71ab8c2STejun Heo } 3389b71ab8c2STejun Heo 3390b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 339197e37d7bSTejun Heo unsigned int flags, 33921e19ffc6STejun Heo int max_active, 3393eb13ba87SJohannes Berg struct lock_class_key *key, 3394b196be89STejun Heo const char *lock_name, ...) 33953af24433SOleg Nesterov { 3396b196be89STejun Heo va_list args, args1; 33973af24433SOleg Nesterov struct workqueue_struct *wq; 339849e3cf44STejun Heo struct pool_workqueue *pwq; 3399b196be89STejun Heo size_t namelen; 3400b196be89STejun Heo 3401b196be89STejun Heo /* determine namelen, allocate wq and format name */ 3402b196be89STejun Heo va_start(args, lock_name); 3403b196be89STejun Heo va_copy(args1, args); 3404b196be89STejun Heo namelen = vsnprintf(NULL, 0, fmt, args) + 1; 3405b196be89STejun Heo 3406b196be89STejun Heo wq = kzalloc(sizeof(*wq) + namelen, GFP_KERNEL); 3407b196be89STejun Heo if (!wq) 3408b196be89STejun Heo goto err; 3409b196be89STejun Heo 3410b196be89STejun Heo vsnprintf(wq->name, namelen, fmt, args1); 3411b196be89STejun Heo va_end(args); 3412b196be89STejun Heo va_end(args1); 34133af24433SOleg Nesterov 3414f3421797STejun Heo /* 34156370a6adSTejun Heo * Workqueues which may be used during memory reclaim should 34166370a6adSTejun Heo * have a rescuer to guarantee forward progress. 34176370a6adSTejun Heo */ 34186370a6adSTejun Heo if (flags & WQ_MEM_RECLAIM) 34196370a6adSTejun Heo flags |= WQ_RESCUER; 34206370a6adSTejun Heo 3421d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3422b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 34233af24433SOleg Nesterov 3424b196be89STejun Heo /* init wq */ 342597e37d7bSTejun Heo wq->flags = flags; 3426a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 342773f53c4aSTejun Heo mutex_init(&wq->flush_mutex); 3428112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 342930cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 343073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 343173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 3432493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 34333af24433SOleg Nesterov 3434eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3435cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 34363af24433SOleg Nesterov 343730cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 3438bdbc5dd7STejun Heo goto err; 3439bdbc5dd7STejun Heo 344076af4d93STejun Heo local_irq_disable(); 344149e3cf44STejun Heo for_each_pwq(pwq, wq) { 3442112202d9STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3443112202d9STejun Heo pwq->wq = wq; 3444112202d9STejun Heo pwq->flush_color = -1; 3445112202d9STejun Heo pwq->max_active = max_active; 3446112202d9STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 3447493a1724STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 3448e22bee78STejun Heo } 344976af4d93STejun Heo local_irq_enable(); 34501537663fSTejun Heo 3451e22bee78STejun Heo if (flags & WQ_RESCUER) { 3452e22bee78STejun Heo struct worker *rescuer; 3453e22bee78STejun Heo 3454e22bee78STejun Heo wq->rescuer = rescuer = alloc_worker(); 3455e22bee78STejun Heo if (!rescuer) 3456e22bee78STejun Heo goto err; 3457e22bee78STejun Heo 3458111c225aSTejun Heo rescuer->rescue_wq = wq; 3459111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 3460b196be89STejun Heo wq->name); 3461e22bee78STejun Heo if (IS_ERR(rescuer->task)) 3462e22bee78STejun Heo goto err; 3463e22bee78STejun Heo 3464e22bee78STejun Heo rescuer->task->flags |= PF_THREAD_BOUND; 3465e22bee78STejun Heo wake_up_process(rescuer->task); 34663af24433SOleg Nesterov } 34671537663fSTejun Heo 34683af24433SOleg Nesterov /* 3469a0a1a5fdSTejun Heo * workqueue_lock protects global freeze state and workqueues 3470a0a1a5fdSTejun Heo * list. Grab it, set max_active accordingly and add the new 3471a0a1a5fdSTejun Heo * workqueue to workqueues list. 34723af24433SOleg Nesterov */ 3473e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3474a0a1a5fdSTejun Heo 347558a69cb4STejun Heo if (workqueue_freezing && wq->flags & WQ_FREEZABLE) 347649e3cf44STejun Heo for_each_pwq(pwq, wq) 347749e3cf44STejun Heo pwq->max_active = 0; 3478a0a1a5fdSTejun Heo 34793af24433SOleg Nesterov list_add(&wq->list, &workqueues); 3480a0a1a5fdSTejun Heo 3481e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 34823af24433SOleg Nesterov 34833af24433SOleg Nesterov return wq; 34844690c4abSTejun Heo err: 34854690c4abSTejun Heo if (wq) { 3486112202d9STejun Heo free_pwqs(wq); 3487e22bee78STejun Heo kfree(wq->rescuer); 34884690c4abSTejun Heo kfree(wq); 34893af24433SOleg Nesterov } 34904690c4abSTejun Heo return NULL; 34911da177e4SLinus Torvalds } 3492d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 34931da177e4SLinus Torvalds 34943af24433SOleg Nesterov /** 34953af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 34963af24433SOleg Nesterov * @wq: target workqueue 34973af24433SOleg Nesterov * 34983af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 34993af24433SOleg Nesterov */ 35003af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 35013af24433SOleg Nesterov { 350249e3cf44STejun Heo struct pool_workqueue *pwq; 35033af24433SOleg Nesterov 35049c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 35059c5a2ba7STejun Heo drain_workqueue(wq); 3506c8efcc25STejun Heo 350776af4d93STejun Heo spin_lock_irq(&workqueue_lock); 350876af4d93STejun Heo 35096183c009STejun Heo /* sanity checks */ 351049e3cf44STejun Heo for_each_pwq(pwq, wq) { 35116183c009STejun Heo int i; 35126183c009STejun Heo 351376af4d93STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) { 351476af4d93STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) { 351576af4d93STejun Heo spin_unlock_irq(&workqueue_lock); 35166183c009STejun Heo return; 351776af4d93STejun Heo } 351876af4d93STejun Heo } 351976af4d93STejun Heo 35206183c009STejun Heo if (WARN_ON(pwq->nr_active) || 352176af4d93STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) { 352276af4d93STejun Heo spin_unlock_irq(&workqueue_lock); 35236183c009STejun Heo return; 35246183c009STejun Heo } 352576af4d93STejun Heo } 35266183c009STejun Heo 3527a0a1a5fdSTejun Heo /* 3528a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3529a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3530a0a1a5fdSTejun Heo */ 35313af24433SOleg Nesterov list_del(&wq->list); 353276af4d93STejun Heo 3533e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 35343af24433SOleg Nesterov 3535e22bee78STejun Heo if (wq->flags & WQ_RESCUER) { 3536e22bee78STejun Heo kthread_stop(wq->rescuer->task); 35378d9df9f0SXiaotian Feng kfree(wq->rescuer); 3538e22bee78STejun Heo } 3539e22bee78STejun Heo 354029c91e99STejun Heo /* 354129c91e99STejun Heo * We're the sole accessor of @wq at this point. Directly access 354229c91e99STejun Heo * the first pwq and put its pool. 354329c91e99STejun Heo */ 354429c91e99STejun Heo if (wq->flags & WQ_UNBOUND) { 354529c91e99STejun Heo pwq = list_first_entry(&wq->pwqs, struct pool_workqueue, 354629c91e99STejun Heo pwqs_node); 354729c91e99STejun Heo put_unbound_pool(pwq->pool); 354829c91e99STejun Heo } 3549112202d9STejun Heo free_pwqs(wq); 35503af24433SOleg Nesterov kfree(wq); 35513af24433SOleg Nesterov } 35523af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 35533af24433SOleg Nesterov 3554dcd989cbSTejun Heo /** 3555112202d9STejun Heo * pwq_set_max_active - adjust max_active of a pwq 3556112202d9STejun Heo * @pwq: target pool_workqueue 35579f4bd4cdSLai Jiangshan * @max_active: new max_active value. 35589f4bd4cdSLai Jiangshan * 3559112202d9STejun Heo * Set @pwq->max_active to @max_active and activate delayed works if 35609f4bd4cdSLai Jiangshan * increased. 35619f4bd4cdSLai Jiangshan * 35629f4bd4cdSLai Jiangshan * CONTEXT: 3563d565ed63STejun Heo * spin_lock_irq(pool->lock). 35649f4bd4cdSLai Jiangshan */ 3565112202d9STejun Heo static void pwq_set_max_active(struct pool_workqueue *pwq, int max_active) 35669f4bd4cdSLai Jiangshan { 3567112202d9STejun Heo pwq->max_active = max_active; 35689f4bd4cdSLai Jiangshan 3569112202d9STejun Heo while (!list_empty(&pwq->delayed_works) && 3570112202d9STejun Heo pwq->nr_active < pwq->max_active) 3571112202d9STejun Heo pwq_activate_first_delayed(pwq); 35729f4bd4cdSLai Jiangshan } 35739f4bd4cdSLai Jiangshan 35749f4bd4cdSLai Jiangshan /** 3575dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3576dcd989cbSTejun Heo * @wq: target workqueue 3577dcd989cbSTejun Heo * @max_active: new max_active value. 3578dcd989cbSTejun Heo * 3579dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3580dcd989cbSTejun Heo * 3581dcd989cbSTejun Heo * CONTEXT: 3582dcd989cbSTejun Heo * Don't call from IRQ context. 3583dcd989cbSTejun Heo */ 3584dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3585dcd989cbSTejun Heo { 358649e3cf44STejun Heo struct pool_workqueue *pwq; 3587dcd989cbSTejun Heo 3588f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3589dcd989cbSTejun Heo 3590e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3591dcd989cbSTejun Heo 3592dcd989cbSTejun Heo wq->saved_max_active = max_active; 3593dcd989cbSTejun Heo 359449e3cf44STejun Heo for_each_pwq(pwq, wq) { 3595112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3596dcd989cbSTejun Heo 3597e98d5b16STejun Heo spin_lock(&pool->lock); 3598dcd989cbSTejun Heo 359958a69cb4STejun Heo if (!(wq->flags & WQ_FREEZABLE) || 360035b6bb63STejun Heo !(pool->flags & POOL_FREEZING)) 3601112202d9STejun Heo pwq_set_max_active(pwq, max_active); 3602dcd989cbSTejun Heo 3603e98d5b16STejun Heo spin_unlock(&pool->lock); 3604dcd989cbSTejun Heo } 3605dcd989cbSTejun Heo 3606e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3607dcd989cbSTejun Heo } 3608dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3609dcd989cbSTejun Heo 3610dcd989cbSTejun Heo /** 3611dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3612dcd989cbSTejun Heo * @cpu: CPU in question 3613dcd989cbSTejun Heo * @wq: target workqueue 3614dcd989cbSTejun Heo * 3615dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3616dcd989cbSTejun Heo * no synchronization around this function and the test result is 3617dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3618dcd989cbSTejun Heo * 3619dcd989cbSTejun Heo * RETURNS: 3620dcd989cbSTejun Heo * %true if congested, %false otherwise. 3621dcd989cbSTejun Heo */ 3622d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 3623dcd989cbSTejun Heo { 36247fb98ea7STejun Heo struct pool_workqueue *pwq; 362576af4d93STejun Heo bool ret; 362676af4d93STejun Heo 362776af4d93STejun Heo preempt_disable(); 36287fb98ea7STejun Heo 36297fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 36307fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 36317fb98ea7STejun Heo else 36327fb98ea7STejun Heo pwq = first_pwq(wq); 3633dcd989cbSTejun Heo 363476af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 363576af4d93STejun Heo preempt_enable(); 363676af4d93STejun Heo 363776af4d93STejun Heo return ret; 3638dcd989cbSTejun Heo } 3639dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 3640dcd989cbSTejun Heo 3641dcd989cbSTejun Heo /** 3642dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 3643dcd989cbSTejun Heo * @work: the work to be tested 3644dcd989cbSTejun Heo * 3645dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 3646dcd989cbSTejun Heo * synchronization around this function and the test result is 3647dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3648dcd989cbSTejun Heo * 3649dcd989cbSTejun Heo * RETURNS: 3650dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 3651dcd989cbSTejun Heo */ 3652dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 3653dcd989cbSTejun Heo { 3654fa1b54e6STejun Heo struct worker_pool *pool; 3655dcd989cbSTejun Heo unsigned long flags; 3656dcd989cbSTejun Heo unsigned int ret = 0; 3657dcd989cbSTejun Heo 3658dcd989cbSTejun Heo if (work_pending(work)) 3659dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 3660038366c5SLai Jiangshan 3661fa1b54e6STejun Heo local_irq_save(flags); 3662fa1b54e6STejun Heo pool = get_work_pool(work); 3663038366c5SLai Jiangshan if (pool) { 3664fa1b54e6STejun Heo spin_lock(&pool->lock); 3665c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 3666dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 3667fa1b54e6STejun Heo spin_unlock(&pool->lock); 3668038366c5SLai Jiangshan } 3669fa1b54e6STejun Heo local_irq_restore(flags); 3670dcd989cbSTejun Heo 3671dcd989cbSTejun Heo return ret; 3672dcd989cbSTejun Heo } 3673dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 3674dcd989cbSTejun Heo 3675db7bccf4STejun Heo /* 3676db7bccf4STejun Heo * CPU hotplug. 3677db7bccf4STejun Heo * 3678e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 3679112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 3680706026c2STejun Heo * pool which make migrating pending and scheduled works very 3681e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 368294cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 3683e22bee78STejun Heo * blocked draining impractical. 3684e22bee78STejun Heo * 368524647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 3686628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 3687628c78e7STejun Heo * cpu comes back online. 3688db7bccf4STejun Heo */ 3689db7bccf4STejun Heo 3690706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 3691db7bccf4STejun Heo { 369238db41d9STejun Heo int cpu = smp_processor_id(); 36934ce62e9eSTejun Heo struct worker_pool *pool; 3694db7bccf4STejun Heo struct worker *worker; 3695db7bccf4STejun Heo int i; 3696db7bccf4STejun Heo 3697f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 36986183c009STejun Heo WARN_ON_ONCE(cpu != smp_processor_id()); 3699db7bccf4STejun Heo 370094cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 370194cf58bbSTejun Heo spin_lock_irq(&pool->lock); 3702e22bee78STejun Heo 3703f2d5a0eeSTejun Heo /* 370494cf58bbSTejun Heo * We've claimed all manager positions. Make all workers 370594cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 370694cf58bbSTejun Heo * except for the ones which are still executing works from 370794cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 370894cf58bbSTejun Heo * this, they may become diasporas. 3709f2d5a0eeSTejun Heo */ 37104ce62e9eSTejun Heo list_for_each_entry(worker, &pool->idle_list, entry) 3711403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3712db7bccf4STejun Heo 3713b67bfe0dSSasha Levin for_each_busy_worker(worker, i, pool) 3714403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 3715db7bccf4STejun Heo 371624647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 3717f2d5a0eeSTejun Heo 371894cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 371994cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 372094cf58bbSTejun Heo } 3721e22bee78STejun Heo 3722e22bee78STejun Heo /* 3723628c78e7STejun Heo * Call schedule() so that we cross rq->lock and thus can guarantee 3724628c78e7STejun Heo * sched callbacks see the %WORKER_UNBOUND flag. This is necessary 3725628c78e7STejun Heo * as scheduler callbacks may be invoked from other cpus. 3726628c78e7STejun Heo */ 3727628c78e7STejun Heo schedule(); 3728628c78e7STejun Heo 3729628c78e7STejun Heo /* 3730628c78e7STejun Heo * Sched callbacks are disabled now. Zap nr_running. After this, 3731628c78e7STejun Heo * nr_running stays zero and need_more_worker() and keep_working() 373238db41d9STejun Heo * are always true as long as the worklist is not empty. Pools on 373338db41d9STejun Heo * @cpu now behave as unbound (in terms of concurrency management) 373438db41d9STejun Heo * pools which are served by workers tied to the CPU. 3735628c78e7STejun Heo * 3736628c78e7STejun Heo * On return from this function, the current worker would trigger 3737628c78e7STejun Heo * unbound chain execution of pending work items if other workers 3738628c78e7STejun Heo * didn't already. 3739e22bee78STejun Heo */ 3740f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) 3741e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 3742db7bccf4STejun Heo } 3743db7bccf4STejun Heo 37448db25e78STejun Heo /* 37458db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 37468db25e78STejun Heo * This will be registered high priority CPU notifier. 37478db25e78STejun Heo */ 37489fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_up_callback(struct notifier_block *nfb, 37491da177e4SLinus Torvalds unsigned long action, 37501da177e4SLinus Torvalds void *hcpu) 37511da177e4SLinus Torvalds { 3752d84ff051STejun Heo int cpu = (unsigned long)hcpu; 37534ce62e9eSTejun Heo struct worker_pool *pool; 37541da177e4SLinus Torvalds 37558db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 37563af24433SOleg Nesterov case CPU_UP_PREPARE: 3757f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 37583ce63377STejun Heo struct worker *worker; 37593ce63377STejun Heo 37603ce63377STejun Heo if (pool->nr_workers) 37613ce63377STejun Heo continue; 37623ce63377STejun Heo 37633ce63377STejun Heo worker = create_worker(pool); 37643ce63377STejun Heo if (!worker) 37653ce63377STejun Heo return NOTIFY_BAD; 37663ce63377STejun Heo 3767d565ed63STejun Heo spin_lock_irq(&pool->lock); 37683ce63377STejun Heo start_worker(worker); 3769d565ed63STejun Heo spin_unlock_irq(&pool->lock); 37703af24433SOleg Nesterov } 37711da177e4SLinus Torvalds break; 37721da177e4SLinus Torvalds 377365758202STejun Heo case CPU_DOWN_FAILED: 377465758202STejun Heo case CPU_ONLINE: 3775f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 377694cf58bbSTejun Heo mutex_lock(&pool->assoc_mutex); 377794cf58bbSTejun Heo spin_lock_irq(&pool->lock); 377894cf58bbSTejun Heo 377924647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 378094cf58bbSTejun Heo rebind_workers(pool); 378194cf58bbSTejun Heo 378294cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 378394cf58bbSTejun Heo mutex_unlock(&pool->assoc_mutex); 378494cf58bbSTejun Heo } 37858db25e78STejun Heo break; 378665758202STejun Heo } 378765758202STejun Heo return NOTIFY_OK; 378865758202STejun Heo } 378965758202STejun Heo 379065758202STejun Heo /* 379165758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 379265758202STejun Heo * This will be registered as low priority CPU notifier. 379365758202STejun Heo */ 37949fdf9b73SLai Jiangshan static int __cpuinit workqueue_cpu_down_callback(struct notifier_block *nfb, 379565758202STejun Heo unsigned long action, 379665758202STejun Heo void *hcpu) 379765758202STejun Heo { 3798d84ff051STejun Heo int cpu = (unsigned long)hcpu; 37998db25e78STejun Heo struct work_struct unbind_work; 38008db25e78STejun Heo 380165758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 380265758202STejun Heo case CPU_DOWN_PREPARE: 38038db25e78STejun Heo /* unbinding should happen on the local CPU */ 3804706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 38057635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 38068db25e78STejun Heo flush_work(&unbind_work); 38078db25e78STejun Heo break; 380865758202STejun Heo } 380965758202STejun Heo return NOTIFY_OK; 381065758202STejun Heo } 381165758202STejun Heo 38122d3854a3SRusty Russell #ifdef CONFIG_SMP 38138ccad40dSRusty Russell 38142d3854a3SRusty Russell struct work_for_cpu { 3815ed48ece2STejun Heo struct work_struct work; 38162d3854a3SRusty Russell long (*fn)(void *); 38172d3854a3SRusty Russell void *arg; 38182d3854a3SRusty Russell long ret; 38192d3854a3SRusty Russell }; 38202d3854a3SRusty Russell 3821ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 38222d3854a3SRusty Russell { 3823ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 3824ed48ece2STejun Heo 38252d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 38262d3854a3SRusty Russell } 38272d3854a3SRusty Russell 38282d3854a3SRusty Russell /** 38292d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 38302d3854a3SRusty Russell * @cpu: the cpu to run on 38312d3854a3SRusty Russell * @fn: the function to run 38322d3854a3SRusty Russell * @arg: the function arg 38332d3854a3SRusty Russell * 383431ad9081SRusty Russell * This will return the value @fn returns. 383531ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 38366b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 38372d3854a3SRusty Russell */ 3838d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 38392d3854a3SRusty Russell { 3840ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 38412d3854a3SRusty Russell 3842ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 3843ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 3844ed48ece2STejun Heo flush_work(&wfc.work); 38452d3854a3SRusty Russell return wfc.ret; 38462d3854a3SRusty Russell } 38472d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 38482d3854a3SRusty Russell #endif /* CONFIG_SMP */ 38492d3854a3SRusty Russell 3850a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 3851e7577c50SRusty Russell 3852a0a1a5fdSTejun Heo /** 3853a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 3854a0a1a5fdSTejun Heo * 385558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 385658a69cb4STejun Heo * workqueues will queue new works to their frozen_works list instead of 3857706026c2STejun Heo * pool->worklist. 3858a0a1a5fdSTejun Heo * 3859a0a1a5fdSTejun Heo * CONTEXT: 3860d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3861a0a1a5fdSTejun Heo */ 3862a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 3863a0a1a5fdSTejun Heo { 386417116969STejun Heo struct worker_pool *pool; 386524b8a847STejun Heo struct workqueue_struct *wq; 386624b8a847STejun Heo struct pool_workqueue *pwq; 386717116969STejun Heo int id; 3868a0a1a5fdSTejun Heo 3869e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3870a0a1a5fdSTejun Heo 38716183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 3872a0a1a5fdSTejun Heo workqueue_freezing = true; 3873a0a1a5fdSTejun Heo 387424b8a847STejun Heo /* set FREEZING */ 387517116969STejun Heo for_each_pool(pool, id) { 3876e98d5b16STejun Heo spin_lock(&pool->lock); 387735b6bb63STejun Heo WARN_ON_ONCE(pool->flags & POOL_FREEZING); 387835b6bb63STejun Heo pool->flags |= POOL_FREEZING; 387924b8a847STejun Heo spin_unlock(&pool->lock); 38801da177e4SLinus Torvalds } 38818b03ae3cSTejun Heo 388224b8a847STejun Heo /* suppress further executions by setting max_active to zero */ 388324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 388424b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 388524b8a847STejun Heo continue; 388624b8a847STejun Heo 388724b8a847STejun Heo for_each_pwq(pwq, wq) { 388824b8a847STejun Heo spin_lock(&pwq->pool->lock); 388924b8a847STejun Heo pwq->max_active = 0; 389024b8a847STejun Heo spin_unlock(&pwq->pool->lock); 389124b8a847STejun Heo } 3892a1056305STejun Heo } 3893a0a1a5fdSTejun Heo 3894e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3895a0a1a5fdSTejun Heo } 3896a0a1a5fdSTejun Heo 3897a0a1a5fdSTejun Heo /** 389858a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 3899a0a1a5fdSTejun Heo * 3900a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 3901a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 3902a0a1a5fdSTejun Heo * 3903a0a1a5fdSTejun Heo * CONTEXT: 3904a0a1a5fdSTejun Heo * Grabs and releases workqueue_lock. 3905a0a1a5fdSTejun Heo * 3906a0a1a5fdSTejun Heo * RETURNS: 390758a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 390858a69cb4STejun Heo * is complete. 3909a0a1a5fdSTejun Heo */ 3910a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 3911a0a1a5fdSTejun Heo { 3912a0a1a5fdSTejun Heo bool busy = false; 391324b8a847STejun Heo struct workqueue_struct *wq; 391424b8a847STejun Heo struct pool_workqueue *pwq; 3915a0a1a5fdSTejun Heo 3916e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3917a0a1a5fdSTejun Heo 39186183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 3919a0a1a5fdSTejun Heo 392024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 392124b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 392224b8a847STejun Heo continue; 3923a0a1a5fdSTejun Heo /* 3924a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 3925a0a1a5fdSTejun Heo * to peek without lock. 3926a0a1a5fdSTejun Heo */ 392724b8a847STejun Heo for_each_pwq(pwq, wq) { 39286183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 3929112202d9STejun Heo if (pwq->nr_active) { 3930a0a1a5fdSTejun Heo busy = true; 3931a0a1a5fdSTejun Heo goto out_unlock; 3932a0a1a5fdSTejun Heo } 3933a0a1a5fdSTejun Heo } 3934a0a1a5fdSTejun Heo } 3935a0a1a5fdSTejun Heo out_unlock: 3936e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3937a0a1a5fdSTejun Heo return busy; 3938a0a1a5fdSTejun Heo } 3939a0a1a5fdSTejun Heo 3940a0a1a5fdSTejun Heo /** 3941a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 3942a0a1a5fdSTejun Heo * 3943a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 3944706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 3945a0a1a5fdSTejun Heo * 3946a0a1a5fdSTejun Heo * CONTEXT: 3947d565ed63STejun Heo * Grabs and releases workqueue_lock and pool->lock's. 3948a0a1a5fdSTejun Heo */ 3949a0a1a5fdSTejun Heo void thaw_workqueues(void) 3950a0a1a5fdSTejun Heo { 395124b8a847STejun Heo struct workqueue_struct *wq; 395224b8a847STejun Heo struct pool_workqueue *pwq; 395324b8a847STejun Heo struct worker_pool *pool; 395424b8a847STejun Heo int id; 3955a0a1a5fdSTejun Heo 3956e98d5b16STejun Heo spin_lock_irq(&workqueue_lock); 3957a0a1a5fdSTejun Heo 3958a0a1a5fdSTejun Heo if (!workqueue_freezing) 3959a0a1a5fdSTejun Heo goto out_unlock; 3960a0a1a5fdSTejun Heo 396124b8a847STejun Heo /* clear FREEZING */ 396224b8a847STejun Heo for_each_pool(pool, id) { 3963e98d5b16STejun Heo spin_lock(&pool->lock); 396435b6bb63STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_FREEZING)); 396535b6bb63STejun Heo pool->flags &= ~POOL_FREEZING; 3966e98d5b16STejun Heo spin_unlock(&pool->lock); 3967d565ed63STejun Heo } 396824b8a847STejun Heo 396924b8a847STejun Heo /* restore max_active and repopulate worklist */ 397024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 397124b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 397224b8a847STejun Heo continue; 397324b8a847STejun Heo 397424b8a847STejun Heo for_each_pwq(pwq, wq) { 397524b8a847STejun Heo spin_lock(&pwq->pool->lock); 397624b8a847STejun Heo pwq_set_max_active(pwq, wq->saved_max_active); 397724b8a847STejun Heo spin_unlock(&pwq->pool->lock); 397824b8a847STejun Heo } 397924b8a847STejun Heo } 398024b8a847STejun Heo 398124b8a847STejun Heo /* kick workers */ 398224b8a847STejun Heo for_each_pool(pool, id) { 398324b8a847STejun Heo spin_lock(&pool->lock); 398424b8a847STejun Heo wake_up_worker(pool); 398524b8a847STejun Heo spin_unlock(&pool->lock); 3986a0a1a5fdSTejun Heo } 3987a0a1a5fdSTejun Heo 3988a0a1a5fdSTejun Heo workqueue_freezing = false; 3989a0a1a5fdSTejun Heo out_unlock: 3990e98d5b16STejun Heo spin_unlock_irq(&workqueue_lock); 3991a0a1a5fdSTejun Heo } 3992a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 3993a0a1a5fdSTejun Heo 39946ee0578bSSuresh Siddha static int __init init_workqueues(void) 39951da177e4SLinus Torvalds { 39967a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 39977a4e344cSTejun Heo int i, cpu; 3998c34056a3STejun Heo 39997c3eed5cSTejun Heo /* make sure we have enough bits for OFFQ pool ID */ 40007c3eed5cSTejun Heo BUILD_BUG_ON((1LU << (BITS_PER_LONG - WORK_OFFQ_POOL_SHIFT)) < 40016be19588SLai Jiangshan WORK_CPU_END * NR_STD_WORKER_POOLS); 4002b5490077STejun Heo 4003e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 4004e904e6c2STejun Heo 4005e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 4006e904e6c2STejun Heo 400765758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 4008a5b4e57dSLai Jiangshan hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 40098b03ae3cSTejun Heo 4010706026c2STejun Heo /* initialize CPU pools */ 401129c91e99STejun Heo for_each_possible_cpu(cpu) { 40124ce62e9eSTejun Heo struct worker_pool *pool; 40138b03ae3cSTejun Heo 40147a4e344cSTejun Heo i = 0; 4015f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 40167a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 4017ec22ca5eSTejun Heo pool->cpu = cpu; 40187a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 40197a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 40207a4e344cSTejun Heo 40219daf9e67STejun Heo /* alloc pool ID */ 40229daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 40234ce62e9eSTejun Heo } 40248b03ae3cSTejun Heo } 40258b03ae3cSTejun Heo 4026e22bee78STejun Heo /* create the initial worker */ 402729c91e99STejun Heo for_each_online_cpu(cpu) { 40284ce62e9eSTejun Heo struct worker_pool *pool; 4029e22bee78STejun Heo 4030f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 40314ce62e9eSTejun Heo struct worker *worker; 40324ce62e9eSTejun Heo 403324647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 403424647570STejun Heo 4035bc2ae0f5STejun Heo worker = create_worker(pool); 4036e22bee78STejun Heo BUG_ON(!worker); 4037d565ed63STejun Heo spin_lock_irq(&pool->lock); 4038e22bee78STejun Heo start_worker(worker); 4039d565ed63STejun Heo spin_unlock_irq(&pool->lock); 4040e22bee78STejun Heo } 40414ce62e9eSTejun Heo } 4042e22bee78STejun Heo 404329c91e99STejun Heo /* create default unbound wq attrs */ 404429c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 404529c91e99STejun Heo struct workqueue_attrs *attrs; 404629c91e99STejun Heo 404729c91e99STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 404829c91e99STejun Heo 404929c91e99STejun Heo attrs->nice = std_nice[i]; 405029c91e99STejun Heo cpumask_setall(attrs->cpumask); 405129c91e99STejun Heo 405229c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 405329c91e99STejun Heo } 405429c91e99STejun Heo 4055d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 40561aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 4057d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 4058f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 4059f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 406024d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 406124d51addSTejun Heo WQ_FREEZABLE, 0); 40621aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 4063ae930e0fSTejun Heo !system_unbound_wq || !system_freezable_wq); 40646ee0578bSSuresh Siddha return 0; 40651da177e4SLinus Torvalds } 40666ee0578bSSuresh Siddha early_initcall(init_workqueues); 4067