11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <dwmw2@infradead.org> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 101da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <tj@kernel.org> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 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 * 249a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst 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> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 52e22bee78STejun Heo 53ea138446STejun Heo #include "workqueue_internal.h" 541da177e4SLinus Torvalds 55c8e55f36STejun Heo enum { 56bc2ae0f5STejun Heo /* 5724647570STejun Heo * worker_pool flags 58bc2ae0f5STejun Heo * 5924647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 60bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 61bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 62bc2ae0f5STejun Heo * is in effect. 63bc2ae0f5STejun Heo * 64bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 65bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6624647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 67bc2ae0f5STejun Heo * 68bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 6992f9c5c4SLai Jiangshan * attach_mutex to avoid changing binding state while 704736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 71bc2ae0f5STejun Heo */ 72692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7324647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 74db7bccf4STejun Heo 75c8e55f36STejun Heo /* worker flags */ 76c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 77c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 78e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 79fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 80f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 81a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 82e22bee78STejun Heo 83a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 84a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 85db7bccf4STejun Heo 86e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 874ce62e9eSTejun Heo 8829c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 89c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 90db7bccf4STejun Heo 91e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 92e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 93e22bee78STejun Heo 943233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 953233cdbdSTejun Heo /* call for help after 10ms 963233cdbdSTejun Heo (min two ticks) */ 97e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 98e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* 101e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1028698a745SDongsheng Yang * all cpus. Give MIN_NICE. 103e22bee78STejun Heo */ 1048698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1058698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 106ecf6881fSTejun Heo 107ecf6881fSTejun Heo WQ_NAME_LEN = 24, 108c8e55f36STejun Heo }; 109c8e55f36STejun Heo 1101da177e4SLinus Torvalds /* 1114690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1124690c4abSTejun Heo * 113e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 114e41e704bSTejun Heo * everyone else. 1154690c4abSTejun Heo * 116e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 117e22bee78STejun Heo * only be modified and accessed from the local cpu. 118e22bee78STejun Heo * 119d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1204690c4abSTejun Heo * 121d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 122d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 123d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 124d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 125e22bee78STejun Heo * 12692f9c5c4SLai Jiangshan * A: pool->attach_mutex protected. 127822d8405STejun Heo * 12868e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 12976af4d93STejun Heo * 13068e13a67SLai Jiangshan * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads. 1315bcab335STejun Heo * 1325b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1335b95e1afSLai Jiangshan * 1345b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 1355b95e1afSLai Jiangshan * sched-RCU for reads. 1365b95e1afSLai Jiangshan * 1373c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1383c25a55dSLai Jiangshan * 139b5927605SLai Jiangshan * WR: wq->mutex protected for writes. Sched-RCU protected for reads. 1402e109a28STejun Heo * 1412e109a28STejun Heo * MD: wq_mayday_lock protected. 1424690c4abSTejun Heo */ 1434690c4abSTejun Heo 1442eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 145c34056a3STejun Heo 146bd7bdd43STejun Heo struct worker_pool { 147d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 148d84ff051STejun Heo int cpu; /* I: the associated cpu */ 149f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1509daf9e67STejun Heo int id; /* I: pool ID */ 15111ebea50STejun Heo unsigned int flags; /* X: flags */ 152bd7bdd43STejun Heo 15382607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 15482607adcSTejun Heo 155bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 156bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 157ea1abd61SLai Jiangshan 158ea1abd61SLai Jiangshan /* nr_idle includes the ones off idle_list for rebinding */ 159bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 160bd7bdd43STejun Heo 161bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 162bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 163bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 164bd7bdd43STejun Heo 165c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 166c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 167c9e7cf27STejun Heo /* L: hash of busy workers */ 168c9e7cf27STejun Heo 169bc3a1afcSTejun Heo /* see manage_workers() for details on the two manager mutexes */ 1702607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 17192f9c5c4SLai Jiangshan struct mutex attach_mutex; /* attach/detach exclusion */ 17292f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 17360f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 174e19e397aSTejun Heo 1757cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 176e19e397aSTejun Heo 1777a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 17868e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 17968e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1807a4e344cSTejun Heo 181e19e397aSTejun Heo /* 182e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 183e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 184e19e397aSTejun Heo * cacheline. 185e19e397aSTejun Heo */ 186e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 18729c91e99STejun Heo 18829c91e99STejun Heo /* 18929c91e99STejun Heo * Destruction of pool is sched-RCU protected to allow dereferences 19029c91e99STejun Heo * from get_work_pool(). 19129c91e99STejun Heo */ 19229c91e99STejun Heo struct rcu_head rcu; 1938b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1948b03ae3cSTejun Heo 1958b03ae3cSTejun Heo /* 196112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 197112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 198112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 199112202d9STejun Heo * number of flag bits. 2001da177e4SLinus Torvalds */ 201112202d9STejun Heo struct pool_workqueue { 202bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2034690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 20473f53c4aSTejun Heo int work_color; /* L: current color */ 20573f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2068864b4e5STejun Heo int refcnt; /* L: reference count */ 20773f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20873f53c4aSTejun Heo /* L: nr of in_flight works */ 2091e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 210a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2111e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2123c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2132e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2148864b4e5STejun Heo 2158864b4e5STejun Heo /* 2168864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2178864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 2188864b4e5STejun Heo * itself is also sched-RCU protected so that the first pwq can be 219b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2208864b4e5STejun Heo */ 2218864b4e5STejun Heo struct work_struct unbound_release_work; 2228864b4e5STejun Heo struct rcu_head rcu; 223e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds /* 22673f53c4aSTejun Heo * Structure used to wait for workqueue flush. 22773f53c4aSTejun Heo */ 22873f53c4aSTejun Heo struct wq_flusher { 2293c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2303c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 23173f53c4aSTejun Heo struct completion done; /* flush completion */ 23273f53c4aSTejun Heo }; 2331da177e4SLinus Torvalds 234226223abSTejun Heo struct wq_device; 235226223abSTejun Heo 23673f53c4aSTejun Heo /* 237c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 238c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2391da177e4SLinus Torvalds */ 2401da177e4SLinus Torvalds struct workqueue_struct { 2413c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 242e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 24373f53c4aSTejun Heo 2443c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2453c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2463c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 247112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2483c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2493c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2503c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 25173f53c4aSTejun Heo 2522e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 253e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 254e22bee78STejun Heo 25587fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 256a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 257226223abSTejun Heo 2585b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 2595b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 2606029a918STejun Heo 261226223abSTejun Heo #ifdef CONFIG_SYSFS 262226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 263226223abSTejun Heo #endif 2644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2654e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2664e6045f1SJohannes Berg #endif 267ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2682728fd2fSTejun Heo 269e2dca7adSTejun Heo /* 270e2dca7adSTejun Heo * Destruction of workqueue_struct is sched-RCU protected to allow 271e2dca7adSTejun Heo * walking the workqueues list without grabbing wq_pool_mutex. 272e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 273e2dca7adSTejun Heo */ 274e2dca7adSTejun Heo struct rcu_head rcu; 275e2dca7adSTejun Heo 2762728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 2772728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 2782728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 2795b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 2801da177e4SLinus Torvalds }; 2811da177e4SLinus Torvalds 282e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 283e904e6c2STejun Heo 284bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 285bce90380STejun Heo /* possible CPUs of each node */ 286bce90380STejun Heo 287d55262c4STejun Heo static bool wq_disable_numa; 288d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 289d55262c4STejun Heo 290cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 291552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 292cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 293cee22a15SViresh Kumar 294863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 2953347fa09STejun Heo 296bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 297bce90380STejun Heo 2984c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 2994c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 3004c16bd32STejun Heo 30168e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3022e109a28STejun Heo static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 303692b4825STejun Heo static DECLARE_WAIT_QUEUE_HEAD(wq_manager_wait); /* wait for manager to go away */ 3045bcab335STejun Heo 305e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 30668e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3077d19c5ceSTejun Heo 308ef557180SMike Galbraith /* PL: allowable cpus for unbound wqs and work items */ 309ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 310ef557180SMike Galbraith 311ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 312ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 313b05a7928SFrederic Weisbecker 314f303fccbSTejun Heo /* 315f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 316f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 317f303fccbSTejun Heo * to uncover usages which depend on it. 318f303fccbSTejun Heo */ 319f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 320f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 321f303fccbSTejun Heo #else 322f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 323f303fccbSTejun Heo #endif 324f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 325f303fccbSTejun Heo 3267d19c5ceSTejun Heo /* the per-cpu worker pools */ 32725528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3287d19c5ceSTejun Heo 32968e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3307d19c5ceSTejun Heo 33168e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 33229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 33329c91e99STejun Heo 334c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 33529c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 33629c91e99STejun Heo 3378a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3388a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3398a2b7538STejun Heo 340d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 341ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 342044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3431aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 344044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 345d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 346044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 347f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 348044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 34924d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3500668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3510668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3520668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3530668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 354d320c038STejun Heo 3557d19c5ceSTejun Heo static int worker_thread(void *__worker); 3566ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 3577d19c5ceSTejun Heo 35897bd2347STejun Heo #define CREATE_TRACE_POINTS 35997bd2347STejun Heo #include <trace/events/workqueue.h> 36097bd2347STejun Heo 36168e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 362f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 363f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 36468e13a67SLai Jiangshan "sched RCU or wq_pool_mutex should be held") 3655bcab335STejun Heo 366b09f4fd3SLai Jiangshan #define assert_rcu_or_wq_mutex(wq) \ 367f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 368f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex), \ 369b09f4fd3SLai Jiangshan "sched RCU or wq->mutex should be held") 37076af4d93STejun Heo 3715b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 372f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 373f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 374f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 3755b95e1afSLai Jiangshan "sched RCU, wq->mutex or wq_pool_mutex should be held") 3765b95e1afSLai Jiangshan 377f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 378f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 379f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 3807a62c2c8STejun Heo (pool)++) 3814ce62e9eSTejun Heo 38249e3cf44STejun Heo /** 38317116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 38417116969STejun Heo * @pool: iteration cursor 385611c92a0STejun Heo * @pi: integer used for iteration 386fa1b54e6STejun Heo * 38768e13a67SLai Jiangshan * This must be called either with wq_pool_mutex held or sched RCU read 38868e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 38968e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 390fa1b54e6STejun Heo * 391fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 392fa1b54e6STejun Heo * ignored. 39317116969STejun Heo */ 394611c92a0STejun Heo #define for_each_pool(pool, pi) \ 395611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 39668e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 397fa1b54e6STejun Heo else 39817116969STejun Heo 39917116969STejun Heo /** 400822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 401822d8405STejun Heo * @worker: iteration cursor 402822d8405STejun Heo * @pool: worker_pool to iterate workers of 403822d8405STejun Heo * 40492f9c5c4SLai Jiangshan * This must be called with @pool->attach_mutex. 405822d8405STejun Heo * 406822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 407822d8405STejun Heo * ignored. 408822d8405STejun Heo */ 409da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 410da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 41192f9c5c4SLai Jiangshan if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \ 412822d8405STejun Heo else 413822d8405STejun Heo 414822d8405STejun Heo /** 41549e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 41649e3cf44STejun Heo * @pwq: iteration cursor 41749e3cf44STejun Heo * @wq: the target workqueue 41876af4d93STejun Heo * 419b09f4fd3SLai Jiangshan * This must be called either with wq->mutex held or sched RCU read locked. 420794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 421794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 42276af4d93STejun Heo * 42376af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 42476af4d93STejun Heo * ignored. 42549e3cf44STejun Heo */ 42649e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 42776af4d93STejun Heo list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ 428b09f4fd3SLai Jiangshan if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \ 42976af4d93STejun Heo else 430f3421797STejun Heo 431dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 432dc186ad7SThomas Gleixner 433dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 434dc186ad7SThomas Gleixner 43599777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 43699777288SStanislaw Gruszka { 43799777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 43899777288SStanislaw Gruszka } 43999777288SStanislaw Gruszka 440b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 441b9fdac7fSDu, Changbin { 442b9fdac7fSDu, Changbin struct work_struct *work = addr; 443b9fdac7fSDu, Changbin 444b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 445b9fdac7fSDu, Changbin } 446b9fdac7fSDu, Changbin 447dc186ad7SThomas Gleixner /* 448dc186ad7SThomas Gleixner * fixup_init is called when: 449dc186ad7SThomas Gleixner * - an active object is initialized 450dc186ad7SThomas Gleixner */ 45102a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 452dc186ad7SThomas Gleixner { 453dc186ad7SThomas Gleixner struct work_struct *work = addr; 454dc186ad7SThomas Gleixner 455dc186ad7SThomas Gleixner switch (state) { 456dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 457dc186ad7SThomas Gleixner cancel_work_sync(work); 458dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 45902a982a6SDu, Changbin return true; 460dc186ad7SThomas Gleixner default: 46102a982a6SDu, Changbin return false; 462dc186ad7SThomas Gleixner } 463dc186ad7SThomas Gleixner } 464dc186ad7SThomas Gleixner 465dc186ad7SThomas Gleixner /* 466dc186ad7SThomas Gleixner * fixup_free is called when: 467dc186ad7SThomas Gleixner * - an active object is freed 468dc186ad7SThomas Gleixner */ 46902a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 470dc186ad7SThomas Gleixner { 471dc186ad7SThomas Gleixner struct work_struct *work = addr; 472dc186ad7SThomas Gleixner 473dc186ad7SThomas Gleixner switch (state) { 474dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 475dc186ad7SThomas Gleixner cancel_work_sync(work); 476dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 47702a982a6SDu, Changbin return true; 478dc186ad7SThomas Gleixner default: 47902a982a6SDu, Changbin return false; 480dc186ad7SThomas Gleixner } 481dc186ad7SThomas Gleixner } 482dc186ad7SThomas Gleixner 483dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 484dc186ad7SThomas Gleixner .name = "work_struct", 48599777288SStanislaw Gruszka .debug_hint = work_debug_hint, 486b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 487dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 488dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 489dc186ad7SThomas Gleixner }; 490dc186ad7SThomas Gleixner 491dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 492dc186ad7SThomas Gleixner { 493dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 494dc186ad7SThomas Gleixner } 495dc186ad7SThomas Gleixner 496dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 497dc186ad7SThomas Gleixner { 498dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 499dc186ad7SThomas Gleixner } 500dc186ad7SThomas Gleixner 501dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 502dc186ad7SThomas Gleixner { 503dc186ad7SThomas Gleixner if (onstack) 504dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 505dc186ad7SThomas Gleixner else 506dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 507dc186ad7SThomas Gleixner } 508dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 509dc186ad7SThomas Gleixner 510dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 511dc186ad7SThomas Gleixner { 512dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 513dc186ad7SThomas Gleixner } 514dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 515dc186ad7SThomas Gleixner 516ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 517ea2e64f2SThomas Gleixner { 518ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 519ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 520ea2e64f2SThomas Gleixner } 521ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 522ea2e64f2SThomas Gleixner 523dc186ad7SThomas Gleixner #else 524dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 525dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 526dc186ad7SThomas Gleixner #endif 527dc186ad7SThomas Gleixner 5284e8b22bdSLi Bin /** 5294e8b22bdSLi Bin * worker_pool_assign_id - allocate ID and assing it to @pool 5304e8b22bdSLi Bin * @pool: the pool pointer of interest 5314e8b22bdSLi Bin * 5324e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5334e8b22bdSLi Bin * successfully, -errno on failure. 5344e8b22bdSLi Bin */ 5359daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5369daf9e67STejun Heo { 5379daf9e67STejun Heo int ret; 5389daf9e67STejun Heo 53968e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5405bcab335STejun Heo 5414e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5424e8b22bdSLi Bin GFP_KERNEL); 543229641a6STejun Heo if (ret >= 0) { 544e68035fbSTejun Heo pool->id = ret; 545229641a6STejun Heo return 0; 546229641a6STejun Heo } 5479daf9e67STejun Heo return ret; 5489daf9e67STejun Heo } 5499daf9e67STejun Heo 55076af4d93STejun Heo /** 551df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 552df2d5ae4STejun Heo * @wq: the target workqueue 553df2d5ae4STejun Heo * @node: the node ID 554df2d5ae4STejun Heo * 5555b95e1afSLai Jiangshan * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU 5565b95e1afSLai Jiangshan * read locked. 557df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 558df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 559d185af30SYacine Belkadi * 560d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 561df2d5ae4STejun Heo */ 562df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 563df2d5ae4STejun Heo int node) 564df2d5ae4STejun Heo { 5655b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 566d6e022f1STejun Heo 567d6e022f1STejun Heo /* 568d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 569d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 570d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 571d6e022f1STejun Heo * happens, this workaround can be removed. 572d6e022f1STejun Heo */ 573d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 574d6e022f1STejun Heo return wq->dfl_pwq; 575d6e022f1STejun Heo 576df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 577df2d5ae4STejun Heo } 578df2d5ae4STejun Heo 57973f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 58073f53c4aSTejun Heo { 58173f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 58273f53c4aSTejun Heo } 58373f53c4aSTejun Heo 58473f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 58573f53c4aSTejun Heo { 58673f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 58773f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 58873f53c4aSTejun Heo } 58973f53c4aSTejun Heo 59073f53c4aSTejun Heo static int work_next_color(int color) 59173f53c4aSTejun Heo { 59273f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 593a848e3b6SOleg Nesterov } 594a848e3b6SOleg Nesterov 5954594bf15SDavid Howells /* 596112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 597112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 5987c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 5997a22ad75STejun Heo * 600112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 601112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 602bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 603bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6047a22ad75STejun Heo * 605112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6067c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 607112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6087c3eed5cSTejun Heo * available only while the work item is queued. 609bbb68dfaSTejun Heo * 610bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 611bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 612bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 613bbb68dfaSTejun Heo * try to steal the PENDING bit. 6144594bf15SDavid Howells */ 6157a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6167a22ad75STejun Heo unsigned long flags) 6177a22ad75STejun Heo { 6186183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6197a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6207a22ad75STejun Heo } 6217a22ad75STejun Heo 622112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6234690c4abSTejun Heo unsigned long extra_flags) 624365970a1SDavid Howells { 625112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 626112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 627365970a1SDavid Howells } 628365970a1SDavid Howells 6294468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6304468a00fSLai Jiangshan int pool_id) 6314468a00fSLai Jiangshan { 6324468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6334468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6344468a00fSLai Jiangshan } 6354468a00fSLai Jiangshan 6367c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6377c3eed5cSTejun Heo int pool_id) 6384d707b9fSOleg Nesterov { 63923657bb1STejun Heo /* 64023657bb1STejun Heo * The following wmb is paired with the implied mb in 64123657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 64223657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 64323657bb1STejun Heo * owner. 64423657bb1STejun Heo */ 64523657bb1STejun Heo smp_wmb(); 6467c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 647346c09f8SRoman Pen /* 648346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 649346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 650346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 651346c09f8SRoman Pen * reordering can lead to a missed execution on attempt to qeueue 652346c09f8SRoman Pen * the same @work. E.g. consider this case: 653346c09f8SRoman Pen * 654346c09f8SRoman Pen * CPU#0 CPU#1 655346c09f8SRoman Pen * ---------------------------- -------------------------------- 656346c09f8SRoman Pen * 657346c09f8SRoman Pen * 1 STORE event_indicated 658346c09f8SRoman Pen * 2 queue_work_on() { 659346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 660346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 661346c09f8SRoman Pen * 5 set_work_data() # clear bit 662346c09f8SRoman Pen * 6 smp_mb() 663346c09f8SRoman Pen * 7 work->current_func() { 664346c09f8SRoman Pen * 8 LOAD event_indicated 665346c09f8SRoman Pen * } 666346c09f8SRoman Pen * 667346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 668346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 669346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 670346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 671346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 672346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 673346c09f8SRoman Pen * before actual STORE. 674346c09f8SRoman Pen */ 675346c09f8SRoman Pen smp_mb(); 6764d707b9fSOleg Nesterov } 6774d707b9fSOleg Nesterov 6787a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 679365970a1SDavid Howells { 6807c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 6817c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 6827a22ad75STejun Heo } 6837a22ad75STejun Heo 684112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 6857a22ad75STejun Heo { 686e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6877a22ad75STejun Heo 688112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 689e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 690e120153dSTejun Heo else 691e120153dSTejun Heo return NULL; 6927a22ad75STejun Heo } 6937a22ad75STejun Heo 6947c3eed5cSTejun Heo /** 6957c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 6967c3eed5cSTejun Heo * @work: the work item of interest 6977c3eed5cSTejun Heo * 69868e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 69968e13a67SLai Jiangshan * access under sched-RCU read lock. As such, this function should be 70068e13a67SLai Jiangshan * called under wq_pool_mutex or with preemption disabled. 701fa1b54e6STejun Heo * 702fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 703fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 704fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 705fa1b54e6STejun Heo * returned pool is and stays online. 706d185af30SYacine Belkadi * 707d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7087c3eed5cSTejun Heo */ 7097c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7107a22ad75STejun Heo { 711e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7127c3eed5cSTejun Heo int pool_id; 7137a22ad75STejun Heo 71468e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 715fa1b54e6STejun Heo 716112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 717112202d9STejun Heo return ((struct pool_workqueue *) 7187c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7197a22ad75STejun Heo 7207c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7217c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7227a22ad75STejun Heo return NULL; 7237a22ad75STejun Heo 724fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7257c3eed5cSTejun Heo } 7267c3eed5cSTejun Heo 7277c3eed5cSTejun Heo /** 7287c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7297c3eed5cSTejun Heo * @work: the work item of interest 7307c3eed5cSTejun Heo * 731d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7327c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7337c3eed5cSTejun Heo */ 7347c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7357c3eed5cSTejun Heo { 73654d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7377c3eed5cSTejun Heo 738112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 739112202d9STejun Heo return ((struct pool_workqueue *) 74054d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 74154d5b7d0SLai Jiangshan 74254d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7437c3eed5cSTejun Heo } 7447c3eed5cSTejun Heo 745bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 746bbb68dfaSTejun Heo { 7477c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 748bbb68dfaSTejun Heo 7497c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7507c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 751bbb68dfaSTejun Heo } 752bbb68dfaSTejun Heo 753bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 754bbb68dfaSTejun Heo { 755bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 756bbb68dfaSTejun Heo 757112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 758bbb68dfaSTejun Heo } 759bbb68dfaSTejun Heo 760e22bee78STejun Heo /* 7613270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7623270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 763d565ed63STejun Heo * they're being called with pool->lock held. 764e22bee78STejun Heo */ 765e22bee78STejun Heo 76663d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 767649027d7STejun Heo { 768e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 769649027d7STejun Heo } 770649027d7STejun Heo 771e22bee78STejun Heo /* 772e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 773e22bee78STejun Heo * running workers. 774974271c4STejun Heo * 775974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 776706026c2STejun Heo * function will always return %true for unbound pools as long as the 777974271c4STejun Heo * worklist isn't empty. 778e22bee78STejun Heo */ 77963d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 780e22bee78STejun Heo { 78163d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 782e22bee78STejun Heo } 783e22bee78STejun Heo 784e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 78563d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 786e22bee78STejun Heo { 78763d95a91STejun Heo return pool->nr_idle; 788e22bee78STejun Heo } 789e22bee78STejun Heo 790e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 79163d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 792e22bee78STejun Heo { 793e19e397aSTejun Heo return !list_empty(&pool->worklist) && 794e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 795e22bee78STejun Heo } 796e22bee78STejun Heo 797e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 79863d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 799e22bee78STejun Heo { 80063d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 801e22bee78STejun Heo } 802e22bee78STejun Heo 803e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 80463d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 805e22bee78STejun Heo { 806692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 80763d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 80863d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 809e22bee78STejun Heo 810e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 811e22bee78STejun Heo } 812e22bee78STejun Heo 813e22bee78STejun Heo /* 814e22bee78STejun Heo * Wake up functions. 815e22bee78STejun Heo */ 816e22bee78STejun Heo 8171037de36SLai Jiangshan /* Return the first idle worker. Safe with preemption disabled */ 8181037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8197e11629dSTejun Heo { 82063d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8217e11629dSTejun Heo return NULL; 8227e11629dSTejun Heo 82363d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8247e11629dSTejun Heo } 8257e11629dSTejun Heo 8267e11629dSTejun Heo /** 8277e11629dSTejun Heo * wake_up_worker - wake up an idle worker 82863d95a91STejun Heo * @pool: worker pool to wake worker from 8297e11629dSTejun Heo * 83063d95a91STejun Heo * Wake up the first idle worker of @pool. 8317e11629dSTejun Heo * 8327e11629dSTejun Heo * CONTEXT: 833d565ed63STejun Heo * spin_lock_irq(pool->lock). 8347e11629dSTejun Heo */ 83563d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8367e11629dSTejun Heo { 8371037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8387e11629dSTejun Heo 8397e11629dSTejun Heo if (likely(worker)) 8407e11629dSTejun Heo wake_up_process(worker->task); 8417e11629dSTejun Heo } 8427e11629dSTejun Heo 8434690c4abSTejun Heo /** 844e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 845e22bee78STejun Heo * @task: task waking up 846e22bee78STejun Heo * @cpu: CPU @task is waking up to 847e22bee78STejun Heo * 848e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 849e22bee78STejun Heo * being awoken. 850e22bee78STejun Heo * 851e22bee78STejun Heo * CONTEXT: 852e22bee78STejun Heo * spin_lock_irq(rq->lock) 853e22bee78STejun Heo */ 854d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu) 855e22bee78STejun Heo { 856e22bee78STejun Heo struct worker *worker = kthread_data(task); 857e22bee78STejun Heo 85836576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 859ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 860e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 861e22bee78STejun Heo } 86236576000SJoonsoo Kim } 863e22bee78STejun Heo 864e22bee78STejun Heo /** 865e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 866e22bee78STejun Heo * @task: task going to sleep 867e22bee78STejun Heo * 868e22bee78STejun Heo * This function is called during schedule() when a busy worker is 869e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 870e22bee78STejun Heo * returning pointer to its task. 871e22bee78STejun Heo * 872e22bee78STejun Heo * CONTEXT: 873e22bee78STejun Heo * spin_lock_irq(rq->lock) 874e22bee78STejun Heo * 875d185af30SYacine Belkadi * Return: 876e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 877e22bee78STejun Heo */ 8789b7f6597SAlexander Gordeev struct task_struct *wq_worker_sleeping(struct task_struct *task) 879e22bee78STejun Heo { 880e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 881111c225aSTejun Heo struct worker_pool *pool; 882e22bee78STejun Heo 883111c225aSTejun Heo /* 884111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 885111c225aSTejun Heo * workers, also reach here, let's not access anything before 886111c225aSTejun Heo * checking NOT_RUNNING. 887111c225aSTejun Heo */ 8882d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 889e22bee78STejun Heo return NULL; 890e22bee78STejun Heo 891111c225aSTejun Heo pool = worker->pool; 892111c225aSTejun Heo 893e22bee78STejun Heo /* this can only happen on the local cpu */ 8949b7f6597SAlexander Gordeev if (WARN_ON_ONCE(pool->cpu != raw_smp_processor_id())) 8956183c009STejun Heo return NULL; 896e22bee78STejun Heo 897e22bee78STejun Heo /* 898e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 899e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 900e22bee78STejun Heo * Please read comment there. 901e22bee78STejun Heo * 902628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 903628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 904628c78e7STejun Heo * disabled, which in turn means that none else could be 905d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 906628c78e7STejun Heo * lock is safe. 907e22bee78STejun Heo */ 908e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 909e19e397aSTejun Heo !list_empty(&pool->worklist)) 9101037de36SLai Jiangshan to_wakeup = first_idle_worker(pool); 911e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 912e22bee78STejun Heo } 913e22bee78STejun Heo 914e22bee78STejun Heo /** 915e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 916cb444766STejun Heo * @worker: self 917d302f017STejun Heo * @flags: flags to set 918d302f017STejun Heo * 919228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 920d302f017STejun Heo * 921cb444766STejun Heo * CONTEXT: 922d565ed63STejun Heo * spin_lock_irq(pool->lock) 923d302f017STejun Heo */ 924228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 925d302f017STejun Heo { 926bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 927e22bee78STejun Heo 928cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 929cb444766STejun Heo 930228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 931e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 932e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 933e19e397aSTejun Heo atomic_dec(&pool->nr_running); 934e22bee78STejun Heo } 935e22bee78STejun Heo 936d302f017STejun Heo worker->flags |= flags; 937d302f017STejun Heo } 938d302f017STejun Heo 939d302f017STejun Heo /** 940e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 941cb444766STejun Heo * @worker: self 942d302f017STejun Heo * @flags: flags to clear 943d302f017STejun Heo * 944e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 945d302f017STejun Heo * 946cb444766STejun Heo * CONTEXT: 947d565ed63STejun Heo * spin_lock_irq(pool->lock) 948d302f017STejun Heo */ 949d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 950d302f017STejun Heo { 95163d95a91STejun Heo struct worker_pool *pool = worker->pool; 952e22bee78STejun Heo unsigned int oflags = worker->flags; 953e22bee78STejun Heo 954cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 955cb444766STejun Heo 956d302f017STejun Heo worker->flags &= ~flags; 957e22bee78STejun Heo 95842c025f3STejun Heo /* 95942c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 96042c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 96142c025f3STejun Heo * of multiple flags, not a single flag. 96242c025f3STejun Heo */ 963e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 964e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 965e19e397aSTejun Heo atomic_inc(&pool->nr_running); 966d302f017STejun Heo } 967d302f017STejun Heo 968d302f017STejun Heo /** 9698cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 970c9e7cf27STejun Heo * @pool: pool of interest 9718cca0eeaSTejun Heo * @work: work to find worker for 9728cca0eeaSTejun Heo * 973c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 974c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 975a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 976a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 977a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 978a2c1c57bSTejun Heo * being executed. 979a2c1c57bSTejun Heo * 980a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 981a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 982a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 983a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 984a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 985a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 986a2c1c57bSTejun Heo * 987c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 988c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 989c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 990c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 991c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 992c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 9938cca0eeaSTejun Heo * 9948cca0eeaSTejun Heo * CONTEXT: 995d565ed63STejun Heo * spin_lock_irq(pool->lock). 9968cca0eeaSTejun Heo * 997d185af30SYacine Belkadi * Return: 998d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 9998cca0eeaSTejun Heo * otherwise. 10008cca0eeaSTejun Heo */ 1001c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 10028cca0eeaSTejun Heo struct work_struct *work) 10038cca0eeaSTejun Heo { 100442f8570fSSasha Levin struct worker *worker; 100542f8570fSSasha Levin 1006b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1007a2c1c57bSTejun Heo (unsigned long)work) 1008a2c1c57bSTejun Heo if (worker->current_work == work && 1009a2c1c57bSTejun Heo worker->current_func == work->func) 101042f8570fSSasha Levin return worker; 101142f8570fSSasha Levin 101242f8570fSSasha Levin return NULL; 10138cca0eeaSTejun Heo } 10148cca0eeaSTejun Heo 10158cca0eeaSTejun Heo /** 1016bf4ede01STejun Heo * move_linked_works - move linked works to a list 1017bf4ede01STejun Heo * @work: start of series of works to be scheduled 1018bf4ede01STejun Heo * @head: target list to append @work to 1019402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1020bf4ede01STejun Heo * 1021bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1022bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1023bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1024bf4ede01STejun Heo * 1025bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1026bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1027bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1028bf4ede01STejun Heo * 1029bf4ede01STejun Heo * CONTEXT: 1030d565ed63STejun Heo * spin_lock_irq(pool->lock). 1031bf4ede01STejun Heo */ 1032bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1033bf4ede01STejun Heo struct work_struct **nextp) 1034bf4ede01STejun Heo { 1035bf4ede01STejun Heo struct work_struct *n; 1036bf4ede01STejun Heo 1037bf4ede01STejun Heo /* 1038bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1039bf4ede01STejun Heo * use NULL for list head. 1040bf4ede01STejun Heo */ 1041bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1042bf4ede01STejun Heo list_move_tail(&work->entry, head); 1043bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1044bf4ede01STejun Heo break; 1045bf4ede01STejun Heo } 1046bf4ede01STejun Heo 1047bf4ede01STejun Heo /* 1048bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1049bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1050bf4ede01STejun Heo * needs to be updated. 1051bf4ede01STejun Heo */ 1052bf4ede01STejun Heo if (nextp) 1053bf4ede01STejun Heo *nextp = n; 1054bf4ede01STejun Heo } 1055bf4ede01STejun Heo 10568864b4e5STejun Heo /** 10578864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 10588864b4e5STejun Heo * @pwq: pool_workqueue to get 10598864b4e5STejun Heo * 10608864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 10618864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 10628864b4e5STejun Heo */ 10638864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 10648864b4e5STejun Heo { 10658864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10668864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 10678864b4e5STejun Heo pwq->refcnt++; 10688864b4e5STejun Heo } 10698864b4e5STejun Heo 10708864b4e5STejun Heo /** 10718864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 10728864b4e5STejun Heo * @pwq: pool_workqueue to put 10738864b4e5STejun Heo * 10748864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 10758864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 10768864b4e5STejun Heo */ 10778864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 10788864b4e5STejun Heo { 10798864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10808864b4e5STejun Heo if (likely(--pwq->refcnt)) 10818864b4e5STejun Heo return; 10828864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 10838864b4e5STejun Heo return; 10848864b4e5STejun Heo /* 10858864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 10868864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 10878864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 10888864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 10898864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 10908864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 10918864b4e5STejun Heo */ 10928864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 10938864b4e5STejun Heo } 10948864b4e5STejun Heo 1095dce90d47STejun Heo /** 1096dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1097dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1098dce90d47STejun Heo * 1099dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1100dce90d47STejun Heo */ 1101dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1102dce90d47STejun Heo { 1103dce90d47STejun Heo if (pwq) { 1104dce90d47STejun Heo /* 1105dce90d47STejun Heo * As both pwqs and pools are sched-RCU protected, the 1106dce90d47STejun Heo * following lock operations are safe. 1107dce90d47STejun Heo */ 1108dce90d47STejun Heo spin_lock_irq(&pwq->pool->lock); 1109dce90d47STejun Heo put_pwq(pwq); 1110dce90d47STejun Heo spin_unlock_irq(&pwq->pool->lock); 1111dce90d47STejun Heo } 1112dce90d47STejun Heo } 1113dce90d47STejun Heo 1114112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 1115bf4ede01STejun Heo { 1116112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1117bf4ede01STejun Heo 1118bf4ede01STejun Heo trace_workqueue_activate_work(work); 111982607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 112082607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1121112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1122bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 1123112202d9STejun Heo pwq->nr_active++; 1124bf4ede01STejun Heo } 1125bf4ede01STejun Heo 1126112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 11273aa62497SLai Jiangshan { 1128112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 11293aa62497SLai Jiangshan struct work_struct, entry); 11303aa62497SLai Jiangshan 1131112202d9STejun Heo pwq_activate_delayed_work(work); 11323aa62497SLai Jiangshan } 11333aa62497SLai Jiangshan 1134bf4ede01STejun Heo /** 1135112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1136112202d9STejun Heo * @pwq: pwq of interest 1137bf4ede01STejun Heo * @color: color of work which left the queue 1138bf4ede01STejun Heo * 1139bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1140112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1141bf4ede01STejun Heo * 1142bf4ede01STejun Heo * CONTEXT: 1143d565ed63STejun Heo * spin_lock_irq(pool->lock). 1144bf4ede01STejun Heo */ 1145112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1146bf4ede01STejun Heo { 11478864b4e5STejun Heo /* uncolored work items don't participate in flushing or nr_active */ 1148bf4ede01STejun Heo if (color == WORK_NO_COLOR) 11498864b4e5STejun Heo goto out_put; 1150bf4ede01STejun Heo 1151112202d9STejun Heo pwq->nr_in_flight[color]--; 1152bf4ede01STejun Heo 1153112202d9STejun Heo pwq->nr_active--; 1154112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 1155bf4ede01STejun Heo /* one down, submit a delayed one */ 1156112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1157112202d9STejun Heo pwq_activate_first_delayed(pwq); 1158bf4ede01STejun Heo } 1159bf4ede01STejun Heo 1160bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1161112202d9STejun Heo if (likely(pwq->flush_color != color)) 11628864b4e5STejun Heo goto out_put; 1163bf4ede01STejun Heo 1164bf4ede01STejun Heo /* are there still in-flight works? */ 1165112202d9STejun Heo if (pwq->nr_in_flight[color]) 11668864b4e5STejun Heo goto out_put; 1167bf4ede01STejun Heo 1168112202d9STejun Heo /* this pwq is done, clear flush_color */ 1169112202d9STejun Heo pwq->flush_color = -1; 1170bf4ede01STejun Heo 1171bf4ede01STejun Heo /* 1172112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1173bf4ede01STejun Heo * will handle the rest. 1174bf4ede01STejun Heo */ 1175112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1176112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 11778864b4e5STejun Heo out_put: 11788864b4e5STejun Heo put_pwq(pwq); 1179bf4ede01STejun Heo } 1180bf4ede01STejun Heo 118136e227d2STejun Heo /** 1182bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 118336e227d2STejun Heo * @work: work item to steal 118436e227d2STejun Heo * @is_dwork: @work is a delayed_work 1185bbb68dfaSTejun Heo * @flags: place to store irq state 118636e227d2STejun Heo * 118736e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1188d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 118936e227d2STejun Heo * 1190d185af30SYacine Belkadi * Return: 119136e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 119236e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 119336e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1194bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1195bbb68dfaSTejun Heo * for arbitrarily long 119636e227d2STejun Heo * 1197d185af30SYacine Belkadi * Note: 1198bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1199e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1200e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1201e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1202bbb68dfaSTejun Heo * 1203bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1204bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1205bbb68dfaSTejun Heo * 1206e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1207bf4ede01STejun Heo */ 1208bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1209bbb68dfaSTejun Heo unsigned long *flags) 1210bf4ede01STejun Heo { 1211d565ed63STejun Heo struct worker_pool *pool; 1212112202d9STejun Heo struct pool_workqueue *pwq; 1213bf4ede01STejun Heo 1214bbb68dfaSTejun Heo local_irq_save(*flags); 1215bbb68dfaSTejun Heo 121636e227d2STejun Heo /* try to steal the timer if it exists */ 121736e227d2STejun Heo if (is_dwork) { 121836e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 121936e227d2STejun Heo 1220e0aecdd8STejun Heo /* 1221e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1222e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1223e0aecdd8STejun Heo * running on the local CPU. 1224e0aecdd8STejun Heo */ 122536e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 122636e227d2STejun Heo return 1; 122736e227d2STejun Heo } 122836e227d2STejun Heo 122936e227d2STejun Heo /* try to claim PENDING the normal way */ 1230bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1231bf4ede01STejun Heo return 0; 1232bf4ede01STejun Heo 1233bf4ede01STejun Heo /* 1234bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1235bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1236bf4ede01STejun Heo */ 1237d565ed63STejun Heo pool = get_work_pool(work); 1238d565ed63STejun Heo if (!pool) 1239bbb68dfaSTejun Heo goto fail; 1240bf4ede01STejun Heo 1241d565ed63STejun Heo spin_lock(&pool->lock); 1242bf4ede01STejun Heo /* 1243112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1244112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1245112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1246112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1247112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 12480b3dae68SLai Jiangshan * item is currently queued on that pool. 1249bf4ede01STejun Heo */ 1250112202d9STejun Heo pwq = get_work_pwq(work); 1251112202d9STejun Heo if (pwq && pwq->pool == pool) { 1252bf4ede01STejun Heo debug_work_deactivate(work); 12533aa62497SLai Jiangshan 12543aa62497SLai Jiangshan /* 125516062836STejun Heo * A delayed work item cannot be grabbed directly because 125616062836STejun Heo * it might have linked NO_COLOR work items which, if left 1257112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 125816062836STejun Heo * management later on and cause stall. Make sure the work 125916062836STejun Heo * item is activated before grabbing. 12603aa62497SLai Jiangshan */ 12613aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1262112202d9STejun Heo pwq_activate_delayed_work(work); 12633aa62497SLai Jiangshan 1264bf4ede01STejun Heo list_del_init(&work->entry); 12659c34a704SLai Jiangshan pwq_dec_nr_in_flight(pwq, get_work_color(work)); 126636e227d2STejun Heo 1267112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 12684468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 12694468a00fSLai Jiangshan 1270d565ed63STejun Heo spin_unlock(&pool->lock); 127136e227d2STejun Heo return 1; 1272bf4ede01STejun Heo } 1273d565ed63STejun Heo spin_unlock(&pool->lock); 1274bbb68dfaSTejun Heo fail: 1275bbb68dfaSTejun Heo local_irq_restore(*flags); 1276bbb68dfaSTejun Heo if (work_is_canceling(work)) 1277bbb68dfaSTejun Heo return -ENOENT; 1278bbb68dfaSTejun Heo cpu_relax(); 127936e227d2STejun Heo return -EAGAIN; 1280bf4ede01STejun Heo } 1281bf4ede01STejun Heo 1282bf4ede01STejun Heo /** 1283706026c2STejun Heo * insert_work - insert a work into a pool 1284112202d9STejun Heo * @pwq: pwq @work belongs to 12854690c4abSTejun Heo * @work: work to insert 12864690c4abSTejun Heo * @head: insertion point 12874690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 12884690c4abSTejun Heo * 1289112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1290706026c2STejun Heo * work_struct flags. 12914690c4abSTejun Heo * 12924690c4abSTejun Heo * CONTEXT: 1293d565ed63STejun Heo * spin_lock_irq(pool->lock). 1294365970a1SDavid Howells */ 1295112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1296112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1297b89deed3SOleg Nesterov { 1298112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1299e1d8aa9fSFrederic Weisbecker 13004690c4abSTejun Heo /* we own @work, set data and link */ 1301112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 13021a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 13038864b4e5STejun Heo get_pwq(pwq); 1304e22bee78STejun Heo 1305e22bee78STejun Heo /* 1306c5aa87bbSTejun Heo * Ensure either wq_worker_sleeping() sees the above 1307c5aa87bbSTejun Heo * list_add_tail() or we see zero nr_running to avoid workers lying 1308c5aa87bbSTejun Heo * around lazily while there are works to be processed. 1309e22bee78STejun Heo */ 1310e22bee78STejun Heo smp_mb(); 1311e22bee78STejun Heo 131263d95a91STejun Heo if (__need_more_worker(pool)) 131363d95a91STejun Heo wake_up_worker(pool); 1314b89deed3SOleg Nesterov } 1315b89deed3SOleg Nesterov 1316c8efcc25STejun Heo /* 1317c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 13188d03ecfeSTejun Heo * same workqueue. 1319c8efcc25STejun Heo */ 1320c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1321c8efcc25STejun Heo { 1322c8efcc25STejun Heo struct worker *worker; 1323c8efcc25STejun Heo 13248d03ecfeSTejun Heo worker = current_wq_worker(); 1325c8efcc25STejun Heo /* 13268d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 13278d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1328c8efcc25STejun Heo */ 1329112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1330c8efcc25STejun Heo } 1331c8efcc25STejun Heo 1332ef557180SMike Galbraith /* 1333ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1334ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1335ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1336ef557180SMike Galbraith */ 1337ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1338ef557180SMike Galbraith { 1339f303fccbSTejun Heo static bool printed_dbg_warning; 1340ef557180SMike Galbraith int new_cpu; 1341ef557180SMike Galbraith 1342f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1343ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1344ef557180SMike Galbraith return cpu; 1345f303fccbSTejun Heo } else if (!printed_dbg_warning) { 1346f303fccbSTejun Heo pr_warn("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1347f303fccbSTejun Heo printed_dbg_warning = true; 1348f303fccbSTejun Heo } 1349f303fccbSTejun Heo 1350ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1351ef557180SMike Galbraith return cpu; 1352ef557180SMike Galbraith 1353ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1354ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1355ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1356ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1357ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1358ef557180SMike Galbraith return cpu; 1359ef557180SMike Galbraith } 1360ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1361ef557180SMike Galbraith 1362ef557180SMike Galbraith return new_cpu; 1363ef557180SMike Galbraith } 1364ef557180SMike Galbraith 1365d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 13661da177e4SLinus Torvalds struct work_struct *work) 13671da177e4SLinus Torvalds { 1368112202d9STejun Heo struct pool_workqueue *pwq; 1369c9178087STejun Heo struct worker_pool *last_pool; 13701e19ffc6STejun Heo struct list_head *worklist; 13718a2e8e5dSTejun Heo unsigned int work_flags; 1372b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 13738930cabaSTejun Heo 13748930cabaSTejun Heo /* 13758930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 13768930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 13778930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 13788930cabaSTejun Heo * happen with IRQ disabled. 13798930cabaSTejun Heo */ 13808e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 13811da177e4SLinus Torvalds 1382dc186ad7SThomas Gleixner debug_work_activate(work); 13831e19ffc6STejun Heo 13849ef28a73SLi Bin /* if draining, only works from the same workqueue are allowed */ 1385618b01ebSTejun Heo if (unlikely(wq->flags & __WQ_DRAINING) && 1386c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1387e41e704bSTejun Heo return; 13889e8cd2f5STejun Heo retry: 1389df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1390ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1391df2d5ae4STejun Heo 1392df2d5ae4STejun Heo /* pwq which will be used unless @work is executing elsewhere */ 1393df2d5ae4STejun Heo if (!(wq->flags & WQ_UNBOUND)) 1394c9178087STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1395df2d5ae4STejun Heo else 1396df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1397f3421797STejun Heo 139818aa9effSTejun Heo /* 1399c9178087STejun Heo * If @work was previously on a different pool, it might still be 1400c9178087STejun Heo * running there, in which case the work needs to be queued on that 1401c9178087STejun Heo * pool to guarantee non-reentrancy. 140218aa9effSTejun Heo */ 1403c9e7cf27STejun Heo last_pool = get_work_pool(work); 1404112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 140518aa9effSTejun Heo struct worker *worker; 140618aa9effSTejun Heo 1407d565ed63STejun Heo spin_lock(&last_pool->lock); 140818aa9effSTejun Heo 1409c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 141018aa9effSTejun Heo 1411112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1412c9178087STejun Heo pwq = worker->current_pwq; 14138594fadeSLai Jiangshan } else { 141418aa9effSTejun Heo /* meh... not running there, queue here */ 1415d565ed63STejun Heo spin_unlock(&last_pool->lock); 1416112202d9STejun Heo spin_lock(&pwq->pool->lock); 141718aa9effSTejun Heo } 14188930cabaSTejun Heo } else { 1419112202d9STejun Heo spin_lock(&pwq->pool->lock); 14208930cabaSTejun Heo } 1421502ca9d8STejun Heo 14229e8cd2f5STejun Heo /* 14239e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 14249e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 14259e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1426df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1427df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 14289e8cd2f5STejun Heo * make forward-progress. 14299e8cd2f5STejun Heo */ 14309e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 14319e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 14329e8cd2f5STejun Heo spin_unlock(&pwq->pool->lock); 14339e8cd2f5STejun Heo cpu_relax(); 14349e8cd2f5STejun Heo goto retry; 14359e8cd2f5STejun Heo } 14369e8cd2f5STejun Heo /* oops */ 14379e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 14389e8cd2f5STejun Heo wq->name, cpu); 14399e8cd2f5STejun Heo } 14409e8cd2f5STejun Heo 1441112202d9STejun Heo /* pwq determined, queue */ 1442112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1443502ca9d8STejun Heo 1444f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1445112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1446f5b2552bSDan Carpenter return; 1447f5b2552bSDan Carpenter } 14481e19ffc6STejun Heo 1449112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1450112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 14511e19ffc6STejun Heo 1452112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1453cdadf009STejun Heo trace_workqueue_activate_work(work); 1454112202d9STejun Heo pwq->nr_active++; 1455112202d9STejun Heo worklist = &pwq->pool->worklist; 145682607adcSTejun Heo if (list_empty(worklist)) 145782607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 14588a2e8e5dSTejun Heo } else { 14598a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1460112202d9STejun Heo worklist = &pwq->delayed_works; 14618a2e8e5dSTejun Heo } 14621e19ffc6STejun Heo 1463112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 14641e19ffc6STejun Heo 1465112202d9STejun Heo spin_unlock(&pwq->pool->lock); 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14680fcb78c2SRolf Eike Beer /** 1469c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1470c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1471c1a220e7SZhang Rui * @wq: workqueue to use 1472c1a220e7SZhang Rui * @work: work to queue 1473c1a220e7SZhang Rui * 1474c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1475c1a220e7SZhang Rui * can't go away. 1476d185af30SYacine Belkadi * 1477d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1478c1a220e7SZhang Rui */ 1479d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1480d4283e93STejun Heo struct work_struct *work) 1481c1a220e7SZhang Rui { 1482d4283e93STejun Heo bool ret = false; 14838930cabaSTejun Heo unsigned long flags; 14848930cabaSTejun Heo 14858930cabaSTejun Heo local_irq_save(flags); 1486c1a220e7SZhang Rui 148722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 14884690c4abSTejun Heo __queue_work(cpu, wq, work); 1489d4283e93STejun Heo ret = true; 1490c1a220e7SZhang Rui } 14918930cabaSTejun Heo 14928930cabaSTejun Heo local_irq_restore(flags); 1493c1a220e7SZhang Rui return ret; 1494c1a220e7SZhang Rui } 1495ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1496c1a220e7SZhang Rui 14978c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 14981da177e4SLinus Torvalds { 14998c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 15001da177e4SLinus Torvalds 1501e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 150260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 15031da177e4SLinus Torvalds } 15041438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 15051da177e4SLinus Torvalds 15067beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 150752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 15081da177e4SLinus Torvalds { 15097beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 15107beb2edfSTejun Heo struct work_struct *work = &dwork->work; 15111da177e4SLinus Torvalds 1512637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 1513841b86f3SKees Cook WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1514fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1515fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 15167beb2edfSTejun Heo 15178852aac2STejun Heo /* 15188852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 15198852aac2STejun Heo * both optimization and correctness. The earliest @timer can 15208852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 15218852aac2STejun Heo * on that there's no such delay when @delay is 0. 15228852aac2STejun Heo */ 15238852aac2STejun Heo if (!delay) { 15248852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 15258852aac2STejun Heo return; 15268852aac2STejun Heo } 15278852aac2STejun Heo 152860c057bcSLai Jiangshan dwork->wq = wq; 15291265057fSTejun Heo dwork->cpu = cpu; 15307beb2edfSTejun Heo timer->expires = jiffies + delay; 15317beb2edfSTejun Heo 1532041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 15337beb2edfSTejun Heo add_timer_on(timer, cpu); 1534041bd12eSTejun Heo else 1535041bd12eSTejun Heo add_timer(timer); 15367beb2edfSTejun Heo } 15371da177e4SLinus Torvalds 15380fcb78c2SRolf Eike Beer /** 15390fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 15400fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 15410fcb78c2SRolf Eike Beer * @wq: workqueue to use 1542af9997e4SRandy Dunlap * @dwork: work to queue 15430fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 15440fcb78c2SRolf Eike Beer * 1545d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1546715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1547715f1300STejun Heo * execution. 15480fcb78c2SRolf Eike Beer */ 1549d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 155052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 15517a6bc1cdSVenkatesh Pallipadi { 155252bad64dSDavid Howells struct work_struct *work = &dwork->work; 1553d4283e93STejun Heo bool ret = false; 15548930cabaSTejun Heo unsigned long flags; 15558930cabaSTejun Heo 15568930cabaSTejun Heo /* read the comment in __queue_work() */ 15578930cabaSTejun Heo local_irq_save(flags); 15587a6bc1cdSVenkatesh Pallipadi 155922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15607beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1561d4283e93STejun Heo ret = true; 15627a6bc1cdSVenkatesh Pallipadi } 15638930cabaSTejun Heo 15648930cabaSTejun Heo local_irq_restore(flags); 15657a6bc1cdSVenkatesh Pallipadi return ret; 15667a6bc1cdSVenkatesh Pallipadi } 1567ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 15681da177e4SLinus Torvalds 1569c8e55f36STejun Heo /** 15708376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 15718376fe22STejun Heo * @cpu: CPU number to execute work on 15728376fe22STejun Heo * @wq: workqueue to use 15738376fe22STejun Heo * @dwork: work to queue 15748376fe22STejun Heo * @delay: number of jiffies to wait before queueing 15758376fe22STejun Heo * 15768376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 15778376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 15788376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 15798376fe22STejun Heo * current state. 15808376fe22STejun Heo * 1581d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 15828376fe22STejun Heo * pending and its timer was modified. 15838376fe22STejun Heo * 1584e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 15858376fe22STejun Heo * See try_to_grab_pending() for details. 15868376fe22STejun Heo */ 15878376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 15888376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 15898376fe22STejun Heo { 15908376fe22STejun Heo unsigned long flags; 15918376fe22STejun Heo int ret; 15928376fe22STejun Heo 15938376fe22STejun Heo do { 15948376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 15958376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 15968376fe22STejun Heo 15978376fe22STejun Heo if (likely(ret >= 0)) { 15988376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 15998376fe22STejun Heo local_irq_restore(flags); 16008376fe22STejun Heo } 16018376fe22STejun Heo 16028376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 16038376fe22STejun Heo return ret; 16048376fe22STejun Heo } 16058376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 16068376fe22STejun Heo 16078376fe22STejun Heo /** 1608c8e55f36STejun Heo * worker_enter_idle - enter idle state 1609c8e55f36STejun Heo * @worker: worker which is entering idle state 1610c8e55f36STejun Heo * 1611c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1612c8e55f36STejun Heo * necessary. 1613c8e55f36STejun Heo * 1614c8e55f36STejun Heo * LOCKING: 1615d565ed63STejun Heo * spin_lock_irq(pool->lock). 1616c8e55f36STejun Heo */ 1617c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 16181da177e4SLinus Torvalds { 1619bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1620c8e55f36STejun Heo 16216183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 16226183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 16236183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 16246183c009STejun Heo return; 1625c8e55f36STejun Heo 1626051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1627cb444766STejun Heo worker->flags |= WORKER_IDLE; 1628bd7bdd43STejun Heo pool->nr_idle++; 1629e22bee78STejun Heo worker->last_active = jiffies; 1630c8e55f36STejun Heo 1631c8e55f36STejun Heo /* idle_list is LIFO */ 1632bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1633db7bccf4STejun Heo 163463d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1635628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1636cb444766STejun Heo 1637544ecf31STejun Heo /* 1638706026c2STejun Heo * Sanity check nr_running. Because wq_unbind_fn() releases 1639d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1640628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1641628c78e7STejun Heo * unbind is not in progress. 1642544ecf31STejun Heo */ 164324647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1644bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1645e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1646c8e55f36STejun Heo } 1647c8e55f36STejun Heo 1648c8e55f36STejun Heo /** 1649c8e55f36STejun Heo * worker_leave_idle - leave idle state 1650c8e55f36STejun Heo * @worker: worker which is leaving idle state 1651c8e55f36STejun Heo * 1652c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1653c8e55f36STejun Heo * 1654c8e55f36STejun Heo * LOCKING: 1655d565ed63STejun Heo * spin_lock_irq(pool->lock). 1656c8e55f36STejun Heo */ 1657c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1658c8e55f36STejun Heo { 1659bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1660c8e55f36STejun Heo 16616183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 16626183c009STejun Heo return; 1663d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1664bd7bdd43STejun Heo pool->nr_idle--; 1665c8e55f36STejun Heo list_del_init(&worker->entry); 1666c8e55f36STejun Heo } 1667c8e55f36STejun Heo 1668f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1669c34056a3STejun Heo { 1670c34056a3STejun Heo struct worker *worker; 1671c34056a3STejun Heo 1672f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1673c8e55f36STejun Heo if (worker) { 1674c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1675affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1676da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1677e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1678e22bee78STejun Heo worker->flags = WORKER_PREP; 1679c8e55f36STejun Heo } 1680c34056a3STejun Heo return worker; 1681c34056a3STejun Heo } 1682c34056a3STejun Heo 1683c34056a3STejun Heo /** 16844736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 16854736cbf7SLai Jiangshan * @worker: worker to be attached 16864736cbf7SLai Jiangshan * @pool: the target pool 16874736cbf7SLai Jiangshan * 16884736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 16894736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 16904736cbf7SLai Jiangshan * cpu-[un]hotplugs. 16914736cbf7SLai Jiangshan */ 16924736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 16934736cbf7SLai Jiangshan struct worker_pool *pool) 16944736cbf7SLai Jiangshan { 16954736cbf7SLai Jiangshan mutex_lock(&pool->attach_mutex); 16964736cbf7SLai Jiangshan 16974736cbf7SLai Jiangshan /* 16984736cbf7SLai Jiangshan * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any 16994736cbf7SLai Jiangshan * online CPUs. It'll be re-applied when any of the CPUs come up. 17004736cbf7SLai Jiangshan */ 17014736cbf7SLai Jiangshan set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 17024736cbf7SLai Jiangshan 17034736cbf7SLai Jiangshan /* 17044736cbf7SLai Jiangshan * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains 17054736cbf7SLai Jiangshan * stable across this function. See the comments above the 17064736cbf7SLai Jiangshan * flag definition for details. 17074736cbf7SLai Jiangshan */ 17084736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 17094736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 17104736cbf7SLai Jiangshan 17114736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 17124736cbf7SLai Jiangshan 17134736cbf7SLai Jiangshan mutex_unlock(&pool->attach_mutex); 17144736cbf7SLai Jiangshan } 17154736cbf7SLai Jiangshan 17164736cbf7SLai Jiangshan /** 171760f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 171860f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 171960f5a4bcSLai Jiangshan * @pool: the pool @worker is attached to 172060f5a4bcSLai Jiangshan * 17214736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 17224736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 17234736cbf7SLai Jiangshan * other reference to the pool. 172460f5a4bcSLai Jiangshan */ 172560f5a4bcSLai Jiangshan static void worker_detach_from_pool(struct worker *worker, 172660f5a4bcSLai Jiangshan struct worker_pool *pool) 172760f5a4bcSLai Jiangshan { 172860f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 172960f5a4bcSLai Jiangshan 173092f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 1731da028469SLai Jiangshan list_del(&worker->node); 1732da028469SLai Jiangshan if (list_empty(&pool->workers)) 173360f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 173492f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 173560f5a4bcSLai Jiangshan 1736b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1737b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1738b62c0751SLai Jiangshan 173960f5a4bcSLai Jiangshan if (detach_completion) 174060f5a4bcSLai Jiangshan complete(detach_completion); 174160f5a4bcSLai Jiangshan } 174260f5a4bcSLai Jiangshan 174360f5a4bcSLai Jiangshan /** 1744c34056a3STejun Heo * create_worker - create a new workqueue worker 174563d95a91STejun Heo * @pool: pool the new worker will belong to 1746c34056a3STejun Heo * 1747051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1748c34056a3STejun Heo * 1749c34056a3STejun Heo * CONTEXT: 1750c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1751c34056a3STejun Heo * 1752d185af30SYacine Belkadi * Return: 1753c34056a3STejun Heo * Pointer to the newly created worker. 1754c34056a3STejun Heo */ 1755bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1756c34056a3STejun Heo { 1757c34056a3STejun Heo struct worker *worker = NULL; 1758f3421797STejun Heo int id = -1; 1759e3c916a4STejun Heo char id_buf[16]; 1760c34056a3STejun Heo 17617cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 17627cda9aaeSLai Jiangshan id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL); 1763822d8405STejun Heo if (id < 0) 1764c34056a3STejun Heo goto fail; 1765c34056a3STejun Heo 1766f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 1767c34056a3STejun Heo if (!worker) 1768c34056a3STejun Heo goto fail; 1769c34056a3STejun Heo 1770bd7bdd43STejun Heo worker->pool = pool; 1771c34056a3STejun Heo worker->id = id; 1772c34056a3STejun Heo 177329c91e99STejun Heo if (pool->cpu >= 0) 1774e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1775e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1776f3421797STejun Heo else 1777e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1778e3c916a4STejun Heo 1779f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1780e3c916a4STejun Heo "kworker/%s", id_buf); 1781c34056a3STejun Heo if (IS_ERR(worker->task)) 1782c34056a3STejun Heo goto fail; 1783c34056a3STejun Heo 178491151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 178525834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 178691151228SOleg Nesterov 1787da028469SLai Jiangshan /* successful, attach the worker to the pool */ 17884736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1789822d8405STejun Heo 1790051e1850SLai Jiangshan /* start the newly created worker */ 1791051e1850SLai Jiangshan spin_lock_irq(&pool->lock); 1792051e1850SLai Jiangshan worker->pool->nr_workers++; 1793051e1850SLai Jiangshan worker_enter_idle(worker); 1794051e1850SLai Jiangshan wake_up_process(worker->task); 1795051e1850SLai Jiangshan spin_unlock_irq(&pool->lock); 1796051e1850SLai Jiangshan 1797c34056a3STejun Heo return worker; 1798822d8405STejun Heo 1799c34056a3STejun Heo fail: 18009625ab17SLai Jiangshan if (id >= 0) 18017cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, id); 1802c34056a3STejun Heo kfree(worker); 1803c34056a3STejun Heo return NULL; 1804c34056a3STejun Heo } 1805c34056a3STejun Heo 1806c34056a3STejun Heo /** 1807c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1808c34056a3STejun Heo * @worker: worker to be destroyed 1809c34056a3STejun Heo * 181073eb7fe7SLai Jiangshan * Destroy @worker and adjust @pool stats accordingly. The worker should 181173eb7fe7SLai Jiangshan * be idle. 1812c8e55f36STejun Heo * 1813c8e55f36STejun Heo * CONTEXT: 181460f5a4bcSLai Jiangshan * spin_lock_irq(pool->lock). 1815c34056a3STejun Heo */ 1816c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1817c34056a3STejun Heo { 1818bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1819c34056a3STejun Heo 1820cd549687STejun Heo lockdep_assert_held(&pool->lock); 1821cd549687STejun Heo 1822c34056a3STejun Heo /* sanity check frenzy */ 18236183c009STejun Heo if (WARN_ON(worker->current_work) || 182473eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 182573eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 18266183c009STejun Heo return; 1827c34056a3STejun Heo 1828bd7bdd43STejun Heo pool->nr_workers--; 1829bd7bdd43STejun Heo pool->nr_idle--; 1830c8e55f36STejun Heo 1831c8e55f36STejun Heo list_del_init(&worker->entry); 1832cb444766STejun Heo worker->flags |= WORKER_DIE; 183360f5a4bcSLai Jiangshan wake_up_process(worker->task); 1834c34056a3STejun Heo } 1835c34056a3STejun Heo 183632a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 1837e22bee78STejun Heo { 183832a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 1839e22bee78STejun Heo 1840d565ed63STejun Heo spin_lock_irq(&pool->lock); 1841e22bee78STejun Heo 18423347fc9fSLai Jiangshan while (too_many_workers(pool)) { 1843e22bee78STejun Heo struct worker *worker; 1844e22bee78STejun Heo unsigned long expires; 1845e22bee78STejun Heo 1846e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 184763d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1848e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1849e22bee78STejun Heo 18503347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 185163d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 18523347fc9fSLai Jiangshan break; 1853e22bee78STejun Heo } 18543347fc9fSLai Jiangshan 18553347fc9fSLai Jiangshan destroy_worker(worker); 1856e22bee78STejun Heo } 1857e22bee78STejun Heo 1858d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1859e22bee78STejun Heo } 1860e22bee78STejun Heo 1861493a1724STejun Heo static void send_mayday(struct work_struct *work) 1862e22bee78STejun Heo { 1863112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1864112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1865493a1724STejun Heo 18662e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 1867e22bee78STejun Heo 1868493008a8STejun Heo if (!wq->rescuer) 1869493a1724STejun Heo return; 1870e22bee78STejun Heo 1871e22bee78STejun Heo /* mayday mayday mayday */ 1872493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 187377668c8bSLai Jiangshan /* 187477668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 187577668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 187677668c8bSLai Jiangshan * rescuer is done with it. 187777668c8bSLai Jiangshan */ 187877668c8bSLai Jiangshan get_pwq(pwq); 1879493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1880e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1881493a1724STejun Heo } 1882e22bee78STejun Heo } 1883e22bee78STejun Heo 188432a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 1885e22bee78STejun Heo { 188632a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 1887e22bee78STejun Heo struct work_struct *work; 1888e22bee78STejun Heo 1889b2d82909STejun Heo spin_lock_irq(&pool->lock); 1890b2d82909STejun Heo spin_lock(&wq_mayday_lock); /* for wq->maydays */ 1891e22bee78STejun Heo 189263d95a91STejun Heo if (need_to_create_worker(pool)) { 1893e22bee78STejun Heo /* 1894e22bee78STejun Heo * We've been trying to create a new worker but 1895e22bee78STejun Heo * haven't been successful. We might be hitting an 1896e22bee78STejun Heo * allocation deadlock. Send distress signals to 1897e22bee78STejun Heo * rescuers. 1898e22bee78STejun Heo */ 189963d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1900e22bee78STejun Heo send_mayday(work); 1901e22bee78STejun Heo } 1902e22bee78STejun Heo 1903b2d82909STejun Heo spin_unlock(&wq_mayday_lock); 1904b2d82909STejun Heo spin_unlock_irq(&pool->lock); 1905e22bee78STejun Heo 190663d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1907e22bee78STejun Heo } 1908e22bee78STejun Heo 1909e22bee78STejun Heo /** 1910e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 191163d95a91STejun Heo * @pool: pool to create a new worker for 1912e22bee78STejun Heo * 191363d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1914e22bee78STejun Heo * have at least one idle worker on return from this function. If 1915e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 191663d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1917e22bee78STejun Heo * possible allocation deadlock. 1918e22bee78STejun Heo * 1919c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 1920c5aa87bbSTejun Heo * may_start_working() %true. 1921e22bee78STejun Heo * 1922e22bee78STejun Heo * LOCKING: 1923d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1924e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1925e22bee78STejun Heo * manager. 1926e22bee78STejun Heo */ 192729187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 1928d565ed63STejun Heo __releases(&pool->lock) 1929d565ed63STejun Heo __acquires(&pool->lock) 1930e22bee78STejun Heo { 1931e22bee78STejun Heo restart: 1932d565ed63STejun Heo spin_unlock_irq(&pool->lock); 19339f9c2364STejun Heo 1934e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 193563d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1936e22bee78STejun Heo 1937e22bee78STejun Heo while (true) { 1938051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 1939e22bee78STejun Heo break; 1940e22bee78STejun Heo 1941e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 19429f9c2364STejun Heo 194363d95a91STejun Heo if (!need_to_create_worker(pool)) 1944e22bee78STejun Heo break; 1945e22bee78STejun Heo } 1946e22bee78STejun Heo 194763d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1948d565ed63STejun Heo spin_lock_irq(&pool->lock); 1949051e1850SLai Jiangshan /* 1950051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 1951051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 1952051e1850SLai Jiangshan * already become busy. 1953051e1850SLai Jiangshan */ 195463d95a91STejun Heo if (need_to_create_worker(pool)) 1955e22bee78STejun Heo goto restart; 1956e22bee78STejun Heo } 1957e22bee78STejun Heo 1958e22bee78STejun Heo /** 1959e22bee78STejun Heo * manage_workers - manage worker pool 1960e22bee78STejun Heo * @worker: self 1961e22bee78STejun Heo * 1962706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1963e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1964706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1965e22bee78STejun Heo * 1966e22bee78STejun Heo * The caller can safely start processing works on false return. On 1967e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1968e22bee78STejun Heo * and may_start_working() is true. 1969e22bee78STejun Heo * 1970e22bee78STejun Heo * CONTEXT: 1971d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1972e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1973e22bee78STejun Heo * 1974d185af30SYacine Belkadi * Return: 197529187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 197629187a9eSTejun Heo * start processing works, %true if management function was performed and 197729187a9eSTejun Heo * the conditions that the caller verified before calling the function may 197829187a9eSTejun Heo * no longer be true. 1979e22bee78STejun Heo */ 1980e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1981e22bee78STejun Heo { 198263d95a91STejun Heo struct worker_pool *pool = worker->pool; 1983e22bee78STejun Heo 1984692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 198529187a9eSTejun Heo return false; 1986692b4825STejun Heo 1987692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 19882607d7a6STejun Heo pool->manager = worker; 1989e22bee78STejun Heo 199029187a9eSTejun Heo maybe_create_worker(pool); 1991e22bee78STejun Heo 19922607d7a6STejun Heo pool->manager = NULL; 1993692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 1994692b4825STejun Heo wake_up(&wq_manager_wait); 199529187a9eSTejun Heo return true; 1996e22bee78STejun Heo } 1997e22bee78STejun Heo 1998a62428c0STejun Heo /** 1999a62428c0STejun Heo * process_one_work - process single work 2000c34056a3STejun Heo * @worker: self 2001a62428c0STejun Heo * @work: work to process 2002a62428c0STejun Heo * 2003a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2004a62428c0STejun Heo * process a single work including synchronization against and 2005a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2006a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2007a62428c0STejun Heo * call this function to process a work. 2008a62428c0STejun Heo * 2009a62428c0STejun Heo * CONTEXT: 2010d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2011a62428c0STejun Heo */ 2012c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2013d565ed63STejun Heo __releases(&pool->lock) 2014d565ed63STejun Heo __acquires(&pool->lock) 20151da177e4SLinus Torvalds { 2016112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2017bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2018112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 201973f53c4aSTejun Heo int work_color; 20207e11629dSTejun Heo struct worker *collision; 20214e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20224e6045f1SJohannes Berg /* 2023a62428c0STejun Heo * It is permissible to free the struct work_struct from 2024a62428c0STejun Heo * inside the function that is called from it, this we need to 2025a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2026a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2027a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20284e6045f1SJohannes Berg */ 20294d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20304d82a1deSPeter Zijlstra 20314d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20324e6045f1SJohannes Berg #endif 2033807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 203485327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2035ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 203625511a47STejun Heo 20377e11629dSTejun Heo /* 20387e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20397e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20407e11629dSTejun Heo * already processing the work. If so, defer the work to the 20417e11629dSTejun Heo * currently executing one. 20427e11629dSTejun Heo */ 2043c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 20447e11629dSTejun Heo if (unlikely(collision)) { 20457e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20467e11629dSTejun Heo return; 20477e11629dSTejun Heo } 20481da177e4SLinus Torvalds 20498930cabaSTejun Heo /* claim and dequeue */ 20501da177e4SLinus Torvalds debug_work_deactivate(work); 2051c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2052c34056a3STejun Heo worker->current_work = work; 2053a2c1c57bSTejun Heo worker->current_func = work->func; 2054112202d9STejun Heo worker->current_pwq = pwq; 205573f53c4aSTejun Heo work_color = get_work_color(work); 20567a22ad75STejun Heo 2057a62428c0STejun Heo list_del_init(&work->entry); 2058a62428c0STejun Heo 2059649027d7STejun Heo /* 2060228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2061228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2062228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2063228f1d00SLai Jiangshan * execution of the pending work items. 2064fb0e7bebSTejun Heo */ 2065fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2066228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2067fb0e7bebSTejun Heo 2068974271c4STejun Heo /* 2069a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2070a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2071a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2072a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2073228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2074974271c4STejun Heo */ 2075a489a03eSLai Jiangshan if (need_more_worker(pool)) 207663d95a91STejun Heo wake_up_worker(pool); 2077974271c4STejun Heo 20788930cabaSTejun Heo /* 20797c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2080d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 208123657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 208223657bb1STejun Heo * disabled. 20838930cabaSTejun Heo */ 20847c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 20851da177e4SLinus Torvalds 2086d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2087365970a1SDavid Howells 2088a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 20893295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2090e6f3faa7SPeter Zijlstra /* 2091f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2092f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2093e6f3faa7SPeter Zijlstra * 2094e6f3faa7SPeter Zijlstra * However, that would result in: 2095e6f3faa7SPeter Zijlstra * 2096e6f3faa7SPeter Zijlstra * A(W1) 2097e6f3faa7SPeter Zijlstra * WFC(C) 2098e6f3faa7SPeter Zijlstra * A(W1) 2099e6f3faa7SPeter Zijlstra * C(C) 2100e6f3faa7SPeter Zijlstra * 2101e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2102e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2103e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2104f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2105e6f3faa7SPeter Zijlstra * these locks. 2106e6f3faa7SPeter Zijlstra * 2107e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2108e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2109e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2110e6f3faa7SPeter Zijlstra */ 2111f52be570SPeter Zijlstra lockdep_invariant_state(true); 2112e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2113a2c1c57bSTejun Heo worker->current_func(work); 2114e36c886aSArjan van de Ven /* 2115e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2116e36c886aSArjan van de Ven * point will only record its address. 2117e36c886aSArjan van de Ven */ 2118e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 21193295f0efSIngo Molnar lock_map_release(&lockdep_map); 2120112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 21211da177e4SLinus Torvalds 2122d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2123044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2124044c782cSValentin Ilie " last function: %pf\n", 2125a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2126a2c1c57bSTejun Heo worker->current_func); 2127d5abe669SPeter Zijlstra debug_show_held_locks(current); 2128d5abe669SPeter Zijlstra dump_stack(); 2129d5abe669SPeter Zijlstra } 2130d5abe669SPeter Zijlstra 2131b22ce278STejun Heo /* 2132b22ce278STejun Heo * The following prevents a kworker from hogging CPU on !PREEMPT 2133b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2134b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2135b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2136789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2137789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2138b22ce278STejun Heo */ 21393e28e377SJoe Lawrence cond_resched_rcu_qs(); 2140b22ce278STejun Heo 2141d565ed63STejun Heo spin_lock_irq(&pool->lock); 2142a62428c0STejun Heo 2143fb0e7bebSTejun Heo /* clear cpu intensive status */ 2144fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2145fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2146fb0e7bebSTejun Heo 2147a62428c0STejun Heo /* we're done with it, release */ 214842f8570fSSasha Levin hash_del(&worker->hentry); 2149c34056a3STejun Heo worker->current_work = NULL; 2150a2c1c57bSTejun Heo worker->current_func = NULL; 2151112202d9STejun Heo worker->current_pwq = NULL; 21523d1cb205STejun Heo worker->desc_valid = false; 2153112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 21541da177e4SLinus Torvalds } 21551da177e4SLinus Torvalds 2156affee4b2STejun Heo /** 2157affee4b2STejun Heo * process_scheduled_works - process scheduled works 2158affee4b2STejun Heo * @worker: self 2159affee4b2STejun Heo * 2160affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2161affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2162affee4b2STejun Heo * fetches a work from the top and executes it. 2163affee4b2STejun Heo * 2164affee4b2STejun Heo * CONTEXT: 2165d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2166affee4b2STejun Heo * multiple times. 2167affee4b2STejun Heo */ 2168affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21691da177e4SLinus Torvalds { 2170affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2171affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2172a62428c0STejun Heo struct work_struct, entry); 2173c34056a3STejun Heo process_one_work(worker, work); 2174a62428c0STejun Heo } 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 21774690c4abSTejun Heo /** 21784690c4abSTejun Heo * worker_thread - the worker thread function 2179c34056a3STejun Heo * @__worker: self 21804690c4abSTejun Heo * 2181c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2182c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2183c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2184c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2185c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2186d185af30SYacine Belkadi * 2187d185af30SYacine Belkadi * Return: 0 21884690c4abSTejun Heo */ 2189c34056a3STejun Heo static int worker_thread(void *__worker) 21901da177e4SLinus Torvalds { 2191c34056a3STejun Heo struct worker *worker = __worker; 2192bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 21931da177e4SLinus Torvalds 2194e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2195e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2196c8e55f36STejun Heo woke_up: 2197d565ed63STejun Heo spin_lock_irq(&pool->lock); 2198affee4b2STejun Heo 2199a9ab775bSTejun Heo /* am I supposed to die? */ 2200a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2201d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2202a9ab775bSTejun Heo WARN_ON_ONCE(!list_empty(&worker->entry)); 2203e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 220460f5a4bcSLai Jiangshan 220560f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 22067cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, worker->id); 220760f5a4bcSLai Jiangshan worker_detach_from_pool(worker, pool); 220860f5a4bcSLai Jiangshan kfree(worker); 2209c8e55f36STejun Heo return 0; 2210c8e55f36STejun Heo } 2211c8e55f36STejun Heo 2212c8e55f36STejun Heo worker_leave_idle(worker); 2213db7bccf4STejun Heo recheck: 2214e22bee78STejun Heo /* no more worker necessary? */ 221563d95a91STejun Heo if (!need_more_worker(pool)) 2216e22bee78STejun Heo goto sleep; 2217e22bee78STejun Heo 2218e22bee78STejun Heo /* do we need to manage? */ 221963d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2220e22bee78STejun Heo goto recheck; 2221e22bee78STejun Heo 2222c8e55f36STejun Heo /* 2223c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2224c8e55f36STejun Heo * preparing to process a work or actually processing it. 2225c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2226c8e55f36STejun Heo */ 22276183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2228c8e55f36STejun Heo 2229e22bee78STejun Heo /* 2230a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2231a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2232a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2233a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2234a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2235e22bee78STejun Heo */ 2236a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2237e22bee78STejun Heo 2238e22bee78STejun Heo do { 2239affee4b2STejun Heo struct work_struct *work = 2240bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2241affee4b2STejun Heo struct work_struct, entry); 2242affee4b2STejun Heo 224382607adcSTejun Heo pool->watchdog_ts = jiffies; 224482607adcSTejun Heo 2245c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2246affee4b2STejun Heo /* optimization path, not strictly necessary */ 2247affee4b2STejun Heo process_one_work(worker, work); 2248affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2249affee4b2STejun Heo process_scheduled_works(worker); 2250affee4b2STejun Heo } else { 2251c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2252affee4b2STejun Heo process_scheduled_works(worker); 2253affee4b2STejun Heo } 225463d95a91STejun Heo } while (keep_working(pool)); 2255affee4b2STejun Heo 2256228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2257d313dd85STejun Heo sleep: 2258c8e55f36STejun Heo /* 2259d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2260d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2261d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2262d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2263d565ed63STejun Heo * event. 2264c8e55f36STejun Heo */ 2265c8e55f36STejun Heo worker_enter_idle(worker); 2266c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2267d565ed63STejun Heo spin_unlock_irq(&pool->lock); 22681da177e4SLinus Torvalds schedule(); 2269c8e55f36STejun Heo goto woke_up; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds 2272e22bee78STejun Heo /** 2273e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2274111c225aSTejun Heo * @__rescuer: self 2275e22bee78STejun Heo * 2276e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2277493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2278e22bee78STejun Heo * 2279706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2280e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2281e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2282e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2283e22bee78STejun Heo * the problem rescuer solves. 2284e22bee78STejun Heo * 2285706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2286706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2287e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2288e22bee78STejun Heo * 2289e22bee78STejun Heo * This should happen rarely. 2290d185af30SYacine Belkadi * 2291d185af30SYacine Belkadi * Return: 0 2292e22bee78STejun Heo */ 2293111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2294e22bee78STejun Heo { 2295111c225aSTejun Heo struct worker *rescuer = __rescuer; 2296111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2297e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 22984d595b86SLai Jiangshan bool should_stop; 2299e22bee78STejun Heo 2300e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2301111c225aSTejun Heo 2302111c225aSTejun Heo /* 2303111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2304111c225aSTejun Heo * doesn't participate in concurrency management. 2305111c225aSTejun Heo */ 2306111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2307e22bee78STejun Heo repeat: 2308c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 23091da177e4SLinus Torvalds 23104d595b86SLai Jiangshan /* 23114d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 23124d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 23134d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 23144d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 23154d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 23164d595b86SLai Jiangshan * list is always empty on exit. 23174d595b86SLai Jiangshan */ 23184d595b86SLai Jiangshan should_stop = kthread_should_stop(); 23191da177e4SLinus Torvalds 2320493a1724STejun Heo /* see whether any pwq is asking for help */ 23212e109a28STejun Heo spin_lock_irq(&wq_mayday_lock); 2322493a1724STejun Heo 2323493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2324493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2325493a1724STejun Heo struct pool_workqueue, mayday_node); 2326112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2327e22bee78STejun Heo struct work_struct *work, *n; 232882607adcSTejun Heo bool first = true; 2329e22bee78STejun Heo 2330e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2331493a1724STejun Heo list_del_init(&pwq->mayday_node); 2332493a1724STejun Heo 23332e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2334e22bee78STejun Heo 233551697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 233651697d39SLai Jiangshan 233751697d39SLai Jiangshan spin_lock_irq(&pool->lock); 2338b3104104SLai Jiangshan rescuer->pool = pool; 2339e22bee78STejun Heo 2340e22bee78STejun Heo /* 2341e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2342e22bee78STejun Heo * process'em. 2343e22bee78STejun Heo */ 23440479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 234582607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 234682607adcSTejun Heo if (get_work_pwq(work) == pwq) { 234782607adcSTejun Heo if (first) 234882607adcSTejun Heo pool->watchdog_ts = jiffies; 2349e22bee78STejun Heo move_linked_works(work, scheduled, &n); 235082607adcSTejun Heo } 235182607adcSTejun Heo first = false; 235282607adcSTejun Heo } 2353e22bee78STejun Heo 2354008847f6SNeilBrown if (!list_empty(scheduled)) { 2355e22bee78STejun Heo process_scheduled_works(rescuer); 23567576958aSTejun Heo 23577576958aSTejun Heo /* 2358008847f6SNeilBrown * The above execution of rescued work items could 2359008847f6SNeilBrown * have created more to rescue through 2360008847f6SNeilBrown * pwq_activate_first_delayed() or chained 2361008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2362008847f6SNeilBrown * that such back-to-back work items, which may be 2363008847f6SNeilBrown * being used to relieve memory pressure, don't 2364008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2365008847f6SNeilBrown */ 2366008847f6SNeilBrown if (need_to_create_worker(pool)) { 2367008847f6SNeilBrown spin_lock(&wq_mayday_lock); 2368008847f6SNeilBrown get_pwq(pwq); 2369008847f6SNeilBrown list_move_tail(&pwq->mayday_node, &wq->maydays); 2370008847f6SNeilBrown spin_unlock(&wq_mayday_lock); 2371008847f6SNeilBrown } 2372008847f6SNeilBrown } 2373008847f6SNeilBrown 2374008847f6SNeilBrown /* 237577668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 237613b1d625SLai Jiangshan * go away while we're still attached to it. 237777668c8bSLai Jiangshan */ 237877668c8bSLai Jiangshan put_pwq(pwq); 237977668c8bSLai Jiangshan 238077668c8bSLai Jiangshan /* 2381d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 23827576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23837576958aSTejun Heo * and stalling the execution. 23847576958aSTejun Heo */ 2385d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 238663d95a91STejun Heo wake_up_worker(pool); 23877576958aSTejun Heo 2388b3104104SLai Jiangshan rescuer->pool = NULL; 238913b1d625SLai Jiangshan spin_unlock_irq(&pool->lock); 239013b1d625SLai Jiangshan 239113b1d625SLai Jiangshan worker_detach_from_pool(rescuer, pool); 239213b1d625SLai Jiangshan 239313b1d625SLai Jiangshan spin_lock_irq(&wq_mayday_lock); 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds 23962e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2397493a1724STejun Heo 23984d595b86SLai Jiangshan if (should_stop) { 23994d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 24004d595b86SLai Jiangshan rescuer->task->flags &= ~PF_WQ_WORKER; 24014d595b86SLai Jiangshan return 0; 24024d595b86SLai Jiangshan } 24034d595b86SLai Jiangshan 2404111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2405111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2406e22bee78STejun Heo schedule(); 2407e22bee78STejun Heo goto repeat; 24081da177e4SLinus Torvalds } 24091da177e4SLinus Torvalds 2410fca839c0STejun Heo /** 2411fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2412fca839c0STejun Heo * @target_wq: workqueue being flushed 2413fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2414fca839c0STejun Heo * 2415fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2416fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2417fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2418fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2419fca839c0STejun Heo * a deadlock. 2420fca839c0STejun Heo */ 2421fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2422fca839c0STejun Heo struct work_struct *target_work) 2423fca839c0STejun Heo { 2424fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2425fca839c0STejun Heo struct worker *worker; 2426fca839c0STejun Heo 2427fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2428fca839c0STejun Heo return; 2429fca839c0STejun Heo 2430fca839c0STejun Heo worker = current_wq_worker(); 2431fca839c0STejun Heo 2432fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2433fca839c0STejun Heo "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%pf", 2434fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 243523d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 243623d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2437fca839c0STejun Heo "workqueue: WQ_MEM_RECLAIM %s:%pf is flushing !WQ_MEM_RECLAIM %s:%pf", 2438fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2439fca839c0STejun Heo target_wq->name, target_func); 2440fca839c0STejun Heo } 2441fca839c0STejun Heo 2442fc2e4d70SOleg Nesterov struct wq_barrier { 2443fc2e4d70SOleg Nesterov struct work_struct work; 2444fc2e4d70SOleg Nesterov struct completion done; 24452607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2446fc2e4d70SOleg Nesterov }; 2447fc2e4d70SOleg Nesterov 2448fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2449fc2e4d70SOleg Nesterov { 2450fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2451fc2e4d70SOleg Nesterov complete(&barr->done); 2452fc2e4d70SOleg Nesterov } 2453fc2e4d70SOleg Nesterov 24544690c4abSTejun Heo /** 24554690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2456112202d9STejun Heo * @pwq: pwq to insert barrier into 24574690c4abSTejun Heo * @barr: wq_barrier to insert 2458affee4b2STejun Heo * @target: target work to attach @barr to 2459affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 24604690c4abSTejun Heo * 2461affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2462affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2463affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2464affee4b2STejun Heo * cpu. 2465affee4b2STejun Heo * 2466affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2467affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2468affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2469affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2470affee4b2STejun Heo * after a work with LINKED flag set. 2471affee4b2STejun Heo * 2472affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2473112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 24744690c4abSTejun Heo * 24754690c4abSTejun Heo * CONTEXT: 2476d565ed63STejun Heo * spin_lock_irq(pool->lock). 24774690c4abSTejun Heo */ 2478112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2479affee4b2STejun Heo struct wq_barrier *barr, 2480affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2481fc2e4d70SOleg Nesterov { 2482affee4b2STejun Heo struct list_head *head; 2483affee4b2STejun Heo unsigned int linked = 0; 2484affee4b2STejun Heo 2485dc186ad7SThomas Gleixner /* 2486d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2487dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2488dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2489dc186ad7SThomas Gleixner * might deadlock. 2490dc186ad7SThomas Gleixner */ 2491ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 249222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 249352fa5bc5SBoqun Feng 2494fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2495fd1a5b04SByungchul Park 24962607d7a6STejun Heo barr->task = current; 249783c22520SOleg Nesterov 2498affee4b2STejun Heo /* 2499affee4b2STejun Heo * If @target is currently being executed, schedule the 2500affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2501affee4b2STejun Heo */ 2502affee4b2STejun Heo if (worker) 2503affee4b2STejun Heo head = worker->scheduled.next; 2504affee4b2STejun Heo else { 2505affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2506affee4b2STejun Heo 2507affee4b2STejun Heo head = target->entry.next; 2508affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2509affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2510affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2511affee4b2STejun Heo } 2512affee4b2STejun Heo 2513dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2514112202d9STejun Heo insert_work(pwq, &barr->work, head, 2515affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2516fc2e4d70SOleg Nesterov } 2517fc2e4d70SOleg Nesterov 251873f53c4aSTejun Heo /** 2519112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 252073f53c4aSTejun Heo * @wq: workqueue being flushed 252173f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 252273f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 252373f53c4aSTejun Heo * 2524112202d9STejun Heo * Prepare pwqs for workqueue flushing. 252573f53c4aSTejun Heo * 2526112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2527112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2528112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2529112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2530112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 253173f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 253273f53c4aSTejun Heo * 253373f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 253473f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 253573f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 253673f53c4aSTejun Heo * is returned. 253773f53c4aSTejun Heo * 2538112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 253973f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 254073f53c4aSTejun Heo * advanced to @work_color. 254173f53c4aSTejun Heo * 254273f53c4aSTejun Heo * CONTEXT: 25433c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 254473f53c4aSTejun Heo * 2545d185af30SYacine Belkadi * Return: 254673f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 254773f53c4aSTejun Heo * otherwise. 254873f53c4aSTejun Heo */ 2549112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 255073f53c4aSTejun Heo int flush_color, int work_color) 25511da177e4SLinus Torvalds { 255273f53c4aSTejun Heo bool wait = false; 255349e3cf44STejun Heo struct pool_workqueue *pwq; 25541da177e4SLinus Torvalds 255573f53c4aSTejun Heo if (flush_color >= 0) { 25566183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2557112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2558dc186ad7SThomas Gleixner } 255914441960SOleg Nesterov 256049e3cf44STejun Heo for_each_pwq(pwq, wq) { 2561112202d9STejun Heo struct worker_pool *pool = pwq->pool; 25621da177e4SLinus Torvalds 2563b09f4fd3SLai Jiangshan spin_lock_irq(&pool->lock); 256473f53c4aSTejun Heo 256573f53c4aSTejun Heo if (flush_color >= 0) { 25666183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 256773f53c4aSTejun Heo 2568112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2569112202d9STejun Heo pwq->flush_color = flush_color; 2570112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 257173f53c4aSTejun Heo wait = true; 25721da177e4SLinus Torvalds } 257373f53c4aSTejun Heo } 257473f53c4aSTejun Heo 257573f53c4aSTejun Heo if (work_color >= 0) { 25766183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2577112202d9STejun Heo pwq->work_color = work_color; 257873f53c4aSTejun Heo } 257973f53c4aSTejun Heo 2580b09f4fd3SLai Jiangshan spin_unlock_irq(&pool->lock); 25811da177e4SLinus Torvalds } 25821da177e4SLinus Torvalds 2583112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 258473f53c4aSTejun Heo complete(&wq->first_flusher->done); 258573f53c4aSTejun Heo 258673f53c4aSTejun Heo return wait; 258783c22520SOleg Nesterov } 25881da177e4SLinus Torvalds 25890fcb78c2SRolf Eike Beer /** 25901da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25910fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25921da177e4SLinus Torvalds * 2593c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2594c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 25951da177e4SLinus Torvalds */ 25967ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25971da177e4SLinus Torvalds { 259873f53c4aSTejun Heo struct wq_flusher this_flusher = { 259973f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 260073f53c4aSTejun Heo .flush_color = -1, 2601fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 260273f53c4aSTejun Heo }; 260373f53c4aSTejun Heo int next_color; 2604b1f4ec17SOleg Nesterov 26053347fa09STejun Heo if (WARN_ON(!wq_online)) 26063347fa09STejun Heo return; 26073347fa09STejun Heo 26083c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 260973f53c4aSTejun Heo 261073f53c4aSTejun Heo /* 261173f53c4aSTejun Heo * Start-to-wait phase 261273f53c4aSTejun Heo */ 261373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 261473f53c4aSTejun Heo 261573f53c4aSTejun Heo if (next_color != wq->flush_color) { 261673f53c4aSTejun Heo /* 261773f53c4aSTejun Heo * Color space is not full. The current work_color 261873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 261973f53c4aSTejun Heo * by one. 262073f53c4aSTejun Heo */ 26216183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 262273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 262373f53c4aSTejun Heo wq->work_color = next_color; 262473f53c4aSTejun Heo 262573f53c4aSTejun Heo if (!wq->first_flusher) { 262673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 26276183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 262873f53c4aSTejun Heo 262973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 263073f53c4aSTejun Heo 2631112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 263273f53c4aSTejun Heo wq->work_color)) { 263373f53c4aSTejun Heo /* nothing to flush, done */ 263473f53c4aSTejun Heo wq->flush_color = next_color; 263573f53c4aSTejun Heo wq->first_flusher = NULL; 263673f53c4aSTejun Heo goto out_unlock; 263773f53c4aSTejun Heo } 263873f53c4aSTejun Heo } else { 263973f53c4aSTejun Heo /* wait in queue */ 26406183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 264173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2642112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 264373f53c4aSTejun Heo } 264473f53c4aSTejun Heo } else { 264573f53c4aSTejun Heo /* 264673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 264773f53c4aSTejun Heo * The next flush completion will assign us 264873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 264973f53c4aSTejun Heo */ 265073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 265173f53c4aSTejun Heo } 265273f53c4aSTejun Heo 2653fca839c0STejun Heo check_flush_dependency(wq, NULL); 2654fca839c0STejun Heo 26553c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 265673f53c4aSTejun Heo 265773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 265873f53c4aSTejun Heo 265973f53c4aSTejun Heo /* 266073f53c4aSTejun Heo * Wake-up-and-cascade phase 266173f53c4aSTejun Heo * 266273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 266373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 266473f53c4aSTejun Heo */ 266573f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 266673f53c4aSTejun Heo return; 266773f53c4aSTejun Heo 26683c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 266973f53c4aSTejun Heo 26704ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26714ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26724ce48b37STejun Heo goto out_unlock; 26734ce48b37STejun Heo 267473f53c4aSTejun Heo wq->first_flusher = NULL; 267573f53c4aSTejun Heo 26766183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 26776183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 267873f53c4aSTejun Heo 267973f53c4aSTejun Heo while (true) { 268073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 268173f53c4aSTejun Heo 268273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 268373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 268473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 268573f53c4aSTejun Heo break; 268673f53c4aSTejun Heo list_del_init(&next->list); 268773f53c4aSTejun Heo complete(&next->done); 268873f53c4aSTejun Heo } 268973f53c4aSTejun Heo 26906183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 269173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 269273f53c4aSTejun Heo 269373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 269473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 269573f53c4aSTejun Heo 269673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 269773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 269873f53c4aSTejun Heo /* 269973f53c4aSTejun Heo * Assign the same color to all overflowed 270073f53c4aSTejun Heo * flushers, advance work_color and append to 270173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 270273f53c4aSTejun Heo * phase for these overflowed flushers. 270373f53c4aSTejun Heo */ 270473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 270573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 270673f53c4aSTejun Heo 270773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 270873f53c4aSTejun Heo 270973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 271073f53c4aSTejun Heo &wq->flusher_queue); 2711112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 271273f53c4aSTejun Heo } 271373f53c4aSTejun Heo 271473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 27156183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 271673f53c4aSTejun Heo break; 271773f53c4aSTejun Heo } 271873f53c4aSTejun Heo 271973f53c4aSTejun Heo /* 272073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2721112202d9STejun Heo * the new first flusher and arm pwqs. 272273f53c4aSTejun Heo */ 27236183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 27246183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 272573f53c4aSTejun Heo 272673f53c4aSTejun Heo list_del_init(&next->list); 272773f53c4aSTejun Heo wq->first_flusher = next; 272873f53c4aSTejun Heo 2729112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 273073f53c4aSTejun Heo break; 273173f53c4aSTejun Heo 273273f53c4aSTejun Heo /* 273373f53c4aSTejun Heo * Meh... this color is already done, clear first 273473f53c4aSTejun Heo * flusher and repeat cascading. 273573f53c4aSTejun Heo */ 273673f53c4aSTejun Heo wq->first_flusher = NULL; 273773f53c4aSTejun Heo } 273873f53c4aSTejun Heo 273973f53c4aSTejun Heo out_unlock: 27403c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 27411da177e4SLinus Torvalds } 27421dadafa8STim Gardner EXPORT_SYMBOL(flush_workqueue); 27431da177e4SLinus Torvalds 27449c5a2ba7STejun Heo /** 27459c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 27469c5a2ba7STejun Heo * @wq: workqueue to drain 27479c5a2ba7STejun Heo * 27489c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 27499c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 27509c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 2751b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 27529c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 27539c5a2ba7STejun Heo * takes too long. 27549c5a2ba7STejun Heo */ 27559c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 27569c5a2ba7STejun Heo { 27579c5a2ba7STejun Heo unsigned int flush_cnt = 0; 275849e3cf44STejun Heo struct pool_workqueue *pwq; 27599c5a2ba7STejun Heo 27609c5a2ba7STejun Heo /* 27619c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 27629c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 2763618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 27649c5a2ba7STejun Heo */ 276587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 27669c5a2ba7STejun Heo if (!wq->nr_drainers++) 2767618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 276887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 27699c5a2ba7STejun Heo reflush: 27709c5a2ba7STejun Heo flush_workqueue(wq); 27719c5a2ba7STejun Heo 2772b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 277376af4d93STejun Heo 277449e3cf44STejun Heo for_each_pwq(pwq, wq) { 2775fa2563e4SThomas Tuttle bool drained; 27769c5a2ba7STejun Heo 2777b09f4fd3SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 2778112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2779b09f4fd3SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 2780fa2563e4SThomas Tuttle 2781fa2563e4SThomas Tuttle if (drained) 27829c5a2ba7STejun Heo continue; 27839c5a2ba7STejun Heo 27849c5a2ba7STejun Heo if (++flush_cnt == 10 || 27859c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2786c5aa87bbSTejun Heo pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n", 27879c5a2ba7STejun Heo wq->name, flush_cnt); 278876af4d93STejun Heo 2789b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 27909c5a2ba7STejun Heo goto reflush; 27919c5a2ba7STejun Heo } 27929c5a2ba7STejun Heo 27939c5a2ba7STejun Heo if (!--wq->nr_drainers) 2794618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 279587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 27969c5a2ba7STejun Heo } 27979c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27989c5a2ba7STejun Heo 2799606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2800baf59022STejun Heo { 2801baf59022STejun Heo struct worker *worker = NULL; 2802c9e7cf27STejun Heo struct worker_pool *pool; 2803112202d9STejun Heo struct pool_workqueue *pwq; 2804baf59022STejun Heo 2805baf59022STejun Heo might_sleep(); 2806baf59022STejun Heo 2807fa1b54e6STejun Heo local_irq_disable(); 2808fa1b54e6STejun Heo pool = get_work_pool(work); 2809fa1b54e6STejun Heo if (!pool) { 2810fa1b54e6STejun Heo local_irq_enable(); 2811fa1b54e6STejun Heo return false; 2812fa1b54e6STejun Heo } 2813fa1b54e6STejun Heo 2814fa1b54e6STejun Heo spin_lock(&pool->lock); 28150b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2816112202d9STejun Heo pwq = get_work_pwq(work); 2817112202d9STejun Heo if (pwq) { 2818112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2819baf59022STejun Heo goto already_gone; 2820606a5020STejun Heo } else { 2821c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2822baf59022STejun Heo if (!worker) 2823baf59022STejun Heo goto already_gone; 2824112202d9STejun Heo pwq = worker->current_pwq; 2825606a5020STejun Heo } 2826baf59022STejun Heo 2827fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 2828fca839c0STejun Heo 2829112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2830d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2831baf59022STejun Heo 2832e159489bSTejun Heo /* 2833a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 2834a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 2835a1d14934SPeter Zijlstra * 2836a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 2837a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 2838a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 2839a1d14934SPeter Zijlstra * forward progress. 2840e159489bSTejun Heo */ 2841a1d14934SPeter Zijlstra if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer) { 2842112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2843112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2844a1d14934SPeter Zijlstra } 2845e159489bSTejun Heo 2846baf59022STejun Heo return true; 2847baf59022STejun Heo already_gone: 2848d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2849baf59022STejun Heo return false; 2850baf59022STejun Heo } 2851baf59022STejun Heo 2852db700897SOleg Nesterov /** 2853401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2854401a8d04STejun Heo * @work: the work to flush 2855db700897SOleg Nesterov * 2856606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2857606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2858401a8d04STejun Heo * 2859d185af30SYacine Belkadi * Return: 2860401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2861401a8d04STejun Heo * %false if it was already idle. 2862db700897SOleg Nesterov */ 2863401a8d04STejun Heo bool flush_work(struct work_struct *work) 2864db700897SOleg Nesterov { 286512997d1aSBjorn Helgaas struct wq_barrier barr; 286612997d1aSBjorn Helgaas 28673347fa09STejun Heo if (WARN_ON(!wq_online)) 28683347fa09STejun Heo return false; 28693347fa09STejun Heo 287012997d1aSBjorn Helgaas if (start_flush_work(work, &barr)) { 287112997d1aSBjorn Helgaas wait_for_completion(&barr.done); 287212997d1aSBjorn Helgaas destroy_work_on_stack(&barr.work); 287312997d1aSBjorn Helgaas return true; 287412997d1aSBjorn Helgaas } else { 287512997d1aSBjorn Helgaas return false; 287612997d1aSBjorn Helgaas } 2877606a5020STejun Heo } 2878db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2879db700897SOleg Nesterov 28808603e1b3STejun Heo struct cwt_wait { 2881ac6424b9SIngo Molnar wait_queue_entry_t wait; 28828603e1b3STejun Heo struct work_struct *work; 28838603e1b3STejun Heo }; 28848603e1b3STejun Heo 2885ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 28868603e1b3STejun Heo { 28878603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 28888603e1b3STejun Heo 28898603e1b3STejun Heo if (cwait->work != key) 28908603e1b3STejun Heo return 0; 28918603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 28928603e1b3STejun Heo } 28938603e1b3STejun Heo 289436e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2895401a8d04STejun Heo { 28968603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 2897bbb68dfaSTejun Heo unsigned long flags; 28981f1f642eSOleg Nesterov int ret; 28991f1f642eSOleg Nesterov 29001f1f642eSOleg Nesterov do { 2901bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2902bbb68dfaSTejun Heo /* 29038603e1b3STejun Heo * If someone else is already canceling, wait for it to 29048603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 29058603e1b3STejun Heo * because we may get scheduled between @work's completion 29068603e1b3STejun Heo * and the other canceling task resuming and clearing 29078603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 29088603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 29098603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 29108603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 29118603e1b3STejun Heo * we're hogging the CPU. 29128603e1b3STejun Heo * 29138603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 29148603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 29158603e1b3STejun Heo * wake function which matches @work along with exclusive 29168603e1b3STejun Heo * wait and wakeup. 2917bbb68dfaSTejun Heo */ 29188603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 29198603e1b3STejun Heo struct cwt_wait cwait; 29208603e1b3STejun Heo 29218603e1b3STejun Heo init_wait(&cwait.wait); 29228603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 29238603e1b3STejun Heo cwait.work = work; 29248603e1b3STejun Heo 29258603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 29268603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 29278603e1b3STejun Heo if (work_is_canceling(work)) 29288603e1b3STejun Heo schedule(); 29298603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 29308603e1b3STejun Heo } 29311f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 29321f1f642eSOleg Nesterov 2933bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2934bbb68dfaSTejun Heo mark_work_canceling(work); 2935bbb68dfaSTejun Heo local_irq_restore(flags); 2936bbb68dfaSTejun Heo 29373347fa09STejun Heo /* 29383347fa09STejun Heo * This allows canceling during early boot. We know that @work 29393347fa09STejun Heo * isn't executing. 29403347fa09STejun Heo */ 29413347fa09STejun Heo if (wq_online) 2942606a5020STejun Heo flush_work(work); 29433347fa09STejun Heo 29447a22ad75STejun Heo clear_work_data(work); 29458603e1b3STejun Heo 29468603e1b3STejun Heo /* 29478603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 29488603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 29498603e1b3STejun Heo * visible there. 29508603e1b3STejun Heo */ 29518603e1b3STejun Heo smp_mb(); 29528603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 29538603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 29548603e1b3STejun Heo 29551f1f642eSOleg Nesterov return ret; 29561f1f642eSOleg Nesterov } 29571f1f642eSOleg Nesterov 29586e84d644SOleg Nesterov /** 2959401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2960401a8d04STejun Heo * @work: the work to cancel 29616e84d644SOleg Nesterov * 2962401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2963401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2964401a8d04STejun Heo * another workqueue. On return from this function, @work is 2965401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 29661f1f642eSOleg Nesterov * 2967401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2968401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 29696e84d644SOleg Nesterov * 2970401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 29716e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2972401a8d04STejun Heo * 2973d185af30SYacine Belkadi * Return: 2974401a8d04STejun Heo * %true if @work was pending, %false otherwise. 29756e84d644SOleg Nesterov */ 2976401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 29776e84d644SOleg Nesterov { 297836e227d2STejun Heo return __cancel_work_timer(work, false); 2979b89deed3SOleg Nesterov } 298028e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2981b89deed3SOleg Nesterov 29826e84d644SOleg Nesterov /** 2983401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2984401a8d04STejun Heo * @dwork: the delayed work to flush 29856e84d644SOleg Nesterov * 2986401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2987401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2988401a8d04STejun Heo * considers the last queueing instance of @dwork. 29891f1f642eSOleg Nesterov * 2990d185af30SYacine Belkadi * Return: 2991401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2992401a8d04STejun Heo * %false if it was already idle. 29936e84d644SOleg Nesterov */ 2994401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2995401a8d04STejun Heo { 29968930cabaSTejun Heo local_irq_disable(); 2997401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 299860c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 29998930cabaSTejun Heo local_irq_enable(); 3000401a8d04STejun Heo return flush_work(&dwork->work); 3001401a8d04STejun Heo } 3002401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3003401a8d04STejun Heo 3004f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3005f72b8792SJens Axboe { 3006f72b8792SJens Axboe unsigned long flags; 3007f72b8792SJens Axboe int ret; 3008f72b8792SJens Axboe 3009f72b8792SJens Axboe do { 3010f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3011f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3012f72b8792SJens Axboe 3013f72b8792SJens Axboe if (unlikely(ret < 0)) 3014f72b8792SJens Axboe return false; 3015f72b8792SJens Axboe 3016f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3017f72b8792SJens Axboe local_irq_restore(flags); 3018f72b8792SJens Axboe return ret; 3019f72b8792SJens Axboe } 3020f72b8792SJens Axboe 3021f72b8792SJens Axboe /* 3022f72b8792SJens Axboe * See cancel_delayed_work() 3023f72b8792SJens Axboe */ 3024f72b8792SJens Axboe bool cancel_work(struct work_struct *work) 3025f72b8792SJens Axboe { 3026f72b8792SJens Axboe return __cancel_work(work, false); 3027f72b8792SJens Axboe } 3028f72b8792SJens Axboe 3029401a8d04STejun Heo /** 303057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 303157b30ae7STejun Heo * @dwork: delayed_work to cancel 303209383498STejun Heo * 3033d185af30SYacine Belkadi * Kill off a pending delayed_work. 3034d185af30SYacine Belkadi * 3035d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3036d185af30SYacine Belkadi * pending. 3037d185af30SYacine Belkadi * 3038d185af30SYacine Belkadi * Note: 3039d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3040d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3041d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 304209383498STejun Heo * 304357b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 304409383498STejun Heo */ 304557b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 304609383498STejun Heo { 3047f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 304809383498STejun Heo } 304957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 305009383498STejun Heo 305109383498STejun Heo /** 3052401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3053401a8d04STejun Heo * @dwork: the delayed work cancel 3054401a8d04STejun Heo * 3055401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3056401a8d04STejun Heo * 3057d185af30SYacine Belkadi * Return: 3058401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3059401a8d04STejun Heo */ 3060401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 30616e84d644SOleg Nesterov { 306236e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 30636e84d644SOleg Nesterov } 3064f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 30651da177e4SLinus Torvalds 30660fcb78c2SRolf Eike Beer /** 306731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3068b6136773SAndrew Morton * @func: the function to call 3069b6136773SAndrew Morton * 307031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 307131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3072b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 307331ddd871STejun Heo * 3074d185af30SYacine Belkadi * Return: 307531ddd871STejun Heo * 0 on success, -errno on failure. 3076b6136773SAndrew Morton */ 307765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 307815316ba8SChristoph Lameter { 307915316ba8SChristoph Lameter int cpu; 308038f51568SNamhyung Kim struct work_struct __percpu *works; 308115316ba8SChristoph Lameter 3082b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3083b6136773SAndrew Morton if (!works) 308415316ba8SChristoph Lameter return -ENOMEM; 3085b6136773SAndrew Morton 308695402b38SGautham R Shenoy get_online_cpus(); 308793981800STejun Heo 308815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30899bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30909bfb1839SIngo Molnar 30919bfb1839SIngo Molnar INIT_WORK(work, func); 30928de6d308SOleg Nesterov schedule_work_on(cpu, work); 309315316ba8SChristoph Lameter } 309493981800STejun Heo 309593981800STejun Heo for_each_online_cpu(cpu) 30968616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 309793981800STejun Heo 309895402b38SGautham R Shenoy put_online_cpus(); 3099b6136773SAndrew Morton free_percpu(works); 310015316ba8SChristoph Lameter return 0; 310115316ba8SChristoph Lameter } 310215316ba8SChristoph Lameter 3103eef6a7d5SAlan Stern /** 31041fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 31051fa44ecaSJames Bottomley * @fn: the function to execute 31061fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 31071fa44ecaSJames Bottomley * be available when the work executes) 31081fa44ecaSJames Bottomley * 31091fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 31101fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 31111fa44ecaSJames Bottomley * 3112d185af30SYacine Belkadi * Return: 0 - function was executed 31131fa44ecaSJames Bottomley * 1 - function was scheduled for execution 31141fa44ecaSJames Bottomley */ 311565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 31161fa44ecaSJames Bottomley { 31171fa44ecaSJames Bottomley if (!in_interrupt()) { 311865f27f38SDavid Howells fn(&ew->work); 31191fa44ecaSJames Bottomley return 0; 31201fa44ecaSJames Bottomley } 31211fa44ecaSJames Bottomley 312265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 31231fa44ecaSJames Bottomley schedule_work(&ew->work); 31241fa44ecaSJames Bottomley 31251fa44ecaSJames Bottomley return 1; 31261fa44ecaSJames Bottomley } 31271fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 31281fa44ecaSJames Bottomley 31297a4e344cSTejun Heo /** 31307a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 31317a4e344cSTejun Heo * @attrs: workqueue_attrs to free 31327a4e344cSTejun Heo * 31337a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 31347a4e344cSTejun Heo */ 31357a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs) 31367a4e344cSTejun Heo { 31377a4e344cSTejun Heo if (attrs) { 31387a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 31397a4e344cSTejun Heo kfree(attrs); 31407a4e344cSTejun Heo } 31417a4e344cSTejun Heo } 31427a4e344cSTejun Heo 31437a4e344cSTejun Heo /** 31447a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 31457a4e344cSTejun Heo * @gfp_mask: allocation mask to use 31467a4e344cSTejun Heo * 31477a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3148d185af30SYacine Belkadi * return it. 3149d185af30SYacine Belkadi * 3150d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 31517a4e344cSTejun Heo */ 31527a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) 31537a4e344cSTejun Heo { 31547a4e344cSTejun Heo struct workqueue_attrs *attrs; 31557a4e344cSTejun Heo 31567a4e344cSTejun Heo attrs = kzalloc(sizeof(*attrs), gfp_mask); 31577a4e344cSTejun Heo if (!attrs) 31587a4e344cSTejun Heo goto fail; 31597a4e344cSTejun Heo if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) 31607a4e344cSTejun Heo goto fail; 31617a4e344cSTejun Heo 316213e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 31637a4e344cSTejun Heo return attrs; 31647a4e344cSTejun Heo fail: 31657a4e344cSTejun Heo free_workqueue_attrs(attrs); 31667a4e344cSTejun Heo return NULL; 31677a4e344cSTejun Heo } 31687a4e344cSTejun Heo 316929c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 317029c91e99STejun Heo const struct workqueue_attrs *from) 317129c91e99STejun Heo { 317229c91e99STejun Heo to->nice = from->nice; 317329c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 31742865a8fbSShaohua Li /* 31752865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 31762865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 31772865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 31782865a8fbSShaohua Li */ 31792865a8fbSShaohua Li to->no_numa = from->no_numa; 318029c91e99STejun Heo } 318129c91e99STejun Heo 318229c91e99STejun Heo /* hash value of the content of @attr */ 318329c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 318429c91e99STejun Heo { 318529c91e99STejun Heo u32 hash = 0; 318629c91e99STejun Heo 318729c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 318813e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 318913e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 319029c91e99STejun Heo return hash; 319129c91e99STejun Heo } 319229c91e99STejun Heo 319329c91e99STejun Heo /* content equality test */ 319429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 319529c91e99STejun Heo const struct workqueue_attrs *b) 319629c91e99STejun Heo { 319729c91e99STejun Heo if (a->nice != b->nice) 319829c91e99STejun Heo return false; 319929c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 320029c91e99STejun Heo return false; 320129c91e99STejun Heo return true; 320229c91e99STejun Heo } 320329c91e99STejun Heo 32047a4e344cSTejun Heo /** 32057a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 32067a4e344cSTejun Heo * @pool: worker_pool to initialize 32077a4e344cSTejun Heo * 3208402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3209d185af30SYacine Belkadi * 3210d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 321129c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 321229c91e99STejun Heo * on @pool safely to release it. 32137a4e344cSTejun Heo */ 32147a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 32154e1a1f9aSTejun Heo { 32164e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 321729c91e99STejun Heo pool->id = -1; 321829c91e99STejun Heo pool->cpu = -1; 3219f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 32204e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 322182607adcSTejun Heo pool->watchdog_ts = jiffies; 32224e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 32234e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 32244e1a1f9aSTejun Heo hash_init(pool->busy_hash); 32254e1a1f9aSTejun Heo 322632a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 32274e1a1f9aSTejun Heo 322832a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 32294e1a1f9aSTejun Heo 323092f9c5c4SLai Jiangshan mutex_init(&pool->attach_mutex); 3231da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 32327a4e344cSTejun Heo 32337cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 323429c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 323529c91e99STejun Heo pool->refcnt = 1; 323629c91e99STejun Heo 323729c91e99STejun Heo /* shouldn't fail above this point */ 32387a4e344cSTejun Heo pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); 32397a4e344cSTejun Heo if (!pool->attrs) 32407a4e344cSTejun Heo return -ENOMEM; 32417a4e344cSTejun Heo return 0; 32424e1a1f9aSTejun Heo } 32434e1a1f9aSTejun Heo 3244e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3245e2dca7adSTejun Heo { 3246e2dca7adSTejun Heo struct workqueue_struct *wq = 3247e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3248e2dca7adSTejun Heo 3249e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3250e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3251e2dca7adSTejun Heo else 3252e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3253e2dca7adSTejun Heo 3254e2dca7adSTejun Heo kfree(wq->rescuer); 3255e2dca7adSTejun Heo kfree(wq); 3256e2dca7adSTejun Heo } 3257e2dca7adSTejun Heo 325829c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 325929c91e99STejun Heo { 326029c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 326129c91e99STejun Heo 32627cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 326329c91e99STejun Heo free_workqueue_attrs(pool->attrs); 326429c91e99STejun Heo kfree(pool); 326529c91e99STejun Heo } 326629c91e99STejun Heo 326729c91e99STejun Heo /** 326829c91e99STejun Heo * put_unbound_pool - put a worker_pool 326929c91e99STejun Heo * @pool: worker_pool to put 327029c91e99STejun Heo * 327129c91e99STejun Heo * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU 3272c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3273c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3274c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3275a892caccSTejun Heo * 3276a892caccSTejun Heo * Should be called with wq_pool_mutex held. 327729c91e99STejun Heo */ 327829c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 327929c91e99STejun Heo { 328060f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 328129c91e99STejun Heo struct worker *worker; 328229c91e99STejun Heo 3283a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3284a892caccSTejun Heo 3285a892caccSTejun Heo if (--pool->refcnt) 328629c91e99STejun Heo return; 328729c91e99STejun Heo 328829c91e99STejun Heo /* sanity checks */ 328961d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3290a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 329129c91e99STejun Heo return; 329229c91e99STejun Heo 329329c91e99STejun Heo /* release id and unhash */ 329429c91e99STejun Heo if (pool->id >= 0) 329529c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 329629c91e99STejun Heo hash_del(&pool->hash_node); 329729c91e99STejun Heo 3298c5aa87bbSTejun Heo /* 3299692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3300692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3301692b4825STejun Heo * manager and @pool gets freed with the flag set. 3302c5aa87bbSTejun Heo */ 330360f5a4bcSLai Jiangshan spin_lock_irq(&pool->lock); 3304692b4825STejun Heo wait_event_lock_irq(wq_manager_wait, 3305692b4825STejun Heo !(pool->flags & POOL_MANAGER_ACTIVE), pool->lock); 3306692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 3307692b4825STejun Heo 33081037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 330929c91e99STejun Heo destroy_worker(worker); 331029c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 331129c91e99STejun Heo spin_unlock_irq(&pool->lock); 331260f5a4bcSLai Jiangshan 331392f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 3314da028469SLai Jiangshan if (!list_empty(&pool->workers)) 331560f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 331692f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 331760f5a4bcSLai Jiangshan 331860f5a4bcSLai Jiangshan if (pool->detach_completion) 331960f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 332060f5a4bcSLai Jiangshan 332129c91e99STejun Heo /* shut down the timers */ 332229c91e99STejun Heo del_timer_sync(&pool->idle_timer); 332329c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 332429c91e99STejun Heo 332529c91e99STejun Heo /* sched-RCU protected to allow dereferences from get_work_pool() */ 332629c91e99STejun Heo call_rcu_sched(&pool->rcu, rcu_free_pool); 332729c91e99STejun Heo } 332829c91e99STejun Heo 332929c91e99STejun Heo /** 333029c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 333129c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 333229c91e99STejun Heo * 333329c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 333429c91e99STejun Heo * reference count and return it. If there already is a matching 333529c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3336d185af30SYacine Belkadi * create a new one. 3337a892caccSTejun Heo * 3338a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3339d185af30SYacine Belkadi * 3340d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3341d185af30SYacine Belkadi * On failure, %NULL. 334229c91e99STejun Heo */ 334329c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 334429c91e99STejun Heo { 334529c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 334629c91e99STejun Heo struct worker_pool *pool; 3347f3f90ad4STejun Heo int node; 3348e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 334929c91e99STejun Heo 3350a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 335129c91e99STejun Heo 335229c91e99STejun Heo /* do we already have a matching pool? */ 335329c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 335429c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 335529c91e99STejun Heo pool->refcnt++; 33563fb1823cSLai Jiangshan return pool; 335729c91e99STejun Heo } 335829c91e99STejun Heo } 335929c91e99STejun Heo 3360e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3361e2273584SXunlei Pang if (wq_numa_enabled) { 3362e2273584SXunlei Pang for_each_node(node) { 3363e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3364e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3365e2273584SXunlei Pang target_node = node; 3366e2273584SXunlei Pang break; 3367e2273584SXunlei Pang } 3368e2273584SXunlei Pang } 3369e2273584SXunlei Pang } 3370e2273584SXunlei Pang 337129c91e99STejun Heo /* nope, create a new one */ 3372e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 337329c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 337429c91e99STejun Heo goto fail; 337529c91e99STejun Heo 33768864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 337729c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3378e2273584SXunlei Pang pool->node = target_node; 337929c91e99STejun Heo 33802865a8fbSShaohua Li /* 33812865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 33822865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 33832865a8fbSShaohua Li */ 33842865a8fbSShaohua Li pool->attrs->no_numa = false; 33852865a8fbSShaohua Li 338629c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 338729c91e99STejun Heo goto fail; 338829c91e99STejun Heo 338929c91e99STejun Heo /* create and start the initial worker */ 33903347fa09STejun Heo if (wq_online && !create_worker(pool)) 339129c91e99STejun Heo goto fail; 339229c91e99STejun Heo 339329c91e99STejun Heo /* install */ 339429c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 33953fb1823cSLai Jiangshan 339629c91e99STejun Heo return pool; 339729c91e99STejun Heo fail: 339829c91e99STejun Heo if (pool) 339929c91e99STejun Heo put_unbound_pool(pool); 340029c91e99STejun Heo return NULL; 340129c91e99STejun Heo } 340229c91e99STejun Heo 34038864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 34048864b4e5STejun Heo { 34058864b4e5STejun Heo kmem_cache_free(pwq_cache, 34068864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 34078864b4e5STejun Heo } 34088864b4e5STejun Heo 34098864b4e5STejun Heo /* 34108864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 34118864b4e5STejun Heo * and needs to be destroyed. 34128864b4e5STejun Heo */ 34138864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 34148864b4e5STejun Heo { 34158864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 34168864b4e5STejun Heo unbound_release_work); 34178864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 34188864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3419bc0caf09STejun Heo bool is_last; 34208864b4e5STejun Heo 34218864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 34228864b4e5STejun Heo return; 34238864b4e5STejun Heo 34243c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 34258864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3426bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 34273c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 34288864b4e5STejun Heo 3429a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 34308864b4e5STejun Heo put_unbound_pool(pool); 3431a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3432a892caccSTejun Heo 34338864b4e5STejun Heo call_rcu_sched(&pwq->rcu, rcu_free_pwq); 34348864b4e5STejun Heo 34358864b4e5STejun Heo /* 34368864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3437e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 34388864b4e5STejun Heo */ 3439e2dca7adSTejun Heo if (is_last) 3440e2dca7adSTejun Heo call_rcu_sched(&wq->rcu, rcu_free_wq); 34416029a918STejun Heo } 34428864b4e5STejun Heo 34430fbd95aaSTejun Heo /** 3444699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 34450fbd95aaSTejun Heo * @pwq: target pool_workqueue 34460fbd95aaSTejun Heo * 3447699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3448699ce097STejun Heo * workqueue's saved_max_active and activate delayed work items 3449699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 34500fbd95aaSTejun Heo */ 3451699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 34520fbd95aaSTejun Heo { 3453699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3454699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 34553347fa09STejun Heo unsigned long flags; 3456699ce097STejun Heo 3457699ce097STejun Heo /* for @wq->saved_max_active */ 3458a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3459699ce097STejun Heo 3460699ce097STejun Heo /* fast exit for non-freezable wqs */ 3461699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3462699ce097STejun Heo return; 3463699ce097STejun Heo 34643347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 34653347fa09STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 3466699ce097STejun Heo 346774b414eaSLai Jiangshan /* 346874b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 346974b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 347074b414eaSLai Jiangshan * is updated and visible. 347174b414eaSLai Jiangshan */ 347274b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 3473699ce097STejun Heo pwq->max_active = wq->saved_max_active; 34740fbd95aaSTejun Heo 34750fbd95aaSTejun Heo while (!list_empty(&pwq->delayed_works) && 34760fbd95aaSTejun Heo pwq->nr_active < pwq->max_active) 34770fbd95aaSTejun Heo pwq_activate_first_delayed(pwq); 3478951a078aSLai Jiangshan 3479951a078aSLai Jiangshan /* 3480951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 3481951a078aSLai Jiangshan * max_active is bumped. It's a slow path. Do it always. 3482951a078aSLai Jiangshan */ 3483951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3484699ce097STejun Heo } else { 3485699ce097STejun Heo pwq->max_active = 0; 3486699ce097STejun Heo } 3487699ce097STejun Heo 34883347fa09STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 34890fbd95aaSTejun Heo } 34900fbd95aaSTejun Heo 3491e50aba9aSTejun Heo /* initialize newly alloced @pwq which is associated with @wq and @pool */ 3492f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3493f147f29eSTejun Heo struct worker_pool *pool) 3494d2c1d404STejun Heo { 3495d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3496d2c1d404STejun Heo 3497e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3498e50aba9aSTejun Heo 3499d2c1d404STejun Heo pwq->pool = pool; 3500d2c1d404STejun Heo pwq->wq = wq; 3501d2c1d404STejun Heo pwq->flush_color = -1; 35028864b4e5STejun Heo pwq->refcnt = 1; 3503d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 35041befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3505d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 35068864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3507f147f29eSTejun Heo } 3508d2c1d404STejun Heo 3509f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 35101befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3511f147f29eSTejun Heo { 3512f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3513f147f29eSTejun Heo 3514f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 351575ccf595STejun Heo 35161befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 35171befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 35181befcf30STejun Heo return; 35191befcf30STejun Heo 352029b1cb41SLai Jiangshan /* set the matching work_color */ 352175ccf595STejun Heo pwq->work_color = wq->work_color; 3522983ca25eSTejun Heo 3523983ca25eSTejun Heo /* sync max_active to the current setting */ 3524983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3525983ca25eSTejun Heo 3526983ca25eSTejun Heo /* link in @pwq */ 35279e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3528df2d5ae4STejun Heo } 35296029a918STejun Heo 3530f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3531f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3532f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3533f147f29eSTejun Heo { 3534f147f29eSTejun Heo struct worker_pool *pool; 3535f147f29eSTejun Heo struct pool_workqueue *pwq; 3536f147f29eSTejun Heo 3537f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3538f147f29eSTejun Heo 3539f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3540f147f29eSTejun Heo if (!pool) 3541f147f29eSTejun Heo return NULL; 3542f147f29eSTejun Heo 3543e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3544f147f29eSTejun Heo if (!pwq) { 3545f147f29eSTejun Heo put_unbound_pool(pool); 3546f147f29eSTejun Heo return NULL; 3547f147f29eSTejun Heo } 3548f147f29eSTejun Heo 3549f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3550f147f29eSTejun Heo return pwq; 3551d2c1d404STejun Heo } 3552d2c1d404STejun Heo 35534c16bd32STejun Heo /** 355430186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 3555042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 35564c16bd32STejun Heo * @node: the target NUMA node 35574c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 35584c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 35594c16bd32STejun Heo * 35604c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 35614c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3562d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 35634c16bd32STejun Heo * 35644c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 35654c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 35664c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 35674c16bd32STejun Heo * @attrs->cpumask. 35684c16bd32STejun Heo * 35694c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 35704c16bd32STejun Heo * stable. 3571d185af30SYacine Belkadi * 3572d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 3573d185af30SYacine Belkadi * %false if equal. 35744c16bd32STejun Heo */ 35754c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 35764c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 35774c16bd32STejun Heo { 3578d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 35794c16bd32STejun Heo goto use_dfl; 35804c16bd32STejun Heo 35814c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 35824c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 35834c16bd32STejun Heo if (cpu_going_down >= 0) 35844c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 35854c16bd32STejun Heo 35864c16bd32STejun Heo if (cpumask_empty(cpumask)) 35874c16bd32STejun Heo goto use_dfl; 35884c16bd32STejun Heo 35894c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 35904c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 35911ad0f0a7SMichael Bringmann 35921ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 35931ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 35941ad0f0a7SMichael Bringmann "possible intersect\n"); 35951ad0f0a7SMichael Bringmann return false; 35961ad0f0a7SMichael Bringmann } 35971ad0f0a7SMichael Bringmann 35984c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 35994c16bd32STejun Heo 36004c16bd32STejun Heo use_dfl: 36014c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 36024c16bd32STejun Heo return false; 36034c16bd32STejun Heo } 36044c16bd32STejun Heo 36051befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 36061befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 36071befcf30STejun Heo int node, 36081befcf30STejun Heo struct pool_workqueue *pwq) 36091befcf30STejun Heo { 36101befcf30STejun Heo struct pool_workqueue *old_pwq; 36111befcf30STejun Heo 36125b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 36131befcf30STejun Heo lockdep_assert_held(&wq->mutex); 36141befcf30STejun Heo 36151befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 36161befcf30STejun Heo link_pwq(pwq); 36171befcf30STejun Heo 36181befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 36191befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 36201befcf30STejun Heo return old_pwq; 36211befcf30STejun Heo } 36221befcf30STejun Heo 36232d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 36242d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 36252d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 36262d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 3627042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 36282d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 36292d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 36302d5f0764SLai Jiangshan }; 36312d5f0764SLai Jiangshan 36322d5f0764SLai Jiangshan /* free the resources after success or abort */ 36332d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 36342d5f0764SLai Jiangshan { 36352d5f0764SLai Jiangshan if (ctx) { 36362d5f0764SLai Jiangshan int node; 36372d5f0764SLai Jiangshan 36382d5f0764SLai Jiangshan for_each_node(node) 36392d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 36402d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 36412d5f0764SLai Jiangshan 36422d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 36432d5f0764SLai Jiangshan 36442d5f0764SLai Jiangshan kfree(ctx); 36452d5f0764SLai Jiangshan } 36462d5f0764SLai Jiangshan } 36472d5f0764SLai Jiangshan 36482d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 36492d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 36502d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 36512d5f0764SLai Jiangshan const struct workqueue_attrs *attrs) 36522d5f0764SLai Jiangshan { 36532d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 36542d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 36552d5f0764SLai Jiangshan int node; 36562d5f0764SLai Jiangshan 36572d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 36582d5f0764SLai Jiangshan 36592d5f0764SLai Jiangshan ctx = kzalloc(sizeof(*ctx) + nr_node_ids * sizeof(ctx->pwq_tbl[0]), 36602d5f0764SLai Jiangshan GFP_KERNEL); 36612d5f0764SLai Jiangshan 36622d5f0764SLai Jiangshan new_attrs = alloc_workqueue_attrs(GFP_KERNEL); 36632d5f0764SLai Jiangshan tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL); 36642d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 36652d5f0764SLai Jiangshan goto out_free; 36662d5f0764SLai Jiangshan 3667042f7df1SLai Jiangshan /* 3668042f7df1SLai Jiangshan * Calculate the attrs of the default pwq. 3669042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 3670042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 3671042f7df1SLai Jiangshan */ 36722d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3673b05a7928SFrederic Weisbecker cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); 3674042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 3675042f7df1SLai Jiangshan cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); 36762d5f0764SLai Jiangshan 36772d5f0764SLai Jiangshan /* 36782d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 36792d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 36802d5f0764SLai Jiangshan * pools. 36812d5f0764SLai Jiangshan */ 36822d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 36832d5f0764SLai Jiangshan 36842d5f0764SLai Jiangshan /* 36852d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 36862d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 36872d5f0764SLai Jiangshan * it even if we don't use it immediately. 36882d5f0764SLai Jiangshan */ 36892d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 36902d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 36912d5f0764SLai Jiangshan goto out_free; 36922d5f0764SLai Jiangshan 36932d5f0764SLai Jiangshan for_each_node(node) { 3694042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 36952d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 36962d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 36972d5f0764SLai Jiangshan goto out_free; 36982d5f0764SLai Jiangshan } else { 36992d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 37002d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 37012d5f0764SLai Jiangshan } 37022d5f0764SLai Jiangshan } 37032d5f0764SLai Jiangshan 3704042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 3705042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3706042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 37072d5f0764SLai Jiangshan ctx->attrs = new_attrs; 3708042f7df1SLai Jiangshan 37092d5f0764SLai Jiangshan ctx->wq = wq; 37102d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 37112d5f0764SLai Jiangshan return ctx; 37122d5f0764SLai Jiangshan 37132d5f0764SLai Jiangshan out_free: 37142d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 37152d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 37162d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 37172d5f0764SLai Jiangshan return NULL; 37182d5f0764SLai Jiangshan } 37192d5f0764SLai Jiangshan 37202d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 37212d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 37222d5f0764SLai Jiangshan { 37232d5f0764SLai Jiangshan int node; 37242d5f0764SLai Jiangshan 37252d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 37262d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 37272d5f0764SLai Jiangshan 37282d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 37292d5f0764SLai Jiangshan 37302d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 37312d5f0764SLai Jiangshan for_each_node(node) 37322d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 37332d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 37342d5f0764SLai Jiangshan 37352d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 37362d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 37372d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 37382d5f0764SLai Jiangshan 37392d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 37402d5f0764SLai Jiangshan } 37412d5f0764SLai Jiangshan 3742a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 3743a0111cf6SLai Jiangshan { 3744a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 3745a0111cf6SLai Jiangshan get_online_cpus(); 3746a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 3747a0111cf6SLai Jiangshan } 3748a0111cf6SLai Jiangshan 3749a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 3750a0111cf6SLai Jiangshan { 3751a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 3752a0111cf6SLai Jiangshan put_online_cpus(); 3753a0111cf6SLai Jiangshan } 3754a0111cf6SLai Jiangshan 3755a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 3756a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 3757a0111cf6SLai Jiangshan { 3758a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 3759a0111cf6SLai Jiangshan 3760a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 3761a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 3762a0111cf6SLai Jiangshan return -EINVAL; 3763a0111cf6SLai Jiangshan 3764a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 37650a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 37660a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 3767a0111cf6SLai Jiangshan return -EINVAL; 3768a0111cf6SLai Jiangshan 37690a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 37700a94efb5STejun Heo } 37710a94efb5STejun Heo 3772a0111cf6SLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs); 37736201171eSwanghaibin if (!ctx) 37746201171eSwanghaibin return -ENOMEM; 3775a0111cf6SLai Jiangshan 3776a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 3777a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 3778a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 3779a0111cf6SLai Jiangshan 37806201171eSwanghaibin return 0; 3781a0111cf6SLai Jiangshan } 3782a0111cf6SLai Jiangshan 37839e8cd2f5STejun Heo /** 37849e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 37859e8cd2f5STejun Heo * @wq: the target workqueue 37869e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 37879e8cd2f5STejun Heo * 37884c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 37894c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 37904c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 37914c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 37924c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 37934c16bd32STejun Heo * back-to-back will stay on its current pwq. 37949e8cd2f5STejun Heo * 3795d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 3796d185af30SYacine Belkadi * 3797d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 37989e8cd2f5STejun Heo */ 37999e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 38009e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 38019e8cd2f5STejun Heo { 3802a0111cf6SLai Jiangshan int ret; 38039e8cd2f5STejun Heo 3804a0111cf6SLai Jiangshan apply_wqattrs_lock(); 3805a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 3806a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 38072d5f0764SLai Jiangshan 38082d5f0764SLai Jiangshan return ret; 38099e8cd2f5STejun Heo } 38109e8cd2f5STejun Heo 38114c16bd32STejun Heo /** 38124c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 38134c16bd32STejun Heo * @wq: the target workqueue 38144c16bd32STejun Heo * @cpu: the CPU coming up or going down 38154c16bd32STejun Heo * @online: whether @cpu is coming up or going down 38164c16bd32STejun Heo * 38174c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 38184c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 38194c16bd32STejun Heo * @wq accordingly. 38204c16bd32STejun Heo * 38214c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 38224c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 38234c16bd32STejun Heo * correct. 38244c16bd32STejun Heo * 38254c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 38264c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 38274c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 38284c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 38294c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 38304c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 38314c16bd32STejun Heo * CPU_DOWN_PREPARE. 38324c16bd32STejun Heo */ 38334c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 38344c16bd32STejun Heo bool online) 38354c16bd32STejun Heo { 38364c16bd32STejun Heo int node = cpu_to_node(cpu); 38374c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 38384c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 38394c16bd32STejun Heo struct workqueue_attrs *target_attrs; 38404c16bd32STejun Heo cpumask_t *cpumask; 38414c16bd32STejun Heo 38424c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 38434c16bd32STejun Heo 3844f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 3845f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 38464c16bd32STejun Heo return; 38474c16bd32STejun Heo 38484c16bd32STejun Heo /* 38494c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 38504c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 38514c16bd32STejun Heo * CPU hotplug exclusion. 38524c16bd32STejun Heo */ 38534c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 38544c16bd32STejun Heo cpumask = target_attrs->cpumask; 38554c16bd32STejun Heo 38564c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 38574c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 38584c16bd32STejun Heo 38594c16bd32STejun Heo /* 38604c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 3861042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 3862042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 3863042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 38644c16bd32STejun Heo */ 3865042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 38664c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 3867f7142ed4SLai Jiangshan return; 38684c16bd32STejun Heo } else { 38694c16bd32STejun Heo goto use_dfl_pwq; 38704c16bd32STejun Heo } 38714c16bd32STejun Heo 38724c16bd32STejun Heo /* create a new pwq */ 38734c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 38744c16bd32STejun Heo if (!pwq) { 38752d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 38764c16bd32STejun Heo wq->name); 387777f300b1SDaeseok Youn goto use_dfl_pwq; 38784c16bd32STejun Heo } 38794c16bd32STejun Heo 3880f7142ed4SLai Jiangshan /* Install the new pwq. */ 38814c16bd32STejun Heo mutex_lock(&wq->mutex); 38824c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 38834c16bd32STejun Heo goto out_unlock; 38844c16bd32STejun Heo 38854c16bd32STejun Heo use_dfl_pwq: 3886f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 38874c16bd32STejun Heo spin_lock_irq(&wq->dfl_pwq->pool->lock); 38884c16bd32STejun Heo get_pwq(wq->dfl_pwq); 38894c16bd32STejun Heo spin_unlock_irq(&wq->dfl_pwq->pool->lock); 38904c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 38914c16bd32STejun Heo out_unlock: 38924c16bd32STejun Heo mutex_unlock(&wq->mutex); 38934c16bd32STejun Heo put_pwq_unlocked(old_pwq); 38944c16bd32STejun Heo } 38954c16bd32STejun Heo 389630cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 38971da177e4SLinus Torvalds { 389849e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 38998a2b7538STejun Heo int cpu, ret; 3900e1d8aa9fSFrederic Weisbecker 390130cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3902420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3903420c0ddbSTejun Heo if (!wq->cpu_pwqs) 390430cdf249STejun Heo return -ENOMEM; 390530cdf249STejun Heo 390630cdf249STejun Heo for_each_possible_cpu(cpu) { 39077fb98ea7STejun Heo struct pool_workqueue *pwq = 39087fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 39097a62c2c8STejun Heo struct worker_pool *cpu_pools = 3910f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 391130cdf249STejun Heo 3912f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 3913f147f29eSTejun Heo 3914f147f29eSTejun Heo mutex_lock(&wq->mutex); 39151befcf30STejun Heo link_pwq(pwq); 3916f147f29eSTejun Heo mutex_unlock(&wq->mutex); 391730cdf249STejun Heo } 391830cdf249STejun Heo return 0; 39198a2b7538STejun Heo } else if (wq->flags & __WQ_ORDERED) { 39208a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 39218a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 39228a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 39238a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 39248a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 39258a2b7538STejun Heo return ret; 39269e8cd2f5STejun Heo } else { 39279e8cd2f5STejun Heo return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 39289e8cd2f5STejun Heo } 39290f900049STejun Heo } 39300f900049STejun Heo 3931f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3932f3421797STejun Heo const char *name) 3933b71ab8c2STejun Heo { 3934f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3935f3421797STejun Heo 3936f3421797STejun Heo if (max_active < 1 || max_active > lim) 3937044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3938f3421797STejun Heo max_active, name, 1, lim); 3939b71ab8c2STejun Heo 3940f3421797STejun Heo return clamp_val(max_active, 1, lim); 3941b71ab8c2STejun Heo } 3942b71ab8c2STejun Heo 3943b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 394497e37d7bSTejun Heo unsigned int flags, 39451e19ffc6STejun Heo int max_active, 3946eb13ba87SJohannes Berg struct lock_class_key *key, 3947b196be89STejun Heo const char *lock_name, ...) 39483af24433SOleg Nesterov { 3949df2d5ae4STejun Heo size_t tbl_size = 0; 3950ecf6881fSTejun Heo va_list args; 39513af24433SOleg Nesterov struct workqueue_struct *wq; 395249e3cf44STejun Heo struct pool_workqueue *pwq; 3953b196be89STejun Heo 39545c0338c6STejun Heo /* 39555c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 39565c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 39575c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 39585c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 39595c0338c6STejun Heo * on NUMA. 39605c0338c6STejun Heo */ 39615c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 39625c0338c6STejun Heo flags |= __WQ_ORDERED; 39635c0338c6STejun Heo 3964cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 3965cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 3966cee22a15SViresh Kumar flags |= WQ_UNBOUND; 3967cee22a15SViresh Kumar 3968ecf6881fSTejun Heo /* allocate wq and format name */ 3969df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 3970ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 3971df2d5ae4STejun Heo 3972df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 3973b196be89STejun Heo if (!wq) 3974d2c1d404STejun Heo return NULL; 3975b196be89STejun Heo 39766029a918STejun Heo if (flags & WQ_UNBOUND) { 39776029a918STejun Heo wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL); 39786029a918STejun Heo if (!wq->unbound_attrs) 39796029a918STejun Heo goto err_free_wq; 39806029a918STejun Heo } 39816029a918STejun Heo 3982ecf6881fSTejun Heo va_start(args, lock_name); 3983ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 3984b196be89STejun Heo va_end(args); 39853af24433SOleg Nesterov 3986d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3987b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 39883af24433SOleg Nesterov 3989b196be89STejun Heo /* init wq */ 399097e37d7bSTejun Heo wq->flags = flags; 3991a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 39923c25a55dSLai Jiangshan mutex_init(&wq->mutex); 3993112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 399430cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 399573f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 399673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 3997493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 39983af24433SOleg Nesterov 3999eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 4000cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 40013af24433SOleg Nesterov 400230cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 4003d2c1d404STejun Heo goto err_free_wq; 40041537663fSTejun Heo 4005493008a8STejun Heo /* 4006493008a8STejun Heo * Workqueues which may be used during memory reclaim should 4007493008a8STejun Heo * have a rescuer to guarantee forward progress. 4008493008a8STejun Heo */ 4009493008a8STejun Heo if (flags & WQ_MEM_RECLAIM) { 4010e22bee78STejun Heo struct worker *rescuer; 4011e22bee78STejun Heo 4012f7537df5SLai Jiangshan rescuer = alloc_worker(NUMA_NO_NODE); 4013e22bee78STejun Heo if (!rescuer) 4014d2c1d404STejun Heo goto err_destroy; 4015e22bee78STejun Heo 4016111c225aSTejun Heo rescuer->rescue_wq = wq; 4017111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 4018b196be89STejun Heo wq->name); 4019d2c1d404STejun Heo if (IS_ERR(rescuer->task)) { 4020d2c1d404STejun Heo kfree(rescuer); 4021d2c1d404STejun Heo goto err_destroy; 4022d2c1d404STejun Heo } 4023e22bee78STejun Heo 4024d2c1d404STejun Heo wq->rescuer = rescuer; 402525834c73SPeter Zijlstra kthread_bind_mask(rescuer->task, cpu_possible_mask); 4026e22bee78STejun Heo wake_up_process(rescuer->task); 40273af24433SOleg Nesterov } 40281537663fSTejun Heo 4029226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4030226223abSTejun Heo goto err_destroy; 4031226223abSTejun Heo 40323af24433SOleg Nesterov /* 403368e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 403468e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 403568e13a67SLai Jiangshan * list. 40363af24433SOleg Nesterov */ 403768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4038a0a1a5fdSTejun Heo 4039a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 404049e3cf44STejun Heo for_each_pwq(pwq, wq) 4041699ce097STejun Heo pwq_adjust_max_active(pwq); 4042a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4043a0a1a5fdSTejun Heo 4044e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4045a0a1a5fdSTejun Heo 404668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 40473af24433SOleg Nesterov 40483af24433SOleg Nesterov return wq; 4049d2c1d404STejun Heo 4050d2c1d404STejun Heo err_free_wq: 40516029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 40524690c4abSTejun Heo kfree(wq); 4053d2c1d404STejun Heo return NULL; 4054d2c1d404STejun Heo err_destroy: 4055d2c1d404STejun Heo destroy_workqueue(wq); 40564690c4abSTejun Heo return NULL; 40571da177e4SLinus Torvalds } 4058d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 40591da177e4SLinus Torvalds 40603af24433SOleg Nesterov /** 40613af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 40623af24433SOleg Nesterov * @wq: target workqueue 40633af24433SOleg Nesterov * 40643af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 40653af24433SOleg Nesterov */ 40663af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 40673af24433SOleg Nesterov { 406849e3cf44STejun Heo struct pool_workqueue *pwq; 40694c16bd32STejun Heo int node; 40703af24433SOleg Nesterov 40719c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 40729c5a2ba7STejun Heo drain_workqueue(wq); 4073c8efcc25STejun Heo 40746183c009STejun Heo /* sanity checks */ 4075b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 407649e3cf44STejun Heo for_each_pwq(pwq, wq) { 40776183c009STejun Heo int i; 40786183c009STejun Heo 407976af4d93STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) { 408076af4d93STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) { 4081b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4082fa07fb6aSTejun Heo show_workqueue_state(); 40836183c009STejun Heo return; 408476af4d93STejun Heo } 408576af4d93STejun Heo } 408676af4d93STejun Heo 40875c529597SLai Jiangshan if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) || 40888864b4e5STejun Heo WARN_ON(pwq->nr_active) || 408976af4d93STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) { 4090b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4091fa07fb6aSTejun Heo show_workqueue_state(); 40926183c009STejun Heo return; 40936183c009STejun Heo } 409476af4d93STejun Heo } 4095b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40966183c009STejun Heo 4097a0a1a5fdSTejun Heo /* 4098a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4099a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4100a0a1a5fdSTejun Heo */ 410168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4102e2dca7adSTejun Heo list_del_rcu(&wq->list); 410368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 41043af24433SOleg Nesterov 4105226223abSTejun Heo workqueue_sysfs_unregister(wq); 4106226223abSTejun Heo 4107e2dca7adSTejun Heo if (wq->rescuer) 4108e22bee78STejun Heo kthread_stop(wq->rescuer->task); 4109e22bee78STejun Heo 41108864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 411129c91e99STejun Heo /* 41128864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4113e2dca7adSTejun Heo * schedule RCU free. 411429c91e99STejun Heo */ 4115e2dca7adSTejun Heo call_rcu_sched(&wq->rcu, rcu_free_wq); 41168864b4e5STejun Heo } else { 41178864b4e5STejun Heo /* 41188864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 41194c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 41204c16bd32STejun Heo * @wq will be freed when the last pwq is released. 41218864b4e5STejun Heo */ 41224c16bd32STejun Heo for_each_node(node) { 41234c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 41244c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 41254c16bd32STejun Heo put_pwq_unlocked(pwq); 41264c16bd32STejun Heo } 41274c16bd32STejun Heo 41284c16bd32STejun Heo /* 41294c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 41304c16bd32STejun Heo * put. Don't access it afterwards. 41314c16bd32STejun Heo */ 41324c16bd32STejun Heo pwq = wq->dfl_pwq; 41334c16bd32STejun Heo wq->dfl_pwq = NULL; 4134dce90d47STejun Heo put_pwq_unlocked(pwq); 413529c91e99STejun Heo } 41363af24433SOleg Nesterov } 41373af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 41383af24433SOleg Nesterov 4139dcd989cbSTejun Heo /** 4140dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4141dcd989cbSTejun Heo * @wq: target workqueue 4142dcd989cbSTejun Heo * @max_active: new max_active value. 4143dcd989cbSTejun Heo * 4144dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4145dcd989cbSTejun Heo * 4146dcd989cbSTejun Heo * CONTEXT: 4147dcd989cbSTejun Heo * Don't call from IRQ context. 4148dcd989cbSTejun Heo */ 4149dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4150dcd989cbSTejun Heo { 415149e3cf44STejun Heo struct pool_workqueue *pwq; 4152dcd989cbSTejun Heo 41538719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 41540a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 41558719dceaSTejun Heo return; 41568719dceaSTejun Heo 4157f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4158dcd989cbSTejun Heo 4159a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4160dcd989cbSTejun Heo 41610a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4162dcd989cbSTejun Heo wq->saved_max_active = max_active; 4163dcd989cbSTejun Heo 4164699ce097STejun Heo for_each_pwq(pwq, wq) 4165699ce097STejun Heo pwq_adjust_max_active(pwq); 4166dcd989cbSTejun Heo 4167a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4168dcd989cbSTejun Heo } 4169dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4170dcd989cbSTejun Heo 4171dcd989cbSTejun Heo /** 4172e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4173e6267616STejun Heo * 4174e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4175e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4176d185af30SYacine Belkadi * 4177d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4178e6267616STejun Heo */ 4179e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4180e6267616STejun Heo { 4181e6267616STejun Heo struct worker *worker = current_wq_worker(); 4182e6267616STejun Heo 41836a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4184e6267616STejun Heo } 4185e6267616STejun Heo 4186e6267616STejun Heo /** 4187dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4188dcd989cbSTejun Heo * @cpu: CPU in question 4189dcd989cbSTejun Heo * @wq: target workqueue 4190dcd989cbSTejun Heo * 4191dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4192dcd989cbSTejun Heo * no synchronization around this function and the test result is 4193dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4194dcd989cbSTejun Heo * 4195d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4196d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4197d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4198d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4199d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4200d3251859STejun Heo * 4201d185af30SYacine Belkadi * Return: 4202dcd989cbSTejun Heo * %true if congested, %false otherwise. 4203dcd989cbSTejun Heo */ 4204d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4205dcd989cbSTejun Heo { 42067fb98ea7STejun Heo struct pool_workqueue *pwq; 420776af4d93STejun Heo bool ret; 420876af4d93STejun Heo 420988109453SLai Jiangshan rcu_read_lock_sched(); 42107fb98ea7STejun Heo 4211d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4212d3251859STejun Heo cpu = smp_processor_id(); 4213d3251859STejun Heo 42147fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 42157fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 42167fb98ea7STejun Heo else 4217df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4218dcd989cbSTejun Heo 421976af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 422088109453SLai Jiangshan rcu_read_unlock_sched(); 422176af4d93STejun Heo 422276af4d93STejun Heo return ret; 4223dcd989cbSTejun Heo } 4224dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4225dcd989cbSTejun Heo 4226dcd989cbSTejun Heo /** 4227dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4228dcd989cbSTejun Heo * @work: the work to be tested 4229dcd989cbSTejun Heo * 4230dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4231dcd989cbSTejun Heo * synchronization around this function and the test result is 4232dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4233dcd989cbSTejun Heo * 4234d185af30SYacine Belkadi * Return: 4235dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4236dcd989cbSTejun Heo */ 4237dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4238dcd989cbSTejun Heo { 4239fa1b54e6STejun Heo struct worker_pool *pool; 4240dcd989cbSTejun Heo unsigned long flags; 4241dcd989cbSTejun Heo unsigned int ret = 0; 4242dcd989cbSTejun Heo 4243dcd989cbSTejun Heo if (work_pending(work)) 4244dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4245038366c5SLai Jiangshan 4246fa1b54e6STejun Heo local_irq_save(flags); 4247fa1b54e6STejun Heo pool = get_work_pool(work); 4248038366c5SLai Jiangshan if (pool) { 4249fa1b54e6STejun Heo spin_lock(&pool->lock); 4250c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4251dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4252fa1b54e6STejun Heo spin_unlock(&pool->lock); 4253038366c5SLai Jiangshan } 4254fa1b54e6STejun Heo local_irq_restore(flags); 4255dcd989cbSTejun Heo 4256dcd989cbSTejun Heo return ret; 4257dcd989cbSTejun Heo } 4258dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4259dcd989cbSTejun Heo 42603d1cb205STejun Heo /** 42613d1cb205STejun Heo * set_worker_desc - set description for the current work item 42623d1cb205STejun Heo * @fmt: printf-style format string 42633d1cb205STejun Heo * @...: arguments for the format string 42643d1cb205STejun Heo * 42653d1cb205STejun Heo * This function can be called by a running work function to describe what 42663d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 42673d1cb205STejun Heo * information will be printed out together to help debugging. The 42683d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 42693d1cb205STejun Heo */ 42703d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 42713d1cb205STejun Heo { 42723d1cb205STejun Heo struct worker *worker = current_wq_worker(); 42733d1cb205STejun Heo va_list args; 42743d1cb205STejun Heo 42753d1cb205STejun Heo if (worker) { 42763d1cb205STejun Heo va_start(args, fmt); 42773d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 42783d1cb205STejun Heo va_end(args); 42793d1cb205STejun Heo worker->desc_valid = true; 42803d1cb205STejun Heo } 42813d1cb205STejun Heo } 42823d1cb205STejun Heo 42833d1cb205STejun Heo /** 42843d1cb205STejun Heo * print_worker_info - print out worker information and description 42853d1cb205STejun Heo * @log_lvl: the log level to use when printing 42863d1cb205STejun Heo * @task: target task 42873d1cb205STejun Heo * 42883d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 42893d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 42903d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 42913d1cb205STejun Heo * 42923d1cb205STejun Heo * This function can be safely called on any task as long as the 42933d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 42943d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 42953d1cb205STejun Heo */ 42963d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 42973d1cb205STejun Heo { 42983d1cb205STejun Heo work_func_t *fn = NULL; 42993d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 43003d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 43013d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 43023d1cb205STejun Heo struct workqueue_struct *wq = NULL; 43033d1cb205STejun Heo bool desc_valid = false; 43043d1cb205STejun Heo struct worker *worker; 43053d1cb205STejun Heo 43063d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 43073d1cb205STejun Heo return; 43083d1cb205STejun Heo 43093d1cb205STejun Heo /* 43103d1cb205STejun Heo * This function is called without any synchronization and @task 43113d1cb205STejun Heo * could be in any state. Be careful with dereferences. 43123d1cb205STejun Heo */ 4313e700591aSPetr Mladek worker = kthread_probe_data(task); 43143d1cb205STejun Heo 43153d1cb205STejun Heo /* 43163d1cb205STejun Heo * Carefully copy the associated workqueue's workfn and name. Keep 43173d1cb205STejun Heo * the original last '\0' in case the original contains garbage. 43183d1cb205STejun Heo */ 43193d1cb205STejun Heo probe_kernel_read(&fn, &worker->current_func, sizeof(fn)); 43203d1cb205STejun Heo probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq)); 43213d1cb205STejun Heo probe_kernel_read(&wq, &pwq->wq, sizeof(wq)); 43223d1cb205STejun Heo probe_kernel_read(name, wq->name, sizeof(name) - 1); 43233d1cb205STejun Heo 43243d1cb205STejun Heo /* copy worker description */ 43253d1cb205STejun Heo probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid)); 43263d1cb205STejun Heo if (desc_valid) 43273d1cb205STejun Heo probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 43283d1cb205STejun Heo 43293d1cb205STejun Heo if (fn || name[0] || desc[0]) { 43303d1cb205STejun Heo printk("%sWorkqueue: %s %pf", log_lvl, name, fn); 43313d1cb205STejun Heo if (desc[0]) 43323d1cb205STejun Heo pr_cont(" (%s)", desc); 43333d1cb205STejun Heo pr_cont("\n"); 43343d1cb205STejun Heo } 43353d1cb205STejun Heo } 43363d1cb205STejun Heo 43373494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 43383494fc30STejun Heo { 43393494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 43403494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 43413494fc30STejun Heo pr_cont(" node=%d", pool->node); 43423494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 43433494fc30STejun Heo } 43443494fc30STejun Heo 43453494fc30STejun Heo static void pr_cont_work(bool comma, struct work_struct *work) 43463494fc30STejun Heo { 43473494fc30STejun Heo if (work->func == wq_barrier_func) { 43483494fc30STejun Heo struct wq_barrier *barr; 43493494fc30STejun Heo 43503494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 43513494fc30STejun Heo 43523494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 43533494fc30STejun Heo task_pid_nr(barr->task)); 43543494fc30STejun Heo } else { 43553494fc30STejun Heo pr_cont("%s %pf", comma ? "," : "", work->func); 43563494fc30STejun Heo } 43573494fc30STejun Heo } 43583494fc30STejun Heo 43593494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 43603494fc30STejun Heo { 43613494fc30STejun Heo struct worker_pool *pool = pwq->pool; 43623494fc30STejun Heo struct work_struct *work; 43633494fc30STejun Heo struct worker *worker; 43643494fc30STejun Heo bool has_in_flight = false, has_pending = false; 43653494fc30STejun Heo int bkt; 43663494fc30STejun Heo 43673494fc30STejun Heo pr_info(" pwq %d:", pool->id); 43683494fc30STejun Heo pr_cont_pool_info(pool); 43693494fc30STejun Heo 43703494fc30STejun Heo pr_cont(" active=%d/%d%s\n", pwq->nr_active, pwq->max_active, 43713494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 43723494fc30STejun Heo 43733494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 43743494fc30STejun Heo if (worker->current_pwq == pwq) { 43753494fc30STejun Heo has_in_flight = true; 43763494fc30STejun Heo break; 43773494fc30STejun Heo } 43783494fc30STejun Heo } 43793494fc30STejun Heo if (has_in_flight) { 43803494fc30STejun Heo bool comma = false; 43813494fc30STejun Heo 43823494fc30STejun Heo pr_info(" in-flight:"); 43833494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 43843494fc30STejun Heo if (worker->current_pwq != pwq) 43853494fc30STejun Heo continue; 43863494fc30STejun Heo 43873494fc30STejun Heo pr_cont("%s %d%s:%pf", comma ? "," : "", 43883494fc30STejun Heo task_pid_nr(worker->task), 43893494fc30STejun Heo worker == pwq->wq->rescuer ? "(RESCUER)" : "", 43903494fc30STejun Heo worker->current_func); 43913494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 43923494fc30STejun Heo pr_cont_work(false, work); 43933494fc30STejun Heo comma = true; 43943494fc30STejun Heo } 43953494fc30STejun Heo pr_cont("\n"); 43963494fc30STejun Heo } 43973494fc30STejun Heo 43983494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 43993494fc30STejun Heo if (get_work_pwq(work) == pwq) { 44003494fc30STejun Heo has_pending = true; 44013494fc30STejun Heo break; 44023494fc30STejun Heo } 44033494fc30STejun Heo } 44043494fc30STejun Heo if (has_pending) { 44053494fc30STejun Heo bool comma = false; 44063494fc30STejun Heo 44073494fc30STejun Heo pr_info(" pending:"); 44083494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 44093494fc30STejun Heo if (get_work_pwq(work) != pwq) 44103494fc30STejun Heo continue; 44113494fc30STejun Heo 44123494fc30STejun Heo pr_cont_work(comma, work); 44133494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 44143494fc30STejun Heo } 44153494fc30STejun Heo pr_cont("\n"); 44163494fc30STejun Heo } 44173494fc30STejun Heo 44183494fc30STejun Heo if (!list_empty(&pwq->delayed_works)) { 44193494fc30STejun Heo bool comma = false; 44203494fc30STejun Heo 44213494fc30STejun Heo pr_info(" delayed:"); 44223494fc30STejun Heo list_for_each_entry(work, &pwq->delayed_works, entry) { 44233494fc30STejun Heo pr_cont_work(comma, work); 44243494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 44253494fc30STejun Heo } 44263494fc30STejun Heo pr_cont("\n"); 44273494fc30STejun Heo } 44283494fc30STejun Heo } 44293494fc30STejun Heo 44303494fc30STejun Heo /** 44313494fc30STejun Heo * show_workqueue_state - dump workqueue state 44323494fc30STejun Heo * 44337b776af6SRoger Lu * Called from a sysrq handler or try_to_freeze_tasks() and prints out 44347b776af6SRoger Lu * all busy workqueues and pools. 44353494fc30STejun Heo */ 44363494fc30STejun Heo void show_workqueue_state(void) 44373494fc30STejun Heo { 44383494fc30STejun Heo struct workqueue_struct *wq; 44393494fc30STejun Heo struct worker_pool *pool; 44403494fc30STejun Heo unsigned long flags; 44413494fc30STejun Heo int pi; 44423494fc30STejun Heo 44433494fc30STejun Heo rcu_read_lock_sched(); 44443494fc30STejun Heo 44453494fc30STejun Heo pr_info("Showing busy workqueues and worker pools:\n"); 44463494fc30STejun Heo 44473494fc30STejun Heo list_for_each_entry_rcu(wq, &workqueues, list) { 44483494fc30STejun Heo struct pool_workqueue *pwq; 44493494fc30STejun Heo bool idle = true; 44503494fc30STejun Heo 44513494fc30STejun Heo for_each_pwq(pwq, wq) { 44523494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) { 44533494fc30STejun Heo idle = false; 44543494fc30STejun Heo break; 44553494fc30STejun Heo } 44563494fc30STejun Heo } 44573494fc30STejun Heo if (idle) 44583494fc30STejun Heo continue; 44593494fc30STejun Heo 44603494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 44613494fc30STejun Heo 44623494fc30STejun Heo for_each_pwq(pwq, wq) { 44633494fc30STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 44643494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) 44653494fc30STejun Heo show_pwq(pwq); 44663494fc30STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 44673494fc30STejun Heo } 44683494fc30STejun Heo } 44693494fc30STejun Heo 44703494fc30STejun Heo for_each_pool(pool, pi) { 44713494fc30STejun Heo struct worker *worker; 44723494fc30STejun Heo bool first = true; 44733494fc30STejun Heo 44743494fc30STejun Heo spin_lock_irqsave(&pool->lock, flags); 44753494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 44763494fc30STejun Heo goto next_pool; 44773494fc30STejun Heo 44783494fc30STejun Heo pr_info("pool %d:", pool->id); 44793494fc30STejun Heo pr_cont_pool_info(pool); 448082607adcSTejun Heo pr_cont(" hung=%us workers=%d", 448182607adcSTejun Heo jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000, 448282607adcSTejun Heo pool->nr_workers); 44833494fc30STejun Heo if (pool->manager) 44843494fc30STejun Heo pr_cont(" manager: %d", 44853494fc30STejun Heo task_pid_nr(pool->manager->task)); 44863494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 44873494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 44883494fc30STejun Heo task_pid_nr(worker->task)); 44893494fc30STejun Heo first = false; 44903494fc30STejun Heo } 44913494fc30STejun Heo pr_cont("\n"); 44923494fc30STejun Heo next_pool: 44933494fc30STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 44943494fc30STejun Heo } 44953494fc30STejun Heo 44963494fc30STejun Heo rcu_read_unlock_sched(); 44973494fc30STejun Heo } 44983494fc30STejun Heo 4499db7bccf4STejun Heo /* 4500db7bccf4STejun Heo * CPU hotplug. 4501db7bccf4STejun Heo * 4502e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 4503112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 4504706026c2STejun Heo * pool which make migrating pending and scheduled works very 4505e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 450694cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 4507e22bee78STejun Heo * blocked draining impractical. 4508e22bee78STejun Heo * 450924647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 4510628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 4511628c78e7STejun Heo * cpu comes back online. 4512db7bccf4STejun Heo */ 4513db7bccf4STejun Heo 4514706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 4515db7bccf4STejun Heo { 451638db41d9STejun Heo int cpu = smp_processor_id(); 45174ce62e9eSTejun Heo struct worker_pool *pool; 4518db7bccf4STejun Heo struct worker *worker; 4519db7bccf4STejun Heo 4520f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 452192f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 452294cf58bbSTejun Heo spin_lock_irq(&pool->lock); 4523e22bee78STejun Heo 4524f2d5a0eeSTejun Heo /* 452592f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 452694cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 452794cf58bbSTejun Heo * except for the ones which are still executing works from 452894cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 452994cf58bbSTejun Heo * this, they may become diasporas. 4530f2d5a0eeSTejun Heo */ 4531da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4532403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 4533db7bccf4STejun Heo 453424647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 4535f2d5a0eeSTejun Heo 453694cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 453792f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 4538e22bee78STejun Heo 4539e22bee78STejun Heo /* 4540eb283428SLai Jiangshan * Call schedule() so that we cross rq->lock and thus can 4541eb283428SLai Jiangshan * guarantee sched callbacks see the %WORKER_UNBOUND flag. 4542eb283428SLai Jiangshan * This is necessary as scheduler callbacks may be invoked 4543eb283428SLai Jiangshan * from other cpus. 4544628c78e7STejun Heo */ 4545628c78e7STejun Heo schedule(); 4546628c78e7STejun Heo 4547628c78e7STejun Heo /* 4548eb283428SLai Jiangshan * Sched callbacks are disabled now. Zap nr_running. 4549eb283428SLai Jiangshan * After this, nr_running stays zero and need_more_worker() 4550eb283428SLai Jiangshan * and keep_working() are always true as long as the 4551eb283428SLai Jiangshan * worklist is not empty. This pool now behaves as an 4552eb283428SLai Jiangshan * unbound (in terms of concurrency management) pool which 4553eb283428SLai Jiangshan * are served by workers tied to the pool. 4554e22bee78STejun Heo */ 4555e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 4556eb283428SLai Jiangshan 4557eb283428SLai Jiangshan /* 4558eb283428SLai Jiangshan * With concurrency management just turned off, a busy 4559eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 4560eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 4561eb283428SLai Jiangshan */ 4562eb283428SLai Jiangshan spin_lock_irq(&pool->lock); 4563eb283428SLai Jiangshan wake_up_worker(pool); 4564eb283428SLai Jiangshan spin_unlock_irq(&pool->lock); 4565eb283428SLai Jiangshan } 4566db7bccf4STejun Heo } 4567db7bccf4STejun Heo 4568bd7c089eSTejun Heo /** 4569bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 4570bd7c089eSTejun Heo * @pool: pool of interest 4571bd7c089eSTejun Heo * 4572a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 4573bd7c089eSTejun Heo */ 4574bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 4575bd7c089eSTejun Heo { 4576a9ab775bSTejun Heo struct worker *worker; 4577bd7c089eSTejun Heo 457892f9c5c4SLai Jiangshan lockdep_assert_held(&pool->attach_mutex); 4579bd7c089eSTejun Heo 4580bd7c089eSTejun Heo /* 4581a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 4582a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 4583402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 4584a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 4585a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 4586bd7c089eSTejun Heo */ 4587da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4588a9ab775bSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 4589a9ab775bSTejun Heo pool->attrs->cpumask) < 0); 4590a9ab775bSTejun Heo 4591a9ab775bSTejun Heo spin_lock_irq(&pool->lock); 4592f7c17d26SWanpeng Li 4593f7c17d26SWanpeng Li /* 4594f7c17d26SWanpeng Li * XXX: CPU hotplug notifiers are weird and can call DOWN_FAILED 4595f7c17d26SWanpeng Li * w/o preceding DOWN_PREPARE. Work around it. CPU hotplug is 4596f7c17d26SWanpeng Li * being reworked and this can go away in time. 4597f7c17d26SWanpeng Li */ 4598f7c17d26SWanpeng Li if (!(pool->flags & POOL_DISASSOCIATED)) { 4599f7c17d26SWanpeng Li spin_unlock_irq(&pool->lock); 4600f7c17d26SWanpeng Li return; 4601f7c17d26SWanpeng Li } 4602f7c17d26SWanpeng Li 46033de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 4604a9ab775bSTejun Heo 4605da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 4606a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 4607a9ab775bSTejun Heo 4608a9ab775bSTejun Heo /* 4609a9ab775bSTejun Heo * A bound idle worker should actually be on the runqueue 4610a9ab775bSTejun Heo * of the associated CPU for local wake-ups targeting it to 4611a9ab775bSTejun Heo * work. Kick all idle workers so that they migrate to the 4612a9ab775bSTejun Heo * associated CPU. Doing this in the same loop as 4613a9ab775bSTejun Heo * replacing UNBOUND with REBOUND is safe as no worker will 4614a9ab775bSTejun Heo * be bound before @pool->lock is released. 4615a9ab775bSTejun Heo */ 4616a9ab775bSTejun Heo if (worker_flags & WORKER_IDLE) 4617bd7c089eSTejun Heo wake_up_process(worker->task); 4618bd7c089eSTejun Heo 4619bd7c089eSTejun Heo /* 4620a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 4621a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 4622a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 4623a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 4624a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 4625a9ab775bSTejun Heo * concurrency management. Note that when or whether 4626a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 4627a9ab775bSTejun Heo * 4628c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 4629a9ab775bSTejun Heo * tested without holding any lock in 4630a9ab775bSTejun Heo * wq_worker_waking_up(). Without it, NOT_RUNNING test may 4631a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 4632a9ab775bSTejun Heo * management operations. 4633bd7c089eSTejun Heo */ 4634a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 4635a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 4636a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 4637c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 4638bd7c089eSTejun Heo } 4639a9ab775bSTejun Heo 4640a9ab775bSTejun Heo spin_unlock_irq(&pool->lock); 4641bd7c089eSTejun Heo } 4642bd7c089eSTejun Heo 46437dbc725eSTejun Heo /** 46447dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 46457dbc725eSTejun Heo * @pool: unbound pool of interest 46467dbc725eSTejun Heo * @cpu: the CPU which is coming up 46477dbc725eSTejun Heo * 46487dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 46497dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 46507dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 46517dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 46527dbc725eSTejun Heo */ 46537dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 46547dbc725eSTejun Heo { 46557dbc725eSTejun Heo static cpumask_t cpumask; 46567dbc725eSTejun Heo struct worker *worker; 46577dbc725eSTejun Heo 465892f9c5c4SLai Jiangshan lockdep_assert_held(&pool->attach_mutex); 46597dbc725eSTejun Heo 46607dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 46617dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 46627dbc725eSTejun Heo return; 46637dbc725eSTejun Heo 46647dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 46657dbc725eSTejun Heo 46667dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 4667da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4668d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 46697dbc725eSTejun Heo } 46707dbc725eSTejun Heo 46717ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 46721da177e4SLinus Torvalds { 46734ce62e9eSTejun Heo struct worker_pool *pool; 46741da177e4SLinus Torvalds 4675f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 46763ce63377STejun Heo if (pool->nr_workers) 46773ce63377STejun Heo continue; 4678051e1850SLai Jiangshan if (!create_worker(pool)) 46797ee681b2SThomas Gleixner return -ENOMEM; 46803af24433SOleg Nesterov } 46817ee681b2SThomas Gleixner return 0; 46827ee681b2SThomas Gleixner } 46831da177e4SLinus Torvalds 46847ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 46857ee681b2SThomas Gleixner { 46867ee681b2SThomas Gleixner struct worker_pool *pool; 46877ee681b2SThomas Gleixner struct workqueue_struct *wq; 46887ee681b2SThomas Gleixner int pi; 46897ee681b2SThomas Gleixner 469068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 46917dbc725eSTejun Heo 46927dbc725eSTejun Heo for_each_pool(pool, pi) { 469392f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 469494cf58bbSTejun Heo 4695f05b558dSLai Jiangshan if (pool->cpu == cpu) 469694cf58bbSTejun Heo rebind_workers(pool); 4697f05b558dSLai Jiangshan else if (pool->cpu < 0) 46987dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 469994cf58bbSTejun Heo 470092f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 470194cf58bbSTejun Heo } 47027dbc725eSTejun Heo 47034c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 47044c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 47054c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 47064c16bd32STejun Heo 470768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 47087ee681b2SThomas Gleixner return 0; 470965758202STejun Heo } 471065758202STejun Heo 47117ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 471265758202STejun Heo { 47138db25e78STejun Heo struct work_struct unbind_work; 47144c16bd32STejun Heo struct workqueue_struct *wq; 47158db25e78STejun Heo 47164c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 4717706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 47187635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 47194c16bd32STejun Heo 47204c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 47214c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 47224c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 47234c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 47244c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 47254c16bd32STejun Heo 47264c16bd32STejun Heo /* wait for per-cpu unbinding to finish */ 47278db25e78STejun Heo flush_work(&unbind_work); 4728440a1136SChuansheng Liu destroy_work_on_stack(&unbind_work); 47297ee681b2SThomas Gleixner return 0; 473065758202STejun Heo } 473165758202STejun Heo 47322d3854a3SRusty Russell #ifdef CONFIG_SMP 47338ccad40dSRusty Russell 47342d3854a3SRusty Russell struct work_for_cpu { 4735ed48ece2STejun Heo struct work_struct work; 47362d3854a3SRusty Russell long (*fn)(void *); 47372d3854a3SRusty Russell void *arg; 47382d3854a3SRusty Russell long ret; 47392d3854a3SRusty Russell }; 47402d3854a3SRusty Russell 4741ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 47422d3854a3SRusty Russell { 4743ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 4744ed48ece2STejun Heo 47452d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 47462d3854a3SRusty Russell } 47472d3854a3SRusty Russell 47482d3854a3SRusty Russell /** 474922aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 47502d3854a3SRusty Russell * @cpu: the cpu to run on 47512d3854a3SRusty Russell * @fn: the function to run 47522d3854a3SRusty Russell * @arg: the function arg 47532d3854a3SRusty Russell * 475431ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 47556b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 4756d185af30SYacine Belkadi * 4757d185af30SYacine Belkadi * Return: The value @fn returns. 47582d3854a3SRusty Russell */ 4759d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 47602d3854a3SRusty Russell { 4761ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 47622d3854a3SRusty Russell 4763ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 4764ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 476512997d1aSBjorn Helgaas flush_work(&wfc.work); 4766440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 47672d3854a3SRusty Russell return wfc.ret; 47682d3854a3SRusty Russell } 47692d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 47700e8d6a93SThomas Gleixner 47710e8d6a93SThomas Gleixner /** 47720e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 47730e8d6a93SThomas Gleixner * @cpu: the cpu to run on 47740e8d6a93SThomas Gleixner * @fn: the function to run 47750e8d6a93SThomas Gleixner * @arg: the function argument 47760e8d6a93SThomas Gleixner * 47770e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 47780e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 47790e8d6a93SThomas Gleixner * 47800e8d6a93SThomas Gleixner * Return: The value @fn returns. 47810e8d6a93SThomas Gleixner */ 47820e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 47830e8d6a93SThomas Gleixner { 47840e8d6a93SThomas Gleixner long ret = -ENODEV; 47850e8d6a93SThomas Gleixner 47860e8d6a93SThomas Gleixner get_online_cpus(); 47870e8d6a93SThomas Gleixner if (cpu_online(cpu)) 47880e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 47890e8d6a93SThomas Gleixner put_online_cpus(); 47900e8d6a93SThomas Gleixner return ret; 47910e8d6a93SThomas Gleixner } 47920e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 47932d3854a3SRusty Russell #endif /* CONFIG_SMP */ 47942d3854a3SRusty Russell 4795a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 4796e7577c50SRusty Russell 4797a0a1a5fdSTejun Heo /** 4798a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 4799a0a1a5fdSTejun Heo * 480058a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 4801c5aa87bbSTejun Heo * workqueues will queue new works to their delayed_works list instead of 4802706026c2STejun Heo * pool->worklist. 4803a0a1a5fdSTejun Heo * 4804a0a1a5fdSTejun Heo * CONTEXT: 4805a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4806a0a1a5fdSTejun Heo */ 4807a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 4808a0a1a5fdSTejun Heo { 480924b8a847STejun Heo struct workqueue_struct *wq; 481024b8a847STejun Heo struct pool_workqueue *pwq; 4811a0a1a5fdSTejun Heo 481268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4813a0a1a5fdSTejun Heo 48146183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 4815a0a1a5fdSTejun Heo workqueue_freezing = true; 4816a0a1a5fdSTejun Heo 481724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4818a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4819699ce097STejun Heo for_each_pwq(pwq, wq) 4820699ce097STejun Heo pwq_adjust_max_active(pwq); 4821a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4822a1056305STejun Heo } 48235bcab335STejun Heo 482468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4825a0a1a5fdSTejun Heo } 4826a0a1a5fdSTejun Heo 4827a0a1a5fdSTejun Heo /** 482858a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 4829a0a1a5fdSTejun Heo * 4830a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 4831a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 4832a0a1a5fdSTejun Heo * 4833a0a1a5fdSTejun Heo * CONTEXT: 483468e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 4835a0a1a5fdSTejun Heo * 4836d185af30SYacine Belkadi * Return: 483758a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 483858a69cb4STejun Heo * is complete. 4839a0a1a5fdSTejun Heo */ 4840a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 4841a0a1a5fdSTejun Heo { 4842a0a1a5fdSTejun Heo bool busy = false; 484324b8a847STejun Heo struct workqueue_struct *wq; 484424b8a847STejun Heo struct pool_workqueue *pwq; 4845a0a1a5fdSTejun Heo 484668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4847a0a1a5fdSTejun Heo 48486183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 4849a0a1a5fdSTejun Heo 485024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 485124b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 485224b8a847STejun Heo continue; 4853a0a1a5fdSTejun Heo /* 4854a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 4855a0a1a5fdSTejun Heo * to peek without lock. 4856a0a1a5fdSTejun Heo */ 485788109453SLai Jiangshan rcu_read_lock_sched(); 485824b8a847STejun Heo for_each_pwq(pwq, wq) { 48596183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 4860112202d9STejun Heo if (pwq->nr_active) { 4861a0a1a5fdSTejun Heo busy = true; 486288109453SLai Jiangshan rcu_read_unlock_sched(); 4863a0a1a5fdSTejun Heo goto out_unlock; 4864a0a1a5fdSTejun Heo } 4865a0a1a5fdSTejun Heo } 486688109453SLai Jiangshan rcu_read_unlock_sched(); 4867a0a1a5fdSTejun Heo } 4868a0a1a5fdSTejun Heo out_unlock: 486968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4870a0a1a5fdSTejun Heo return busy; 4871a0a1a5fdSTejun Heo } 4872a0a1a5fdSTejun Heo 4873a0a1a5fdSTejun Heo /** 4874a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 4875a0a1a5fdSTejun Heo * 4876a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 4877706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 4878a0a1a5fdSTejun Heo * 4879a0a1a5fdSTejun Heo * CONTEXT: 4880a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4881a0a1a5fdSTejun Heo */ 4882a0a1a5fdSTejun Heo void thaw_workqueues(void) 4883a0a1a5fdSTejun Heo { 488424b8a847STejun Heo struct workqueue_struct *wq; 488524b8a847STejun Heo struct pool_workqueue *pwq; 4886a0a1a5fdSTejun Heo 488768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4888a0a1a5fdSTejun Heo 4889a0a1a5fdSTejun Heo if (!workqueue_freezing) 4890a0a1a5fdSTejun Heo goto out_unlock; 4891a0a1a5fdSTejun Heo 489274b414eaSLai Jiangshan workqueue_freezing = false; 489324b8a847STejun Heo 489424b8a847STejun Heo /* restore max_active and repopulate worklist */ 489524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4896a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4897699ce097STejun Heo for_each_pwq(pwq, wq) 4898699ce097STejun Heo pwq_adjust_max_active(pwq); 4899a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 490024b8a847STejun Heo } 490124b8a847STejun Heo 4902a0a1a5fdSTejun Heo out_unlock: 490368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4904a0a1a5fdSTejun Heo } 4905a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 4906a0a1a5fdSTejun Heo 4907042f7df1SLai Jiangshan static int workqueue_apply_unbound_cpumask(void) 4908042f7df1SLai Jiangshan { 4909042f7df1SLai Jiangshan LIST_HEAD(ctxs); 4910042f7df1SLai Jiangshan int ret = 0; 4911042f7df1SLai Jiangshan struct workqueue_struct *wq; 4912042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 4913042f7df1SLai Jiangshan 4914042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 4915042f7df1SLai Jiangshan 4916042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 4917042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 4918042f7df1SLai Jiangshan continue; 4919042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 4920042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 4921042f7df1SLai Jiangshan continue; 4922042f7df1SLai Jiangshan 4923042f7df1SLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); 4924042f7df1SLai Jiangshan if (!ctx) { 4925042f7df1SLai Jiangshan ret = -ENOMEM; 4926042f7df1SLai Jiangshan break; 4927042f7df1SLai Jiangshan } 4928042f7df1SLai Jiangshan 4929042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 4930042f7df1SLai Jiangshan } 4931042f7df1SLai Jiangshan 4932042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 4933042f7df1SLai Jiangshan if (!ret) 4934042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 4935042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 4936042f7df1SLai Jiangshan } 4937042f7df1SLai Jiangshan 4938042f7df1SLai Jiangshan return ret; 4939042f7df1SLai Jiangshan } 4940042f7df1SLai Jiangshan 4941042f7df1SLai Jiangshan /** 4942042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 4943042f7df1SLai Jiangshan * @cpumask: the cpumask to set 4944042f7df1SLai Jiangshan * 4945042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 4946042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 4947042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 4948042f7df1SLai Jiangshan * 4949042f7df1SLai Jiangshan * Retun: 0 - Success 4950042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 4951042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 4952042f7df1SLai Jiangshan */ 4953042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 4954042f7df1SLai Jiangshan { 4955042f7df1SLai Jiangshan int ret = -EINVAL; 4956042f7df1SLai Jiangshan cpumask_var_t saved_cpumask; 4957042f7df1SLai Jiangshan 4958042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) 4959042f7df1SLai Jiangshan return -ENOMEM; 4960042f7df1SLai Jiangshan 4961c98a9805STal Shorer /* 4962c98a9805STal Shorer * Not excluding isolated cpus on purpose. 4963c98a9805STal Shorer * If the user wishes to include them, we allow that. 4964c98a9805STal Shorer */ 4965042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 4966042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 4967a0111cf6SLai Jiangshan apply_wqattrs_lock(); 4968042f7df1SLai Jiangshan 4969042f7df1SLai Jiangshan /* save the old wq_unbound_cpumask. */ 4970042f7df1SLai Jiangshan cpumask_copy(saved_cpumask, wq_unbound_cpumask); 4971042f7df1SLai Jiangshan 4972042f7df1SLai Jiangshan /* update wq_unbound_cpumask at first and apply it to wqs. */ 4973042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, cpumask); 4974042f7df1SLai Jiangshan ret = workqueue_apply_unbound_cpumask(); 4975042f7df1SLai Jiangshan 4976042f7df1SLai Jiangshan /* restore the wq_unbound_cpumask when failed. */ 4977042f7df1SLai Jiangshan if (ret < 0) 4978042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, saved_cpumask); 4979042f7df1SLai Jiangshan 4980a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 4981042f7df1SLai Jiangshan } 4982042f7df1SLai Jiangshan 4983042f7df1SLai Jiangshan free_cpumask_var(saved_cpumask); 4984042f7df1SLai Jiangshan return ret; 4985042f7df1SLai Jiangshan } 4986042f7df1SLai Jiangshan 49876ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 49886ba94429SFrederic Weisbecker /* 49896ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 49906ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 49916ba94429SFrederic Weisbecker * following attributes. 49926ba94429SFrederic Weisbecker * 49936ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 49946ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 49956ba94429SFrederic Weisbecker * 49966ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 49976ba94429SFrederic Weisbecker * 49989a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 49996ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 50006ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 50019a19b463SWang Long * numa RW bool : whether enable NUMA affinity 50026ba94429SFrederic Weisbecker */ 50036ba94429SFrederic Weisbecker struct wq_device { 50046ba94429SFrederic Weisbecker struct workqueue_struct *wq; 50056ba94429SFrederic Weisbecker struct device dev; 50066ba94429SFrederic Weisbecker }; 50076ba94429SFrederic Weisbecker 50086ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 50096ba94429SFrederic Weisbecker { 50106ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 50116ba94429SFrederic Weisbecker 50126ba94429SFrederic Weisbecker return wq_dev->wq; 50136ba94429SFrederic Weisbecker } 50146ba94429SFrederic Weisbecker 50156ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 50166ba94429SFrederic Weisbecker char *buf) 50176ba94429SFrederic Weisbecker { 50186ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50196ba94429SFrederic Weisbecker 50206ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 50216ba94429SFrederic Weisbecker } 50226ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 50236ba94429SFrederic Weisbecker 50246ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 50256ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 50266ba94429SFrederic Weisbecker { 50276ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50286ba94429SFrederic Weisbecker 50296ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 50306ba94429SFrederic Weisbecker } 50316ba94429SFrederic Weisbecker 50326ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 50336ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 50346ba94429SFrederic Weisbecker size_t count) 50356ba94429SFrederic Weisbecker { 50366ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50376ba94429SFrederic Weisbecker int val; 50386ba94429SFrederic Weisbecker 50396ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 50406ba94429SFrederic Weisbecker return -EINVAL; 50416ba94429SFrederic Weisbecker 50426ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 50436ba94429SFrederic Weisbecker return count; 50446ba94429SFrederic Weisbecker } 50456ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 50466ba94429SFrederic Weisbecker 50476ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 50486ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 50496ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 50506ba94429SFrederic Weisbecker NULL, 50516ba94429SFrederic Weisbecker }; 50526ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 50536ba94429SFrederic Weisbecker 50546ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 50556ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 50566ba94429SFrederic Weisbecker { 50576ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50586ba94429SFrederic Weisbecker const char *delim = ""; 50596ba94429SFrederic Weisbecker int node, written = 0; 50606ba94429SFrederic Weisbecker 50616ba94429SFrederic Weisbecker rcu_read_lock_sched(); 50626ba94429SFrederic Weisbecker for_each_node(node) { 50636ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 50646ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 50656ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 50666ba94429SFrederic Weisbecker delim = " "; 50676ba94429SFrederic Weisbecker } 50686ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 50696ba94429SFrederic Weisbecker rcu_read_unlock_sched(); 50706ba94429SFrederic Weisbecker 50716ba94429SFrederic Weisbecker return written; 50726ba94429SFrederic Weisbecker } 50736ba94429SFrederic Weisbecker 50746ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 50756ba94429SFrederic Weisbecker char *buf) 50766ba94429SFrederic Weisbecker { 50776ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50786ba94429SFrederic Weisbecker int written; 50796ba94429SFrederic Weisbecker 50806ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 50816ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 50826ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 50836ba94429SFrederic Weisbecker 50846ba94429SFrederic Weisbecker return written; 50856ba94429SFrederic Weisbecker } 50866ba94429SFrederic Weisbecker 50876ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 50886ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 50896ba94429SFrederic Weisbecker { 50906ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 50916ba94429SFrederic Weisbecker 5092899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5093899a94feSLai Jiangshan 50946ba94429SFrederic Weisbecker attrs = alloc_workqueue_attrs(GFP_KERNEL); 50956ba94429SFrederic Weisbecker if (!attrs) 50966ba94429SFrederic Weisbecker return NULL; 50976ba94429SFrederic Weisbecker 50986ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 50996ba94429SFrederic Weisbecker return attrs; 51006ba94429SFrederic Weisbecker } 51016ba94429SFrederic Weisbecker 51026ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 51036ba94429SFrederic Weisbecker const char *buf, size_t count) 51046ba94429SFrederic Weisbecker { 51056ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51066ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5107d4d3e257SLai Jiangshan int ret = -ENOMEM; 5108d4d3e257SLai Jiangshan 5109d4d3e257SLai Jiangshan apply_wqattrs_lock(); 51106ba94429SFrederic Weisbecker 51116ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 51126ba94429SFrederic Weisbecker if (!attrs) 5113d4d3e257SLai Jiangshan goto out_unlock; 51146ba94429SFrederic Weisbecker 51156ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 51166ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5117d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 51186ba94429SFrederic Weisbecker else 51196ba94429SFrederic Weisbecker ret = -EINVAL; 51206ba94429SFrederic Weisbecker 5121d4d3e257SLai Jiangshan out_unlock: 5122d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 51236ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 51246ba94429SFrederic Weisbecker return ret ?: count; 51256ba94429SFrederic Weisbecker } 51266ba94429SFrederic Weisbecker 51276ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 51286ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 51296ba94429SFrederic Weisbecker { 51306ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51316ba94429SFrederic Weisbecker int written; 51326ba94429SFrederic Weisbecker 51336ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 51346ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 51356ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 51366ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 51376ba94429SFrederic Weisbecker return written; 51386ba94429SFrederic Weisbecker } 51396ba94429SFrederic Weisbecker 51406ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 51416ba94429SFrederic Weisbecker struct device_attribute *attr, 51426ba94429SFrederic Weisbecker const char *buf, size_t count) 51436ba94429SFrederic Weisbecker { 51446ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51456ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5146d4d3e257SLai Jiangshan int ret = -ENOMEM; 5147d4d3e257SLai Jiangshan 5148d4d3e257SLai Jiangshan apply_wqattrs_lock(); 51496ba94429SFrederic Weisbecker 51506ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 51516ba94429SFrederic Weisbecker if (!attrs) 5152d4d3e257SLai Jiangshan goto out_unlock; 51536ba94429SFrederic Weisbecker 51546ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 51556ba94429SFrederic Weisbecker if (!ret) 5156d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 51576ba94429SFrederic Weisbecker 5158d4d3e257SLai Jiangshan out_unlock: 5159d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 51606ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 51616ba94429SFrederic Weisbecker return ret ?: count; 51626ba94429SFrederic Weisbecker } 51636ba94429SFrederic Weisbecker 51646ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 51656ba94429SFrederic Weisbecker char *buf) 51666ba94429SFrederic Weisbecker { 51676ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51686ba94429SFrederic Weisbecker int written; 51696ba94429SFrederic Weisbecker 51706ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 51716ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 51726ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 51736ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 51746ba94429SFrederic Weisbecker 51756ba94429SFrederic Weisbecker return written; 51766ba94429SFrederic Weisbecker } 51776ba94429SFrederic Weisbecker 51786ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 51796ba94429SFrederic Weisbecker const char *buf, size_t count) 51806ba94429SFrederic Weisbecker { 51816ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 51826ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5183d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5184d4d3e257SLai Jiangshan 5185d4d3e257SLai Jiangshan apply_wqattrs_lock(); 51866ba94429SFrederic Weisbecker 51876ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 51886ba94429SFrederic Weisbecker if (!attrs) 5189d4d3e257SLai Jiangshan goto out_unlock; 51906ba94429SFrederic Weisbecker 51916ba94429SFrederic Weisbecker ret = -EINVAL; 51926ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 51936ba94429SFrederic Weisbecker attrs->no_numa = !v; 5194d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 51956ba94429SFrederic Weisbecker } 51966ba94429SFrederic Weisbecker 5197d4d3e257SLai Jiangshan out_unlock: 5198d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 51996ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 52006ba94429SFrederic Weisbecker return ret ?: count; 52016ba94429SFrederic Weisbecker } 52026ba94429SFrederic Weisbecker 52036ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 52046ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 52056ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 52066ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 52076ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 52086ba94429SFrederic Weisbecker __ATTR_NULL, 52096ba94429SFrederic Weisbecker }; 52106ba94429SFrederic Weisbecker 52116ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 52126ba94429SFrederic Weisbecker .name = "workqueue", 52136ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 52146ba94429SFrederic Weisbecker }; 52156ba94429SFrederic Weisbecker 5216b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5217b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5218b05a7928SFrederic Weisbecker { 5219b05a7928SFrederic Weisbecker int written; 5220b05a7928SFrederic Weisbecker 5221042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5222b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5223b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5224042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5225b05a7928SFrederic Weisbecker 5226b05a7928SFrederic Weisbecker return written; 5227b05a7928SFrederic Weisbecker } 5228b05a7928SFrederic Weisbecker 5229042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5230042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5231042f7df1SLai Jiangshan { 5232042f7df1SLai Jiangshan cpumask_var_t cpumask; 5233042f7df1SLai Jiangshan int ret; 5234042f7df1SLai Jiangshan 5235042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5236042f7df1SLai Jiangshan return -ENOMEM; 5237042f7df1SLai Jiangshan 5238042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5239042f7df1SLai Jiangshan if (!ret) 5240042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5241042f7df1SLai Jiangshan 5242042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5243042f7df1SLai Jiangshan return ret ? ret : count; 5244042f7df1SLai Jiangshan } 5245042f7df1SLai Jiangshan 5246b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5247042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5248042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5249b05a7928SFrederic Weisbecker 52506ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 52516ba94429SFrederic Weisbecker { 5252b05a7928SFrederic Weisbecker int err; 5253b05a7928SFrederic Weisbecker 5254b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5255b05a7928SFrederic Weisbecker if (err) 5256b05a7928SFrederic Weisbecker return err; 5257b05a7928SFrederic Weisbecker 5258b05a7928SFrederic Weisbecker return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr); 52596ba94429SFrederic Weisbecker } 52606ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 52616ba94429SFrederic Weisbecker 52626ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 52636ba94429SFrederic Weisbecker { 52646ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 52656ba94429SFrederic Weisbecker 52666ba94429SFrederic Weisbecker kfree(wq_dev); 52676ba94429SFrederic Weisbecker } 52686ba94429SFrederic Weisbecker 52696ba94429SFrederic Weisbecker /** 52706ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 52716ba94429SFrederic Weisbecker * @wq: the workqueue to register 52726ba94429SFrederic Weisbecker * 52736ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 52746ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 52756ba94429SFrederic Weisbecker * which is the preferred method. 52766ba94429SFrederic Weisbecker * 52776ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 52786ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 52796ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 52806ba94429SFrederic Weisbecker * attributes. 52816ba94429SFrederic Weisbecker * 52826ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 52836ba94429SFrederic Weisbecker */ 52846ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 52856ba94429SFrederic Weisbecker { 52866ba94429SFrederic Weisbecker struct wq_device *wq_dev; 52876ba94429SFrederic Weisbecker int ret; 52886ba94429SFrederic Weisbecker 52896ba94429SFrederic Weisbecker /* 5290402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 52916ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 52926ba94429SFrederic Weisbecker * workqueues. 52936ba94429SFrederic Weisbecker */ 52940a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 52956ba94429SFrederic Weisbecker return -EINVAL; 52966ba94429SFrederic Weisbecker 52976ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 52986ba94429SFrederic Weisbecker if (!wq_dev) 52996ba94429SFrederic Weisbecker return -ENOMEM; 53006ba94429SFrederic Weisbecker 53016ba94429SFrederic Weisbecker wq_dev->wq = wq; 53026ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 53036ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 530423217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 53056ba94429SFrederic Weisbecker 53066ba94429SFrederic Weisbecker /* 53076ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 53086ba94429SFrederic Weisbecker * everything is ready. 53096ba94429SFrederic Weisbecker */ 53106ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 53116ba94429SFrederic Weisbecker 53126ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 53136ba94429SFrederic Weisbecker if (ret) { 53146ba94429SFrederic Weisbecker kfree(wq_dev); 53156ba94429SFrederic Weisbecker wq->wq_dev = NULL; 53166ba94429SFrederic Weisbecker return ret; 53176ba94429SFrederic Weisbecker } 53186ba94429SFrederic Weisbecker 53196ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 53206ba94429SFrederic Weisbecker struct device_attribute *attr; 53216ba94429SFrederic Weisbecker 53226ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 53236ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 53246ba94429SFrederic Weisbecker if (ret) { 53256ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 53266ba94429SFrederic Weisbecker wq->wq_dev = NULL; 53276ba94429SFrederic Weisbecker return ret; 53286ba94429SFrederic Weisbecker } 53296ba94429SFrederic Weisbecker } 53306ba94429SFrederic Weisbecker } 53316ba94429SFrederic Weisbecker 53326ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 53336ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 53346ba94429SFrederic Weisbecker return 0; 53356ba94429SFrederic Weisbecker } 53366ba94429SFrederic Weisbecker 53376ba94429SFrederic Weisbecker /** 53386ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 53396ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 53406ba94429SFrederic Weisbecker * 53416ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 53426ba94429SFrederic Weisbecker */ 53436ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 53446ba94429SFrederic Weisbecker { 53456ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 53466ba94429SFrederic Weisbecker 53476ba94429SFrederic Weisbecker if (!wq->wq_dev) 53486ba94429SFrederic Weisbecker return; 53496ba94429SFrederic Weisbecker 53506ba94429SFrederic Weisbecker wq->wq_dev = NULL; 53516ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 53526ba94429SFrederic Weisbecker } 53536ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 53546ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 53556ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 53566ba94429SFrederic Weisbecker 535782607adcSTejun Heo /* 535882607adcSTejun Heo * Workqueue watchdog. 535982607adcSTejun Heo * 536082607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 536182607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 536282607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 536382607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 536482607adcSTejun Heo * largely opaque. 536582607adcSTejun Heo * 536682607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 536782607adcSTejun Heo * state if some pools failed to make forward progress for a while where 536882607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 536982607adcSTejun Heo * 537082607adcSTejun Heo * This mechanism is controlled through the kernel parameter 537182607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 537282607adcSTejun Heo * corresponding sysfs parameter file. 537382607adcSTejun Heo */ 537482607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 537582607adcSTejun Heo 537682607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 53775cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 537882607adcSTejun Heo 537982607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 538082607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 538182607adcSTejun Heo 538282607adcSTejun Heo static void wq_watchdog_reset_touched(void) 538382607adcSTejun Heo { 538482607adcSTejun Heo int cpu; 538582607adcSTejun Heo 538682607adcSTejun Heo wq_watchdog_touched = jiffies; 538782607adcSTejun Heo for_each_possible_cpu(cpu) 538882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 538982607adcSTejun Heo } 539082607adcSTejun Heo 53915cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 539282607adcSTejun Heo { 539382607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 539482607adcSTejun Heo bool lockup_detected = false; 539582607adcSTejun Heo struct worker_pool *pool; 539682607adcSTejun Heo int pi; 539782607adcSTejun Heo 539882607adcSTejun Heo if (!thresh) 539982607adcSTejun Heo return; 540082607adcSTejun Heo 540182607adcSTejun Heo rcu_read_lock(); 540282607adcSTejun Heo 540382607adcSTejun Heo for_each_pool(pool, pi) { 540482607adcSTejun Heo unsigned long pool_ts, touched, ts; 540582607adcSTejun Heo 540682607adcSTejun Heo if (list_empty(&pool->worklist)) 540782607adcSTejun Heo continue; 540882607adcSTejun Heo 540982607adcSTejun Heo /* get the latest of pool and touched timestamps */ 541082607adcSTejun Heo pool_ts = READ_ONCE(pool->watchdog_ts); 541182607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 541282607adcSTejun Heo 541382607adcSTejun Heo if (time_after(pool_ts, touched)) 541482607adcSTejun Heo ts = pool_ts; 541582607adcSTejun Heo else 541682607adcSTejun Heo ts = touched; 541782607adcSTejun Heo 541882607adcSTejun Heo if (pool->cpu >= 0) { 541982607adcSTejun Heo unsigned long cpu_touched = 542082607adcSTejun Heo READ_ONCE(per_cpu(wq_watchdog_touched_cpu, 542182607adcSTejun Heo pool->cpu)); 542282607adcSTejun Heo if (time_after(cpu_touched, ts)) 542382607adcSTejun Heo ts = cpu_touched; 542482607adcSTejun Heo } 542582607adcSTejun Heo 542682607adcSTejun Heo /* did we stall? */ 542782607adcSTejun Heo if (time_after(jiffies, ts + thresh)) { 542882607adcSTejun Heo lockup_detected = true; 542982607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 543082607adcSTejun Heo pr_cont_pool_info(pool); 543182607adcSTejun Heo pr_cont(" stuck for %us!\n", 543282607adcSTejun Heo jiffies_to_msecs(jiffies - pool_ts) / 1000); 543382607adcSTejun Heo } 543482607adcSTejun Heo } 543582607adcSTejun Heo 543682607adcSTejun Heo rcu_read_unlock(); 543782607adcSTejun Heo 543882607adcSTejun Heo if (lockup_detected) 543982607adcSTejun Heo show_workqueue_state(); 544082607adcSTejun Heo 544182607adcSTejun Heo wq_watchdog_reset_touched(); 544282607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 544382607adcSTejun Heo } 544482607adcSTejun Heo 544582607adcSTejun Heo void wq_watchdog_touch(int cpu) 544682607adcSTejun Heo { 544782607adcSTejun Heo if (cpu >= 0) 544882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 544982607adcSTejun Heo else 545082607adcSTejun Heo wq_watchdog_touched = jiffies; 545182607adcSTejun Heo } 545282607adcSTejun Heo 545382607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 545482607adcSTejun Heo { 545582607adcSTejun Heo wq_watchdog_thresh = 0; 545682607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 545782607adcSTejun Heo 545882607adcSTejun Heo if (thresh) { 545982607adcSTejun Heo wq_watchdog_thresh = thresh; 546082607adcSTejun Heo wq_watchdog_reset_touched(); 546182607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 546282607adcSTejun Heo } 546382607adcSTejun Heo } 546482607adcSTejun Heo 546582607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 546682607adcSTejun Heo const struct kernel_param *kp) 546782607adcSTejun Heo { 546882607adcSTejun Heo unsigned long thresh; 546982607adcSTejun Heo int ret; 547082607adcSTejun Heo 547182607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 547282607adcSTejun Heo if (ret) 547382607adcSTejun Heo return ret; 547482607adcSTejun Heo 547582607adcSTejun Heo if (system_wq) 547682607adcSTejun Heo wq_watchdog_set_thresh(thresh); 547782607adcSTejun Heo else 547882607adcSTejun Heo wq_watchdog_thresh = thresh; 547982607adcSTejun Heo 548082607adcSTejun Heo return 0; 548182607adcSTejun Heo } 548282607adcSTejun Heo 548382607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 548482607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 548582607adcSTejun Heo .get = param_get_ulong, 548682607adcSTejun Heo }; 548782607adcSTejun Heo 548882607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 548982607adcSTejun Heo 0644); 549082607adcSTejun Heo 549182607adcSTejun Heo static void wq_watchdog_init(void) 549282607adcSTejun Heo { 54935cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 549482607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 549582607adcSTejun Heo } 549682607adcSTejun Heo 549782607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 549882607adcSTejun Heo 549982607adcSTejun Heo static inline void wq_watchdog_init(void) { } 550082607adcSTejun Heo 550182607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 550282607adcSTejun Heo 5503bce90380STejun Heo static void __init wq_numa_init(void) 5504bce90380STejun Heo { 5505bce90380STejun Heo cpumask_var_t *tbl; 5506bce90380STejun Heo int node, cpu; 5507bce90380STejun Heo 5508bce90380STejun Heo if (num_possible_nodes() <= 1) 5509bce90380STejun Heo return; 5510bce90380STejun Heo 5511d55262c4STejun Heo if (wq_disable_numa) { 5512d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 5513d55262c4STejun Heo return; 5514d55262c4STejun Heo } 5515d55262c4STejun Heo 55164c16bd32STejun Heo wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL); 55174c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 55184c16bd32STejun Heo 5519bce90380STejun Heo /* 5520bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 5521bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 5522bce90380STejun Heo * fully initialized by now. 5523bce90380STejun Heo */ 5524ddcb57e2SLai Jiangshan tbl = kzalloc(nr_node_ids * sizeof(tbl[0]), GFP_KERNEL); 5525bce90380STejun Heo BUG_ON(!tbl); 5526bce90380STejun Heo 5527bce90380STejun Heo for_each_node(node) 55285a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 55291be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 5530bce90380STejun Heo 5531bce90380STejun Heo for_each_possible_cpu(cpu) { 5532bce90380STejun Heo node = cpu_to_node(cpu); 5533bce90380STejun Heo if (WARN_ON(node == NUMA_NO_NODE)) { 5534bce90380STejun Heo pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 5535bce90380STejun Heo /* happens iff arch is bonkers, let's just proceed */ 5536bce90380STejun Heo return; 5537bce90380STejun Heo } 5538bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 5539bce90380STejun Heo } 5540bce90380STejun Heo 5541bce90380STejun Heo wq_numa_possible_cpumask = tbl; 5542bce90380STejun Heo wq_numa_enabled = true; 5543bce90380STejun Heo } 5544bce90380STejun Heo 55453347fa09STejun Heo /** 55463347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 55473347fa09STejun Heo * 55483347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 55493347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 55503347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 55513347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 55523347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 55533347fa09STejun Heo * created and scheduled right before early initcalls. 55543347fa09STejun Heo */ 55553347fa09STejun Heo int __init workqueue_init_early(void) 55561da177e4SLinus Torvalds { 55577a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 55587a4e344cSTejun Heo int i, cpu; 5559c34056a3STejun Heo 5560e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 5561e904e6c2STejun Heo 5562b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 5563c98a9805STal Shorer cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_FLAG_DOMAIN)); 5564b05a7928SFrederic Weisbecker 5565e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 5566e904e6c2STejun Heo 5567706026c2STejun Heo /* initialize CPU pools */ 556829c91e99STejun Heo for_each_possible_cpu(cpu) { 55694ce62e9eSTejun Heo struct worker_pool *pool; 55708b03ae3cSTejun Heo 55717a4e344cSTejun Heo i = 0; 5572f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 55737a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 5574ec22ca5eSTejun Heo pool->cpu = cpu; 55757a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 55767a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 5577f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 55787a4e344cSTejun Heo 55799daf9e67STejun Heo /* alloc pool ID */ 558068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 55819daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 558268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55834ce62e9eSTejun Heo } 55848b03ae3cSTejun Heo } 55858b03ae3cSTejun Heo 55868a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 558729c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 558829c91e99STejun Heo struct workqueue_attrs *attrs; 558929c91e99STejun Heo 559029c91e99STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 559129c91e99STejun Heo attrs->nice = std_nice[i]; 559229c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 55938a2b7538STejun Heo 55948a2b7538STejun Heo /* 55958a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 55968a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 55978a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 55988a2b7538STejun Heo */ 55998a2b7538STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 56008a2b7538STejun Heo attrs->nice = std_nice[i]; 56018a2b7538STejun Heo attrs->no_numa = true; 56028a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 560329c91e99STejun Heo } 560429c91e99STejun Heo 5605d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 56061aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 5607d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 5608f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 5609f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 561024d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 561124d51addSTejun Heo WQ_FREEZABLE, 0); 56120668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 56130668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 56140668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 56150668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 56160668106cSViresh Kumar 0); 56171aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 56180668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 56190668106cSViresh Kumar !system_power_efficient_wq || 56200668106cSViresh Kumar !system_freezable_power_efficient_wq); 562182607adcSTejun Heo 56223347fa09STejun Heo return 0; 56233347fa09STejun Heo } 56243347fa09STejun Heo 56253347fa09STejun Heo /** 56263347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 56273347fa09STejun Heo * 56283347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 56293347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 56303347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 56313347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 56323347fa09STejun Heo * with the initial workers and enable future kworker creations. 56333347fa09STejun Heo */ 56343347fa09STejun Heo int __init workqueue_init(void) 56353347fa09STejun Heo { 56362186d9f9STejun Heo struct workqueue_struct *wq; 56373347fa09STejun Heo struct worker_pool *pool; 56383347fa09STejun Heo int cpu, bkt; 56393347fa09STejun Heo 56402186d9f9STejun Heo /* 56412186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 56422186d9f9STejun Heo * CPU to node mapping may not be available that early on some 56432186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 56442186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 56452186d9f9STejun Heo * affinity, fix them up. 56462186d9f9STejun Heo */ 56472186d9f9STejun Heo wq_numa_init(); 56482186d9f9STejun Heo 56492186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 56502186d9f9STejun Heo 56512186d9f9STejun Heo for_each_possible_cpu(cpu) { 56522186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 56532186d9f9STejun Heo pool->node = cpu_to_node(cpu); 56542186d9f9STejun Heo } 56552186d9f9STejun Heo } 56562186d9f9STejun Heo 56572186d9f9STejun Heo list_for_each_entry(wq, &workqueues, list) 56582186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 56592186d9f9STejun Heo 56602186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 56612186d9f9STejun Heo 56623347fa09STejun Heo /* create the initial workers */ 56633347fa09STejun Heo for_each_online_cpu(cpu) { 56643347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 56653347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 56663347fa09STejun Heo BUG_ON(!create_worker(pool)); 56673347fa09STejun Heo } 56683347fa09STejun Heo } 56693347fa09STejun Heo 56703347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 56713347fa09STejun Heo BUG_ON(!create_worker(pool)); 56723347fa09STejun Heo 56733347fa09STejun Heo wq_online = true; 567482607adcSTejun Heo wq_watchdog_init(); 567582607adcSTejun Heo 56766ee0578bSSuresh Siddha return 0; 56771da177e4SLinus Torvalds } 5678