11da177e4SLinus Torvalds /* 2c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 31da177e4SLinus Torvalds * 4c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 71da177e4SLinus Torvalds * David Woodhouse <[email protected]> 8e1f8e874SFrancois Cami * Andrew Morton 91da177e4SLinus Torvalds * Kai Petzke <[email protected]> 101da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1189ada679SChristoph Lameter * 12cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 13c54fce6eSTejun Heo * 14c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 15c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 16c54fce6eSTejun Heo * 17c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 18c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 19b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 20b11895c4SLibin * normal work items and the other for high priority ones) and some extra 21b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 22b11895c4SLibin * number of these backing pools is dynamic. 23c54fce6eSTejun Heo * 24c54fce6eSTejun Heo * Please read Documentation/workqueue.txt for details. 251da177e4SLinus Torvalds */ 261da177e4SLinus Torvalds 279984de1aSPaul Gortmaker #include <linux/export.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 291da177e4SLinus Torvalds #include <linux/sched.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/signal.h> 321da177e4SLinus Torvalds #include <linux/completion.h> 331da177e4SLinus Torvalds #include <linux/workqueue.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/cpu.h> 361da177e4SLinus Torvalds #include <linux/notifier.h> 371da177e4SLinus Torvalds #include <linux/kthread.h> 381fa44ecaSJames Bottomley #include <linux/hardirq.h> 3946934023SChristoph Lameter #include <linux/mempolicy.h> 40341a5958SRafael J. Wysocki #include <linux/freezer.h> 41d5abe669SPeter Zijlstra #include <linux/kallsyms.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51e22bee78STejun Heo 52ea138446STejun Heo #include "workqueue_internal.h" 531da177e4SLinus Torvalds 54c8e55f36STejun Heo enum { 55bc2ae0f5STejun Heo /* 5624647570STejun Heo * worker_pool flags 57bc2ae0f5STejun Heo * 5824647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 59bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 60bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 61bc2ae0f5STejun Heo * is in effect. 62bc2ae0f5STejun Heo * 63bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 64bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6524647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 66bc2ae0f5STejun Heo * 67bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 6892f9c5c4SLai Jiangshan * attach_mutex to avoid changing binding state while 694736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 70bc2ae0f5STejun Heo */ 7124647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 72db7bccf4STejun Heo 73c8e55f36STejun Heo /* worker flags */ 74c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 75c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 76e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 77fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 78f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 79a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 80e22bee78STejun Heo 81a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 82a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 83db7bccf4STejun Heo 84e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 854ce62e9eSTejun Heo 8629c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 87c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 88db7bccf4STejun Heo 89e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 90e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 91e22bee78STejun Heo 923233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 933233cdbdSTejun Heo /* call for help after 10ms 943233cdbdSTejun Heo (min two ticks) */ 95e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 96e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 99e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1008698a745SDongsheng Yang * all cpus. Give MIN_NICE. 101e22bee78STejun Heo */ 1028698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1038698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 104ecf6881fSTejun Heo 105ecf6881fSTejun Heo WQ_NAME_LEN = 24, 106c8e55f36STejun Heo }; 107c8e55f36STejun Heo 1081da177e4SLinus Torvalds /* 1094690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1104690c4abSTejun Heo * 111e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 112e41e704bSTejun Heo * everyone else. 1134690c4abSTejun Heo * 114e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 115e22bee78STejun Heo * only be modified and accessed from the local cpu. 116e22bee78STejun Heo * 117d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1184690c4abSTejun Heo * 119d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 120d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 121d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 122d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 123e22bee78STejun Heo * 12492f9c5c4SLai Jiangshan * A: pool->attach_mutex protected. 125822d8405STejun Heo * 12668e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 12776af4d93STejun Heo * 12868e13a67SLai Jiangshan * PR: wq_pool_mutex protected for writes. Sched-RCU protected for reads. 1295bcab335STejun Heo * 1305b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1315b95e1afSLai Jiangshan * 1325b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 1335b95e1afSLai Jiangshan * sched-RCU for reads. 1345b95e1afSLai Jiangshan * 1353c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1363c25a55dSLai Jiangshan * 137b5927605SLai Jiangshan * WR: wq->mutex protected for writes. Sched-RCU protected for reads. 1382e109a28STejun Heo * 1392e109a28STejun Heo * MD: wq_mayday_lock protected. 1404690c4abSTejun Heo */ 1414690c4abSTejun Heo 1422eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 143c34056a3STejun Heo 144bd7bdd43STejun Heo struct worker_pool { 145d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 146d84ff051STejun Heo int cpu; /* I: the associated cpu */ 147f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1489daf9e67STejun Heo int id; /* I: pool ID */ 14911ebea50STejun Heo unsigned int flags; /* X: flags */ 150bd7bdd43STejun Heo 15182607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 15282607adcSTejun Heo 153bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 154bd7bdd43STejun Heo int nr_workers; /* L: total number of workers */ 155ea1abd61SLai Jiangshan 156ea1abd61SLai Jiangshan /* nr_idle includes the ones off idle_list for rebinding */ 157bd7bdd43STejun Heo int nr_idle; /* L: currently idle ones */ 158bd7bdd43STejun Heo 159bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 160bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 161bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 162bd7bdd43STejun Heo 163c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 164c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 165c9e7cf27STejun Heo /* L: hash of busy workers */ 166c9e7cf27STejun Heo 167bc3a1afcSTejun Heo /* see manage_workers() for details on the two manager mutexes */ 16834a06bd6STejun Heo struct mutex manager_arb; /* manager arbitration */ 1692607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 17092f9c5c4SLai Jiangshan struct mutex attach_mutex; /* attach/detach exclusion */ 17192f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 17260f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 173e19e397aSTejun Heo 1747cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 175e19e397aSTejun Heo 1767a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 17768e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 17868e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1797a4e344cSTejun Heo 180e19e397aSTejun Heo /* 181e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 182e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 183e19e397aSTejun Heo * cacheline. 184e19e397aSTejun Heo */ 185e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 18629c91e99STejun Heo 18729c91e99STejun Heo /* 18829c91e99STejun Heo * Destruction of pool is sched-RCU protected to allow dereferences 18929c91e99STejun Heo * from get_work_pool(). 19029c91e99STejun Heo */ 19129c91e99STejun Heo struct rcu_head rcu; 1928b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1938b03ae3cSTejun Heo 1948b03ae3cSTejun Heo /* 195112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 196112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 197112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 198112202d9STejun Heo * number of flag bits. 1991da177e4SLinus Torvalds */ 200112202d9STejun Heo struct pool_workqueue { 201bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2024690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 20373f53c4aSTejun Heo int work_color; /* L: current color */ 20473f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2058864b4e5STejun Heo int refcnt; /* L: reference count */ 20673f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20773f53c4aSTejun Heo /* L: nr of in_flight works */ 2081e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 209a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2101e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2113c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2122e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2138864b4e5STejun Heo 2148864b4e5STejun Heo /* 2158864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2168864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 2178864b4e5STejun Heo * itself is also sched-RCU protected so that the first pwq can be 218b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2198864b4e5STejun Heo */ 2208864b4e5STejun Heo struct work_struct unbound_release_work; 2218864b4e5STejun Heo struct rcu_head rcu; 222e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds /* 22573f53c4aSTejun Heo * Structure used to wait for workqueue flush. 22673f53c4aSTejun Heo */ 22773f53c4aSTejun Heo struct wq_flusher { 2283c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2293c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 23073f53c4aSTejun Heo struct completion done; /* flush completion */ 23173f53c4aSTejun Heo }; 2321da177e4SLinus Torvalds 233226223abSTejun Heo struct wq_device; 234226223abSTejun Heo 23573f53c4aSTejun Heo /* 236c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 237c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2381da177e4SLinus Torvalds */ 2391da177e4SLinus Torvalds struct workqueue_struct { 2403c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 241e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 24273f53c4aSTejun Heo 2433c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2443c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2453c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 246112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2473c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2483c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2493c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 25073f53c4aSTejun Heo 2512e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 252e22bee78STejun Heo struct worker *rescuer; /* I: rescue worker */ 253e22bee78STejun Heo 25487fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 255a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 256226223abSTejun Heo 2575b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 2585b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 2596029a918STejun Heo 260226223abSTejun Heo #ifdef CONFIG_SYSFS 261226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 262226223abSTejun Heo #endif 2634e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 2644e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2654e6045f1SJohannes Berg #endif 266ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2672728fd2fSTejun Heo 268e2dca7adSTejun Heo /* 269e2dca7adSTejun Heo * Destruction of workqueue_struct is sched-RCU protected to allow 270e2dca7adSTejun Heo * walking the workqueues list without grabbing wq_pool_mutex. 271e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 272e2dca7adSTejun Heo */ 273e2dca7adSTejun Heo struct rcu_head rcu; 274e2dca7adSTejun Heo 2752728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 2762728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 2772728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 2785b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 2791da177e4SLinus Torvalds }; 2801da177e4SLinus Torvalds 281e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 282e904e6c2STejun Heo 283bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 284bce90380STejun Heo /* possible CPUs of each node */ 285bce90380STejun Heo 286d55262c4STejun Heo static bool wq_disable_numa; 287d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 288d55262c4STejun Heo 289cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 290552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 291cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 292cee22a15SViresh Kumar 293bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 294bce90380STejun Heo 2954c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 2964c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 2974c16bd32STejun Heo 29868e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 2992e109a28STejun Heo static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 3005bcab335STejun Heo 301e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 30268e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3037d19c5ceSTejun Heo 304ef557180SMike Galbraith /* PL: allowable cpus for unbound wqs and work items */ 305ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 306ef557180SMike Galbraith 307ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 308ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 309b05a7928SFrederic Weisbecker 310*f303fccbSTejun Heo /* 311*f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 312*f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 313*f303fccbSTejun Heo * to uncover usages which depend on it. 314*f303fccbSTejun Heo */ 315*f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 316*f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 317*f303fccbSTejun Heo #else 318*f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 319*f303fccbSTejun Heo #endif 320*f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 321*f303fccbSTejun Heo 3227d19c5ceSTejun Heo /* the per-cpu worker pools */ 3237d19c5ceSTejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 3247d19c5ceSTejun Heo cpu_worker_pools); 3257d19c5ceSTejun Heo 32668e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3277d19c5ceSTejun Heo 32868e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 32929c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 33029c91e99STejun Heo 331c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 33229c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 33329c91e99STejun Heo 3348a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3358a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3368a2b7538STejun Heo 337d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 338ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 339044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3401aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 341044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 342d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 343044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 344f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 345044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 34624d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3470668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3480668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3490668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3500668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 351d320c038STejun Heo 3527d19c5ceSTejun Heo static int worker_thread(void *__worker); 3536ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 3547d19c5ceSTejun Heo 35597bd2347STejun Heo #define CREATE_TRACE_POINTS 35697bd2347STejun Heo #include <trace/events/workqueue.h> 35797bd2347STejun Heo 35868e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 359f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 360f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 36168e13a67SLai Jiangshan "sched RCU or wq_pool_mutex should be held") 3625bcab335STejun Heo 363b09f4fd3SLai Jiangshan #define assert_rcu_or_wq_mutex(wq) \ 364f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 365f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex), \ 366b09f4fd3SLai Jiangshan "sched RCU or wq->mutex should be held") 36776af4d93STejun Heo 3685b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 369f78f5b90SPaul E. McKenney RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held() && \ 370f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 371f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 3725b95e1afSLai Jiangshan "sched RCU, wq->mutex or wq_pool_mutex should be held") 3735b95e1afSLai Jiangshan 374f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 375f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 376f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 3777a62c2c8STejun Heo (pool)++) 3784ce62e9eSTejun Heo 37949e3cf44STejun Heo /** 38017116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 38117116969STejun Heo * @pool: iteration cursor 382611c92a0STejun Heo * @pi: integer used for iteration 383fa1b54e6STejun Heo * 38468e13a67SLai Jiangshan * This must be called either with wq_pool_mutex held or sched RCU read 38568e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 38668e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 387fa1b54e6STejun Heo * 388fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 389fa1b54e6STejun Heo * ignored. 39017116969STejun Heo */ 391611c92a0STejun Heo #define for_each_pool(pool, pi) \ 392611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 39368e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 394fa1b54e6STejun Heo else 39517116969STejun Heo 39617116969STejun Heo /** 397822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 398822d8405STejun Heo * @worker: iteration cursor 399822d8405STejun Heo * @pool: worker_pool to iterate workers of 400822d8405STejun Heo * 40192f9c5c4SLai Jiangshan * This must be called with @pool->attach_mutex. 402822d8405STejun Heo * 403822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 404822d8405STejun Heo * ignored. 405822d8405STejun Heo */ 406da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 407da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 40892f9c5c4SLai Jiangshan if (({ lockdep_assert_held(&pool->attach_mutex); false; })) { } \ 409822d8405STejun Heo else 410822d8405STejun Heo 411822d8405STejun Heo /** 41249e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 41349e3cf44STejun Heo * @pwq: iteration cursor 41449e3cf44STejun Heo * @wq: the target workqueue 41576af4d93STejun Heo * 416b09f4fd3SLai Jiangshan * This must be called either with wq->mutex held or sched RCU read locked. 417794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 418794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 41976af4d93STejun Heo * 42076af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 42176af4d93STejun Heo * ignored. 42249e3cf44STejun Heo */ 42349e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 42476af4d93STejun Heo list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node) \ 425b09f4fd3SLai Jiangshan if (({ assert_rcu_or_wq_mutex(wq); false; })) { } \ 42676af4d93STejun Heo else 427f3421797STejun Heo 428dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 429dc186ad7SThomas Gleixner 430dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 431dc186ad7SThomas Gleixner 43299777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 43399777288SStanislaw Gruszka { 43499777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 43599777288SStanislaw Gruszka } 43699777288SStanislaw Gruszka 437dc186ad7SThomas Gleixner /* 438dc186ad7SThomas Gleixner * fixup_init is called when: 439dc186ad7SThomas Gleixner * - an active object is initialized 440dc186ad7SThomas Gleixner */ 441dc186ad7SThomas Gleixner static int work_fixup_init(void *addr, enum debug_obj_state state) 442dc186ad7SThomas Gleixner { 443dc186ad7SThomas Gleixner struct work_struct *work = addr; 444dc186ad7SThomas Gleixner 445dc186ad7SThomas Gleixner switch (state) { 446dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 447dc186ad7SThomas Gleixner cancel_work_sync(work); 448dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 449dc186ad7SThomas Gleixner return 1; 450dc186ad7SThomas Gleixner default: 451dc186ad7SThomas Gleixner return 0; 452dc186ad7SThomas Gleixner } 453dc186ad7SThomas Gleixner } 454dc186ad7SThomas Gleixner 455dc186ad7SThomas Gleixner /* 456dc186ad7SThomas Gleixner * fixup_activate is called when: 457dc186ad7SThomas Gleixner * - an active object is activated 458dc186ad7SThomas Gleixner * - an unknown object is activated (might be a statically initialized object) 459dc186ad7SThomas Gleixner */ 460dc186ad7SThomas Gleixner static int work_fixup_activate(void *addr, enum debug_obj_state state) 461dc186ad7SThomas Gleixner { 462dc186ad7SThomas Gleixner struct work_struct *work = addr; 463dc186ad7SThomas Gleixner 464dc186ad7SThomas Gleixner switch (state) { 465dc186ad7SThomas Gleixner 466dc186ad7SThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 467dc186ad7SThomas Gleixner /* 468dc186ad7SThomas Gleixner * This is not really a fixup. The work struct was 469dc186ad7SThomas Gleixner * statically initialized. We just make sure that it 470dc186ad7SThomas Gleixner * is tracked in the object tracker. 471dc186ad7SThomas Gleixner */ 47222df02bbSTejun Heo if (test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work))) { 473dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 474dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 475dc186ad7SThomas Gleixner return 0; 476dc186ad7SThomas Gleixner } 477dc186ad7SThomas Gleixner WARN_ON_ONCE(1); 478dc186ad7SThomas Gleixner return 0; 479dc186ad7SThomas Gleixner 480dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 481dc186ad7SThomas Gleixner WARN_ON(1); 482dc186ad7SThomas Gleixner 483dc186ad7SThomas Gleixner default: 484dc186ad7SThomas Gleixner return 0; 485dc186ad7SThomas Gleixner } 486dc186ad7SThomas Gleixner } 487dc186ad7SThomas Gleixner 488dc186ad7SThomas Gleixner /* 489dc186ad7SThomas Gleixner * fixup_free is called when: 490dc186ad7SThomas Gleixner * - an active object is freed 491dc186ad7SThomas Gleixner */ 492dc186ad7SThomas Gleixner static int work_fixup_free(void *addr, enum debug_obj_state state) 493dc186ad7SThomas Gleixner { 494dc186ad7SThomas Gleixner struct work_struct *work = addr; 495dc186ad7SThomas Gleixner 496dc186ad7SThomas Gleixner switch (state) { 497dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 498dc186ad7SThomas Gleixner cancel_work_sync(work); 499dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 500dc186ad7SThomas Gleixner return 1; 501dc186ad7SThomas Gleixner default: 502dc186ad7SThomas Gleixner return 0; 503dc186ad7SThomas Gleixner } 504dc186ad7SThomas Gleixner } 505dc186ad7SThomas Gleixner 506dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 507dc186ad7SThomas Gleixner .name = "work_struct", 50899777288SStanislaw Gruszka .debug_hint = work_debug_hint, 509dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 510dc186ad7SThomas Gleixner .fixup_activate = work_fixup_activate, 511dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 512dc186ad7SThomas Gleixner }; 513dc186ad7SThomas Gleixner 514dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 515dc186ad7SThomas Gleixner { 516dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 517dc186ad7SThomas Gleixner } 518dc186ad7SThomas Gleixner 519dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 520dc186ad7SThomas Gleixner { 521dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 522dc186ad7SThomas Gleixner } 523dc186ad7SThomas Gleixner 524dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 525dc186ad7SThomas Gleixner { 526dc186ad7SThomas Gleixner if (onstack) 527dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 528dc186ad7SThomas Gleixner else 529dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 530dc186ad7SThomas Gleixner } 531dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 532dc186ad7SThomas Gleixner 533dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 534dc186ad7SThomas Gleixner { 535dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 536dc186ad7SThomas Gleixner } 537dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 538dc186ad7SThomas Gleixner 539ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 540ea2e64f2SThomas Gleixner { 541ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 542ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 543ea2e64f2SThomas Gleixner } 544ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 545ea2e64f2SThomas Gleixner 546dc186ad7SThomas Gleixner #else 547dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 548dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 549dc186ad7SThomas Gleixner #endif 550dc186ad7SThomas Gleixner 5514e8b22bdSLi Bin /** 5524e8b22bdSLi Bin * worker_pool_assign_id - allocate ID and assing it to @pool 5534e8b22bdSLi Bin * @pool: the pool pointer of interest 5544e8b22bdSLi Bin * 5554e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5564e8b22bdSLi Bin * successfully, -errno on failure. 5574e8b22bdSLi Bin */ 5589daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5599daf9e67STejun Heo { 5609daf9e67STejun Heo int ret; 5619daf9e67STejun Heo 56268e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5635bcab335STejun Heo 5644e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5654e8b22bdSLi Bin GFP_KERNEL); 566229641a6STejun Heo if (ret >= 0) { 567e68035fbSTejun Heo pool->id = ret; 568229641a6STejun Heo return 0; 569229641a6STejun Heo } 5709daf9e67STejun Heo return ret; 5719daf9e67STejun Heo } 5729daf9e67STejun Heo 57376af4d93STejun Heo /** 574df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 575df2d5ae4STejun Heo * @wq: the target workqueue 576df2d5ae4STejun Heo * @node: the node ID 577df2d5ae4STejun Heo * 5785b95e1afSLai Jiangshan * This must be called with any of wq_pool_mutex, wq->mutex or sched RCU 5795b95e1afSLai Jiangshan * read locked. 580df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 581df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 582d185af30SYacine Belkadi * 583d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 584df2d5ae4STejun Heo */ 585df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 586df2d5ae4STejun Heo int node) 587df2d5ae4STejun Heo { 5885b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 589df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 590df2d5ae4STejun Heo } 591df2d5ae4STejun Heo 59273f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 59373f53c4aSTejun Heo { 59473f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 59573f53c4aSTejun Heo } 59673f53c4aSTejun Heo 59773f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 59873f53c4aSTejun Heo { 59973f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 60073f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 60173f53c4aSTejun Heo } 60273f53c4aSTejun Heo 60373f53c4aSTejun Heo static int work_next_color(int color) 60473f53c4aSTejun Heo { 60573f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 606a848e3b6SOleg Nesterov } 607a848e3b6SOleg Nesterov 6084594bf15SDavid Howells /* 609112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 610112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6117c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6127a22ad75STejun Heo * 613112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 614112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 615bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 616bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6177a22ad75STejun Heo * 618112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6197c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 620112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6217c3eed5cSTejun Heo * available only while the work item is queued. 622bbb68dfaSTejun Heo * 623bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 624bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 625bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 626bbb68dfaSTejun Heo * try to steal the PENDING bit. 6274594bf15SDavid Howells */ 6287a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6297a22ad75STejun Heo unsigned long flags) 6307a22ad75STejun Heo { 6316183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6327a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6337a22ad75STejun Heo } 6347a22ad75STejun Heo 635112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6364690c4abSTejun Heo unsigned long extra_flags) 637365970a1SDavid Howells { 638112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 639112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 640365970a1SDavid Howells } 641365970a1SDavid Howells 6424468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6434468a00fSLai Jiangshan int pool_id) 6444468a00fSLai Jiangshan { 6454468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6464468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6474468a00fSLai Jiangshan } 6484468a00fSLai Jiangshan 6497c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6507c3eed5cSTejun Heo int pool_id) 6514d707b9fSOleg Nesterov { 65223657bb1STejun Heo /* 65323657bb1STejun Heo * The following wmb is paired with the implied mb in 65423657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 65523657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 65623657bb1STejun Heo * owner. 65723657bb1STejun Heo */ 65823657bb1STejun Heo smp_wmb(); 6597c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 6604d707b9fSOleg Nesterov } 6614d707b9fSOleg Nesterov 6627a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 663365970a1SDavid Howells { 6647c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 6657c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 6667a22ad75STejun Heo } 6677a22ad75STejun Heo 668112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 6697a22ad75STejun Heo { 670e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6717a22ad75STejun Heo 672112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 673e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 674e120153dSTejun Heo else 675e120153dSTejun Heo return NULL; 6767a22ad75STejun Heo } 6777a22ad75STejun Heo 6787c3eed5cSTejun Heo /** 6797c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 6807c3eed5cSTejun Heo * @work: the work item of interest 6817c3eed5cSTejun Heo * 68268e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 68368e13a67SLai Jiangshan * access under sched-RCU read lock. As such, this function should be 68468e13a67SLai Jiangshan * called under wq_pool_mutex or with preemption disabled. 685fa1b54e6STejun Heo * 686fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 687fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 688fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 689fa1b54e6STejun Heo * returned pool is and stays online. 690d185af30SYacine Belkadi * 691d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 6927c3eed5cSTejun Heo */ 6937c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 6947a22ad75STejun Heo { 695e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6967c3eed5cSTejun Heo int pool_id; 6977a22ad75STejun Heo 69868e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 699fa1b54e6STejun Heo 700112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 701112202d9STejun Heo return ((struct pool_workqueue *) 7027c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7037a22ad75STejun Heo 7047c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7057c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7067a22ad75STejun Heo return NULL; 7077a22ad75STejun Heo 708fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7097c3eed5cSTejun Heo } 7107c3eed5cSTejun Heo 7117c3eed5cSTejun Heo /** 7127c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7137c3eed5cSTejun Heo * @work: the work item of interest 7147c3eed5cSTejun Heo * 715d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7167c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7177c3eed5cSTejun Heo */ 7187c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7197c3eed5cSTejun Heo { 72054d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7217c3eed5cSTejun Heo 722112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 723112202d9STejun Heo return ((struct pool_workqueue *) 72454d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 72554d5b7d0SLai Jiangshan 72654d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7277c3eed5cSTejun Heo } 7287c3eed5cSTejun Heo 729bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 730bbb68dfaSTejun Heo { 7317c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 732bbb68dfaSTejun Heo 7337c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7347c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 735bbb68dfaSTejun Heo } 736bbb68dfaSTejun Heo 737bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 738bbb68dfaSTejun Heo { 739bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 740bbb68dfaSTejun Heo 741112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 742bbb68dfaSTejun Heo } 743bbb68dfaSTejun Heo 744e22bee78STejun Heo /* 7453270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7463270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 747d565ed63STejun Heo * they're being called with pool->lock held. 748e22bee78STejun Heo */ 749e22bee78STejun Heo 75063d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 751649027d7STejun Heo { 752e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 753649027d7STejun Heo } 754649027d7STejun Heo 755e22bee78STejun Heo /* 756e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 757e22bee78STejun Heo * running workers. 758974271c4STejun Heo * 759974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 760706026c2STejun Heo * function will always return %true for unbound pools as long as the 761974271c4STejun Heo * worklist isn't empty. 762e22bee78STejun Heo */ 76363d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 764e22bee78STejun Heo { 76563d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 766e22bee78STejun Heo } 767e22bee78STejun Heo 768e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 76963d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 770e22bee78STejun Heo { 77163d95a91STejun Heo return pool->nr_idle; 772e22bee78STejun Heo } 773e22bee78STejun Heo 774e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 77563d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 776e22bee78STejun Heo { 777e19e397aSTejun Heo return !list_empty(&pool->worklist) && 778e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 779e22bee78STejun Heo } 780e22bee78STejun Heo 781e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 78263d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 783e22bee78STejun Heo { 78463d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 785e22bee78STejun Heo } 786e22bee78STejun Heo 787e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 78863d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 789e22bee78STejun Heo { 79034a06bd6STejun Heo bool managing = mutex_is_locked(&pool->manager_arb); 79163d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 79263d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 793e22bee78STejun Heo 794e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 795e22bee78STejun Heo } 796e22bee78STejun Heo 797e22bee78STejun Heo /* 798e22bee78STejun Heo * Wake up functions. 799e22bee78STejun Heo */ 800e22bee78STejun Heo 8011037de36SLai Jiangshan /* Return the first idle worker. Safe with preemption disabled */ 8021037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8037e11629dSTejun Heo { 80463d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8057e11629dSTejun Heo return NULL; 8067e11629dSTejun Heo 80763d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8087e11629dSTejun Heo } 8097e11629dSTejun Heo 8107e11629dSTejun Heo /** 8117e11629dSTejun Heo * wake_up_worker - wake up an idle worker 81263d95a91STejun Heo * @pool: worker pool to wake worker from 8137e11629dSTejun Heo * 81463d95a91STejun Heo * Wake up the first idle worker of @pool. 8157e11629dSTejun Heo * 8167e11629dSTejun Heo * CONTEXT: 817d565ed63STejun Heo * spin_lock_irq(pool->lock). 8187e11629dSTejun Heo */ 81963d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8207e11629dSTejun Heo { 8211037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8227e11629dSTejun Heo 8237e11629dSTejun Heo if (likely(worker)) 8247e11629dSTejun Heo wake_up_process(worker->task); 8257e11629dSTejun Heo } 8267e11629dSTejun Heo 8274690c4abSTejun Heo /** 828e22bee78STejun Heo * wq_worker_waking_up - a worker is waking up 829e22bee78STejun Heo * @task: task waking up 830e22bee78STejun Heo * @cpu: CPU @task is waking up to 831e22bee78STejun Heo * 832e22bee78STejun Heo * This function is called during try_to_wake_up() when a worker is 833e22bee78STejun Heo * being awoken. 834e22bee78STejun Heo * 835e22bee78STejun Heo * CONTEXT: 836e22bee78STejun Heo * spin_lock_irq(rq->lock) 837e22bee78STejun Heo */ 838d84ff051STejun Heo void wq_worker_waking_up(struct task_struct *task, int cpu) 839e22bee78STejun Heo { 840e22bee78STejun Heo struct worker *worker = kthread_data(task); 841e22bee78STejun Heo 84236576000SJoonsoo Kim if (!(worker->flags & WORKER_NOT_RUNNING)) { 843ec22ca5eSTejun Heo WARN_ON_ONCE(worker->pool->cpu != cpu); 844e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 845e22bee78STejun Heo } 84636576000SJoonsoo Kim } 847e22bee78STejun Heo 848e22bee78STejun Heo /** 849e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 850e22bee78STejun Heo * @task: task going to sleep 851e22bee78STejun Heo * @cpu: CPU in question, must be the current CPU number 852e22bee78STejun Heo * 853e22bee78STejun Heo * This function is called during schedule() when a busy worker is 854e22bee78STejun Heo * going to sleep. Worker on the same cpu can be woken up by 855e22bee78STejun Heo * returning pointer to its task. 856e22bee78STejun Heo * 857e22bee78STejun Heo * CONTEXT: 858e22bee78STejun Heo * spin_lock_irq(rq->lock) 859e22bee78STejun Heo * 860d185af30SYacine Belkadi * Return: 861e22bee78STejun Heo * Worker task on @cpu to wake up, %NULL if none. 862e22bee78STejun Heo */ 863d84ff051STejun Heo struct task_struct *wq_worker_sleeping(struct task_struct *task, int cpu) 864e22bee78STejun Heo { 865e22bee78STejun Heo struct worker *worker = kthread_data(task), *to_wakeup = NULL; 866111c225aSTejun Heo struct worker_pool *pool; 867e22bee78STejun Heo 868111c225aSTejun Heo /* 869111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 870111c225aSTejun Heo * workers, also reach here, let's not access anything before 871111c225aSTejun Heo * checking NOT_RUNNING. 872111c225aSTejun Heo */ 8732d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 874e22bee78STejun Heo return NULL; 875e22bee78STejun Heo 876111c225aSTejun Heo pool = worker->pool; 877111c225aSTejun Heo 878e22bee78STejun Heo /* this can only happen on the local cpu */ 87992b69f50SLai Jiangshan if (WARN_ON_ONCE(cpu != raw_smp_processor_id() || pool->cpu != cpu)) 8806183c009STejun Heo return NULL; 881e22bee78STejun Heo 882e22bee78STejun Heo /* 883e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 884e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 885e22bee78STejun Heo * Please read comment there. 886e22bee78STejun Heo * 887628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 888628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 889628c78e7STejun Heo * disabled, which in turn means that none else could be 890d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 891628c78e7STejun Heo * lock is safe. 892e22bee78STejun Heo */ 893e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 894e19e397aSTejun Heo !list_empty(&pool->worklist)) 8951037de36SLai Jiangshan to_wakeup = first_idle_worker(pool); 896e22bee78STejun Heo return to_wakeup ? to_wakeup->task : NULL; 897e22bee78STejun Heo } 898e22bee78STejun Heo 899e22bee78STejun Heo /** 900e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 901cb444766STejun Heo * @worker: self 902d302f017STejun Heo * @flags: flags to set 903d302f017STejun Heo * 904228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 905d302f017STejun Heo * 906cb444766STejun Heo * CONTEXT: 907d565ed63STejun Heo * spin_lock_irq(pool->lock) 908d302f017STejun Heo */ 909228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 910d302f017STejun Heo { 911bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 912e22bee78STejun Heo 913cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 914cb444766STejun Heo 915228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 916e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 917e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 918e19e397aSTejun Heo atomic_dec(&pool->nr_running); 919e22bee78STejun Heo } 920e22bee78STejun Heo 921d302f017STejun Heo worker->flags |= flags; 922d302f017STejun Heo } 923d302f017STejun Heo 924d302f017STejun Heo /** 925e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 926cb444766STejun Heo * @worker: self 927d302f017STejun Heo * @flags: flags to clear 928d302f017STejun Heo * 929e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 930d302f017STejun Heo * 931cb444766STejun Heo * CONTEXT: 932d565ed63STejun Heo * spin_lock_irq(pool->lock) 933d302f017STejun Heo */ 934d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 935d302f017STejun Heo { 93663d95a91STejun Heo struct worker_pool *pool = worker->pool; 937e22bee78STejun Heo unsigned int oflags = worker->flags; 938e22bee78STejun Heo 939cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 940cb444766STejun Heo 941d302f017STejun Heo worker->flags &= ~flags; 942e22bee78STejun Heo 94342c025f3STejun Heo /* 94442c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 94542c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 94642c025f3STejun Heo * of multiple flags, not a single flag. 94742c025f3STejun Heo */ 948e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 949e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 950e19e397aSTejun Heo atomic_inc(&pool->nr_running); 951d302f017STejun Heo } 952d302f017STejun Heo 953d302f017STejun Heo /** 9548cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 955c9e7cf27STejun Heo * @pool: pool of interest 9568cca0eeaSTejun Heo * @work: work to find worker for 9578cca0eeaSTejun Heo * 958c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 959c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 960a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 961a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 962a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 963a2c1c57bSTejun Heo * being executed. 964a2c1c57bSTejun Heo * 965a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 966a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 967a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 968a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 969a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 970a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 971a2c1c57bSTejun Heo * 972c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 973c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 974c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 975c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 976c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 977c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 9788cca0eeaSTejun Heo * 9798cca0eeaSTejun Heo * CONTEXT: 980d565ed63STejun Heo * spin_lock_irq(pool->lock). 9818cca0eeaSTejun Heo * 982d185af30SYacine Belkadi * Return: 983d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 9848cca0eeaSTejun Heo * otherwise. 9858cca0eeaSTejun Heo */ 986c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 9878cca0eeaSTejun Heo struct work_struct *work) 9888cca0eeaSTejun Heo { 98942f8570fSSasha Levin struct worker *worker; 99042f8570fSSasha Levin 991b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 992a2c1c57bSTejun Heo (unsigned long)work) 993a2c1c57bSTejun Heo if (worker->current_work == work && 994a2c1c57bSTejun Heo worker->current_func == work->func) 99542f8570fSSasha Levin return worker; 99642f8570fSSasha Levin 99742f8570fSSasha Levin return NULL; 9988cca0eeaSTejun Heo } 9998cca0eeaSTejun Heo 10008cca0eeaSTejun Heo /** 1001bf4ede01STejun Heo * move_linked_works - move linked works to a list 1002bf4ede01STejun Heo * @work: start of series of works to be scheduled 1003bf4ede01STejun Heo * @head: target list to append @work to 1004402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1005bf4ede01STejun Heo * 1006bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1007bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1008bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1009bf4ede01STejun Heo * 1010bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1011bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1012bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1013bf4ede01STejun Heo * 1014bf4ede01STejun Heo * CONTEXT: 1015d565ed63STejun Heo * spin_lock_irq(pool->lock). 1016bf4ede01STejun Heo */ 1017bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1018bf4ede01STejun Heo struct work_struct **nextp) 1019bf4ede01STejun Heo { 1020bf4ede01STejun Heo struct work_struct *n; 1021bf4ede01STejun Heo 1022bf4ede01STejun Heo /* 1023bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1024bf4ede01STejun Heo * use NULL for list head. 1025bf4ede01STejun Heo */ 1026bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1027bf4ede01STejun Heo list_move_tail(&work->entry, head); 1028bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1029bf4ede01STejun Heo break; 1030bf4ede01STejun Heo } 1031bf4ede01STejun Heo 1032bf4ede01STejun Heo /* 1033bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1034bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1035bf4ede01STejun Heo * needs to be updated. 1036bf4ede01STejun Heo */ 1037bf4ede01STejun Heo if (nextp) 1038bf4ede01STejun Heo *nextp = n; 1039bf4ede01STejun Heo } 1040bf4ede01STejun Heo 10418864b4e5STejun Heo /** 10428864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 10438864b4e5STejun Heo * @pwq: pool_workqueue to get 10448864b4e5STejun Heo * 10458864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 10468864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 10478864b4e5STejun Heo */ 10488864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 10498864b4e5STejun Heo { 10508864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10518864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 10528864b4e5STejun Heo pwq->refcnt++; 10538864b4e5STejun Heo } 10548864b4e5STejun Heo 10558864b4e5STejun Heo /** 10568864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 10578864b4e5STejun Heo * @pwq: pool_workqueue to put 10588864b4e5STejun Heo * 10598864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 10608864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 10618864b4e5STejun Heo */ 10628864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 10638864b4e5STejun Heo { 10648864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10658864b4e5STejun Heo if (likely(--pwq->refcnt)) 10668864b4e5STejun Heo return; 10678864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 10688864b4e5STejun Heo return; 10698864b4e5STejun Heo /* 10708864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 10718864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 10728864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 10738864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 10748864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 10758864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 10768864b4e5STejun Heo */ 10778864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 10788864b4e5STejun Heo } 10798864b4e5STejun Heo 1080dce90d47STejun Heo /** 1081dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1082dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1083dce90d47STejun Heo * 1084dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1085dce90d47STejun Heo */ 1086dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1087dce90d47STejun Heo { 1088dce90d47STejun Heo if (pwq) { 1089dce90d47STejun Heo /* 1090dce90d47STejun Heo * As both pwqs and pools are sched-RCU protected, the 1091dce90d47STejun Heo * following lock operations are safe. 1092dce90d47STejun Heo */ 1093dce90d47STejun Heo spin_lock_irq(&pwq->pool->lock); 1094dce90d47STejun Heo put_pwq(pwq); 1095dce90d47STejun Heo spin_unlock_irq(&pwq->pool->lock); 1096dce90d47STejun Heo } 1097dce90d47STejun Heo } 1098dce90d47STejun Heo 1099112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 1100bf4ede01STejun Heo { 1101112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1102bf4ede01STejun Heo 1103bf4ede01STejun Heo trace_workqueue_activate_work(work); 110482607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 110582607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1106112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1107bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 1108112202d9STejun Heo pwq->nr_active++; 1109bf4ede01STejun Heo } 1110bf4ede01STejun Heo 1111112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 11123aa62497SLai Jiangshan { 1113112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 11143aa62497SLai Jiangshan struct work_struct, entry); 11153aa62497SLai Jiangshan 1116112202d9STejun Heo pwq_activate_delayed_work(work); 11173aa62497SLai Jiangshan } 11183aa62497SLai Jiangshan 1119bf4ede01STejun Heo /** 1120112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1121112202d9STejun Heo * @pwq: pwq of interest 1122bf4ede01STejun Heo * @color: color of work which left the queue 1123bf4ede01STejun Heo * 1124bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1125112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1126bf4ede01STejun Heo * 1127bf4ede01STejun Heo * CONTEXT: 1128d565ed63STejun Heo * spin_lock_irq(pool->lock). 1129bf4ede01STejun Heo */ 1130112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1131bf4ede01STejun Heo { 11328864b4e5STejun Heo /* uncolored work items don't participate in flushing or nr_active */ 1133bf4ede01STejun Heo if (color == WORK_NO_COLOR) 11348864b4e5STejun Heo goto out_put; 1135bf4ede01STejun Heo 1136112202d9STejun Heo pwq->nr_in_flight[color]--; 1137bf4ede01STejun Heo 1138112202d9STejun Heo pwq->nr_active--; 1139112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 1140bf4ede01STejun Heo /* one down, submit a delayed one */ 1141112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1142112202d9STejun Heo pwq_activate_first_delayed(pwq); 1143bf4ede01STejun Heo } 1144bf4ede01STejun Heo 1145bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1146112202d9STejun Heo if (likely(pwq->flush_color != color)) 11478864b4e5STejun Heo goto out_put; 1148bf4ede01STejun Heo 1149bf4ede01STejun Heo /* are there still in-flight works? */ 1150112202d9STejun Heo if (pwq->nr_in_flight[color]) 11518864b4e5STejun Heo goto out_put; 1152bf4ede01STejun Heo 1153112202d9STejun Heo /* this pwq is done, clear flush_color */ 1154112202d9STejun Heo pwq->flush_color = -1; 1155bf4ede01STejun Heo 1156bf4ede01STejun Heo /* 1157112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1158bf4ede01STejun Heo * will handle the rest. 1159bf4ede01STejun Heo */ 1160112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1161112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 11628864b4e5STejun Heo out_put: 11638864b4e5STejun Heo put_pwq(pwq); 1164bf4ede01STejun Heo } 1165bf4ede01STejun Heo 116636e227d2STejun Heo /** 1167bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 116836e227d2STejun Heo * @work: work item to steal 116936e227d2STejun Heo * @is_dwork: @work is a delayed_work 1170bbb68dfaSTejun Heo * @flags: place to store irq state 117136e227d2STejun Heo * 117236e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1173d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 117436e227d2STejun Heo * 1175d185af30SYacine Belkadi * Return: 117636e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 117736e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 117836e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1179bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1180bbb68dfaSTejun Heo * for arbitrarily long 118136e227d2STejun Heo * 1182d185af30SYacine Belkadi * Note: 1183bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1184e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1185e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1186e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1187bbb68dfaSTejun Heo * 1188bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1189bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1190bbb68dfaSTejun Heo * 1191e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1192bf4ede01STejun Heo */ 1193bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1194bbb68dfaSTejun Heo unsigned long *flags) 1195bf4ede01STejun Heo { 1196d565ed63STejun Heo struct worker_pool *pool; 1197112202d9STejun Heo struct pool_workqueue *pwq; 1198bf4ede01STejun Heo 1199bbb68dfaSTejun Heo local_irq_save(*flags); 1200bbb68dfaSTejun Heo 120136e227d2STejun Heo /* try to steal the timer if it exists */ 120236e227d2STejun Heo if (is_dwork) { 120336e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 120436e227d2STejun Heo 1205e0aecdd8STejun Heo /* 1206e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1207e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1208e0aecdd8STejun Heo * running on the local CPU. 1209e0aecdd8STejun Heo */ 121036e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 121136e227d2STejun Heo return 1; 121236e227d2STejun Heo } 121336e227d2STejun Heo 121436e227d2STejun Heo /* try to claim PENDING the normal way */ 1215bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1216bf4ede01STejun Heo return 0; 1217bf4ede01STejun Heo 1218bf4ede01STejun Heo /* 1219bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1220bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1221bf4ede01STejun Heo */ 1222d565ed63STejun Heo pool = get_work_pool(work); 1223d565ed63STejun Heo if (!pool) 1224bbb68dfaSTejun Heo goto fail; 1225bf4ede01STejun Heo 1226d565ed63STejun Heo spin_lock(&pool->lock); 1227bf4ede01STejun Heo /* 1228112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1229112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1230112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1231112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1232112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 12330b3dae68SLai Jiangshan * item is currently queued on that pool. 1234bf4ede01STejun Heo */ 1235112202d9STejun Heo pwq = get_work_pwq(work); 1236112202d9STejun Heo if (pwq && pwq->pool == pool) { 1237bf4ede01STejun Heo debug_work_deactivate(work); 12383aa62497SLai Jiangshan 12393aa62497SLai Jiangshan /* 124016062836STejun Heo * A delayed work item cannot be grabbed directly because 124116062836STejun Heo * it might have linked NO_COLOR work items which, if left 1242112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 124316062836STejun Heo * management later on and cause stall. Make sure the work 124416062836STejun Heo * item is activated before grabbing. 12453aa62497SLai Jiangshan */ 12463aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1247112202d9STejun Heo pwq_activate_delayed_work(work); 12483aa62497SLai Jiangshan 1249bf4ede01STejun Heo list_del_init(&work->entry); 12509c34a704SLai Jiangshan pwq_dec_nr_in_flight(pwq, get_work_color(work)); 125136e227d2STejun Heo 1252112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 12534468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 12544468a00fSLai Jiangshan 1255d565ed63STejun Heo spin_unlock(&pool->lock); 125636e227d2STejun Heo return 1; 1257bf4ede01STejun Heo } 1258d565ed63STejun Heo spin_unlock(&pool->lock); 1259bbb68dfaSTejun Heo fail: 1260bbb68dfaSTejun Heo local_irq_restore(*flags); 1261bbb68dfaSTejun Heo if (work_is_canceling(work)) 1262bbb68dfaSTejun Heo return -ENOENT; 1263bbb68dfaSTejun Heo cpu_relax(); 126436e227d2STejun Heo return -EAGAIN; 1265bf4ede01STejun Heo } 1266bf4ede01STejun Heo 1267bf4ede01STejun Heo /** 1268706026c2STejun Heo * insert_work - insert a work into a pool 1269112202d9STejun Heo * @pwq: pwq @work belongs to 12704690c4abSTejun Heo * @work: work to insert 12714690c4abSTejun Heo * @head: insertion point 12724690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 12734690c4abSTejun Heo * 1274112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1275706026c2STejun Heo * work_struct flags. 12764690c4abSTejun Heo * 12774690c4abSTejun Heo * CONTEXT: 1278d565ed63STejun Heo * spin_lock_irq(pool->lock). 1279365970a1SDavid Howells */ 1280112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1281112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1282b89deed3SOleg Nesterov { 1283112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1284e1d8aa9fSFrederic Weisbecker 12854690c4abSTejun Heo /* we own @work, set data and link */ 1286112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 12871a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 12888864b4e5STejun Heo get_pwq(pwq); 1289e22bee78STejun Heo 1290e22bee78STejun Heo /* 1291c5aa87bbSTejun Heo * Ensure either wq_worker_sleeping() sees the above 1292c5aa87bbSTejun Heo * list_add_tail() or we see zero nr_running to avoid workers lying 1293c5aa87bbSTejun Heo * around lazily while there are works to be processed. 1294e22bee78STejun Heo */ 1295e22bee78STejun Heo smp_mb(); 1296e22bee78STejun Heo 129763d95a91STejun Heo if (__need_more_worker(pool)) 129863d95a91STejun Heo wake_up_worker(pool); 1299b89deed3SOleg Nesterov } 1300b89deed3SOleg Nesterov 1301c8efcc25STejun Heo /* 1302c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 13038d03ecfeSTejun Heo * same workqueue. 1304c8efcc25STejun Heo */ 1305c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1306c8efcc25STejun Heo { 1307c8efcc25STejun Heo struct worker *worker; 1308c8efcc25STejun Heo 13098d03ecfeSTejun Heo worker = current_wq_worker(); 1310c8efcc25STejun Heo /* 13118d03ecfeSTejun Heo * Return %true iff I'm a worker execuing a work item on @wq. If 13128d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1313c8efcc25STejun Heo */ 1314112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1315c8efcc25STejun Heo } 1316c8efcc25STejun Heo 1317ef557180SMike Galbraith /* 1318ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1319ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1320ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1321ef557180SMike Galbraith */ 1322ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1323ef557180SMike Galbraith { 1324*f303fccbSTejun Heo static bool printed_dbg_warning; 1325ef557180SMike Galbraith int new_cpu; 1326ef557180SMike Galbraith 1327*f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1328ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1329ef557180SMike Galbraith return cpu; 1330*f303fccbSTejun Heo } else if (!printed_dbg_warning) { 1331*f303fccbSTejun Heo pr_warn("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1332*f303fccbSTejun Heo printed_dbg_warning = true; 1333*f303fccbSTejun Heo } 1334*f303fccbSTejun Heo 1335ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1336ef557180SMike Galbraith return cpu; 1337ef557180SMike Galbraith 1338ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1339ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1340ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1341ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1342ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1343ef557180SMike Galbraith return cpu; 1344ef557180SMike Galbraith } 1345ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1346ef557180SMike Galbraith 1347ef557180SMike Galbraith return new_cpu; 1348ef557180SMike Galbraith } 1349ef557180SMike Galbraith 1350d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 13511da177e4SLinus Torvalds struct work_struct *work) 13521da177e4SLinus Torvalds { 1353112202d9STejun Heo struct pool_workqueue *pwq; 1354c9178087STejun Heo struct worker_pool *last_pool; 13551e19ffc6STejun Heo struct list_head *worklist; 13568a2e8e5dSTejun Heo unsigned int work_flags; 1357b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 13588930cabaSTejun Heo 13598930cabaSTejun Heo /* 13608930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 13618930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 13628930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 13638930cabaSTejun Heo * happen with IRQ disabled. 13648930cabaSTejun Heo */ 13658930cabaSTejun Heo WARN_ON_ONCE(!irqs_disabled()); 13661da177e4SLinus Torvalds 1367dc186ad7SThomas Gleixner debug_work_activate(work); 13681e19ffc6STejun Heo 13699ef28a73SLi Bin /* if draining, only works from the same workqueue are allowed */ 1370618b01ebSTejun Heo if (unlikely(wq->flags & __WQ_DRAINING) && 1371c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1372e41e704bSTejun Heo return; 13739e8cd2f5STejun Heo retry: 1374df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1375ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1376df2d5ae4STejun Heo 1377df2d5ae4STejun Heo /* pwq which will be used unless @work is executing elsewhere */ 1378df2d5ae4STejun Heo if (!(wq->flags & WQ_UNBOUND)) 1379c9178087STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1380df2d5ae4STejun Heo else 1381df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1382f3421797STejun Heo 138318aa9effSTejun Heo /* 1384c9178087STejun Heo * If @work was previously on a different pool, it might still be 1385c9178087STejun Heo * running there, in which case the work needs to be queued on that 1386c9178087STejun Heo * pool to guarantee non-reentrancy. 138718aa9effSTejun Heo */ 1388c9e7cf27STejun Heo last_pool = get_work_pool(work); 1389112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 139018aa9effSTejun Heo struct worker *worker; 139118aa9effSTejun Heo 1392d565ed63STejun Heo spin_lock(&last_pool->lock); 139318aa9effSTejun Heo 1394c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 139518aa9effSTejun Heo 1396112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1397c9178087STejun Heo pwq = worker->current_pwq; 13988594fadeSLai Jiangshan } else { 139918aa9effSTejun Heo /* meh... not running there, queue here */ 1400d565ed63STejun Heo spin_unlock(&last_pool->lock); 1401112202d9STejun Heo spin_lock(&pwq->pool->lock); 140218aa9effSTejun Heo } 14038930cabaSTejun Heo } else { 1404112202d9STejun Heo spin_lock(&pwq->pool->lock); 14058930cabaSTejun Heo } 1406502ca9d8STejun Heo 14079e8cd2f5STejun Heo /* 14089e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 14099e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 14109e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1411df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1412df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 14139e8cd2f5STejun Heo * make forward-progress. 14149e8cd2f5STejun Heo */ 14159e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 14169e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 14179e8cd2f5STejun Heo spin_unlock(&pwq->pool->lock); 14189e8cd2f5STejun Heo cpu_relax(); 14199e8cd2f5STejun Heo goto retry; 14209e8cd2f5STejun Heo } 14219e8cd2f5STejun Heo /* oops */ 14229e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 14239e8cd2f5STejun Heo wq->name, cpu); 14249e8cd2f5STejun Heo } 14259e8cd2f5STejun Heo 1426112202d9STejun Heo /* pwq determined, queue */ 1427112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1428502ca9d8STejun Heo 1429f5b2552bSDan Carpenter if (WARN_ON(!list_empty(&work->entry))) { 1430112202d9STejun Heo spin_unlock(&pwq->pool->lock); 1431f5b2552bSDan Carpenter return; 1432f5b2552bSDan Carpenter } 14331e19ffc6STejun Heo 1434112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1435112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 14361e19ffc6STejun Heo 1437112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1438cdadf009STejun Heo trace_workqueue_activate_work(work); 1439112202d9STejun Heo pwq->nr_active++; 1440112202d9STejun Heo worklist = &pwq->pool->worklist; 144182607adcSTejun Heo if (list_empty(worklist)) 144282607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 14438a2e8e5dSTejun Heo } else { 14448a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1445112202d9STejun Heo worklist = &pwq->delayed_works; 14468a2e8e5dSTejun Heo } 14471e19ffc6STejun Heo 1448112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 14491e19ffc6STejun Heo 1450112202d9STejun Heo spin_unlock(&pwq->pool->lock); 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14530fcb78c2SRolf Eike Beer /** 1454c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1455c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1456c1a220e7SZhang Rui * @wq: workqueue to use 1457c1a220e7SZhang Rui * @work: work to queue 1458c1a220e7SZhang Rui * 1459c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1460c1a220e7SZhang Rui * can't go away. 1461d185af30SYacine Belkadi * 1462d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1463c1a220e7SZhang Rui */ 1464d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1465d4283e93STejun Heo struct work_struct *work) 1466c1a220e7SZhang Rui { 1467d4283e93STejun Heo bool ret = false; 14688930cabaSTejun Heo unsigned long flags; 14698930cabaSTejun Heo 14708930cabaSTejun Heo local_irq_save(flags); 1471c1a220e7SZhang Rui 147222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 14734690c4abSTejun Heo __queue_work(cpu, wq, work); 1474d4283e93STejun Heo ret = true; 1475c1a220e7SZhang Rui } 14768930cabaSTejun Heo 14778930cabaSTejun Heo local_irq_restore(flags); 1478c1a220e7SZhang Rui return ret; 1479c1a220e7SZhang Rui } 1480ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1481c1a220e7SZhang Rui 1482d8e794dfSTejun Heo void delayed_work_timer_fn(unsigned long __data) 14831da177e4SLinus Torvalds { 148452bad64dSDavid Howells struct delayed_work *dwork = (struct delayed_work *)__data; 14851da177e4SLinus Torvalds 1486e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 148760c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 14881da177e4SLinus Torvalds } 14891438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 14901da177e4SLinus Torvalds 14917beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 149252bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 14931da177e4SLinus Torvalds { 14947beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 14957beb2edfSTejun Heo struct work_struct *work = &dwork->work; 14961da177e4SLinus Torvalds 14977beb2edfSTejun Heo WARN_ON_ONCE(timer->function != delayed_work_timer_fn || 14987beb2edfSTejun Heo timer->data != (unsigned long)dwork); 1499fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1500fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 15017beb2edfSTejun Heo 15028852aac2STejun Heo /* 15038852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 15048852aac2STejun Heo * both optimization and correctness. The earliest @timer can 15058852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 15068852aac2STejun Heo * on that there's no such delay when @delay is 0. 15078852aac2STejun Heo */ 15088852aac2STejun Heo if (!delay) { 15098852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 15108852aac2STejun Heo return; 15118852aac2STejun Heo } 15128852aac2STejun Heo 15137beb2edfSTejun Heo timer_stats_timer_set_start_info(&dwork->timer); 15147beb2edfSTejun Heo 151560c057bcSLai Jiangshan dwork->wq = wq; 15161265057fSTejun Heo dwork->cpu = cpu; 15177beb2edfSTejun Heo timer->expires = jiffies + delay; 15187beb2edfSTejun Heo 1519041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 15207beb2edfSTejun Heo add_timer_on(timer, cpu); 1521041bd12eSTejun Heo else 1522041bd12eSTejun Heo add_timer(timer); 15237beb2edfSTejun Heo } 15241da177e4SLinus Torvalds 15250fcb78c2SRolf Eike Beer /** 15260fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 15270fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 15280fcb78c2SRolf Eike Beer * @wq: workqueue to use 1529af9997e4SRandy Dunlap * @dwork: work to queue 15300fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 15310fcb78c2SRolf Eike Beer * 1532d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1533715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1534715f1300STejun Heo * execution. 15350fcb78c2SRolf Eike Beer */ 1536d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 153752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 15387a6bc1cdSVenkatesh Pallipadi { 153952bad64dSDavid Howells struct work_struct *work = &dwork->work; 1540d4283e93STejun Heo bool ret = false; 15418930cabaSTejun Heo unsigned long flags; 15428930cabaSTejun Heo 15438930cabaSTejun Heo /* read the comment in __queue_work() */ 15448930cabaSTejun Heo local_irq_save(flags); 15457a6bc1cdSVenkatesh Pallipadi 154622df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15477beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1548d4283e93STejun Heo ret = true; 15497a6bc1cdSVenkatesh Pallipadi } 15508930cabaSTejun Heo 15518930cabaSTejun Heo local_irq_restore(flags); 15527a6bc1cdSVenkatesh Pallipadi return ret; 15537a6bc1cdSVenkatesh Pallipadi } 1554ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 15551da177e4SLinus Torvalds 1556c8e55f36STejun Heo /** 15578376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 15588376fe22STejun Heo * @cpu: CPU number to execute work on 15598376fe22STejun Heo * @wq: workqueue to use 15608376fe22STejun Heo * @dwork: work to queue 15618376fe22STejun Heo * @delay: number of jiffies to wait before queueing 15628376fe22STejun Heo * 15638376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 15648376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 15658376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 15668376fe22STejun Heo * current state. 15678376fe22STejun Heo * 1568d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 15698376fe22STejun Heo * pending and its timer was modified. 15708376fe22STejun Heo * 1571e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 15728376fe22STejun Heo * See try_to_grab_pending() for details. 15738376fe22STejun Heo */ 15748376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 15758376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 15768376fe22STejun Heo { 15778376fe22STejun Heo unsigned long flags; 15788376fe22STejun Heo int ret; 15798376fe22STejun Heo 15808376fe22STejun Heo do { 15818376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 15828376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 15838376fe22STejun Heo 15848376fe22STejun Heo if (likely(ret >= 0)) { 15858376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 15868376fe22STejun Heo local_irq_restore(flags); 15878376fe22STejun Heo } 15888376fe22STejun Heo 15898376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 15908376fe22STejun Heo return ret; 15918376fe22STejun Heo } 15928376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 15938376fe22STejun Heo 15948376fe22STejun Heo /** 1595c8e55f36STejun Heo * worker_enter_idle - enter idle state 1596c8e55f36STejun Heo * @worker: worker which is entering idle state 1597c8e55f36STejun Heo * 1598c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1599c8e55f36STejun Heo * necessary. 1600c8e55f36STejun Heo * 1601c8e55f36STejun Heo * LOCKING: 1602d565ed63STejun Heo * spin_lock_irq(pool->lock). 1603c8e55f36STejun Heo */ 1604c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 16051da177e4SLinus Torvalds { 1606bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1607c8e55f36STejun Heo 16086183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 16096183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 16106183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 16116183c009STejun Heo return; 1612c8e55f36STejun Heo 1613051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1614cb444766STejun Heo worker->flags |= WORKER_IDLE; 1615bd7bdd43STejun Heo pool->nr_idle++; 1616e22bee78STejun Heo worker->last_active = jiffies; 1617c8e55f36STejun Heo 1618c8e55f36STejun Heo /* idle_list is LIFO */ 1619bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1620db7bccf4STejun Heo 162163d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1622628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1623cb444766STejun Heo 1624544ecf31STejun Heo /* 1625706026c2STejun Heo * Sanity check nr_running. Because wq_unbind_fn() releases 1626d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1627628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1628628c78e7STejun Heo * unbind is not in progress. 1629544ecf31STejun Heo */ 163024647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1631bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1632e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1633c8e55f36STejun Heo } 1634c8e55f36STejun Heo 1635c8e55f36STejun Heo /** 1636c8e55f36STejun Heo * worker_leave_idle - leave idle state 1637c8e55f36STejun Heo * @worker: worker which is leaving idle state 1638c8e55f36STejun Heo * 1639c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1640c8e55f36STejun Heo * 1641c8e55f36STejun Heo * LOCKING: 1642d565ed63STejun Heo * spin_lock_irq(pool->lock). 1643c8e55f36STejun Heo */ 1644c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1645c8e55f36STejun Heo { 1646bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1647c8e55f36STejun Heo 16486183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 16496183c009STejun Heo return; 1650d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1651bd7bdd43STejun Heo pool->nr_idle--; 1652c8e55f36STejun Heo list_del_init(&worker->entry); 1653c8e55f36STejun Heo } 1654c8e55f36STejun Heo 1655f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1656c34056a3STejun Heo { 1657c34056a3STejun Heo struct worker *worker; 1658c34056a3STejun Heo 1659f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1660c8e55f36STejun Heo if (worker) { 1661c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1662affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1663da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1664e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1665e22bee78STejun Heo worker->flags = WORKER_PREP; 1666c8e55f36STejun Heo } 1667c34056a3STejun Heo return worker; 1668c34056a3STejun Heo } 1669c34056a3STejun Heo 1670c34056a3STejun Heo /** 16714736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 16724736cbf7SLai Jiangshan * @worker: worker to be attached 16734736cbf7SLai Jiangshan * @pool: the target pool 16744736cbf7SLai Jiangshan * 16754736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 16764736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 16774736cbf7SLai Jiangshan * cpu-[un]hotplugs. 16784736cbf7SLai Jiangshan */ 16794736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 16804736cbf7SLai Jiangshan struct worker_pool *pool) 16814736cbf7SLai Jiangshan { 16824736cbf7SLai Jiangshan mutex_lock(&pool->attach_mutex); 16834736cbf7SLai Jiangshan 16844736cbf7SLai Jiangshan /* 16854736cbf7SLai Jiangshan * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any 16864736cbf7SLai Jiangshan * online CPUs. It'll be re-applied when any of the CPUs come up. 16874736cbf7SLai Jiangshan */ 16884736cbf7SLai Jiangshan set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 16894736cbf7SLai Jiangshan 16904736cbf7SLai Jiangshan /* 16914736cbf7SLai Jiangshan * The pool->attach_mutex ensures %POOL_DISASSOCIATED remains 16924736cbf7SLai Jiangshan * stable across this function. See the comments above the 16934736cbf7SLai Jiangshan * flag definition for details. 16944736cbf7SLai Jiangshan */ 16954736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 16964736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 16974736cbf7SLai Jiangshan 16984736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 16994736cbf7SLai Jiangshan 17004736cbf7SLai Jiangshan mutex_unlock(&pool->attach_mutex); 17014736cbf7SLai Jiangshan } 17024736cbf7SLai Jiangshan 17034736cbf7SLai Jiangshan /** 170460f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 170560f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 170660f5a4bcSLai Jiangshan * @pool: the pool @worker is attached to 170760f5a4bcSLai Jiangshan * 17084736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 17094736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 17104736cbf7SLai Jiangshan * other reference to the pool. 171160f5a4bcSLai Jiangshan */ 171260f5a4bcSLai Jiangshan static void worker_detach_from_pool(struct worker *worker, 171360f5a4bcSLai Jiangshan struct worker_pool *pool) 171460f5a4bcSLai Jiangshan { 171560f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 171660f5a4bcSLai Jiangshan 171792f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 1718da028469SLai Jiangshan list_del(&worker->node); 1719da028469SLai Jiangshan if (list_empty(&pool->workers)) 172060f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 172192f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 172260f5a4bcSLai Jiangshan 1723b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1724b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1725b62c0751SLai Jiangshan 172660f5a4bcSLai Jiangshan if (detach_completion) 172760f5a4bcSLai Jiangshan complete(detach_completion); 172860f5a4bcSLai Jiangshan } 172960f5a4bcSLai Jiangshan 173060f5a4bcSLai Jiangshan /** 1731c34056a3STejun Heo * create_worker - create a new workqueue worker 173263d95a91STejun Heo * @pool: pool the new worker will belong to 1733c34056a3STejun Heo * 1734051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1735c34056a3STejun Heo * 1736c34056a3STejun Heo * CONTEXT: 1737c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1738c34056a3STejun Heo * 1739d185af30SYacine Belkadi * Return: 1740c34056a3STejun Heo * Pointer to the newly created worker. 1741c34056a3STejun Heo */ 1742bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1743c34056a3STejun Heo { 1744c34056a3STejun Heo struct worker *worker = NULL; 1745f3421797STejun Heo int id = -1; 1746e3c916a4STejun Heo char id_buf[16]; 1747c34056a3STejun Heo 17487cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 17497cda9aaeSLai Jiangshan id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL); 1750822d8405STejun Heo if (id < 0) 1751c34056a3STejun Heo goto fail; 1752c34056a3STejun Heo 1753f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 1754c34056a3STejun Heo if (!worker) 1755c34056a3STejun Heo goto fail; 1756c34056a3STejun Heo 1757bd7bdd43STejun Heo worker->pool = pool; 1758c34056a3STejun Heo worker->id = id; 1759c34056a3STejun Heo 176029c91e99STejun Heo if (pool->cpu >= 0) 1761e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1762e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1763f3421797STejun Heo else 1764e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1765e3c916a4STejun Heo 1766f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1767e3c916a4STejun Heo "kworker/%s", id_buf); 1768c34056a3STejun Heo if (IS_ERR(worker->task)) 1769c34056a3STejun Heo goto fail; 1770c34056a3STejun Heo 177191151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 177225834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 177391151228SOleg Nesterov 1774da028469SLai Jiangshan /* successful, attach the worker to the pool */ 17754736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1776822d8405STejun Heo 1777051e1850SLai Jiangshan /* start the newly created worker */ 1778051e1850SLai Jiangshan spin_lock_irq(&pool->lock); 1779051e1850SLai Jiangshan worker->pool->nr_workers++; 1780051e1850SLai Jiangshan worker_enter_idle(worker); 1781051e1850SLai Jiangshan wake_up_process(worker->task); 1782051e1850SLai Jiangshan spin_unlock_irq(&pool->lock); 1783051e1850SLai Jiangshan 1784c34056a3STejun Heo return worker; 1785822d8405STejun Heo 1786c34056a3STejun Heo fail: 17879625ab17SLai Jiangshan if (id >= 0) 17887cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, id); 1789c34056a3STejun Heo kfree(worker); 1790c34056a3STejun Heo return NULL; 1791c34056a3STejun Heo } 1792c34056a3STejun Heo 1793c34056a3STejun Heo /** 1794c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1795c34056a3STejun Heo * @worker: worker to be destroyed 1796c34056a3STejun Heo * 179773eb7fe7SLai Jiangshan * Destroy @worker and adjust @pool stats accordingly. The worker should 179873eb7fe7SLai Jiangshan * be idle. 1799c8e55f36STejun Heo * 1800c8e55f36STejun Heo * CONTEXT: 180160f5a4bcSLai Jiangshan * spin_lock_irq(pool->lock). 1802c34056a3STejun Heo */ 1803c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1804c34056a3STejun Heo { 1805bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1806c34056a3STejun Heo 1807cd549687STejun Heo lockdep_assert_held(&pool->lock); 1808cd549687STejun Heo 1809c34056a3STejun Heo /* sanity check frenzy */ 18106183c009STejun Heo if (WARN_ON(worker->current_work) || 181173eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 181273eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 18136183c009STejun Heo return; 1814c34056a3STejun Heo 1815bd7bdd43STejun Heo pool->nr_workers--; 1816bd7bdd43STejun Heo pool->nr_idle--; 1817c8e55f36STejun Heo 1818c8e55f36STejun Heo list_del_init(&worker->entry); 1819cb444766STejun Heo worker->flags |= WORKER_DIE; 182060f5a4bcSLai Jiangshan wake_up_process(worker->task); 1821c34056a3STejun Heo } 1822c34056a3STejun Heo 182363d95a91STejun Heo static void idle_worker_timeout(unsigned long __pool) 1824e22bee78STejun Heo { 182563d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1826e22bee78STejun Heo 1827d565ed63STejun Heo spin_lock_irq(&pool->lock); 1828e22bee78STejun Heo 18293347fc9fSLai Jiangshan while (too_many_workers(pool)) { 1830e22bee78STejun Heo struct worker *worker; 1831e22bee78STejun Heo unsigned long expires; 1832e22bee78STejun Heo 1833e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 183463d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1835e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1836e22bee78STejun Heo 18373347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 183863d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 18393347fc9fSLai Jiangshan break; 1840e22bee78STejun Heo } 18413347fc9fSLai Jiangshan 18423347fc9fSLai Jiangshan destroy_worker(worker); 1843e22bee78STejun Heo } 1844e22bee78STejun Heo 1845d565ed63STejun Heo spin_unlock_irq(&pool->lock); 1846e22bee78STejun Heo } 1847e22bee78STejun Heo 1848493a1724STejun Heo static void send_mayday(struct work_struct *work) 1849e22bee78STejun Heo { 1850112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1851112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 1852493a1724STejun Heo 18532e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 1854e22bee78STejun Heo 1855493008a8STejun Heo if (!wq->rescuer) 1856493a1724STejun Heo return; 1857e22bee78STejun Heo 1858e22bee78STejun Heo /* mayday mayday mayday */ 1859493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 186077668c8bSLai Jiangshan /* 186177668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 186277668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 186377668c8bSLai Jiangshan * rescuer is done with it. 186477668c8bSLai Jiangshan */ 186577668c8bSLai Jiangshan get_pwq(pwq); 1866493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 1867e22bee78STejun Heo wake_up_process(wq->rescuer->task); 1868493a1724STejun Heo } 1869e22bee78STejun Heo } 1870e22bee78STejun Heo 1871706026c2STejun Heo static void pool_mayday_timeout(unsigned long __pool) 1872e22bee78STejun Heo { 187363d95a91STejun Heo struct worker_pool *pool = (void *)__pool; 1874e22bee78STejun Heo struct work_struct *work; 1875e22bee78STejun Heo 1876b2d82909STejun Heo spin_lock_irq(&pool->lock); 1877b2d82909STejun Heo spin_lock(&wq_mayday_lock); /* for wq->maydays */ 1878e22bee78STejun Heo 187963d95a91STejun Heo if (need_to_create_worker(pool)) { 1880e22bee78STejun Heo /* 1881e22bee78STejun Heo * We've been trying to create a new worker but 1882e22bee78STejun Heo * haven't been successful. We might be hitting an 1883e22bee78STejun Heo * allocation deadlock. Send distress signals to 1884e22bee78STejun Heo * rescuers. 1885e22bee78STejun Heo */ 188663d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 1887e22bee78STejun Heo send_mayday(work); 1888e22bee78STejun Heo } 1889e22bee78STejun Heo 1890b2d82909STejun Heo spin_unlock(&wq_mayday_lock); 1891b2d82909STejun Heo spin_unlock_irq(&pool->lock); 1892e22bee78STejun Heo 189363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 1894e22bee78STejun Heo } 1895e22bee78STejun Heo 1896e22bee78STejun Heo /** 1897e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 189863d95a91STejun Heo * @pool: pool to create a new worker for 1899e22bee78STejun Heo * 190063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 1901e22bee78STejun Heo * have at least one idle worker on return from this function. If 1902e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 190363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 1904e22bee78STejun Heo * possible allocation deadlock. 1905e22bee78STejun Heo * 1906c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 1907c5aa87bbSTejun Heo * may_start_working() %true. 1908e22bee78STejun Heo * 1909e22bee78STejun Heo * LOCKING: 1910d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1911e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 1912e22bee78STejun Heo * manager. 1913e22bee78STejun Heo */ 191429187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 1915d565ed63STejun Heo __releases(&pool->lock) 1916d565ed63STejun Heo __acquires(&pool->lock) 1917e22bee78STejun Heo { 1918e22bee78STejun Heo restart: 1919d565ed63STejun Heo spin_unlock_irq(&pool->lock); 19209f9c2364STejun Heo 1921e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 192263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 1923e22bee78STejun Heo 1924e22bee78STejun Heo while (true) { 1925051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 1926e22bee78STejun Heo break; 1927e22bee78STejun Heo 1928e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 19299f9c2364STejun Heo 193063d95a91STejun Heo if (!need_to_create_worker(pool)) 1931e22bee78STejun Heo break; 1932e22bee78STejun Heo } 1933e22bee78STejun Heo 193463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 1935d565ed63STejun Heo spin_lock_irq(&pool->lock); 1936051e1850SLai Jiangshan /* 1937051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 1938051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 1939051e1850SLai Jiangshan * already become busy. 1940051e1850SLai Jiangshan */ 194163d95a91STejun Heo if (need_to_create_worker(pool)) 1942e22bee78STejun Heo goto restart; 1943e22bee78STejun Heo } 1944e22bee78STejun Heo 1945e22bee78STejun Heo /** 1946e22bee78STejun Heo * manage_workers - manage worker pool 1947e22bee78STejun Heo * @worker: self 1948e22bee78STejun Heo * 1949706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 1950e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 1951706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 1952e22bee78STejun Heo * 1953e22bee78STejun Heo * The caller can safely start processing works on false return. On 1954e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 1955e22bee78STejun Heo * and may_start_working() is true. 1956e22bee78STejun Heo * 1957e22bee78STejun Heo * CONTEXT: 1958d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 1959e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 1960e22bee78STejun Heo * 1961d185af30SYacine Belkadi * Return: 196229187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 196329187a9eSTejun Heo * start processing works, %true if management function was performed and 196429187a9eSTejun Heo * the conditions that the caller verified before calling the function may 196529187a9eSTejun Heo * no longer be true. 1966e22bee78STejun Heo */ 1967e22bee78STejun Heo static bool manage_workers(struct worker *worker) 1968e22bee78STejun Heo { 196963d95a91STejun Heo struct worker_pool *pool = worker->pool; 1970e22bee78STejun Heo 1971bc3a1afcSTejun Heo /* 1972bc3a1afcSTejun Heo * Anyone who successfully grabs manager_arb wins the arbitration 1973bc3a1afcSTejun Heo * and becomes the manager. mutex_trylock() on pool->manager_arb 1974bc3a1afcSTejun Heo * failure while holding pool->lock reliably indicates that someone 1975bc3a1afcSTejun Heo * else is managing the pool and the worker which failed trylock 1976bc3a1afcSTejun Heo * can proceed to executing work items. This means that anyone 1977bc3a1afcSTejun Heo * grabbing manager_arb is responsible for actually performing 1978bc3a1afcSTejun Heo * manager duties. If manager_arb is grabbed and released without 1979bc3a1afcSTejun Heo * actual management, the pool may stall indefinitely. 1980bc3a1afcSTejun Heo */ 198134a06bd6STejun Heo if (!mutex_trylock(&pool->manager_arb)) 198229187a9eSTejun Heo return false; 19832607d7a6STejun Heo pool->manager = worker; 1984e22bee78STejun Heo 198529187a9eSTejun Heo maybe_create_worker(pool); 1986e22bee78STejun Heo 19872607d7a6STejun Heo pool->manager = NULL; 198834a06bd6STejun Heo mutex_unlock(&pool->manager_arb); 198929187a9eSTejun Heo return true; 1990e22bee78STejun Heo } 1991e22bee78STejun Heo 1992a62428c0STejun Heo /** 1993a62428c0STejun Heo * process_one_work - process single work 1994c34056a3STejun Heo * @worker: self 1995a62428c0STejun Heo * @work: work to process 1996a62428c0STejun Heo * 1997a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 1998a62428c0STejun Heo * process a single work including synchronization against and 1999a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2000a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2001a62428c0STejun Heo * call this function to process a work. 2002a62428c0STejun Heo * 2003a62428c0STejun Heo * CONTEXT: 2004d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2005a62428c0STejun Heo */ 2006c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2007d565ed63STejun Heo __releases(&pool->lock) 2008d565ed63STejun Heo __acquires(&pool->lock) 20091da177e4SLinus Torvalds { 2010112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2011bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2012112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 201373f53c4aSTejun Heo int work_color; 20147e11629dSTejun Heo struct worker *collision; 20154e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 20164e6045f1SJohannes Berg /* 2017a62428c0STejun Heo * It is permissible to free the struct work_struct from 2018a62428c0STejun Heo * inside the function that is called from it, this we need to 2019a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2020a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2021a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 20224e6045f1SJohannes Berg */ 20234d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 20244d82a1deSPeter Zijlstra 20254d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 20264e6045f1SJohannes Berg #endif 2027807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 202885327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2029ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 203025511a47STejun Heo 20317e11629dSTejun Heo /* 20327e11629dSTejun Heo * A single work shouldn't be executed concurrently by 20337e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 20347e11629dSTejun Heo * already processing the work. If so, defer the work to the 20357e11629dSTejun Heo * currently executing one. 20367e11629dSTejun Heo */ 2037c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 20387e11629dSTejun Heo if (unlikely(collision)) { 20397e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 20407e11629dSTejun Heo return; 20417e11629dSTejun Heo } 20421da177e4SLinus Torvalds 20438930cabaSTejun Heo /* claim and dequeue */ 20441da177e4SLinus Torvalds debug_work_deactivate(work); 2045c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2046c34056a3STejun Heo worker->current_work = work; 2047a2c1c57bSTejun Heo worker->current_func = work->func; 2048112202d9STejun Heo worker->current_pwq = pwq; 204973f53c4aSTejun Heo work_color = get_work_color(work); 20507a22ad75STejun Heo 2051a62428c0STejun Heo list_del_init(&work->entry); 2052a62428c0STejun Heo 2053649027d7STejun Heo /* 2054228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2055228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2056228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2057228f1d00SLai Jiangshan * execution of the pending work items. 2058fb0e7bebSTejun Heo */ 2059fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2060228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2061fb0e7bebSTejun Heo 2062974271c4STejun Heo /* 2063a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2064a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2065a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2066a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2067228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2068974271c4STejun Heo */ 2069a489a03eSLai Jiangshan if (need_more_worker(pool)) 207063d95a91STejun Heo wake_up_worker(pool); 2071974271c4STejun Heo 20728930cabaSTejun Heo /* 20737c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2074d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 207523657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 207623657bb1STejun Heo * disabled. 20778930cabaSTejun Heo */ 20787c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 20791da177e4SLinus Torvalds 2080d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2081365970a1SDavid Howells 2082112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 20833295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2084e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2085a2c1c57bSTejun Heo worker->current_func(work); 2086e36c886aSArjan van de Ven /* 2087e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2088e36c886aSArjan van de Ven * point will only record its address. 2089e36c886aSArjan van de Ven */ 2090e36c886aSArjan van de Ven trace_workqueue_execute_end(work); 20913295f0efSIngo Molnar lock_map_release(&lockdep_map); 2092112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 20931da177e4SLinus Torvalds 2094d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2095044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2096044c782cSValentin Ilie " last function: %pf\n", 2097a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2098a2c1c57bSTejun Heo worker->current_func); 2099d5abe669SPeter Zijlstra debug_show_held_locks(current); 2100d5abe669SPeter Zijlstra dump_stack(); 2101d5abe669SPeter Zijlstra } 2102d5abe669SPeter Zijlstra 2103b22ce278STejun Heo /* 2104b22ce278STejun Heo * The following prevents a kworker from hogging CPU on !PREEMPT 2105b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2106b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2107b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2108789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2109789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2110b22ce278STejun Heo */ 21113e28e377SJoe Lawrence cond_resched_rcu_qs(); 2112b22ce278STejun Heo 2113d565ed63STejun Heo spin_lock_irq(&pool->lock); 2114a62428c0STejun Heo 2115fb0e7bebSTejun Heo /* clear cpu intensive status */ 2116fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2117fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2118fb0e7bebSTejun Heo 2119a62428c0STejun Heo /* we're done with it, release */ 212042f8570fSSasha Levin hash_del(&worker->hentry); 2121c34056a3STejun Heo worker->current_work = NULL; 2122a2c1c57bSTejun Heo worker->current_func = NULL; 2123112202d9STejun Heo worker->current_pwq = NULL; 21243d1cb205STejun Heo worker->desc_valid = false; 2125112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 2128affee4b2STejun Heo /** 2129affee4b2STejun Heo * process_scheduled_works - process scheduled works 2130affee4b2STejun Heo * @worker: self 2131affee4b2STejun Heo * 2132affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2133affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2134affee4b2STejun Heo * fetches a work from the top and executes it. 2135affee4b2STejun Heo * 2136affee4b2STejun Heo * CONTEXT: 2137d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2138affee4b2STejun Heo * multiple times. 2139affee4b2STejun Heo */ 2140affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 21411da177e4SLinus Torvalds { 2142affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2143affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2144a62428c0STejun Heo struct work_struct, entry); 2145c34056a3STejun Heo process_one_work(worker, work); 2146a62428c0STejun Heo } 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds 21494690c4abSTejun Heo /** 21504690c4abSTejun Heo * worker_thread - the worker thread function 2151c34056a3STejun Heo * @__worker: self 21524690c4abSTejun Heo * 2153c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2154c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2155c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2156c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2157c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2158d185af30SYacine Belkadi * 2159d185af30SYacine Belkadi * Return: 0 21604690c4abSTejun Heo */ 2161c34056a3STejun Heo static int worker_thread(void *__worker) 21621da177e4SLinus Torvalds { 2163c34056a3STejun Heo struct worker *worker = __worker; 2164bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 21651da177e4SLinus Torvalds 2166e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2167e22bee78STejun Heo worker->task->flags |= PF_WQ_WORKER; 2168c8e55f36STejun Heo woke_up: 2169d565ed63STejun Heo spin_lock_irq(&pool->lock); 2170affee4b2STejun Heo 2171a9ab775bSTejun Heo /* am I supposed to die? */ 2172a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2173d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2174a9ab775bSTejun Heo WARN_ON_ONCE(!list_empty(&worker->entry)); 2175e22bee78STejun Heo worker->task->flags &= ~PF_WQ_WORKER; 217660f5a4bcSLai Jiangshan 217760f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 21787cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, worker->id); 217960f5a4bcSLai Jiangshan worker_detach_from_pool(worker, pool); 218060f5a4bcSLai Jiangshan kfree(worker); 2181c8e55f36STejun Heo return 0; 2182c8e55f36STejun Heo } 2183c8e55f36STejun Heo 2184c8e55f36STejun Heo worker_leave_idle(worker); 2185db7bccf4STejun Heo recheck: 2186e22bee78STejun Heo /* no more worker necessary? */ 218763d95a91STejun Heo if (!need_more_worker(pool)) 2188e22bee78STejun Heo goto sleep; 2189e22bee78STejun Heo 2190e22bee78STejun Heo /* do we need to manage? */ 219163d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2192e22bee78STejun Heo goto recheck; 2193e22bee78STejun Heo 2194c8e55f36STejun Heo /* 2195c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2196c8e55f36STejun Heo * preparing to process a work or actually processing it. 2197c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2198c8e55f36STejun Heo */ 21996183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2200c8e55f36STejun Heo 2201e22bee78STejun Heo /* 2202a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2203a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2204a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2205a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2206a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2207e22bee78STejun Heo */ 2208a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2209e22bee78STejun Heo 2210e22bee78STejun Heo do { 2211affee4b2STejun Heo struct work_struct *work = 2212bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2213affee4b2STejun Heo struct work_struct, entry); 2214affee4b2STejun Heo 221582607adcSTejun Heo pool->watchdog_ts = jiffies; 221682607adcSTejun Heo 2217c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2218affee4b2STejun Heo /* optimization path, not strictly necessary */ 2219affee4b2STejun Heo process_one_work(worker, work); 2220affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2221affee4b2STejun Heo process_scheduled_works(worker); 2222affee4b2STejun Heo } else { 2223c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2224affee4b2STejun Heo process_scheduled_works(worker); 2225affee4b2STejun Heo } 222663d95a91STejun Heo } while (keep_working(pool)); 2227affee4b2STejun Heo 2228228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2229d313dd85STejun Heo sleep: 2230c8e55f36STejun Heo /* 2231d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2232d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2233d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2234d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2235d565ed63STejun Heo * event. 2236c8e55f36STejun Heo */ 2237c8e55f36STejun Heo worker_enter_idle(worker); 2238c8e55f36STejun Heo __set_current_state(TASK_INTERRUPTIBLE); 2239d565ed63STejun Heo spin_unlock_irq(&pool->lock); 22401da177e4SLinus Torvalds schedule(); 2241c8e55f36STejun Heo goto woke_up; 22421da177e4SLinus Torvalds } 22431da177e4SLinus Torvalds 2244e22bee78STejun Heo /** 2245e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2246111c225aSTejun Heo * @__rescuer: self 2247e22bee78STejun Heo * 2248e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2249493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2250e22bee78STejun Heo * 2251706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2252e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2253e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2254e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2255e22bee78STejun Heo * the problem rescuer solves. 2256e22bee78STejun Heo * 2257706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2258706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2259e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2260e22bee78STejun Heo * 2261e22bee78STejun Heo * This should happen rarely. 2262d185af30SYacine Belkadi * 2263d185af30SYacine Belkadi * Return: 0 2264e22bee78STejun Heo */ 2265111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2266e22bee78STejun Heo { 2267111c225aSTejun Heo struct worker *rescuer = __rescuer; 2268111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2269e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 22704d595b86SLai Jiangshan bool should_stop; 2271e22bee78STejun Heo 2272e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2273111c225aSTejun Heo 2274111c225aSTejun Heo /* 2275111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2276111c225aSTejun Heo * doesn't participate in concurrency management. 2277111c225aSTejun Heo */ 2278111c225aSTejun Heo rescuer->task->flags |= PF_WQ_WORKER; 2279e22bee78STejun Heo repeat: 2280e22bee78STejun Heo set_current_state(TASK_INTERRUPTIBLE); 22811da177e4SLinus Torvalds 22824d595b86SLai Jiangshan /* 22834d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 22844d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 22854d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 22864d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 22874d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 22884d595b86SLai Jiangshan * list is always empty on exit. 22894d595b86SLai Jiangshan */ 22904d595b86SLai Jiangshan should_stop = kthread_should_stop(); 22911da177e4SLinus Torvalds 2292493a1724STejun Heo /* see whether any pwq is asking for help */ 22932e109a28STejun Heo spin_lock_irq(&wq_mayday_lock); 2294493a1724STejun Heo 2295493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2296493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2297493a1724STejun Heo struct pool_workqueue, mayday_node); 2298112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2299e22bee78STejun Heo struct work_struct *work, *n; 230082607adcSTejun Heo bool first = true; 2301e22bee78STejun Heo 2302e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2303493a1724STejun Heo list_del_init(&pwq->mayday_node); 2304493a1724STejun Heo 23052e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2306e22bee78STejun Heo 230751697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 230851697d39SLai Jiangshan 230951697d39SLai Jiangshan spin_lock_irq(&pool->lock); 2310b3104104SLai Jiangshan rescuer->pool = pool; 2311e22bee78STejun Heo 2312e22bee78STejun Heo /* 2313e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2314e22bee78STejun Heo * process'em. 2315e22bee78STejun Heo */ 23160479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 231782607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 231882607adcSTejun Heo if (get_work_pwq(work) == pwq) { 231982607adcSTejun Heo if (first) 232082607adcSTejun Heo pool->watchdog_ts = jiffies; 2321e22bee78STejun Heo move_linked_works(work, scheduled, &n); 232282607adcSTejun Heo } 232382607adcSTejun Heo first = false; 232482607adcSTejun Heo } 2325e22bee78STejun Heo 2326008847f6SNeilBrown if (!list_empty(scheduled)) { 2327e22bee78STejun Heo process_scheduled_works(rescuer); 23287576958aSTejun Heo 23297576958aSTejun Heo /* 2330008847f6SNeilBrown * The above execution of rescued work items could 2331008847f6SNeilBrown * have created more to rescue through 2332008847f6SNeilBrown * pwq_activate_first_delayed() or chained 2333008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2334008847f6SNeilBrown * that such back-to-back work items, which may be 2335008847f6SNeilBrown * being used to relieve memory pressure, don't 2336008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2337008847f6SNeilBrown */ 2338008847f6SNeilBrown if (need_to_create_worker(pool)) { 2339008847f6SNeilBrown spin_lock(&wq_mayday_lock); 2340008847f6SNeilBrown get_pwq(pwq); 2341008847f6SNeilBrown list_move_tail(&pwq->mayday_node, &wq->maydays); 2342008847f6SNeilBrown spin_unlock(&wq_mayday_lock); 2343008847f6SNeilBrown } 2344008847f6SNeilBrown } 2345008847f6SNeilBrown 2346008847f6SNeilBrown /* 234777668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 234813b1d625SLai Jiangshan * go away while we're still attached to it. 234977668c8bSLai Jiangshan */ 235077668c8bSLai Jiangshan put_pwq(pwq); 235177668c8bSLai Jiangshan 235277668c8bSLai Jiangshan /* 2353d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 23547576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 23557576958aSTejun Heo * and stalling the execution. 23567576958aSTejun Heo */ 2357d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 235863d95a91STejun Heo wake_up_worker(pool); 23597576958aSTejun Heo 2360b3104104SLai Jiangshan rescuer->pool = NULL; 236113b1d625SLai Jiangshan spin_unlock_irq(&pool->lock); 236213b1d625SLai Jiangshan 236313b1d625SLai Jiangshan worker_detach_from_pool(rescuer, pool); 236413b1d625SLai Jiangshan 236513b1d625SLai Jiangshan spin_lock_irq(&wq_mayday_lock); 23661da177e4SLinus Torvalds } 23671da177e4SLinus Torvalds 23682e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2369493a1724STejun Heo 23704d595b86SLai Jiangshan if (should_stop) { 23714d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 23724d595b86SLai Jiangshan rescuer->task->flags &= ~PF_WQ_WORKER; 23734d595b86SLai Jiangshan return 0; 23744d595b86SLai Jiangshan } 23754d595b86SLai Jiangshan 2376111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2377111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2378e22bee78STejun Heo schedule(); 2379e22bee78STejun Heo goto repeat; 23801da177e4SLinus Torvalds } 23811da177e4SLinus Torvalds 2382fca839c0STejun Heo /** 2383fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2384fca839c0STejun Heo * @target_wq: workqueue being flushed 2385fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2386fca839c0STejun Heo * 2387fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2388fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2389fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2390fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2391fca839c0STejun Heo * a deadlock. 2392fca839c0STejun Heo */ 2393fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2394fca839c0STejun Heo struct work_struct *target_work) 2395fca839c0STejun Heo { 2396fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2397fca839c0STejun Heo struct worker *worker; 2398fca839c0STejun Heo 2399fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2400fca839c0STejun Heo return; 2401fca839c0STejun Heo 2402fca839c0STejun Heo worker = current_wq_worker(); 2403fca839c0STejun Heo 2404fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2405fca839c0STejun Heo "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%pf", 2406fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 240723d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 240823d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2409fca839c0STejun Heo "workqueue: WQ_MEM_RECLAIM %s:%pf is flushing !WQ_MEM_RECLAIM %s:%pf", 2410fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2411fca839c0STejun Heo target_wq->name, target_func); 2412fca839c0STejun Heo } 2413fca839c0STejun Heo 2414fc2e4d70SOleg Nesterov struct wq_barrier { 2415fc2e4d70SOleg Nesterov struct work_struct work; 2416fc2e4d70SOleg Nesterov struct completion done; 24172607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2418fc2e4d70SOleg Nesterov }; 2419fc2e4d70SOleg Nesterov 2420fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2421fc2e4d70SOleg Nesterov { 2422fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2423fc2e4d70SOleg Nesterov complete(&barr->done); 2424fc2e4d70SOleg Nesterov } 2425fc2e4d70SOleg Nesterov 24264690c4abSTejun Heo /** 24274690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2428112202d9STejun Heo * @pwq: pwq to insert barrier into 24294690c4abSTejun Heo * @barr: wq_barrier to insert 2430affee4b2STejun Heo * @target: target work to attach @barr to 2431affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 24324690c4abSTejun Heo * 2433affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2434affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2435affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2436affee4b2STejun Heo * cpu. 2437affee4b2STejun Heo * 2438affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2439affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2440affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2441affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2442affee4b2STejun Heo * after a work with LINKED flag set. 2443affee4b2STejun Heo * 2444affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2445112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 24464690c4abSTejun Heo * 24474690c4abSTejun Heo * CONTEXT: 2448d565ed63STejun Heo * spin_lock_irq(pool->lock). 24494690c4abSTejun Heo */ 2450112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2451affee4b2STejun Heo struct wq_barrier *barr, 2452affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2453fc2e4d70SOleg Nesterov { 2454affee4b2STejun Heo struct list_head *head; 2455affee4b2STejun Heo unsigned int linked = 0; 2456affee4b2STejun Heo 2457dc186ad7SThomas Gleixner /* 2458d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2459dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2460dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2461dc186ad7SThomas Gleixner * might deadlock. 2462dc186ad7SThomas Gleixner */ 2463ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 246422df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 2465fc2e4d70SOleg Nesterov init_completion(&barr->done); 24662607d7a6STejun Heo barr->task = current; 246783c22520SOleg Nesterov 2468affee4b2STejun Heo /* 2469affee4b2STejun Heo * If @target is currently being executed, schedule the 2470affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2471affee4b2STejun Heo */ 2472affee4b2STejun Heo if (worker) 2473affee4b2STejun Heo head = worker->scheduled.next; 2474affee4b2STejun Heo else { 2475affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2476affee4b2STejun Heo 2477affee4b2STejun Heo head = target->entry.next; 2478affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2479affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2480affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2481affee4b2STejun Heo } 2482affee4b2STejun Heo 2483dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2484112202d9STejun Heo insert_work(pwq, &barr->work, head, 2485affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2486fc2e4d70SOleg Nesterov } 2487fc2e4d70SOleg Nesterov 248873f53c4aSTejun Heo /** 2489112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 249073f53c4aSTejun Heo * @wq: workqueue being flushed 249173f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 249273f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 249373f53c4aSTejun Heo * 2494112202d9STejun Heo * Prepare pwqs for workqueue flushing. 249573f53c4aSTejun Heo * 2496112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2497112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2498112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2499112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2500112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 250173f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 250273f53c4aSTejun Heo * 250373f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 250473f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 250573f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 250673f53c4aSTejun Heo * is returned. 250773f53c4aSTejun Heo * 2508112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 250973f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 251073f53c4aSTejun Heo * advanced to @work_color. 251173f53c4aSTejun Heo * 251273f53c4aSTejun Heo * CONTEXT: 25133c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 251473f53c4aSTejun Heo * 2515d185af30SYacine Belkadi * Return: 251673f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 251773f53c4aSTejun Heo * otherwise. 251873f53c4aSTejun Heo */ 2519112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 252073f53c4aSTejun Heo int flush_color, int work_color) 25211da177e4SLinus Torvalds { 252273f53c4aSTejun Heo bool wait = false; 252349e3cf44STejun Heo struct pool_workqueue *pwq; 25241da177e4SLinus Torvalds 252573f53c4aSTejun Heo if (flush_color >= 0) { 25266183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2527112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2528dc186ad7SThomas Gleixner } 252914441960SOleg Nesterov 253049e3cf44STejun Heo for_each_pwq(pwq, wq) { 2531112202d9STejun Heo struct worker_pool *pool = pwq->pool; 25321da177e4SLinus Torvalds 2533b09f4fd3SLai Jiangshan spin_lock_irq(&pool->lock); 253473f53c4aSTejun Heo 253573f53c4aSTejun Heo if (flush_color >= 0) { 25366183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 253773f53c4aSTejun Heo 2538112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2539112202d9STejun Heo pwq->flush_color = flush_color; 2540112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 254173f53c4aSTejun Heo wait = true; 25421da177e4SLinus Torvalds } 254373f53c4aSTejun Heo } 254473f53c4aSTejun Heo 254573f53c4aSTejun Heo if (work_color >= 0) { 25466183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2547112202d9STejun Heo pwq->work_color = work_color; 254873f53c4aSTejun Heo } 254973f53c4aSTejun Heo 2550b09f4fd3SLai Jiangshan spin_unlock_irq(&pool->lock); 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds 2553112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 255473f53c4aSTejun Heo complete(&wq->first_flusher->done); 255573f53c4aSTejun Heo 255673f53c4aSTejun Heo return wait; 255783c22520SOleg Nesterov } 25581da177e4SLinus Torvalds 25590fcb78c2SRolf Eike Beer /** 25601da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 25610fcb78c2SRolf Eike Beer * @wq: workqueue to flush 25621da177e4SLinus Torvalds * 2563c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2564c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 25651da177e4SLinus Torvalds */ 25667ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 25671da177e4SLinus Torvalds { 256873f53c4aSTejun Heo struct wq_flusher this_flusher = { 256973f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 257073f53c4aSTejun Heo .flush_color = -1, 257173f53c4aSTejun Heo .done = COMPLETION_INITIALIZER_ONSTACK(this_flusher.done), 257273f53c4aSTejun Heo }; 257373f53c4aSTejun Heo int next_color; 2574b1f4ec17SOleg Nesterov 25753295f0efSIngo Molnar lock_map_acquire(&wq->lockdep_map); 25763295f0efSIngo Molnar lock_map_release(&wq->lockdep_map); 257773f53c4aSTejun Heo 25783c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 257973f53c4aSTejun Heo 258073f53c4aSTejun Heo /* 258173f53c4aSTejun Heo * Start-to-wait phase 258273f53c4aSTejun Heo */ 258373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 258473f53c4aSTejun Heo 258573f53c4aSTejun Heo if (next_color != wq->flush_color) { 258673f53c4aSTejun Heo /* 258773f53c4aSTejun Heo * Color space is not full. The current work_color 258873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 258973f53c4aSTejun Heo * by one. 259073f53c4aSTejun Heo */ 25916183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 259273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 259373f53c4aSTejun Heo wq->work_color = next_color; 259473f53c4aSTejun Heo 259573f53c4aSTejun Heo if (!wq->first_flusher) { 259673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 25976183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 259873f53c4aSTejun Heo 259973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 260073f53c4aSTejun Heo 2601112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 260273f53c4aSTejun Heo wq->work_color)) { 260373f53c4aSTejun Heo /* nothing to flush, done */ 260473f53c4aSTejun Heo wq->flush_color = next_color; 260573f53c4aSTejun Heo wq->first_flusher = NULL; 260673f53c4aSTejun Heo goto out_unlock; 260773f53c4aSTejun Heo } 260873f53c4aSTejun Heo } else { 260973f53c4aSTejun Heo /* wait in queue */ 26106183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 261173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2612112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 261373f53c4aSTejun Heo } 261473f53c4aSTejun Heo } else { 261573f53c4aSTejun Heo /* 261673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 261773f53c4aSTejun Heo * The next flush completion will assign us 261873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 261973f53c4aSTejun Heo */ 262073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 262173f53c4aSTejun Heo } 262273f53c4aSTejun Heo 2623fca839c0STejun Heo check_flush_dependency(wq, NULL); 2624fca839c0STejun Heo 26253c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 262673f53c4aSTejun Heo 262773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 262873f53c4aSTejun Heo 262973f53c4aSTejun Heo /* 263073f53c4aSTejun Heo * Wake-up-and-cascade phase 263173f53c4aSTejun Heo * 263273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 263373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 263473f53c4aSTejun Heo */ 263573f53c4aSTejun Heo if (wq->first_flusher != &this_flusher) 263673f53c4aSTejun Heo return; 263773f53c4aSTejun Heo 26383c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 263973f53c4aSTejun Heo 26404ce48b37STejun Heo /* we might have raced, check again with mutex held */ 26414ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 26424ce48b37STejun Heo goto out_unlock; 26434ce48b37STejun Heo 264473f53c4aSTejun Heo wq->first_flusher = NULL; 264573f53c4aSTejun Heo 26466183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 26476183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 264873f53c4aSTejun Heo 264973f53c4aSTejun Heo while (true) { 265073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 265173f53c4aSTejun Heo 265273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 265373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 265473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 265573f53c4aSTejun Heo break; 265673f53c4aSTejun Heo list_del_init(&next->list); 265773f53c4aSTejun Heo complete(&next->done); 265873f53c4aSTejun Heo } 265973f53c4aSTejun Heo 26606183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 266173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 266273f53c4aSTejun Heo 266373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 266473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 266573f53c4aSTejun Heo 266673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 266773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 266873f53c4aSTejun Heo /* 266973f53c4aSTejun Heo * Assign the same color to all overflowed 267073f53c4aSTejun Heo * flushers, advance work_color and append to 267173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 267273f53c4aSTejun Heo * phase for these overflowed flushers. 267373f53c4aSTejun Heo */ 267473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 267573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 267673f53c4aSTejun Heo 267773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 267873f53c4aSTejun Heo 267973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 268073f53c4aSTejun Heo &wq->flusher_queue); 2681112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 268273f53c4aSTejun Heo } 268373f53c4aSTejun Heo 268473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 26856183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 268673f53c4aSTejun Heo break; 268773f53c4aSTejun Heo } 268873f53c4aSTejun Heo 268973f53c4aSTejun Heo /* 269073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2691112202d9STejun Heo * the new first flusher and arm pwqs. 269273f53c4aSTejun Heo */ 26936183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 26946183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 269573f53c4aSTejun Heo 269673f53c4aSTejun Heo list_del_init(&next->list); 269773f53c4aSTejun Heo wq->first_flusher = next; 269873f53c4aSTejun Heo 2699112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 270073f53c4aSTejun Heo break; 270173f53c4aSTejun Heo 270273f53c4aSTejun Heo /* 270373f53c4aSTejun Heo * Meh... this color is already done, clear first 270473f53c4aSTejun Heo * flusher and repeat cascading. 270573f53c4aSTejun Heo */ 270673f53c4aSTejun Heo wq->first_flusher = NULL; 270773f53c4aSTejun Heo } 270873f53c4aSTejun Heo 270973f53c4aSTejun Heo out_unlock: 27103c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 27111da177e4SLinus Torvalds } 27121dadafa8STim Gardner EXPORT_SYMBOL(flush_workqueue); 27131da177e4SLinus Torvalds 27149c5a2ba7STejun Heo /** 27159c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 27169c5a2ba7STejun Heo * @wq: workqueue to drain 27179c5a2ba7STejun Heo * 27189c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 27199c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 27209c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 2721b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 27229c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 27239c5a2ba7STejun Heo * takes too long. 27249c5a2ba7STejun Heo */ 27259c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 27269c5a2ba7STejun Heo { 27279c5a2ba7STejun Heo unsigned int flush_cnt = 0; 272849e3cf44STejun Heo struct pool_workqueue *pwq; 27299c5a2ba7STejun Heo 27309c5a2ba7STejun Heo /* 27319c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 27329c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 2733618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 27349c5a2ba7STejun Heo */ 273587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 27369c5a2ba7STejun Heo if (!wq->nr_drainers++) 2737618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 273887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 27399c5a2ba7STejun Heo reflush: 27409c5a2ba7STejun Heo flush_workqueue(wq); 27419c5a2ba7STejun Heo 2742b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 274376af4d93STejun Heo 274449e3cf44STejun Heo for_each_pwq(pwq, wq) { 2745fa2563e4SThomas Tuttle bool drained; 27469c5a2ba7STejun Heo 2747b09f4fd3SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 2748112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2749b09f4fd3SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 2750fa2563e4SThomas Tuttle 2751fa2563e4SThomas Tuttle if (drained) 27529c5a2ba7STejun Heo continue; 27539c5a2ba7STejun Heo 27549c5a2ba7STejun Heo if (++flush_cnt == 10 || 27559c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2756c5aa87bbSTejun Heo pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n", 27579c5a2ba7STejun Heo wq->name, flush_cnt); 275876af4d93STejun Heo 2759b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 27609c5a2ba7STejun Heo goto reflush; 27619c5a2ba7STejun Heo } 27629c5a2ba7STejun Heo 27639c5a2ba7STejun Heo if (!--wq->nr_drainers) 2764618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 276587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 27669c5a2ba7STejun Heo } 27679c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 27689c5a2ba7STejun Heo 2769606a5020STejun Heo static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr) 2770baf59022STejun Heo { 2771baf59022STejun Heo struct worker *worker = NULL; 2772c9e7cf27STejun Heo struct worker_pool *pool; 2773112202d9STejun Heo struct pool_workqueue *pwq; 2774baf59022STejun Heo 2775baf59022STejun Heo might_sleep(); 2776baf59022STejun Heo 2777fa1b54e6STejun Heo local_irq_disable(); 2778fa1b54e6STejun Heo pool = get_work_pool(work); 2779fa1b54e6STejun Heo if (!pool) { 2780fa1b54e6STejun Heo local_irq_enable(); 2781fa1b54e6STejun Heo return false; 2782fa1b54e6STejun Heo } 2783fa1b54e6STejun Heo 2784fa1b54e6STejun Heo spin_lock(&pool->lock); 27850b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2786112202d9STejun Heo pwq = get_work_pwq(work); 2787112202d9STejun Heo if (pwq) { 2788112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2789baf59022STejun Heo goto already_gone; 2790606a5020STejun Heo } else { 2791c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2792baf59022STejun Heo if (!worker) 2793baf59022STejun Heo goto already_gone; 2794112202d9STejun Heo pwq = worker->current_pwq; 2795606a5020STejun Heo } 2796baf59022STejun Heo 2797fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 2798fca839c0STejun Heo 2799112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 2800d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2801baf59022STejun Heo 2802e159489bSTejun Heo /* 2803e159489bSTejun Heo * If @max_active is 1 or rescuer is in use, flushing another work 2804e159489bSTejun Heo * item on the same workqueue may lead to deadlock. Make sure the 2805e159489bSTejun Heo * flusher is not running on the same workqueue by verifying write 2806e159489bSTejun Heo * access. 2807e159489bSTejun Heo */ 2808493008a8STejun Heo if (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer) 2809112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 2810e159489bSTejun Heo else 2811112202d9STejun Heo lock_map_acquire_read(&pwq->wq->lockdep_map); 2812112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 2813e159489bSTejun Heo 2814baf59022STejun Heo return true; 2815baf59022STejun Heo already_gone: 2816d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2817baf59022STejun Heo return false; 2818baf59022STejun Heo } 2819baf59022STejun Heo 2820db700897SOleg Nesterov /** 2821401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 2822401a8d04STejun Heo * @work: the work to flush 2823db700897SOleg Nesterov * 2824606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 2825606a5020STejun Heo * on return if it hasn't been requeued since flush started. 2826401a8d04STejun Heo * 2827d185af30SYacine Belkadi * Return: 2828401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2829401a8d04STejun Heo * %false if it was already idle. 2830db700897SOleg Nesterov */ 2831401a8d04STejun Heo bool flush_work(struct work_struct *work) 2832db700897SOleg Nesterov { 283312997d1aSBjorn Helgaas struct wq_barrier barr; 283412997d1aSBjorn Helgaas 28350976dfc1SStephen Boyd lock_map_acquire(&work->lockdep_map); 28360976dfc1SStephen Boyd lock_map_release(&work->lockdep_map); 28370976dfc1SStephen Boyd 283812997d1aSBjorn Helgaas if (start_flush_work(work, &barr)) { 283912997d1aSBjorn Helgaas wait_for_completion(&barr.done); 284012997d1aSBjorn Helgaas destroy_work_on_stack(&barr.work); 284112997d1aSBjorn Helgaas return true; 284212997d1aSBjorn Helgaas } else { 284312997d1aSBjorn Helgaas return false; 284412997d1aSBjorn Helgaas } 2845606a5020STejun Heo } 2846db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 2847db700897SOleg Nesterov 28488603e1b3STejun Heo struct cwt_wait { 28498603e1b3STejun Heo wait_queue_t wait; 28508603e1b3STejun Heo struct work_struct *work; 28518603e1b3STejun Heo }; 28528603e1b3STejun Heo 28538603e1b3STejun Heo static int cwt_wakefn(wait_queue_t *wait, unsigned mode, int sync, void *key) 28548603e1b3STejun Heo { 28558603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 28568603e1b3STejun Heo 28578603e1b3STejun Heo if (cwait->work != key) 28588603e1b3STejun Heo return 0; 28598603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 28608603e1b3STejun Heo } 28618603e1b3STejun Heo 286236e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 2863401a8d04STejun Heo { 28648603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 2865bbb68dfaSTejun Heo unsigned long flags; 28661f1f642eSOleg Nesterov int ret; 28671f1f642eSOleg Nesterov 28681f1f642eSOleg Nesterov do { 2869bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 2870bbb68dfaSTejun Heo /* 28718603e1b3STejun Heo * If someone else is already canceling, wait for it to 28728603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 28738603e1b3STejun Heo * because we may get scheduled between @work's completion 28748603e1b3STejun Heo * and the other canceling task resuming and clearing 28758603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 28768603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 28778603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 28788603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 28798603e1b3STejun Heo * we're hogging the CPU. 28808603e1b3STejun Heo * 28818603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 28828603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 28838603e1b3STejun Heo * wake function which matches @work along with exclusive 28848603e1b3STejun Heo * wait and wakeup. 2885bbb68dfaSTejun Heo */ 28868603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 28878603e1b3STejun Heo struct cwt_wait cwait; 28888603e1b3STejun Heo 28898603e1b3STejun Heo init_wait(&cwait.wait); 28908603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 28918603e1b3STejun Heo cwait.work = work; 28928603e1b3STejun Heo 28938603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 28948603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 28958603e1b3STejun Heo if (work_is_canceling(work)) 28968603e1b3STejun Heo schedule(); 28978603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 28988603e1b3STejun Heo } 28991f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 29001f1f642eSOleg Nesterov 2901bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 2902bbb68dfaSTejun Heo mark_work_canceling(work); 2903bbb68dfaSTejun Heo local_irq_restore(flags); 2904bbb68dfaSTejun Heo 2905606a5020STejun Heo flush_work(work); 29067a22ad75STejun Heo clear_work_data(work); 29078603e1b3STejun Heo 29088603e1b3STejun Heo /* 29098603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 29108603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 29118603e1b3STejun Heo * visible there. 29128603e1b3STejun Heo */ 29138603e1b3STejun Heo smp_mb(); 29148603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 29158603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 29168603e1b3STejun Heo 29171f1f642eSOleg Nesterov return ret; 29181f1f642eSOleg Nesterov } 29191f1f642eSOleg Nesterov 29206e84d644SOleg Nesterov /** 2921401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 2922401a8d04STejun Heo * @work: the work to cancel 29236e84d644SOleg Nesterov * 2924401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 2925401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 2926401a8d04STejun Heo * another workqueue. On return from this function, @work is 2927401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 29281f1f642eSOleg Nesterov * 2929401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 2930401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 29316e84d644SOleg Nesterov * 2932401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 29336e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 2934401a8d04STejun Heo * 2935d185af30SYacine Belkadi * Return: 2936401a8d04STejun Heo * %true if @work was pending, %false otherwise. 29376e84d644SOleg Nesterov */ 2938401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 29396e84d644SOleg Nesterov { 294036e227d2STejun Heo return __cancel_work_timer(work, false); 2941b89deed3SOleg Nesterov } 294228e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 2943b89deed3SOleg Nesterov 29446e84d644SOleg Nesterov /** 2945401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 2946401a8d04STejun Heo * @dwork: the delayed work to flush 29476e84d644SOleg Nesterov * 2948401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 2949401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 2950401a8d04STejun Heo * considers the last queueing instance of @dwork. 29511f1f642eSOleg Nesterov * 2952d185af30SYacine Belkadi * Return: 2953401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 2954401a8d04STejun Heo * %false if it was already idle. 29556e84d644SOleg Nesterov */ 2956401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 2957401a8d04STejun Heo { 29588930cabaSTejun Heo local_irq_disable(); 2959401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 296060c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 29618930cabaSTejun Heo local_irq_enable(); 2962401a8d04STejun Heo return flush_work(&dwork->work); 2963401a8d04STejun Heo } 2964401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 2965401a8d04STejun Heo 2966401a8d04STejun Heo /** 296757b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 296857b30ae7STejun Heo * @dwork: delayed_work to cancel 296909383498STejun Heo * 2970d185af30SYacine Belkadi * Kill off a pending delayed_work. 2971d185af30SYacine Belkadi * 2972d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 2973d185af30SYacine Belkadi * pending. 2974d185af30SYacine Belkadi * 2975d185af30SYacine Belkadi * Note: 2976d185af30SYacine Belkadi * The work callback function may still be running on return, unless 2977d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 2978d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 297909383498STejun Heo * 298057b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 298109383498STejun Heo */ 298257b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 298309383498STejun Heo { 298457b30ae7STejun Heo unsigned long flags; 298557b30ae7STejun Heo int ret; 298657b30ae7STejun Heo 298757b30ae7STejun Heo do { 298857b30ae7STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 298957b30ae7STejun Heo } while (unlikely(ret == -EAGAIN)); 299057b30ae7STejun Heo 299157b30ae7STejun Heo if (unlikely(ret < 0)) 299257b30ae7STejun Heo return false; 299357b30ae7STejun Heo 29947c3eed5cSTejun Heo set_work_pool_and_clear_pending(&dwork->work, 29957c3eed5cSTejun Heo get_work_pool_id(&dwork->work)); 299657b30ae7STejun Heo local_irq_restore(flags); 2997c0158ca6SDan Magenheimer return ret; 299809383498STejun Heo } 299957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 300009383498STejun Heo 300109383498STejun Heo /** 3002401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3003401a8d04STejun Heo * @dwork: the delayed work cancel 3004401a8d04STejun Heo * 3005401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3006401a8d04STejun Heo * 3007d185af30SYacine Belkadi * Return: 3008401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3009401a8d04STejun Heo */ 3010401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 30116e84d644SOleg Nesterov { 301236e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 30136e84d644SOleg Nesterov } 3014f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 30151da177e4SLinus Torvalds 30160fcb78c2SRolf Eike Beer /** 301731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3018b6136773SAndrew Morton * @func: the function to call 3019b6136773SAndrew Morton * 302031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 302131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3022b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 302331ddd871STejun Heo * 3024d185af30SYacine Belkadi * Return: 302531ddd871STejun Heo * 0 on success, -errno on failure. 3026b6136773SAndrew Morton */ 302765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 302815316ba8SChristoph Lameter { 302915316ba8SChristoph Lameter int cpu; 303038f51568SNamhyung Kim struct work_struct __percpu *works; 303115316ba8SChristoph Lameter 3032b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3033b6136773SAndrew Morton if (!works) 303415316ba8SChristoph Lameter return -ENOMEM; 3035b6136773SAndrew Morton 303695402b38SGautham R Shenoy get_online_cpus(); 303793981800STejun Heo 303815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 30399bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 30409bfb1839SIngo Molnar 30419bfb1839SIngo Molnar INIT_WORK(work, func); 30428de6d308SOleg Nesterov schedule_work_on(cpu, work); 304315316ba8SChristoph Lameter } 304493981800STejun Heo 304593981800STejun Heo for_each_online_cpu(cpu) 30468616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 304793981800STejun Heo 304895402b38SGautham R Shenoy put_online_cpus(); 3049b6136773SAndrew Morton free_percpu(works); 305015316ba8SChristoph Lameter return 0; 305115316ba8SChristoph Lameter } 305215316ba8SChristoph Lameter 3053eef6a7d5SAlan Stern /** 30541fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 30551fa44ecaSJames Bottomley * @fn: the function to execute 30561fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 30571fa44ecaSJames Bottomley * be available when the work executes) 30581fa44ecaSJames Bottomley * 30591fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 30601fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 30611fa44ecaSJames Bottomley * 3062d185af30SYacine Belkadi * Return: 0 - function was executed 30631fa44ecaSJames Bottomley * 1 - function was scheduled for execution 30641fa44ecaSJames Bottomley */ 306565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 30661fa44ecaSJames Bottomley { 30671fa44ecaSJames Bottomley if (!in_interrupt()) { 306865f27f38SDavid Howells fn(&ew->work); 30691fa44ecaSJames Bottomley return 0; 30701fa44ecaSJames Bottomley } 30711fa44ecaSJames Bottomley 307265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 30731fa44ecaSJames Bottomley schedule_work(&ew->work); 30741fa44ecaSJames Bottomley 30751fa44ecaSJames Bottomley return 1; 30761fa44ecaSJames Bottomley } 30771fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 30781fa44ecaSJames Bottomley 30797a4e344cSTejun Heo /** 30807a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 30817a4e344cSTejun Heo * @attrs: workqueue_attrs to free 30827a4e344cSTejun Heo * 30837a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 30847a4e344cSTejun Heo */ 30857a4e344cSTejun Heo void free_workqueue_attrs(struct workqueue_attrs *attrs) 30867a4e344cSTejun Heo { 30877a4e344cSTejun Heo if (attrs) { 30887a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 30897a4e344cSTejun Heo kfree(attrs); 30907a4e344cSTejun Heo } 30917a4e344cSTejun Heo } 30927a4e344cSTejun Heo 30937a4e344cSTejun Heo /** 30947a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 30957a4e344cSTejun Heo * @gfp_mask: allocation mask to use 30967a4e344cSTejun Heo * 30977a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3098d185af30SYacine Belkadi * return it. 3099d185af30SYacine Belkadi * 3100d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 31017a4e344cSTejun Heo */ 31027a4e344cSTejun Heo struct workqueue_attrs *alloc_workqueue_attrs(gfp_t gfp_mask) 31037a4e344cSTejun Heo { 31047a4e344cSTejun Heo struct workqueue_attrs *attrs; 31057a4e344cSTejun Heo 31067a4e344cSTejun Heo attrs = kzalloc(sizeof(*attrs), gfp_mask); 31077a4e344cSTejun Heo if (!attrs) 31087a4e344cSTejun Heo goto fail; 31097a4e344cSTejun Heo if (!alloc_cpumask_var(&attrs->cpumask, gfp_mask)) 31107a4e344cSTejun Heo goto fail; 31117a4e344cSTejun Heo 311213e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 31137a4e344cSTejun Heo return attrs; 31147a4e344cSTejun Heo fail: 31157a4e344cSTejun Heo free_workqueue_attrs(attrs); 31167a4e344cSTejun Heo return NULL; 31177a4e344cSTejun Heo } 31187a4e344cSTejun Heo 311929c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 312029c91e99STejun Heo const struct workqueue_attrs *from) 312129c91e99STejun Heo { 312229c91e99STejun Heo to->nice = from->nice; 312329c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 31242865a8fbSShaohua Li /* 31252865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 31262865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 31272865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 31282865a8fbSShaohua Li */ 31292865a8fbSShaohua Li to->no_numa = from->no_numa; 313029c91e99STejun Heo } 313129c91e99STejun Heo 313229c91e99STejun Heo /* hash value of the content of @attr */ 313329c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 313429c91e99STejun Heo { 313529c91e99STejun Heo u32 hash = 0; 313629c91e99STejun Heo 313729c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 313813e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 313913e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 314029c91e99STejun Heo return hash; 314129c91e99STejun Heo } 314229c91e99STejun Heo 314329c91e99STejun Heo /* content equality test */ 314429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 314529c91e99STejun Heo const struct workqueue_attrs *b) 314629c91e99STejun Heo { 314729c91e99STejun Heo if (a->nice != b->nice) 314829c91e99STejun Heo return false; 314929c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 315029c91e99STejun Heo return false; 315129c91e99STejun Heo return true; 315229c91e99STejun Heo } 315329c91e99STejun Heo 31547a4e344cSTejun Heo /** 31557a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 31567a4e344cSTejun Heo * @pool: worker_pool to initialize 31577a4e344cSTejun Heo * 3158402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3159d185af30SYacine Belkadi * 3160d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 316129c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 316229c91e99STejun Heo * on @pool safely to release it. 31637a4e344cSTejun Heo */ 31647a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 31654e1a1f9aSTejun Heo { 31664e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 316729c91e99STejun Heo pool->id = -1; 316829c91e99STejun Heo pool->cpu = -1; 3169f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 31704e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 317182607adcSTejun Heo pool->watchdog_ts = jiffies; 31724e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 31734e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 31744e1a1f9aSTejun Heo hash_init(pool->busy_hash); 31754e1a1f9aSTejun Heo 31764e1a1f9aSTejun Heo init_timer_deferrable(&pool->idle_timer); 31774e1a1f9aSTejun Heo pool->idle_timer.function = idle_worker_timeout; 31784e1a1f9aSTejun Heo pool->idle_timer.data = (unsigned long)pool; 31794e1a1f9aSTejun Heo 31804e1a1f9aSTejun Heo setup_timer(&pool->mayday_timer, pool_mayday_timeout, 31814e1a1f9aSTejun Heo (unsigned long)pool); 31824e1a1f9aSTejun Heo 31834e1a1f9aSTejun Heo mutex_init(&pool->manager_arb); 318492f9c5c4SLai Jiangshan mutex_init(&pool->attach_mutex); 3185da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 31867a4e344cSTejun Heo 31877cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 318829c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 318929c91e99STejun Heo pool->refcnt = 1; 319029c91e99STejun Heo 319129c91e99STejun Heo /* shouldn't fail above this point */ 31927a4e344cSTejun Heo pool->attrs = alloc_workqueue_attrs(GFP_KERNEL); 31937a4e344cSTejun Heo if (!pool->attrs) 31947a4e344cSTejun Heo return -ENOMEM; 31957a4e344cSTejun Heo return 0; 31964e1a1f9aSTejun Heo } 31974e1a1f9aSTejun Heo 3198e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3199e2dca7adSTejun Heo { 3200e2dca7adSTejun Heo struct workqueue_struct *wq = 3201e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3202e2dca7adSTejun Heo 3203e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3204e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3205e2dca7adSTejun Heo else 3206e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3207e2dca7adSTejun Heo 3208e2dca7adSTejun Heo kfree(wq->rescuer); 3209e2dca7adSTejun Heo kfree(wq); 3210e2dca7adSTejun Heo } 3211e2dca7adSTejun Heo 321229c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 321329c91e99STejun Heo { 321429c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 321529c91e99STejun Heo 32167cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 321729c91e99STejun Heo free_workqueue_attrs(pool->attrs); 321829c91e99STejun Heo kfree(pool); 321929c91e99STejun Heo } 322029c91e99STejun Heo 322129c91e99STejun Heo /** 322229c91e99STejun Heo * put_unbound_pool - put a worker_pool 322329c91e99STejun Heo * @pool: worker_pool to put 322429c91e99STejun Heo * 322529c91e99STejun Heo * Put @pool. If its refcnt reaches zero, it gets destroyed in sched-RCU 3226c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3227c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3228c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3229a892caccSTejun Heo * 3230a892caccSTejun Heo * Should be called with wq_pool_mutex held. 323129c91e99STejun Heo */ 323229c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 323329c91e99STejun Heo { 323460f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 323529c91e99STejun Heo struct worker *worker; 323629c91e99STejun Heo 3237a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3238a892caccSTejun Heo 3239a892caccSTejun Heo if (--pool->refcnt) 324029c91e99STejun Heo return; 324129c91e99STejun Heo 324229c91e99STejun Heo /* sanity checks */ 324361d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3244a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 324529c91e99STejun Heo return; 324629c91e99STejun Heo 324729c91e99STejun Heo /* release id and unhash */ 324829c91e99STejun Heo if (pool->id >= 0) 324929c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 325029c91e99STejun Heo hash_del(&pool->hash_node); 325129c91e99STejun Heo 3252c5aa87bbSTejun Heo /* 3253c5aa87bbSTejun Heo * Become the manager and destroy all workers. Grabbing 3254c5aa87bbSTejun Heo * manager_arb prevents @pool's workers from blocking on 325592f9c5c4SLai Jiangshan * attach_mutex. 3256c5aa87bbSTejun Heo */ 325729c91e99STejun Heo mutex_lock(&pool->manager_arb); 325829c91e99STejun Heo 325960f5a4bcSLai Jiangshan spin_lock_irq(&pool->lock); 32601037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 326129c91e99STejun Heo destroy_worker(worker); 326229c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 326329c91e99STejun Heo spin_unlock_irq(&pool->lock); 326460f5a4bcSLai Jiangshan 326592f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 3266da028469SLai Jiangshan if (!list_empty(&pool->workers)) 326760f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 326892f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 326960f5a4bcSLai Jiangshan 327060f5a4bcSLai Jiangshan if (pool->detach_completion) 327160f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 327260f5a4bcSLai Jiangshan 327329c91e99STejun Heo mutex_unlock(&pool->manager_arb); 327429c91e99STejun Heo 327529c91e99STejun Heo /* shut down the timers */ 327629c91e99STejun Heo del_timer_sync(&pool->idle_timer); 327729c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 327829c91e99STejun Heo 327929c91e99STejun Heo /* sched-RCU protected to allow dereferences from get_work_pool() */ 328029c91e99STejun Heo call_rcu_sched(&pool->rcu, rcu_free_pool); 328129c91e99STejun Heo } 328229c91e99STejun Heo 328329c91e99STejun Heo /** 328429c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 328529c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 328629c91e99STejun Heo * 328729c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 328829c91e99STejun Heo * reference count and return it. If there already is a matching 328929c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3290d185af30SYacine Belkadi * create a new one. 3291a892caccSTejun Heo * 3292a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3293d185af30SYacine Belkadi * 3294d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3295d185af30SYacine Belkadi * On failure, %NULL. 329629c91e99STejun Heo */ 329729c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 329829c91e99STejun Heo { 329929c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 330029c91e99STejun Heo struct worker_pool *pool; 3301f3f90ad4STejun Heo int node; 3302e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 330329c91e99STejun Heo 3304a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 330529c91e99STejun Heo 330629c91e99STejun Heo /* do we already have a matching pool? */ 330729c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 330829c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 330929c91e99STejun Heo pool->refcnt++; 33103fb1823cSLai Jiangshan return pool; 331129c91e99STejun Heo } 331229c91e99STejun Heo } 331329c91e99STejun Heo 3314e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3315e2273584SXunlei Pang if (wq_numa_enabled) { 3316e2273584SXunlei Pang for_each_node(node) { 3317e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3318e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3319e2273584SXunlei Pang target_node = node; 3320e2273584SXunlei Pang break; 3321e2273584SXunlei Pang } 3322e2273584SXunlei Pang } 3323e2273584SXunlei Pang } 3324e2273584SXunlei Pang 332529c91e99STejun Heo /* nope, create a new one */ 3326e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 332729c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 332829c91e99STejun Heo goto fail; 332929c91e99STejun Heo 33308864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 333129c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3332e2273584SXunlei Pang pool->node = target_node; 333329c91e99STejun Heo 33342865a8fbSShaohua Li /* 33352865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 33362865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 33372865a8fbSShaohua Li */ 33382865a8fbSShaohua Li pool->attrs->no_numa = false; 33392865a8fbSShaohua Li 334029c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 334129c91e99STejun Heo goto fail; 334229c91e99STejun Heo 334329c91e99STejun Heo /* create and start the initial worker */ 3344051e1850SLai Jiangshan if (!create_worker(pool)) 334529c91e99STejun Heo goto fail; 334629c91e99STejun Heo 334729c91e99STejun Heo /* install */ 334829c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 33493fb1823cSLai Jiangshan 335029c91e99STejun Heo return pool; 335129c91e99STejun Heo fail: 335229c91e99STejun Heo if (pool) 335329c91e99STejun Heo put_unbound_pool(pool); 335429c91e99STejun Heo return NULL; 335529c91e99STejun Heo } 335629c91e99STejun Heo 33578864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 33588864b4e5STejun Heo { 33598864b4e5STejun Heo kmem_cache_free(pwq_cache, 33608864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 33618864b4e5STejun Heo } 33628864b4e5STejun Heo 33638864b4e5STejun Heo /* 33648864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 33658864b4e5STejun Heo * and needs to be destroyed. 33668864b4e5STejun Heo */ 33678864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 33688864b4e5STejun Heo { 33698864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 33708864b4e5STejun Heo unbound_release_work); 33718864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 33728864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3373bc0caf09STejun Heo bool is_last; 33748864b4e5STejun Heo 33758864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 33768864b4e5STejun Heo return; 33778864b4e5STejun Heo 33783c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 33798864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3380bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 33813c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 33828864b4e5STejun Heo 3383a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 33848864b4e5STejun Heo put_unbound_pool(pool); 3385a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3386a892caccSTejun Heo 33878864b4e5STejun Heo call_rcu_sched(&pwq->rcu, rcu_free_pwq); 33888864b4e5STejun Heo 33898864b4e5STejun Heo /* 33908864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3391e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 33928864b4e5STejun Heo */ 3393e2dca7adSTejun Heo if (is_last) 3394e2dca7adSTejun Heo call_rcu_sched(&wq->rcu, rcu_free_wq); 33956029a918STejun Heo } 33968864b4e5STejun Heo 33970fbd95aaSTejun Heo /** 3398699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 33990fbd95aaSTejun Heo * @pwq: target pool_workqueue 34000fbd95aaSTejun Heo * 3401699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3402699ce097STejun Heo * workqueue's saved_max_active and activate delayed work items 3403699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 34040fbd95aaSTejun Heo */ 3405699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 34060fbd95aaSTejun Heo { 3407699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3408699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 3409699ce097STejun Heo 3410699ce097STejun Heo /* for @wq->saved_max_active */ 3411a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3412699ce097STejun Heo 3413699ce097STejun Heo /* fast exit for non-freezable wqs */ 3414699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3415699ce097STejun Heo return; 3416699ce097STejun Heo 3417a357fc03SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 3418699ce097STejun Heo 341974b414eaSLai Jiangshan /* 342074b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 342174b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 342274b414eaSLai Jiangshan * is updated and visible. 342374b414eaSLai Jiangshan */ 342474b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 3425699ce097STejun Heo pwq->max_active = wq->saved_max_active; 34260fbd95aaSTejun Heo 34270fbd95aaSTejun Heo while (!list_empty(&pwq->delayed_works) && 34280fbd95aaSTejun Heo pwq->nr_active < pwq->max_active) 34290fbd95aaSTejun Heo pwq_activate_first_delayed(pwq); 3430951a078aSLai Jiangshan 3431951a078aSLai Jiangshan /* 3432951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 3433951a078aSLai Jiangshan * max_active is bumped. It's a slow path. Do it always. 3434951a078aSLai Jiangshan */ 3435951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3436699ce097STejun Heo } else { 3437699ce097STejun Heo pwq->max_active = 0; 3438699ce097STejun Heo } 3439699ce097STejun Heo 3440a357fc03SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 34410fbd95aaSTejun Heo } 34420fbd95aaSTejun Heo 3443e50aba9aSTejun Heo /* initialize newly alloced @pwq which is associated with @wq and @pool */ 3444f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3445f147f29eSTejun Heo struct worker_pool *pool) 3446d2c1d404STejun Heo { 3447d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3448d2c1d404STejun Heo 3449e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3450e50aba9aSTejun Heo 3451d2c1d404STejun Heo pwq->pool = pool; 3452d2c1d404STejun Heo pwq->wq = wq; 3453d2c1d404STejun Heo pwq->flush_color = -1; 34548864b4e5STejun Heo pwq->refcnt = 1; 3455d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 34561befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3457d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 34588864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3459f147f29eSTejun Heo } 3460d2c1d404STejun Heo 3461f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 34621befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3463f147f29eSTejun Heo { 3464f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3465f147f29eSTejun Heo 3466f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 346775ccf595STejun Heo 34681befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 34691befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 34701befcf30STejun Heo return; 34711befcf30STejun Heo 347229b1cb41SLai Jiangshan /* set the matching work_color */ 347375ccf595STejun Heo pwq->work_color = wq->work_color; 3474983ca25eSTejun Heo 3475983ca25eSTejun Heo /* sync max_active to the current setting */ 3476983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3477983ca25eSTejun Heo 3478983ca25eSTejun Heo /* link in @pwq */ 34799e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3480df2d5ae4STejun Heo } 34816029a918STejun Heo 3482f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3483f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3484f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3485f147f29eSTejun Heo { 3486f147f29eSTejun Heo struct worker_pool *pool; 3487f147f29eSTejun Heo struct pool_workqueue *pwq; 3488f147f29eSTejun Heo 3489f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3490f147f29eSTejun Heo 3491f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3492f147f29eSTejun Heo if (!pool) 3493f147f29eSTejun Heo return NULL; 3494f147f29eSTejun Heo 3495e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3496f147f29eSTejun Heo if (!pwq) { 3497f147f29eSTejun Heo put_unbound_pool(pool); 3498f147f29eSTejun Heo return NULL; 3499f147f29eSTejun Heo } 3500f147f29eSTejun Heo 3501f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3502f147f29eSTejun Heo return pwq; 3503d2c1d404STejun Heo } 3504d2c1d404STejun Heo 35054c16bd32STejun Heo /** 350630186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 3507042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 35084c16bd32STejun Heo * @node: the target NUMA node 35094c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 35104c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 35114c16bd32STejun Heo * 35124c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 35134c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3514d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 35154c16bd32STejun Heo * 35164c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 35174c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 35184c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 35194c16bd32STejun Heo * @attrs->cpumask. 35204c16bd32STejun Heo * 35214c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 35224c16bd32STejun Heo * stable. 3523d185af30SYacine Belkadi * 3524d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 3525d185af30SYacine Belkadi * %false if equal. 35264c16bd32STejun Heo */ 35274c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 35284c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 35294c16bd32STejun Heo { 3530d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 35314c16bd32STejun Heo goto use_dfl; 35324c16bd32STejun Heo 35334c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 35344c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 35354c16bd32STejun Heo if (cpu_going_down >= 0) 35364c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 35374c16bd32STejun Heo 35384c16bd32STejun Heo if (cpumask_empty(cpumask)) 35394c16bd32STejun Heo goto use_dfl; 35404c16bd32STejun Heo 35414c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 35424c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 35434c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 35444c16bd32STejun Heo 35454c16bd32STejun Heo use_dfl: 35464c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 35474c16bd32STejun Heo return false; 35484c16bd32STejun Heo } 35494c16bd32STejun Heo 35501befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 35511befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 35521befcf30STejun Heo int node, 35531befcf30STejun Heo struct pool_workqueue *pwq) 35541befcf30STejun Heo { 35551befcf30STejun Heo struct pool_workqueue *old_pwq; 35561befcf30STejun Heo 35575b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 35581befcf30STejun Heo lockdep_assert_held(&wq->mutex); 35591befcf30STejun Heo 35601befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 35611befcf30STejun Heo link_pwq(pwq); 35621befcf30STejun Heo 35631befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 35641befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 35651befcf30STejun Heo return old_pwq; 35661befcf30STejun Heo } 35671befcf30STejun Heo 35682d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 35692d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 35702d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 35712d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 3572042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 35732d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 35742d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 35752d5f0764SLai Jiangshan }; 35762d5f0764SLai Jiangshan 35772d5f0764SLai Jiangshan /* free the resources after success or abort */ 35782d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 35792d5f0764SLai Jiangshan { 35802d5f0764SLai Jiangshan if (ctx) { 35812d5f0764SLai Jiangshan int node; 35822d5f0764SLai Jiangshan 35832d5f0764SLai Jiangshan for_each_node(node) 35842d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 35852d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 35862d5f0764SLai Jiangshan 35872d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 35882d5f0764SLai Jiangshan 35892d5f0764SLai Jiangshan kfree(ctx); 35902d5f0764SLai Jiangshan } 35912d5f0764SLai Jiangshan } 35922d5f0764SLai Jiangshan 35932d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 35942d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 35952d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 35962d5f0764SLai Jiangshan const struct workqueue_attrs *attrs) 35972d5f0764SLai Jiangshan { 35982d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 35992d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 36002d5f0764SLai Jiangshan int node; 36012d5f0764SLai Jiangshan 36022d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 36032d5f0764SLai Jiangshan 36042d5f0764SLai Jiangshan ctx = kzalloc(sizeof(*ctx) + nr_node_ids * sizeof(ctx->pwq_tbl[0]), 36052d5f0764SLai Jiangshan GFP_KERNEL); 36062d5f0764SLai Jiangshan 36072d5f0764SLai Jiangshan new_attrs = alloc_workqueue_attrs(GFP_KERNEL); 36082d5f0764SLai Jiangshan tmp_attrs = alloc_workqueue_attrs(GFP_KERNEL); 36092d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 36102d5f0764SLai Jiangshan goto out_free; 36112d5f0764SLai Jiangshan 3612042f7df1SLai Jiangshan /* 3613042f7df1SLai Jiangshan * Calculate the attrs of the default pwq. 3614042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 3615042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 3616042f7df1SLai Jiangshan */ 36172d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3618b05a7928SFrederic Weisbecker cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); 3619042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 3620042f7df1SLai Jiangshan cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); 36212d5f0764SLai Jiangshan 36222d5f0764SLai Jiangshan /* 36232d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 36242d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 36252d5f0764SLai Jiangshan * pools. 36262d5f0764SLai Jiangshan */ 36272d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 36282d5f0764SLai Jiangshan 36292d5f0764SLai Jiangshan /* 36302d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 36312d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 36322d5f0764SLai Jiangshan * it even if we don't use it immediately. 36332d5f0764SLai Jiangshan */ 36342d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 36352d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 36362d5f0764SLai Jiangshan goto out_free; 36372d5f0764SLai Jiangshan 36382d5f0764SLai Jiangshan for_each_node(node) { 3639042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 36402d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 36412d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 36422d5f0764SLai Jiangshan goto out_free; 36432d5f0764SLai Jiangshan } else { 36442d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 36452d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 36462d5f0764SLai Jiangshan } 36472d5f0764SLai Jiangshan } 36482d5f0764SLai Jiangshan 3649042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 3650042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3651042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 36522d5f0764SLai Jiangshan ctx->attrs = new_attrs; 3653042f7df1SLai Jiangshan 36542d5f0764SLai Jiangshan ctx->wq = wq; 36552d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 36562d5f0764SLai Jiangshan return ctx; 36572d5f0764SLai Jiangshan 36582d5f0764SLai Jiangshan out_free: 36592d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 36602d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 36612d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 36622d5f0764SLai Jiangshan return NULL; 36632d5f0764SLai Jiangshan } 36642d5f0764SLai Jiangshan 36652d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 36662d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 36672d5f0764SLai Jiangshan { 36682d5f0764SLai Jiangshan int node; 36692d5f0764SLai Jiangshan 36702d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 36712d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 36722d5f0764SLai Jiangshan 36732d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 36742d5f0764SLai Jiangshan 36752d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 36762d5f0764SLai Jiangshan for_each_node(node) 36772d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 36782d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 36792d5f0764SLai Jiangshan 36802d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 36812d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 36822d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 36832d5f0764SLai Jiangshan 36842d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 36852d5f0764SLai Jiangshan } 36862d5f0764SLai Jiangshan 3687a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 3688a0111cf6SLai Jiangshan { 3689a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 3690a0111cf6SLai Jiangshan get_online_cpus(); 3691a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 3692a0111cf6SLai Jiangshan } 3693a0111cf6SLai Jiangshan 3694a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 3695a0111cf6SLai Jiangshan { 3696a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 3697a0111cf6SLai Jiangshan put_online_cpus(); 3698a0111cf6SLai Jiangshan } 3699a0111cf6SLai Jiangshan 3700a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 3701a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 3702a0111cf6SLai Jiangshan { 3703a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 3704a0111cf6SLai Jiangshan 3705a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 3706a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 3707a0111cf6SLai Jiangshan return -EINVAL; 3708a0111cf6SLai Jiangshan 3709a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 3710a0111cf6SLai Jiangshan if (WARN_ON((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs))) 3711a0111cf6SLai Jiangshan return -EINVAL; 3712a0111cf6SLai Jiangshan 3713a0111cf6SLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs); 37146201171eSwanghaibin if (!ctx) 37156201171eSwanghaibin return -ENOMEM; 3716a0111cf6SLai Jiangshan 3717a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 3718a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 3719a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 3720a0111cf6SLai Jiangshan 37216201171eSwanghaibin return 0; 3722a0111cf6SLai Jiangshan } 3723a0111cf6SLai Jiangshan 37249e8cd2f5STejun Heo /** 37259e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 37269e8cd2f5STejun Heo * @wq: the target workqueue 37279e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 37289e8cd2f5STejun Heo * 37294c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 37304c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 37314c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 37324c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 37334c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 37344c16bd32STejun Heo * back-to-back will stay on its current pwq. 37359e8cd2f5STejun Heo * 3736d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 3737d185af30SYacine Belkadi * 3738d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 37399e8cd2f5STejun Heo */ 37409e8cd2f5STejun Heo int apply_workqueue_attrs(struct workqueue_struct *wq, 37419e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 37429e8cd2f5STejun Heo { 3743a0111cf6SLai Jiangshan int ret; 37449e8cd2f5STejun Heo 3745a0111cf6SLai Jiangshan apply_wqattrs_lock(); 3746a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 3747a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 37482d5f0764SLai Jiangshan 37492d5f0764SLai Jiangshan return ret; 37509e8cd2f5STejun Heo } 37519e8cd2f5STejun Heo 37524c16bd32STejun Heo /** 37534c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 37544c16bd32STejun Heo * @wq: the target workqueue 37554c16bd32STejun Heo * @cpu: the CPU coming up or going down 37564c16bd32STejun Heo * @online: whether @cpu is coming up or going down 37574c16bd32STejun Heo * 37584c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 37594c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 37604c16bd32STejun Heo * @wq accordingly. 37614c16bd32STejun Heo * 37624c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 37634c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 37644c16bd32STejun Heo * correct. 37654c16bd32STejun Heo * 37664c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 37674c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 37684c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 37694c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 37704c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 37714c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 37724c16bd32STejun Heo * CPU_DOWN_PREPARE. 37734c16bd32STejun Heo */ 37744c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 37754c16bd32STejun Heo bool online) 37764c16bd32STejun Heo { 37774c16bd32STejun Heo int node = cpu_to_node(cpu); 37784c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 37794c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 37804c16bd32STejun Heo struct workqueue_attrs *target_attrs; 37814c16bd32STejun Heo cpumask_t *cpumask; 37824c16bd32STejun Heo 37834c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 37844c16bd32STejun Heo 3785f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 3786f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 37874c16bd32STejun Heo return; 37884c16bd32STejun Heo 37894c16bd32STejun Heo /* 37904c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 37914c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 37924c16bd32STejun Heo * CPU hotplug exclusion. 37934c16bd32STejun Heo */ 37944c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 37954c16bd32STejun Heo cpumask = target_attrs->cpumask; 37964c16bd32STejun Heo 37974c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 37984c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 37994c16bd32STejun Heo 38004c16bd32STejun Heo /* 38014c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 3802042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 3803042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 3804042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 38054c16bd32STejun Heo */ 3806042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 38074c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 3808f7142ed4SLai Jiangshan return; 38094c16bd32STejun Heo } else { 38104c16bd32STejun Heo goto use_dfl_pwq; 38114c16bd32STejun Heo } 38124c16bd32STejun Heo 38134c16bd32STejun Heo /* create a new pwq */ 38144c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 38154c16bd32STejun Heo if (!pwq) { 38162d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 38174c16bd32STejun Heo wq->name); 381877f300b1SDaeseok Youn goto use_dfl_pwq; 38194c16bd32STejun Heo } 38204c16bd32STejun Heo 3821f7142ed4SLai Jiangshan /* Install the new pwq. */ 38224c16bd32STejun Heo mutex_lock(&wq->mutex); 38234c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 38244c16bd32STejun Heo goto out_unlock; 38254c16bd32STejun Heo 38264c16bd32STejun Heo use_dfl_pwq: 3827f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 38284c16bd32STejun Heo spin_lock_irq(&wq->dfl_pwq->pool->lock); 38294c16bd32STejun Heo get_pwq(wq->dfl_pwq); 38304c16bd32STejun Heo spin_unlock_irq(&wq->dfl_pwq->pool->lock); 38314c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 38324c16bd32STejun Heo out_unlock: 38334c16bd32STejun Heo mutex_unlock(&wq->mutex); 38344c16bd32STejun Heo put_pwq_unlocked(old_pwq); 38354c16bd32STejun Heo } 38364c16bd32STejun Heo 383730cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 38381da177e4SLinus Torvalds { 383949e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 38408a2b7538STejun Heo int cpu, ret; 3841e1d8aa9fSFrederic Weisbecker 384230cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 3843420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 3844420c0ddbSTejun Heo if (!wq->cpu_pwqs) 384530cdf249STejun Heo return -ENOMEM; 384630cdf249STejun Heo 384730cdf249STejun Heo for_each_possible_cpu(cpu) { 38487fb98ea7STejun Heo struct pool_workqueue *pwq = 38497fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 38507a62c2c8STejun Heo struct worker_pool *cpu_pools = 3851f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 385230cdf249STejun Heo 3853f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 3854f147f29eSTejun Heo 3855f147f29eSTejun Heo mutex_lock(&wq->mutex); 38561befcf30STejun Heo link_pwq(pwq); 3857f147f29eSTejun Heo mutex_unlock(&wq->mutex); 385830cdf249STejun Heo } 385930cdf249STejun Heo return 0; 38608a2b7538STejun Heo } else if (wq->flags & __WQ_ORDERED) { 38618a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 38628a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 38638a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 38648a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 38658a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 38668a2b7538STejun Heo return ret; 38679e8cd2f5STejun Heo } else { 38689e8cd2f5STejun Heo return apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 38699e8cd2f5STejun Heo } 38700f900049STejun Heo } 38710f900049STejun Heo 3872f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 3873f3421797STejun Heo const char *name) 3874b71ab8c2STejun Heo { 3875f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 3876f3421797STejun Heo 3877f3421797STejun Heo if (max_active < 1 || max_active > lim) 3878044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 3879f3421797STejun Heo max_active, name, 1, lim); 3880b71ab8c2STejun Heo 3881f3421797STejun Heo return clamp_val(max_active, 1, lim); 3882b71ab8c2STejun Heo } 3883b71ab8c2STejun Heo 3884b196be89STejun Heo struct workqueue_struct *__alloc_workqueue_key(const char *fmt, 388597e37d7bSTejun Heo unsigned int flags, 38861e19ffc6STejun Heo int max_active, 3887eb13ba87SJohannes Berg struct lock_class_key *key, 3888b196be89STejun Heo const char *lock_name, ...) 38893af24433SOleg Nesterov { 3890df2d5ae4STejun Heo size_t tbl_size = 0; 3891ecf6881fSTejun Heo va_list args; 38923af24433SOleg Nesterov struct workqueue_struct *wq; 389349e3cf44STejun Heo struct pool_workqueue *pwq; 3894b196be89STejun Heo 3895cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 3896cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 3897cee22a15SViresh Kumar flags |= WQ_UNBOUND; 3898cee22a15SViresh Kumar 3899ecf6881fSTejun Heo /* allocate wq and format name */ 3900df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 3901ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 3902df2d5ae4STejun Heo 3903df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 3904b196be89STejun Heo if (!wq) 3905d2c1d404STejun Heo return NULL; 3906b196be89STejun Heo 39076029a918STejun Heo if (flags & WQ_UNBOUND) { 39086029a918STejun Heo wq->unbound_attrs = alloc_workqueue_attrs(GFP_KERNEL); 39096029a918STejun Heo if (!wq->unbound_attrs) 39106029a918STejun Heo goto err_free_wq; 39116029a918STejun Heo } 39126029a918STejun Heo 3913ecf6881fSTejun Heo va_start(args, lock_name); 3914ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 3915b196be89STejun Heo va_end(args); 39163af24433SOleg Nesterov 3917d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 3918b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 39193af24433SOleg Nesterov 3920b196be89STejun Heo /* init wq */ 392197e37d7bSTejun Heo wq->flags = flags; 3922a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 39233c25a55dSLai Jiangshan mutex_init(&wq->mutex); 3924112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 392530cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 392673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 392773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 3928493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 39293af24433SOleg Nesterov 3930eb13ba87SJohannes Berg lockdep_init_map(&wq->lockdep_map, lock_name, key, 0); 3931cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 39323af24433SOleg Nesterov 393330cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 3934d2c1d404STejun Heo goto err_free_wq; 39351537663fSTejun Heo 3936493008a8STejun Heo /* 3937493008a8STejun Heo * Workqueues which may be used during memory reclaim should 3938493008a8STejun Heo * have a rescuer to guarantee forward progress. 3939493008a8STejun Heo */ 3940493008a8STejun Heo if (flags & WQ_MEM_RECLAIM) { 3941e22bee78STejun Heo struct worker *rescuer; 3942e22bee78STejun Heo 3943f7537df5SLai Jiangshan rescuer = alloc_worker(NUMA_NO_NODE); 3944e22bee78STejun Heo if (!rescuer) 3945d2c1d404STejun Heo goto err_destroy; 3946e22bee78STejun Heo 3947111c225aSTejun Heo rescuer->rescue_wq = wq; 3948111c225aSTejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", 3949b196be89STejun Heo wq->name); 3950d2c1d404STejun Heo if (IS_ERR(rescuer->task)) { 3951d2c1d404STejun Heo kfree(rescuer); 3952d2c1d404STejun Heo goto err_destroy; 3953d2c1d404STejun Heo } 3954e22bee78STejun Heo 3955d2c1d404STejun Heo wq->rescuer = rescuer; 395625834c73SPeter Zijlstra kthread_bind_mask(rescuer->task, cpu_possible_mask); 3957e22bee78STejun Heo wake_up_process(rescuer->task); 39583af24433SOleg Nesterov } 39591537663fSTejun Heo 3960226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 3961226223abSTejun Heo goto err_destroy; 3962226223abSTejun Heo 39633af24433SOleg Nesterov /* 396468e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 396568e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 396668e13a67SLai Jiangshan * list. 39673af24433SOleg Nesterov */ 396868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 3969a0a1a5fdSTejun Heo 3970a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 397149e3cf44STejun Heo for_each_pwq(pwq, wq) 3972699ce097STejun Heo pwq_adjust_max_active(pwq); 3973a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 3974a0a1a5fdSTejun Heo 3975e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 3976a0a1a5fdSTejun Heo 397768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 39783af24433SOleg Nesterov 39793af24433SOleg Nesterov return wq; 3980d2c1d404STejun Heo 3981d2c1d404STejun Heo err_free_wq: 39826029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 39834690c4abSTejun Heo kfree(wq); 3984d2c1d404STejun Heo return NULL; 3985d2c1d404STejun Heo err_destroy: 3986d2c1d404STejun Heo destroy_workqueue(wq); 39874690c4abSTejun Heo return NULL; 39881da177e4SLinus Torvalds } 3989d320c038STejun Heo EXPORT_SYMBOL_GPL(__alloc_workqueue_key); 39901da177e4SLinus Torvalds 39913af24433SOleg Nesterov /** 39923af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 39933af24433SOleg Nesterov * @wq: target workqueue 39943af24433SOleg Nesterov * 39953af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 39963af24433SOleg Nesterov */ 39973af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 39983af24433SOleg Nesterov { 399949e3cf44STejun Heo struct pool_workqueue *pwq; 40004c16bd32STejun Heo int node; 40013af24433SOleg Nesterov 40029c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 40039c5a2ba7STejun Heo drain_workqueue(wq); 4004c8efcc25STejun Heo 40056183c009STejun Heo /* sanity checks */ 4006b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 400749e3cf44STejun Heo for_each_pwq(pwq, wq) { 40086183c009STejun Heo int i; 40096183c009STejun Heo 401076af4d93STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) { 401176af4d93STejun Heo if (WARN_ON(pwq->nr_in_flight[i])) { 4012b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40136183c009STejun Heo return; 401476af4d93STejun Heo } 401576af4d93STejun Heo } 401676af4d93STejun Heo 40175c529597SLai Jiangshan if (WARN_ON((pwq != wq->dfl_pwq) && (pwq->refcnt > 1)) || 40188864b4e5STejun Heo WARN_ON(pwq->nr_active) || 401976af4d93STejun Heo WARN_ON(!list_empty(&pwq->delayed_works))) { 4020b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40216183c009STejun Heo return; 40226183c009STejun Heo } 402376af4d93STejun Heo } 4024b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40256183c009STejun Heo 4026a0a1a5fdSTejun Heo /* 4027a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4028a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4029a0a1a5fdSTejun Heo */ 403068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4031e2dca7adSTejun Heo list_del_rcu(&wq->list); 403268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 40333af24433SOleg Nesterov 4034226223abSTejun Heo workqueue_sysfs_unregister(wq); 4035226223abSTejun Heo 4036e2dca7adSTejun Heo if (wq->rescuer) 4037e22bee78STejun Heo kthread_stop(wq->rescuer->task); 4038e22bee78STejun Heo 40398864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 404029c91e99STejun Heo /* 40418864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4042e2dca7adSTejun Heo * schedule RCU free. 404329c91e99STejun Heo */ 4044e2dca7adSTejun Heo call_rcu_sched(&wq->rcu, rcu_free_wq); 40458864b4e5STejun Heo } else { 40468864b4e5STejun Heo /* 40478864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 40484c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 40494c16bd32STejun Heo * @wq will be freed when the last pwq is released. 40508864b4e5STejun Heo */ 40514c16bd32STejun Heo for_each_node(node) { 40524c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 40534c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 40544c16bd32STejun Heo put_pwq_unlocked(pwq); 40554c16bd32STejun Heo } 40564c16bd32STejun Heo 40574c16bd32STejun Heo /* 40584c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 40594c16bd32STejun Heo * put. Don't access it afterwards. 40604c16bd32STejun Heo */ 40614c16bd32STejun Heo pwq = wq->dfl_pwq; 40624c16bd32STejun Heo wq->dfl_pwq = NULL; 4063dce90d47STejun Heo put_pwq_unlocked(pwq); 406429c91e99STejun Heo } 40653af24433SOleg Nesterov } 40663af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 40673af24433SOleg Nesterov 4068dcd989cbSTejun Heo /** 4069dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4070dcd989cbSTejun Heo * @wq: target workqueue 4071dcd989cbSTejun Heo * @max_active: new max_active value. 4072dcd989cbSTejun Heo * 4073dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4074dcd989cbSTejun Heo * 4075dcd989cbSTejun Heo * CONTEXT: 4076dcd989cbSTejun Heo * Don't call from IRQ context. 4077dcd989cbSTejun Heo */ 4078dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4079dcd989cbSTejun Heo { 408049e3cf44STejun Heo struct pool_workqueue *pwq; 4081dcd989cbSTejun Heo 40828719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 40838719dceaSTejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 40848719dceaSTejun Heo return; 40858719dceaSTejun Heo 4086f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4087dcd989cbSTejun Heo 4088a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4089dcd989cbSTejun Heo 4090dcd989cbSTejun Heo wq->saved_max_active = max_active; 4091dcd989cbSTejun Heo 4092699ce097STejun Heo for_each_pwq(pwq, wq) 4093699ce097STejun Heo pwq_adjust_max_active(pwq); 4094dcd989cbSTejun Heo 4095a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4096dcd989cbSTejun Heo } 4097dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4098dcd989cbSTejun Heo 4099dcd989cbSTejun Heo /** 4100e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4101e6267616STejun Heo * 4102e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4103e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4104d185af30SYacine Belkadi * 4105d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4106e6267616STejun Heo */ 4107e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4108e6267616STejun Heo { 4109e6267616STejun Heo struct worker *worker = current_wq_worker(); 4110e6267616STejun Heo 41116a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4112e6267616STejun Heo } 4113e6267616STejun Heo 4114e6267616STejun Heo /** 4115dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4116dcd989cbSTejun Heo * @cpu: CPU in question 4117dcd989cbSTejun Heo * @wq: target workqueue 4118dcd989cbSTejun Heo * 4119dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4120dcd989cbSTejun Heo * no synchronization around this function and the test result is 4121dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4122dcd989cbSTejun Heo * 4123d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4124d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4125d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4126d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4127d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4128d3251859STejun Heo * 4129d185af30SYacine Belkadi * Return: 4130dcd989cbSTejun Heo * %true if congested, %false otherwise. 4131dcd989cbSTejun Heo */ 4132d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4133dcd989cbSTejun Heo { 41347fb98ea7STejun Heo struct pool_workqueue *pwq; 413576af4d93STejun Heo bool ret; 413676af4d93STejun Heo 413788109453SLai Jiangshan rcu_read_lock_sched(); 41387fb98ea7STejun Heo 4139d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4140d3251859STejun Heo cpu = smp_processor_id(); 4141d3251859STejun Heo 41427fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 41437fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 41447fb98ea7STejun Heo else 4145df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4146dcd989cbSTejun Heo 414776af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 414888109453SLai Jiangshan rcu_read_unlock_sched(); 414976af4d93STejun Heo 415076af4d93STejun Heo return ret; 4151dcd989cbSTejun Heo } 4152dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4153dcd989cbSTejun Heo 4154dcd989cbSTejun Heo /** 4155dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4156dcd989cbSTejun Heo * @work: the work to be tested 4157dcd989cbSTejun Heo * 4158dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4159dcd989cbSTejun Heo * synchronization around this function and the test result is 4160dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4161dcd989cbSTejun Heo * 4162d185af30SYacine Belkadi * Return: 4163dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4164dcd989cbSTejun Heo */ 4165dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4166dcd989cbSTejun Heo { 4167fa1b54e6STejun Heo struct worker_pool *pool; 4168dcd989cbSTejun Heo unsigned long flags; 4169dcd989cbSTejun Heo unsigned int ret = 0; 4170dcd989cbSTejun Heo 4171dcd989cbSTejun Heo if (work_pending(work)) 4172dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4173038366c5SLai Jiangshan 4174fa1b54e6STejun Heo local_irq_save(flags); 4175fa1b54e6STejun Heo pool = get_work_pool(work); 4176038366c5SLai Jiangshan if (pool) { 4177fa1b54e6STejun Heo spin_lock(&pool->lock); 4178c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4179dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4180fa1b54e6STejun Heo spin_unlock(&pool->lock); 4181038366c5SLai Jiangshan } 4182fa1b54e6STejun Heo local_irq_restore(flags); 4183dcd989cbSTejun Heo 4184dcd989cbSTejun Heo return ret; 4185dcd989cbSTejun Heo } 4186dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4187dcd989cbSTejun Heo 41883d1cb205STejun Heo /** 41893d1cb205STejun Heo * set_worker_desc - set description for the current work item 41903d1cb205STejun Heo * @fmt: printf-style format string 41913d1cb205STejun Heo * @...: arguments for the format string 41923d1cb205STejun Heo * 41933d1cb205STejun Heo * This function can be called by a running work function to describe what 41943d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 41953d1cb205STejun Heo * information will be printed out together to help debugging. The 41963d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 41973d1cb205STejun Heo */ 41983d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 41993d1cb205STejun Heo { 42003d1cb205STejun Heo struct worker *worker = current_wq_worker(); 42013d1cb205STejun Heo va_list args; 42023d1cb205STejun Heo 42033d1cb205STejun Heo if (worker) { 42043d1cb205STejun Heo va_start(args, fmt); 42053d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 42063d1cb205STejun Heo va_end(args); 42073d1cb205STejun Heo worker->desc_valid = true; 42083d1cb205STejun Heo } 42093d1cb205STejun Heo } 42103d1cb205STejun Heo 42113d1cb205STejun Heo /** 42123d1cb205STejun Heo * print_worker_info - print out worker information and description 42133d1cb205STejun Heo * @log_lvl: the log level to use when printing 42143d1cb205STejun Heo * @task: target task 42153d1cb205STejun Heo * 42163d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 42173d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 42183d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 42193d1cb205STejun Heo * 42203d1cb205STejun Heo * This function can be safely called on any task as long as the 42213d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 42223d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 42233d1cb205STejun Heo */ 42243d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 42253d1cb205STejun Heo { 42263d1cb205STejun Heo work_func_t *fn = NULL; 42273d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 42283d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 42293d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 42303d1cb205STejun Heo struct workqueue_struct *wq = NULL; 42313d1cb205STejun Heo bool desc_valid = false; 42323d1cb205STejun Heo struct worker *worker; 42333d1cb205STejun Heo 42343d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 42353d1cb205STejun Heo return; 42363d1cb205STejun Heo 42373d1cb205STejun Heo /* 42383d1cb205STejun Heo * This function is called without any synchronization and @task 42393d1cb205STejun Heo * could be in any state. Be careful with dereferences. 42403d1cb205STejun Heo */ 42413d1cb205STejun Heo worker = probe_kthread_data(task); 42423d1cb205STejun Heo 42433d1cb205STejun Heo /* 42443d1cb205STejun Heo * Carefully copy the associated workqueue's workfn and name. Keep 42453d1cb205STejun Heo * the original last '\0' in case the original contains garbage. 42463d1cb205STejun Heo */ 42473d1cb205STejun Heo probe_kernel_read(&fn, &worker->current_func, sizeof(fn)); 42483d1cb205STejun Heo probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq)); 42493d1cb205STejun Heo probe_kernel_read(&wq, &pwq->wq, sizeof(wq)); 42503d1cb205STejun Heo probe_kernel_read(name, wq->name, sizeof(name) - 1); 42513d1cb205STejun Heo 42523d1cb205STejun Heo /* copy worker description */ 42533d1cb205STejun Heo probe_kernel_read(&desc_valid, &worker->desc_valid, sizeof(desc_valid)); 42543d1cb205STejun Heo if (desc_valid) 42553d1cb205STejun Heo probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 42563d1cb205STejun Heo 42573d1cb205STejun Heo if (fn || name[0] || desc[0]) { 42583d1cb205STejun Heo printk("%sWorkqueue: %s %pf", log_lvl, name, fn); 42593d1cb205STejun Heo if (desc[0]) 42603d1cb205STejun Heo pr_cont(" (%s)", desc); 42613d1cb205STejun Heo pr_cont("\n"); 42623d1cb205STejun Heo } 42633d1cb205STejun Heo } 42643d1cb205STejun Heo 42653494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 42663494fc30STejun Heo { 42673494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 42683494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 42693494fc30STejun Heo pr_cont(" node=%d", pool->node); 42703494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 42713494fc30STejun Heo } 42723494fc30STejun Heo 42733494fc30STejun Heo static void pr_cont_work(bool comma, struct work_struct *work) 42743494fc30STejun Heo { 42753494fc30STejun Heo if (work->func == wq_barrier_func) { 42763494fc30STejun Heo struct wq_barrier *barr; 42773494fc30STejun Heo 42783494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 42793494fc30STejun Heo 42803494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 42813494fc30STejun Heo task_pid_nr(barr->task)); 42823494fc30STejun Heo } else { 42833494fc30STejun Heo pr_cont("%s %pf", comma ? "," : "", work->func); 42843494fc30STejun Heo } 42853494fc30STejun Heo } 42863494fc30STejun Heo 42873494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 42883494fc30STejun Heo { 42893494fc30STejun Heo struct worker_pool *pool = pwq->pool; 42903494fc30STejun Heo struct work_struct *work; 42913494fc30STejun Heo struct worker *worker; 42923494fc30STejun Heo bool has_in_flight = false, has_pending = false; 42933494fc30STejun Heo int bkt; 42943494fc30STejun Heo 42953494fc30STejun Heo pr_info(" pwq %d:", pool->id); 42963494fc30STejun Heo pr_cont_pool_info(pool); 42973494fc30STejun Heo 42983494fc30STejun Heo pr_cont(" active=%d/%d%s\n", pwq->nr_active, pwq->max_active, 42993494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 43003494fc30STejun Heo 43013494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 43023494fc30STejun Heo if (worker->current_pwq == pwq) { 43033494fc30STejun Heo has_in_flight = true; 43043494fc30STejun Heo break; 43053494fc30STejun Heo } 43063494fc30STejun Heo } 43073494fc30STejun Heo if (has_in_flight) { 43083494fc30STejun Heo bool comma = false; 43093494fc30STejun Heo 43103494fc30STejun Heo pr_info(" in-flight:"); 43113494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 43123494fc30STejun Heo if (worker->current_pwq != pwq) 43133494fc30STejun Heo continue; 43143494fc30STejun Heo 43153494fc30STejun Heo pr_cont("%s %d%s:%pf", comma ? "," : "", 43163494fc30STejun Heo task_pid_nr(worker->task), 43173494fc30STejun Heo worker == pwq->wq->rescuer ? "(RESCUER)" : "", 43183494fc30STejun Heo worker->current_func); 43193494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 43203494fc30STejun Heo pr_cont_work(false, work); 43213494fc30STejun Heo comma = true; 43223494fc30STejun Heo } 43233494fc30STejun Heo pr_cont("\n"); 43243494fc30STejun Heo } 43253494fc30STejun Heo 43263494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 43273494fc30STejun Heo if (get_work_pwq(work) == pwq) { 43283494fc30STejun Heo has_pending = true; 43293494fc30STejun Heo break; 43303494fc30STejun Heo } 43313494fc30STejun Heo } 43323494fc30STejun Heo if (has_pending) { 43333494fc30STejun Heo bool comma = false; 43343494fc30STejun Heo 43353494fc30STejun Heo pr_info(" pending:"); 43363494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 43373494fc30STejun Heo if (get_work_pwq(work) != pwq) 43383494fc30STejun Heo continue; 43393494fc30STejun Heo 43403494fc30STejun Heo pr_cont_work(comma, work); 43413494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 43423494fc30STejun Heo } 43433494fc30STejun Heo pr_cont("\n"); 43443494fc30STejun Heo } 43453494fc30STejun Heo 43463494fc30STejun Heo if (!list_empty(&pwq->delayed_works)) { 43473494fc30STejun Heo bool comma = false; 43483494fc30STejun Heo 43493494fc30STejun Heo pr_info(" delayed:"); 43503494fc30STejun Heo list_for_each_entry(work, &pwq->delayed_works, entry) { 43513494fc30STejun Heo pr_cont_work(comma, work); 43523494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 43533494fc30STejun Heo } 43543494fc30STejun Heo pr_cont("\n"); 43553494fc30STejun Heo } 43563494fc30STejun Heo } 43573494fc30STejun Heo 43583494fc30STejun Heo /** 43593494fc30STejun Heo * show_workqueue_state - dump workqueue state 43603494fc30STejun Heo * 43613494fc30STejun Heo * Called from a sysrq handler and prints out all busy workqueues and 43623494fc30STejun Heo * pools. 43633494fc30STejun Heo */ 43643494fc30STejun Heo void show_workqueue_state(void) 43653494fc30STejun Heo { 43663494fc30STejun Heo struct workqueue_struct *wq; 43673494fc30STejun Heo struct worker_pool *pool; 43683494fc30STejun Heo unsigned long flags; 43693494fc30STejun Heo int pi; 43703494fc30STejun Heo 43713494fc30STejun Heo rcu_read_lock_sched(); 43723494fc30STejun Heo 43733494fc30STejun Heo pr_info("Showing busy workqueues and worker pools:\n"); 43743494fc30STejun Heo 43753494fc30STejun Heo list_for_each_entry_rcu(wq, &workqueues, list) { 43763494fc30STejun Heo struct pool_workqueue *pwq; 43773494fc30STejun Heo bool idle = true; 43783494fc30STejun Heo 43793494fc30STejun Heo for_each_pwq(pwq, wq) { 43803494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) { 43813494fc30STejun Heo idle = false; 43823494fc30STejun Heo break; 43833494fc30STejun Heo } 43843494fc30STejun Heo } 43853494fc30STejun Heo if (idle) 43863494fc30STejun Heo continue; 43873494fc30STejun Heo 43883494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 43893494fc30STejun Heo 43903494fc30STejun Heo for_each_pwq(pwq, wq) { 43913494fc30STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 43923494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) 43933494fc30STejun Heo show_pwq(pwq); 43943494fc30STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 43953494fc30STejun Heo } 43963494fc30STejun Heo } 43973494fc30STejun Heo 43983494fc30STejun Heo for_each_pool(pool, pi) { 43993494fc30STejun Heo struct worker *worker; 44003494fc30STejun Heo bool first = true; 44013494fc30STejun Heo 44023494fc30STejun Heo spin_lock_irqsave(&pool->lock, flags); 44033494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 44043494fc30STejun Heo goto next_pool; 44053494fc30STejun Heo 44063494fc30STejun Heo pr_info("pool %d:", pool->id); 44073494fc30STejun Heo pr_cont_pool_info(pool); 440882607adcSTejun Heo pr_cont(" hung=%us workers=%d", 440982607adcSTejun Heo jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000, 441082607adcSTejun Heo pool->nr_workers); 44113494fc30STejun Heo if (pool->manager) 44123494fc30STejun Heo pr_cont(" manager: %d", 44133494fc30STejun Heo task_pid_nr(pool->manager->task)); 44143494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 44153494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 44163494fc30STejun Heo task_pid_nr(worker->task)); 44173494fc30STejun Heo first = false; 44183494fc30STejun Heo } 44193494fc30STejun Heo pr_cont("\n"); 44203494fc30STejun Heo next_pool: 44213494fc30STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 44223494fc30STejun Heo } 44233494fc30STejun Heo 44243494fc30STejun Heo rcu_read_unlock_sched(); 44253494fc30STejun Heo } 44263494fc30STejun Heo 4427db7bccf4STejun Heo /* 4428db7bccf4STejun Heo * CPU hotplug. 4429db7bccf4STejun Heo * 4430e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 4431112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 4432706026c2STejun Heo * pool which make migrating pending and scheduled works very 4433e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 443494cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 4435e22bee78STejun Heo * blocked draining impractical. 4436e22bee78STejun Heo * 443724647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 4438628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 4439628c78e7STejun Heo * cpu comes back online. 4440db7bccf4STejun Heo */ 4441db7bccf4STejun Heo 4442706026c2STejun Heo static void wq_unbind_fn(struct work_struct *work) 4443db7bccf4STejun Heo { 444438db41d9STejun Heo int cpu = smp_processor_id(); 44454ce62e9eSTejun Heo struct worker_pool *pool; 4446db7bccf4STejun Heo struct worker *worker; 4447db7bccf4STejun Heo 4448f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 444992f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 445094cf58bbSTejun Heo spin_lock_irq(&pool->lock); 4451e22bee78STejun Heo 4452f2d5a0eeSTejun Heo /* 445392f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 445494cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 445594cf58bbSTejun Heo * except for the ones which are still executing works from 445694cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 445794cf58bbSTejun Heo * this, they may become diasporas. 4458f2d5a0eeSTejun Heo */ 4459da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4460403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 4461db7bccf4STejun Heo 446224647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 4463f2d5a0eeSTejun Heo 446494cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 446592f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 4466e22bee78STejun Heo 4467e22bee78STejun Heo /* 4468eb283428SLai Jiangshan * Call schedule() so that we cross rq->lock and thus can 4469eb283428SLai Jiangshan * guarantee sched callbacks see the %WORKER_UNBOUND flag. 4470eb283428SLai Jiangshan * This is necessary as scheduler callbacks may be invoked 4471eb283428SLai Jiangshan * from other cpus. 4472628c78e7STejun Heo */ 4473628c78e7STejun Heo schedule(); 4474628c78e7STejun Heo 4475628c78e7STejun Heo /* 4476eb283428SLai Jiangshan * Sched callbacks are disabled now. Zap nr_running. 4477eb283428SLai Jiangshan * After this, nr_running stays zero and need_more_worker() 4478eb283428SLai Jiangshan * and keep_working() are always true as long as the 4479eb283428SLai Jiangshan * worklist is not empty. This pool now behaves as an 4480eb283428SLai Jiangshan * unbound (in terms of concurrency management) pool which 4481eb283428SLai Jiangshan * are served by workers tied to the pool. 4482e22bee78STejun Heo */ 4483e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 4484eb283428SLai Jiangshan 4485eb283428SLai Jiangshan /* 4486eb283428SLai Jiangshan * With concurrency management just turned off, a busy 4487eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 4488eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 4489eb283428SLai Jiangshan */ 4490eb283428SLai Jiangshan spin_lock_irq(&pool->lock); 4491eb283428SLai Jiangshan wake_up_worker(pool); 4492eb283428SLai Jiangshan spin_unlock_irq(&pool->lock); 4493eb283428SLai Jiangshan } 4494db7bccf4STejun Heo } 4495db7bccf4STejun Heo 4496bd7c089eSTejun Heo /** 4497bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 4498bd7c089eSTejun Heo * @pool: pool of interest 4499bd7c089eSTejun Heo * 4500a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 4501bd7c089eSTejun Heo */ 4502bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 4503bd7c089eSTejun Heo { 4504a9ab775bSTejun Heo struct worker *worker; 4505bd7c089eSTejun Heo 450692f9c5c4SLai Jiangshan lockdep_assert_held(&pool->attach_mutex); 4507bd7c089eSTejun Heo 4508bd7c089eSTejun Heo /* 4509a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 4510a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 4511402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 4512a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 4513a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 4514bd7c089eSTejun Heo */ 4515da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4516a9ab775bSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 4517a9ab775bSTejun Heo pool->attrs->cpumask) < 0); 4518a9ab775bSTejun Heo 4519a9ab775bSTejun Heo spin_lock_irq(&pool->lock); 45203de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 4521a9ab775bSTejun Heo 4522da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 4523a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 4524a9ab775bSTejun Heo 4525a9ab775bSTejun Heo /* 4526a9ab775bSTejun Heo * A bound idle worker should actually be on the runqueue 4527a9ab775bSTejun Heo * of the associated CPU for local wake-ups targeting it to 4528a9ab775bSTejun Heo * work. Kick all idle workers so that they migrate to the 4529a9ab775bSTejun Heo * associated CPU. Doing this in the same loop as 4530a9ab775bSTejun Heo * replacing UNBOUND with REBOUND is safe as no worker will 4531a9ab775bSTejun Heo * be bound before @pool->lock is released. 4532a9ab775bSTejun Heo */ 4533a9ab775bSTejun Heo if (worker_flags & WORKER_IDLE) 4534bd7c089eSTejun Heo wake_up_process(worker->task); 4535bd7c089eSTejun Heo 4536bd7c089eSTejun Heo /* 4537a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 4538a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 4539a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 4540a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 4541a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 4542a9ab775bSTejun Heo * concurrency management. Note that when or whether 4543a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 4544a9ab775bSTejun Heo * 4545a9ab775bSTejun Heo * ACCESS_ONCE() is necessary because @worker->flags may be 4546a9ab775bSTejun Heo * tested without holding any lock in 4547a9ab775bSTejun Heo * wq_worker_waking_up(). Without it, NOT_RUNNING test may 4548a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 4549a9ab775bSTejun Heo * management operations. 4550bd7c089eSTejun Heo */ 4551a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 4552a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 4553a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 4554a9ab775bSTejun Heo ACCESS_ONCE(worker->flags) = worker_flags; 4555bd7c089eSTejun Heo } 4556a9ab775bSTejun Heo 4557a9ab775bSTejun Heo spin_unlock_irq(&pool->lock); 4558bd7c089eSTejun Heo } 4559bd7c089eSTejun Heo 45607dbc725eSTejun Heo /** 45617dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 45627dbc725eSTejun Heo * @pool: unbound pool of interest 45637dbc725eSTejun Heo * @cpu: the CPU which is coming up 45647dbc725eSTejun Heo * 45657dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 45667dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 45677dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 45687dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 45697dbc725eSTejun Heo */ 45707dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 45717dbc725eSTejun Heo { 45727dbc725eSTejun Heo static cpumask_t cpumask; 45737dbc725eSTejun Heo struct worker *worker; 45747dbc725eSTejun Heo 457592f9c5c4SLai Jiangshan lockdep_assert_held(&pool->attach_mutex); 45767dbc725eSTejun Heo 45777dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 45787dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 45797dbc725eSTejun Heo return; 45807dbc725eSTejun Heo 45817dbc725eSTejun Heo /* is @cpu the only online CPU? */ 45827dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 45837dbc725eSTejun Heo if (cpumask_weight(&cpumask) != 1) 45847dbc725eSTejun Heo return; 45857dbc725eSTejun Heo 45867dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 4587da028469SLai Jiangshan for_each_pool_worker(worker, pool) 45887dbc725eSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 45897dbc725eSTejun Heo pool->attrs->cpumask) < 0); 45907dbc725eSTejun Heo } 45917dbc725eSTejun Heo 45928db25e78STejun Heo /* 45938db25e78STejun Heo * Workqueues should be brought up before normal priority CPU notifiers. 45948db25e78STejun Heo * This will be registered high priority CPU notifier. 45958db25e78STejun Heo */ 45960db0628dSPaul Gortmaker static int workqueue_cpu_up_callback(struct notifier_block *nfb, 45971da177e4SLinus Torvalds unsigned long action, 45981da177e4SLinus Torvalds void *hcpu) 45991da177e4SLinus Torvalds { 4600d84ff051STejun Heo int cpu = (unsigned long)hcpu; 46014ce62e9eSTejun Heo struct worker_pool *pool; 46024c16bd32STejun Heo struct workqueue_struct *wq; 46037dbc725eSTejun Heo int pi; 46041da177e4SLinus Torvalds 46058db25e78STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 46063af24433SOleg Nesterov case CPU_UP_PREPARE: 4607f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 46083ce63377STejun Heo if (pool->nr_workers) 46093ce63377STejun Heo continue; 4610051e1850SLai Jiangshan if (!create_worker(pool)) 46113ce63377STejun Heo return NOTIFY_BAD; 46123af24433SOleg Nesterov } 46131da177e4SLinus Torvalds break; 46141da177e4SLinus Torvalds 461565758202STejun Heo case CPU_DOWN_FAILED: 461665758202STejun Heo case CPU_ONLINE: 461768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 46187dbc725eSTejun Heo 46197dbc725eSTejun Heo for_each_pool(pool, pi) { 462092f9c5c4SLai Jiangshan mutex_lock(&pool->attach_mutex); 462194cf58bbSTejun Heo 4622f05b558dSLai Jiangshan if (pool->cpu == cpu) 462394cf58bbSTejun Heo rebind_workers(pool); 4624f05b558dSLai Jiangshan else if (pool->cpu < 0) 46257dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 462694cf58bbSTejun Heo 462792f9c5c4SLai Jiangshan mutex_unlock(&pool->attach_mutex); 462894cf58bbSTejun Heo } 46297dbc725eSTejun Heo 46304c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 46314c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 46324c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 46334c16bd32STejun Heo 463468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 46358db25e78STejun Heo break; 463665758202STejun Heo } 463765758202STejun Heo return NOTIFY_OK; 463865758202STejun Heo } 463965758202STejun Heo 464065758202STejun Heo /* 464165758202STejun Heo * Workqueues should be brought down after normal priority CPU notifiers. 464265758202STejun Heo * This will be registered as low priority CPU notifier. 464365758202STejun Heo */ 46440db0628dSPaul Gortmaker static int workqueue_cpu_down_callback(struct notifier_block *nfb, 464565758202STejun Heo unsigned long action, 464665758202STejun Heo void *hcpu) 464765758202STejun Heo { 4648d84ff051STejun Heo int cpu = (unsigned long)hcpu; 46498db25e78STejun Heo struct work_struct unbind_work; 46504c16bd32STejun Heo struct workqueue_struct *wq; 46518db25e78STejun Heo 465265758202STejun Heo switch (action & ~CPU_TASKS_FROZEN) { 465365758202STejun Heo case CPU_DOWN_PREPARE: 46544c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 4655706026c2STejun Heo INIT_WORK_ONSTACK(&unbind_work, wq_unbind_fn); 46567635d2fdSJoonsoo Kim queue_work_on(cpu, system_highpri_wq, &unbind_work); 46574c16bd32STejun Heo 46584c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 46594c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 46604c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 46614c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 46624c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 46634c16bd32STejun Heo 46644c16bd32STejun Heo /* wait for per-cpu unbinding to finish */ 46658db25e78STejun Heo flush_work(&unbind_work); 4666440a1136SChuansheng Liu destroy_work_on_stack(&unbind_work); 46678db25e78STejun Heo break; 466865758202STejun Heo } 466965758202STejun Heo return NOTIFY_OK; 467065758202STejun Heo } 467165758202STejun Heo 46722d3854a3SRusty Russell #ifdef CONFIG_SMP 46738ccad40dSRusty Russell 46742d3854a3SRusty Russell struct work_for_cpu { 4675ed48ece2STejun Heo struct work_struct work; 46762d3854a3SRusty Russell long (*fn)(void *); 46772d3854a3SRusty Russell void *arg; 46782d3854a3SRusty Russell long ret; 46792d3854a3SRusty Russell }; 46802d3854a3SRusty Russell 4681ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 46822d3854a3SRusty Russell { 4683ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 4684ed48ece2STejun Heo 46852d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 46862d3854a3SRusty Russell } 46872d3854a3SRusty Russell 46882d3854a3SRusty Russell /** 46892d3854a3SRusty Russell * work_on_cpu - run a function in user context on a particular cpu 46902d3854a3SRusty Russell * @cpu: the cpu to run on 46912d3854a3SRusty Russell * @fn: the function to run 46922d3854a3SRusty Russell * @arg: the function arg 46932d3854a3SRusty Russell * 469431ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 46956b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 4696d185af30SYacine Belkadi * 4697d185af30SYacine Belkadi * Return: The value @fn returns. 46982d3854a3SRusty Russell */ 4699d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 47002d3854a3SRusty Russell { 4701ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 47022d3854a3SRusty Russell 4703ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 4704ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 470512997d1aSBjorn Helgaas flush_work(&wfc.work); 4706440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 47072d3854a3SRusty Russell return wfc.ret; 47082d3854a3SRusty Russell } 47092d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 47102d3854a3SRusty Russell #endif /* CONFIG_SMP */ 47112d3854a3SRusty Russell 4712a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 4713e7577c50SRusty Russell 4714a0a1a5fdSTejun Heo /** 4715a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 4716a0a1a5fdSTejun Heo * 471758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 4718c5aa87bbSTejun Heo * workqueues will queue new works to their delayed_works list instead of 4719706026c2STejun Heo * pool->worklist. 4720a0a1a5fdSTejun Heo * 4721a0a1a5fdSTejun Heo * CONTEXT: 4722a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4723a0a1a5fdSTejun Heo */ 4724a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 4725a0a1a5fdSTejun Heo { 472624b8a847STejun Heo struct workqueue_struct *wq; 472724b8a847STejun Heo struct pool_workqueue *pwq; 4728a0a1a5fdSTejun Heo 472968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4730a0a1a5fdSTejun Heo 47316183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 4732a0a1a5fdSTejun Heo workqueue_freezing = true; 4733a0a1a5fdSTejun Heo 473424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4735a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4736699ce097STejun Heo for_each_pwq(pwq, wq) 4737699ce097STejun Heo pwq_adjust_max_active(pwq); 4738a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4739a1056305STejun Heo } 47405bcab335STejun Heo 474168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4742a0a1a5fdSTejun Heo } 4743a0a1a5fdSTejun Heo 4744a0a1a5fdSTejun Heo /** 474558a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 4746a0a1a5fdSTejun Heo * 4747a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 4748a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 4749a0a1a5fdSTejun Heo * 4750a0a1a5fdSTejun Heo * CONTEXT: 475168e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 4752a0a1a5fdSTejun Heo * 4753d185af30SYacine Belkadi * Return: 475458a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 475558a69cb4STejun Heo * is complete. 4756a0a1a5fdSTejun Heo */ 4757a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 4758a0a1a5fdSTejun Heo { 4759a0a1a5fdSTejun Heo bool busy = false; 476024b8a847STejun Heo struct workqueue_struct *wq; 476124b8a847STejun Heo struct pool_workqueue *pwq; 4762a0a1a5fdSTejun Heo 476368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4764a0a1a5fdSTejun Heo 47656183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 4766a0a1a5fdSTejun Heo 476724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 476824b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 476924b8a847STejun Heo continue; 4770a0a1a5fdSTejun Heo /* 4771a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 4772a0a1a5fdSTejun Heo * to peek without lock. 4773a0a1a5fdSTejun Heo */ 477488109453SLai Jiangshan rcu_read_lock_sched(); 477524b8a847STejun Heo for_each_pwq(pwq, wq) { 47766183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 4777112202d9STejun Heo if (pwq->nr_active) { 4778a0a1a5fdSTejun Heo busy = true; 477988109453SLai Jiangshan rcu_read_unlock_sched(); 4780a0a1a5fdSTejun Heo goto out_unlock; 4781a0a1a5fdSTejun Heo } 4782a0a1a5fdSTejun Heo } 478388109453SLai Jiangshan rcu_read_unlock_sched(); 4784a0a1a5fdSTejun Heo } 4785a0a1a5fdSTejun Heo out_unlock: 478668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4787a0a1a5fdSTejun Heo return busy; 4788a0a1a5fdSTejun Heo } 4789a0a1a5fdSTejun Heo 4790a0a1a5fdSTejun Heo /** 4791a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 4792a0a1a5fdSTejun Heo * 4793a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 4794706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 4795a0a1a5fdSTejun Heo * 4796a0a1a5fdSTejun Heo * CONTEXT: 4797a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 4798a0a1a5fdSTejun Heo */ 4799a0a1a5fdSTejun Heo void thaw_workqueues(void) 4800a0a1a5fdSTejun Heo { 480124b8a847STejun Heo struct workqueue_struct *wq; 480224b8a847STejun Heo struct pool_workqueue *pwq; 4803a0a1a5fdSTejun Heo 480468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4805a0a1a5fdSTejun Heo 4806a0a1a5fdSTejun Heo if (!workqueue_freezing) 4807a0a1a5fdSTejun Heo goto out_unlock; 4808a0a1a5fdSTejun Heo 480974b414eaSLai Jiangshan workqueue_freezing = false; 481024b8a847STejun Heo 481124b8a847STejun Heo /* restore max_active and repopulate worklist */ 481224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 4813a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4814699ce097STejun Heo for_each_pwq(pwq, wq) 4815699ce097STejun Heo pwq_adjust_max_active(pwq); 4816a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 481724b8a847STejun Heo } 481824b8a847STejun Heo 4819a0a1a5fdSTejun Heo out_unlock: 482068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4821a0a1a5fdSTejun Heo } 4822a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 4823a0a1a5fdSTejun Heo 4824042f7df1SLai Jiangshan static int workqueue_apply_unbound_cpumask(void) 4825042f7df1SLai Jiangshan { 4826042f7df1SLai Jiangshan LIST_HEAD(ctxs); 4827042f7df1SLai Jiangshan int ret = 0; 4828042f7df1SLai Jiangshan struct workqueue_struct *wq; 4829042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 4830042f7df1SLai Jiangshan 4831042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 4832042f7df1SLai Jiangshan 4833042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 4834042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 4835042f7df1SLai Jiangshan continue; 4836042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 4837042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 4838042f7df1SLai Jiangshan continue; 4839042f7df1SLai Jiangshan 4840042f7df1SLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); 4841042f7df1SLai Jiangshan if (!ctx) { 4842042f7df1SLai Jiangshan ret = -ENOMEM; 4843042f7df1SLai Jiangshan break; 4844042f7df1SLai Jiangshan } 4845042f7df1SLai Jiangshan 4846042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 4847042f7df1SLai Jiangshan } 4848042f7df1SLai Jiangshan 4849042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 4850042f7df1SLai Jiangshan if (!ret) 4851042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 4852042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 4853042f7df1SLai Jiangshan } 4854042f7df1SLai Jiangshan 4855042f7df1SLai Jiangshan return ret; 4856042f7df1SLai Jiangshan } 4857042f7df1SLai Jiangshan 4858042f7df1SLai Jiangshan /** 4859042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 4860042f7df1SLai Jiangshan * @cpumask: the cpumask to set 4861042f7df1SLai Jiangshan * 4862042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 4863042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 4864042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 4865042f7df1SLai Jiangshan * 4866042f7df1SLai Jiangshan * Retun: 0 - Success 4867042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 4868042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 4869042f7df1SLai Jiangshan */ 4870042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 4871042f7df1SLai Jiangshan { 4872042f7df1SLai Jiangshan int ret = -EINVAL; 4873042f7df1SLai Jiangshan cpumask_var_t saved_cpumask; 4874042f7df1SLai Jiangshan 4875042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) 4876042f7df1SLai Jiangshan return -ENOMEM; 4877042f7df1SLai Jiangshan 4878042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 4879042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 4880a0111cf6SLai Jiangshan apply_wqattrs_lock(); 4881042f7df1SLai Jiangshan 4882042f7df1SLai Jiangshan /* save the old wq_unbound_cpumask. */ 4883042f7df1SLai Jiangshan cpumask_copy(saved_cpumask, wq_unbound_cpumask); 4884042f7df1SLai Jiangshan 4885042f7df1SLai Jiangshan /* update wq_unbound_cpumask at first and apply it to wqs. */ 4886042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, cpumask); 4887042f7df1SLai Jiangshan ret = workqueue_apply_unbound_cpumask(); 4888042f7df1SLai Jiangshan 4889042f7df1SLai Jiangshan /* restore the wq_unbound_cpumask when failed. */ 4890042f7df1SLai Jiangshan if (ret < 0) 4891042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, saved_cpumask); 4892042f7df1SLai Jiangshan 4893a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 4894042f7df1SLai Jiangshan } 4895042f7df1SLai Jiangshan 4896042f7df1SLai Jiangshan free_cpumask_var(saved_cpumask); 4897042f7df1SLai Jiangshan return ret; 4898042f7df1SLai Jiangshan } 4899042f7df1SLai Jiangshan 49006ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 49016ba94429SFrederic Weisbecker /* 49026ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 49036ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 49046ba94429SFrederic Weisbecker * following attributes. 49056ba94429SFrederic Weisbecker * 49066ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 49076ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 49086ba94429SFrederic Weisbecker * 49096ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 49106ba94429SFrederic Weisbecker * 49116ba94429SFrederic Weisbecker * id RO int : the associated pool ID 49126ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 49136ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 49146ba94429SFrederic Weisbecker */ 49156ba94429SFrederic Weisbecker struct wq_device { 49166ba94429SFrederic Weisbecker struct workqueue_struct *wq; 49176ba94429SFrederic Weisbecker struct device dev; 49186ba94429SFrederic Weisbecker }; 49196ba94429SFrederic Weisbecker 49206ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 49216ba94429SFrederic Weisbecker { 49226ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 49236ba94429SFrederic Weisbecker 49246ba94429SFrederic Weisbecker return wq_dev->wq; 49256ba94429SFrederic Weisbecker } 49266ba94429SFrederic Weisbecker 49276ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 49286ba94429SFrederic Weisbecker char *buf) 49296ba94429SFrederic Weisbecker { 49306ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 49316ba94429SFrederic Weisbecker 49326ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 49336ba94429SFrederic Weisbecker } 49346ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 49356ba94429SFrederic Weisbecker 49366ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 49376ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 49386ba94429SFrederic Weisbecker { 49396ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 49406ba94429SFrederic Weisbecker 49416ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 49426ba94429SFrederic Weisbecker } 49436ba94429SFrederic Weisbecker 49446ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 49456ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 49466ba94429SFrederic Weisbecker size_t count) 49476ba94429SFrederic Weisbecker { 49486ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 49496ba94429SFrederic Weisbecker int val; 49506ba94429SFrederic Weisbecker 49516ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 49526ba94429SFrederic Weisbecker return -EINVAL; 49536ba94429SFrederic Weisbecker 49546ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 49556ba94429SFrederic Weisbecker return count; 49566ba94429SFrederic Weisbecker } 49576ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 49586ba94429SFrederic Weisbecker 49596ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 49606ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 49616ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 49626ba94429SFrederic Weisbecker NULL, 49636ba94429SFrederic Weisbecker }; 49646ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 49656ba94429SFrederic Weisbecker 49666ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 49676ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 49686ba94429SFrederic Weisbecker { 49696ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 49706ba94429SFrederic Weisbecker const char *delim = ""; 49716ba94429SFrederic Weisbecker int node, written = 0; 49726ba94429SFrederic Weisbecker 49736ba94429SFrederic Weisbecker rcu_read_lock_sched(); 49746ba94429SFrederic Weisbecker for_each_node(node) { 49756ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 49766ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 49776ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 49786ba94429SFrederic Weisbecker delim = " "; 49796ba94429SFrederic Weisbecker } 49806ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 49816ba94429SFrederic Weisbecker rcu_read_unlock_sched(); 49826ba94429SFrederic Weisbecker 49836ba94429SFrederic Weisbecker return written; 49846ba94429SFrederic Weisbecker } 49856ba94429SFrederic Weisbecker 49866ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 49876ba94429SFrederic Weisbecker char *buf) 49886ba94429SFrederic Weisbecker { 49896ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 49906ba94429SFrederic Weisbecker int written; 49916ba94429SFrederic Weisbecker 49926ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 49936ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 49946ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 49956ba94429SFrederic Weisbecker 49966ba94429SFrederic Weisbecker return written; 49976ba94429SFrederic Weisbecker } 49986ba94429SFrederic Weisbecker 49996ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 50006ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 50016ba94429SFrederic Weisbecker { 50026ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 50036ba94429SFrederic Weisbecker 5004899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5005899a94feSLai Jiangshan 50066ba94429SFrederic Weisbecker attrs = alloc_workqueue_attrs(GFP_KERNEL); 50076ba94429SFrederic Weisbecker if (!attrs) 50086ba94429SFrederic Weisbecker return NULL; 50096ba94429SFrederic Weisbecker 50106ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 50116ba94429SFrederic Weisbecker return attrs; 50126ba94429SFrederic Weisbecker } 50136ba94429SFrederic Weisbecker 50146ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 50156ba94429SFrederic Weisbecker const char *buf, size_t count) 50166ba94429SFrederic Weisbecker { 50176ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50186ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5019d4d3e257SLai Jiangshan int ret = -ENOMEM; 5020d4d3e257SLai Jiangshan 5021d4d3e257SLai Jiangshan apply_wqattrs_lock(); 50226ba94429SFrederic Weisbecker 50236ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 50246ba94429SFrederic Weisbecker if (!attrs) 5025d4d3e257SLai Jiangshan goto out_unlock; 50266ba94429SFrederic Weisbecker 50276ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 50286ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5029d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 50306ba94429SFrederic Weisbecker else 50316ba94429SFrederic Weisbecker ret = -EINVAL; 50326ba94429SFrederic Weisbecker 5033d4d3e257SLai Jiangshan out_unlock: 5034d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 50356ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 50366ba94429SFrederic Weisbecker return ret ?: count; 50376ba94429SFrederic Weisbecker } 50386ba94429SFrederic Weisbecker 50396ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 50406ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 50416ba94429SFrederic Weisbecker { 50426ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50436ba94429SFrederic Weisbecker int written; 50446ba94429SFrederic Weisbecker 50456ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 50466ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 50476ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 50486ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 50496ba94429SFrederic Weisbecker return written; 50506ba94429SFrederic Weisbecker } 50516ba94429SFrederic Weisbecker 50526ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 50536ba94429SFrederic Weisbecker struct device_attribute *attr, 50546ba94429SFrederic Weisbecker const char *buf, size_t count) 50556ba94429SFrederic Weisbecker { 50566ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50576ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5058d4d3e257SLai Jiangshan int ret = -ENOMEM; 5059d4d3e257SLai Jiangshan 5060d4d3e257SLai Jiangshan apply_wqattrs_lock(); 50616ba94429SFrederic Weisbecker 50626ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 50636ba94429SFrederic Weisbecker if (!attrs) 5064d4d3e257SLai Jiangshan goto out_unlock; 50656ba94429SFrederic Weisbecker 50666ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 50676ba94429SFrederic Weisbecker if (!ret) 5068d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 50696ba94429SFrederic Weisbecker 5070d4d3e257SLai Jiangshan out_unlock: 5071d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 50726ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 50736ba94429SFrederic Weisbecker return ret ?: count; 50746ba94429SFrederic Weisbecker } 50756ba94429SFrederic Weisbecker 50766ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 50776ba94429SFrederic Weisbecker char *buf) 50786ba94429SFrederic Weisbecker { 50796ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50806ba94429SFrederic Weisbecker int written; 50816ba94429SFrederic Weisbecker 50826ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 50836ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 50846ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 50856ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 50866ba94429SFrederic Weisbecker 50876ba94429SFrederic Weisbecker return written; 50886ba94429SFrederic Weisbecker } 50896ba94429SFrederic Weisbecker 50906ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 50916ba94429SFrederic Weisbecker const char *buf, size_t count) 50926ba94429SFrederic Weisbecker { 50936ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 50946ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5095d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5096d4d3e257SLai Jiangshan 5097d4d3e257SLai Jiangshan apply_wqattrs_lock(); 50986ba94429SFrederic Weisbecker 50996ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 51006ba94429SFrederic Weisbecker if (!attrs) 5101d4d3e257SLai Jiangshan goto out_unlock; 51026ba94429SFrederic Weisbecker 51036ba94429SFrederic Weisbecker ret = -EINVAL; 51046ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 51056ba94429SFrederic Weisbecker attrs->no_numa = !v; 5106d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 51076ba94429SFrederic Weisbecker } 51086ba94429SFrederic Weisbecker 5109d4d3e257SLai Jiangshan out_unlock: 5110d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 51116ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 51126ba94429SFrederic Weisbecker return ret ?: count; 51136ba94429SFrederic Weisbecker } 51146ba94429SFrederic Weisbecker 51156ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 51166ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 51176ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 51186ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 51196ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 51206ba94429SFrederic Weisbecker __ATTR_NULL, 51216ba94429SFrederic Weisbecker }; 51226ba94429SFrederic Weisbecker 51236ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 51246ba94429SFrederic Weisbecker .name = "workqueue", 51256ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 51266ba94429SFrederic Weisbecker }; 51276ba94429SFrederic Weisbecker 5128b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5129b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5130b05a7928SFrederic Weisbecker { 5131b05a7928SFrederic Weisbecker int written; 5132b05a7928SFrederic Weisbecker 5133042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5134b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5135b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5136042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5137b05a7928SFrederic Weisbecker 5138b05a7928SFrederic Weisbecker return written; 5139b05a7928SFrederic Weisbecker } 5140b05a7928SFrederic Weisbecker 5141042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5142042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5143042f7df1SLai Jiangshan { 5144042f7df1SLai Jiangshan cpumask_var_t cpumask; 5145042f7df1SLai Jiangshan int ret; 5146042f7df1SLai Jiangshan 5147042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5148042f7df1SLai Jiangshan return -ENOMEM; 5149042f7df1SLai Jiangshan 5150042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5151042f7df1SLai Jiangshan if (!ret) 5152042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5153042f7df1SLai Jiangshan 5154042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5155042f7df1SLai Jiangshan return ret ? ret : count; 5156042f7df1SLai Jiangshan } 5157042f7df1SLai Jiangshan 5158b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5159042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5160042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5161b05a7928SFrederic Weisbecker 51626ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 51636ba94429SFrederic Weisbecker { 5164b05a7928SFrederic Weisbecker int err; 5165b05a7928SFrederic Weisbecker 5166b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5167b05a7928SFrederic Weisbecker if (err) 5168b05a7928SFrederic Weisbecker return err; 5169b05a7928SFrederic Weisbecker 5170b05a7928SFrederic Weisbecker return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr); 51716ba94429SFrederic Weisbecker } 51726ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 51736ba94429SFrederic Weisbecker 51746ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 51756ba94429SFrederic Weisbecker { 51766ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 51776ba94429SFrederic Weisbecker 51786ba94429SFrederic Weisbecker kfree(wq_dev); 51796ba94429SFrederic Weisbecker } 51806ba94429SFrederic Weisbecker 51816ba94429SFrederic Weisbecker /** 51826ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 51836ba94429SFrederic Weisbecker * @wq: the workqueue to register 51846ba94429SFrederic Weisbecker * 51856ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 51866ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 51876ba94429SFrederic Weisbecker * which is the preferred method. 51886ba94429SFrederic Weisbecker * 51896ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 51906ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 51916ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 51926ba94429SFrederic Weisbecker * attributes. 51936ba94429SFrederic Weisbecker * 51946ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 51956ba94429SFrederic Weisbecker */ 51966ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 51976ba94429SFrederic Weisbecker { 51986ba94429SFrederic Weisbecker struct wq_device *wq_dev; 51996ba94429SFrederic Weisbecker int ret; 52006ba94429SFrederic Weisbecker 52016ba94429SFrederic Weisbecker /* 5202402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 52036ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 52046ba94429SFrederic Weisbecker * workqueues. 52056ba94429SFrederic Weisbecker */ 52066ba94429SFrederic Weisbecker if (WARN_ON(wq->flags & __WQ_ORDERED)) 52076ba94429SFrederic Weisbecker return -EINVAL; 52086ba94429SFrederic Weisbecker 52096ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 52106ba94429SFrederic Weisbecker if (!wq_dev) 52116ba94429SFrederic Weisbecker return -ENOMEM; 52126ba94429SFrederic Weisbecker 52136ba94429SFrederic Weisbecker wq_dev->wq = wq; 52146ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 52156ba94429SFrederic Weisbecker wq_dev->dev.init_name = wq->name; 52166ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 52176ba94429SFrederic Weisbecker 52186ba94429SFrederic Weisbecker /* 52196ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 52206ba94429SFrederic Weisbecker * everything is ready. 52216ba94429SFrederic Weisbecker */ 52226ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 52236ba94429SFrederic Weisbecker 52246ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 52256ba94429SFrederic Weisbecker if (ret) { 52266ba94429SFrederic Weisbecker kfree(wq_dev); 52276ba94429SFrederic Weisbecker wq->wq_dev = NULL; 52286ba94429SFrederic Weisbecker return ret; 52296ba94429SFrederic Weisbecker } 52306ba94429SFrederic Weisbecker 52316ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 52326ba94429SFrederic Weisbecker struct device_attribute *attr; 52336ba94429SFrederic Weisbecker 52346ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 52356ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 52366ba94429SFrederic Weisbecker if (ret) { 52376ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 52386ba94429SFrederic Weisbecker wq->wq_dev = NULL; 52396ba94429SFrederic Weisbecker return ret; 52406ba94429SFrederic Weisbecker } 52416ba94429SFrederic Weisbecker } 52426ba94429SFrederic Weisbecker } 52436ba94429SFrederic Weisbecker 52446ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 52456ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 52466ba94429SFrederic Weisbecker return 0; 52476ba94429SFrederic Weisbecker } 52486ba94429SFrederic Weisbecker 52496ba94429SFrederic Weisbecker /** 52506ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 52516ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 52526ba94429SFrederic Weisbecker * 52536ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 52546ba94429SFrederic Weisbecker */ 52556ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 52566ba94429SFrederic Weisbecker { 52576ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 52586ba94429SFrederic Weisbecker 52596ba94429SFrederic Weisbecker if (!wq->wq_dev) 52606ba94429SFrederic Weisbecker return; 52616ba94429SFrederic Weisbecker 52626ba94429SFrederic Weisbecker wq->wq_dev = NULL; 52636ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 52646ba94429SFrederic Weisbecker } 52656ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 52666ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 52676ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 52686ba94429SFrederic Weisbecker 526982607adcSTejun Heo /* 527082607adcSTejun Heo * Workqueue watchdog. 527182607adcSTejun Heo * 527282607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 527382607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 527482607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 527582607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 527682607adcSTejun Heo * largely opaque. 527782607adcSTejun Heo * 527882607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 527982607adcSTejun Heo * state if some pools failed to make forward progress for a while where 528082607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 528182607adcSTejun Heo * 528282607adcSTejun Heo * This mechanism is controlled through the kernel parameter 528382607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 528482607adcSTejun Heo * corresponding sysfs parameter file. 528582607adcSTejun Heo */ 528682607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 528782607adcSTejun Heo 528882607adcSTejun Heo static void wq_watchdog_timer_fn(unsigned long data); 528982607adcSTejun Heo 529082607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 529182607adcSTejun Heo static struct timer_list wq_watchdog_timer = 529282607adcSTejun Heo TIMER_DEFERRED_INITIALIZER(wq_watchdog_timer_fn, 0, 0); 529382607adcSTejun Heo 529482607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 529582607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 529682607adcSTejun Heo 529782607adcSTejun Heo static void wq_watchdog_reset_touched(void) 529882607adcSTejun Heo { 529982607adcSTejun Heo int cpu; 530082607adcSTejun Heo 530182607adcSTejun Heo wq_watchdog_touched = jiffies; 530282607adcSTejun Heo for_each_possible_cpu(cpu) 530382607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 530482607adcSTejun Heo } 530582607adcSTejun Heo 530682607adcSTejun Heo static void wq_watchdog_timer_fn(unsigned long data) 530782607adcSTejun Heo { 530882607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 530982607adcSTejun Heo bool lockup_detected = false; 531082607adcSTejun Heo struct worker_pool *pool; 531182607adcSTejun Heo int pi; 531282607adcSTejun Heo 531382607adcSTejun Heo if (!thresh) 531482607adcSTejun Heo return; 531582607adcSTejun Heo 531682607adcSTejun Heo rcu_read_lock(); 531782607adcSTejun Heo 531882607adcSTejun Heo for_each_pool(pool, pi) { 531982607adcSTejun Heo unsigned long pool_ts, touched, ts; 532082607adcSTejun Heo 532182607adcSTejun Heo if (list_empty(&pool->worklist)) 532282607adcSTejun Heo continue; 532382607adcSTejun Heo 532482607adcSTejun Heo /* get the latest of pool and touched timestamps */ 532582607adcSTejun Heo pool_ts = READ_ONCE(pool->watchdog_ts); 532682607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 532782607adcSTejun Heo 532882607adcSTejun Heo if (time_after(pool_ts, touched)) 532982607adcSTejun Heo ts = pool_ts; 533082607adcSTejun Heo else 533182607adcSTejun Heo ts = touched; 533282607adcSTejun Heo 533382607adcSTejun Heo if (pool->cpu >= 0) { 533482607adcSTejun Heo unsigned long cpu_touched = 533582607adcSTejun Heo READ_ONCE(per_cpu(wq_watchdog_touched_cpu, 533682607adcSTejun Heo pool->cpu)); 533782607adcSTejun Heo if (time_after(cpu_touched, ts)) 533882607adcSTejun Heo ts = cpu_touched; 533982607adcSTejun Heo } 534082607adcSTejun Heo 534182607adcSTejun Heo /* did we stall? */ 534282607adcSTejun Heo if (time_after(jiffies, ts + thresh)) { 534382607adcSTejun Heo lockup_detected = true; 534482607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 534582607adcSTejun Heo pr_cont_pool_info(pool); 534682607adcSTejun Heo pr_cont(" stuck for %us!\n", 534782607adcSTejun Heo jiffies_to_msecs(jiffies - pool_ts) / 1000); 534882607adcSTejun Heo } 534982607adcSTejun Heo } 535082607adcSTejun Heo 535182607adcSTejun Heo rcu_read_unlock(); 535282607adcSTejun Heo 535382607adcSTejun Heo if (lockup_detected) 535482607adcSTejun Heo show_workqueue_state(); 535582607adcSTejun Heo 535682607adcSTejun Heo wq_watchdog_reset_touched(); 535782607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 535882607adcSTejun Heo } 535982607adcSTejun Heo 536082607adcSTejun Heo void wq_watchdog_touch(int cpu) 536182607adcSTejun Heo { 536282607adcSTejun Heo if (cpu >= 0) 536382607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 536482607adcSTejun Heo else 536582607adcSTejun Heo wq_watchdog_touched = jiffies; 536682607adcSTejun Heo } 536782607adcSTejun Heo 536882607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 536982607adcSTejun Heo { 537082607adcSTejun Heo wq_watchdog_thresh = 0; 537182607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 537282607adcSTejun Heo 537382607adcSTejun Heo if (thresh) { 537482607adcSTejun Heo wq_watchdog_thresh = thresh; 537582607adcSTejun Heo wq_watchdog_reset_touched(); 537682607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 537782607adcSTejun Heo } 537882607adcSTejun Heo } 537982607adcSTejun Heo 538082607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 538182607adcSTejun Heo const struct kernel_param *kp) 538282607adcSTejun Heo { 538382607adcSTejun Heo unsigned long thresh; 538482607adcSTejun Heo int ret; 538582607adcSTejun Heo 538682607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 538782607adcSTejun Heo if (ret) 538882607adcSTejun Heo return ret; 538982607adcSTejun Heo 539082607adcSTejun Heo if (system_wq) 539182607adcSTejun Heo wq_watchdog_set_thresh(thresh); 539282607adcSTejun Heo else 539382607adcSTejun Heo wq_watchdog_thresh = thresh; 539482607adcSTejun Heo 539582607adcSTejun Heo return 0; 539682607adcSTejun Heo } 539782607adcSTejun Heo 539882607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 539982607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 540082607adcSTejun Heo .get = param_get_ulong, 540182607adcSTejun Heo }; 540282607adcSTejun Heo 540382607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 540482607adcSTejun Heo 0644); 540582607adcSTejun Heo 540682607adcSTejun Heo static void wq_watchdog_init(void) 540782607adcSTejun Heo { 540882607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 540982607adcSTejun Heo } 541082607adcSTejun Heo 541182607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 541282607adcSTejun Heo 541382607adcSTejun Heo static inline void wq_watchdog_init(void) { } 541482607adcSTejun Heo 541582607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 541682607adcSTejun Heo 5417bce90380STejun Heo static void __init wq_numa_init(void) 5418bce90380STejun Heo { 5419bce90380STejun Heo cpumask_var_t *tbl; 5420bce90380STejun Heo int node, cpu; 5421bce90380STejun Heo 5422bce90380STejun Heo if (num_possible_nodes() <= 1) 5423bce90380STejun Heo return; 5424bce90380STejun Heo 5425d55262c4STejun Heo if (wq_disable_numa) { 5426d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 5427d55262c4STejun Heo return; 5428d55262c4STejun Heo } 5429d55262c4STejun Heo 54304c16bd32STejun Heo wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(GFP_KERNEL); 54314c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 54324c16bd32STejun Heo 5433bce90380STejun Heo /* 5434bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 5435bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 5436bce90380STejun Heo * fully initialized by now. 5437bce90380STejun Heo */ 5438ddcb57e2SLai Jiangshan tbl = kzalloc(nr_node_ids * sizeof(tbl[0]), GFP_KERNEL); 5439bce90380STejun Heo BUG_ON(!tbl); 5440bce90380STejun Heo 5441bce90380STejun Heo for_each_node(node) 54425a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 54431be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 5444bce90380STejun Heo 5445bce90380STejun Heo for_each_possible_cpu(cpu) { 5446bce90380STejun Heo node = cpu_to_node(cpu); 5447bce90380STejun Heo if (WARN_ON(node == NUMA_NO_NODE)) { 5448bce90380STejun Heo pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 5449bce90380STejun Heo /* happens iff arch is bonkers, let's just proceed */ 5450bce90380STejun Heo return; 5451bce90380STejun Heo } 5452bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 5453bce90380STejun Heo } 5454bce90380STejun Heo 5455bce90380STejun Heo wq_numa_possible_cpumask = tbl; 5456bce90380STejun Heo wq_numa_enabled = true; 5457bce90380STejun Heo } 5458bce90380STejun Heo 54596ee0578bSSuresh Siddha static int __init init_workqueues(void) 54601da177e4SLinus Torvalds { 54617a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 54627a4e344cSTejun Heo int i, cpu; 5463c34056a3STejun Heo 5464e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 5465e904e6c2STejun Heo 5466b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 5467b05a7928SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 5468b05a7928SFrederic Weisbecker 5469e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 5470e904e6c2STejun Heo 547165758202STejun Heo cpu_notifier(workqueue_cpu_up_callback, CPU_PRI_WORKQUEUE_UP); 5472a5b4e57dSLai Jiangshan hotcpu_notifier(workqueue_cpu_down_callback, CPU_PRI_WORKQUEUE_DOWN); 54738b03ae3cSTejun Heo 5474bce90380STejun Heo wq_numa_init(); 5475bce90380STejun Heo 5476706026c2STejun Heo /* initialize CPU pools */ 547729c91e99STejun Heo for_each_possible_cpu(cpu) { 54784ce62e9eSTejun Heo struct worker_pool *pool; 54798b03ae3cSTejun Heo 54807a4e344cSTejun Heo i = 0; 5481f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 54827a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 5483ec22ca5eSTejun Heo pool->cpu = cpu; 54847a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 54857a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 5486f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 54877a4e344cSTejun Heo 54889daf9e67STejun Heo /* alloc pool ID */ 548968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 54909daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 549168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 54924ce62e9eSTejun Heo } 54938b03ae3cSTejun Heo } 54948b03ae3cSTejun Heo 5495e22bee78STejun Heo /* create the initial worker */ 549629c91e99STejun Heo for_each_online_cpu(cpu) { 54974ce62e9eSTejun Heo struct worker_pool *pool; 5498e22bee78STejun Heo 5499f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 550024647570STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 5501051e1850SLai Jiangshan BUG_ON(!create_worker(pool)); 5502e22bee78STejun Heo } 55034ce62e9eSTejun Heo } 5504e22bee78STejun Heo 55058a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 550629c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 550729c91e99STejun Heo struct workqueue_attrs *attrs; 550829c91e99STejun Heo 550929c91e99STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 551029c91e99STejun Heo attrs->nice = std_nice[i]; 551129c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 55128a2b7538STejun Heo 55138a2b7538STejun Heo /* 55148a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 55158a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 55168a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 55178a2b7538STejun Heo */ 55188a2b7538STejun Heo BUG_ON(!(attrs = alloc_workqueue_attrs(GFP_KERNEL))); 55198a2b7538STejun Heo attrs->nice = std_nice[i]; 55208a2b7538STejun Heo attrs->no_numa = true; 55218a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 552229c91e99STejun Heo } 552329c91e99STejun Heo 5524d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 55251aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 5526d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 5527f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 5528f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 552924d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 553024d51addSTejun Heo WQ_FREEZABLE, 0); 55310668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 55320668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 55330668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 55340668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 55350668106cSViresh Kumar 0); 55361aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 55370668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 55380668106cSViresh Kumar !system_power_efficient_wq || 55390668106cSViresh Kumar !system_freezable_power_efficient_wq); 554082607adcSTejun Heo 554182607adcSTejun Heo wq_watchdog_init(); 554282607adcSTejun Heo 55436ee0578bSSuresh Siddha return 0; 55441da177e4SLinus Torvalds } 55456ee0578bSSuresh Siddha early_initcall(init_workqueues); 5546