11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <[email protected]> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <[email protected]> 101da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 20b11895c4SLibin * normal work items and the other for high priority ones) and some extra 21b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 22b11895c4SLibin * number of these backing pools is dynamic. 23c54fce6eSTejun Heo * 24c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 279984de1aSPaul Gortmaker #include <linux/export.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 291da177e4SLinus Torvalds #include <linux/sched.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/signal.h> 321da177e4SLinus Torvalds #include <linux/completion.h> 331da177e4SLinus Torvalds #include <linux/workqueue.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 361da177e4SLinus Torvalds #include <linux/notifier.h> 371da177e4SLinus Torvalds #include <linux/kthread.h> 381fa44ecaSJames Bottomley #include <linux/hardirq.h> 3946934023SChristoph Lameter #include <linux/mempolicy.h> 40341a5958SRafael J. Wysocki #include <linux/freezer.h> 41d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51e22bee78STejun Heo 52ea138446STejun Heo #include "workqueue_internal.h" 531da177e4SLinus Torvalds 54c8e55f36STejun Heo enum { 55bc2ae0f5STejun Heo /* 5624647570STejun Heo * worker_pool flags 57bc2ae0f5STejun Heo * 5824647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 59bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 60bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 61bc2ae0f5STejun Heo * is in effect. 62bc2ae0f5STejun Heo * 63bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 64bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6524647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 66bc2ae0f5STejun Heo * 67bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 6892f9c5c4SLai Jiangshan * attach_mutex to avoid changing binding state while 694736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 70bc2ae0f5STejun Heo */ 7124647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 72db7bccf4STejun Heo 73c8e55f36STejun Heo /* worker flags */ 74c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 75c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 76e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 77fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 78f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 79a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 80e22bee78STejun Heo 81a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 82a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 83db7bccf4STejun Heo 84e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 854ce62e9eSTejun Heo 8629c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 87c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 88db7bccf4STejun Heo 89e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 90e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 91e22bee78STejun Heo 923233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 933233cdbdSTejun Heo /* call for help after 10ms 943233cdbdSTejun Heo (min two ticks) */ 95e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 96e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 99e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1008698a745SDongsheng Yang * all cpus. Give MIN_NICE. 101e22bee78STejun Heo */ 1028698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1038698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 104ecf6881fSTejun Heo 105ecf6881fSTejun Heo WQ_NAME_LEN = 24, 106c8e55f36STejun Heo }; 107c8e55f36STejun Heo 1081da177e4SLinus Torvalds /* 1094690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1104690c4abSTejun Heo * 111e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 112e41e704bSTejun Heo * everyone else. 1134690c4abSTejun Heo * 114e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 115e22bee78STejun Heo * only be modified and accessed from the local cpu. 116e22bee78STejun Heo * 117d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1184690c4abSTejun Heo * 119d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 120d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 121d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 122d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 123e22bee78STejun Heo * 12492f9c5c4SLai Jiangshan * A: pool->attach_mutex protected. 125822d8405STejun Heo * 12668e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 12776af4d93STejun Heo * 12868e13a67SLai Jiangshan * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads. 1295bcab335STejun Heo * 1303c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1313c25a55dSLai Jiangshan * 132b5927605SLai Jiangshan * WR: wq->mutex protected for writes. Sched-RCU protected for reads. 1332e109a28STejun Heo * 1342e109a28STejun Heo * MD: wq_mayday_lock protected. 1354690c4abSTejun Heo */ 1364690c4abSTejun Heo 1372eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 138c34056a3STejun Heo 139bd7bdd43STejun Heo struct worker_pool { 140d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 141d84ff051STejun Heo int cpu; /* I: the associated cpu */ 142f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1439daf9e67STejun Heo int id; /* I: pool ID */ 14411ebea50STejun Heo unsigned int flags; /* X: flags */ 145bd7bdd43STejun Heo 146bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 147bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 148ea1abd61SLai Jiangshan 149ea1abd61SLai Jiangshan /* nr_idle includes the ones off idle_list for rebinding */ 150bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 151bd7bdd43STejun Heo 152bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 153bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 154bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 155bd7bdd43STejun Heo 156c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 157c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 158c9e7cf27STejun Heo /* L: hash of busy workers */ 159c9e7cf27STejun Heo 160bc3a1afcSTejun Heo /* see manage_workers() for details on the two manager mutexes */ 16134a06bd6STejun Heo struct mutex manager_arb; /* manager arbitration */ 1622607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 16392f9c5c4SLai Jiangshan struct mutex attach_mutex; /* attach/detach exclusion */ 16492f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 16560f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 166e19e397aSTejun Heo 1677cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 168e19e397aSTejun Heo 1697a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 17068e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 17168e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1727a4e344cSTejun Heo 173e19e397aSTejun Heo /* 174e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 175e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 176e19e397aSTejun Heo * cacheline. 177e19e397aSTejun Heo */ 178e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 17929c91e99STejun Heo 18029c91e99STejun Heo /* 18129c91e99STejun Heo * Destruction of pool is sched-RCU protected to allow dereferences 18229c91e99STejun Heo * from get_work_pool(). 18329c91e99STejun Heo */ 18429c91e99STejun Heo struct rcu_head rcu; 1858b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1868b03ae3cSTejun Heo 1878b03ae3cSTejun Heo /* 188112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 189112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 190112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 191112202d9STejun Heo * number of flag bits. 1921da177e4SLinus Torvalds */ 193112202d9STejun Heo struct pool_workqueue { 194bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 1954690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 19673f53c4aSTejun Heo int work_color; /* L: current color */ 19773f53c4aSTejun Heo int flush_color; /* L: flushing color */ 1988864b4e5STejun Heo int refcnt; /* L: reference count */ 19973f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20073f53c4aSTejun Heo /* L: nr of in_flight works */ 2011e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 202a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2031e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2043c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2052e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2068864b4e5STejun Heo 2078864b4e5STejun Heo /* 2088864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2098864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 2108864b4e5STejun Heo * itself is also sched-RCU protected so that the first pwq can be 211b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2128864b4e5STejun Heo */ 2138864b4e5STejun Heo struct work_struct unbound_release_work; 2148864b4e5STejun Heo struct rcu_head rcu; 215e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* 21873f53c4aSTejun Heo * Structure used to wait for workqueue flush. 21973f53c4aSTejun Heo */ 22073f53c4aSTejun Heo struct wq_flusher { 2213c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2223c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 22373f53c4aSTejun Heo struct completion done; /* flush completion */ 22473f53c4aSTejun Heo }; 2251da177e4SLinus Torvalds 226226223abSTejun Heo struct wq_device; 227226223abSTejun Heo 22873f53c4aSTejun Heo /* 229c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 230c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2311da177e4SLinus Torvalds */ 2321da177e4SLinus Torvalds struct workqueue_struct { 2333c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 234e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 23573f53c4aSTejun Heo 2363c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2373c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2383c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 239112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2403c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2413c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2423c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 24373f53c4aSTejun Heo 2442e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 245e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 246e22bee78STejun Heo 24787fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 248a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 249226223abSTejun Heo 2506029a918STejun Heo struct workqueue_attrs *unbound_attrs; /* WQ: only for unbound wqs */ 2514c16bd32STejun Heo struct pool_workqueue *dfl_pwq; /* WQ: only for unbound wqs */ 2526029a918STejun Heo 253226223abSTejun Heo #ifdef CONFIG_SYSFS 254226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 255226223abSTejun Heo #endif 2564e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2574e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2584e6045f1SJohannes Berg #endif 259ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2602728fd2fSTejun Heo 261e2dca7adSTejun Heo /* 262e2dca7adSTejun Heo * Destruction of workqueue_struct is sched-RCU protected to allow 263e2dca7adSTejun Heo * walking the workqueues list without grabbing wq_pool_mutex. 264e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 265e2dca7adSTejun Heo */ 266e2dca7adSTejun Heo struct rcu_head rcu; 267e2dca7adSTejun Heo 2682728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 2692728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 2702728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 271df2d5ae4STejun Heo struct pool_workqueue __rcu *numa_pwq_tbl[]; /* FR: unbound pwqs indexed by node */ 2721da177e4SLinus Torvalds }; 2731da177e4SLinus Torvalds 274e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 275e904e6c2STejun Heo 276bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 277bce90380STejun Heo /* possible CPUs of each node */ 278bce90380STejun Heo 279d55262c4STejun Heo static bool wq_disable_numa; 280d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 281d55262c4STejun Heo 282cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 283*552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 284cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 285cee22a15SViresh Kumar 286bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 287bce90380STejun Heo 2884c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 2894c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 2904c16bd32STejun Heo 29168e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 2922e109a28STejun Heo static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 2935bcab335STejun Heo 294e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 29568e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 2967d19c5ceSTejun Heo 2977d19c5ceSTejun Heo /* the per-cpu worker pools */ 2987d19c5ceSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 2997d19c5ceSTejun Heo cpu_worker_pools); 3007d19c5ceSTejun Heo 30168e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3027d19c5ceSTejun Heo 30368e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 30429c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 30529c91e99STejun Heo 306c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 30729c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 30829c91e99STejun Heo 3098a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3108a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3118a2b7538STejun Heo 312d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 313ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 314044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3151aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 316044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 317d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 318044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 319f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 320044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 32124d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3220668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3230668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3240668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3250668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 326d320c038STejun Heo 3277d19c5ceSTejun Heo static int worker_thread(void *__worker); 3287d19c5ceSTejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 3297d19c5ceSTejun Heo const struct workqueue_attrs *from); 3306ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 3317d19c5ceSTejun Heo 33297bd2347STejun Heo #define CREATE_TRACE_POINTS 33397bd2347STejun Heo #include <trace/events/workqueue.h> 33497bd2347STejun Heo 33568e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 3365bcab335STejun Heo rcu_lockdep_assert(rcu_read_lock_sched_held() || \ 33768e13a67SLai Jiangshan lockdep_is_held(&wq_pool_mutex), \ 33868e13a67SLai Jiangshan "sched RCU or wq_pool_mutex should be held") 3395bcab335STejun Heo 340b09f4fd3SLai Jiangshan #define assert_rcu_or_wq_mutex(wq) \ 34176af4d93STejun Heo rcu_lockdep_assert(rcu_read_lock_sched_held() || \ 342b5927605SLai Jiangshan lockdep_is_held(&wq->mutex), \ 343b09f4fd3SLai Jiangshan "sched RCU or wq->mutex should be held") 34476af4d93STejun Heo 345f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 346f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 347f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 3487a62c2c8STejun Heo (pool)++) 3494ce62e9eSTejun Heo 35049e3cf44STejun Heo /** 35117116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 35217116969STejun Heo * @pool: iteration cursor 353611c92a0STejun Heo * @pi: integer used for iteration 354fa1b54e6STejun Heo * 35568e13a67SLai Jiangshan * This must be called either with wq_pool_mutex held or sched RCU read 35668e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 35768e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 358fa1b54e6STejun Heo * 359fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 360fa1b54e6STejun Heo * ignored. 36117116969STejun Heo */ 362611c92a0STejun Heo #define for_each_pool(pool, pi) \ 363611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 36468e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 365fa1b54e6STejun Heo else 36617116969STejun Heo 36717116969STejun Heo /** 368822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 369822d8405STejun Heo * @worker: iteration cursor 370822d8405STejun Heo * @pool: worker_pool to iterate workers of 371822d8405STejun Heo * 37292f9c5c4SLai Jiangshan * This must be called with @pool->attach_mutex. 373822d8405STejun Heo * 374822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 375822d8405STejun Heo * ignored. 376822d8405STejun Heo */ 377da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 378da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 37992f9c5c4SLai Jiangshan if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \ 380822d8405STejun Heo else 381822d8405STejun Heo 382822d8405STejun Heo /** 38349e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 38449e3cf44STejun Heo * @pwq: iteration cursor 38549e3cf44STejun Heo * @wq: the target workqueue 38676af4d93STejun Heo * 387b09f4fd3SLai Jiangshan * This must be called either with wq->mutex held or sched RCU read locked. 388794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 389794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 39076af4d93STejun Heo * 39176af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 39276af4d93STejun Heo * ignored. 39349e3cf44STejun Heo */ 39449e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 39576af4d93STejun Heo list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ 396b09f4fd3SLai Jiangshan if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \ 39776af4d93STejun Heo else 398f3421797STejun Heo 399dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 400dc186ad7SThomas Gleixner 401dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 402dc186ad7SThomas Gleixner 40399777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 40499777288SStanislaw Gruszka { 40599777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 40699777288SStanislaw Gruszka } 40799777288SStanislaw Gruszka 408dc186ad7SThomas Gleixner /* 409dc186ad7SThomas Gleixner * fixup_init is called when: 410dc186ad7SThomas Gleixner * - an active object is initialized 411dc186ad7SThomas Gleixner */ 412dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 413dc186ad7SThomas Gleixner { 414dc186ad7SThomas Gleixner struct work_struct *work = addr; 415dc186ad7SThomas Gleixner 416dc186ad7SThomas Gleixner switch (state) { 417dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 418dc186ad7SThomas Gleixner cancel_work_sync(work); 419dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 420dc186ad7SThomas Gleixner return 1; 421dc186ad7SThomas Gleixner default: 422dc186ad7SThomas Gleixner return 0; 423dc186ad7SThomas Gleixner } 424dc186ad7SThomas Gleixner } 425dc186ad7SThomas Gleixner 426dc186ad7SThomas Gleixner /* 427dc186ad7SThomas Gleixner * fixup_activate is called when: 428dc186ad7SThomas Gleixner * - an active object is activated 429dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 430dc186ad7SThomas Gleixner */ 431dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 432dc186ad7SThomas Gleixner { 433dc186ad7SThomas Gleixner struct work_struct *work = addr; 434dc186ad7SThomas Gleixner 435dc186ad7SThomas Gleixner switch (state) { 436dc186ad7SThomas Gleixner 437dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 438dc186ad7SThomas Gleixner /* 439dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 440dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 441dc186ad7SThomas Gleixner * is tracked in the object tracker. 442dc186ad7SThomas Gleixner */ 44322df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 444dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 445dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 446dc186ad7SThomas Gleixner return 0; 447dc186ad7SThomas Gleixner } 448dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 449dc186ad7SThomas Gleixner return 0; 450dc186ad7SThomas Gleixner 451dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 452dc186ad7SThomas Gleixner WARN_ON(1); 453dc186ad7SThomas Gleixner 454dc186ad7SThomas Gleixner default: 455dc186ad7SThomas Gleixner return 0; 456dc186ad7SThomas Gleixner } 457dc186ad7SThomas Gleixner } 458dc186ad7SThomas Gleixner 459dc186ad7SThomas Gleixner /* 460dc186ad7SThomas Gleixner * fixup_free is called when: 461dc186ad7SThomas Gleixner * - an active object is freed 462dc186ad7SThomas Gleixner */ 463dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 464dc186ad7SThomas Gleixner { 465dc186ad7SThomas Gleixner struct work_struct *work = addr; 466dc186ad7SThomas Gleixner 467dc186ad7SThomas Gleixner switch (state) { 468dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 469dc186ad7SThomas Gleixner cancel_work_sync(work); 470dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 471dc186ad7SThomas Gleixner return 1; 472dc186ad7SThomas Gleixner default: 473dc186ad7SThomas Gleixner return 0; 474dc186ad7SThomas Gleixner } 475dc186ad7SThomas Gleixner } 476dc186ad7SThomas Gleixner 477dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 478dc186ad7SThomas Gleixner .name = "work_struct", 47999777288SStanislaw Gruszka .debug_hint = work_debug_hint, 480dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 481dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 482dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 483dc186ad7SThomas Gleixner }; 484dc186ad7SThomas Gleixner 485dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 486dc186ad7SThomas Gleixner { 487dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 488dc186ad7SThomas Gleixner } 489dc186ad7SThomas Gleixner 490dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 491dc186ad7SThomas Gleixner { 492dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 493dc186ad7SThomas Gleixner } 494dc186ad7SThomas Gleixner 495dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 496dc186ad7SThomas Gleixner { 497dc186ad7SThomas Gleixner if (onstack) 498dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 499dc186ad7SThomas Gleixner else 500dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 501dc186ad7SThomas Gleixner } 502dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 503dc186ad7SThomas Gleixner 504dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 505dc186ad7SThomas Gleixner { 506dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 507dc186ad7SThomas Gleixner } 508dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 509dc186ad7SThomas Gleixner 510ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 511ea2e64f2SThomas Gleixner { 512ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 513ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 514ea2e64f2SThomas Gleixner } 515ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 516ea2e64f2SThomas Gleixner 517dc186ad7SThomas Gleixner #else 518dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 519dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 520dc186ad7SThomas Gleixner #endif 521dc186ad7SThomas Gleixner 5224e8b22bdSLi Bin /** 5234e8b22bdSLi Bin * worker_pool_assign_id - allocate ID and assing it to @pool 5244e8b22bdSLi Bin * @pool: the pool pointer of interest 5254e8b22bdSLi Bin * 5264e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5274e8b22bdSLi Bin * successfully, -errno on failure. 5284e8b22bdSLi Bin */ 5299daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5309daf9e67STejun Heo { 5319daf9e67STejun Heo int ret; 5329daf9e67STejun Heo 53368e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5345bcab335STejun Heo 5354e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5364e8b22bdSLi Bin GFP_KERNEL); 537229641a6STejun Heo if (ret >= 0) { 538e68035fbSTejun Heo pool->id = ret; 539229641a6STejun Heo return 0; 540229641a6STejun Heo } 5419daf9e67STejun Heo return ret; 5429daf9e67STejun Heo } 5439daf9e67STejun Heo 54476af4d93STejun Heo /** 545df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 546df2d5ae4STejun Heo * @wq: the target workqueue 547df2d5ae4STejun Heo * @node: the node ID 548df2d5ae4STejun Heo * 549df2d5ae4STejun Heo * This must be called either with pwq_lock held or sched RCU read locked. 550df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 551df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 552d185af30SYacine Belkadi * 553d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 554df2d5ae4STejun Heo */ 555df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 556df2d5ae4STejun Heo int node) 557df2d5ae4STejun Heo { 558df2d5ae4STejun Heo assert_rcu_or_wq_mutex(wq); 559df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 560df2d5ae4STejun Heo } 561df2d5ae4STejun Heo 56273f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 56373f53c4aSTejun Heo { 56473f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 56573f53c4aSTejun Heo } 56673f53c4aSTejun Heo 56773f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 56873f53c4aSTejun Heo { 56973f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 57073f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 57173f53c4aSTejun Heo } 57273f53c4aSTejun Heo 57373f53c4aSTejun Heo static int work_next_color(int color) 57473f53c4aSTejun Heo { 57573f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 576a848e3b6SOleg Nesterov } 577a848e3b6SOleg Nesterov 5784594bf15SDavid Howells /* 579112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 580112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 5817c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 5827a22ad75STejun Heo * 583112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 584112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 585bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 586bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 5877a22ad75STejun Heo * 588112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 5897c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 590112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 5917c3eed5cSTejun Heo * available only while the work item is queued. 592bbb68dfaSTejun Heo * 593bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 594bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 595bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 596bbb68dfaSTejun Heo * try to steal the PENDING bit. 5974594bf15SDavid Howells */ 5987a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 5997a22ad75STejun Heo unsigned long flags) 6007a22ad75STejun Heo { 6016183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6027a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6037a22ad75STejun Heo } 6047a22ad75STejun Heo 605112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6064690c4abSTejun Heo unsigned long extra_flags) 607365970a1SDavid Howells { 608112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 609112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 610365970a1SDavid Howells } 611365970a1SDavid Howells 6124468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6134468a00fSLai Jiangshan int pool_id) 6144468a00fSLai Jiangshan { 6154468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6164468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6174468a00fSLai Jiangshan } 6184468a00fSLai Jiangshan 6197c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6207c3eed5cSTejun Heo int pool_id) 6214d707b9fSOleg Nesterov { 62223657bb1STejun Heo /* 62323657bb1STejun Heo * The following wmb is paired with the implied mb in 62423657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 62523657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 62623657bb1STejun Heo * owner. 62723657bb1STejun Heo */ 62823657bb1STejun Heo smp_wmb(); 6297c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 6304d707b9fSOleg Nesterov } 6314d707b9fSOleg Nesterov 6327a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 633365970a1SDavid Howells { 6347c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 6357c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 6367a22ad75STejun Heo } 6377a22ad75STejun Heo 638112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 6397a22ad75STejun Heo { 640e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6417a22ad75STejun Heo 642112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 643e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 644e120153dSTejun Heo else 645e120153dSTejun Heo return NULL; 6467a22ad75STejun Heo } 6477a22ad75STejun Heo 6487c3eed5cSTejun Heo /** 6497c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 6507c3eed5cSTejun Heo * @work: the work item of interest 6517c3eed5cSTejun Heo * 65268e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 65368e13a67SLai Jiangshan * access under sched-RCU read lock. As such, this function should be 65468e13a67SLai Jiangshan * called under wq_pool_mutex or with preemption disabled. 655fa1b54e6STejun Heo * 656fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 657fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 658fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 659fa1b54e6STejun Heo * returned pool is and stays online. 660d185af30SYacine Belkadi * 661d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 6627c3eed5cSTejun Heo */ 6637c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 6647a22ad75STejun Heo { 665e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6667c3eed5cSTejun Heo int pool_id; 6677a22ad75STejun Heo 66868e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 669fa1b54e6STejun Heo 670112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 671112202d9STejun Heo return ((struct pool_workqueue *) 6727c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 6737a22ad75STejun Heo 6747c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 6757c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 6767a22ad75STejun Heo return NULL; 6777a22ad75STejun Heo 678fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 6797c3eed5cSTejun Heo } 6807c3eed5cSTejun Heo 6817c3eed5cSTejun Heo /** 6827c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 6837c3eed5cSTejun Heo * @work: the work item of interest 6847c3eed5cSTejun Heo * 685d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 6867c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 6877c3eed5cSTejun Heo */ 6887c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 6897c3eed5cSTejun Heo { 69054d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 6917c3eed5cSTejun Heo 692112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 693112202d9STejun Heo return ((struct pool_workqueue *) 69454d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 69554d5b7d0SLai Jiangshan 69654d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 6977c3eed5cSTejun Heo } 6987c3eed5cSTejun Heo 699bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 700bbb68dfaSTejun Heo { 7017c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 702bbb68dfaSTejun Heo 7037c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7047c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 705bbb68dfaSTejun Heo } 706bbb68dfaSTejun Heo 707bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 708bbb68dfaSTejun Heo { 709bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 710bbb68dfaSTejun Heo 711112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 712bbb68dfaSTejun Heo } 713bbb68dfaSTejun Heo 714e22bee78STejun Heo /* 7153270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7163270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 717d565ed63STejun Heo * they're being called with pool->lock held. 718e22bee78STejun Heo */ 719e22bee78STejun Heo 72063d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 721649027d7STejun Heo { 722e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 723649027d7STejun Heo } 724649027d7STejun Heo 725e22bee78STejun Heo /* 726e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 727e22bee78STejun Heo * running workers. 728974271c4STejun Heo * 729974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 730706026c2STejun Heo * function will always return %true for unbound pools as long as the 731974271c4STejun Heo * worklist isn't empty. 732e22bee78STejun Heo */ 73363d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 734e22bee78STejun Heo { 73563d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 736e22bee78STejun Heo } 737e22bee78STejun Heo 738e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 73963d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 740e22bee78STejun Heo { 74163d95a91STejun Heo return pool->nr_idle; 742e22bee78STejun Heo } 743e22bee78STejun Heo 744e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 74563d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 746e22bee78STejun Heo { 747e19e397aSTejun Heo return !list_empty(&pool->worklist) && 748e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 749e22bee78STejun Heo } 750e22bee78STejun Heo 751e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 75263d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 753e22bee78STejun Heo { 75463d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 755e22bee78STejun Heo } 756e22bee78STejun Heo 757e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 75863d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 759e22bee78STejun Heo { 76034a06bd6STejun Heo bool managing = mutex_is_locked(&pool->manager_arb); 76163d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 76263d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 763e22bee78STejun Heo 764e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 765e22bee78STejun Heo } 766e22bee78STejun Heo 767e22bee78STejun Heo /* 768e22bee78STejun Heo * Wake up functions. 769e22bee78STejun Heo */ 770e22bee78STejun Heo 7711037de36SLai Jiangshan /* Return the first idle worker. Safe with preemption disabled */ 7721037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 7737e11629dSTejun Heo { 77463d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 7757e11629dSTejun Heo return NULL; 7767e11629dSTejun Heo 77763d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 7787e11629dSTejun Heo } 7797e11629dSTejun Heo 7807e11629dSTejun Heo /** 7817e11629dSTejun Heo * wake_up_worker - wake up an idle worker 78263d95a91STejun Heo * @pool: worker pool to wake worker from 7837e11629dSTejun Heo * 78463d95a91STejun Heo * Wake up the first idle worker of @pool. 7857e11629dSTejun Heo * 7867e11629dSTejun Heo * CONTEXT: 787d565ed63STejun Heo * spin_lock_irq(pool->lock). 7887e11629dSTejun Heo */ 78963d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 7907e11629dSTejun Heo { 7911037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 7927e11629dSTejun Heo 7937e11629dSTejun Heo if (likely(worker)) 7947e11629dSTejun Heo wake_up_process(worker->task); 7957e11629dSTejun Heo } 7967e11629dSTejun Heo 7974690c4abSTejun Heo /** 798e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 799e22bee78STejun Heo * @task: task waking up 800e22bee78STejun Heo * @cpu: CPU @task is waking up to 801e22bee78STejun Heo * 802e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 803e22bee78STejun Heo * being awoken. 804e22bee78STejun Heo * 805e22bee78STejun Heo * CONTEXT: 806e22bee78STejun Heo * spin_lock_irq(rq->lock) 807e22bee78STejun Heo */ 808d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu) 809e22bee78STejun Heo { 810e22bee78STejun Heo struct worker *worker = kthread_data(task); 811e22bee78STejun Heo 81236576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 813ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 814e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 815e22bee78STejun Heo } 81636576000SJoonsoo Kim } 817e22bee78STejun Heo 818e22bee78STejun Heo /** 819e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 820e22bee78STejun Heo * @task: task going to sleep 821e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 822e22bee78STejun Heo * 823e22bee78STejun Heo * This function is called during schedule() when a busy worker is 824e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 825e22bee78STejun Heo * returning pointer to its task. 826e22bee78STejun Heo * 827e22bee78STejun Heo * CONTEXT: 828e22bee78STejun Heo * spin_lock_irq(rq->lock) 829e22bee78STejun Heo * 830d185af30SYacine Belkadi * Return: 831e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 832e22bee78STejun Heo */ 833d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu) 834e22bee78STejun Heo { 835e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 836111c225aSTejun Heo struct worker_pool *pool; 837e22bee78STejun Heo 838111c225aSTejun Heo /* 839111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 840111c225aSTejun Heo * workers, also reach here, let's not access anything before 841111c225aSTejun Heo * checking NOT_RUNNING. 842111c225aSTejun Heo */ 8432d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 844e22bee78STejun Heo return NULL; 845e22bee78STejun Heo 846111c225aSTejun Heo pool = worker->pool; 847111c225aSTejun Heo 848e22bee78STejun Heo /* this can only happen on the local cpu */ 84992b69f50SLai Jiangshan if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu)) 8506183c009STejun Heo return NULL; 851e22bee78STejun Heo 852e22bee78STejun Heo /* 853e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 854e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 855e22bee78STejun Heo * Please read comment there. 856e22bee78STejun Heo * 857628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 858628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 859628c78e7STejun Heo * disabled, which in turn means that none else could be 860d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 861628c78e7STejun Heo * lock is safe. 862e22bee78STejun Heo */ 863e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 864e19e397aSTejun Heo !list_empty(&pool->worklist)) 8651037de36SLai Jiangshan to_wakeup = first_idle_worker(pool); 866e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 867e22bee78STejun Heo } 868e22bee78STejun Heo 869e22bee78STejun Heo /** 870e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 871cb444766STejun Heo * @worker: self 872d302f017STejun Heo * @flags: flags to set 873d302f017STejun Heo * 874228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 875d302f017STejun Heo * 876cb444766STejun Heo * CONTEXT: 877d565ed63STejun Heo * spin_lock_irq(pool->lock) 878d302f017STejun Heo */ 879228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 880d302f017STejun Heo { 881bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 882e22bee78STejun Heo 883cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 884cb444766STejun Heo 885228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 886e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 887e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 888e19e397aSTejun Heo atomic_dec(&pool->nr_running); 889e22bee78STejun Heo } 890e22bee78STejun Heo 891d302f017STejun Heo worker->flags |= flags; 892d302f017STejun Heo } 893d302f017STejun Heo 894d302f017STejun Heo /** 895e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 896cb444766STejun Heo * @worker: self 897d302f017STejun Heo * @flags: flags to clear 898d302f017STejun Heo * 899e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 900d302f017STejun Heo * 901cb444766STejun Heo * CONTEXT: 902d565ed63STejun Heo * spin_lock_irq(pool->lock) 903d302f017STejun Heo */ 904d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 905d302f017STejun Heo { 90663d95a91STejun Heo struct worker_pool *pool = worker->pool; 907e22bee78STejun Heo unsigned int oflags = worker->flags; 908e22bee78STejun Heo 909cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 910cb444766STejun Heo 911d302f017STejun Heo worker->flags &= ~flags; 912e22bee78STejun Heo 91342c025f3STejun Heo /* 91442c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 91542c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 91642c025f3STejun Heo * of multiple flags, not a single flag. 91742c025f3STejun Heo */ 918e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 919e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 920e19e397aSTejun Heo atomic_inc(&pool->nr_running); 921d302f017STejun Heo } 922d302f017STejun Heo 923d302f017STejun Heo /** 9248cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 925c9e7cf27STejun Heo * @pool: pool of interest 9268cca0eeaSTejun Heo * @work: work to find worker for 9278cca0eeaSTejun Heo * 928c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 929c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 930a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 931a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 932a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 933a2c1c57bSTejun Heo * being executed. 934a2c1c57bSTejun Heo * 935a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 936a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 937a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 938a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 939a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 940a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 941a2c1c57bSTejun Heo * 942c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 943c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 944c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 945c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 946c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 947c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 9488cca0eeaSTejun Heo * 9498cca0eeaSTejun Heo * CONTEXT: 950d565ed63STejun Heo * spin_lock_irq(pool->lock). 9518cca0eeaSTejun Heo * 952d185af30SYacine Belkadi * Return: 953d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 9548cca0eeaSTejun Heo * otherwise. 9558cca0eeaSTejun Heo */ 956c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 9578cca0eeaSTejun Heo struct work_struct *work) 9588cca0eeaSTejun Heo { 95942f8570fSSasha Levin struct worker *worker; 96042f8570fSSasha Levin 961b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 962a2c1c57bSTejun Heo (unsigned long)work) 963a2c1c57bSTejun Heo if (worker->current_work == work && 964a2c1c57bSTejun Heo worker->current_func == work->func) 96542f8570fSSasha Levin return worker; 96642f8570fSSasha Levin 96742f8570fSSasha Levin return NULL; 9688cca0eeaSTejun Heo } 9698cca0eeaSTejun Heo 9708cca0eeaSTejun Heo /** 971bf4ede01STejun Heo * move_linked_works - move linked works to a list 972bf4ede01STejun Heo * @work: start of series of works to be scheduled 973bf4ede01STejun Heo * @head: target list to append @work to 974bf4ede01STejun Heo * @nextp: out paramter for nested worklist walking 975bf4ede01STejun Heo * 976bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 977bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 978bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 979bf4ede01STejun Heo * 980bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 981bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 982bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 983bf4ede01STejun Heo * 984bf4ede01STejun Heo * CONTEXT: 985d565ed63STejun Heo * spin_lock_irq(pool->lock). 986bf4ede01STejun Heo */ 987bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 988bf4ede01STejun Heo struct work_struct **nextp) 989bf4ede01STejun Heo { 990bf4ede01STejun Heo struct work_struct *n; 991bf4ede01STejun Heo 992bf4ede01STejun Heo /* 993bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 994bf4ede01STejun Heo * use NULL for list head. 995bf4ede01STejun Heo */ 996bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 997bf4ede01STejun Heo list_move_tail(&work->entry, head); 998bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 999bf4ede01STejun Heo break; 1000bf4ede01STejun Heo } 1001bf4ede01STejun Heo 1002bf4ede01STejun Heo /* 1003bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1004bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1005bf4ede01STejun Heo * needs to be updated. 1006bf4ede01STejun Heo */ 1007bf4ede01STejun Heo if (nextp) 1008bf4ede01STejun Heo *nextp = n; 1009bf4ede01STejun Heo } 1010bf4ede01STejun Heo 10118864b4e5STejun Heo /** 10128864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 10138864b4e5STejun Heo * @pwq: pool_workqueue to get 10148864b4e5STejun Heo * 10158864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 10168864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 10178864b4e5STejun Heo */ 10188864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 10198864b4e5STejun Heo { 10208864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10218864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 10228864b4e5STejun Heo pwq->refcnt++; 10238864b4e5STejun Heo } 10248864b4e5STejun Heo 10258864b4e5STejun Heo /** 10268864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 10278864b4e5STejun Heo * @pwq: pool_workqueue to put 10288864b4e5STejun Heo * 10298864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 10308864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 10318864b4e5STejun Heo */ 10328864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 10338864b4e5STejun Heo { 10348864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10358864b4e5STejun Heo if (likely(--pwq->refcnt)) 10368864b4e5STejun Heo return; 10378864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 10388864b4e5STejun Heo return; 10398864b4e5STejun Heo /* 10408864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 10418864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 10428864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 10438864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 10448864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 10458864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 10468864b4e5STejun Heo */ 10478864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 10488864b4e5STejun Heo } 10498864b4e5STejun Heo 1050dce90d47STejun Heo /** 1051dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1052dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1053dce90d47STejun Heo * 1054dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1055dce90d47STejun Heo */ 1056dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1057dce90d47STejun Heo { 1058dce90d47STejun Heo if (pwq) { 1059dce90d47STejun Heo /* 1060dce90d47STejun Heo * As both pwqs and pools are sched-RCU protected, the 1061dce90d47STejun Heo * following lock operations are safe. 1062dce90d47STejun Heo */ 1063dce90d47STejun Heo spin_lock_irq(&pwq->pool->lock); 1064dce90d47STejun Heo put_pwq(pwq); 1065dce90d47STejun Heo spin_unlock_irq(&pwq->pool->lock); 1066dce90d47STejun Heo } 1067dce90d47STejun Heo } 1068dce90d47STejun Heo 1069112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 1070bf4ede01STejun Heo { 1071112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1072bf4ede01STejun Heo 1073bf4ede01STejun Heo trace_workqueue_activate_work(work); 1074112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1075bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 1076112202d9STejun Heo pwq->nr_active++; 1077bf4ede01STejun Heo } 1078bf4ede01STejun Heo 1079112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 10803aa62497SLai Jiangshan { 1081112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 10823aa62497SLai Jiangshan struct work_struct, entry); 10833aa62497SLai Jiangshan 1084112202d9STejun Heo pwq_activate_delayed_work(work); 10853aa62497SLai Jiangshan } 10863aa62497SLai Jiangshan 1087bf4ede01STejun Heo /** 1088112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1089112202d9STejun Heo * @pwq: pwq of interest 1090bf4ede01STejun Heo * @color: color of work which left the queue 1091bf4ede01STejun Heo * 1092bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1093112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1094bf4ede01STejun Heo * 1095bf4ede01STejun Heo * CONTEXT: 1096d565ed63STejun Heo * spin_lock_irq(pool->lock). 1097bf4ede01STejun Heo */ 1098112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1099bf4ede01STejun Heo { 11008864b4e5STejun Heo /* uncolored work items don't participate in flushing or nr_active */ 1101bf4ede01STejun Heo if (color == WORK_NO_COLOR) 11028864b4e5STejun Heo goto out_put; 1103bf4ede01STejun Heo 1104112202d9STejun Heo pwq->nr_in_flight[color]--; 1105bf4ede01STejun Heo 1106112202d9STejun Heo pwq->nr_active--; 1107112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 1108bf4ede01STejun Heo /* one down, submit a delayed one */ 1109112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1110112202d9STejun Heo pwq_activate_first_delayed(pwq); 1111bf4ede01STejun Heo } 1112bf4ede01STejun Heo 1113bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1114112202d9STejun Heo if (likely(pwq->flush_color != color)) 11158864b4e5STejun Heo goto out_put; 1116bf4ede01STejun Heo 1117bf4ede01STejun Heo /* are there still in-flight works? */ 1118112202d9STejun Heo if (pwq->nr_in_flight[color]) 11198864b4e5STejun Heo goto out_put; 1120bf4ede01STejun Heo 1121112202d9STejun Heo /* this pwq is done, clear flush_color */ 1122112202d9STejun Heo pwq->flush_color = -1; 1123bf4ede01STejun Heo 1124bf4ede01STejun Heo /* 1125112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1126bf4ede01STejun Heo * will handle the rest. 1127bf4ede01STejun Heo */ 1128112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1129112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 11308864b4e5STejun Heo out_put: 11318864b4e5STejun Heo put_pwq(pwq); 1132bf4ede01STejun Heo } 1133bf4ede01STejun Heo 113436e227d2STejun Heo /** 1135bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 113636e227d2STejun Heo * @work: work item to steal 113736e227d2STejun Heo * @is_dwork: @work is a delayed_work 1138bbb68dfaSTejun Heo * @flags: place to store irq state 113936e227d2STejun Heo * 114036e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1141d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 114236e227d2STejun Heo * 1143d185af30SYacine Belkadi * Return: 114436e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 114536e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 114636e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1147bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1148bbb68dfaSTejun Heo * for arbitrarily long 114936e227d2STejun Heo * 1150d185af30SYacine Belkadi * Note: 1151bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1152e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1153e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1154e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1155bbb68dfaSTejun Heo * 1156bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1157bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1158bbb68dfaSTejun Heo * 1159e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1160bf4ede01STejun Heo */ 1161bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1162bbb68dfaSTejun Heo unsigned long *flags) 1163bf4ede01STejun Heo { 1164d565ed63STejun Heo struct worker_pool *pool; 1165112202d9STejun Heo struct pool_workqueue *pwq; 1166bf4ede01STejun Heo 1167bbb68dfaSTejun Heo local_irq_save(*flags); 1168bbb68dfaSTejun Heo 116936e227d2STejun Heo /* try to steal the timer if it exists */ 117036e227d2STejun Heo if (is_dwork) { 117136e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 117236e227d2STejun Heo 1173e0aecdd8STejun Heo /* 1174e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1175e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1176e0aecdd8STejun Heo * running on the local CPU. 1177e0aecdd8STejun Heo */ 117836e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 117936e227d2STejun Heo return 1; 118036e227d2STejun Heo } 118136e227d2STejun Heo 118236e227d2STejun Heo /* try to claim PENDING the normal way */ 1183bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1184bf4ede01STejun Heo return 0; 1185bf4ede01STejun Heo 1186bf4ede01STejun Heo /* 1187bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1188bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1189bf4ede01STejun Heo */ 1190d565ed63STejun Heo pool = get_work_pool(work); 1191d565ed63STejun Heo if (!pool) 1192bbb68dfaSTejun Heo goto fail; 1193bf4ede01STejun Heo 1194d565ed63STejun Heo spin_lock(&pool->lock); 1195bf4ede01STejun Heo /* 1196112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1197112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1198112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1199112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1200112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 12010b3dae68SLai Jiangshan * item is currently queued on that pool. 1202bf4ede01STejun Heo */ 1203112202d9STejun Heo pwq = get_work_pwq(work); 1204112202d9STejun Heo if (pwq && pwq->pool == pool) { 1205bf4ede01STejun Heo debug_work_deactivate(work); 12063aa62497SLai Jiangshan 12073aa62497SLai Jiangshan /* 120816062836STejun Heo * A delayed work item cannot be grabbed directly because 120916062836STejun Heo * it might have linked NO_COLOR work items which, if left 1210112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 121116062836STejun Heo * management later on and cause stall. Make sure the work 121216062836STejun Heo * item is activated before grabbing. 12133aa62497SLai Jiangshan */ 12143aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1215112202d9STejun Heo pwq_activate_delayed_work(work); 12163aa62497SLai Jiangshan 1217bf4ede01STejun Heo list_del_init(&work->entry); 12189c34a704SLai Jiangshan pwq_dec_nr_in_flight(pwq, get_work_color(work)); 121936e227d2STejun Heo 1220112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 12214468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 12224468a00fSLai Jiangshan 1223d565ed63STejun Heo spin_unlock(&pool->lock); 122436e227d2STejun Heo return 1; 1225bf4ede01STejun Heo } 1226d565ed63STejun Heo spin_unlock(&pool->lock); 1227bbb68dfaSTejun Heo fail: 1228bbb68dfaSTejun Heo local_irq_restore(*flags); 1229bbb68dfaSTejun Heo if (work_is_canceling(work)) 1230bbb68dfaSTejun Heo return -ENOENT; 1231bbb68dfaSTejun Heo cpu_relax(); 123236e227d2STejun Heo return -EAGAIN; 1233bf4ede01STejun Heo } 1234bf4ede01STejun Heo 1235bf4ede01STejun Heo /** 1236706026c2STejun Heo * insert_work - insert a work into a pool 1237112202d9STejun Heo * @pwq: pwq @work belongs to 12384690c4abSTejun Heo * @work: work to insert 12394690c4abSTejun Heo * @head: insertion point 12404690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 12414690c4abSTejun Heo * 1242112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1243706026c2STejun Heo * work_struct flags. 12444690c4abSTejun Heo * 12454690c4abSTejun Heo * CONTEXT: 1246d565ed63STejun Heo * spin_lock_irq(pool->lock). 1247365970a1SDavid Howells */ 1248112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1249112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1250b89deed3SOleg Nesterov { 1251112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1252e1d8aa9fSFrederic Weisbecker 12534690c4abSTejun Heo /* we own @work, set data and link */ 1254112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 12551a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 12568864b4e5STejun Heo get_pwq(pwq); 1257e22bee78STejun Heo 1258e22bee78STejun Heo /* 1259c5aa87bbSTejun Heo * Ensure either wq_worker_sleeping() sees the above 1260c5aa87bbSTejun Heo * list_add_tail() or we see zero nr_running to avoid workers lying 1261c5aa87bbSTejun Heo * around lazily while there are works to be processed. 1262e22bee78STejun Heo */ 1263e22bee78STejun Heo smp_mb(); 1264e22bee78STejun Heo 126563d95a91STejun Heo if (__need_more_worker(pool)) 126663d95a91STejun Heo wake_up_worker(pool); 1267b89deed3SOleg Nesterov } 1268b89deed3SOleg Nesterov 1269c8efcc25STejun Heo /* 1270c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 12718d03ecfeSTejun Heo * same workqueue. 1272c8efcc25STejun Heo */ 1273c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1274c8efcc25STejun Heo { 1275c8efcc25STejun Heo struct worker *worker; 1276c8efcc25STejun Heo 12778d03ecfeSTejun Heo worker = current_wq_worker(); 1278c8efcc25STejun Heo /* 12798d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 12808d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1281c8efcc25STejun Heo */ 1282112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1283c8efcc25STejun Heo } 1284c8efcc25STejun Heo 1285d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 12861da177e4SLinus Torvalds struct work_struct *work) 12871da177e4SLinus Torvalds { 1288112202d9STejun Heo struct pool_workqueue *pwq; 1289c9178087STejun Heo struct worker_pool *last_pool; 12901e19ffc6STejun Heo struct list_head *worklist; 12918a2e8e5dSTejun Heo unsigned int work_flags; 1292b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 12938930cabaSTejun Heo 12948930cabaSTejun Heo /* 12958930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 12968930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 12978930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 12988930cabaSTejun Heo * happen with IRQ disabled. 12998930cabaSTejun Heo */ 13008930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 13011da177e4SLinus Torvalds 1302dc186ad7SThomas Gleixner debug_work_activate(work); 13031e19ffc6STejun Heo 13049ef28a73SLi Bin /* if draining, only works from the same workqueue are allowed */ 1305618b01ebSTejun Heo if (unlikely(wq->flags & __WQ_DRAINING) && 1306c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1307e41e704bSTejun Heo return; 13089e8cd2f5STejun Heo retry: 1309df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1310f3421797STejun Heo cpu = raw_smp_processor_id(); 1311df2d5ae4STejun Heo 1312df2d5ae4STejun Heo /* pwq which will be used unless @work is executing elsewhere */ 1313df2d5ae4STejun Heo if (!(wq->flags & WQ_UNBOUND)) 1314c9178087STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1315df2d5ae4STejun Heo else 1316df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1317f3421797STejun Heo 131818aa9effSTejun Heo /* 1319c9178087STejun Heo * If @work was previously on a different pool, it might still be 1320c9178087STejun Heo * running there, in which case the work needs to be queued on that 1321c9178087STejun Heo * pool to guarantee non-reentrancy. 132218aa9effSTejun Heo */ 1323c9e7cf27STejun Heo last_pool = get_work_pool(work); 1324112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 132518aa9effSTejun Heo struct worker *worker; 132618aa9effSTejun Heo 1327d565ed63STejun Heo spin_lock(&last_pool->lock); 132818aa9effSTejun Heo 1329c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 133018aa9effSTejun Heo 1331112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1332c9178087STejun Heo pwq = worker->current_pwq; 13338594fadeSLai Jiangshan } else { 133418aa9effSTejun Heo /* meh... not running there, queue here */ 1335d565ed63STejun Heo spin_unlock(&last_pool->lock); 1336112202d9STejun Heo spin_lock(&pwq->pool->lock); 133718aa9effSTejun Heo } 13388930cabaSTejun Heo } else { 1339112202d9STejun Heo spin_lock(&pwq->pool->lock); 13408930cabaSTejun Heo } 1341502ca9d8STejun Heo 13429e8cd2f5STejun Heo /* 13439e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 13449e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 13459e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1346df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1347df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 13489e8cd2f5STejun Heo * make forward-progress. 13499e8cd2f5STejun Heo */ 13509e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 13519e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 13529e8cd2f5STejun Heo spin_unlock(&pwq->pool->lock); 13539e8cd2f5STejun Heo cpu_relax(); 13549e8cd2f5STejun Heo goto retry; 13559e8cd2f5STejun Heo } 13569e8cd2f5STejun Heo /* oops */ 13579e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 13589e8cd2f5STejun Heo wq->name, cpu); 13599e8cd2f5STejun Heo } 13609e8cd2f5STejun Heo 1361112202d9STejun Heo /* pwq determined, queue */ 1362112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1363502ca9d8STejun Heo 1364f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1365112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1366f5b2552bSDan Carpenter return; 1367f5b2552bSDan Carpenter } 13681e19ffc6STejun Heo 1369112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1370112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 13711e19ffc6STejun Heo 1372112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1373cdadf009STejun Heo trace_workqueue_activate_work(work); 1374112202d9STejun Heo pwq->nr_active++; 1375112202d9STejun Heo worklist = &pwq->pool->worklist; 13768a2e8e5dSTejun Heo } else { 13778a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1378112202d9STejun Heo worklist = &pwq->delayed_works; 13798a2e8e5dSTejun Heo } 13801e19ffc6STejun Heo 1381112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 13821e19ffc6STejun Heo 1383112202d9STejun Heo spin_unlock(&pwq->pool->lock); 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 13860fcb78c2SRolf Eike Beer /** 1387c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1388c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1389c1a220e7SZhang Rui * @wq: workqueue to use 1390c1a220e7SZhang Rui * @work: work to queue 1391c1a220e7SZhang Rui * 1392c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1393c1a220e7SZhang Rui * can't go away. 1394d185af30SYacine Belkadi * 1395d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1396c1a220e7SZhang Rui */ 1397d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1398d4283e93STejun Heo struct work_struct *work) 1399c1a220e7SZhang Rui { 1400d4283e93STejun Heo bool ret = false; 14018930cabaSTejun Heo unsigned long flags; 14028930cabaSTejun Heo 14038930cabaSTejun Heo local_irq_save(flags); 1404c1a220e7SZhang Rui 140522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 14064690c4abSTejun Heo __queue_work(cpu, wq, work); 1407d4283e93STejun Heo ret = true; 1408c1a220e7SZhang Rui } 14098930cabaSTejun Heo 14108930cabaSTejun Heo local_irq_restore(flags); 1411c1a220e7SZhang Rui return ret; 1412c1a220e7SZhang Rui } 1413ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1414c1a220e7SZhang Rui 1415d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 14161da177e4SLinus Torvalds { 141752bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 14181da177e4SLinus Torvalds 1419e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 142060c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 14211da177e4SLinus Torvalds } 14221438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 14231da177e4SLinus Torvalds 14247beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 142552bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 14261da177e4SLinus Torvalds { 14277beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 14287beb2edfSTejun Heo struct work_struct *work = &dwork->work; 14291da177e4SLinus Torvalds 14307beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 14317beb2edfSTejun Heo timer->data != (unsigned long)dwork); 1432fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1433fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 14347beb2edfSTejun Heo 14358852aac2STejun Heo /* 14368852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 14378852aac2STejun Heo * both optimization and correctness. The earliest @timer can 14388852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 14398852aac2STejun Heo * on that there's no such delay when @delay is 0. 14408852aac2STejun Heo */ 14418852aac2STejun Heo if (!delay) { 14428852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 14438852aac2STejun Heo return; 14448852aac2STejun Heo } 14458852aac2STejun Heo 14467beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 14477beb2edfSTejun Heo 144860c057bcSLai Jiangshan dwork->wq = wq; 14491265057fSTejun Heo dwork->cpu = cpu; 14507beb2edfSTejun Heo timer->expires = jiffies + delay; 14517beb2edfSTejun Heo 14527beb2edfSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 14537beb2edfSTejun Heo add_timer_on(timer, cpu); 14547beb2edfSTejun Heo else 14557beb2edfSTejun Heo add_timer(timer); 14567beb2edfSTejun Heo } 14571da177e4SLinus Torvalds 14580fcb78c2SRolf Eike Beer /** 14590fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 14600fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 14610fcb78c2SRolf Eike Beer * @wq: workqueue to use 1462af9997e4SRandy Dunlap * @dwork: work to queue 14630fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 14640fcb78c2SRolf Eike Beer * 1465d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1466715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1467715f1300STejun Heo * execution. 14680fcb78c2SRolf Eike Beer */ 1469d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 147052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 14717a6bc1cdSVenkatesh Pallipadi { 147252bad64dSDavid Howells struct work_struct *work = &dwork->work; 1473d4283e93STejun Heo bool ret = false; 14748930cabaSTejun Heo unsigned long flags; 14758930cabaSTejun Heo 14768930cabaSTejun Heo /* read the comment in __queue_work() */ 14778930cabaSTejun Heo local_irq_save(flags); 14787a6bc1cdSVenkatesh Pallipadi 147922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 14807beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1481d4283e93STejun Heo ret = true; 14827a6bc1cdSVenkatesh Pallipadi } 14838930cabaSTejun Heo 14848930cabaSTejun Heo local_irq_restore(flags); 14857a6bc1cdSVenkatesh Pallipadi return ret; 14867a6bc1cdSVenkatesh Pallipadi } 1487ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 14881da177e4SLinus Torvalds 1489c8e55f36STejun Heo /** 14908376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 14918376fe22STejun Heo * @cpu: CPU number to execute work on 14928376fe22STejun Heo * @wq: workqueue to use 14938376fe22STejun Heo * @dwork: work to queue 14948376fe22STejun Heo * @delay: number of jiffies to wait before queueing 14958376fe22STejun Heo * 14968376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 14978376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 14988376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 14998376fe22STejun Heo * current state. 15008376fe22STejun Heo * 1501d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 15028376fe22STejun Heo * pending and its timer was modified. 15038376fe22STejun Heo * 1504e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 15058376fe22STejun Heo * See try_to_grab_pending() for details. 15068376fe22STejun Heo */ 15078376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 15088376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 15098376fe22STejun Heo { 15108376fe22STejun Heo unsigned long flags; 15118376fe22STejun Heo int ret; 15128376fe22STejun Heo 15138376fe22STejun Heo do { 15148376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 15158376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 15168376fe22STejun Heo 15178376fe22STejun Heo if (likely(ret >= 0)) { 15188376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 15198376fe22STejun Heo local_irq_restore(flags); 15208376fe22STejun Heo } 15218376fe22STejun Heo 15228376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 15238376fe22STejun Heo return ret; 15248376fe22STejun Heo } 15258376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 15268376fe22STejun Heo 15278376fe22STejun Heo /** 1528c8e55f36STejun Heo * worker_enter_idle - enter idle state 1529c8e55f36STejun Heo * @worker: worker which is entering idle state 1530c8e55f36STejun Heo * 1531c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1532c8e55f36STejun Heo * necessary. 1533c8e55f36STejun Heo * 1534c8e55f36STejun Heo * LOCKING: 1535d565ed63STejun Heo * spin_lock_irq(pool->lock). 1536c8e55f36STejun Heo */ 1537c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 15381da177e4SLinus Torvalds { 1539bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1540c8e55f36STejun Heo 15416183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 15426183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 15436183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 15446183c009STejun Heo return; 1545c8e55f36STejun Heo 1546051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1547cb444766STejun Heo worker->flags |= WORKER_IDLE; 1548bd7bdd43STejun Heo pool->nr_idle++; 1549e22bee78STejun Heo worker->last_active = jiffies; 1550c8e55f36STejun Heo 1551c8e55f36STejun Heo /* idle_list is LIFO */ 1552bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1553db7bccf4STejun Heo 155463d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1555628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1556cb444766STejun Heo 1557544ecf31STejun Heo /* 1558706026c2STejun Heo * Sanity check nr_running. Because wq_unbind_fn() releases 1559d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1560628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1561628c78e7STejun Heo * unbind is not in progress. 1562544ecf31STejun Heo */ 156324647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1564bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1565e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1566c8e55f36STejun Heo } 1567c8e55f36STejun Heo 1568c8e55f36STejun Heo /** 1569c8e55f36STejun Heo * worker_leave_idle - leave idle state 1570c8e55f36STejun Heo * @worker: worker which is leaving idle state 1571c8e55f36STejun Heo * 1572c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1573c8e55f36STejun Heo * 1574c8e55f36STejun Heo * LOCKING: 1575d565ed63STejun Heo * spin_lock_irq(pool->lock). 1576c8e55f36STejun Heo */ 1577c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1578c8e55f36STejun Heo { 1579bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1580c8e55f36STejun Heo 15816183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 15826183c009STejun Heo return; 1583d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1584bd7bdd43STejun Heo pool->nr_idle--; 1585c8e55f36STejun Heo list_del_init(&worker->entry); 1586c8e55f36STejun Heo } 1587c8e55f36STejun Heo 1588f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1589c34056a3STejun Heo { 1590c34056a3STejun Heo struct worker *worker; 1591c34056a3STejun Heo 1592f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1593c8e55f36STejun Heo if (worker) { 1594c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1595affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1596da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1597e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1598e22bee78STejun Heo worker->flags = WORKER_PREP; 1599c8e55f36STejun Heo } 1600c34056a3STejun Heo return worker; 1601c34056a3STejun Heo } 1602c34056a3STejun Heo 1603c34056a3STejun Heo /** 16044736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 16054736cbf7SLai Jiangshan * @worker: worker to be attached 16064736cbf7SLai Jiangshan * @pool: the target pool 16074736cbf7SLai Jiangshan * 16084736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 16094736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 16104736cbf7SLai Jiangshan * cpu-[un]hotplugs. 16114736cbf7SLai Jiangshan */ 16124736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 16134736cbf7SLai Jiangshan struct worker_pool *pool) 16144736cbf7SLai Jiangshan { 16154736cbf7SLai Jiangshan mutex_lock(&pool->attach_mutex); 16164736cbf7SLai Jiangshan 16174736cbf7SLai Jiangshan /* 16184736cbf7SLai Jiangshan * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any 16194736cbf7SLai Jiangshan * online CPUs. It'll be re-applied when any of the CPUs come up. 16204736cbf7SLai Jiangshan */ 16214736cbf7SLai Jiangshan set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 16224736cbf7SLai Jiangshan 16234736cbf7SLai Jiangshan /* 16244736cbf7SLai Jiangshan * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains 16254736cbf7SLai Jiangshan * stable across this function. See the comments above the 16264736cbf7SLai Jiangshan * flag definition for details. 16274736cbf7SLai Jiangshan */ 16284736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 16294736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 16304736cbf7SLai Jiangshan 16314736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 16324736cbf7SLai Jiangshan 16334736cbf7SLai Jiangshan mutex_unlock(&pool->attach_mutex); 16344736cbf7SLai Jiangshan } 16354736cbf7SLai Jiangshan 16364736cbf7SLai Jiangshan /** 163760f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 163860f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 163960f5a4bcSLai Jiangshan * @pool: the pool @worker is attached to 164060f5a4bcSLai Jiangshan * 16414736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 16424736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 16434736cbf7SLai Jiangshan * other reference to the pool. 164460f5a4bcSLai Jiangshan */ 164560f5a4bcSLai Jiangshan static void worker_detach_from_pool(struct worker *worker, 164660f5a4bcSLai Jiangshan struct worker_pool *pool) 164760f5a4bcSLai Jiangshan { 164860f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 164960f5a4bcSLai Jiangshan 165092f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 1651da028469SLai Jiangshan list_del(&worker->node); 1652da028469SLai Jiangshan if (list_empty(&pool->workers)) 165360f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 165492f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 165560f5a4bcSLai Jiangshan 1656b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1657b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1658b62c0751SLai Jiangshan 165960f5a4bcSLai Jiangshan if (detach_completion) 166060f5a4bcSLai Jiangshan complete(detach_completion); 166160f5a4bcSLai Jiangshan } 166260f5a4bcSLai Jiangshan 166360f5a4bcSLai Jiangshan /** 1664c34056a3STejun Heo * create_worker - create a new workqueue worker 166563d95a91STejun Heo * @pool: pool the new worker will belong to 1666c34056a3STejun Heo * 1667051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1668c34056a3STejun Heo * 1669c34056a3STejun Heo * CONTEXT: 1670c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1671c34056a3STejun Heo * 1672d185af30SYacine Belkadi * Return: 1673c34056a3STejun Heo * Pointer to the newly created worker. 1674c34056a3STejun Heo */ 1675bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1676c34056a3STejun Heo { 1677c34056a3STejun Heo struct worker *worker = NULL; 1678f3421797STejun Heo int id = -1; 1679e3c916a4STejun Heo char id_buf[16]; 1680c34056a3STejun Heo 16817cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 16827cda9aaeSLai Jiangshan id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL); 1683822d8405STejun Heo if (id < 0) 1684c34056a3STejun Heo goto fail; 1685c34056a3STejun Heo 1686f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 1687c34056a3STejun Heo if (!worker) 1688c34056a3STejun Heo goto fail; 1689c34056a3STejun Heo 1690bd7bdd43STejun Heo worker->pool = pool; 1691c34056a3STejun Heo worker->id = id; 1692c34056a3STejun Heo 169329c91e99STejun Heo if (pool->cpu >= 0) 1694e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1695e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1696f3421797STejun Heo else 1697e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1698e3c916a4STejun Heo 1699f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1700e3c916a4STejun Heo "kworker/%s", id_buf); 1701c34056a3STejun Heo if (IS_ERR(worker->task)) 1702c34056a3STejun Heo goto fail; 1703c34056a3STejun Heo 170491151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 170591151228SOleg Nesterov 170691151228SOleg Nesterov /* prevent userland from meddling with cpumask of workqueue workers */ 170791151228SOleg Nesterov worker->task->flags |= PF_NO_SETAFFINITY; 170891151228SOleg Nesterov 1709da028469SLai Jiangshan /* successful, attach the worker to the pool */ 17104736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1711822d8405STejun Heo 1712051e1850SLai Jiangshan /* start the newly created worker */ 1713051e1850SLai Jiangshan spin_lock_irq(&pool->lock); 1714051e1850SLai Jiangshan worker->pool->nr_workers++; 1715051e1850SLai Jiangshan worker_enter_idle(worker); 1716051e1850SLai Jiangshan wake_up_process(worker->task); 1717051e1850SLai Jiangshan spin_unlock_irq(&pool->lock); 1718051e1850SLai Jiangshan 1719c34056a3STejun Heo return worker; 1720822d8405STejun Heo 1721c34056a3STejun Heo fail: 17229625ab17SLai Jiangshan if (id >= 0) 17237cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, id); 1724c34056a3STejun Heo kfree(worker); 1725c34056a3STejun Heo return NULL; 1726c34056a3STejun Heo } 1727c34056a3STejun Heo 1728c34056a3STejun Heo /** 1729c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1730c34056a3STejun Heo * @worker: worker to be destroyed 1731c34056a3STejun Heo * 173273eb7fe7SLai Jiangshan * Destroy @worker and adjust @pool stats accordingly. The worker should 173373eb7fe7SLai Jiangshan * be idle. 1734c8e55f36STejun Heo * 1735c8e55f36STejun Heo * CONTEXT: 173660f5a4bcSLai Jiangshan * spin_lock_irq(pool->lock). 1737c34056a3STejun Heo */ 1738c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1739c34056a3STejun Heo { 1740bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1741c34056a3STejun Heo 1742cd549687STejun Heo lockdep_assert_held(&pool->lock); 1743cd549687STejun Heo 1744c34056a3STejun Heo /* sanity check frenzy */ 17456183c009STejun Heo if (WARN_ON(worker->current_work) || 174673eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 174773eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 17486183c009STejun Heo return; 1749c34056a3STejun Heo 1750bd7bdd43STejun Heo pool->nr_workers--; 1751bd7bdd43STejun Heo pool->nr_idle--; 1752c8e55f36STejun Heo 1753c8e55f36STejun Heo list_del_init(&worker->entry); 1754cb444766STejun Heo worker->flags |= WORKER_DIE; 175560f5a4bcSLai Jiangshan wake_up_process(worker->task); 1756c34056a3STejun Heo } 1757c34056a3STejun Heo 175863d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1759e22bee78STejun Heo { 176063d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1761e22bee78STejun Heo 1762d565ed63STejun Heo spin_lock_irq(&pool->lock); 1763e22bee78STejun Heo 17643347fc9fSLai Jiangshan while (too_many_workers(pool)) { 1765e22bee78STejun Heo struct worker *worker; 1766e22bee78STejun Heo unsigned long expires; 1767e22bee78STejun Heo 1768e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 176963d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1770e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1771e22bee78STejun Heo 17723347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 177363d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 17743347fc9fSLai Jiangshan break; 1775e22bee78STejun Heo } 17763347fc9fSLai Jiangshan 17773347fc9fSLai Jiangshan destroy_worker(worker); 1778e22bee78STejun Heo } 1779e22bee78STejun Heo 1780d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1781e22bee78STejun Heo } 1782e22bee78STejun Heo 1783493a1724STejun Heo static void send_mayday(struct work_struct *work) 1784e22bee78STejun Heo { 1785112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1786112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1787493a1724STejun Heo 17882e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 1789e22bee78STejun Heo 1790493008a8STejun Heo if (!wq->rescuer) 1791493a1724STejun Heo return; 1792e22bee78STejun Heo 1793e22bee78STejun Heo /* mayday mayday mayday */ 1794493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 179577668c8bSLai Jiangshan /* 179677668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 179777668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 179877668c8bSLai Jiangshan * rescuer is done with it. 179977668c8bSLai Jiangshan */ 180077668c8bSLai Jiangshan get_pwq(pwq); 1801493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1802e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1803493a1724STejun Heo } 1804e22bee78STejun Heo } 1805e22bee78STejun Heo 1806706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool) 1807e22bee78STejun Heo { 180863d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1809e22bee78STejun Heo struct work_struct *work; 1810e22bee78STejun Heo 1811b2d82909STejun Heo spin_lock_irq(&pool->lock); 1812b2d82909STejun Heo spin_lock(&wq_mayday_lock); /* for wq->maydays */ 1813e22bee78STejun Heo 181463d95a91STejun Heo if (need_to_create_worker(pool)) { 1815e22bee78STejun Heo /* 1816e22bee78STejun Heo * We've been trying to create a new worker but 1817e22bee78STejun Heo * haven't been successful. We might be hitting an 1818e22bee78STejun Heo * allocation deadlock. Send distress signals to 1819e22bee78STejun Heo * rescuers. 1820e22bee78STejun Heo */ 182163d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1822e22bee78STejun Heo send_mayday(work); 1823e22bee78STejun Heo } 1824e22bee78STejun Heo 1825b2d82909STejun Heo spin_unlock(&wq_mayday_lock); 1826b2d82909STejun Heo spin_unlock_irq(&pool->lock); 1827e22bee78STejun Heo 182863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1829e22bee78STejun Heo } 1830e22bee78STejun Heo 1831e22bee78STejun Heo /** 1832e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 183363d95a91STejun Heo * @pool: pool to create a new worker for 1834e22bee78STejun Heo * 183563d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1836e22bee78STejun Heo * have at least one idle worker on return from this function. If 1837e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 183863d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1839e22bee78STejun Heo * possible allocation deadlock. 1840e22bee78STejun Heo * 1841c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 1842c5aa87bbSTejun Heo * may_start_working() %true. 1843e22bee78STejun Heo * 1844e22bee78STejun Heo * LOCKING: 1845d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1846e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1847e22bee78STejun Heo * manager. 1848e22bee78STejun Heo */ 184929187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 1850d565ed63STejun Heo __releases(&pool->lock) 1851d565ed63STejun Heo __acquires(&pool->lock) 1852e22bee78STejun Heo { 1853e22bee78STejun Heo restart: 1854d565ed63STejun Heo spin_unlock_irq(&pool->lock); 18559f9c2364STejun Heo 1856e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 185763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1858e22bee78STejun Heo 1859e22bee78STejun Heo while (true) { 1860051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 1861e22bee78STejun Heo break; 1862e22bee78STejun Heo 1863e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 18649f9c2364STejun Heo 186563d95a91STejun Heo if (!need_to_create_worker(pool)) 1866e22bee78STejun Heo break; 1867e22bee78STejun Heo } 1868e22bee78STejun Heo 186963d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1870d565ed63STejun Heo spin_lock_irq(&pool->lock); 1871051e1850SLai Jiangshan /* 1872051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 1873051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 1874051e1850SLai Jiangshan * already become busy. 1875051e1850SLai Jiangshan */ 187663d95a91STejun Heo if (need_to_create_worker(pool)) 1877e22bee78STejun Heo goto restart; 1878e22bee78STejun Heo } 1879e22bee78STejun Heo 1880e22bee78STejun Heo /** 1881e22bee78STejun Heo * manage_workers - manage worker pool 1882e22bee78STejun Heo * @worker: self 1883e22bee78STejun Heo * 1884706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1885e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1886706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1887e22bee78STejun Heo * 1888e22bee78STejun Heo * The caller can safely start processing works on false return. On 1889e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1890e22bee78STejun Heo * and may_start_working() is true. 1891e22bee78STejun Heo * 1892e22bee78STejun Heo * CONTEXT: 1893d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1894e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1895e22bee78STejun Heo * 1896d185af30SYacine Belkadi * Return: 189729187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 189829187a9eSTejun Heo * start processing works, %true if management function was performed and 189929187a9eSTejun Heo * the conditions that the caller verified before calling the function may 190029187a9eSTejun Heo * no longer be true. 1901e22bee78STejun Heo */ 1902e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1903e22bee78STejun Heo { 190463d95a91STejun Heo struct worker_pool *pool = worker->pool; 1905e22bee78STejun Heo 1906bc3a1afcSTejun Heo /* 1907bc3a1afcSTejun Heo * Anyone who successfully grabs manager_arb wins the arbitration 1908bc3a1afcSTejun Heo * and becomes the manager. mutex_trylock() on pool->manager_arb 1909bc3a1afcSTejun Heo * failure while holding pool->lock reliably indicates that someone 1910bc3a1afcSTejun Heo * else is managing the pool and the worker which failed trylock 1911bc3a1afcSTejun Heo * can proceed to executing work items. This means that anyone 1912bc3a1afcSTejun Heo * grabbing manager_arb is responsible for actually performing 1913bc3a1afcSTejun Heo * manager duties. If manager_arb is grabbed and released without 1914bc3a1afcSTejun Heo * actual management, the pool may stall indefinitely. 1915bc3a1afcSTejun Heo */ 191634a06bd6STejun Heo if (!mutex_trylock(&pool->manager_arb)) 191729187a9eSTejun Heo return false; 19182607d7a6STejun Heo pool->manager = worker; 1919e22bee78STejun Heo 192029187a9eSTejun Heo maybe_create_worker(pool); 1921e22bee78STejun Heo 19222607d7a6STejun Heo pool->manager = NULL; 192334a06bd6STejun Heo mutex_unlock(&pool->manager_arb); 192429187a9eSTejun Heo return true; 1925e22bee78STejun Heo } 1926e22bee78STejun Heo 1927a62428c0STejun Heo /** 1928a62428c0STejun Heo * process_one_work - process single work 1929c34056a3STejun Heo * @worker: self 1930a62428c0STejun Heo * @work: work to process 1931a62428c0STejun Heo * 1932a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1933a62428c0STejun Heo * process a single work including synchronization against and 1934a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 1935a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 1936a62428c0STejun Heo * call this function to process a work. 1937a62428c0STejun Heo * 1938a62428c0STejun Heo * CONTEXT: 1939d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 1940a62428c0STejun Heo */ 1941c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 1942d565ed63STejun Heo __releases(&pool->lock) 1943d565ed63STejun Heo __acquires(&pool->lock) 19441da177e4SLinus Torvalds { 1945112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1946bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1947112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 194873f53c4aSTejun Heo int work_color; 19497e11629dSTejun Heo struct worker *collision; 19504e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 19514e6045f1SJohannes Berg /* 1952a62428c0STejun Heo * It is permissible to free the struct work_struct from 1953a62428c0STejun Heo * inside the function that is called from it, this we need to 1954a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 1955a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 1956a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 19574e6045f1SJohannes Berg */ 19584d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 19594d82a1deSPeter Zijlstra 19604d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 19614e6045f1SJohannes Berg #endif 1962807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 196385327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1964ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 196525511a47STejun Heo 19667e11629dSTejun Heo /* 19677e11629dSTejun Heo * A single work shouldn't be executed concurrently by 19687e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 19697e11629dSTejun Heo * already processing the work. If so, defer the work to the 19707e11629dSTejun Heo * currently executing one. 19717e11629dSTejun Heo */ 1972c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 19737e11629dSTejun Heo if (unlikely(collision)) { 19747e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 19757e11629dSTejun Heo return; 19767e11629dSTejun Heo } 19771da177e4SLinus Torvalds 19788930cabaSTejun Heo /* claim and dequeue */ 19791da177e4SLinus Torvalds debug_work_deactivate(work); 1980c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 1981c34056a3STejun Heo worker->current_work = work; 1982a2c1c57bSTejun Heo worker->current_func = work->func; 1983112202d9STejun Heo worker->current_pwq = pwq; 198473f53c4aSTejun Heo work_color = get_work_color(work); 19857a22ad75STejun Heo 1986a62428c0STejun Heo list_del_init(&work->entry); 1987a62428c0STejun Heo 1988649027d7STejun Heo /* 1989228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 1990228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 1991228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 1992228f1d00SLai Jiangshan * execution of the pending work items. 1993fb0e7bebSTejun Heo */ 1994fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 1995228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 1996fb0e7bebSTejun Heo 1997974271c4STejun Heo /* 1998a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 1999a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2000a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2001a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2002228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2003974271c4STejun Heo */ 2004a489a03eSLai Jiangshan if (need_more_worker(pool)) 200563d95a91STejun Heo wake_up_worker(pool); 2006974271c4STejun Heo 20078930cabaSTejun Heo /* 20087c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2009d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 201023657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 201123657bb1STejun Heo * disabled. 20128930cabaSTejun Heo */ 20137c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 20141da177e4SLinus Torvalds 2015d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2016365970a1SDavid Howells 2017112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 20183295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2019e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2020a2c1c57bSTejun Heo worker->current_func(work); 2021e36c886aSArjan van de Ven /* 2022e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2023e36c886aSArjan van de Ven * point will only record its address. 2024e36c886aSArjan van de Ven */ 2025e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20263295f0efSIngo Molnar lock_map_release(&lockdep_map); 2027112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 20281da177e4SLinus Torvalds 2029d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2030044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2031044c782cSValentin Ilie " last function: %pf\n", 2032a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2033a2c1c57bSTejun Heo worker->current_func); 2034d5abe669SPeter Zijlstra debug_show_held_locks(current); 2035d5abe669SPeter Zijlstra dump_stack(); 2036d5abe669SPeter Zijlstra } 2037d5abe669SPeter Zijlstra 2038b22ce278STejun Heo /* 2039b22ce278STejun Heo * The following prevents a kworker from hogging CPU on !PREEMPT 2040b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2041b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2042b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2043789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2044789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2045b22ce278STejun Heo */ 20463e28e377SJoe Lawrence cond_resched_rcu_qs(); 2047b22ce278STejun Heo 2048d565ed63STejun Heo spin_lock_irq(&pool->lock); 2049a62428c0STejun Heo 2050fb0e7bebSTejun Heo /* clear cpu intensive status */ 2051fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2052fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2053fb0e7bebSTejun Heo 2054a62428c0STejun Heo /* we're done with it, release */ 205542f8570fSSasha Levin hash_del(&worker->hentry); 2056c34056a3STejun Heo worker->current_work = NULL; 2057a2c1c57bSTejun Heo worker->current_func = NULL; 2058112202d9STejun Heo worker->current_pwq = NULL; 20593d1cb205STejun Heo worker->desc_valid = false; 2060112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 2063affee4b2STejun Heo /** 2064affee4b2STejun Heo * process_scheduled_works - process scheduled works 2065affee4b2STejun Heo * @worker: self 2066affee4b2STejun Heo * 2067affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2068affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2069affee4b2STejun Heo * fetches a work from the top and executes it. 2070affee4b2STejun Heo * 2071affee4b2STejun Heo * CONTEXT: 2072d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2073affee4b2STejun Heo * multiple times. 2074affee4b2STejun Heo */ 2075affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 20761da177e4SLinus Torvalds { 2077affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2078affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2079a62428c0STejun Heo struct work_struct, entry); 2080c34056a3STejun Heo process_one_work(worker, work); 2081a62428c0STejun Heo } 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 20844690c4abSTejun Heo /** 20854690c4abSTejun Heo * worker_thread - the worker thread function 2086c34056a3STejun Heo * @__worker: self 20874690c4abSTejun Heo * 2088c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2089c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2090c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2091c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2092c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2093d185af30SYacine Belkadi * 2094d185af30SYacine Belkadi * Return: 0 20954690c4abSTejun Heo */ 2096c34056a3STejun Heo static int worker_thread(void *__worker) 20971da177e4SLinus Torvalds { 2098c34056a3STejun Heo struct worker *worker = __worker; 2099bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 21001da177e4SLinus Torvalds 2101e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2102e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2103c8e55f36STejun Heo woke_up: 2104d565ed63STejun Heo spin_lock_irq(&pool->lock); 2105affee4b2STejun Heo 2106a9ab775bSTejun Heo /* am I supposed to die? */ 2107a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2108d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2109a9ab775bSTejun Heo WARN_ON_ONCE(!list_empty(&worker->entry)); 2110e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 211160f5a4bcSLai Jiangshan 211260f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 21137cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, worker->id); 211460f5a4bcSLai Jiangshan worker_detach_from_pool(worker, pool); 211560f5a4bcSLai Jiangshan kfree(worker); 2116c8e55f36STejun Heo return 0; 2117c8e55f36STejun Heo } 2118c8e55f36STejun Heo 2119c8e55f36STejun Heo worker_leave_idle(worker); 2120db7bccf4STejun Heo recheck: 2121e22bee78STejun Heo /* no more worker necessary? */ 212263d95a91STejun Heo if (!need_more_worker(pool)) 2123e22bee78STejun Heo goto sleep; 2124e22bee78STejun Heo 2125e22bee78STejun Heo /* do we need to manage? */ 212663d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2127e22bee78STejun Heo goto recheck; 2128e22bee78STejun Heo 2129c8e55f36STejun Heo /* 2130c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2131c8e55f36STejun Heo * preparing to process a work or actually processing it. 2132c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2133c8e55f36STejun Heo */ 21346183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2135c8e55f36STejun Heo 2136e22bee78STejun Heo /* 2137a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2138a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2139a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2140a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2141a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2142e22bee78STejun Heo */ 2143a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2144e22bee78STejun Heo 2145e22bee78STejun Heo do { 2146affee4b2STejun Heo struct work_struct *work = 2147bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2148affee4b2STejun Heo struct work_struct, entry); 2149affee4b2STejun Heo 2150c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2151affee4b2STejun Heo /* optimization path, not strictly necessary */ 2152affee4b2STejun Heo process_one_work(worker, work); 2153affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2154affee4b2STejun Heo process_scheduled_works(worker); 2155affee4b2STejun Heo } else { 2156c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2157affee4b2STejun Heo process_scheduled_works(worker); 2158affee4b2STejun Heo } 215963d95a91STejun Heo } while (keep_working(pool)); 2160affee4b2STejun Heo 2161228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2162d313dd85STejun Heo sleep: 2163c8e55f36STejun Heo /* 2164d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2165d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2166d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2167d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2168d565ed63STejun Heo * event. 2169c8e55f36STejun Heo */ 2170c8e55f36STejun Heo worker_enter_idle(worker); 2171c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 2172d565ed63STejun Heo spin_unlock_irq(&pool->lock); 21731da177e4SLinus Torvalds schedule(); 2174c8e55f36STejun Heo goto woke_up; 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 2177e22bee78STejun Heo /** 2178e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2179111c225aSTejun Heo * @__rescuer: self 2180e22bee78STejun Heo * 2181e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2182493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2183e22bee78STejun Heo * 2184706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2185e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2186e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2187e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2188e22bee78STejun Heo * the problem rescuer solves. 2189e22bee78STejun Heo * 2190706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2191706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2192e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2193e22bee78STejun Heo * 2194e22bee78STejun Heo * This should happen rarely. 2195d185af30SYacine Belkadi * 2196d185af30SYacine Belkadi * Return: 0 2197e22bee78STejun Heo */ 2198111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2199e22bee78STejun Heo { 2200111c225aSTejun Heo struct worker *rescuer = __rescuer; 2201111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2202e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 22034d595b86SLai Jiangshan bool should_stop; 2204e22bee78STejun Heo 2205e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2206111c225aSTejun Heo 2207111c225aSTejun Heo /* 2208111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2209111c225aSTejun Heo * doesn't participate in concurrency management. 2210111c225aSTejun Heo */ 2211111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2212e22bee78STejun Heo repeat: 2213e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 22141da177e4SLinus Torvalds 22154d595b86SLai Jiangshan /* 22164d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 22174d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 22184d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 22194d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 22204d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 22214d595b86SLai Jiangshan * list is always empty on exit. 22224d595b86SLai Jiangshan */ 22234d595b86SLai Jiangshan should_stop = kthread_should_stop(); 22241da177e4SLinus Torvalds 2225493a1724STejun Heo /* see whether any pwq is asking for help */ 22262e109a28STejun Heo spin_lock_irq(&wq_mayday_lock); 2227493a1724STejun Heo 2228493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2229493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2230493a1724STejun Heo struct pool_workqueue, mayday_node); 2231112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2232e22bee78STejun Heo struct work_struct *work, *n; 2233e22bee78STejun Heo 2234e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2235493a1724STejun Heo list_del_init(&pwq->mayday_node); 2236493a1724STejun Heo 22372e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2238e22bee78STejun Heo 223951697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 224051697d39SLai Jiangshan 224151697d39SLai Jiangshan spin_lock_irq(&pool->lock); 2242b3104104SLai Jiangshan rescuer->pool = pool; 2243e22bee78STejun Heo 2244e22bee78STejun Heo /* 2245e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2246e22bee78STejun Heo * process'em. 2247e22bee78STejun Heo */ 22480479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 2249bd7bdd43STejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) 2250112202d9STejun Heo if (get_work_pwq(work) == pwq) 2251e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2252e22bee78STejun Heo 2253008847f6SNeilBrown if (!list_empty(scheduled)) { 2254e22bee78STejun Heo process_scheduled_works(rescuer); 22557576958aSTejun Heo 22567576958aSTejun Heo /* 2257008847f6SNeilBrown * The above execution of rescued work items could 2258008847f6SNeilBrown * have created more to rescue through 2259008847f6SNeilBrown * pwq_activate_first_delayed() or chained 2260008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2261008847f6SNeilBrown * that such back-to-back work items, which may be 2262008847f6SNeilBrown * being used to relieve memory pressure, don't 2263008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2264008847f6SNeilBrown */ 2265008847f6SNeilBrown if (need_to_create_worker(pool)) { 2266008847f6SNeilBrown spin_lock(&wq_mayday_lock); 2267008847f6SNeilBrown get_pwq(pwq); 2268008847f6SNeilBrown list_move_tail(&pwq->mayday_node, &wq->maydays); 2269008847f6SNeilBrown spin_unlock(&wq_mayday_lock); 2270008847f6SNeilBrown } 2271008847f6SNeilBrown } 2272008847f6SNeilBrown 2273008847f6SNeilBrown /* 227477668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 227513b1d625SLai Jiangshan * go away while we're still attached to it. 227677668c8bSLai Jiangshan */ 227777668c8bSLai Jiangshan put_pwq(pwq); 227877668c8bSLai Jiangshan 227977668c8bSLai Jiangshan /* 2280d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 22817576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 22827576958aSTejun Heo * and stalling the execution. 22837576958aSTejun Heo */ 2284d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 228563d95a91STejun Heo wake_up_worker(pool); 22867576958aSTejun Heo 2287b3104104SLai Jiangshan rescuer->pool = NULL; 228813b1d625SLai Jiangshan spin_unlock_irq(&pool->lock); 228913b1d625SLai Jiangshan 229013b1d625SLai Jiangshan worker_detach_from_pool(rescuer, pool); 229113b1d625SLai Jiangshan 229213b1d625SLai Jiangshan spin_lock_irq(&wq_mayday_lock); 22931da177e4SLinus Torvalds } 22941da177e4SLinus Torvalds 22952e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2296493a1724STejun Heo 22974d595b86SLai Jiangshan if (should_stop) { 22984d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 22994d595b86SLai Jiangshan rescuer->task->flags &= ~PF_WQ_WORKER; 23004d595b86SLai Jiangshan return 0; 23014d595b86SLai Jiangshan } 23024d595b86SLai Jiangshan 2303111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2304111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2305e22bee78STejun Heo schedule(); 2306e22bee78STejun Heo goto repeat; 23071da177e4SLinus Torvalds } 23081da177e4SLinus Torvalds 2309fc2e4d70SOleg Nesterov struct wq_barrier { 2310fc2e4d70SOleg Nesterov struct work_struct work; 2311fc2e4d70SOleg Nesterov struct completion done; 23122607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2313fc2e4d70SOleg Nesterov }; 2314fc2e4d70SOleg Nesterov 2315fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2316fc2e4d70SOleg Nesterov { 2317fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2318fc2e4d70SOleg Nesterov complete(&barr->done); 2319fc2e4d70SOleg Nesterov } 2320fc2e4d70SOleg Nesterov 23214690c4abSTejun Heo /** 23224690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2323112202d9STejun Heo * @pwq: pwq to insert barrier into 23244690c4abSTejun Heo * @barr: wq_barrier to insert 2325affee4b2STejun Heo * @target: target work to attach @barr to 2326affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 23274690c4abSTejun Heo * 2328affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2329affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2330affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2331affee4b2STejun Heo * cpu. 2332affee4b2STejun Heo * 2333affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2334affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2335affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2336affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2337affee4b2STejun Heo * after a work with LINKED flag set. 2338affee4b2STejun Heo * 2339affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2340112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 23414690c4abSTejun Heo * 23424690c4abSTejun Heo * CONTEXT: 2343d565ed63STejun Heo * spin_lock_irq(pool->lock). 23444690c4abSTejun Heo */ 2345112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2346affee4b2STejun Heo struct wq_barrier *barr, 2347affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2348fc2e4d70SOleg Nesterov { 2349affee4b2STejun Heo struct list_head *head; 2350affee4b2STejun Heo unsigned int linked = 0; 2351affee4b2STejun Heo 2352dc186ad7SThomas Gleixner /* 2353d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2354dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2355dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2356dc186ad7SThomas Gleixner * might deadlock. 2357dc186ad7SThomas Gleixner */ 2358ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 235922df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2360fc2e4d70SOleg Nesterov init_completion(&barr->done); 23612607d7a6STejun Heo barr->task = current; 236283c22520SOleg Nesterov 2363affee4b2STejun Heo /* 2364affee4b2STejun Heo * If @target is currently being executed, schedule the 2365affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2366affee4b2STejun Heo */ 2367affee4b2STejun Heo if (worker) 2368affee4b2STejun Heo head = worker->scheduled.next; 2369affee4b2STejun Heo else { 2370affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2371affee4b2STejun Heo 2372affee4b2STejun Heo head = target->entry.next; 2373affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2374affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2375affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2376affee4b2STejun Heo } 2377affee4b2STejun Heo 2378dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2379112202d9STejun Heo insert_work(pwq, &barr->work, head, 2380affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2381fc2e4d70SOleg Nesterov } 2382fc2e4d70SOleg Nesterov 238373f53c4aSTejun Heo /** 2384112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 238573f53c4aSTejun Heo * @wq: workqueue being flushed 238673f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 238773f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 238873f53c4aSTejun Heo * 2389112202d9STejun Heo * Prepare pwqs for workqueue flushing. 239073f53c4aSTejun Heo * 2391112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2392112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2393112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2394112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2395112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 239673f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 239773f53c4aSTejun Heo * 239873f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 239973f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 240073f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 240173f53c4aSTejun Heo * is returned. 240273f53c4aSTejun Heo * 2403112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 240473f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 240573f53c4aSTejun Heo * advanced to @work_color. 240673f53c4aSTejun Heo * 240773f53c4aSTejun Heo * CONTEXT: 24083c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 240973f53c4aSTejun Heo * 2410d185af30SYacine Belkadi * Return: 241173f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 241273f53c4aSTejun Heo * otherwise. 241373f53c4aSTejun Heo */ 2414112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 241573f53c4aSTejun Heo int flush_color, int work_color) 24161da177e4SLinus Torvalds { 241773f53c4aSTejun Heo bool wait = false; 241849e3cf44STejun Heo struct pool_workqueue *pwq; 24191da177e4SLinus Torvalds 242073f53c4aSTejun Heo if (flush_color >= 0) { 24216183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2422112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2423dc186ad7SThomas Gleixner } 242414441960SOleg Nesterov 242549e3cf44STejun Heo for_each_pwq(pwq, wq) { 2426112202d9STejun Heo struct worker_pool *pool = pwq->pool; 24271da177e4SLinus Torvalds 2428b09f4fd3SLai Jiangshan spin_lock_irq(&pool->lock); 242973f53c4aSTejun Heo 243073f53c4aSTejun Heo if (flush_color >= 0) { 24316183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 243273f53c4aSTejun Heo 2433112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2434112202d9STejun Heo pwq->flush_color = flush_color; 2435112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 243673f53c4aSTejun Heo wait = true; 24371da177e4SLinus Torvalds } 243873f53c4aSTejun Heo } 243973f53c4aSTejun Heo 244073f53c4aSTejun Heo if (work_color >= 0) { 24416183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2442112202d9STejun Heo pwq->work_color = work_color; 244373f53c4aSTejun Heo } 244473f53c4aSTejun Heo 2445b09f4fd3SLai Jiangshan spin_unlock_irq(&pool->lock); 24461da177e4SLinus Torvalds } 24471da177e4SLinus Torvalds 2448112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 244973f53c4aSTejun Heo complete(&wq->first_flusher->done); 245073f53c4aSTejun Heo 245173f53c4aSTejun Heo return wait; 245283c22520SOleg Nesterov } 24531da177e4SLinus Torvalds 24540fcb78c2SRolf Eike Beer /** 24551da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 24560fcb78c2SRolf Eike Beer * @wq: workqueue to flush 24571da177e4SLinus Torvalds * 2458c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2459c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 24601da177e4SLinus Torvalds */ 24617ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 24621da177e4SLinus Torvalds { 246373f53c4aSTejun Heo struct wq_flusher this_flusher = { 246473f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 246573f53c4aSTejun Heo .flush_color = -1, 246673f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 246773f53c4aSTejun Heo }; 246873f53c4aSTejun Heo int next_color; 2469b1f4ec17SOleg Nesterov 24703295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 24713295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 247273f53c4aSTejun Heo 24733c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 247473f53c4aSTejun Heo 247573f53c4aSTejun Heo /* 247673f53c4aSTejun Heo * Start-to-wait phase 247773f53c4aSTejun Heo */ 247873f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 247973f53c4aSTejun Heo 248073f53c4aSTejun Heo if (next_color != wq->flush_color) { 248173f53c4aSTejun Heo /* 248273f53c4aSTejun Heo * Color space is not full. The current work_color 248373f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 248473f53c4aSTejun Heo * by one. 248573f53c4aSTejun Heo */ 24866183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 248773f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 248873f53c4aSTejun Heo wq->work_color = next_color; 248973f53c4aSTejun Heo 249073f53c4aSTejun Heo if (!wq->first_flusher) { 249173f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 24926183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 249373f53c4aSTejun Heo 249473f53c4aSTejun Heo wq->first_flusher = &this_flusher; 249573f53c4aSTejun Heo 2496112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 249773f53c4aSTejun Heo wq->work_color)) { 249873f53c4aSTejun Heo /* nothing to flush, done */ 249973f53c4aSTejun Heo wq->flush_color = next_color; 250073f53c4aSTejun Heo wq->first_flusher = NULL; 250173f53c4aSTejun Heo goto out_unlock; 250273f53c4aSTejun Heo } 250373f53c4aSTejun Heo } else { 250473f53c4aSTejun Heo /* wait in queue */ 25056183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 250673f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2507112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 250873f53c4aSTejun Heo } 250973f53c4aSTejun Heo } else { 251073f53c4aSTejun Heo /* 251173f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 251273f53c4aSTejun Heo * The next flush completion will assign us 251373f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 251473f53c4aSTejun Heo */ 251573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 251673f53c4aSTejun Heo } 251773f53c4aSTejun Heo 25183c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 251973f53c4aSTejun Heo 252073f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 252173f53c4aSTejun Heo 252273f53c4aSTejun Heo /* 252373f53c4aSTejun Heo * Wake-up-and-cascade phase 252473f53c4aSTejun Heo * 252573f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 252673f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 252773f53c4aSTejun Heo */ 252873f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 252973f53c4aSTejun Heo return; 253073f53c4aSTejun Heo 25313c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 253273f53c4aSTejun Heo 25334ce48b37STejun Heo /* we might have raced, check again with mutex held */ 25344ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 25354ce48b37STejun Heo goto out_unlock; 25364ce48b37STejun Heo 253773f53c4aSTejun Heo wq->first_flusher = NULL; 253873f53c4aSTejun Heo 25396183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 25406183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 254173f53c4aSTejun Heo 254273f53c4aSTejun Heo while (true) { 254373f53c4aSTejun Heo struct wq_flusher *next, *tmp; 254473f53c4aSTejun Heo 254573f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 254673f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 254773f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 254873f53c4aSTejun Heo break; 254973f53c4aSTejun Heo list_del_init(&next->list); 255073f53c4aSTejun Heo complete(&next->done); 255173f53c4aSTejun Heo } 255273f53c4aSTejun Heo 25536183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 255473f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 255573f53c4aSTejun Heo 255673f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 255773f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 255873f53c4aSTejun Heo 255973f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 256073f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 256173f53c4aSTejun Heo /* 256273f53c4aSTejun Heo * Assign the same color to all overflowed 256373f53c4aSTejun Heo * flushers, advance work_color and append to 256473f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 256573f53c4aSTejun Heo * phase for these overflowed flushers. 256673f53c4aSTejun Heo */ 256773f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 256873f53c4aSTejun Heo tmp->flush_color = wq->work_color; 256973f53c4aSTejun Heo 257073f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 257173f53c4aSTejun Heo 257273f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 257373f53c4aSTejun Heo &wq->flusher_queue); 2574112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 257573f53c4aSTejun Heo } 257673f53c4aSTejun Heo 257773f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 25786183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 257973f53c4aSTejun Heo break; 258073f53c4aSTejun Heo } 258173f53c4aSTejun Heo 258273f53c4aSTejun Heo /* 258373f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2584112202d9STejun Heo * the new first flusher and arm pwqs. 258573f53c4aSTejun Heo */ 25866183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 25876183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 258873f53c4aSTejun Heo 258973f53c4aSTejun Heo list_del_init(&next->list); 259073f53c4aSTejun Heo wq->first_flusher = next; 259173f53c4aSTejun Heo 2592112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 259373f53c4aSTejun Heo break; 259473f53c4aSTejun Heo 259573f53c4aSTejun Heo /* 259673f53c4aSTejun Heo * Meh... this color is already done, clear first 259773f53c4aSTejun Heo * flusher and repeat cascading. 259873f53c4aSTejun Heo */ 259973f53c4aSTejun Heo wq->first_flusher = NULL; 260073f53c4aSTejun Heo } 260173f53c4aSTejun Heo 260273f53c4aSTejun Heo out_unlock: 26033c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 26041da177e4SLinus Torvalds } 2605ae90dd5dSDave Jones EXPORT_SYMBOL_GPL(flush_workqueue); 26061da177e4SLinus Torvalds 26079c5a2ba7STejun Heo /** 26089c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 26099c5a2ba7STejun Heo * @wq: workqueue to drain 26109c5a2ba7STejun Heo * 26119c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 26129c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 26139c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 26149c5a2ba7STejun Heo * repeatedly until it becomes empty. The number of flushing is detemined 26159c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 26169c5a2ba7STejun Heo * takes too long. 26179c5a2ba7STejun Heo */ 26189c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 26199c5a2ba7STejun Heo { 26209c5a2ba7STejun Heo unsigned int flush_cnt = 0; 262149e3cf44STejun Heo struct pool_workqueue *pwq; 26229c5a2ba7STejun Heo 26239c5a2ba7STejun Heo /* 26249c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 26259c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 2626618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 26279c5a2ba7STejun Heo */ 262887fc741eSLai Jiangshan mutex_lock(&wq->mutex); 26299c5a2ba7STejun Heo if (!wq->nr_drainers++) 2630618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 263187fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 26329c5a2ba7STejun Heo reflush: 26339c5a2ba7STejun Heo flush_workqueue(wq); 26349c5a2ba7STejun Heo 2635b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 263676af4d93STejun Heo 263749e3cf44STejun Heo for_each_pwq(pwq, wq) { 2638fa2563e4SThomas Tuttle bool drained; 26399c5a2ba7STejun Heo 2640b09f4fd3SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 2641112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2642b09f4fd3SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 2643fa2563e4SThomas Tuttle 2644fa2563e4SThomas Tuttle if (drained) 26459c5a2ba7STejun Heo continue; 26469c5a2ba7STejun Heo 26479c5a2ba7STejun Heo if (++flush_cnt == 10 || 26489c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2649c5aa87bbSTejun Heo pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n", 26509c5a2ba7STejun Heo wq->name, flush_cnt); 265176af4d93STejun Heo 2652b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 26539c5a2ba7STejun Heo goto reflush; 26549c5a2ba7STejun Heo } 26559c5a2ba7STejun Heo 26569c5a2ba7STejun Heo if (!--wq->nr_drainers) 2657618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 265887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 26599c5a2ba7STejun Heo } 26609c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 26619c5a2ba7STejun Heo 2662606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2663baf59022STejun Heo { 2664baf59022STejun Heo struct worker *worker = NULL; 2665c9e7cf27STejun Heo struct worker_pool *pool; 2666112202d9STejun Heo struct pool_workqueue *pwq; 2667baf59022STejun Heo 2668baf59022STejun Heo might_sleep(); 2669baf59022STejun Heo 2670fa1b54e6STejun Heo local_irq_disable(); 2671fa1b54e6STejun Heo pool = get_work_pool(work); 2672fa1b54e6STejun Heo if (!pool) { 2673fa1b54e6STejun Heo local_irq_enable(); 2674fa1b54e6STejun Heo return false; 2675fa1b54e6STejun Heo } 2676fa1b54e6STejun Heo 2677fa1b54e6STejun Heo spin_lock(&pool->lock); 26780b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2679112202d9STejun Heo pwq = get_work_pwq(work); 2680112202d9STejun Heo if (pwq) { 2681112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2682baf59022STejun Heo goto already_gone; 2683606a5020STejun Heo } else { 2684c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2685baf59022STejun Heo if (!worker) 2686baf59022STejun Heo goto already_gone; 2687112202d9STejun Heo pwq = worker->current_pwq; 2688606a5020STejun Heo } 2689baf59022STejun Heo 2690112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2691d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2692baf59022STejun Heo 2693e159489bSTejun Heo /* 2694e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2695e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2696e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2697e159489bSTejun Heo * access. 2698e159489bSTejun Heo */ 2699493008a8STejun Heo if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer) 2700112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2701e159489bSTejun Heo else 2702112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 2703112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2704e159489bSTejun Heo 2705baf59022STejun Heo return true; 2706baf59022STejun Heo already_gone: 2707d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2708baf59022STejun Heo return false; 2709baf59022STejun Heo } 2710baf59022STejun Heo 2711db700897SOleg Nesterov /** 2712401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2713401a8d04STejun Heo * @work: the work to flush 2714db700897SOleg Nesterov * 2715606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2716606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2717401a8d04STejun Heo * 2718d185af30SYacine Belkadi * Return: 2719401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2720401a8d04STejun Heo * %false if it was already idle. 2721db700897SOleg Nesterov */ 2722401a8d04STejun Heo bool flush_work(struct work_struct *work) 2723db700897SOleg Nesterov { 272412997d1aSBjorn Helgaas struct wq_barrier barr; 272512997d1aSBjorn Helgaas 27260976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 27270976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 27280976dfc1SStephen Boyd 272912997d1aSBjorn Helgaas if (start_flush_work(work, &barr)) { 273012997d1aSBjorn Helgaas wait_for_completion(&barr.done); 273112997d1aSBjorn Helgaas destroy_work_on_stack(&barr.work); 273212997d1aSBjorn Helgaas return true; 273312997d1aSBjorn Helgaas } else { 273412997d1aSBjorn Helgaas return false; 273512997d1aSBjorn Helgaas } 2736606a5020STejun Heo } 2737db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2738db700897SOleg Nesterov 27398603e1b3STejun Heo struct cwt_wait { 27408603e1b3STejun Heo wait_queue_t wait; 27418603e1b3STejun Heo struct work_struct *work; 27428603e1b3STejun Heo }; 27438603e1b3STejun Heo 27448603e1b3STejun Heo static int cwt_wakefn(wait_queue_t *wait, unsigned mode, int sync, void *key) 27458603e1b3STejun Heo { 27468603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 27478603e1b3STejun Heo 27488603e1b3STejun Heo if (cwait->work != key) 27498603e1b3STejun Heo return 0; 27508603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 27518603e1b3STejun Heo } 27528603e1b3STejun Heo 275336e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2754401a8d04STejun Heo { 27558603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 2756bbb68dfaSTejun Heo unsigned long flags; 27571f1f642eSOleg Nesterov int ret; 27581f1f642eSOleg Nesterov 27591f1f642eSOleg Nesterov do { 2760bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2761bbb68dfaSTejun Heo /* 27628603e1b3STejun Heo * If someone else is already canceling, wait for it to 27638603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 27648603e1b3STejun Heo * because we may get scheduled between @work's completion 27658603e1b3STejun Heo * and the other canceling task resuming and clearing 27668603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 27678603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 27688603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 27698603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 27708603e1b3STejun Heo * we're hogging the CPU. 27718603e1b3STejun Heo * 27728603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 27738603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 27748603e1b3STejun Heo * wake function which matches @work along with exclusive 27758603e1b3STejun Heo * wait and wakeup. 2776bbb68dfaSTejun Heo */ 27778603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 27788603e1b3STejun Heo struct cwt_wait cwait; 27798603e1b3STejun Heo 27808603e1b3STejun Heo init_wait(&cwait.wait); 27818603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 27828603e1b3STejun Heo cwait.work = work; 27838603e1b3STejun Heo 27848603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 27858603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 27868603e1b3STejun Heo if (work_is_canceling(work)) 27878603e1b3STejun Heo schedule(); 27888603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 27898603e1b3STejun Heo } 27901f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 27911f1f642eSOleg Nesterov 2792bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2793bbb68dfaSTejun Heo mark_work_canceling(work); 2794bbb68dfaSTejun Heo local_irq_restore(flags); 2795bbb68dfaSTejun Heo 2796606a5020STejun Heo flush_work(work); 27977a22ad75STejun Heo clear_work_data(work); 27988603e1b3STejun Heo 27998603e1b3STejun Heo /* 28008603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 28018603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 28028603e1b3STejun Heo * visible there. 28038603e1b3STejun Heo */ 28048603e1b3STejun Heo smp_mb(); 28058603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 28068603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 28078603e1b3STejun Heo 28081f1f642eSOleg Nesterov return ret; 28091f1f642eSOleg Nesterov } 28101f1f642eSOleg Nesterov 28116e84d644SOleg Nesterov /** 2812401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2813401a8d04STejun Heo * @work: the work to cancel 28146e84d644SOleg Nesterov * 2815401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2816401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2817401a8d04STejun Heo * another workqueue. On return from this function, @work is 2818401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 28191f1f642eSOleg Nesterov * 2820401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2821401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 28226e84d644SOleg Nesterov * 2823401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 28246e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2825401a8d04STejun Heo * 2826d185af30SYacine Belkadi * Return: 2827401a8d04STejun Heo * %true if @work was pending, %false otherwise. 28286e84d644SOleg Nesterov */ 2829401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 28306e84d644SOleg Nesterov { 283136e227d2STejun Heo return __cancel_work_timer(work, false); 2832b89deed3SOleg Nesterov } 283328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2834b89deed3SOleg Nesterov 28356e84d644SOleg Nesterov /** 2836401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2837401a8d04STejun Heo * @dwork: the delayed work to flush 28386e84d644SOleg Nesterov * 2839401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2840401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2841401a8d04STejun Heo * considers the last queueing instance of @dwork. 28421f1f642eSOleg Nesterov * 2843d185af30SYacine Belkadi * Return: 2844401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2845401a8d04STejun Heo * %false if it was already idle. 28466e84d644SOleg Nesterov */ 2847401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2848401a8d04STejun Heo { 28498930cabaSTejun Heo local_irq_disable(); 2850401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 285160c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 28528930cabaSTejun Heo local_irq_enable(); 2853401a8d04STejun Heo return flush_work(&dwork->work); 2854401a8d04STejun Heo } 2855401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2856401a8d04STejun Heo 2857401a8d04STejun Heo /** 285857b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 285957b30ae7STejun Heo * @dwork: delayed_work to cancel 286009383498STejun Heo * 2861d185af30SYacine Belkadi * Kill off a pending delayed_work. 2862d185af30SYacine Belkadi * 2863d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 2864d185af30SYacine Belkadi * pending. 2865d185af30SYacine Belkadi * 2866d185af30SYacine Belkadi * Note: 2867d185af30SYacine Belkadi * The work callback function may still be running on return, unless 2868d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 2869d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 287009383498STejun Heo * 287157b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 287209383498STejun Heo */ 287357b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 287409383498STejun Heo { 287557b30ae7STejun Heo unsigned long flags; 287657b30ae7STejun Heo int ret; 287757b30ae7STejun Heo 287857b30ae7STejun Heo do { 287957b30ae7STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 288057b30ae7STejun Heo } while (unlikely(ret == -EAGAIN)); 288157b30ae7STejun Heo 288257b30ae7STejun Heo if (unlikely(ret < 0)) 288357b30ae7STejun Heo return false; 288457b30ae7STejun Heo 28857c3eed5cSTejun Heo set_work_pool_and_clear_pending(&dwork->work, 28867c3eed5cSTejun Heo get_work_pool_id(&dwork->work)); 288757b30ae7STejun Heo local_irq_restore(flags); 2888c0158ca6SDan Magenheimer return ret; 288909383498STejun Heo } 289057b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 289109383498STejun Heo 289209383498STejun Heo /** 2893401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 2894401a8d04STejun Heo * @dwork: the delayed work cancel 2895401a8d04STejun Heo * 2896401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 2897401a8d04STejun Heo * 2898d185af30SYacine Belkadi * Return: 2899401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 2900401a8d04STejun Heo */ 2901401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 29026e84d644SOleg Nesterov { 290336e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 29046e84d644SOleg Nesterov } 2905f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 29061da177e4SLinus Torvalds 29070fcb78c2SRolf Eike Beer /** 290831ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 2909b6136773SAndrew Morton * @func: the function to call 2910b6136773SAndrew Morton * 291131ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 291231ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 2913b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 291431ddd871STejun Heo * 2915d185af30SYacine Belkadi * Return: 291631ddd871STejun Heo * 0 on success, -errno on failure. 2917b6136773SAndrew Morton */ 291865f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 291915316ba8SChristoph Lameter { 292015316ba8SChristoph Lameter int cpu; 292138f51568SNamhyung Kim struct work_struct __percpu *works; 292215316ba8SChristoph Lameter 2923b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 2924b6136773SAndrew Morton if (!works) 292515316ba8SChristoph Lameter return -ENOMEM; 2926b6136773SAndrew Morton 292795402b38SGautham R Shenoy get_online_cpus(); 292893981800STejun Heo 292915316ba8SChristoph Lameter for_each_online_cpu(cpu) { 29309bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 29319bfb1839SIngo Molnar 29329bfb1839SIngo Molnar INIT_WORK(work, func); 29338de6d308SOleg Nesterov schedule_work_on(cpu, work); 293415316ba8SChristoph Lameter } 293593981800STejun Heo 293693981800STejun Heo for_each_online_cpu(cpu) 29378616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 293893981800STejun Heo 293995402b38SGautham R Shenoy put_online_cpus(); 2940b6136773SAndrew Morton free_percpu(works); 294115316ba8SChristoph Lameter return 0; 294215316ba8SChristoph Lameter } 294315316ba8SChristoph Lameter 2944eef6a7d5SAlan Stern /** 2945eef6a7d5SAlan Stern * flush_scheduled_work - ensure that any scheduled work has run to completion. 2946eef6a7d5SAlan Stern * 2947eef6a7d5SAlan Stern * Forces execution of the kernel-global workqueue and blocks until its 2948eef6a7d5SAlan Stern * completion. 2949eef6a7d5SAlan Stern * 2950eef6a7d5SAlan Stern * Think twice before calling this function! It's very easy to get into 2951eef6a7d5SAlan Stern * trouble if you don't take great care. Either of the following situations 2952eef6a7d5SAlan Stern * will lead to deadlock: 2953eef6a7d5SAlan Stern * 2954eef6a7d5SAlan Stern * One of the work items currently on the workqueue needs to acquire 2955eef6a7d5SAlan Stern * a lock held by your code or its caller. 2956eef6a7d5SAlan Stern * 2957eef6a7d5SAlan Stern * Your code is running in the context of a work routine. 2958eef6a7d5SAlan Stern * 2959eef6a7d5SAlan Stern * They will be detected by lockdep when they occur, but the first might not 2960eef6a7d5SAlan Stern * occur very often. It depends on what work items are on the workqueue and 2961eef6a7d5SAlan Stern * what locks they need, which you have no control over. 2962eef6a7d5SAlan Stern * 2963eef6a7d5SAlan Stern * In most situations flushing the entire workqueue is overkill; you merely 2964eef6a7d5SAlan Stern * need to know that a particular work item isn't queued and isn't running. 2965eef6a7d5SAlan Stern * In such cases you should use cancel_delayed_work_sync() or 2966eef6a7d5SAlan Stern * cancel_work_sync() instead. 2967eef6a7d5SAlan Stern */ 29681da177e4SLinus Torvalds void flush_scheduled_work(void) 29691da177e4SLinus Torvalds { 2970d320c038STejun Heo flush_workqueue(system_wq); 29711da177e4SLinus Torvalds } 2972ae90dd5dSDave Jones EXPORT_SYMBOL(flush_scheduled_work); 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds /** 29751fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 29761fa44ecaSJames Bottomley * @fn: the function to execute 29771fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 29781fa44ecaSJames Bottomley * be available when the work executes) 29791fa44ecaSJames Bottomley * 29801fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 29811fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 29821fa44ecaSJames Bottomley * 2983d185af30SYacine Belkadi * Return: 0 - function was executed 29841fa44ecaSJames Bottomley * 1 - function was scheduled for execution 29851fa44ecaSJames Bottomley */ 298665f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 29871fa44ecaSJames Bottomley { 29881fa44ecaSJames Bottomley if (!in_interrupt()) { 298965f27f38SDavid Howells fn(&ew->work); 29901fa44ecaSJames Bottomley return 0; 29911fa44ecaSJames Bottomley } 29921fa44ecaSJames Bottomley 299365f27f38SDavid Howells INIT_WORK(&ew->work, fn); 29941fa44ecaSJames Bottomley schedule_work(&ew->work); 29951fa44ecaSJames Bottomley 29961fa44ecaSJames Bottomley return 1; 29971fa44ecaSJames Bottomley } 29981fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 29991fa44ecaSJames Bottomley 30007a4e344cSTejun Heo /** 30017a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 30027a4e344cSTejun Heo * @attrs: workqueue_attrs to free 30037a4e344cSTejun Heo * 30047a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 30057a4e344cSTejun Heo */ 30067a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs) 30077a4e344cSTejun Heo { 30087a4e344cSTejun Heo if (attrs) { 30097a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 30107a4e344cSTejun Heo kfree(attrs); 30117a4e344cSTejun Heo } 30127a4e344cSTejun Heo } 30137a4e344cSTejun Heo 30147a4e344cSTejun Heo /** 30157a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 30167a4e344cSTejun Heo * @gfp_mask: allocation mask to use 30177a4e344cSTejun Heo * 30187a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3019d185af30SYacine Belkadi * return it. 3020d185af30SYacine Belkadi * 3021d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 30227a4e344cSTejun Heo */ 30237a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) 30247a4e344cSTejun Heo { 30257a4e344cSTejun Heo struct workqueue_attrs *attrs; 30267a4e344cSTejun Heo 30277a4e344cSTejun Heo attrs = kzalloc(sizeof(*attrs), gfp_mask); 30287a4e344cSTejun Heo if (!attrs) 30297a4e344cSTejun Heo goto fail; 30307a4e344cSTejun Heo if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) 30317a4e344cSTejun Heo goto fail; 30327a4e344cSTejun Heo 303313e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 30347a4e344cSTejun Heo return attrs; 30357a4e344cSTejun Heo fail: 30367a4e344cSTejun Heo free_workqueue_attrs(attrs); 30377a4e344cSTejun Heo return NULL; 30387a4e344cSTejun Heo } 30397a4e344cSTejun Heo 304029c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 304129c91e99STejun Heo const struct workqueue_attrs *from) 304229c91e99STejun Heo { 304329c91e99STejun Heo to->nice = from->nice; 304429c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 30452865a8fbSShaohua Li /* 30462865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 30472865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 30482865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 30492865a8fbSShaohua Li */ 30502865a8fbSShaohua Li to->no_numa = from->no_numa; 305129c91e99STejun Heo } 305229c91e99STejun Heo 305329c91e99STejun Heo /* hash value of the content of @attr */ 305429c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 305529c91e99STejun Heo { 305629c91e99STejun Heo u32 hash = 0; 305729c91e99STejun Heo 305829c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 305913e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 306013e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 306129c91e99STejun Heo return hash; 306229c91e99STejun Heo } 306329c91e99STejun Heo 306429c91e99STejun Heo /* content equality test */ 306529c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 306629c91e99STejun Heo const struct workqueue_attrs *b) 306729c91e99STejun Heo { 306829c91e99STejun Heo if (a->nice != b->nice) 306929c91e99STejun Heo return false; 307029c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 307129c91e99STejun Heo return false; 307229c91e99STejun Heo return true; 307329c91e99STejun Heo } 307429c91e99STejun Heo 30757a4e344cSTejun Heo /** 30767a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 30777a4e344cSTejun Heo * @pool: worker_pool to initialize 30787a4e344cSTejun Heo * 30797a4e344cSTejun Heo * Initiailize a newly zalloc'd @pool. It also allocates @pool->attrs. 3080d185af30SYacine Belkadi * 3081d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 308229c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 308329c91e99STejun Heo * on @pool safely to release it. 30847a4e344cSTejun Heo */ 30857a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 30864e1a1f9aSTejun Heo { 30874e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 308829c91e99STejun Heo pool->id = -1; 308929c91e99STejun Heo pool->cpu = -1; 3090f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 30914e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 30924e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 30934e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 30944e1a1f9aSTejun Heo hash_init(pool->busy_hash); 30954e1a1f9aSTejun Heo 30964e1a1f9aSTejun Heo init_timer_deferrable(&pool->idle_timer); 30974e1a1f9aSTejun Heo pool->idle_timer.function = idle_worker_timeout; 30984e1a1f9aSTejun Heo pool->idle_timer.data = (unsigned long)pool; 30994e1a1f9aSTejun Heo 31004e1a1f9aSTejun Heo setup_timer(&pool->mayday_timer, pool_mayday_timeout, 31014e1a1f9aSTejun Heo (unsigned long)pool); 31024e1a1f9aSTejun Heo 31034e1a1f9aSTejun Heo mutex_init(&pool->manager_arb); 310492f9c5c4SLai Jiangshan mutex_init(&pool->attach_mutex); 3105da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 31067a4e344cSTejun Heo 31077cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 310829c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 310929c91e99STejun Heo pool->refcnt = 1; 311029c91e99STejun Heo 311129c91e99STejun Heo /* shouldn't fail above this point */ 31127a4e344cSTejun Heo pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); 31137a4e344cSTejun Heo if (!pool->attrs) 31147a4e344cSTejun Heo return -ENOMEM; 31157a4e344cSTejun Heo return 0; 31164e1a1f9aSTejun Heo } 31174e1a1f9aSTejun Heo 3118e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3119e2dca7adSTejun Heo { 3120e2dca7adSTejun Heo struct workqueue_struct *wq = 3121e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3122e2dca7adSTejun Heo 3123e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3124e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3125e2dca7adSTejun Heo else 3126e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3127e2dca7adSTejun Heo 3128e2dca7adSTejun Heo kfree(wq->rescuer); 3129e2dca7adSTejun Heo kfree(wq); 3130e2dca7adSTejun Heo } 3131e2dca7adSTejun Heo 313229c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 313329c91e99STejun Heo { 313429c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 313529c91e99STejun Heo 31367cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 313729c91e99STejun Heo free_workqueue_attrs(pool->attrs); 313829c91e99STejun Heo kfree(pool); 313929c91e99STejun Heo } 314029c91e99STejun Heo 314129c91e99STejun Heo /** 314229c91e99STejun Heo * put_unbound_pool - put a worker_pool 314329c91e99STejun Heo * @pool: worker_pool to put 314429c91e99STejun Heo * 314529c91e99STejun Heo * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU 3146c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3147c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3148c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3149a892caccSTejun Heo * 3150a892caccSTejun Heo * Should be called with wq_pool_mutex held. 315129c91e99STejun Heo */ 315229c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 315329c91e99STejun Heo { 315460f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 315529c91e99STejun Heo struct worker *worker; 315629c91e99STejun Heo 3157a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3158a892caccSTejun Heo 3159a892caccSTejun Heo if (--pool->refcnt) 316029c91e99STejun Heo return; 316129c91e99STejun Heo 316229c91e99STejun Heo /* sanity checks */ 316361d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3164a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 316529c91e99STejun Heo return; 316629c91e99STejun Heo 316729c91e99STejun Heo /* release id and unhash */ 316829c91e99STejun Heo if (pool->id >= 0) 316929c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 317029c91e99STejun Heo hash_del(&pool->hash_node); 317129c91e99STejun Heo 3172c5aa87bbSTejun Heo /* 3173c5aa87bbSTejun Heo * Become the manager and destroy all workers. Grabbing 3174c5aa87bbSTejun Heo * manager_arb prevents @pool's workers from blocking on 317592f9c5c4SLai Jiangshan * attach_mutex. 3176c5aa87bbSTejun Heo */ 317729c91e99STejun Heo mutex_lock(&pool->manager_arb); 317829c91e99STejun Heo 317960f5a4bcSLai Jiangshan spin_lock_irq(&pool->lock); 31801037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 318129c91e99STejun Heo destroy_worker(worker); 318229c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 318329c91e99STejun Heo spin_unlock_irq(&pool->lock); 318460f5a4bcSLai Jiangshan 318592f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 3186da028469SLai Jiangshan if (!list_empty(&pool->workers)) 318760f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 318892f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 318960f5a4bcSLai Jiangshan 319060f5a4bcSLai Jiangshan if (pool->detach_completion) 319160f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 319260f5a4bcSLai Jiangshan 319329c91e99STejun Heo mutex_unlock(&pool->manager_arb); 319429c91e99STejun Heo 319529c91e99STejun Heo /* shut down the timers */ 319629c91e99STejun Heo del_timer_sync(&pool->idle_timer); 319729c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 319829c91e99STejun Heo 319929c91e99STejun Heo /* sched-RCU protected to allow dereferences from get_work_pool() */ 320029c91e99STejun Heo call_rcu_sched(&pool->rcu, rcu_free_pool); 320129c91e99STejun Heo } 320229c91e99STejun Heo 320329c91e99STejun Heo /** 320429c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 320529c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 320629c91e99STejun Heo * 320729c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 320829c91e99STejun Heo * reference count and return it. If there already is a matching 320929c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3210d185af30SYacine Belkadi * create a new one. 3211a892caccSTejun Heo * 3212a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3213d185af30SYacine Belkadi * 3214d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3215d185af30SYacine Belkadi * On failure, %NULL. 321629c91e99STejun Heo */ 321729c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 321829c91e99STejun Heo { 321929c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 322029c91e99STejun Heo struct worker_pool *pool; 3221f3f90ad4STejun Heo int node; 322229c91e99STejun Heo 3223a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 322429c91e99STejun Heo 322529c91e99STejun Heo /* do we already have a matching pool? */ 322629c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 322729c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 322829c91e99STejun Heo pool->refcnt++; 32293fb1823cSLai Jiangshan return pool; 323029c91e99STejun Heo } 323129c91e99STejun Heo } 323229c91e99STejun Heo 323329c91e99STejun Heo /* nope, create a new one */ 323429c91e99STejun Heo pool = kzalloc(sizeof(*pool), GFP_KERNEL); 323529c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 323629c91e99STejun Heo goto fail; 323729c91e99STejun Heo 32388864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 323929c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 324029c91e99STejun Heo 32412865a8fbSShaohua Li /* 32422865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 32432865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 32442865a8fbSShaohua Li */ 32452865a8fbSShaohua Li pool->attrs->no_numa = false; 32462865a8fbSShaohua Li 3247f3f90ad4STejun Heo /* if cpumask is contained inside a NUMA node, we belong to that node */ 3248f3f90ad4STejun Heo if (wq_numa_enabled) { 3249f3f90ad4STejun Heo for_each_node(node) { 3250f3f90ad4STejun Heo if (cpumask_subset(pool->attrs->cpumask, 3251f3f90ad4STejun Heo wq_numa_possible_cpumask[node])) { 3252f3f90ad4STejun Heo pool->node = node; 3253f3f90ad4STejun Heo break; 3254f3f90ad4STejun Heo } 3255f3f90ad4STejun Heo } 3256f3f90ad4STejun Heo } 3257f3f90ad4STejun Heo 325829c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 325929c91e99STejun Heo goto fail; 326029c91e99STejun Heo 326129c91e99STejun Heo /* create and start the initial worker */ 3262051e1850SLai Jiangshan if (!create_worker(pool)) 326329c91e99STejun Heo goto fail; 326429c91e99STejun Heo 326529c91e99STejun Heo /* install */ 326629c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 32673fb1823cSLai Jiangshan 326829c91e99STejun Heo return pool; 326929c91e99STejun Heo fail: 327029c91e99STejun Heo if (pool) 327129c91e99STejun Heo put_unbound_pool(pool); 327229c91e99STejun Heo return NULL; 327329c91e99STejun Heo } 327429c91e99STejun Heo 32758864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 32768864b4e5STejun Heo { 32778864b4e5STejun Heo kmem_cache_free(pwq_cache, 32788864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 32798864b4e5STejun Heo } 32808864b4e5STejun Heo 32818864b4e5STejun Heo /* 32828864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 32838864b4e5STejun Heo * and needs to be destroyed. 32848864b4e5STejun Heo */ 32858864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 32868864b4e5STejun Heo { 32878864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 32888864b4e5STejun Heo unbound_release_work); 32898864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 32908864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3291bc0caf09STejun Heo bool is_last; 32928864b4e5STejun Heo 32938864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 32948864b4e5STejun Heo return; 32958864b4e5STejun Heo 32963c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 32978864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3298bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 32993c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 33008864b4e5STejun Heo 3301a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 33028864b4e5STejun Heo put_unbound_pool(pool); 3303a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3304a892caccSTejun Heo 33058864b4e5STejun Heo call_rcu_sched(&pwq->rcu, rcu_free_pwq); 33068864b4e5STejun Heo 33078864b4e5STejun Heo /* 33088864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3309e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 33108864b4e5STejun Heo */ 3311e2dca7adSTejun Heo if (is_last) 3312e2dca7adSTejun Heo call_rcu_sched(&wq->rcu, rcu_free_wq); 33136029a918STejun Heo } 33148864b4e5STejun Heo 33150fbd95aaSTejun Heo /** 3316699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 33170fbd95aaSTejun Heo * @pwq: target pool_workqueue 33180fbd95aaSTejun Heo * 3319699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3320699ce097STejun Heo * workqueue's saved_max_active and activate delayed work items 3321699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 33220fbd95aaSTejun Heo */ 3323699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 33240fbd95aaSTejun Heo { 3325699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3326699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 3327699ce097STejun Heo 3328699ce097STejun Heo /* for @wq->saved_max_active */ 3329a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3330699ce097STejun Heo 3331699ce097STejun Heo /* fast exit for non-freezable wqs */ 3332699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3333699ce097STejun Heo return; 3334699ce097STejun Heo 3335a357fc03SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 3336699ce097STejun Heo 333774b414eaSLai Jiangshan /* 333874b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 333974b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 334074b414eaSLai Jiangshan * is updated and visible. 334174b414eaSLai Jiangshan */ 334274b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 3343699ce097STejun Heo pwq->max_active = wq->saved_max_active; 33440fbd95aaSTejun Heo 33450fbd95aaSTejun Heo while (!list_empty(&pwq->delayed_works) && 33460fbd95aaSTejun Heo pwq->nr_active < pwq->max_active) 33470fbd95aaSTejun Heo pwq_activate_first_delayed(pwq); 3348951a078aSLai Jiangshan 3349951a078aSLai Jiangshan /* 3350951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 3351951a078aSLai Jiangshan * max_active is bumped. It's a slow path. Do it always. 3352951a078aSLai Jiangshan */ 3353951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3354699ce097STejun Heo } else { 3355699ce097STejun Heo pwq->max_active = 0; 3356699ce097STejun Heo } 3357699ce097STejun Heo 3358a357fc03SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 33590fbd95aaSTejun Heo } 33600fbd95aaSTejun Heo 3361e50aba9aSTejun Heo /* initialize newly alloced @pwq which is associated with @wq and @pool */ 3362f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3363f147f29eSTejun Heo struct worker_pool *pool) 3364d2c1d404STejun Heo { 3365d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3366d2c1d404STejun Heo 3367e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3368e50aba9aSTejun Heo 3369d2c1d404STejun Heo pwq->pool = pool; 3370d2c1d404STejun Heo pwq->wq = wq; 3371d2c1d404STejun Heo pwq->flush_color = -1; 33728864b4e5STejun Heo pwq->refcnt = 1; 3373d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 33741befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3375d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 33768864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3377f147f29eSTejun Heo } 3378d2c1d404STejun Heo 3379f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 33801befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3381f147f29eSTejun Heo { 3382f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3383f147f29eSTejun Heo 3384f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 338575ccf595STejun Heo 33861befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 33871befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 33881befcf30STejun Heo return; 33891befcf30STejun Heo 339029b1cb41SLai Jiangshan /* set the matching work_color */ 339175ccf595STejun Heo pwq->work_color = wq->work_color; 3392983ca25eSTejun Heo 3393983ca25eSTejun Heo /* sync max_active to the current setting */ 3394983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3395983ca25eSTejun Heo 3396983ca25eSTejun Heo /* link in @pwq */ 33979e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3398df2d5ae4STejun Heo } 33996029a918STejun Heo 3400f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3401f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3402f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3403f147f29eSTejun Heo { 3404f147f29eSTejun Heo struct worker_pool *pool; 3405f147f29eSTejun Heo struct pool_workqueue *pwq; 3406f147f29eSTejun Heo 3407f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3408f147f29eSTejun Heo 3409f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3410f147f29eSTejun Heo if (!pool) 3411f147f29eSTejun Heo return NULL; 3412f147f29eSTejun Heo 3413e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3414f147f29eSTejun Heo if (!pwq) { 3415f147f29eSTejun Heo put_unbound_pool(pool); 3416f147f29eSTejun Heo return NULL; 3417f147f29eSTejun Heo } 3418f147f29eSTejun Heo 3419f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3420f147f29eSTejun Heo return pwq; 3421d2c1d404STejun Heo } 3422d2c1d404STejun Heo 34234c16bd32STejun Heo /* undo alloc_unbound_pwq(), used only in the error path */ 34244c16bd32STejun Heo static void free_unbound_pwq(struct pool_workqueue *pwq) 34254c16bd32STejun Heo { 34264c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 34274c16bd32STejun Heo 34284c16bd32STejun Heo if (pwq) { 34294c16bd32STejun Heo put_unbound_pool(pwq->pool); 3430cece95dfSWei Yongjun kmem_cache_free(pwq_cache, pwq); 34314c16bd32STejun Heo } 34324c16bd32STejun Heo } 34334c16bd32STejun Heo 34344c16bd32STejun Heo /** 34354c16bd32STejun Heo * wq_calc_node_mask - calculate a wq_attrs' cpumask for the specified node 34364c16bd32STejun Heo * @attrs: the wq_attrs of interest 34374c16bd32STejun Heo * @node: the target NUMA node 34384c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 34394c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 34404c16bd32STejun Heo * 34414c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 34424c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3443d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 34444c16bd32STejun Heo * 34454c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 34464c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 34474c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 34484c16bd32STejun Heo * @attrs->cpumask. 34494c16bd32STejun Heo * 34504c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 34514c16bd32STejun Heo * stable. 3452d185af30SYacine Belkadi * 3453d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 3454d185af30SYacine Belkadi * %false if equal. 34554c16bd32STejun Heo */ 34564c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 34574c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 34584c16bd32STejun Heo { 3459d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 34604c16bd32STejun Heo goto use_dfl; 34614c16bd32STejun Heo 34624c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 34634c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 34644c16bd32STejun Heo if (cpu_going_down >= 0) 34654c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 34664c16bd32STejun Heo 34674c16bd32STejun Heo if (cpumask_empty(cpumask)) 34684c16bd32STejun Heo goto use_dfl; 34694c16bd32STejun Heo 34704c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 34714c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 34724c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 34734c16bd32STejun Heo 34744c16bd32STejun Heo use_dfl: 34754c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 34764c16bd32STejun Heo return false; 34774c16bd32STejun Heo } 34784c16bd32STejun Heo 34791befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 34801befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 34811befcf30STejun Heo int node, 34821befcf30STejun Heo struct pool_workqueue *pwq) 34831befcf30STejun Heo { 34841befcf30STejun Heo struct pool_workqueue *old_pwq; 34851befcf30STejun Heo 34861befcf30STejun Heo lockdep_assert_held(&wq->mutex); 34871befcf30STejun Heo 34881befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 34891befcf30STejun Heo link_pwq(pwq); 34901befcf30STejun Heo 34911befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 34921befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 34931befcf30STejun Heo return old_pwq; 34941befcf30STejun Heo } 34951befcf30STejun Heo 34969e8cd2f5STejun Heo /** 34979e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 34989e8cd2f5STejun Heo * @wq: the target workqueue 34999e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 35009e8cd2f5STejun Heo * 35014c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 35024c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 35034c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 35044c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 35054c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 35064c16bd32STejun Heo * back-to-back will stay on its current pwq. 35079e8cd2f5STejun Heo * 3508d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 3509d185af30SYacine Belkadi * 3510d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 35119e8cd2f5STejun Heo */ 35129e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 35139e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 35149e8cd2f5STejun Heo { 35154c16bd32STejun Heo struct workqueue_attrs *new_attrs, *tmp_attrs; 35164c16bd32STejun Heo struct pool_workqueue **pwq_tbl, *dfl_pwq; 3517f147f29eSTejun Heo int node, ret; 35189e8cd2f5STejun Heo 35198719dceaSTejun Heo /* only unbound workqueues can change attributes */ 35209e8cd2f5STejun Heo if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 35219e8cd2f5STejun Heo return -EINVAL; 35229e8cd2f5STejun Heo 35238719dceaSTejun Heo /* creating multiple pwqs breaks ordering guarantee */ 35248719dceaSTejun Heo if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs))) 35258719dceaSTejun Heo return -EINVAL; 35268719dceaSTejun Heo 3527ddcb57e2SLai Jiangshan pwq_tbl = kzalloc(nr_node_ids * sizeof(pwq_tbl[0]), GFP_KERNEL); 352813e2e556STejun Heo new_attrs = alloc_workqueue_attrs(GFP_KERNEL); 35294c16bd32STejun Heo tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL); 35304c16bd32STejun Heo if (!pwq_tbl || !new_attrs || !tmp_attrs) 353113e2e556STejun Heo goto enomem; 353213e2e556STejun Heo 35334c16bd32STejun Heo /* make a copy of @attrs and sanitize it */ 353413e2e556STejun Heo copy_workqueue_attrs(new_attrs, attrs); 353513e2e556STejun Heo cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 353613e2e556STejun Heo 35374c16bd32STejun Heo /* 35384c16bd32STejun Heo * We may create multiple pwqs with differing cpumasks. Make a 35394c16bd32STejun Heo * copy of @new_attrs which will be modified and used to obtain 35404c16bd32STejun Heo * pools. 35414c16bd32STejun Heo */ 35424c16bd32STejun Heo copy_workqueue_attrs(tmp_attrs, new_attrs); 35439e8cd2f5STejun Heo 35444c16bd32STejun Heo /* 35454c16bd32STejun Heo * CPUs should stay stable across pwq creations and installations. 35464c16bd32STejun Heo * Pin CPUs, determine the target cpumask for each node and create 35474c16bd32STejun Heo * pwqs accordingly. 35484c16bd32STejun Heo */ 35494c16bd32STejun Heo get_online_cpus(); 35504c16bd32STejun Heo 35514c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 35524c16bd32STejun Heo 35534c16bd32STejun Heo /* 35544c16bd32STejun Heo * If something goes wrong during CPU up/down, we'll fall back to 35554c16bd32STejun Heo * the default pwq covering whole @attrs->cpumask. Always create 35564c16bd32STejun Heo * it even if we don't use it immediately. 35574c16bd32STejun Heo */ 35584c16bd32STejun Heo dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 35594c16bd32STejun Heo if (!dfl_pwq) 35604c16bd32STejun Heo goto enomem_pwq; 35614c16bd32STejun Heo 35624c16bd32STejun Heo for_each_node(node) { 35634c16bd32STejun Heo if (wq_calc_node_cpumask(attrs, node, -1, tmp_attrs->cpumask)) { 35644c16bd32STejun Heo pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 35654c16bd32STejun Heo if (!pwq_tbl[node]) 35664c16bd32STejun Heo goto enomem_pwq; 35674c16bd32STejun Heo } else { 35684c16bd32STejun Heo dfl_pwq->refcnt++; 35694c16bd32STejun Heo pwq_tbl[node] = dfl_pwq; 35704c16bd32STejun Heo } 35714c16bd32STejun Heo } 35724c16bd32STejun Heo 35734c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 35744c16bd32STejun Heo 35754c16bd32STejun Heo /* all pwqs have been created successfully, let's install'em */ 3576f147f29eSTejun Heo mutex_lock(&wq->mutex); 3577a892caccSTejun Heo 3578f147f29eSTejun Heo copy_workqueue_attrs(wq->unbound_attrs, new_attrs); 35794c16bd32STejun Heo 35804c16bd32STejun Heo /* save the previous pwq and install the new one */ 3581f147f29eSTejun Heo for_each_node(node) 35824c16bd32STejun Heo pwq_tbl[node] = numa_pwq_tbl_install(wq, node, pwq_tbl[node]); 35834c16bd32STejun Heo 35844c16bd32STejun Heo /* @dfl_pwq might not have been used, ensure it's linked */ 35854c16bd32STejun Heo link_pwq(dfl_pwq); 35864c16bd32STejun Heo swap(wq->dfl_pwq, dfl_pwq); 3587f147f29eSTejun Heo 3588f147f29eSTejun Heo mutex_unlock(&wq->mutex); 3589f147f29eSTejun Heo 35904c16bd32STejun Heo /* put the old pwqs */ 35914c16bd32STejun Heo for_each_node(node) 35924c16bd32STejun Heo put_pwq_unlocked(pwq_tbl[node]); 35934c16bd32STejun Heo put_pwq_unlocked(dfl_pwq); 35944c16bd32STejun Heo 35954c16bd32STejun Heo put_online_cpus(); 35964862125bSTejun Heo ret = 0; 35974862125bSTejun Heo /* fall through */ 35984862125bSTejun Heo out_free: 35994c16bd32STejun Heo free_workqueue_attrs(tmp_attrs); 36004862125bSTejun Heo free_workqueue_attrs(new_attrs); 36014c16bd32STejun Heo kfree(pwq_tbl); 36024862125bSTejun Heo return ret; 360313e2e556STejun Heo 36044c16bd32STejun Heo enomem_pwq: 36054c16bd32STejun Heo free_unbound_pwq(dfl_pwq); 36064c16bd32STejun Heo for_each_node(node) 36074c16bd32STejun Heo if (pwq_tbl && pwq_tbl[node] != dfl_pwq) 36084c16bd32STejun Heo free_unbound_pwq(pwq_tbl[node]); 36094c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 36104c16bd32STejun Heo put_online_cpus(); 361113e2e556STejun Heo enomem: 36124862125bSTejun Heo ret = -ENOMEM; 36134862125bSTejun Heo goto out_free; 36149e8cd2f5STejun Heo } 36159e8cd2f5STejun Heo 36164c16bd32STejun Heo /** 36174c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 36184c16bd32STejun Heo * @wq: the target workqueue 36194c16bd32STejun Heo * @cpu: the CPU coming up or going down 36204c16bd32STejun Heo * @online: whether @cpu is coming up or going down 36214c16bd32STejun Heo * 36224c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 36234c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 36244c16bd32STejun Heo * @wq accordingly. 36254c16bd32STejun Heo * 36264c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 36274c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 36284c16bd32STejun Heo * correct. 36294c16bd32STejun Heo * 36304c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 36314c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 36324c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 36334c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 36344c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 36354c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 36364c16bd32STejun Heo * CPU_DOWN_PREPARE. 36374c16bd32STejun Heo */ 36384c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 36394c16bd32STejun Heo bool online) 36404c16bd32STejun Heo { 36414c16bd32STejun Heo int node = cpu_to_node(cpu); 36424c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 36434c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 36444c16bd32STejun Heo struct workqueue_attrs *target_attrs; 36454c16bd32STejun Heo cpumask_t *cpumask; 36464c16bd32STejun Heo 36474c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 36484c16bd32STejun Heo 36494c16bd32STejun Heo if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND)) 36504c16bd32STejun Heo return; 36514c16bd32STejun Heo 36524c16bd32STejun Heo /* 36534c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 36544c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 36554c16bd32STejun Heo * CPU hotplug exclusion. 36564c16bd32STejun Heo */ 36574c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 36584c16bd32STejun Heo cpumask = target_attrs->cpumask; 36594c16bd32STejun Heo 36604c16bd32STejun Heo mutex_lock(&wq->mutex); 3661d55262c4STejun Heo if (wq->unbound_attrs->no_numa) 3662d55262c4STejun Heo goto out_unlock; 36634c16bd32STejun Heo 36644c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 36654c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 36664c16bd32STejun Heo 36674c16bd32STejun Heo /* 36684c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 36694c16bd32STejun Heo * different from wq's, we need to compare it to @pwq's and create 36704c16bd32STejun Heo * a new one if they don't match. If the target cpumask equals 3671534a3fbbSDaeseok Youn * wq's, the default pwq should be used. 36724c16bd32STejun Heo */ 36734c16bd32STejun Heo if (wq_calc_node_cpumask(wq->unbound_attrs, node, cpu_off, cpumask)) { 36744c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 36754c16bd32STejun Heo goto out_unlock; 36764c16bd32STejun Heo } else { 36774c16bd32STejun Heo goto use_dfl_pwq; 36784c16bd32STejun Heo } 36794c16bd32STejun Heo 36804c16bd32STejun Heo mutex_unlock(&wq->mutex); 36814c16bd32STejun Heo 36824c16bd32STejun Heo /* create a new pwq */ 36834c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 36844c16bd32STejun Heo if (!pwq) { 36852d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 36864c16bd32STejun Heo wq->name); 368777f300b1SDaeseok Youn mutex_lock(&wq->mutex); 368877f300b1SDaeseok Youn goto use_dfl_pwq; 36894c16bd32STejun Heo } 36904c16bd32STejun Heo 36914c16bd32STejun Heo /* 36924c16bd32STejun Heo * Install the new pwq. As this function is called only from CPU 36934c16bd32STejun Heo * hotplug callbacks and applying a new attrs is wrapped with 36944c16bd32STejun Heo * get/put_online_cpus(), @wq->unbound_attrs couldn't have changed 36954c16bd32STejun Heo * inbetween. 36964c16bd32STejun Heo */ 36974c16bd32STejun Heo mutex_lock(&wq->mutex); 36984c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 36994c16bd32STejun Heo goto out_unlock; 37004c16bd32STejun Heo 37014c16bd32STejun Heo use_dfl_pwq: 37024c16bd32STejun Heo spin_lock_irq(&wq->dfl_pwq->pool->lock); 37034c16bd32STejun Heo get_pwq(wq->dfl_pwq); 37044c16bd32STejun Heo spin_unlock_irq(&wq->dfl_pwq->pool->lock); 37054c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 37064c16bd32STejun Heo out_unlock: 37074c16bd32STejun Heo mutex_unlock(&wq->mutex); 37084c16bd32STejun Heo put_pwq_unlocked(old_pwq); 37094c16bd32STejun Heo } 37104c16bd32STejun Heo 371130cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 37121da177e4SLinus Torvalds { 371349e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 37148a2b7538STejun Heo int cpu, ret; 3715e1d8aa9fSFrederic Weisbecker 371630cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3717420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3718420c0ddbSTejun Heo if (!wq->cpu_pwqs) 371930cdf249STejun Heo return -ENOMEM; 372030cdf249STejun Heo 372130cdf249STejun Heo for_each_possible_cpu(cpu) { 37227fb98ea7STejun Heo struct pool_workqueue *pwq = 37237fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 37247a62c2c8STejun Heo struct worker_pool *cpu_pools = 3725f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 372630cdf249STejun Heo 3727f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 3728f147f29eSTejun Heo 3729f147f29eSTejun Heo mutex_lock(&wq->mutex); 37301befcf30STejun Heo link_pwq(pwq); 3731f147f29eSTejun Heo mutex_unlock(&wq->mutex); 373230cdf249STejun Heo } 373330cdf249STejun Heo return 0; 37348a2b7538STejun Heo } else if (wq->flags & __WQ_ORDERED) { 37358a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 37368a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 37378a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 37388a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 37398a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 37408a2b7538STejun Heo return ret; 37419e8cd2f5STejun Heo } else { 37429e8cd2f5STejun Heo return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 37439e8cd2f5STejun Heo } 37440f900049STejun Heo } 37450f900049STejun Heo 3746f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3747f3421797STejun Heo const char *name) 3748b71ab8c2STejun Heo { 3749f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3750f3421797STejun Heo 3751f3421797STejun Heo if (max_active < 1 || max_active > lim) 3752044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3753f3421797STejun Heo max_active, name, 1, lim); 3754b71ab8c2STejun Heo 3755f3421797STejun Heo return clamp_val(max_active, 1, lim); 3756b71ab8c2STejun Heo } 3757b71ab8c2STejun Heo 3758b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 375997e37d7bSTejun Heo unsigned int flags, 37601e19ffc6STejun Heo int max_active, 3761eb13ba87SJohannes Berg struct lock_class_key *key, 3762b196be89STejun Heo const char *lock_name, ...) 37633af24433SOleg Nesterov { 3764df2d5ae4STejun Heo size_t tbl_size = 0; 3765ecf6881fSTejun Heo va_list args; 37663af24433SOleg Nesterov struct workqueue_struct *wq; 376749e3cf44STejun Heo struct pool_workqueue *pwq; 3768b196be89STejun Heo 3769cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 3770cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 3771cee22a15SViresh Kumar flags |= WQ_UNBOUND; 3772cee22a15SViresh Kumar 3773ecf6881fSTejun Heo /* allocate wq and format name */ 3774df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 3775ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 3776df2d5ae4STejun Heo 3777df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 3778b196be89STejun Heo if (!wq) 3779d2c1d404STejun Heo return NULL; 3780b196be89STejun Heo 37816029a918STejun Heo if (flags & WQ_UNBOUND) { 37826029a918STejun Heo wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL); 37836029a918STejun Heo if (!wq->unbound_attrs) 37846029a918STejun Heo goto err_free_wq; 37856029a918STejun Heo } 37866029a918STejun Heo 3787ecf6881fSTejun Heo va_start(args, lock_name); 3788ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 3789b196be89STejun Heo va_end(args); 37903af24433SOleg Nesterov 3791d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3792b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 37933af24433SOleg Nesterov 3794b196be89STejun Heo /* init wq */ 379597e37d7bSTejun Heo wq->flags = flags; 3796a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 37973c25a55dSLai Jiangshan mutex_init(&wq->mutex); 3798112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 379930cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 380073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 380173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 3802493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 38033af24433SOleg Nesterov 3804eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3805cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 38063af24433SOleg Nesterov 380730cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 3808d2c1d404STejun Heo goto err_free_wq; 38091537663fSTejun Heo 3810493008a8STejun Heo /* 3811493008a8STejun Heo * Workqueues which may be used during memory reclaim should 3812493008a8STejun Heo * have a rescuer to guarantee forward progress. 3813493008a8STejun Heo */ 3814493008a8STejun Heo if (flags & WQ_MEM_RECLAIM) { 3815e22bee78STejun Heo struct worker *rescuer; 3816e22bee78STejun Heo 3817f7537df5SLai Jiangshan rescuer = alloc_worker(NUMA_NO_NODE); 3818e22bee78STejun Heo if (!rescuer) 3819d2c1d404STejun Heo goto err_destroy; 3820e22bee78STejun Heo 3821111c225aSTejun Heo rescuer->rescue_wq = wq; 3822111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 3823b196be89STejun Heo wq->name); 3824d2c1d404STejun Heo if (IS_ERR(rescuer->task)) { 3825d2c1d404STejun Heo kfree(rescuer); 3826d2c1d404STejun Heo goto err_destroy; 3827d2c1d404STejun Heo } 3828e22bee78STejun Heo 3829d2c1d404STejun Heo wq->rescuer = rescuer; 383014a40ffcSTejun Heo rescuer->task->flags |= PF_NO_SETAFFINITY; 3831e22bee78STejun Heo wake_up_process(rescuer->task); 38323af24433SOleg Nesterov } 38331537663fSTejun Heo 3834226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 3835226223abSTejun Heo goto err_destroy; 3836226223abSTejun Heo 38373af24433SOleg Nesterov /* 383868e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 383968e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 384068e13a67SLai Jiangshan * list. 38413af24433SOleg Nesterov */ 384268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 3843a0a1a5fdSTejun Heo 3844a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 384549e3cf44STejun Heo for_each_pwq(pwq, wq) 3846699ce097STejun Heo pwq_adjust_max_active(pwq); 3847a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 3848a0a1a5fdSTejun Heo 3849e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 3850a0a1a5fdSTejun Heo 385168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 38523af24433SOleg Nesterov 38533af24433SOleg Nesterov return wq; 3854d2c1d404STejun Heo 3855d2c1d404STejun Heo err_free_wq: 38566029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 38574690c4abSTejun Heo kfree(wq); 3858d2c1d404STejun Heo return NULL; 3859d2c1d404STejun Heo err_destroy: 3860d2c1d404STejun Heo destroy_workqueue(wq); 38614690c4abSTejun Heo return NULL; 38621da177e4SLinus Torvalds } 3863d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 38641da177e4SLinus Torvalds 38653af24433SOleg Nesterov /** 38663af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 38673af24433SOleg Nesterov * @wq: target workqueue 38683af24433SOleg Nesterov * 38693af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 38703af24433SOleg Nesterov */ 38713af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 38723af24433SOleg Nesterov { 387349e3cf44STejun Heo struct pool_workqueue *pwq; 38744c16bd32STejun Heo int node; 38753af24433SOleg Nesterov 38769c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 38779c5a2ba7STejun Heo drain_workqueue(wq); 3878c8efcc25STejun Heo 38796183c009STejun Heo /* sanity checks */ 3880b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 388149e3cf44STejun Heo for_each_pwq(pwq, wq) { 38826183c009STejun Heo int i; 38836183c009STejun Heo 388476af4d93STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) { 388576af4d93STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) { 3886b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 38876183c009STejun Heo return; 388876af4d93STejun Heo } 388976af4d93STejun Heo } 389076af4d93STejun Heo 38915c529597SLai Jiangshan if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) || 38928864b4e5STejun Heo WARN_ON(pwq->nr_active) || 389376af4d93STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) { 3894b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 38956183c009STejun Heo return; 38966183c009STejun Heo } 389776af4d93STejun Heo } 3898b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 38996183c009STejun Heo 3900a0a1a5fdSTejun Heo /* 3901a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 3902a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 3903a0a1a5fdSTejun Heo */ 390468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 3905e2dca7adSTejun Heo list_del_rcu(&wq->list); 390668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 39073af24433SOleg Nesterov 3908226223abSTejun Heo workqueue_sysfs_unregister(wq); 3909226223abSTejun Heo 3910e2dca7adSTejun Heo if (wq->rescuer) 3911e22bee78STejun Heo kthread_stop(wq->rescuer->task); 3912e22bee78STejun Heo 39138864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 391429c91e99STejun Heo /* 39158864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 3916e2dca7adSTejun Heo * schedule RCU free. 391729c91e99STejun Heo */ 3918e2dca7adSTejun Heo call_rcu_sched(&wq->rcu, rcu_free_wq); 39198864b4e5STejun Heo } else { 39208864b4e5STejun Heo /* 39218864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 39224c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 39234c16bd32STejun Heo * @wq will be freed when the last pwq is released. 39248864b4e5STejun Heo */ 39254c16bd32STejun Heo for_each_node(node) { 39264c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 39274c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 39284c16bd32STejun Heo put_pwq_unlocked(pwq); 39294c16bd32STejun Heo } 39304c16bd32STejun Heo 39314c16bd32STejun Heo /* 39324c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 39334c16bd32STejun Heo * put. Don't access it afterwards. 39344c16bd32STejun Heo */ 39354c16bd32STejun Heo pwq = wq->dfl_pwq; 39364c16bd32STejun Heo wq->dfl_pwq = NULL; 3937dce90d47STejun Heo put_pwq_unlocked(pwq); 393829c91e99STejun Heo } 39393af24433SOleg Nesterov } 39403af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 39413af24433SOleg Nesterov 3942dcd989cbSTejun Heo /** 3943dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 3944dcd989cbSTejun Heo * @wq: target workqueue 3945dcd989cbSTejun Heo * @max_active: new max_active value. 3946dcd989cbSTejun Heo * 3947dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 3948dcd989cbSTejun Heo * 3949dcd989cbSTejun Heo * CONTEXT: 3950dcd989cbSTejun Heo * Don't call from IRQ context. 3951dcd989cbSTejun Heo */ 3952dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 3953dcd989cbSTejun Heo { 395449e3cf44STejun Heo struct pool_workqueue *pwq; 3955dcd989cbSTejun Heo 39568719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 39578719dceaSTejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 39588719dceaSTejun Heo return; 39598719dceaSTejun Heo 3960f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 3961dcd989cbSTejun Heo 3962a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 3963dcd989cbSTejun Heo 3964dcd989cbSTejun Heo wq->saved_max_active = max_active; 3965dcd989cbSTejun Heo 3966699ce097STejun Heo for_each_pwq(pwq, wq) 3967699ce097STejun Heo pwq_adjust_max_active(pwq); 3968dcd989cbSTejun Heo 3969a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 3970dcd989cbSTejun Heo } 3971dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 3972dcd989cbSTejun Heo 3973dcd989cbSTejun Heo /** 3974e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 3975e6267616STejun Heo * 3976e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 3977e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 3978d185af30SYacine Belkadi * 3979d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 3980e6267616STejun Heo */ 3981e6267616STejun Heo bool current_is_workqueue_rescuer(void) 3982e6267616STejun Heo { 3983e6267616STejun Heo struct worker *worker = current_wq_worker(); 3984e6267616STejun Heo 39856a092dfdSLai Jiangshan return worker && worker->rescue_wq; 3986e6267616STejun Heo } 3987e6267616STejun Heo 3988e6267616STejun Heo /** 3989dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 3990dcd989cbSTejun Heo * @cpu: CPU in question 3991dcd989cbSTejun Heo * @wq: target workqueue 3992dcd989cbSTejun Heo * 3993dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 3994dcd989cbSTejun Heo * no synchronization around this function and the test result is 3995dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 3996dcd989cbSTejun Heo * 3997d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 3998d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 3999d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4000d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4001d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4002d3251859STejun Heo * 4003d185af30SYacine Belkadi * Return: 4004dcd989cbSTejun Heo * %true if congested, %false otherwise. 4005dcd989cbSTejun Heo */ 4006d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4007dcd989cbSTejun Heo { 40087fb98ea7STejun Heo struct pool_workqueue *pwq; 400976af4d93STejun Heo bool ret; 401076af4d93STejun Heo 401188109453SLai Jiangshan rcu_read_lock_sched(); 40127fb98ea7STejun Heo 4013d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4014d3251859STejun Heo cpu = smp_processor_id(); 4015d3251859STejun Heo 40167fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 40177fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 40187fb98ea7STejun Heo else 4019df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4020dcd989cbSTejun Heo 402176af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 402288109453SLai Jiangshan rcu_read_unlock_sched(); 402376af4d93STejun Heo 402476af4d93STejun Heo return ret; 4025dcd989cbSTejun Heo } 4026dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4027dcd989cbSTejun Heo 4028dcd989cbSTejun Heo /** 4029dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4030dcd989cbSTejun Heo * @work: the work to be tested 4031dcd989cbSTejun Heo * 4032dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4033dcd989cbSTejun Heo * synchronization around this function and the test result is 4034dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4035dcd989cbSTejun Heo * 4036d185af30SYacine Belkadi * Return: 4037dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4038dcd989cbSTejun Heo */ 4039dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4040dcd989cbSTejun Heo { 4041fa1b54e6STejun Heo struct worker_pool *pool; 4042dcd989cbSTejun Heo unsigned long flags; 4043dcd989cbSTejun Heo unsigned int ret = 0; 4044dcd989cbSTejun Heo 4045dcd989cbSTejun Heo if (work_pending(work)) 4046dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4047038366c5SLai Jiangshan 4048fa1b54e6STejun Heo local_irq_save(flags); 4049fa1b54e6STejun Heo pool = get_work_pool(work); 4050038366c5SLai Jiangshan if (pool) { 4051fa1b54e6STejun Heo spin_lock(&pool->lock); 4052c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4053dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4054fa1b54e6STejun Heo spin_unlock(&pool->lock); 4055038366c5SLai Jiangshan } 4056fa1b54e6STejun Heo local_irq_restore(flags); 4057dcd989cbSTejun Heo 4058dcd989cbSTejun Heo return ret; 4059dcd989cbSTejun Heo } 4060dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4061dcd989cbSTejun Heo 40623d1cb205STejun Heo /** 40633d1cb205STejun Heo * set_worker_desc - set description for the current work item 40643d1cb205STejun Heo * @fmt: printf-style format string 40653d1cb205STejun Heo * @...: arguments for the format string 40663d1cb205STejun Heo * 40673d1cb205STejun Heo * This function can be called by a running work function to describe what 40683d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 40693d1cb205STejun Heo * information will be printed out together to help debugging. The 40703d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 40713d1cb205STejun Heo */ 40723d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 40733d1cb205STejun Heo { 40743d1cb205STejun Heo struct worker *worker = current_wq_worker(); 40753d1cb205STejun Heo va_list args; 40763d1cb205STejun Heo 40773d1cb205STejun Heo if (worker) { 40783d1cb205STejun Heo va_start(args, fmt); 40793d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 40803d1cb205STejun Heo va_end(args); 40813d1cb205STejun Heo worker->desc_valid = true; 40823d1cb205STejun Heo } 40833d1cb205STejun Heo } 40843d1cb205STejun Heo 40853d1cb205STejun Heo /** 40863d1cb205STejun Heo * print_worker_info - print out worker information and description 40873d1cb205STejun Heo * @log_lvl: the log level to use when printing 40883d1cb205STejun Heo * @task: target task 40893d1cb205STejun Heo * 40903d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 40913d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 40923d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 40933d1cb205STejun Heo * 40943d1cb205STejun Heo * This function can be safely called on any task as long as the 40953d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 40963d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 40973d1cb205STejun Heo */ 40983d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 40993d1cb205STejun Heo { 41003d1cb205STejun Heo work_func_t *fn = NULL; 41013d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 41023d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 41033d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 41043d1cb205STejun Heo struct workqueue_struct *wq = NULL; 41053d1cb205STejun Heo bool desc_valid = false; 41063d1cb205STejun Heo struct worker *worker; 41073d1cb205STejun Heo 41083d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 41093d1cb205STejun Heo return; 41103d1cb205STejun Heo 41113d1cb205STejun Heo /* 41123d1cb205STejun Heo * This function is called without any synchronization and @task 41133d1cb205STejun Heo * could be in any state. Be careful with dereferences. 41143d1cb205STejun Heo */ 41153d1cb205STejun Heo worker = probe_kthread_data(task); 41163d1cb205STejun Heo 41173d1cb205STejun Heo /* 41183d1cb205STejun Heo * Carefully copy the associated workqueue's workfn and name. Keep 41193d1cb205STejun Heo * the original last '\0' in case the original contains garbage. 41203d1cb205STejun Heo */ 41213d1cb205STejun Heo probe_kernel_read(&fn, &worker->current_func, sizeof(fn)); 41223d1cb205STejun Heo probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq)); 41233d1cb205STejun Heo probe_kernel_read(&wq, &pwq->wq, sizeof(wq)); 41243d1cb205STejun Heo probe_kernel_read(name, wq->name, sizeof(name) - 1); 41253d1cb205STejun Heo 41263d1cb205STejun Heo /* copy worker description */ 41273d1cb205STejun Heo probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid)); 41283d1cb205STejun Heo if (desc_valid) 41293d1cb205STejun Heo probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 41303d1cb205STejun Heo 41313d1cb205STejun Heo if (fn || name[0] || desc[0]) { 41323d1cb205STejun Heo printk("%sWorkqueue: %s %pf", log_lvl, name, fn); 41333d1cb205STejun Heo if (desc[0]) 41343d1cb205STejun Heo pr_cont(" (%s)", desc); 41353d1cb205STejun Heo pr_cont("\n"); 41363d1cb205STejun Heo } 41373d1cb205STejun Heo } 41383d1cb205STejun Heo 41393494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 41403494fc30STejun Heo { 41413494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 41423494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 41433494fc30STejun Heo pr_cont(" node=%d", pool->node); 41443494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 41453494fc30STejun Heo } 41463494fc30STejun Heo 41473494fc30STejun Heo static void pr_cont_work(bool comma, struct work_struct *work) 41483494fc30STejun Heo { 41493494fc30STejun Heo if (work->func == wq_barrier_func) { 41503494fc30STejun Heo struct wq_barrier *barr; 41513494fc30STejun Heo 41523494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 41533494fc30STejun Heo 41543494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 41553494fc30STejun Heo task_pid_nr(barr->task)); 41563494fc30STejun Heo } else { 41573494fc30STejun Heo pr_cont("%s %pf", comma ? "," : "", work->func); 41583494fc30STejun Heo } 41593494fc30STejun Heo } 41603494fc30STejun Heo 41613494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 41623494fc30STejun Heo { 41633494fc30STejun Heo struct worker_pool *pool = pwq->pool; 41643494fc30STejun Heo struct work_struct *work; 41653494fc30STejun Heo struct worker *worker; 41663494fc30STejun Heo bool has_in_flight = false, has_pending = false; 41673494fc30STejun Heo int bkt; 41683494fc30STejun Heo 41693494fc30STejun Heo pr_info(" pwq %d:", pool->id); 41703494fc30STejun Heo pr_cont_pool_info(pool); 41713494fc30STejun Heo 41723494fc30STejun Heo pr_cont(" active=%d/%d%s\n", pwq->nr_active, pwq->max_active, 41733494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 41743494fc30STejun Heo 41753494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 41763494fc30STejun Heo if (worker->current_pwq == pwq) { 41773494fc30STejun Heo has_in_flight = true; 41783494fc30STejun Heo break; 41793494fc30STejun Heo } 41803494fc30STejun Heo } 41813494fc30STejun Heo if (has_in_flight) { 41823494fc30STejun Heo bool comma = false; 41833494fc30STejun Heo 41843494fc30STejun Heo pr_info(" in-flight:"); 41853494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 41863494fc30STejun Heo if (worker->current_pwq != pwq) 41873494fc30STejun Heo continue; 41883494fc30STejun Heo 41893494fc30STejun Heo pr_cont("%s %d%s:%pf", comma ? "," : "", 41903494fc30STejun Heo task_pid_nr(worker->task), 41913494fc30STejun Heo worker == pwq->wq->rescuer ? "(RESCUER)" : "", 41923494fc30STejun Heo worker->current_func); 41933494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 41943494fc30STejun Heo pr_cont_work(false, work); 41953494fc30STejun Heo comma = true; 41963494fc30STejun Heo } 41973494fc30STejun Heo pr_cont("\n"); 41983494fc30STejun Heo } 41993494fc30STejun Heo 42003494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 42013494fc30STejun Heo if (get_work_pwq(work) == pwq) { 42023494fc30STejun Heo has_pending = true; 42033494fc30STejun Heo break; 42043494fc30STejun Heo } 42053494fc30STejun Heo } 42063494fc30STejun Heo if (has_pending) { 42073494fc30STejun Heo bool comma = false; 42083494fc30STejun Heo 42093494fc30STejun Heo pr_info(" pending:"); 42103494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 42113494fc30STejun Heo if (get_work_pwq(work) != pwq) 42123494fc30STejun Heo continue; 42133494fc30STejun Heo 42143494fc30STejun Heo pr_cont_work(comma, work); 42153494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 42163494fc30STejun Heo } 42173494fc30STejun Heo pr_cont("\n"); 42183494fc30STejun Heo } 42193494fc30STejun Heo 42203494fc30STejun Heo if (!list_empty(&pwq->delayed_works)) { 42213494fc30STejun Heo bool comma = false; 42223494fc30STejun Heo 42233494fc30STejun Heo pr_info(" delayed:"); 42243494fc30STejun Heo list_for_each_entry(work, &pwq->delayed_works, entry) { 42253494fc30STejun Heo pr_cont_work(comma, work); 42263494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 42273494fc30STejun Heo } 42283494fc30STejun Heo pr_cont("\n"); 42293494fc30STejun Heo } 42303494fc30STejun Heo } 42313494fc30STejun Heo 42323494fc30STejun Heo /** 42333494fc30STejun Heo * show_workqueue_state - dump workqueue state 42343494fc30STejun Heo * 42353494fc30STejun Heo * Called from a sysrq handler and prints out all busy workqueues and 42363494fc30STejun Heo * pools. 42373494fc30STejun Heo */ 42383494fc30STejun Heo void show_workqueue_state(void) 42393494fc30STejun Heo { 42403494fc30STejun Heo struct workqueue_struct *wq; 42413494fc30STejun Heo struct worker_pool *pool; 42423494fc30STejun Heo unsigned long flags; 42433494fc30STejun Heo int pi; 42443494fc30STejun Heo 42453494fc30STejun Heo rcu_read_lock_sched(); 42463494fc30STejun Heo 42473494fc30STejun Heo pr_info("Showing busy workqueues and worker pools:\n"); 42483494fc30STejun Heo 42493494fc30STejun Heo list_for_each_entry_rcu(wq, &workqueues, list) { 42503494fc30STejun Heo struct pool_workqueue *pwq; 42513494fc30STejun Heo bool idle = true; 42523494fc30STejun Heo 42533494fc30STejun Heo for_each_pwq(pwq, wq) { 42543494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) { 42553494fc30STejun Heo idle = false; 42563494fc30STejun Heo break; 42573494fc30STejun Heo } 42583494fc30STejun Heo } 42593494fc30STejun Heo if (idle) 42603494fc30STejun Heo continue; 42613494fc30STejun Heo 42623494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 42633494fc30STejun Heo 42643494fc30STejun Heo for_each_pwq(pwq, wq) { 42653494fc30STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 42663494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) 42673494fc30STejun Heo show_pwq(pwq); 42683494fc30STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 42693494fc30STejun Heo } 42703494fc30STejun Heo } 42713494fc30STejun Heo 42723494fc30STejun Heo for_each_pool(pool, pi) { 42733494fc30STejun Heo struct worker *worker; 42743494fc30STejun Heo bool first = true; 42753494fc30STejun Heo 42763494fc30STejun Heo spin_lock_irqsave(&pool->lock, flags); 42773494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 42783494fc30STejun Heo goto next_pool; 42793494fc30STejun Heo 42803494fc30STejun Heo pr_info("pool %d:", pool->id); 42813494fc30STejun Heo pr_cont_pool_info(pool); 42823494fc30STejun Heo pr_cont(" workers=%d", pool->nr_workers); 42833494fc30STejun Heo if (pool->manager) 42843494fc30STejun Heo pr_cont(" manager: %d", 42853494fc30STejun Heo task_pid_nr(pool->manager->task)); 42863494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 42873494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 42883494fc30STejun Heo task_pid_nr(worker->task)); 42893494fc30STejun Heo first = false; 42903494fc30STejun Heo } 42913494fc30STejun Heo pr_cont("\n"); 42923494fc30STejun Heo next_pool: 42933494fc30STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 42943494fc30STejun Heo } 42953494fc30STejun Heo 42963494fc30STejun Heo rcu_read_unlock_sched(); 42973494fc30STejun Heo } 42983494fc30STejun Heo 4299db7bccf4STejun Heo /* 4300db7bccf4STejun Heo * CPU hotplug. 4301db7bccf4STejun Heo * 4302e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 4303112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 4304706026c2STejun Heo * pool which make migrating pending and scheduled works very 4305e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 430694cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 4307e22bee78STejun Heo * blocked draining impractical. 4308e22bee78STejun Heo * 430924647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 4310628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 4311628c78e7STejun Heo * cpu comes back online. 4312db7bccf4STejun Heo */ 4313db7bccf4STejun Heo 4314706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 4315db7bccf4STejun Heo { 431638db41d9STejun Heo int cpu = smp_processor_id(); 43174ce62e9eSTejun Heo struct worker_pool *pool; 4318db7bccf4STejun Heo struct worker *worker; 4319db7bccf4STejun Heo 4320f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 432192f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 432294cf58bbSTejun Heo spin_lock_irq(&pool->lock); 4323e22bee78STejun Heo 4324f2d5a0eeSTejun Heo /* 432592f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 432694cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 432794cf58bbSTejun Heo * except for the ones which are still executing works from 432894cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 432994cf58bbSTejun Heo * this, they may become diasporas. 4330f2d5a0eeSTejun Heo */ 4331da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4332403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 4333db7bccf4STejun Heo 433424647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 4335f2d5a0eeSTejun Heo 433694cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 433792f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 4338e22bee78STejun Heo 4339e22bee78STejun Heo /* 4340eb283428SLai Jiangshan * Call schedule() so that we cross rq->lock and thus can 4341eb283428SLai Jiangshan * guarantee sched callbacks see the %WORKER_UNBOUND flag. 4342eb283428SLai Jiangshan * This is necessary as scheduler callbacks may be invoked 4343eb283428SLai Jiangshan * from other cpus. 4344628c78e7STejun Heo */ 4345628c78e7STejun Heo schedule(); 4346628c78e7STejun Heo 4347628c78e7STejun Heo /* 4348eb283428SLai Jiangshan * Sched callbacks are disabled now. Zap nr_running. 4349eb283428SLai Jiangshan * After this, nr_running stays zero and need_more_worker() 4350eb283428SLai Jiangshan * and keep_working() are always true as long as the 4351eb283428SLai Jiangshan * worklist is not empty. This pool now behaves as an 4352eb283428SLai Jiangshan * unbound (in terms of concurrency management) pool which 4353eb283428SLai Jiangshan * are served by workers tied to the pool. 4354e22bee78STejun Heo */ 4355e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 4356eb283428SLai Jiangshan 4357eb283428SLai Jiangshan /* 4358eb283428SLai Jiangshan * With concurrency management just turned off, a busy 4359eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 4360eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 4361eb283428SLai Jiangshan */ 4362eb283428SLai Jiangshan spin_lock_irq(&pool->lock); 4363eb283428SLai Jiangshan wake_up_worker(pool); 4364eb283428SLai Jiangshan spin_unlock_irq(&pool->lock); 4365eb283428SLai Jiangshan } 4366db7bccf4STejun Heo } 4367db7bccf4STejun Heo 4368bd7c089eSTejun Heo /** 4369bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 4370bd7c089eSTejun Heo * @pool: pool of interest 4371bd7c089eSTejun Heo * 4372a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 4373bd7c089eSTejun Heo */ 4374bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 4375bd7c089eSTejun Heo { 4376a9ab775bSTejun Heo struct worker *worker; 4377bd7c089eSTejun Heo 437892f9c5c4SLai Jiangshan lockdep_assert_held(&pool->attach_mutex); 4379bd7c089eSTejun Heo 4380bd7c089eSTejun Heo /* 4381a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 4382a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 4383a9ab775bSTejun Heo * wake-ups for concurrency management happen, restore CPU affinty 4384a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 4385a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 4386bd7c089eSTejun Heo */ 4387da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4388a9ab775bSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 4389a9ab775bSTejun Heo pool->attrs->cpumask) < 0); 4390a9ab775bSTejun Heo 4391a9ab775bSTejun Heo spin_lock_irq(&pool->lock); 43923de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 4393a9ab775bSTejun Heo 4394da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 4395a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 4396a9ab775bSTejun Heo 4397a9ab775bSTejun Heo /* 4398a9ab775bSTejun Heo * A bound idle worker should actually be on the runqueue 4399a9ab775bSTejun Heo * of the associated CPU for local wake-ups targeting it to 4400a9ab775bSTejun Heo * work. Kick all idle workers so that they migrate to the 4401a9ab775bSTejun Heo * associated CPU. Doing this in the same loop as 4402a9ab775bSTejun Heo * replacing UNBOUND with REBOUND is safe as no worker will 4403a9ab775bSTejun Heo * be bound before @pool->lock is released. 4404a9ab775bSTejun Heo */ 4405a9ab775bSTejun Heo if (worker_flags & WORKER_IDLE) 4406bd7c089eSTejun Heo wake_up_process(worker->task); 4407bd7c089eSTejun Heo 4408bd7c089eSTejun Heo /* 4409a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 4410a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 4411a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 4412a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 4413a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 4414a9ab775bSTejun Heo * concurrency management. Note that when or whether 4415a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 4416a9ab775bSTejun Heo * 4417a9ab775bSTejun Heo * ACCESS_ONCE() is necessary because @worker->flags may be 4418a9ab775bSTejun Heo * tested without holding any lock in 4419a9ab775bSTejun Heo * wq_worker_waking_up(). Without it, NOT_RUNNING test may 4420a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 4421a9ab775bSTejun Heo * management operations. 4422bd7c089eSTejun Heo */ 4423a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 4424a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 4425a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 4426a9ab775bSTejun Heo ACCESS_ONCE(worker->flags) = worker_flags; 4427bd7c089eSTejun Heo } 4428a9ab775bSTejun Heo 4429a9ab775bSTejun Heo spin_unlock_irq(&pool->lock); 4430bd7c089eSTejun Heo } 4431bd7c089eSTejun Heo 44327dbc725eSTejun Heo /** 44337dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 44347dbc725eSTejun Heo * @pool: unbound pool of interest 44357dbc725eSTejun Heo * @cpu: the CPU which is coming up 44367dbc725eSTejun Heo * 44377dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 44387dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 44397dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 44407dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 44417dbc725eSTejun Heo */ 44427dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 44437dbc725eSTejun Heo { 44447dbc725eSTejun Heo static cpumask_t cpumask; 44457dbc725eSTejun Heo struct worker *worker; 44467dbc725eSTejun Heo 444792f9c5c4SLai Jiangshan lockdep_assert_held(&pool->attach_mutex); 44487dbc725eSTejun Heo 44497dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 44507dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 44517dbc725eSTejun Heo return; 44527dbc725eSTejun Heo 44537dbc725eSTejun Heo /* is @cpu the only online CPU? */ 44547dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 44557dbc725eSTejun Heo if (cpumask_weight(&cpumask) != 1) 44567dbc725eSTejun Heo return; 44577dbc725eSTejun Heo 44587dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 4459da028469SLai Jiangshan for_each_pool_worker(worker, pool) 44607dbc725eSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 44617dbc725eSTejun Heo pool->attrs->cpumask) < 0); 44627dbc725eSTejun Heo } 44637dbc725eSTejun Heo 44648db25e78STejun Heo /* 44658db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 44668db25e78STejun Heo * This will be registered high priority CPU notifier. 44678db25e78STejun Heo */ 44680db0628dSPaul Gortmaker static int workqueue_cpu_up_callback(struct notifier_block *nfb, 44691da177e4SLinus Torvalds unsigned long action, 44701da177e4SLinus Torvalds void *hcpu) 44711da177e4SLinus Torvalds { 4472d84ff051STejun Heo int cpu = (unsigned long)hcpu; 44734ce62e9eSTejun Heo struct worker_pool *pool; 44744c16bd32STejun Heo struct workqueue_struct *wq; 44757dbc725eSTejun Heo int pi; 44761da177e4SLinus Torvalds 44778db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 44783af24433SOleg Nesterov case CPU_UP_PREPARE: 4479f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 44803ce63377STejun Heo if (pool->nr_workers) 44813ce63377STejun Heo continue; 4482051e1850SLai Jiangshan if (!create_worker(pool)) 44833ce63377STejun Heo return NOTIFY_BAD; 44843af24433SOleg Nesterov } 44851da177e4SLinus Torvalds break; 44861da177e4SLinus Torvalds 448765758202STejun Heo case CPU_DOWN_FAILED: 448865758202STejun Heo case CPU_ONLINE: 448968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 44907dbc725eSTejun Heo 44917dbc725eSTejun Heo for_each_pool(pool, pi) { 449292f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 449394cf58bbSTejun Heo 4494f05b558dSLai Jiangshan if (pool->cpu == cpu) 449594cf58bbSTejun Heo rebind_workers(pool); 4496f05b558dSLai Jiangshan else if (pool->cpu < 0) 44977dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 449894cf58bbSTejun Heo 449992f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 450094cf58bbSTejun Heo } 45017dbc725eSTejun Heo 45024c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 45034c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 45044c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 45054c16bd32STejun Heo 450668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 45078db25e78STejun Heo break; 450865758202STejun Heo } 450965758202STejun Heo return NOTIFY_OK; 451065758202STejun Heo } 451165758202STejun Heo 451265758202STejun Heo /* 451365758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 451465758202STejun Heo * This will be registered as low priority CPU notifier. 451565758202STejun Heo */ 45160db0628dSPaul Gortmaker static int workqueue_cpu_down_callback(struct notifier_block *nfb, 451765758202STejun Heo unsigned long action, 451865758202STejun Heo void *hcpu) 451965758202STejun Heo { 4520d84ff051STejun Heo int cpu = (unsigned long)hcpu; 45218db25e78STejun Heo struct work_struct unbind_work; 45224c16bd32STejun Heo struct workqueue_struct *wq; 45238db25e78STejun Heo 452465758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 452565758202STejun Heo case CPU_DOWN_PREPARE: 45264c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 4527706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 45287635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 45294c16bd32STejun Heo 45304c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 45314c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 45324c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 45334c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 45344c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 45354c16bd32STejun Heo 45364c16bd32STejun Heo /* wait for per-cpu unbinding to finish */ 45378db25e78STejun Heo flush_work(&unbind_work); 4538440a1136SChuansheng Liu destroy_work_on_stack(&unbind_work); 45398db25e78STejun Heo break; 454065758202STejun Heo } 454165758202STejun Heo return NOTIFY_OK; 454265758202STejun Heo } 454365758202STejun Heo 45442d3854a3SRusty Russell #ifdef CONFIG_SMP 45458ccad40dSRusty Russell 45462d3854a3SRusty Russell struct work_for_cpu { 4547ed48ece2STejun Heo struct work_struct work; 45482d3854a3SRusty Russell long (*fn)(void *); 45492d3854a3SRusty Russell void *arg; 45502d3854a3SRusty Russell long ret; 45512d3854a3SRusty Russell }; 45522d3854a3SRusty Russell 4553ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 45542d3854a3SRusty Russell { 4555ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 4556ed48ece2STejun Heo 45572d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 45582d3854a3SRusty Russell } 45592d3854a3SRusty Russell 45602d3854a3SRusty Russell /** 45612d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 45622d3854a3SRusty Russell * @cpu: the cpu to run on 45632d3854a3SRusty Russell * @fn: the function to run 45642d3854a3SRusty Russell * @arg: the function arg 45652d3854a3SRusty Russell * 456631ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 45676b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 4568d185af30SYacine Belkadi * 4569d185af30SYacine Belkadi * Return: The value @fn returns. 45702d3854a3SRusty Russell */ 4571d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 45722d3854a3SRusty Russell { 4573ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 45742d3854a3SRusty Russell 4575ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 4576ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 457712997d1aSBjorn Helgaas flush_work(&wfc.work); 4578440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 45792d3854a3SRusty Russell return wfc.ret; 45802d3854a3SRusty Russell } 45812d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 45822d3854a3SRusty Russell #endif /* CONFIG_SMP */ 45832d3854a3SRusty Russell 4584a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 4585e7577c50SRusty Russell 4586a0a1a5fdSTejun Heo /** 4587a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 4588a0a1a5fdSTejun Heo * 458958a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 4590c5aa87bbSTejun Heo * workqueues will queue new works to their delayed_works list instead of 4591706026c2STejun Heo * pool->worklist. 4592a0a1a5fdSTejun Heo * 4593a0a1a5fdSTejun Heo * CONTEXT: 4594a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4595a0a1a5fdSTejun Heo */ 4596a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 4597a0a1a5fdSTejun Heo { 459824b8a847STejun Heo struct workqueue_struct *wq; 459924b8a847STejun Heo struct pool_workqueue *pwq; 4600a0a1a5fdSTejun Heo 460168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4602a0a1a5fdSTejun Heo 46036183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 4604a0a1a5fdSTejun Heo workqueue_freezing = true; 4605a0a1a5fdSTejun Heo 460624b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4607a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4608699ce097STejun Heo for_each_pwq(pwq, wq) 4609699ce097STejun Heo pwq_adjust_max_active(pwq); 4610a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4611a1056305STejun Heo } 46125bcab335STejun Heo 461368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4614a0a1a5fdSTejun Heo } 4615a0a1a5fdSTejun Heo 4616a0a1a5fdSTejun Heo /** 461758a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 4618a0a1a5fdSTejun Heo * 4619a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 4620a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 4621a0a1a5fdSTejun Heo * 4622a0a1a5fdSTejun Heo * CONTEXT: 462368e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 4624a0a1a5fdSTejun Heo * 4625d185af30SYacine Belkadi * Return: 462658a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 462758a69cb4STejun Heo * is complete. 4628a0a1a5fdSTejun Heo */ 4629a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 4630a0a1a5fdSTejun Heo { 4631a0a1a5fdSTejun Heo bool busy = false; 463224b8a847STejun Heo struct workqueue_struct *wq; 463324b8a847STejun Heo struct pool_workqueue *pwq; 4634a0a1a5fdSTejun Heo 463568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4636a0a1a5fdSTejun Heo 46376183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 4638a0a1a5fdSTejun Heo 463924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 464024b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 464124b8a847STejun Heo continue; 4642a0a1a5fdSTejun Heo /* 4643a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 4644a0a1a5fdSTejun Heo * to peek without lock. 4645a0a1a5fdSTejun Heo */ 464688109453SLai Jiangshan rcu_read_lock_sched(); 464724b8a847STejun Heo for_each_pwq(pwq, wq) { 46486183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 4649112202d9STejun Heo if (pwq->nr_active) { 4650a0a1a5fdSTejun Heo busy = true; 465188109453SLai Jiangshan rcu_read_unlock_sched(); 4652a0a1a5fdSTejun Heo goto out_unlock; 4653a0a1a5fdSTejun Heo } 4654a0a1a5fdSTejun Heo } 465588109453SLai Jiangshan rcu_read_unlock_sched(); 4656a0a1a5fdSTejun Heo } 4657a0a1a5fdSTejun Heo out_unlock: 465868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4659a0a1a5fdSTejun Heo return busy; 4660a0a1a5fdSTejun Heo } 4661a0a1a5fdSTejun Heo 4662a0a1a5fdSTejun Heo /** 4663a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 4664a0a1a5fdSTejun Heo * 4665a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 4666706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 4667a0a1a5fdSTejun Heo * 4668a0a1a5fdSTejun Heo * CONTEXT: 4669a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4670a0a1a5fdSTejun Heo */ 4671a0a1a5fdSTejun Heo void thaw_workqueues(void) 4672a0a1a5fdSTejun Heo { 467324b8a847STejun Heo struct workqueue_struct *wq; 467424b8a847STejun Heo struct pool_workqueue *pwq; 4675a0a1a5fdSTejun Heo 467668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4677a0a1a5fdSTejun Heo 4678a0a1a5fdSTejun Heo if (!workqueue_freezing) 4679a0a1a5fdSTejun Heo goto out_unlock; 4680a0a1a5fdSTejun Heo 468174b414eaSLai Jiangshan workqueue_freezing = false; 468224b8a847STejun Heo 468324b8a847STejun Heo /* restore max_active and repopulate worklist */ 468424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4685a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4686699ce097STejun Heo for_each_pwq(pwq, wq) 4687699ce097STejun Heo pwq_adjust_max_active(pwq); 4688a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 468924b8a847STejun Heo } 469024b8a847STejun Heo 4691a0a1a5fdSTejun Heo out_unlock: 469268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4693a0a1a5fdSTejun Heo } 4694a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 4695a0a1a5fdSTejun Heo 46966ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 46976ba94429SFrederic Weisbecker /* 46986ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 46996ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 47006ba94429SFrederic Weisbecker * following attributes. 47016ba94429SFrederic Weisbecker * 47026ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 47036ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 47046ba94429SFrederic Weisbecker * 47056ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 47066ba94429SFrederic Weisbecker * 47076ba94429SFrederic Weisbecker * id RO int : the associated pool ID 47086ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 47096ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 47106ba94429SFrederic Weisbecker */ 47116ba94429SFrederic Weisbecker struct wq_device { 47126ba94429SFrederic Weisbecker struct workqueue_struct *wq; 47136ba94429SFrederic Weisbecker struct device dev; 47146ba94429SFrederic Weisbecker }; 47156ba94429SFrederic Weisbecker 47166ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 47176ba94429SFrederic Weisbecker { 47186ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 47196ba94429SFrederic Weisbecker 47206ba94429SFrederic Weisbecker return wq_dev->wq; 47216ba94429SFrederic Weisbecker } 47226ba94429SFrederic Weisbecker 47236ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 47246ba94429SFrederic Weisbecker char *buf) 47256ba94429SFrederic Weisbecker { 47266ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 47276ba94429SFrederic Weisbecker 47286ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 47296ba94429SFrederic Weisbecker } 47306ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 47316ba94429SFrederic Weisbecker 47326ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 47336ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 47346ba94429SFrederic Weisbecker { 47356ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 47366ba94429SFrederic Weisbecker 47376ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 47386ba94429SFrederic Weisbecker } 47396ba94429SFrederic Weisbecker 47406ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 47416ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 47426ba94429SFrederic Weisbecker size_t count) 47436ba94429SFrederic Weisbecker { 47446ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 47456ba94429SFrederic Weisbecker int val; 47466ba94429SFrederic Weisbecker 47476ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 47486ba94429SFrederic Weisbecker return -EINVAL; 47496ba94429SFrederic Weisbecker 47506ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 47516ba94429SFrederic Weisbecker return count; 47526ba94429SFrederic Weisbecker } 47536ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 47546ba94429SFrederic Weisbecker 47556ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 47566ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 47576ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 47586ba94429SFrederic Weisbecker NULL, 47596ba94429SFrederic Weisbecker }; 47606ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 47616ba94429SFrederic Weisbecker 47626ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 47636ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 47646ba94429SFrederic Weisbecker { 47656ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 47666ba94429SFrederic Weisbecker const char *delim = ""; 47676ba94429SFrederic Weisbecker int node, written = 0; 47686ba94429SFrederic Weisbecker 47696ba94429SFrederic Weisbecker rcu_read_lock_sched(); 47706ba94429SFrederic Weisbecker for_each_node(node) { 47716ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 47726ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 47736ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 47746ba94429SFrederic Weisbecker delim = " "; 47756ba94429SFrederic Weisbecker } 47766ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 47776ba94429SFrederic Weisbecker rcu_read_unlock_sched(); 47786ba94429SFrederic Weisbecker 47796ba94429SFrederic Weisbecker return written; 47806ba94429SFrederic Weisbecker } 47816ba94429SFrederic Weisbecker 47826ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 47836ba94429SFrederic Weisbecker char *buf) 47846ba94429SFrederic Weisbecker { 47856ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 47866ba94429SFrederic Weisbecker int written; 47876ba94429SFrederic Weisbecker 47886ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 47896ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 47906ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 47916ba94429SFrederic Weisbecker 47926ba94429SFrederic Weisbecker return written; 47936ba94429SFrederic Weisbecker } 47946ba94429SFrederic Weisbecker 47956ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 47966ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 47976ba94429SFrederic Weisbecker { 47986ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 47996ba94429SFrederic Weisbecker 48006ba94429SFrederic Weisbecker attrs = alloc_workqueue_attrs(GFP_KERNEL); 48016ba94429SFrederic Weisbecker if (!attrs) 48026ba94429SFrederic Weisbecker return NULL; 48036ba94429SFrederic Weisbecker 48046ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 48056ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 48066ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 48076ba94429SFrederic Weisbecker return attrs; 48086ba94429SFrederic Weisbecker } 48096ba94429SFrederic Weisbecker 48106ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 48116ba94429SFrederic Weisbecker const char *buf, size_t count) 48126ba94429SFrederic Weisbecker { 48136ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 48146ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 48156ba94429SFrederic Weisbecker int ret; 48166ba94429SFrederic Weisbecker 48176ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 48186ba94429SFrederic Weisbecker if (!attrs) 48196ba94429SFrederic Weisbecker return -ENOMEM; 48206ba94429SFrederic Weisbecker 48216ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 48226ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 48236ba94429SFrederic Weisbecker ret = apply_workqueue_attrs(wq, attrs); 48246ba94429SFrederic Weisbecker else 48256ba94429SFrederic Weisbecker ret = -EINVAL; 48266ba94429SFrederic Weisbecker 48276ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 48286ba94429SFrederic Weisbecker return ret ?: count; 48296ba94429SFrederic Weisbecker } 48306ba94429SFrederic Weisbecker 48316ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 48326ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 48336ba94429SFrederic Weisbecker { 48346ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 48356ba94429SFrederic Weisbecker int written; 48366ba94429SFrederic Weisbecker 48376ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 48386ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 48396ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 48406ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 48416ba94429SFrederic Weisbecker return written; 48426ba94429SFrederic Weisbecker } 48436ba94429SFrederic Weisbecker 48446ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 48456ba94429SFrederic Weisbecker struct device_attribute *attr, 48466ba94429SFrederic Weisbecker const char *buf, size_t count) 48476ba94429SFrederic Weisbecker { 48486ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 48496ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 48506ba94429SFrederic Weisbecker int ret; 48516ba94429SFrederic Weisbecker 48526ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 48536ba94429SFrederic Weisbecker if (!attrs) 48546ba94429SFrederic Weisbecker return -ENOMEM; 48556ba94429SFrederic Weisbecker 48566ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 48576ba94429SFrederic Weisbecker if (!ret) 48586ba94429SFrederic Weisbecker ret = apply_workqueue_attrs(wq, attrs); 48596ba94429SFrederic Weisbecker 48606ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 48616ba94429SFrederic Weisbecker return ret ?: count; 48626ba94429SFrederic Weisbecker } 48636ba94429SFrederic Weisbecker 48646ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 48656ba94429SFrederic Weisbecker char *buf) 48666ba94429SFrederic Weisbecker { 48676ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 48686ba94429SFrederic Weisbecker int written; 48696ba94429SFrederic Weisbecker 48706ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 48716ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 48726ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 48736ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 48746ba94429SFrederic Weisbecker 48756ba94429SFrederic Weisbecker return written; 48766ba94429SFrederic Weisbecker } 48776ba94429SFrederic Weisbecker 48786ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 48796ba94429SFrederic Weisbecker const char *buf, size_t count) 48806ba94429SFrederic Weisbecker { 48816ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 48826ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 48836ba94429SFrederic Weisbecker int v, ret; 48846ba94429SFrederic Weisbecker 48856ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 48866ba94429SFrederic Weisbecker if (!attrs) 48876ba94429SFrederic Weisbecker return -ENOMEM; 48886ba94429SFrederic Weisbecker 48896ba94429SFrederic Weisbecker ret = -EINVAL; 48906ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 48916ba94429SFrederic Weisbecker attrs->no_numa = !v; 48926ba94429SFrederic Weisbecker ret = apply_workqueue_attrs(wq, attrs); 48936ba94429SFrederic Weisbecker } 48946ba94429SFrederic Weisbecker 48956ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 48966ba94429SFrederic Weisbecker return ret ?: count; 48976ba94429SFrederic Weisbecker } 48986ba94429SFrederic Weisbecker 48996ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 49006ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 49016ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 49026ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 49036ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 49046ba94429SFrederic Weisbecker __ATTR_NULL, 49056ba94429SFrederic Weisbecker }; 49066ba94429SFrederic Weisbecker 49076ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 49086ba94429SFrederic Weisbecker .name = "workqueue", 49096ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 49106ba94429SFrederic Weisbecker }; 49116ba94429SFrederic Weisbecker 49126ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 49136ba94429SFrederic Weisbecker { 49146ba94429SFrederic Weisbecker return subsys_virtual_register(&wq_subsys, NULL); 49156ba94429SFrederic Weisbecker } 49166ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 49176ba94429SFrederic Weisbecker 49186ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 49196ba94429SFrederic Weisbecker { 49206ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 49216ba94429SFrederic Weisbecker 49226ba94429SFrederic Weisbecker kfree(wq_dev); 49236ba94429SFrederic Weisbecker } 49246ba94429SFrederic Weisbecker 49256ba94429SFrederic Weisbecker /** 49266ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 49276ba94429SFrederic Weisbecker * @wq: the workqueue to register 49286ba94429SFrederic Weisbecker * 49296ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 49306ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 49316ba94429SFrederic Weisbecker * which is the preferred method. 49326ba94429SFrederic Weisbecker * 49336ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 49346ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 49356ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 49366ba94429SFrederic Weisbecker * attributes. 49376ba94429SFrederic Weisbecker * 49386ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 49396ba94429SFrederic Weisbecker */ 49406ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 49416ba94429SFrederic Weisbecker { 49426ba94429SFrederic Weisbecker struct wq_device *wq_dev; 49436ba94429SFrederic Weisbecker int ret; 49446ba94429SFrederic Weisbecker 49456ba94429SFrederic Weisbecker /* 49466ba94429SFrederic Weisbecker * Adjusting max_active or creating new pwqs by applyting 49476ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 49486ba94429SFrederic Weisbecker * workqueues. 49496ba94429SFrederic Weisbecker */ 49506ba94429SFrederic Weisbecker if (WARN_ON(wq->flags & __WQ_ORDERED)) 49516ba94429SFrederic Weisbecker return -EINVAL; 49526ba94429SFrederic Weisbecker 49536ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 49546ba94429SFrederic Weisbecker if (!wq_dev) 49556ba94429SFrederic Weisbecker return -ENOMEM; 49566ba94429SFrederic Weisbecker 49576ba94429SFrederic Weisbecker wq_dev->wq = wq; 49586ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 49596ba94429SFrederic Weisbecker wq_dev->dev.init_name = wq->name; 49606ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 49616ba94429SFrederic Weisbecker 49626ba94429SFrederic Weisbecker /* 49636ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 49646ba94429SFrederic Weisbecker * everything is ready. 49656ba94429SFrederic Weisbecker */ 49666ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 49676ba94429SFrederic Weisbecker 49686ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 49696ba94429SFrederic Weisbecker if (ret) { 49706ba94429SFrederic Weisbecker kfree(wq_dev); 49716ba94429SFrederic Weisbecker wq->wq_dev = NULL; 49726ba94429SFrederic Weisbecker return ret; 49736ba94429SFrederic Weisbecker } 49746ba94429SFrederic Weisbecker 49756ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 49766ba94429SFrederic Weisbecker struct device_attribute *attr; 49776ba94429SFrederic Weisbecker 49786ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 49796ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 49806ba94429SFrederic Weisbecker if (ret) { 49816ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 49826ba94429SFrederic Weisbecker wq->wq_dev = NULL; 49836ba94429SFrederic Weisbecker return ret; 49846ba94429SFrederic Weisbecker } 49856ba94429SFrederic Weisbecker } 49866ba94429SFrederic Weisbecker } 49876ba94429SFrederic Weisbecker 49886ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 49896ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 49906ba94429SFrederic Weisbecker return 0; 49916ba94429SFrederic Weisbecker } 49926ba94429SFrederic Weisbecker 49936ba94429SFrederic Weisbecker /** 49946ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 49956ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 49966ba94429SFrederic Weisbecker * 49976ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 49986ba94429SFrederic Weisbecker */ 49996ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 50006ba94429SFrederic Weisbecker { 50016ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 50026ba94429SFrederic Weisbecker 50036ba94429SFrederic Weisbecker if (!wq->wq_dev) 50046ba94429SFrederic Weisbecker return; 50056ba94429SFrederic Weisbecker 50066ba94429SFrederic Weisbecker wq->wq_dev = NULL; 50076ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 50086ba94429SFrederic Weisbecker } 50096ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 50106ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 50116ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 50126ba94429SFrederic Weisbecker 5013bce90380STejun Heo static void __init wq_numa_init(void) 5014bce90380STejun Heo { 5015bce90380STejun Heo cpumask_var_t *tbl; 5016bce90380STejun Heo int node, cpu; 5017bce90380STejun Heo 5018bce90380STejun Heo if (num_possible_nodes() <= 1) 5019bce90380STejun Heo return; 5020bce90380STejun Heo 5021d55262c4STejun Heo if (wq_disable_numa) { 5022d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 5023d55262c4STejun Heo return; 5024d55262c4STejun Heo } 5025d55262c4STejun Heo 50264c16bd32STejun Heo wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL); 50274c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 50284c16bd32STejun Heo 5029bce90380STejun Heo /* 5030bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 5031bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 5032bce90380STejun Heo * fully initialized by now. 5033bce90380STejun Heo */ 5034ddcb57e2SLai Jiangshan tbl = kzalloc(nr_node_ids * sizeof(tbl[0]), GFP_KERNEL); 5035bce90380STejun Heo BUG_ON(!tbl); 5036bce90380STejun Heo 5037bce90380STejun Heo for_each_node(node) 50385a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 50391be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 5040bce90380STejun Heo 5041bce90380STejun Heo for_each_possible_cpu(cpu) { 5042bce90380STejun Heo node = cpu_to_node(cpu); 5043bce90380STejun Heo if (WARN_ON(node == NUMA_NO_NODE)) { 5044bce90380STejun Heo pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 5045bce90380STejun Heo /* happens iff arch is bonkers, let's just proceed */ 5046bce90380STejun Heo return; 5047bce90380STejun Heo } 5048bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 5049bce90380STejun Heo } 5050bce90380STejun Heo 5051bce90380STejun Heo wq_numa_possible_cpumask = tbl; 5052bce90380STejun Heo wq_numa_enabled = true; 5053bce90380STejun Heo } 5054bce90380STejun Heo 50556ee0578bSSuresh Siddha static int __init init_workqueues(void) 50561da177e4SLinus Torvalds { 50577a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 50587a4e344cSTejun Heo int i, cpu; 5059c34056a3STejun Heo 5060e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 5061e904e6c2STejun Heo 5062e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 5063e904e6c2STejun Heo 506465758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 5065a5b4e57dSLai Jiangshan hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 50668b03ae3cSTejun Heo 5067bce90380STejun Heo wq_numa_init(); 5068bce90380STejun Heo 5069706026c2STejun Heo /* initialize CPU pools */ 507029c91e99STejun Heo for_each_possible_cpu(cpu) { 50714ce62e9eSTejun Heo struct worker_pool *pool; 50728b03ae3cSTejun Heo 50737a4e344cSTejun Heo i = 0; 5074f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 50757a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 5076ec22ca5eSTejun Heo pool->cpu = cpu; 50777a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 50787a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 5079f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 50807a4e344cSTejun Heo 50819daf9e67STejun Heo /* alloc pool ID */ 508268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 50839daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 508468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 50854ce62e9eSTejun Heo } 50868b03ae3cSTejun Heo } 50878b03ae3cSTejun Heo 5088e22bee78STejun Heo /* create the initial worker */ 508929c91e99STejun Heo for_each_online_cpu(cpu) { 50904ce62e9eSTejun Heo struct worker_pool *pool; 5091e22bee78STejun Heo 5092f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 509324647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 5094051e1850SLai Jiangshan BUG_ON(!create_worker(pool)); 5095e22bee78STejun Heo } 50964ce62e9eSTejun Heo } 5097e22bee78STejun Heo 50988a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 509929c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 510029c91e99STejun Heo struct workqueue_attrs *attrs; 510129c91e99STejun Heo 510229c91e99STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 510329c91e99STejun Heo attrs->nice = std_nice[i]; 510429c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 51058a2b7538STejun Heo 51068a2b7538STejun Heo /* 51078a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 51088a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 51098a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 51108a2b7538STejun Heo */ 51118a2b7538STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 51128a2b7538STejun Heo attrs->nice = std_nice[i]; 51138a2b7538STejun Heo attrs->no_numa = true; 51148a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 511529c91e99STejun Heo } 511629c91e99STejun Heo 5117d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 51181aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 5119d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 5120f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 5121f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 512224d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 512324d51addSTejun Heo WQ_FREEZABLE, 0); 51240668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 51250668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 51260668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 51270668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 51280668106cSViresh Kumar 0); 51291aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 51300668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 51310668106cSViresh Kumar !system_power_efficient_wq || 51320668106cSViresh Kumar !system_freezable_power_efficient_wq); 51336ee0578bSSuresh Siddha return 0; 51341da177e4SLinus Torvalds } 51356ee0578bSSuresh Siddha early_initcall(init_workqueues); 5136