1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 41da177e4SLinus Torvalds * 5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 81da177e4SLinus Torvalds * David Woodhouse <dwmw2@infradead.org> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 111da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 1289ada679SChristoph Lameter * 13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 14c54fce6eSTejun Heo * 15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <tj@kernel.org> 17c54fce6eSTejun Heo * 18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 21b11895c4SLibin * normal work items and the other for high priority ones) and some extra 22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 23b11895c4SLibin * number of these backing pools is dynamic. 24c54fce6eSTejun Heo * 259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 289984de1aSPaul Gortmaker #include <linux/export.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/signal.h> 331da177e4SLinus Torvalds #include <linux/completion.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/notifier.h> 381da177e4SLinus Torvalds #include <linux/kthread.h> 391fa44ecaSJames Bottomley #include <linux/hardirq.h> 4046934023SChristoph Lameter #include <linux/mempolicy.h> 41341a5958SRafael J. Wysocki #include <linux/freezer.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 5262635ea8SSergey Senozhatsky #include <linux/nmi.h> 53940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 54e22bee78STejun Heo 55ea138446STejun Heo #include "workqueue_internal.h" 561da177e4SLinus Torvalds 57c8e55f36STejun Heo enum { 58bc2ae0f5STejun Heo /* 5924647570STejun Heo * worker_pool flags 60bc2ae0f5STejun Heo * 6124647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 62bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 63bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 64bc2ae0f5STejun Heo * is in effect. 65bc2ae0f5STejun Heo * 66bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 67bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6824647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 69bc2ae0f5STejun Heo * 70bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 711258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 724736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 73bc2ae0f5STejun Heo */ 74692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7524647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 76db7bccf4STejun Heo 77c8e55f36STejun Heo /* worker flags */ 78c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 79c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 80e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 81fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 82f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 83a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 84e22bee78STejun Heo 85a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 86a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 87db7bccf4STejun Heo 88e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 894ce62e9eSTejun Heo 9029c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 91c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 92db7bccf4STejun Heo 93e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 94e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 95e22bee78STejun Heo 963233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 973233cdbdSTejun Heo /* call for help after 10ms 983233cdbdSTejun Heo (min two ticks) */ 99e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 100e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* 103e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1048698a745SDongsheng Yang * all cpus. Give MIN_NICE. 105e22bee78STejun Heo */ 1068698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1078698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 108ecf6881fSTejun Heo 109ecf6881fSTejun Heo WQ_NAME_LEN = 24, 110c8e55f36STejun Heo }; 111c8e55f36STejun Heo 1121da177e4SLinus Torvalds /* 1134690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1144690c4abSTejun Heo * 115e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 116e41e704bSTejun Heo * everyone else. 1174690c4abSTejun Heo * 118e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 119e22bee78STejun Heo * only be modified and accessed from the local cpu. 120e22bee78STejun Heo * 121d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1224690c4abSTejun Heo * 123d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 124d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 125d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 126d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 127e22bee78STejun Heo * 1281258fae7STejun Heo * A: wq_pool_attach_mutex protected. 129822d8405STejun Heo * 13068e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13176af4d93STejun Heo * 13224acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1335bcab335STejun Heo * 1345b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1355b95e1afSLai Jiangshan * 1365b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 13724acfb71SThomas Gleixner * RCU for reads. 1385b95e1afSLai Jiangshan * 1393c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1403c25a55dSLai Jiangshan * 14124acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1422e109a28STejun Heo * 1432e109a28STejun Heo * MD: wq_mayday_lock protected. 1444690c4abSTejun Heo */ 1454690c4abSTejun Heo 1462eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 147c34056a3STejun Heo 148bd7bdd43STejun Heo struct worker_pool { 149a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 150d84ff051STejun Heo int cpu; /* I: the associated cpu */ 151f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1529daf9e67STejun Heo int id; /* I: pool ID */ 15311ebea50STejun Heo unsigned int flags; /* X: flags */ 154bd7bdd43STejun Heo 15582607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 15682607adcSTejun Heo 157bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 158ea1abd61SLai Jiangshan 1595826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1605826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 161bd7bdd43STejun Heo 162bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 163bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 164bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 165bd7bdd43STejun Heo 166c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 167c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 168c9e7cf27STejun Heo /* L: hash of busy workers */ 169c9e7cf27STejun Heo 1702607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 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 /* 18824acfb71SThomas Gleixner * Destruction of pool is 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 */ 210*f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive 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 21724acfb71SThomas Gleixner * itself is also 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 */ 25230ae2fc0STejun Heo struct worker *rescuer; /* MD: 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 264669de8bdSBart Van Assche char *lock_name; 265669de8bdSBart Van Assche struct lock_class_key key; 2664e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2674e6045f1SJohannes Berg #endif 268ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2692728fd2fSTejun Heo 270e2dca7adSTejun Heo /* 27124acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 27224acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 273e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 274e2dca7adSTejun Heo */ 275e2dca7adSTejun Heo struct rcu_head rcu; 276e2dca7adSTejun Heo 2772728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 2782728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 2792728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 2805b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 2811da177e4SLinus Torvalds }; 2821da177e4SLinus Torvalds 283e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 284e904e6c2STejun Heo 285bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 286bce90380STejun Heo /* possible CPUs of each node */ 287bce90380STejun Heo 288d55262c4STejun Heo static bool wq_disable_numa; 289d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 290d55262c4STejun Heo 291cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 292552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 293cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 294cee22a15SViresh Kumar 295863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 2963347fa09STejun Heo 297bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 298bce90380STejun Heo 2994c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 3004c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 3014c16bd32STejun Heo 30268e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3031258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 304a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 305d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 306d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3075bcab335STejun Heo 308e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 30968e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3107d19c5ceSTejun Heo 311ef557180SMike Galbraith /* PL: allowable cpus for unbound wqs and work items */ 312ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 313ef557180SMike Galbraith 314ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 315ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 316b05a7928SFrederic Weisbecker 317f303fccbSTejun Heo /* 318f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 319f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 320f303fccbSTejun Heo * to uncover usages which depend on it. 321f303fccbSTejun Heo */ 322f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 323f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 324f303fccbSTejun Heo #else 325f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 326f303fccbSTejun Heo #endif 327f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 328f303fccbSTejun Heo 3297d19c5ceSTejun Heo /* the per-cpu worker pools */ 33025528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3317d19c5ceSTejun Heo 33268e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3337d19c5ceSTejun Heo 33468e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 33529c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 33629c91e99STejun Heo 337c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 33829c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 33929c91e99STejun Heo 3408a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3418a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3428a2b7538STejun Heo 343d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 344ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 345044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3461aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 347044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 348d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 349044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 350f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 351044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 35224d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3530668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3540668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3550668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3560668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 357d320c038STejun Heo 3587d19c5ceSTejun Heo static int worker_thread(void *__worker); 3596ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 360c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 3617d19c5ceSTejun Heo 36297bd2347STejun Heo #define CREATE_TRACE_POINTS 36397bd2347STejun Heo #include <trace/events/workqueue.h> 36497bd2347STejun Heo 36568e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 36624acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 367f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 36824acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 3695bcab335STejun Heo 3705b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 37124acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 372f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 373f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 37424acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 3755b95e1afSLai Jiangshan 376f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 377f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 378f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 3797a62c2c8STejun Heo (pool)++) 3804ce62e9eSTejun Heo 38149e3cf44STejun Heo /** 38217116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 38317116969STejun Heo * @pool: iteration cursor 384611c92a0STejun Heo * @pi: integer used for iteration 385fa1b54e6STejun Heo * 38624acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 38768e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 38868e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 389fa1b54e6STejun Heo * 390fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 391fa1b54e6STejun Heo * ignored. 39217116969STejun Heo */ 393611c92a0STejun Heo #define for_each_pool(pool, pi) \ 394611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 39568e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 396fa1b54e6STejun Heo else 39717116969STejun Heo 39817116969STejun Heo /** 399822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 400822d8405STejun Heo * @worker: iteration cursor 401822d8405STejun Heo * @pool: worker_pool to iterate workers of 402822d8405STejun Heo * 4031258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 404822d8405STejun Heo * 405822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 406822d8405STejun Heo * ignored. 407822d8405STejun Heo */ 408da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 409da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4101258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 411822d8405STejun Heo else 412822d8405STejun Heo 413822d8405STejun Heo /** 41449e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 41549e3cf44STejun Heo * @pwq: iteration cursor 41649e3cf44STejun Heo * @wq: the target workqueue 41776af4d93STejun Heo * 41824acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 419794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 420794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 42176af4d93STejun Heo * 42276af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 42376af4d93STejun Heo * ignored. 42449e3cf44STejun Heo */ 42549e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 42649e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 4275a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 428f3421797STejun Heo 429dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 430dc186ad7SThomas Gleixner 431f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 432dc186ad7SThomas Gleixner 43399777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 43499777288SStanislaw Gruszka { 43599777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 43699777288SStanislaw Gruszka } 43799777288SStanislaw Gruszka 438b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 439b9fdac7fSDu, Changbin { 440b9fdac7fSDu, Changbin struct work_struct *work = addr; 441b9fdac7fSDu, Changbin 442b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 443b9fdac7fSDu, Changbin } 444b9fdac7fSDu, Changbin 445dc186ad7SThomas Gleixner /* 446dc186ad7SThomas Gleixner * fixup_init is called when: 447dc186ad7SThomas Gleixner * - an active object is initialized 448dc186ad7SThomas Gleixner */ 44902a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 450dc186ad7SThomas Gleixner { 451dc186ad7SThomas Gleixner struct work_struct *work = addr; 452dc186ad7SThomas Gleixner 453dc186ad7SThomas Gleixner switch (state) { 454dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 455dc186ad7SThomas Gleixner cancel_work_sync(work); 456dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 45702a982a6SDu, Changbin return true; 458dc186ad7SThomas Gleixner default: 45902a982a6SDu, Changbin return false; 460dc186ad7SThomas Gleixner } 461dc186ad7SThomas Gleixner } 462dc186ad7SThomas Gleixner 463dc186ad7SThomas Gleixner /* 464dc186ad7SThomas Gleixner * fixup_free is called when: 465dc186ad7SThomas Gleixner * - an active object is freed 466dc186ad7SThomas Gleixner */ 46702a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 468dc186ad7SThomas Gleixner { 469dc186ad7SThomas Gleixner struct work_struct *work = addr; 470dc186ad7SThomas Gleixner 471dc186ad7SThomas Gleixner switch (state) { 472dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 473dc186ad7SThomas Gleixner cancel_work_sync(work); 474dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 47502a982a6SDu, Changbin return true; 476dc186ad7SThomas Gleixner default: 47702a982a6SDu, Changbin return false; 478dc186ad7SThomas Gleixner } 479dc186ad7SThomas Gleixner } 480dc186ad7SThomas Gleixner 481f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 482dc186ad7SThomas Gleixner .name = "work_struct", 48399777288SStanislaw Gruszka .debug_hint = work_debug_hint, 484b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 485dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 486dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 487dc186ad7SThomas Gleixner }; 488dc186ad7SThomas Gleixner 489dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 490dc186ad7SThomas Gleixner { 491dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 492dc186ad7SThomas Gleixner } 493dc186ad7SThomas Gleixner 494dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 495dc186ad7SThomas Gleixner { 496dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 497dc186ad7SThomas Gleixner } 498dc186ad7SThomas Gleixner 499dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 500dc186ad7SThomas Gleixner { 501dc186ad7SThomas Gleixner if (onstack) 502dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 503dc186ad7SThomas Gleixner else 504dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 505dc186ad7SThomas Gleixner } 506dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 507dc186ad7SThomas Gleixner 508dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 509dc186ad7SThomas Gleixner { 510dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 511dc186ad7SThomas Gleixner } 512dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 513dc186ad7SThomas Gleixner 514ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 515ea2e64f2SThomas Gleixner { 516ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 517ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 518ea2e64f2SThomas Gleixner } 519ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 520ea2e64f2SThomas Gleixner 521dc186ad7SThomas Gleixner #else 522dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 523dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 524dc186ad7SThomas Gleixner #endif 525dc186ad7SThomas Gleixner 5264e8b22bdSLi Bin /** 52767dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 5284e8b22bdSLi Bin * @pool: the pool pointer of interest 5294e8b22bdSLi Bin * 5304e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5314e8b22bdSLi Bin * successfully, -errno on failure. 5324e8b22bdSLi Bin */ 5339daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5349daf9e67STejun Heo { 5359daf9e67STejun Heo int ret; 5369daf9e67STejun Heo 53768e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5385bcab335STejun Heo 5394e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5404e8b22bdSLi Bin GFP_KERNEL); 541229641a6STejun Heo if (ret >= 0) { 542e68035fbSTejun Heo pool->id = ret; 543229641a6STejun Heo return 0; 544229641a6STejun Heo } 5459daf9e67STejun Heo return ret; 5469daf9e67STejun Heo } 5479daf9e67STejun Heo 54876af4d93STejun Heo /** 549df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 550df2d5ae4STejun Heo * @wq: the target workqueue 551df2d5ae4STejun Heo * @node: the node ID 552df2d5ae4STejun Heo * 55324acfb71SThomas Gleixner * This must be called with any of wq_pool_mutex, wq->mutex or RCU 5545b95e1afSLai Jiangshan * read locked. 555df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 556df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 557d185af30SYacine Belkadi * 558d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 559df2d5ae4STejun Heo */ 560df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 561df2d5ae4STejun Heo int node) 562df2d5ae4STejun Heo { 5635b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 564d6e022f1STejun Heo 565d6e022f1STejun Heo /* 566d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 567d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 568d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 569d6e022f1STejun Heo * happens, this workaround can be removed. 570d6e022f1STejun Heo */ 571d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 572d6e022f1STejun Heo return wq->dfl_pwq; 573d6e022f1STejun Heo 574df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 575df2d5ae4STejun Heo } 576df2d5ae4STejun Heo 57773f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 57873f53c4aSTejun Heo { 57973f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 58073f53c4aSTejun Heo } 58173f53c4aSTejun Heo 58273f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 58373f53c4aSTejun Heo { 58473f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 58573f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 58673f53c4aSTejun Heo } 58773f53c4aSTejun Heo 58873f53c4aSTejun Heo static int work_next_color(int color) 58973f53c4aSTejun Heo { 59073f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 591a848e3b6SOleg Nesterov } 592a848e3b6SOleg Nesterov 5934594bf15SDavid Howells /* 594112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 595112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 5967c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 5977a22ad75STejun Heo * 598112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 599112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 600bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 601bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6027a22ad75STejun Heo * 603112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6047c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 605112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6067c3eed5cSTejun Heo * available only while the work item is queued. 607bbb68dfaSTejun Heo * 608bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 609bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 610bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 611bbb68dfaSTejun Heo * try to steal the PENDING bit. 6124594bf15SDavid Howells */ 6137a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6147a22ad75STejun Heo unsigned long flags) 6157a22ad75STejun Heo { 6166183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6177a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6187a22ad75STejun Heo } 6197a22ad75STejun Heo 620112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6214690c4abSTejun Heo unsigned long extra_flags) 622365970a1SDavid Howells { 623112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 624112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 625365970a1SDavid Howells } 626365970a1SDavid Howells 6274468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6284468a00fSLai Jiangshan int pool_id) 6294468a00fSLai Jiangshan { 6304468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6314468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6324468a00fSLai Jiangshan } 6334468a00fSLai Jiangshan 6347c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6357c3eed5cSTejun Heo int pool_id) 6364d707b9fSOleg Nesterov { 63723657bb1STejun Heo /* 63823657bb1STejun Heo * The following wmb is paired with the implied mb in 63923657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 64023657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 64123657bb1STejun Heo * owner. 64223657bb1STejun Heo */ 64323657bb1STejun Heo smp_wmb(); 6447c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 645346c09f8SRoman Pen /* 646346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 647346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 648346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 6498bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 650346c09f8SRoman Pen * the same @work. E.g. consider this case: 651346c09f8SRoman Pen * 652346c09f8SRoman Pen * CPU#0 CPU#1 653346c09f8SRoman Pen * ---------------------------- -------------------------------- 654346c09f8SRoman Pen * 655346c09f8SRoman Pen * 1 STORE event_indicated 656346c09f8SRoman Pen * 2 queue_work_on() { 657346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 658346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 659346c09f8SRoman Pen * 5 set_work_data() # clear bit 660346c09f8SRoman Pen * 6 smp_mb() 661346c09f8SRoman Pen * 7 work->current_func() { 662346c09f8SRoman Pen * 8 LOAD event_indicated 663346c09f8SRoman Pen * } 664346c09f8SRoman Pen * 665346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 666346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 667346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 668346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 669346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 670346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 671346c09f8SRoman Pen * before actual STORE. 672346c09f8SRoman Pen */ 673346c09f8SRoman Pen smp_mb(); 6744d707b9fSOleg Nesterov } 6754d707b9fSOleg Nesterov 6767a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 677365970a1SDavid Howells { 6787c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 6797c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 6807a22ad75STejun Heo } 6817a22ad75STejun Heo 682112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 6837a22ad75STejun Heo { 684e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6857a22ad75STejun Heo 686112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 687e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 688e120153dSTejun Heo else 689e120153dSTejun Heo return NULL; 6907a22ad75STejun Heo } 6917a22ad75STejun Heo 6927c3eed5cSTejun Heo /** 6937c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 6947c3eed5cSTejun Heo * @work: the work item of interest 6957c3eed5cSTejun Heo * 69668e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 69724acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 69824acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 699fa1b54e6STejun Heo * 700fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 701fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 702fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 703fa1b54e6STejun Heo * returned pool is and stays online. 704d185af30SYacine Belkadi * 705d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7067c3eed5cSTejun Heo */ 7077c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7087a22ad75STejun Heo { 709e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7107c3eed5cSTejun Heo int pool_id; 7117a22ad75STejun Heo 71268e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 713fa1b54e6STejun Heo 714112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 715112202d9STejun Heo return ((struct pool_workqueue *) 7167c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7177a22ad75STejun Heo 7187c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7197c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7207a22ad75STejun Heo return NULL; 7217a22ad75STejun Heo 722fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7237c3eed5cSTejun Heo } 7247c3eed5cSTejun Heo 7257c3eed5cSTejun Heo /** 7267c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7277c3eed5cSTejun Heo * @work: the work item of interest 7287c3eed5cSTejun Heo * 729d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7307c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7317c3eed5cSTejun Heo */ 7327c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7337c3eed5cSTejun Heo { 73454d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7357c3eed5cSTejun Heo 736112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 737112202d9STejun Heo return ((struct pool_workqueue *) 73854d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 73954d5b7d0SLai Jiangshan 74054d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7417c3eed5cSTejun Heo } 7427c3eed5cSTejun Heo 743bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 744bbb68dfaSTejun Heo { 7457c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 746bbb68dfaSTejun Heo 7477c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7487c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 749bbb68dfaSTejun Heo } 750bbb68dfaSTejun Heo 751bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 752bbb68dfaSTejun Heo { 753bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 754bbb68dfaSTejun Heo 755112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 756bbb68dfaSTejun Heo } 757bbb68dfaSTejun Heo 758e22bee78STejun Heo /* 7593270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7603270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 761d565ed63STejun Heo * they're being called with pool->lock held. 762e22bee78STejun Heo */ 763e22bee78STejun Heo 76463d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 765649027d7STejun Heo { 766e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 767649027d7STejun Heo } 768649027d7STejun Heo 769e22bee78STejun Heo /* 770e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 771e22bee78STejun Heo * running workers. 772974271c4STejun Heo * 773974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 774706026c2STejun Heo * function will always return %true for unbound pools as long as the 775974271c4STejun Heo * worklist isn't empty. 776e22bee78STejun Heo */ 77763d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 778e22bee78STejun Heo { 77963d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 780e22bee78STejun Heo } 781e22bee78STejun Heo 782e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 78363d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 784e22bee78STejun Heo { 78563d95a91STejun Heo return pool->nr_idle; 786e22bee78STejun Heo } 787e22bee78STejun Heo 788e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 78963d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 790e22bee78STejun Heo { 791e19e397aSTejun Heo return !list_empty(&pool->worklist) && 792e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 793e22bee78STejun Heo } 794e22bee78STejun Heo 795e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 79663d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 797e22bee78STejun Heo { 79863d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 799e22bee78STejun Heo } 800e22bee78STejun Heo 801e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 80263d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 803e22bee78STejun Heo { 804692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 80563d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 80663d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 807e22bee78STejun Heo 808e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 809e22bee78STejun Heo } 810e22bee78STejun Heo 811e22bee78STejun Heo /* 812e22bee78STejun Heo * Wake up functions. 813e22bee78STejun Heo */ 814e22bee78STejun Heo 8151037de36SLai Jiangshan /* Return the first idle worker. Safe with preemption disabled */ 8161037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8177e11629dSTejun Heo { 81863d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8197e11629dSTejun Heo return NULL; 8207e11629dSTejun Heo 82163d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8227e11629dSTejun Heo } 8237e11629dSTejun Heo 8247e11629dSTejun Heo /** 8257e11629dSTejun Heo * wake_up_worker - wake up an idle worker 82663d95a91STejun Heo * @pool: worker pool to wake worker from 8277e11629dSTejun Heo * 82863d95a91STejun Heo * Wake up the first idle worker of @pool. 8297e11629dSTejun Heo * 8307e11629dSTejun Heo * CONTEXT: 831a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 8327e11629dSTejun Heo */ 83363d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8347e11629dSTejun Heo { 8351037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8367e11629dSTejun Heo 8377e11629dSTejun Heo if (likely(worker)) 8387e11629dSTejun Heo wake_up_process(worker->task); 8397e11629dSTejun Heo } 8407e11629dSTejun Heo 8414690c4abSTejun Heo /** 8426d25be57SThomas Gleixner * wq_worker_running - a worker is running again 843e22bee78STejun Heo * @task: task waking up 844e22bee78STejun Heo * 8456d25be57SThomas Gleixner * This function is called when a worker returns from schedule() 846e22bee78STejun Heo */ 8476d25be57SThomas Gleixner void wq_worker_running(struct task_struct *task) 848e22bee78STejun Heo { 849e22bee78STejun Heo struct worker *worker = kthread_data(task); 850e22bee78STejun Heo 8516d25be57SThomas Gleixner if (!worker->sleeping) 8526d25be57SThomas Gleixner return; 8536d25be57SThomas Gleixner if (!(worker->flags & WORKER_NOT_RUNNING)) 854e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 8556d25be57SThomas Gleixner worker->sleeping = 0; 85636576000SJoonsoo Kim } 857e22bee78STejun Heo 858e22bee78STejun Heo /** 859e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 860e22bee78STejun Heo * @task: task going to sleep 861e22bee78STejun Heo * 8626d25be57SThomas Gleixner * This function is called from schedule() when a busy worker is 86362849a96SSebastian Andrzej Siewior * going to sleep. Preemption needs to be disabled to protect ->sleeping 86462849a96SSebastian Andrzej Siewior * assignment. 865e22bee78STejun Heo */ 8666d25be57SThomas Gleixner void wq_worker_sleeping(struct task_struct *task) 867e22bee78STejun Heo { 8686d25be57SThomas Gleixner struct worker *next, *worker = kthread_data(task); 869111c225aSTejun Heo struct worker_pool *pool; 870e22bee78STejun Heo 871111c225aSTejun Heo /* 872111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 873111c225aSTejun Heo * workers, also reach here, let's not access anything before 874111c225aSTejun Heo * checking NOT_RUNNING. 875111c225aSTejun Heo */ 8762d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 8776d25be57SThomas Gleixner return; 878e22bee78STejun Heo 879111c225aSTejun Heo pool = worker->pool; 880111c225aSTejun Heo 88162849a96SSebastian Andrzej Siewior /* Return if preempted before wq_worker_running() was reached */ 88262849a96SSebastian Andrzej Siewior if (worker->sleeping) 8836d25be57SThomas Gleixner return; 8846d25be57SThomas Gleixner 8856d25be57SThomas Gleixner worker->sleeping = 1; 886a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 887e22bee78STejun Heo 888e22bee78STejun Heo /* 889e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 890e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 891e22bee78STejun Heo * Please read comment there. 892e22bee78STejun Heo * 893628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 894628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 895628c78e7STejun Heo * disabled, which in turn means that none else could be 896d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 897628c78e7STejun Heo * lock is safe. 898e22bee78STejun Heo */ 899e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 9006d25be57SThomas Gleixner !list_empty(&pool->worklist)) { 9016d25be57SThomas Gleixner next = first_idle_worker(pool); 9026d25be57SThomas Gleixner if (next) 9036d25be57SThomas Gleixner wake_up_process(next->task); 9046d25be57SThomas Gleixner } 905a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 906e22bee78STejun Heo } 907e22bee78STejun Heo 908e22bee78STejun Heo /** 9091b69ac6bSJohannes Weiner * wq_worker_last_func - retrieve worker's last work function 9108194fe94SBart Van Assche * @task: Task to retrieve last work function of. 9111b69ac6bSJohannes Weiner * 9121b69ac6bSJohannes Weiner * Determine the last function a worker executed. This is called from 9131b69ac6bSJohannes Weiner * the scheduler to get a worker's last known identity. 9141b69ac6bSJohannes Weiner * 9151b69ac6bSJohannes Weiner * CONTEXT: 916a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(rq->lock) 9171b69ac6bSJohannes Weiner * 9184b047002SJohannes Weiner * This function is called during schedule() when a kworker is going 9194b047002SJohannes Weiner * to sleep. It's used by psi to identify aggregation workers during 9204b047002SJohannes Weiner * dequeuing, to allow periodic aggregation to shut-off when that 9214b047002SJohannes Weiner * worker is the last task in the system or cgroup to go to sleep. 9224b047002SJohannes Weiner * 9234b047002SJohannes Weiner * As this function doesn't involve any workqueue-related locking, it 9244b047002SJohannes Weiner * only returns stable values when called from inside the scheduler's 9254b047002SJohannes Weiner * queuing and dequeuing paths, when @task, which must be a kworker, 9264b047002SJohannes Weiner * is guaranteed to not be processing any works. 9274b047002SJohannes Weiner * 9281b69ac6bSJohannes Weiner * Return: 9291b69ac6bSJohannes Weiner * The last work function %current executed as a worker, NULL if it 9301b69ac6bSJohannes Weiner * hasn't executed any work yet. 9311b69ac6bSJohannes Weiner */ 9321b69ac6bSJohannes Weiner work_func_t wq_worker_last_func(struct task_struct *task) 9331b69ac6bSJohannes Weiner { 9341b69ac6bSJohannes Weiner struct worker *worker = kthread_data(task); 9351b69ac6bSJohannes Weiner 9361b69ac6bSJohannes Weiner return worker->last_func; 9371b69ac6bSJohannes Weiner } 9381b69ac6bSJohannes Weiner 9391b69ac6bSJohannes Weiner /** 940e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 941cb444766STejun Heo * @worker: self 942d302f017STejun Heo * @flags: flags to set 943d302f017STejun Heo * 944228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 945d302f017STejun Heo * 946cb444766STejun Heo * CONTEXT: 947a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) 948d302f017STejun Heo */ 949228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 950d302f017STejun Heo { 951bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 952e22bee78STejun Heo 953cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 954cb444766STejun Heo 955228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 956e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 957e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 958e19e397aSTejun Heo atomic_dec(&pool->nr_running); 959e22bee78STejun Heo } 960e22bee78STejun Heo 961d302f017STejun Heo worker->flags |= flags; 962d302f017STejun Heo } 963d302f017STejun Heo 964d302f017STejun Heo /** 965e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 966cb444766STejun Heo * @worker: self 967d302f017STejun Heo * @flags: flags to clear 968d302f017STejun Heo * 969e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 970d302f017STejun Heo * 971cb444766STejun Heo * CONTEXT: 972a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) 973d302f017STejun Heo */ 974d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 975d302f017STejun Heo { 97663d95a91STejun Heo struct worker_pool *pool = worker->pool; 977e22bee78STejun Heo unsigned int oflags = worker->flags; 978e22bee78STejun Heo 979cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 980cb444766STejun Heo 981d302f017STejun Heo worker->flags &= ~flags; 982e22bee78STejun Heo 98342c025f3STejun Heo /* 98442c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 98542c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 98642c025f3STejun Heo * of multiple flags, not a single flag. 98742c025f3STejun Heo */ 988e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 989e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 990e19e397aSTejun Heo atomic_inc(&pool->nr_running); 991d302f017STejun Heo } 992d302f017STejun Heo 993d302f017STejun Heo /** 9948cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 995c9e7cf27STejun Heo * @pool: pool of interest 9968cca0eeaSTejun Heo * @work: work to find worker for 9978cca0eeaSTejun Heo * 998c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 999c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1000a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 1001a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 1002a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 1003a2c1c57bSTejun Heo * being executed. 1004a2c1c57bSTejun Heo * 1005a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 1006a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 1007a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 1008a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 1009a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 1010a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 1011a2c1c57bSTejun Heo * 1012c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 1013c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 1014c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 1015c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1016c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1017c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 10188cca0eeaSTejun Heo * 10198cca0eeaSTejun Heo * CONTEXT: 1020a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 10218cca0eeaSTejun Heo * 1022d185af30SYacine Belkadi * Return: 1023d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 10248cca0eeaSTejun Heo * otherwise. 10258cca0eeaSTejun Heo */ 1026c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 10278cca0eeaSTejun Heo struct work_struct *work) 10288cca0eeaSTejun Heo { 102942f8570fSSasha Levin struct worker *worker; 103042f8570fSSasha Levin 1031b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1032a2c1c57bSTejun Heo (unsigned long)work) 1033a2c1c57bSTejun Heo if (worker->current_work == work && 1034a2c1c57bSTejun Heo worker->current_func == work->func) 103542f8570fSSasha Levin return worker; 103642f8570fSSasha Levin 103742f8570fSSasha Levin return NULL; 10388cca0eeaSTejun Heo } 10398cca0eeaSTejun Heo 10408cca0eeaSTejun Heo /** 1041bf4ede01STejun Heo * move_linked_works - move linked works to a list 1042bf4ede01STejun Heo * @work: start of series of works to be scheduled 1043bf4ede01STejun Heo * @head: target list to append @work to 1044402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1045bf4ede01STejun Heo * 1046bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1047bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1048bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1049bf4ede01STejun Heo * 1050bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1051bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1052bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1053bf4ede01STejun Heo * 1054bf4ede01STejun Heo * CONTEXT: 1055a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1056bf4ede01STejun Heo */ 1057bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1058bf4ede01STejun Heo struct work_struct **nextp) 1059bf4ede01STejun Heo { 1060bf4ede01STejun Heo struct work_struct *n; 1061bf4ede01STejun Heo 1062bf4ede01STejun Heo /* 1063bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1064bf4ede01STejun Heo * use NULL for list head. 1065bf4ede01STejun Heo */ 1066bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1067bf4ede01STejun Heo list_move_tail(&work->entry, head); 1068bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1069bf4ede01STejun Heo break; 1070bf4ede01STejun Heo } 1071bf4ede01STejun Heo 1072bf4ede01STejun Heo /* 1073bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1074bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1075bf4ede01STejun Heo * needs to be updated. 1076bf4ede01STejun Heo */ 1077bf4ede01STejun Heo if (nextp) 1078bf4ede01STejun Heo *nextp = n; 1079bf4ede01STejun Heo } 1080bf4ede01STejun Heo 10818864b4e5STejun Heo /** 10828864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 10838864b4e5STejun Heo * @pwq: pool_workqueue to get 10848864b4e5STejun Heo * 10858864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 10868864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 10878864b4e5STejun Heo */ 10888864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 10898864b4e5STejun Heo { 10908864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10918864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 10928864b4e5STejun Heo pwq->refcnt++; 10938864b4e5STejun Heo } 10948864b4e5STejun Heo 10958864b4e5STejun Heo /** 10968864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 10978864b4e5STejun Heo * @pwq: pool_workqueue to put 10988864b4e5STejun Heo * 10998864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 11008864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 11018864b4e5STejun Heo */ 11028864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 11038864b4e5STejun Heo { 11048864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11058864b4e5STejun Heo if (likely(--pwq->refcnt)) 11068864b4e5STejun Heo return; 11078864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 11088864b4e5STejun Heo return; 11098864b4e5STejun Heo /* 11108864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 11118864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 11128864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 11138864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 11148864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 11158864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 11168864b4e5STejun Heo */ 11178864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 11188864b4e5STejun Heo } 11198864b4e5STejun Heo 1120dce90d47STejun Heo /** 1121dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1122dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1123dce90d47STejun Heo * 1124dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1125dce90d47STejun Heo */ 1126dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1127dce90d47STejun Heo { 1128dce90d47STejun Heo if (pwq) { 1129dce90d47STejun Heo /* 113024acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1131dce90d47STejun Heo * following lock operations are safe. 1132dce90d47STejun Heo */ 1133a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1134dce90d47STejun Heo put_pwq(pwq); 1135a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1136dce90d47STejun Heo } 1137dce90d47STejun Heo } 1138dce90d47STejun Heo 1139*f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1140bf4ede01STejun Heo { 1141112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1142bf4ede01STejun Heo 1143bf4ede01STejun Heo trace_workqueue_activate_work(work); 114482607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 114582607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1146112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1147*f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1148112202d9STejun Heo pwq->nr_active++; 1149bf4ede01STejun Heo } 1150bf4ede01STejun Heo 1151*f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 11523aa62497SLai Jiangshan { 1153*f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 11543aa62497SLai Jiangshan struct work_struct, entry); 11553aa62497SLai Jiangshan 1156*f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 11573aa62497SLai Jiangshan } 11583aa62497SLai Jiangshan 1159bf4ede01STejun Heo /** 1160112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1161112202d9STejun Heo * @pwq: pwq of interest 1162bf4ede01STejun Heo * @color: color of work which left the queue 1163bf4ede01STejun Heo * 1164bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1165112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1166bf4ede01STejun Heo * 1167bf4ede01STejun Heo * CONTEXT: 1168a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1169bf4ede01STejun Heo */ 1170112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1171bf4ede01STejun Heo { 11728864b4e5STejun Heo /* uncolored work items don't participate in flushing or nr_active */ 1173bf4ede01STejun Heo if (color == WORK_NO_COLOR) 11748864b4e5STejun Heo goto out_put; 1175bf4ede01STejun Heo 1176112202d9STejun Heo pwq->nr_in_flight[color]--; 1177bf4ede01STejun Heo 1178112202d9STejun Heo pwq->nr_active--; 1179*f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1180*f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1181112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1182*f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1183bf4ede01STejun Heo } 1184bf4ede01STejun Heo 1185bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1186112202d9STejun Heo if (likely(pwq->flush_color != color)) 11878864b4e5STejun Heo goto out_put; 1188bf4ede01STejun Heo 1189bf4ede01STejun Heo /* are there still in-flight works? */ 1190112202d9STejun Heo if (pwq->nr_in_flight[color]) 11918864b4e5STejun Heo goto out_put; 1192bf4ede01STejun Heo 1193112202d9STejun Heo /* this pwq is done, clear flush_color */ 1194112202d9STejun Heo pwq->flush_color = -1; 1195bf4ede01STejun Heo 1196bf4ede01STejun Heo /* 1197112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1198bf4ede01STejun Heo * will handle the rest. 1199bf4ede01STejun Heo */ 1200112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1201112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 12028864b4e5STejun Heo out_put: 12038864b4e5STejun Heo put_pwq(pwq); 1204bf4ede01STejun Heo } 1205bf4ede01STejun Heo 120636e227d2STejun Heo /** 1207bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 120836e227d2STejun Heo * @work: work item to steal 120936e227d2STejun Heo * @is_dwork: @work is a delayed_work 1210bbb68dfaSTejun Heo * @flags: place to store irq state 121136e227d2STejun Heo * 121236e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1213d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 121436e227d2STejun Heo * 1215d185af30SYacine Belkadi * Return: 12163eb6b31bSMauro Carvalho Chehab * 12173eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 121836e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 121936e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 122036e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1221bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1222bbb68dfaSTejun Heo * for arbitrarily long 12233eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 122436e227d2STejun Heo * 1225d185af30SYacine Belkadi * Note: 1226bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1227e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1228e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1229e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1230bbb68dfaSTejun Heo * 1231bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1232bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1233bbb68dfaSTejun Heo * 1234e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1235bf4ede01STejun Heo */ 1236bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1237bbb68dfaSTejun Heo unsigned long *flags) 1238bf4ede01STejun Heo { 1239d565ed63STejun Heo struct worker_pool *pool; 1240112202d9STejun Heo struct pool_workqueue *pwq; 1241bf4ede01STejun Heo 1242bbb68dfaSTejun Heo local_irq_save(*flags); 1243bbb68dfaSTejun Heo 124436e227d2STejun Heo /* try to steal the timer if it exists */ 124536e227d2STejun Heo if (is_dwork) { 124636e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 124736e227d2STejun Heo 1248e0aecdd8STejun Heo /* 1249e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1250e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1251e0aecdd8STejun Heo * running on the local CPU. 1252e0aecdd8STejun Heo */ 125336e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 125436e227d2STejun Heo return 1; 125536e227d2STejun Heo } 125636e227d2STejun Heo 125736e227d2STejun Heo /* try to claim PENDING the normal way */ 1258bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1259bf4ede01STejun Heo return 0; 1260bf4ede01STejun Heo 126124acfb71SThomas Gleixner rcu_read_lock(); 1262bf4ede01STejun Heo /* 1263bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1264bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1265bf4ede01STejun Heo */ 1266d565ed63STejun Heo pool = get_work_pool(work); 1267d565ed63STejun Heo if (!pool) 1268bbb68dfaSTejun Heo goto fail; 1269bf4ede01STejun Heo 1270a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1271bf4ede01STejun Heo /* 1272112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1273112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1274112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1275112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1276112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 12770b3dae68SLai Jiangshan * item is currently queued on that pool. 1278bf4ede01STejun Heo */ 1279112202d9STejun Heo pwq = get_work_pwq(work); 1280112202d9STejun Heo if (pwq && pwq->pool == pool) { 1281bf4ede01STejun Heo debug_work_deactivate(work); 12823aa62497SLai Jiangshan 12833aa62497SLai Jiangshan /* 1284*f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 128516062836STejun Heo * it might have linked NO_COLOR work items which, if left 1286*f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 128716062836STejun Heo * management later on and cause stall. Make sure the work 128816062836STejun Heo * item is activated before grabbing. 12893aa62497SLai Jiangshan */ 1290*f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1291*f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 12923aa62497SLai Jiangshan 1293bf4ede01STejun Heo list_del_init(&work->entry); 12949c34a704SLai Jiangshan pwq_dec_nr_in_flight(pwq, get_work_color(work)); 129536e227d2STejun Heo 1296112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 12974468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 12984468a00fSLai Jiangshan 1299a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 130024acfb71SThomas Gleixner rcu_read_unlock(); 130136e227d2STejun Heo return 1; 1302bf4ede01STejun Heo } 1303a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1304bbb68dfaSTejun Heo fail: 130524acfb71SThomas Gleixner rcu_read_unlock(); 1306bbb68dfaSTejun Heo local_irq_restore(*flags); 1307bbb68dfaSTejun Heo if (work_is_canceling(work)) 1308bbb68dfaSTejun Heo return -ENOENT; 1309bbb68dfaSTejun Heo cpu_relax(); 131036e227d2STejun Heo return -EAGAIN; 1311bf4ede01STejun Heo } 1312bf4ede01STejun Heo 1313bf4ede01STejun Heo /** 1314706026c2STejun Heo * insert_work - insert a work into a pool 1315112202d9STejun Heo * @pwq: pwq @work belongs to 13164690c4abSTejun Heo * @work: work to insert 13174690c4abSTejun Heo * @head: insertion point 13184690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 13194690c4abSTejun Heo * 1320112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1321706026c2STejun Heo * work_struct flags. 13224690c4abSTejun Heo * 13234690c4abSTejun Heo * CONTEXT: 1324a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1325365970a1SDavid Howells */ 1326112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1327112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1328b89deed3SOleg Nesterov { 1329112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1330e1d8aa9fSFrederic Weisbecker 1331e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1332e89a85d6SWalter Wu kasan_record_aux_stack(work); 1333e89a85d6SWalter Wu 13344690c4abSTejun Heo /* we own @work, set data and link */ 1335112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 13361a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 13378864b4e5STejun Heo get_pwq(pwq); 1338e22bee78STejun Heo 1339e22bee78STejun Heo /* 1340c5aa87bbSTejun Heo * Ensure either wq_worker_sleeping() sees the above 1341c5aa87bbSTejun Heo * list_add_tail() or we see zero nr_running to avoid workers lying 1342c5aa87bbSTejun Heo * around lazily while there are works to be processed. 1343e22bee78STejun Heo */ 1344e22bee78STejun Heo smp_mb(); 1345e22bee78STejun Heo 134663d95a91STejun Heo if (__need_more_worker(pool)) 134763d95a91STejun Heo wake_up_worker(pool); 1348b89deed3SOleg Nesterov } 1349b89deed3SOleg Nesterov 1350c8efcc25STejun Heo /* 1351c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 13528d03ecfeSTejun Heo * same workqueue. 1353c8efcc25STejun Heo */ 1354c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1355c8efcc25STejun Heo { 1356c8efcc25STejun Heo struct worker *worker; 1357c8efcc25STejun Heo 13588d03ecfeSTejun Heo worker = current_wq_worker(); 1359c8efcc25STejun Heo /* 1360bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 13618d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1362c8efcc25STejun Heo */ 1363112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1364c8efcc25STejun Heo } 1365c8efcc25STejun Heo 1366ef557180SMike Galbraith /* 1367ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1368ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1369ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1370ef557180SMike Galbraith */ 1371ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1372ef557180SMike Galbraith { 1373f303fccbSTejun Heo static bool printed_dbg_warning; 1374ef557180SMike Galbraith int new_cpu; 1375ef557180SMike Galbraith 1376f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1377ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1378ef557180SMike Galbraith return cpu; 1379f303fccbSTejun Heo } else if (!printed_dbg_warning) { 1380f303fccbSTejun Heo pr_warn("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1381f303fccbSTejun Heo printed_dbg_warning = true; 1382f303fccbSTejun Heo } 1383f303fccbSTejun Heo 1384ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1385ef557180SMike Galbraith return cpu; 1386ef557180SMike Galbraith 1387ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1388ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1389ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1390ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1391ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1392ef557180SMike Galbraith return cpu; 1393ef557180SMike Galbraith } 1394ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1395ef557180SMike Galbraith 1396ef557180SMike Galbraith return new_cpu; 1397ef557180SMike Galbraith } 1398ef557180SMike Galbraith 1399d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 14001da177e4SLinus Torvalds struct work_struct *work) 14011da177e4SLinus Torvalds { 1402112202d9STejun Heo struct pool_workqueue *pwq; 1403c9178087STejun Heo struct worker_pool *last_pool; 14041e19ffc6STejun Heo struct list_head *worklist; 14058a2e8e5dSTejun Heo unsigned int work_flags; 1406b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 14078930cabaSTejun Heo 14088930cabaSTejun Heo /* 14098930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 14108930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 14118930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 14128930cabaSTejun Heo * happen with IRQ disabled. 14138930cabaSTejun Heo */ 14148e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 14151da177e4SLinus Torvalds 14161e19ffc6STejun Heo 14179ef28a73SLi Bin /* if draining, only works from the same workqueue are allowed */ 1418618b01ebSTejun Heo if (unlikely(wq->flags & __WQ_DRAINING) && 1419c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1420e41e704bSTejun Heo return; 142124acfb71SThomas Gleixner rcu_read_lock(); 14229e8cd2f5STejun Heo retry: 1423aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1424aa202f1fSHillf Danton if (wq->flags & WQ_UNBOUND) { 1425df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1426ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1427df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1428aa202f1fSHillf Danton } else { 1429aa202f1fSHillf Danton if (req_cpu == WORK_CPU_UNBOUND) 1430aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1431aa202f1fSHillf Danton pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1432aa202f1fSHillf Danton } 1433f3421797STejun Heo 143418aa9effSTejun Heo /* 1435c9178087STejun Heo * If @work was previously on a different pool, it might still be 1436c9178087STejun Heo * running there, in which case the work needs to be queued on that 1437c9178087STejun Heo * pool to guarantee non-reentrancy. 143818aa9effSTejun Heo */ 1439c9e7cf27STejun Heo last_pool = get_work_pool(work); 1440112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 144118aa9effSTejun Heo struct worker *worker; 144218aa9effSTejun Heo 1443a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 144418aa9effSTejun Heo 1445c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 144618aa9effSTejun Heo 1447112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1448c9178087STejun Heo pwq = worker->current_pwq; 14498594fadeSLai Jiangshan } else { 145018aa9effSTejun Heo /* meh... not running there, queue here */ 1451a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1452a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 145318aa9effSTejun Heo } 14548930cabaSTejun Heo } else { 1455a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 14568930cabaSTejun Heo } 1457502ca9d8STejun Heo 14589e8cd2f5STejun Heo /* 14599e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 14609e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 14619e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1462df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1463df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 14649e8cd2f5STejun Heo * make forward-progress. 14659e8cd2f5STejun Heo */ 14669e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 14679e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1468a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 14699e8cd2f5STejun Heo cpu_relax(); 14709e8cd2f5STejun Heo goto retry; 14719e8cd2f5STejun Heo } 14729e8cd2f5STejun Heo /* oops */ 14739e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 14749e8cd2f5STejun Heo wq->name, cpu); 14759e8cd2f5STejun Heo } 14769e8cd2f5STejun Heo 1477112202d9STejun Heo /* pwq determined, queue */ 1478112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1479502ca9d8STejun Heo 148024acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 148124acfb71SThomas Gleixner goto out; 14821e19ffc6STejun Heo 1483112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1484112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 14851e19ffc6STejun Heo 1486112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1487cdadf009STejun Heo trace_workqueue_activate_work(work); 1488112202d9STejun Heo pwq->nr_active++; 1489112202d9STejun Heo worklist = &pwq->pool->worklist; 149082607adcSTejun Heo if (list_empty(worklist)) 149182607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 14928a2e8e5dSTejun Heo } else { 1493*f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1494*f97a4a1aSLai Jiangshan worklist = &pwq->inactive_works; 14958a2e8e5dSTejun Heo } 14961e19ffc6STejun Heo 14970687c66bSZqiang debug_work_activate(work); 1498112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 14991e19ffc6STejun Heo 150024acfb71SThomas Gleixner out: 1501a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 150224acfb71SThomas Gleixner rcu_read_unlock(); 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15050fcb78c2SRolf Eike Beer /** 1506c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1507c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1508c1a220e7SZhang Rui * @wq: workqueue to use 1509c1a220e7SZhang Rui * @work: work to queue 1510c1a220e7SZhang Rui * 1511c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1512c1a220e7SZhang Rui * can't go away. 1513d185af30SYacine Belkadi * 1514d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1515c1a220e7SZhang Rui */ 1516d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1517d4283e93STejun Heo struct work_struct *work) 1518c1a220e7SZhang Rui { 1519d4283e93STejun Heo bool ret = false; 15208930cabaSTejun Heo unsigned long flags; 15218930cabaSTejun Heo 15228930cabaSTejun Heo local_irq_save(flags); 1523c1a220e7SZhang Rui 152422df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15254690c4abSTejun Heo __queue_work(cpu, wq, work); 1526d4283e93STejun Heo ret = true; 1527c1a220e7SZhang Rui } 15288930cabaSTejun Heo 15298930cabaSTejun Heo local_irq_restore(flags); 1530c1a220e7SZhang Rui return ret; 1531c1a220e7SZhang Rui } 1532ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1533c1a220e7SZhang Rui 15348204e0c1SAlexander Duyck /** 15358204e0c1SAlexander Duyck * workqueue_select_cpu_near - Select a CPU based on NUMA node 15368204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 15378204e0c1SAlexander Duyck * 15388204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 15398204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 15408204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 15418204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 15428204e0c1SAlexander Duyck */ 15438204e0c1SAlexander Duyck static int workqueue_select_cpu_near(int node) 15448204e0c1SAlexander Duyck { 15458204e0c1SAlexander Duyck int cpu; 15468204e0c1SAlexander Duyck 15478204e0c1SAlexander Duyck /* No point in doing this if NUMA isn't enabled for workqueues */ 15488204e0c1SAlexander Duyck if (!wq_numa_enabled) 15498204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 15508204e0c1SAlexander Duyck 15518204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 15528204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 15538204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 15548204e0c1SAlexander Duyck 15558204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 15568204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 15578204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 15588204e0c1SAlexander Duyck return cpu; 15598204e0c1SAlexander Duyck 15608204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 15618204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 15628204e0c1SAlexander Duyck 15638204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 15648204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 15658204e0c1SAlexander Duyck } 15668204e0c1SAlexander Duyck 15678204e0c1SAlexander Duyck /** 15688204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 15698204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 15708204e0c1SAlexander Duyck * @wq: workqueue to use 15718204e0c1SAlexander Duyck * @work: work to queue 15728204e0c1SAlexander Duyck * 15738204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 15748204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 15758204e0c1SAlexander Duyck * NUMA node. 15768204e0c1SAlexander Duyck * 15778204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 15788204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 15798204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 15808204e0c1SAlexander Duyck * 15818204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 15828204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 15838204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 15848204e0c1SAlexander Duyck * 15858204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 15868204e0c1SAlexander Duyck */ 15878204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 15888204e0c1SAlexander Duyck struct work_struct *work) 15898204e0c1SAlexander Duyck { 15908204e0c1SAlexander Duyck unsigned long flags; 15918204e0c1SAlexander Duyck bool ret = false; 15928204e0c1SAlexander Duyck 15938204e0c1SAlexander Duyck /* 15948204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 15958204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 15968204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 15978204e0c1SAlexander Duyck * 15988204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 15998204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 16008204e0c1SAlexander Duyck * some round robin type logic. 16018204e0c1SAlexander Duyck */ 16028204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 16038204e0c1SAlexander Duyck 16048204e0c1SAlexander Duyck local_irq_save(flags); 16058204e0c1SAlexander Duyck 16068204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 16078204e0c1SAlexander Duyck int cpu = workqueue_select_cpu_near(node); 16088204e0c1SAlexander Duyck 16098204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 16108204e0c1SAlexander Duyck ret = true; 16118204e0c1SAlexander Duyck } 16128204e0c1SAlexander Duyck 16138204e0c1SAlexander Duyck local_irq_restore(flags); 16148204e0c1SAlexander Duyck return ret; 16158204e0c1SAlexander Duyck } 16168204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 16178204e0c1SAlexander Duyck 16188c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 16191da177e4SLinus Torvalds { 16208c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 16211da177e4SLinus Torvalds 1622e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 162360c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 16241da177e4SLinus Torvalds } 16251438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 16261da177e4SLinus Torvalds 16277beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 162852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 16291da177e4SLinus Torvalds { 16307beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 16317beb2edfSTejun Heo struct work_struct *work = &dwork->work; 16321da177e4SLinus Torvalds 1633637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 163498173112SSami Tolvanen WARN_ON_FUNCTION_MISMATCH(timer->function, delayed_work_timer_fn); 1635fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1636fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 16377beb2edfSTejun Heo 16388852aac2STejun Heo /* 16398852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 16408852aac2STejun Heo * both optimization and correctness. The earliest @timer can 16418852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 16428852aac2STejun Heo * on that there's no such delay when @delay is 0. 16438852aac2STejun Heo */ 16448852aac2STejun Heo if (!delay) { 16458852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 16468852aac2STejun Heo return; 16478852aac2STejun Heo } 16488852aac2STejun Heo 164960c057bcSLai Jiangshan dwork->wq = wq; 16501265057fSTejun Heo dwork->cpu = cpu; 16517beb2edfSTejun Heo timer->expires = jiffies + delay; 16527beb2edfSTejun Heo 1653041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 16547beb2edfSTejun Heo add_timer_on(timer, cpu); 1655041bd12eSTejun Heo else 1656041bd12eSTejun Heo add_timer(timer); 16577beb2edfSTejun Heo } 16581da177e4SLinus Torvalds 16590fcb78c2SRolf Eike Beer /** 16600fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 16610fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 16620fcb78c2SRolf Eike Beer * @wq: workqueue to use 1663af9997e4SRandy Dunlap * @dwork: work to queue 16640fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 16650fcb78c2SRolf Eike Beer * 1666d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1667715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1668715f1300STejun Heo * execution. 16690fcb78c2SRolf Eike Beer */ 1670d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 167152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 16727a6bc1cdSVenkatesh Pallipadi { 167352bad64dSDavid Howells struct work_struct *work = &dwork->work; 1674d4283e93STejun Heo bool ret = false; 16758930cabaSTejun Heo unsigned long flags; 16768930cabaSTejun Heo 16778930cabaSTejun Heo /* read the comment in __queue_work() */ 16788930cabaSTejun Heo local_irq_save(flags); 16797a6bc1cdSVenkatesh Pallipadi 168022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 16817beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1682d4283e93STejun Heo ret = true; 16837a6bc1cdSVenkatesh Pallipadi } 16848930cabaSTejun Heo 16858930cabaSTejun Heo local_irq_restore(flags); 16867a6bc1cdSVenkatesh Pallipadi return ret; 16877a6bc1cdSVenkatesh Pallipadi } 1688ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 16891da177e4SLinus Torvalds 1690c8e55f36STejun Heo /** 16918376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 16928376fe22STejun Heo * @cpu: CPU number to execute work on 16938376fe22STejun Heo * @wq: workqueue to use 16948376fe22STejun Heo * @dwork: work to queue 16958376fe22STejun Heo * @delay: number of jiffies to wait before queueing 16968376fe22STejun Heo * 16978376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 16988376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 16998376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 17008376fe22STejun Heo * current state. 17018376fe22STejun Heo * 1702d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 17038376fe22STejun Heo * pending and its timer was modified. 17048376fe22STejun Heo * 1705e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 17068376fe22STejun Heo * See try_to_grab_pending() for details. 17078376fe22STejun Heo */ 17088376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 17098376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 17108376fe22STejun Heo { 17118376fe22STejun Heo unsigned long flags; 17128376fe22STejun Heo int ret; 17138376fe22STejun Heo 17148376fe22STejun Heo do { 17158376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 17168376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 17178376fe22STejun Heo 17188376fe22STejun Heo if (likely(ret >= 0)) { 17198376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 17208376fe22STejun Heo local_irq_restore(flags); 17218376fe22STejun Heo } 17228376fe22STejun Heo 17238376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 17248376fe22STejun Heo return ret; 17258376fe22STejun Heo } 17268376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 17278376fe22STejun Heo 172805f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 172905f0fe6bSTejun Heo { 173005f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 173105f0fe6bSTejun Heo 173205f0fe6bSTejun Heo /* read the comment in __queue_work() */ 173305f0fe6bSTejun Heo local_irq_disable(); 173405f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 173505f0fe6bSTejun Heo local_irq_enable(); 173605f0fe6bSTejun Heo } 173705f0fe6bSTejun Heo 173805f0fe6bSTejun Heo /** 173905f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 174005f0fe6bSTejun Heo * @wq: workqueue to use 174105f0fe6bSTejun Heo * @rwork: work to queue 174205f0fe6bSTejun Heo * 174305f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 174405f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 1745bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 174605f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 174705f0fe6bSTejun Heo */ 174805f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 174905f0fe6bSTejun Heo { 175005f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 175105f0fe6bSTejun Heo 175205f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 175305f0fe6bSTejun Heo rwork->wq = wq; 175405f0fe6bSTejun Heo call_rcu(&rwork->rcu, rcu_work_rcufn); 175505f0fe6bSTejun Heo return true; 175605f0fe6bSTejun Heo } 175705f0fe6bSTejun Heo 175805f0fe6bSTejun Heo return false; 175905f0fe6bSTejun Heo } 176005f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 176105f0fe6bSTejun Heo 17628376fe22STejun Heo /** 1763c8e55f36STejun Heo * worker_enter_idle - enter idle state 1764c8e55f36STejun Heo * @worker: worker which is entering idle state 1765c8e55f36STejun Heo * 1766c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1767c8e55f36STejun Heo * necessary. 1768c8e55f36STejun Heo * 1769c8e55f36STejun Heo * LOCKING: 1770a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1771c8e55f36STejun Heo */ 1772c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 17731da177e4SLinus Torvalds { 1774bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1775c8e55f36STejun Heo 17766183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 17776183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 17786183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 17796183c009STejun Heo return; 1780c8e55f36STejun Heo 1781051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1782cb444766STejun Heo worker->flags |= WORKER_IDLE; 1783bd7bdd43STejun Heo pool->nr_idle++; 1784e22bee78STejun Heo worker->last_active = jiffies; 1785c8e55f36STejun Heo 1786c8e55f36STejun Heo /* idle_list is LIFO */ 1787bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1788db7bccf4STejun Heo 178963d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1790628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1791cb444766STejun Heo 1792544ecf31STejun Heo /* 1793e8b3f8dbSLai Jiangshan * Sanity check nr_running. Because unbind_workers() releases 1794d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1795628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1796628c78e7STejun Heo * unbind is not in progress. 1797544ecf31STejun Heo */ 179824647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1799bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1800e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1801c8e55f36STejun Heo } 1802c8e55f36STejun Heo 1803c8e55f36STejun Heo /** 1804c8e55f36STejun Heo * worker_leave_idle - leave idle state 1805c8e55f36STejun Heo * @worker: worker which is leaving idle state 1806c8e55f36STejun Heo * 1807c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1808c8e55f36STejun Heo * 1809c8e55f36STejun Heo * LOCKING: 1810a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1811c8e55f36STejun Heo */ 1812c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1813c8e55f36STejun Heo { 1814bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1815c8e55f36STejun Heo 18166183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 18176183c009STejun Heo return; 1818d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1819bd7bdd43STejun Heo pool->nr_idle--; 1820c8e55f36STejun Heo list_del_init(&worker->entry); 1821c8e55f36STejun Heo } 1822c8e55f36STejun Heo 1823f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1824c34056a3STejun Heo { 1825c34056a3STejun Heo struct worker *worker; 1826c34056a3STejun Heo 1827f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1828c8e55f36STejun Heo if (worker) { 1829c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1830affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1831da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1832e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1833e22bee78STejun Heo worker->flags = WORKER_PREP; 1834c8e55f36STejun Heo } 1835c34056a3STejun Heo return worker; 1836c34056a3STejun Heo } 1837c34056a3STejun Heo 1838c34056a3STejun Heo /** 18394736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 18404736cbf7SLai Jiangshan * @worker: worker to be attached 18414736cbf7SLai Jiangshan * @pool: the target pool 18424736cbf7SLai Jiangshan * 18434736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 18444736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 18454736cbf7SLai Jiangshan * cpu-[un]hotplugs. 18464736cbf7SLai Jiangshan */ 18474736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 18484736cbf7SLai Jiangshan struct worker_pool *pool) 18494736cbf7SLai Jiangshan { 18501258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 18514736cbf7SLai Jiangshan 18524736cbf7SLai Jiangshan /* 18531258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 18541258fae7STejun Heo * stable across this function. See the comments above the flag 18551258fae7STejun Heo * definition for details. 18564736cbf7SLai Jiangshan */ 18574736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 18584736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 18595c25b5ffSPeter Zijlstra else 18605c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 18614736cbf7SLai Jiangshan 1862640f17c8SPeter Zijlstra if (worker->rescue_wq) 1863640f17c8SPeter Zijlstra set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 1864640f17c8SPeter Zijlstra 18654736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 1866a2d812a2STejun Heo worker->pool = pool; 18674736cbf7SLai Jiangshan 18681258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 18694736cbf7SLai Jiangshan } 18704736cbf7SLai Jiangshan 18714736cbf7SLai Jiangshan /** 187260f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 187360f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 187460f5a4bcSLai Jiangshan * 18754736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 18764736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 18774736cbf7SLai Jiangshan * other reference to the pool. 187860f5a4bcSLai Jiangshan */ 1879a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 188060f5a4bcSLai Jiangshan { 1881a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 188260f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 188360f5a4bcSLai Jiangshan 18841258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 1885a2d812a2STejun Heo 18865c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 1887da028469SLai Jiangshan list_del(&worker->node); 1888a2d812a2STejun Heo worker->pool = NULL; 1889a2d812a2STejun Heo 1890da028469SLai Jiangshan if (list_empty(&pool->workers)) 189160f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 18921258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 189360f5a4bcSLai Jiangshan 1894b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1895b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1896b62c0751SLai Jiangshan 189760f5a4bcSLai Jiangshan if (detach_completion) 189860f5a4bcSLai Jiangshan complete(detach_completion); 189960f5a4bcSLai Jiangshan } 190060f5a4bcSLai Jiangshan 190160f5a4bcSLai Jiangshan /** 1902c34056a3STejun Heo * create_worker - create a new workqueue worker 190363d95a91STejun Heo * @pool: pool the new worker will belong to 1904c34056a3STejun Heo * 1905051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1906c34056a3STejun Heo * 1907c34056a3STejun Heo * CONTEXT: 1908c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1909c34056a3STejun Heo * 1910d185af30SYacine Belkadi * Return: 1911c34056a3STejun Heo * Pointer to the newly created worker. 1912c34056a3STejun Heo */ 1913bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1914c34056a3STejun Heo { 1915e441b56fSZhen Lei struct worker *worker; 1916e441b56fSZhen Lei int id; 1917e3c916a4STejun Heo char id_buf[16]; 1918c34056a3STejun Heo 19197cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 1920e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 1921822d8405STejun Heo if (id < 0) 1922e441b56fSZhen Lei return NULL; 1923c34056a3STejun Heo 1924f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 1925c34056a3STejun Heo if (!worker) 1926c34056a3STejun Heo goto fail; 1927c34056a3STejun Heo 1928c34056a3STejun Heo worker->id = id; 1929c34056a3STejun Heo 193029c91e99STejun Heo if (pool->cpu >= 0) 1931e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1932e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1933f3421797STejun Heo else 1934e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1935e3c916a4STejun Heo 1936f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1937e3c916a4STejun Heo "kworker/%s", id_buf); 1938c34056a3STejun Heo if (IS_ERR(worker->task)) 1939c34056a3STejun Heo goto fail; 1940c34056a3STejun Heo 194191151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 194225834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 194391151228SOleg Nesterov 1944da028469SLai Jiangshan /* successful, attach the worker to the pool */ 19454736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1946822d8405STejun Heo 1947051e1850SLai Jiangshan /* start the newly created worker */ 1948a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 1949051e1850SLai Jiangshan worker->pool->nr_workers++; 1950051e1850SLai Jiangshan worker_enter_idle(worker); 1951051e1850SLai Jiangshan wake_up_process(worker->task); 1952a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 1953051e1850SLai Jiangshan 1954c34056a3STejun Heo return worker; 1955822d8405STejun Heo 1956c34056a3STejun Heo fail: 1957e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 1958c34056a3STejun Heo kfree(worker); 1959c34056a3STejun Heo return NULL; 1960c34056a3STejun Heo } 1961c34056a3STejun Heo 1962c34056a3STejun Heo /** 1963c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1964c34056a3STejun Heo * @worker: worker to be destroyed 1965c34056a3STejun Heo * 196673eb7fe7SLai Jiangshan * Destroy @worker and adjust @pool stats accordingly. The worker should 196773eb7fe7SLai Jiangshan * be idle. 1968c8e55f36STejun Heo * 1969c8e55f36STejun Heo * CONTEXT: 1970a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1971c34056a3STejun Heo */ 1972c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1973c34056a3STejun Heo { 1974bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1975c34056a3STejun Heo 1976cd549687STejun Heo lockdep_assert_held(&pool->lock); 1977cd549687STejun Heo 1978c34056a3STejun Heo /* sanity check frenzy */ 19796183c009STejun Heo if (WARN_ON(worker->current_work) || 198073eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 198173eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 19826183c009STejun Heo return; 1983c34056a3STejun Heo 1984bd7bdd43STejun Heo pool->nr_workers--; 1985bd7bdd43STejun Heo pool->nr_idle--; 1986c8e55f36STejun Heo 1987c8e55f36STejun Heo list_del_init(&worker->entry); 1988cb444766STejun Heo worker->flags |= WORKER_DIE; 198960f5a4bcSLai Jiangshan wake_up_process(worker->task); 1990c34056a3STejun Heo } 1991c34056a3STejun Heo 199232a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 1993e22bee78STejun Heo { 199432a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 1995e22bee78STejun Heo 1996a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 1997e22bee78STejun Heo 19983347fc9fSLai Jiangshan while (too_many_workers(pool)) { 1999e22bee78STejun Heo struct worker *worker; 2000e22bee78STejun Heo unsigned long expires; 2001e22bee78STejun Heo 2002e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 200363d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2004e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2005e22bee78STejun Heo 20063347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 200763d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 20083347fc9fSLai Jiangshan break; 2009e22bee78STejun Heo } 20103347fc9fSLai Jiangshan 20113347fc9fSLai Jiangshan destroy_worker(worker); 2012e22bee78STejun Heo } 2013e22bee78STejun Heo 2014a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2015e22bee78STejun Heo } 2016e22bee78STejun Heo 2017493a1724STejun Heo static void send_mayday(struct work_struct *work) 2018e22bee78STejun Heo { 2019112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2020112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2021493a1724STejun Heo 20222e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2023e22bee78STejun Heo 2024493008a8STejun Heo if (!wq->rescuer) 2025493a1724STejun Heo return; 2026e22bee78STejun Heo 2027e22bee78STejun Heo /* mayday mayday mayday */ 2028493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 202977668c8bSLai Jiangshan /* 203077668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 203177668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 203277668c8bSLai Jiangshan * rescuer is done with it. 203377668c8bSLai Jiangshan */ 203477668c8bSLai Jiangshan get_pwq(pwq); 2035493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2036e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2037493a1724STejun Heo } 2038e22bee78STejun Heo } 2039e22bee78STejun Heo 204032a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2041e22bee78STejun Heo { 204232a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2043e22bee78STejun Heo struct work_struct *work; 2044e22bee78STejun Heo 2045a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2046a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2047e22bee78STejun Heo 204863d95a91STejun Heo if (need_to_create_worker(pool)) { 2049e22bee78STejun Heo /* 2050e22bee78STejun Heo * We've been trying to create a new worker but 2051e22bee78STejun Heo * haven't been successful. We might be hitting an 2052e22bee78STejun Heo * allocation deadlock. Send distress signals to 2053e22bee78STejun Heo * rescuers. 2054e22bee78STejun Heo */ 205563d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2056e22bee78STejun Heo send_mayday(work); 2057e22bee78STejun Heo } 2058e22bee78STejun Heo 2059a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2060a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2061e22bee78STejun Heo 206263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2063e22bee78STejun Heo } 2064e22bee78STejun Heo 2065e22bee78STejun Heo /** 2066e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 206763d95a91STejun Heo * @pool: pool to create a new worker for 2068e22bee78STejun Heo * 206963d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2070e22bee78STejun Heo * have at least one idle worker on return from this function. If 2071e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 207263d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2073e22bee78STejun Heo * possible allocation deadlock. 2074e22bee78STejun Heo * 2075c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2076c5aa87bbSTejun Heo * may_start_working() %true. 2077e22bee78STejun Heo * 2078e22bee78STejun Heo * LOCKING: 2079a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2080e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2081e22bee78STejun Heo * manager. 2082e22bee78STejun Heo */ 208329187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2084d565ed63STejun Heo __releases(&pool->lock) 2085d565ed63STejun Heo __acquires(&pool->lock) 2086e22bee78STejun Heo { 2087e22bee78STejun Heo restart: 2088a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 20899f9c2364STejun Heo 2090e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 209163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2092e22bee78STejun Heo 2093e22bee78STejun Heo while (true) { 2094051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2095e22bee78STejun Heo break; 2096e22bee78STejun Heo 2097e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 20989f9c2364STejun Heo 209963d95a91STejun Heo if (!need_to_create_worker(pool)) 2100e22bee78STejun Heo break; 2101e22bee78STejun Heo } 2102e22bee78STejun Heo 210363d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2104a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2105051e1850SLai Jiangshan /* 2106051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2107051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2108051e1850SLai Jiangshan * already become busy. 2109051e1850SLai Jiangshan */ 211063d95a91STejun Heo if (need_to_create_worker(pool)) 2111e22bee78STejun Heo goto restart; 2112e22bee78STejun Heo } 2113e22bee78STejun Heo 2114e22bee78STejun Heo /** 2115e22bee78STejun Heo * manage_workers - manage worker pool 2116e22bee78STejun Heo * @worker: self 2117e22bee78STejun Heo * 2118706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2119e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2120706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2121e22bee78STejun Heo * 2122e22bee78STejun Heo * The caller can safely start processing works on false return. On 2123e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2124e22bee78STejun Heo * and may_start_working() is true. 2125e22bee78STejun Heo * 2126e22bee78STejun Heo * CONTEXT: 2127a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2128e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2129e22bee78STejun Heo * 2130d185af30SYacine Belkadi * Return: 213129187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 213229187a9eSTejun Heo * start processing works, %true if management function was performed and 213329187a9eSTejun Heo * the conditions that the caller verified before calling the function may 213429187a9eSTejun Heo * no longer be true. 2135e22bee78STejun Heo */ 2136e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2137e22bee78STejun Heo { 213863d95a91STejun Heo struct worker_pool *pool = worker->pool; 2139e22bee78STejun Heo 2140692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 214129187a9eSTejun Heo return false; 2142692b4825STejun Heo 2143692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 21442607d7a6STejun Heo pool->manager = worker; 2145e22bee78STejun Heo 214629187a9eSTejun Heo maybe_create_worker(pool); 2147e22bee78STejun Heo 21482607d7a6STejun Heo pool->manager = NULL; 2149692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2150d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 215129187a9eSTejun Heo return true; 2152e22bee78STejun Heo } 2153e22bee78STejun Heo 2154a62428c0STejun Heo /** 2155a62428c0STejun Heo * process_one_work - process single work 2156c34056a3STejun Heo * @worker: self 2157a62428c0STejun Heo * @work: work to process 2158a62428c0STejun Heo * 2159a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2160a62428c0STejun Heo * process a single work including synchronization against and 2161a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2162a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2163a62428c0STejun Heo * call this function to process a work. 2164a62428c0STejun Heo * 2165a62428c0STejun Heo * CONTEXT: 2166a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2167a62428c0STejun Heo */ 2168c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2169d565ed63STejun Heo __releases(&pool->lock) 2170d565ed63STejun Heo __acquires(&pool->lock) 21711da177e4SLinus Torvalds { 2172112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2173bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2174112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 217573f53c4aSTejun Heo int work_color; 21767e11629dSTejun Heo struct worker *collision; 21774e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 21784e6045f1SJohannes Berg /* 2179a62428c0STejun Heo * It is permissible to free the struct work_struct from 2180a62428c0STejun Heo * inside the function that is called from it, this we need to 2181a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2182a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2183a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 21844e6045f1SJohannes Berg */ 21854d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 21864d82a1deSPeter Zijlstra 21874d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 21884e6045f1SJohannes Berg #endif 2189807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 219085327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2191ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 219225511a47STejun Heo 21937e11629dSTejun Heo /* 21947e11629dSTejun Heo * A single work shouldn't be executed concurrently by 21957e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 21967e11629dSTejun Heo * already processing the work. If so, defer the work to the 21977e11629dSTejun Heo * currently executing one. 21987e11629dSTejun Heo */ 2199c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 22007e11629dSTejun Heo if (unlikely(collision)) { 22017e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 22027e11629dSTejun Heo return; 22037e11629dSTejun Heo } 22041da177e4SLinus Torvalds 22058930cabaSTejun Heo /* claim and dequeue */ 22061da177e4SLinus Torvalds debug_work_deactivate(work); 2207c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2208c34056a3STejun Heo worker->current_work = work; 2209a2c1c57bSTejun Heo worker->current_func = work->func; 2210112202d9STejun Heo worker->current_pwq = pwq; 221173f53c4aSTejun Heo work_color = get_work_color(work); 22127a22ad75STejun Heo 22138bf89593STejun Heo /* 22148bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 22158bf89593STejun Heo * overridden through set_worker_desc(). 22168bf89593STejun Heo */ 22178bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 22188bf89593STejun Heo 2219a62428c0STejun Heo list_del_init(&work->entry); 2220a62428c0STejun Heo 2221649027d7STejun Heo /* 2222228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2223228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2224228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2225228f1d00SLai Jiangshan * execution of the pending work items. 2226fb0e7bebSTejun Heo */ 2227fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2228228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2229fb0e7bebSTejun Heo 2230974271c4STejun Heo /* 2231a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2232a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2233a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2234a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2235228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2236974271c4STejun Heo */ 2237a489a03eSLai Jiangshan if (need_more_worker(pool)) 223863d95a91STejun Heo wake_up_worker(pool); 2239974271c4STejun Heo 22408930cabaSTejun Heo /* 22417c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2242d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 224323657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 224423657bb1STejun Heo * disabled. 22458930cabaSTejun Heo */ 22467c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 22471da177e4SLinus Torvalds 2248a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2249365970a1SDavid Howells 2250a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 22513295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2252e6f3faa7SPeter Zijlstra /* 2253f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2254f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2255e6f3faa7SPeter Zijlstra * 2256e6f3faa7SPeter Zijlstra * However, that would result in: 2257e6f3faa7SPeter Zijlstra * 2258e6f3faa7SPeter Zijlstra * A(W1) 2259e6f3faa7SPeter Zijlstra * WFC(C) 2260e6f3faa7SPeter Zijlstra * A(W1) 2261e6f3faa7SPeter Zijlstra * C(C) 2262e6f3faa7SPeter Zijlstra * 2263e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2264e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2265e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2266f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2267e6f3faa7SPeter Zijlstra * these locks. 2268e6f3faa7SPeter Zijlstra * 2269e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2270e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2271e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2272e6f3faa7SPeter Zijlstra */ 2273f52be570SPeter Zijlstra lockdep_invariant_state(true); 2274e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2275a2c1c57bSTejun Heo worker->current_func(work); 2276e36c886aSArjan van de Ven /* 2277e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2278e36c886aSArjan van de Ven * point will only record its address. 2279e36c886aSArjan van de Ven */ 22801c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 22813295f0efSIngo Molnar lock_map_release(&lockdep_map); 2282112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 22831da177e4SLinus Torvalds 2284d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2285044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2286d75f773cSSakari Ailus " last function: %ps\n", 2287a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2288a2c1c57bSTejun Heo worker->current_func); 2289d5abe669SPeter Zijlstra debug_show_held_locks(current); 2290d5abe669SPeter Zijlstra dump_stack(); 2291d5abe669SPeter Zijlstra } 2292d5abe669SPeter Zijlstra 2293b22ce278STejun Heo /* 2294025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2295b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2296b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2297b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2298789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2299789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2300b22ce278STejun Heo */ 2301a7e6425eSPaul E. McKenney cond_resched(); 2302b22ce278STejun Heo 2303a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2304a62428c0STejun Heo 2305fb0e7bebSTejun Heo /* clear cpu intensive status */ 2306fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2307fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2308fb0e7bebSTejun Heo 23091b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 23101b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 23111b69ac6bSJohannes Weiner 2312a62428c0STejun Heo /* we're done with it, release */ 231342f8570fSSasha Levin hash_del(&worker->hentry); 2314c34056a3STejun Heo worker->current_work = NULL; 2315a2c1c57bSTejun Heo worker->current_func = NULL; 2316112202d9STejun Heo worker->current_pwq = NULL; 2317112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 23181da177e4SLinus Torvalds } 23191da177e4SLinus Torvalds 2320affee4b2STejun Heo /** 2321affee4b2STejun Heo * process_scheduled_works - process scheduled works 2322affee4b2STejun Heo * @worker: self 2323affee4b2STejun Heo * 2324affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2325affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2326affee4b2STejun Heo * fetches a work from the top and executes it. 2327affee4b2STejun Heo * 2328affee4b2STejun Heo * CONTEXT: 2329a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2330affee4b2STejun Heo * multiple times. 2331affee4b2STejun Heo */ 2332affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 23331da177e4SLinus Torvalds { 2334affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2335affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2336a62428c0STejun Heo struct work_struct, entry); 2337c34056a3STejun Heo process_one_work(worker, work); 2338a62428c0STejun Heo } 23391da177e4SLinus Torvalds } 23401da177e4SLinus Torvalds 2341197f6accSTejun Heo static void set_pf_worker(bool val) 2342197f6accSTejun Heo { 2343197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2344197f6accSTejun Heo if (val) 2345197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2346197f6accSTejun Heo else 2347197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2348197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2349197f6accSTejun Heo } 2350197f6accSTejun Heo 23514690c4abSTejun Heo /** 23524690c4abSTejun Heo * worker_thread - the worker thread function 2353c34056a3STejun Heo * @__worker: self 23544690c4abSTejun Heo * 2355c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2356c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2357c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2358c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2359c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2360d185af30SYacine Belkadi * 2361d185af30SYacine Belkadi * Return: 0 23624690c4abSTejun Heo */ 2363c34056a3STejun Heo static int worker_thread(void *__worker) 23641da177e4SLinus Torvalds { 2365c34056a3STejun Heo struct worker *worker = __worker; 2366bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 23671da177e4SLinus Torvalds 2368e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2369197f6accSTejun Heo set_pf_worker(true); 2370c8e55f36STejun Heo woke_up: 2371a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2372affee4b2STejun Heo 2373a9ab775bSTejun Heo /* am I supposed to die? */ 2374a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2375a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2376a9ab775bSTejun Heo WARN_ON_ONCE(!list_empty(&worker->entry)); 2377197f6accSTejun Heo set_pf_worker(false); 237860f5a4bcSLai Jiangshan 237960f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2380e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2381a2d812a2STejun Heo worker_detach_from_pool(worker); 238260f5a4bcSLai Jiangshan kfree(worker); 2383c8e55f36STejun Heo return 0; 2384c8e55f36STejun Heo } 2385c8e55f36STejun Heo 2386c8e55f36STejun Heo worker_leave_idle(worker); 2387db7bccf4STejun Heo recheck: 2388e22bee78STejun Heo /* no more worker necessary? */ 238963d95a91STejun Heo if (!need_more_worker(pool)) 2390e22bee78STejun Heo goto sleep; 2391e22bee78STejun Heo 2392e22bee78STejun Heo /* do we need to manage? */ 239363d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2394e22bee78STejun Heo goto recheck; 2395e22bee78STejun Heo 2396c8e55f36STejun Heo /* 2397c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2398c8e55f36STejun Heo * preparing to process a work or actually processing it. 2399c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2400c8e55f36STejun Heo */ 24016183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2402c8e55f36STejun Heo 2403e22bee78STejun Heo /* 2404a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2405a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2406a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2407a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2408a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2409e22bee78STejun Heo */ 2410a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2411e22bee78STejun Heo 2412e22bee78STejun Heo do { 2413affee4b2STejun Heo struct work_struct *work = 2414bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2415affee4b2STejun Heo struct work_struct, entry); 2416affee4b2STejun Heo 241782607adcSTejun Heo pool->watchdog_ts = jiffies; 241882607adcSTejun Heo 2419c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2420affee4b2STejun Heo /* optimization path, not strictly necessary */ 2421affee4b2STejun Heo process_one_work(worker, work); 2422affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2423affee4b2STejun Heo process_scheduled_works(worker); 2424affee4b2STejun Heo } else { 2425c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2426affee4b2STejun Heo process_scheduled_works(worker); 2427affee4b2STejun Heo } 242863d95a91STejun Heo } while (keep_working(pool)); 2429affee4b2STejun Heo 2430228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2431d313dd85STejun Heo sleep: 2432c8e55f36STejun Heo /* 2433d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2434d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2435d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2436d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2437d565ed63STejun Heo * event. 2438c8e55f36STejun Heo */ 2439c8e55f36STejun Heo worker_enter_idle(worker); 2440c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2441a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 24421da177e4SLinus Torvalds schedule(); 2443c8e55f36STejun Heo goto woke_up; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 2446e22bee78STejun Heo /** 2447e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2448111c225aSTejun Heo * @__rescuer: self 2449e22bee78STejun Heo * 2450e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2451493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2452e22bee78STejun Heo * 2453706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2454e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2455e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2456e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2457e22bee78STejun Heo * the problem rescuer solves. 2458e22bee78STejun Heo * 2459706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2460706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2461e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2462e22bee78STejun Heo * 2463e22bee78STejun Heo * This should happen rarely. 2464d185af30SYacine Belkadi * 2465d185af30SYacine Belkadi * Return: 0 2466e22bee78STejun Heo */ 2467111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2468e22bee78STejun Heo { 2469111c225aSTejun Heo struct worker *rescuer = __rescuer; 2470111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2471e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 24724d595b86SLai Jiangshan bool should_stop; 2473e22bee78STejun Heo 2474e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2475111c225aSTejun Heo 2476111c225aSTejun Heo /* 2477111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2478111c225aSTejun Heo * doesn't participate in concurrency management. 2479111c225aSTejun Heo */ 2480197f6accSTejun Heo set_pf_worker(true); 2481e22bee78STejun Heo repeat: 2482c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 24831da177e4SLinus Torvalds 24844d595b86SLai Jiangshan /* 24854d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 24864d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 24874d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 24884d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 24894d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 24904d595b86SLai Jiangshan * list is always empty on exit. 24914d595b86SLai Jiangshan */ 24924d595b86SLai Jiangshan should_stop = kthread_should_stop(); 24931da177e4SLinus Torvalds 2494493a1724STejun Heo /* see whether any pwq is asking for help */ 2495a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2496493a1724STejun Heo 2497493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2498493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2499493a1724STejun Heo struct pool_workqueue, mayday_node); 2500112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2501e22bee78STejun Heo struct work_struct *work, *n; 250282607adcSTejun Heo bool first = true; 2503e22bee78STejun Heo 2504e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2505493a1724STejun Heo list_del_init(&pwq->mayday_node); 2506493a1724STejun Heo 2507a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2508e22bee78STejun Heo 250951697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 251051697d39SLai Jiangshan 2511a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2512e22bee78STejun Heo 2513e22bee78STejun Heo /* 2514e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2515e22bee78STejun Heo * process'em. 2516e22bee78STejun Heo */ 25170479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 251882607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 251982607adcSTejun Heo if (get_work_pwq(work) == pwq) { 252082607adcSTejun Heo if (first) 252182607adcSTejun Heo pool->watchdog_ts = jiffies; 2522e22bee78STejun Heo move_linked_works(work, scheduled, &n); 252382607adcSTejun Heo } 252482607adcSTejun Heo first = false; 252582607adcSTejun Heo } 2526e22bee78STejun Heo 2527008847f6SNeilBrown if (!list_empty(scheduled)) { 2528e22bee78STejun Heo process_scheduled_works(rescuer); 25297576958aSTejun Heo 25307576958aSTejun Heo /* 2531008847f6SNeilBrown * The above execution of rescued work items could 2532008847f6SNeilBrown * have created more to rescue through 2533*f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2534008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2535008847f6SNeilBrown * that such back-to-back work items, which may be 2536008847f6SNeilBrown * being used to relieve memory pressure, don't 2537008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2538008847f6SNeilBrown */ 25394f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2540a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2541e66b39afSTejun Heo /* 2542e66b39afSTejun Heo * Queue iff we aren't racing destruction 2543e66b39afSTejun Heo * and somebody else hasn't queued it already. 2544e66b39afSTejun Heo */ 2545e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2546008847f6SNeilBrown get_pwq(pwq); 2547e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2548e66b39afSTejun Heo } 2549a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2550008847f6SNeilBrown } 2551008847f6SNeilBrown } 2552008847f6SNeilBrown 2553008847f6SNeilBrown /* 255477668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 255513b1d625SLai Jiangshan * go away while we're still attached to it. 255677668c8bSLai Jiangshan */ 255777668c8bSLai Jiangshan put_pwq(pwq); 255877668c8bSLai Jiangshan 255977668c8bSLai Jiangshan /* 2560d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 25617576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 25627576958aSTejun Heo * and stalling the execution. 25637576958aSTejun Heo */ 2564d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 256563d95a91STejun Heo wake_up_worker(pool); 25667576958aSTejun Heo 2567a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 256813b1d625SLai Jiangshan 2569a2d812a2STejun Heo worker_detach_from_pool(rescuer); 257013b1d625SLai Jiangshan 2571a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 2574a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2575493a1724STejun Heo 25764d595b86SLai Jiangshan if (should_stop) { 25774d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2578197f6accSTejun Heo set_pf_worker(false); 25794d595b86SLai Jiangshan return 0; 25804d595b86SLai Jiangshan } 25814d595b86SLai Jiangshan 2582111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2583111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2584e22bee78STejun Heo schedule(); 2585e22bee78STejun Heo goto repeat; 25861da177e4SLinus Torvalds } 25871da177e4SLinus Torvalds 2588fca839c0STejun Heo /** 2589fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2590fca839c0STejun Heo * @target_wq: workqueue being flushed 2591fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2592fca839c0STejun Heo * 2593fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2594fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2595fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2596fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2597fca839c0STejun Heo * a deadlock. 2598fca839c0STejun Heo */ 2599fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2600fca839c0STejun Heo struct work_struct *target_work) 2601fca839c0STejun Heo { 2602fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2603fca839c0STejun Heo struct worker *worker; 2604fca839c0STejun Heo 2605fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2606fca839c0STejun Heo return; 2607fca839c0STejun Heo 2608fca839c0STejun Heo worker = current_wq_worker(); 2609fca839c0STejun Heo 2610fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2611d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2612fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 261323d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 261423d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2615d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2616fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2617fca839c0STejun Heo target_wq->name, target_func); 2618fca839c0STejun Heo } 2619fca839c0STejun Heo 2620fc2e4d70SOleg Nesterov struct wq_barrier { 2621fc2e4d70SOleg Nesterov struct work_struct work; 2622fc2e4d70SOleg Nesterov struct completion done; 26232607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2624fc2e4d70SOleg Nesterov }; 2625fc2e4d70SOleg Nesterov 2626fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2627fc2e4d70SOleg Nesterov { 2628fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2629fc2e4d70SOleg Nesterov complete(&barr->done); 2630fc2e4d70SOleg Nesterov } 2631fc2e4d70SOleg Nesterov 26324690c4abSTejun Heo /** 26334690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2634112202d9STejun Heo * @pwq: pwq to insert barrier into 26354690c4abSTejun Heo * @barr: wq_barrier to insert 2636affee4b2STejun Heo * @target: target work to attach @barr to 2637affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 26384690c4abSTejun Heo * 2639affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2640affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2641affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2642affee4b2STejun Heo * cpu. 2643affee4b2STejun Heo * 2644affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2645affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2646affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2647affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2648affee4b2STejun Heo * after a work with LINKED flag set. 2649affee4b2STejun Heo * 2650affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2651112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 26524690c4abSTejun Heo * 26534690c4abSTejun Heo * CONTEXT: 2654a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 26554690c4abSTejun Heo */ 2656112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2657affee4b2STejun Heo struct wq_barrier *barr, 2658affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2659fc2e4d70SOleg Nesterov { 2660affee4b2STejun Heo struct list_head *head; 2661affee4b2STejun Heo unsigned int linked = 0; 2662affee4b2STejun Heo 2663dc186ad7SThomas Gleixner /* 2664d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2665dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2666dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2667dc186ad7SThomas Gleixner * might deadlock. 2668dc186ad7SThomas Gleixner */ 2669ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 267022df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 267152fa5bc5SBoqun Feng 2672fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2673fd1a5b04SByungchul Park 26742607d7a6STejun Heo barr->task = current; 267583c22520SOleg Nesterov 2676affee4b2STejun Heo /* 2677affee4b2STejun Heo * If @target is currently being executed, schedule the 2678affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2679affee4b2STejun Heo */ 2680affee4b2STejun Heo if (worker) 2681affee4b2STejun Heo head = worker->scheduled.next; 2682affee4b2STejun Heo else { 2683affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2684affee4b2STejun Heo 2685affee4b2STejun Heo head = target->entry.next; 2686affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2687affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2688affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2689affee4b2STejun Heo } 2690affee4b2STejun Heo 2691dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2692112202d9STejun Heo insert_work(pwq, &barr->work, head, 2693affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2694fc2e4d70SOleg Nesterov } 2695fc2e4d70SOleg Nesterov 269673f53c4aSTejun Heo /** 2697112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 269873f53c4aSTejun Heo * @wq: workqueue being flushed 269973f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 270073f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 270173f53c4aSTejun Heo * 2702112202d9STejun Heo * Prepare pwqs for workqueue flushing. 270373f53c4aSTejun Heo * 2704112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2705112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2706112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2707112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2708112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 270973f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 271073f53c4aSTejun Heo * 271173f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 271273f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 271373f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 271473f53c4aSTejun Heo * is returned. 271573f53c4aSTejun Heo * 2716112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 271773f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 271873f53c4aSTejun Heo * advanced to @work_color. 271973f53c4aSTejun Heo * 272073f53c4aSTejun Heo * CONTEXT: 27213c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 272273f53c4aSTejun Heo * 2723d185af30SYacine Belkadi * Return: 272473f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 272573f53c4aSTejun Heo * otherwise. 272673f53c4aSTejun Heo */ 2727112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 272873f53c4aSTejun Heo int flush_color, int work_color) 27291da177e4SLinus Torvalds { 273073f53c4aSTejun Heo bool wait = false; 273149e3cf44STejun Heo struct pool_workqueue *pwq; 27321da177e4SLinus Torvalds 273373f53c4aSTejun Heo if (flush_color >= 0) { 27346183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2735112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2736dc186ad7SThomas Gleixner } 273714441960SOleg Nesterov 273849e3cf44STejun Heo for_each_pwq(pwq, wq) { 2739112202d9STejun Heo struct worker_pool *pool = pwq->pool; 27401da177e4SLinus Torvalds 2741a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 274273f53c4aSTejun Heo 274373f53c4aSTejun Heo if (flush_color >= 0) { 27446183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 274573f53c4aSTejun Heo 2746112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2747112202d9STejun Heo pwq->flush_color = flush_color; 2748112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 274973f53c4aSTejun Heo wait = true; 27501da177e4SLinus Torvalds } 275173f53c4aSTejun Heo } 275273f53c4aSTejun Heo 275373f53c4aSTejun Heo if (work_color >= 0) { 27546183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2755112202d9STejun Heo pwq->work_color = work_color; 275673f53c4aSTejun Heo } 275773f53c4aSTejun Heo 2758a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 27591da177e4SLinus Torvalds } 27601da177e4SLinus Torvalds 2761112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 276273f53c4aSTejun Heo complete(&wq->first_flusher->done); 276373f53c4aSTejun Heo 276473f53c4aSTejun Heo return wait; 276583c22520SOleg Nesterov } 27661da177e4SLinus Torvalds 27670fcb78c2SRolf Eike Beer /** 27681da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 27690fcb78c2SRolf Eike Beer * @wq: workqueue to flush 27701da177e4SLinus Torvalds * 2771c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2772c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 27731da177e4SLinus Torvalds */ 27747ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 27751da177e4SLinus Torvalds { 277673f53c4aSTejun Heo struct wq_flusher this_flusher = { 277773f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 277873f53c4aSTejun Heo .flush_color = -1, 2779fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 278073f53c4aSTejun Heo }; 278173f53c4aSTejun Heo int next_color; 2782b1f4ec17SOleg Nesterov 27833347fa09STejun Heo if (WARN_ON(!wq_online)) 27843347fa09STejun Heo return; 27853347fa09STejun Heo 278687915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 278787915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 278887915adcSJohannes Berg 27893c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 279073f53c4aSTejun Heo 279173f53c4aSTejun Heo /* 279273f53c4aSTejun Heo * Start-to-wait phase 279373f53c4aSTejun Heo */ 279473f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 279573f53c4aSTejun Heo 279673f53c4aSTejun Heo if (next_color != wq->flush_color) { 279773f53c4aSTejun Heo /* 279873f53c4aSTejun Heo * Color space is not full. The current work_color 279973f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 280073f53c4aSTejun Heo * by one. 280173f53c4aSTejun Heo */ 28026183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 280373f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 280473f53c4aSTejun Heo wq->work_color = next_color; 280573f53c4aSTejun Heo 280673f53c4aSTejun Heo if (!wq->first_flusher) { 280773f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 28086183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 280973f53c4aSTejun Heo 281073f53c4aSTejun Heo wq->first_flusher = &this_flusher; 281173f53c4aSTejun Heo 2812112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 281373f53c4aSTejun Heo wq->work_color)) { 281473f53c4aSTejun Heo /* nothing to flush, done */ 281573f53c4aSTejun Heo wq->flush_color = next_color; 281673f53c4aSTejun Heo wq->first_flusher = NULL; 281773f53c4aSTejun Heo goto out_unlock; 281873f53c4aSTejun Heo } 281973f53c4aSTejun Heo } else { 282073f53c4aSTejun Heo /* wait in queue */ 28216183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 282273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2823112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 282473f53c4aSTejun Heo } 282573f53c4aSTejun Heo } else { 282673f53c4aSTejun Heo /* 282773f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 282873f53c4aSTejun Heo * The next flush completion will assign us 282973f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 283073f53c4aSTejun Heo */ 283173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 283273f53c4aSTejun Heo } 283373f53c4aSTejun Heo 2834fca839c0STejun Heo check_flush_dependency(wq, NULL); 2835fca839c0STejun Heo 28363c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 283773f53c4aSTejun Heo 283873f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 283973f53c4aSTejun Heo 284073f53c4aSTejun Heo /* 284173f53c4aSTejun Heo * Wake-up-and-cascade phase 284273f53c4aSTejun Heo * 284373f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 284473f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 284573f53c4aSTejun Heo */ 284600d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 284773f53c4aSTejun Heo return; 284873f53c4aSTejun Heo 28493c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 285073f53c4aSTejun Heo 28514ce48b37STejun Heo /* we might have raced, check again with mutex held */ 28524ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 28534ce48b37STejun Heo goto out_unlock; 28544ce48b37STejun Heo 285500d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 285673f53c4aSTejun Heo 28576183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 28586183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 285973f53c4aSTejun Heo 286073f53c4aSTejun Heo while (true) { 286173f53c4aSTejun Heo struct wq_flusher *next, *tmp; 286273f53c4aSTejun Heo 286373f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 286473f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 286573f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 286673f53c4aSTejun Heo break; 286773f53c4aSTejun Heo list_del_init(&next->list); 286873f53c4aSTejun Heo complete(&next->done); 286973f53c4aSTejun Heo } 287073f53c4aSTejun Heo 28716183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 287273f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 287373f53c4aSTejun Heo 287473f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 287573f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 287673f53c4aSTejun Heo 287773f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 287873f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 287973f53c4aSTejun Heo /* 288073f53c4aSTejun Heo * Assign the same color to all overflowed 288173f53c4aSTejun Heo * flushers, advance work_color and append to 288273f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 288373f53c4aSTejun Heo * phase for these overflowed flushers. 288473f53c4aSTejun Heo */ 288573f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 288673f53c4aSTejun Heo tmp->flush_color = wq->work_color; 288773f53c4aSTejun Heo 288873f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 288973f53c4aSTejun Heo 289073f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 289173f53c4aSTejun Heo &wq->flusher_queue); 2892112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 289373f53c4aSTejun Heo } 289473f53c4aSTejun Heo 289573f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 28966183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 289773f53c4aSTejun Heo break; 289873f53c4aSTejun Heo } 289973f53c4aSTejun Heo 290073f53c4aSTejun Heo /* 290173f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2902112202d9STejun Heo * the new first flusher and arm pwqs. 290373f53c4aSTejun Heo */ 29046183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 29056183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 290673f53c4aSTejun Heo 290773f53c4aSTejun Heo list_del_init(&next->list); 290873f53c4aSTejun Heo wq->first_flusher = next; 290973f53c4aSTejun Heo 2910112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 291173f53c4aSTejun Heo break; 291273f53c4aSTejun Heo 291373f53c4aSTejun Heo /* 291473f53c4aSTejun Heo * Meh... this color is already done, clear first 291573f53c4aSTejun Heo * flusher and repeat cascading. 291673f53c4aSTejun Heo */ 291773f53c4aSTejun Heo wq->first_flusher = NULL; 291873f53c4aSTejun Heo } 291973f53c4aSTejun Heo 292073f53c4aSTejun Heo out_unlock: 29213c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 29221da177e4SLinus Torvalds } 29231dadafa8STim Gardner EXPORT_SYMBOL(flush_workqueue); 29241da177e4SLinus Torvalds 29259c5a2ba7STejun Heo /** 29269c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 29279c5a2ba7STejun Heo * @wq: workqueue to drain 29289c5a2ba7STejun Heo * 29299c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 29309c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 29319c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 2932b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 29339c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 29349c5a2ba7STejun Heo * takes too long. 29359c5a2ba7STejun Heo */ 29369c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 29379c5a2ba7STejun Heo { 29389c5a2ba7STejun Heo unsigned int flush_cnt = 0; 293949e3cf44STejun Heo struct pool_workqueue *pwq; 29409c5a2ba7STejun Heo 29419c5a2ba7STejun Heo /* 29429c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 29439c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 2944618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 29459c5a2ba7STejun Heo */ 294687fc741eSLai Jiangshan mutex_lock(&wq->mutex); 29479c5a2ba7STejun Heo if (!wq->nr_drainers++) 2948618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 294987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 29509c5a2ba7STejun Heo reflush: 29519c5a2ba7STejun Heo flush_workqueue(wq); 29529c5a2ba7STejun Heo 2953b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 295476af4d93STejun Heo 295549e3cf44STejun Heo for_each_pwq(pwq, wq) { 2956fa2563e4SThomas Tuttle bool drained; 29579c5a2ba7STejun Heo 2958a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 2959*f97a4a1aSLai Jiangshan drained = !pwq->nr_active && list_empty(&pwq->inactive_works); 2960a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 2961fa2563e4SThomas Tuttle 2962fa2563e4SThomas Tuttle if (drained) 29639c5a2ba7STejun Heo continue; 29649c5a2ba7STejun Heo 29659c5a2ba7STejun Heo if (++flush_cnt == 10 || 29669c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2967e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 2968e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 296976af4d93STejun Heo 2970b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 29719c5a2ba7STejun Heo goto reflush; 29729c5a2ba7STejun Heo } 29739c5a2ba7STejun Heo 29749c5a2ba7STejun Heo if (!--wq->nr_drainers) 2975618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 297687fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 29779c5a2ba7STejun Heo } 29789c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 29799c5a2ba7STejun Heo 2980d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2981d6e89786SJohannes Berg bool from_cancel) 2982baf59022STejun Heo { 2983baf59022STejun Heo struct worker *worker = NULL; 2984c9e7cf27STejun Heo struct worker_pool *pool; 2985112202d9STejun Heo struct pool_workqueue *pwq; 2986baf59022STejun Heo 2987baf59022STejun Heo might_sleep(); 2988baf59022STejun Heo 298924acfb71SThomas Gleixner rcu_read_lock(); 2990fa1b54e6STejun Heo pool = get_work_pool(work); 2991fa1b54e6STejun Heo if (!pool) { 299224acfb71SThomas Gleixner rcu_read_unlock(); 2993fa1b54e6STejun Heo return false; 2994fa1b54e6STejun Heo } 2995fa1b54e6STejun Heo 2996a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 29970b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2998112202d9STejun Heo pwq = get_work_pwq(work); 2999112202d9STejun Heo if (pwq) { 3000112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3001baf59022STejun Heo goto already_gone; 3002606a5020STejun Heo } else { 3003c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3004baf59022STejun Heo if (!worker) 3005baf59022STejun Heo goto already_gone; 3006112202d9STejun Heo pwq = worker->current_pwq; 3007606a5020STejun Heo } 3008baf59022STejun Heo 3009fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3010fca839c0STejun Heo 3011112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3012a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3013baf59022STejun Heo 3014e159489bSTejun Heo /* 3015a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3016a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3017a1d14934SPeter Zijlstra * 3018a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3019a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3020a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3021a1d14934SPeter Zijlstra * forward progress. 3022e159489bSTejun Heo */ 3023d6e89786SJohannes Berg if (!from_cancel && 3024d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3025112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3026112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3027a1d14934SPeter Zijlstra } 302824acfb71SThomas Gleixner rcu_read_unlock(); 3029baf59022STejun Heo return true; 3030baf59022STejun Heo already_gone: 3031a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 303224acfb71SThomas Gleixner rcu_read_unlock(); 3033baf59022STejun Heo return false; 3034baf59022STejun Heo } 3035baf59022STejun Heo 3036d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3037d6e89786SJohannes Berg { 3038d6e89786SJohannes Berg struct wq_barrier barr; 3039d6e89786SJohannes Berg 3040d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3041d6e89786SJohannes Berg return false; 3042d6e89786SJohannes Berg 30434d43d395STetsuo Handa if (WARN_ON(!work->func)) 30444d43d395STetsuo Handa return false; 30454d43d395STetsuo Handa 304687915adcSJohannes Berg if (!from_cancel) { 304787915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 304887915adcSJohannes Berg lock_map_release(&work->lockdep_map); 304987915adcSJohannes Berg } 305087915adcSJohannes Berg 3051d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3052d6e89786SJohannes Berg wait_for_completion(&barr.done); 3053d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3054d6e89786SJohannes Berg return true; 3055d6e89786SJohannes Berg } else { 3056d6e89786SJohannes Berg return false; 3057d6e89786SJohannes Berg } 3058d6e89786SJohannes Berg } 3059d6e89786SJohannes Berg 3060db700897SOleg Nesterov /** 3061401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3062401a8d04STejun Heo * @work: the work to flush 3063db700897SOleg Nesterov * 3064606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3065606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3066401a8d04STejun Heo * 3067d185af30SYacine Belkadi * Return: 3068401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3069401a8d04STejun Heo * %false if it was already idle. 3070db700897SOleg Nesterov */ 3071401a8d04STejun Heo bool flush_work(struct work_struct *work) 3072db700897SOleg Nesterov { 3073d6e89786SJohannes Berg return __flush_work(work, false); 3074606a5020STejun Heo } 3075db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3076db700897SOleg Nesterov 30778603e1b3STejun Heo struct cwt_wait { 3078ac6424b9SIngo Molnar wait_queue_entry_t wait; 30798603e1b3STejun Heo struct work_struct *work; 30808603e1b3STejun Heo }; 30818603e1b3STejun Heo 3082ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 30838603e1b3STejun Heo { 30848603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 30858603e1b3STejun Heo 30868603e1b3STejun Heo if (cwait->work != key) 30878603e1b3STejun Heo return 0; 30888603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 30898603e1b3STejun Heo } 30908603e1b3STejun Heo 309136e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3092401a8d04STejun Heo { 30938603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3094bbb68dfaSTejun Heo unsigned long flags; 30951f1f642eSOleg Nesterov int ret; 30961f1f642eSOleg Nesterov 30971f1f642eSOleg Nesterov do { 3098bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3099bbb68dfaSTejun Heo /* 31008603e1b3STejun Heo * If someone else is already canceling, wait for it to 31018603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 31028603e1b3STejun Heo * because we may get scheduled between @work's completion 31038603e1b3STejun Heo * and the other canceling task resuming and clearing 31048603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 31058603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 31068603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 31078603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 31088603e1b3STejun Heo * we're hogging the CPU. 31098603e1b3STejun Heo * 31108603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 31118603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 31128603e1b3STejun Heo * wake function which matches @work along with exclusive 31138603e1b3STejun Heo * wait and wakeup. 3114bbb68dfaSTejun Heo */ 31158603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 31168603e1b3STejun Heo struct cwt_wait cwait; 31178603e1b3STejun Heo 31188603e1b3STejun Heo init_wait(&cwait.wait); 31198603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 31208603e1b3STejun Heo cwait.work = work; 31218603e1b3STejun Heo 31228603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 31238603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 31248603e1b3STejun Heo if (work_is_canceling(work)) 31258603e1b3STejun Heo schedule(); 31268603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 31278603e1b3STejun Heo } 31281f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 31291f1f642eSOleg Nesterov 3130bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3131bbb68dfaSTejun Heo mark_work_canceling(work); 3132bbb68dfaSTejun Heo local_irq_restore(flags); 3133bbb68dfaSTejun Heo 31343347fa09STejun Heo /* 31353347fa09STejun Heo * This allows canceling during early boot. We know that @work 31363347fa09STejun Heo * isn't executing. 31373347fa09STejun Heo */ 31383347fa09STejun Heo if (wq_online) 3139d6e89786SJohannes Berg __flush_work(work, true); 31403347fa09STejun Heo 31417a22ad75STejun Heo clear_work_data(work); 31428603e1b3STejun Heo 31438603e1b3STejun Heo /* 31448603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 31458603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 31468603e1b3STejun Heo * visible there. 31478603e1b3STejun Heo */ 31488603e1b3STejun Heo smp_mb(); 31498603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 31508603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 31518603e1b3STejun Heo 31521f1f642eSOleg Nesterov return ret; 31531f1f642eSOleg Nesterov } 31541f1f642eSOleg Nesterov 31556e84d644SOleg Nesterov /** 3156401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3157401a8d04STejun Heo * @work: the work to cancel 31586e84d644SOleg Nesterov * 3159401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3160401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3161401a8d04STejun Heo * another workqueue. On return from this function, @work is 3162401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 31631f1f642eSOleg Nesterov * 3164401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3165401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 31666e84d644SOleg Nesterov * 3167401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 31686e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3169401a8d04STejun Heo * 3170d185af30SYacine Belkadi * Return: 3171401a8d04STejun Heo * %true if @work was pending, %false otherwise. 31726e84d644SOleg Nesterov */ 3173401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 31746e84d644SOleg Nesterov { 317536e227d2STejun Heo return __cancel_work_timer(work, false); 3176b89deed3SOleg Nesterov } 317728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3178b89deed3SOleg Nesterov 31796e84d644SOleg Nesterov /** 3180401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3181401a8d04STejun Heo * @dwork: the delayed work to flush 31826e84d644SOleg Nesterov * 3183401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3184401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3185401a8d04STejun Heo * considers the last queueing instance of @dwork. 31861f1f642eSOleg Nesterov * 3187d185af30SYacine Belkadi * Return: 3188401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3189401a8d04STejun Heo * %false if it was already idle. 31906e84d644SOleg Nesterov */ 3191401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3192401a8d04STejun Heo { 31938930cabaSTejun Heo local_irq_disable(); 3194401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 319560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 31968930cabaSTejun Heo local_irq_enable(); 3197401a8d04STejun Heo return flush_work(&dwork->work); 3198401a8d04STejun Heo } 3199401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3200401a8d04STejun Heo 320105f0fe6bSTejun Heo /** 320205f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 320305f0fe6bSTejun Heo * @rwork: the rcu work to flush 320405f0fe6bSTejun Heo * 320505f0fe6bSTejun Heo * Return: 320605f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 320705f0fe6bSTejun Heo * %false if it was already idle. 320805f0fe6bSTejun Heo */ 320905f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 321005f0fe6bSTejun Heo { 321105f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 321205f0fe6bSTejun Heo rcu_barrier(); 321305f0fe6bSTejun Heo flush_work(&rwork->work); 321405f0fe6bSTejun Heo return true; 321505f0fe6bSTejun Heo } else { 321605f0fe6bSTejun Heo return flush_work(&rwork->work); 321705f0fe6bSTejun Heo } 321805f0fe6bSTejun Heo } 321905f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 322005f0fe6bSTejun Heo 3221f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3222f72b8792SJens Axboe { 3223f72b8792SJens Axboe unsigned long flags; 3224f72b8792SJens Axboe int ret; 3225f72b8792SJens Axboe 3226f72b8792SJens Axboe do { 3227f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3228f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3229f72b8792SJens Axboe 3230f72b8792SJens Axboe if (unlikely(ret < 0)) 3231f72b8792SJens Axboe return false; 3232f72b8792SJens Axboe 3233f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3234f72b8792SJens Axboe local_irq_restore(flags); 3235f72b8792SJens Axboe return ret; 3236f72b8792SJens Axboe } 3237f72b8792SJens Axboe 3238401a8d04STejun Heo /** 323957b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 324057b30ae7STejun Heo * @dwork: delayed_work to cancel 324109383498STejun Heo * 3242d185af30SYacine Belkadi * Kill off a pending delayed_work. 3243d185af30SYacine Belkadi * 3244d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3245d185af30SYacine Belkadi * pending. 3246d185af30SYacine Belkadi * 3247d185af30SYacine Belkadi * Note: 3248d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3249d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3250d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 325109383498STejun Heo * 325257b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 325309383498STejun Heo */ 325457b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 325509383498STejun Heo { 3256f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 325709383498STejun Heo } 325857b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 325909383498STejun Heo 326009383498STejun Heo /** 3261401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3262401a8d04STejun Heo * @dwork: the delayed work cancel 3263401a8d04STejun Heo * 3264401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3265401a8d04STejun Heo * 3266d185af30SYacine Belkadi * Return: 3267401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3268401a8d04STejun Heo */ 3269401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 32706e84d644SOleg Nesterov { 327136e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 32726e84d644SOleg Nesterov } 3273f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 32741da177e4SLinus Torvalds 32750fcb78c2SRolf Eike Beer /** 327631ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3277b6136773SAndrew Morton * @func: the function to call 3278b6136773SAndrew Morton * 327931ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 328031ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3281b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 328231ddd871STejun Heo * 3283d185af30SYacine Belkadi * Return: 328431ddd871STejun Heo * 0 on success, -errno on failure. 3285b6136773SAndrew Morton */ 328665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 328715316ba8SChristoph Lameter { 328815316ba8SChristoph Lameter int cpu; 328938f51568SNamhyung Kim struct work_struct __percpu *works; 329015316ba8SChristoph Lameter 3291b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3292b6136773SAndrew Morton if (!works) 329315316ba8SChristoph Lameter return -ENOMEM; 3294b6136773SAndrew Morton 3295ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 329693981800STejun Heo 329715316ba8SChristoph Lameter for_each_online_cpu(cpu) { 32989bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 32999bfb1839SIngo Molnar 33009bfb1839SIngo Molnar INIT_WORK(work, func); 33018de6d308SOleg Nesterov schedule_work_on(cpu, work); 330215316ba8SChristoph Lameter } 330393981800STejun Heo 330493981800STejun Heo for_each_online_cpu(cpu) 33058616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 330693981800STejun Heo 3307ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3308b6136773SAndrew Morton free_percpu(works); 330915316ba8SChristoph Lameter return 0; 331015316ba8SChristoph Lameter } 331115316ba8SChristoph Lameter 3312eef6a7d5SAlan Stern /** 33131fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 33141fa44ecaSJames Bottomley * @fn: the function to execute 33151fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 33161fa44ecaSJames Bottomley * be available when the work executes) 33171fa44ecaSJames Bottomley * 33181fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 33191fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 33201fa44ecaSJames Bottomley * 3321d185af30SYacine Belkadi * Return: 0 - function was executed 33221fa44ecaSJames Bottomley * 1 - function was scheduled for execution 33231fa44ecaSJames Bottomley */ 332465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 33251fa44ecaSJames Bottomley { 33261fa44ecaSJames Bottomley if (!in_interrupt()) { 332765f27f38SDavid Howells fn(&ew->work); 33281fa44ecaSJames Bottomley return 0; 33291fa44ecaSJames Bottomley } 33301fa44ecaSJames Bottomley 333165f27f38SDavid Howells INIT_WORK(&ew->work, fn); 33321fa44ecaSJames Bottomley schedule_work(&ew->work); 33331fa44ecaSJames Bottomley 33341fa44ecaSJames Bottomley return 1; 33351fa44ecaSJames Bottomley } 33361fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 33371fa44ecaSJames Bottomley 33387a4e344cSTejun Heo /** 33397a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 33407a4e344cSTejun Heo * @attrs: workqueue_attrs to free 33417a4e344cSTejun Heo * 33427a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 33437a4e344cSTejun Heo */ 3344513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 33457a4e344cSTejun Heo { 33467a4e344cSTejun Heo if (attrs) { 33477a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 33487a4e344cSTejun Heo kfree(attrs); 33497a4e344cSTejun Heo } 33507a4e344cSTejun Heo } 33517a4e344cSTejun Heo 33527a4e344cSTejun Heo /** 33537a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 33547a4e344cSTejun Heo * 33557a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3356d185af30SYacine Belkadi * return it. 3357d185af30SYacine Belkadi * 3358d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 33597a4e344cSTejun Heo */ 3360513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 33617a4e344cSTejun Heo { 33627a4e344cSTejun Heo struct workqueue_attrs *attrs; 33637a4e344cSTejun Heo 3364be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 33657a4e344cSTejun Heo if (!attrs) 33667a4e344cSTejun Heo goto fail; 3367be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 33687a4e344cSTejun Heo goto fail; 33697a4e344cSTejun Heo 337013e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 33717a4e344cSTejun Heo return attrs; 33727a4e344cSTejun Heo fail: 33737a4e344cSTejun Heo free_workqueue_attrs(attrs); 33747a4e344cSTejun Heo return NULL; 33757a4e344cSTejun Heo } 33767a4e344cSTejun Heo 337729c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 337829c91e99STejun Heo const struct workqueue_attrs *from) 337929c91e99STejun Heo { 338029c91e99STejun Heo to->nice = from->nice; 338129c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 33822865a8fbSShaohua Li /* 33832865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 33842865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 33852865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 33862865a8fbSShaohua Li */ 33872865a8fbSShaohua Li to->no_numa = from->no_numa; 338829c91e99STejun Heo } 338929c91e99STejun Heo 339029c91e99STejun Heo /* hash value of the content of @attr */ 339129c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 339229c91e99STejun Heo { 339329c91e99STejun Heo u32 hash = 0; 339429c91e99STejun Heo 339529c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 339613e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 339713e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 339829c91e99STejun Heo return hash; 339929c91e99STejun Heo } 340029c91e99STejun Heo 340129c91e99STejun Heo /* content equality test */ 340229c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 340329c91e99STejun Heo const struct workqueue_attrs *b) 340429c91e99STejun Heo { 340529c91e99STejun Heo if (a->nice != b->nice) 340629c91e99STejun Heo return false; 340729c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 340829c91e99STejun Heo return false; 340929c91e99STejun Heo return true; 341029c91e99STejun Heo } 341129c91e99STejun Heo 34127a4e344cSTejun Heo /** 34137a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 34147a4e344cSTejun Heo * @pool: worker_pool to initialize 34157a4e344cSTejun Heo * 3416402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3417d185af30SYacine Belkadi * 3418d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 341929c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 342029c91e99STejun Heo * on @pool safely to release it. 34217a4e344cSTejun Heo */ 34227a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 34234e1a1f9aSTejun Heo { 3424a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 342529c91e99STejun Heo pool->id = -1; 342629c91e99STejun Heo pool->cpu = -1; 3427f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 34284e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 342982607adcSTejun Heo pool->watchdog_ts = jiffies; 34304e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 34314e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 34324e1a1f9aSTejun Heo hash_init(pool->busy_hash); 34334e1a1f9aSTejun Heo 343432a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 34354e1a1f9aSTejun Heo 343632a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 34374e1a1f9aSTejun Heo 3438da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 34397a4e344cSTejun Heo 34407cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 344129c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 344229c91e99STejun Heo pool->refcnt = 1; 344329c91e99STejun Heo 344429c91e99STejun Heo /* shouldn't fail above this point */ 3445be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 34467a4e344cSTejun Heo if (!pool->attrs) 34477a4e344cSTejun Heo return -ENOMEM; 34487a4e344cSTejun Heo return 0; 34494e1a1f9aSTejun Heo } 34504e1a1f9aSTejun Heo 3451669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3452669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3453669de8bdSBart Van Assche { 3454669de8bdSBart Van Assche char *lock_name; 3455669de8bdSBart Van Assche 3456669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3457669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3458669de8bdSBart Van Assche if (!lock_name) 3459669de8bdSBart Van Assche lock_name = wq->name; 346069a106c0SQian Cai 346169a106c0SQian Cai wq->lock_name = lock_name; 3462669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3463669de8bdSBart Van Assche } 3464669de8bdSBart Van Assche 3465669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3466669de8bdSBart Van Assche { 3467669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3468669de8bdSBart Van Assche } 3469669de8bdSBart Van Assche 3470669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3471669de8bdSBart Van Assche { 3472669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3473669de8bdSBart Van Assche kfree(wq->lock_name); 3474669de8bdSBart Van Assche } 3475669de8bdSBart Van Assche #else 3476669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3477669de8bdSBart Van Assche { 3478669de8bdSBart Van Assche } 3479669de8bdSBart Van Assche 3480669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3481669de8bdSBart Van Assche { 3482669de8bdSBart Van Assche } 3483669de8bdSBart Van Assche 3484669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3485669de8bdSBart Van Assche { 3486669de8bdSBart Van Assche } 3487669de8bdSBart Van Assche #endif 3488669de8bdSBart Van Assche 3489e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3490e2dca7adSTejun Heo { 3491e2dca7adSTejun Heo struct workqueue_struct *wq = 3492e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3493e2dca7adSTejun Heo 3494669de8bdSBart Van Assche wq_free_lockdep(wq); 3495669de8bdSBart Van Assche 3496e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3497e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3498e2dca7adSTejun Heo else 3499e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3500e2dca7adSTejun Heo 3501e2dca7adSTejun Heo kfree(wq); 3502e2dca7adSTejun Heo } 3503e2dca7adSTejun Heo 350429c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 350529c91e99STejun Heo { 350629c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 350729c91e99STejun Heo 35087cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 350929c91e99STejun Heo free_workqueue_attrs(pool->attrs); 351029c91e99STejun Heo kfree(pool); 351129c91e99STejun Heo } 351229c91e99STejun Heo 3513d8bb65abSSebastian Andrzej Siewior /* This returns with the lock held on success (pool manager is inactive). */ 3514d8bb65abSSebastian Andrzej Siewior static bool wq_manager_inactive(struct worker_pool *pool) 3515d8bb65abSSebastian Andrzej Siewior { 3516a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3517d8bb65abSSebastian Andrzej Siewior 3518d8bb65abSSebastian Andrzej Siewior if (pool->flags & POOL_MANAGER_ACTIVE) { 3519a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3520d8bb65abSSebastian Andrzej Siewior return false; 3521d8bb65abSSebastian Andrzej Siewior } 3522d8bb65abSSebastian Andrzej Siewior return true; 3523d8bb65abSSebastian Andrzej Siewior } 3524d8bb65abSSebastian Andrzej Siewior 352529c91e99STejun Heo /** 352629c91e99STejun Heo * put_unbound_pool - put a worker_pool 352729c91e99STejun Heo * @pool: worker_pool to put 352829c91e99STejun Heo * 352924acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3530c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3531c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3532c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3533a892caccSTejun Heo * 3534a892caccSTejun Heo * Should be called with wq_pool_mutex held. 353529c91e99STejun Heo */ 353629c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 353729c91e99STejun Heo { 353860f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 353929c91e99STejun Heo struct worker *worker; 354029c91e99STejun Heo 3541a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3542a892caccSTejun Heo 3543a892caccSTejun Heo if (--pool->refcnt) 354429c91e99STejun Heo return; 354529c91e99STejun Heo 354629c91e99STejun Heo /* sanity checks */ 354761d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3548a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 354929c91e99STejun Heo return; 355029c91e99STejun Heo 355129c91e99STejun Heo /* release id and unhash */ 355229c91e99STejun Heo if (pool->id >= 0) 355329c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 355429c91e99STejun Heo hash_del(&pool->hash_node); 355529c91e99STejun Heo 3556c5aa87bbSTejun Heo /* 3557692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3558692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3559692b4825STejun Heo * manager and @pool gets freed with the flag set. 3560d8bb65abSSebastian Andrzej Siewior * Because of how wq_manager_inactive() works, we will hold the 3561d8bb65abSSebastian Andrzej Siewior * spinlock after a successful wait. 3562c5aa87bbSTejun Heo */ 3563d8bb65abSSebastian Andrzej Siewior rcuwait_wait_event(&manager_wait, wq_manager_inactive(pool), 3564d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 3565692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 3566692b4825STejun Heo 35671037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 356829c91e99STejun Heo destroy_worker(worker); 356929c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 3570a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 357160f5a4bcSLai Jiangshan 35721258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 3573da028469SLai Jiangshan if (!list_empty(&pool->workers)) 357460f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 35751258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 357660f5a4bcSLai Jiangshan 357760f5a4bcSLai Jiangshan if (pool->detach_completion) 357860f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 357960f5a4bcSLai Jiangshan 358029c91e99STejun Heo /* shut down the timers */ 358129c91e99STejun Heo del_timer_sync(&pool->idle_timer); 358229c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 358329c91e99STejun Heo 358424acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 358525b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 358629c91e99STejun Heo } 358729c91e99STejun Heo 358829c91e99STejun Heo /** 358929c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 359029c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 359129c91e99STejun Heo * 359229c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 359329c91e99STejun Heo * reference count and return it. If there already is a matching 359429c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3595d185af30SYacine Belkadi * create a new one. 3596a892caccSTejun Heo * 3597a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3598d185af30SYacine Belkadi * 3599d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3600d185af30SYacine Belkadi * On failure, %NULL. 360129c91e99STejun Heo */ 360229c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 360329c91e99STejun Heo { 360429c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 360529c91e99STejun Heo struct worker_pool *pool; 3606f3f90ad4STejun Heo int node; 3607e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 360829c91e99STejun Heo 3609a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 361029c91e99STejun Heo 361129c91e99STejun Heo /* do we already have a matching pool? */ 361229c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 361329c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 361429c91e99STejun Heo pool->refcnt++; 36153fb1823cSLai Jiangshan return pool; 361629c91e99STejun Heo } 361729c91e99STejun Heo } 361829c91e99STejun Heo 3619e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3620e2273584SXunlei Pang if (wq_numa_enabled) { 3621e2273584SXunlei Pang for_each_node(node) { 3622e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3623e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3624e2273584SXunlei Pang target_node = node; 3625e2273584SXunlei Pang break; 3626e2273584SXunlei Pang } 3627e2273584SXunlei Pang } 3628e2273584SXunlei Pang } 3629e2273584SXunlei Pang 363029c91e99STejun Heo /* nope, create a new one */ 3631e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 363229c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 363329c91e99STejun Heo goto fail; 363429c91e99STejun Heo 36358864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 363629c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3637e2273584SXunlei Pang pool->node = target_node; 363829c91e99STejun Heo 36392865a8fbSShaohua Li /* 36402865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 36412865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 36422865a8fbSShaohua Li */ 36432865a8fbSShaohua Li pool->attrs->no_numa = false; 36442865a8fbSShaohua Li 364529c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 364629c91e99STejun Heo goto fail; 364729c91e99STejun Heo 364829c91e99STejun Heo /* create and start the initial worker */ 36493347fa09STejun Heo if (wq_online && !create_worker(pool)) 365029c91e99STejun Heo goto fail; 365129c91e99STejun Heo 365229c91e99STejun Heo /* install */ 365329c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 36543fb1823cSLai Jiangshan 365529c91e99STejun Heo return pool; 365629c91e99STejun Heo fail: 365729c91e99STejun Heo if (pool) 365829c91e99STejun Heo put_unbound_pool(pool); 365929c91e99STejun Heo return NULL; 366029c91e99STejun Heo } 366129c91e99STejun Heo 36628864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 36638864b4e5STejun Heo { 36648864b4e5STejun Heo kmem_cache_free(pwq_cache, 36658864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 36668864b4e5STejun Heo } 36678864b4e5STejun Heo 36688864b4e5STejun Heo /* 36698864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 36708864b4e5STejun Heo * and needs to be destroyed. 36718864b4e5STejun Heo */ 36728864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 36738864b4e5STejun Heo { 36748864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 36758864b4e5STejun Heo unbound_release_work); 36768864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 36778864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3678b42b0bddSYang Yingliang bool is_last = false; 36798864b4e5STejun Heo 3680b42b0bddSYang Yingliang /* 3681b42b0bddSYang Yingliang * when @pwq is not linked, it doesn't hold any reference to the 3682b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 3683b42b0bddSYang Yingliang */ 3684b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 36858864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 36868864b4e5STejun Heo return; 36878864b4e5STejun Heo 36883c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 36898864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3690bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 36913c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 3692b42b0bddSYang Yingliang } 36938864b4e5STejun Heo 3694a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 36958864b4e5STejun Heo put_unbound_pool(pool); 3696a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3697a892caccSTejun Heo 369825b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 36998864b4e5STejun Heo 37008864b4e5STejun Heo /* 37018864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3702e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 37038864b4e5STejun Heo */ 3704669de8bdSBart Van Assche if (is_last) { 3705669de8bdSBart Van Assche wq_unregister_lockdep(wq); 370625b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 37076029a918STejun Heo } 3708669de8bdSBart Van Assche } 37098864b4e5STejun Heo 37100fbd95aaSTejun Heo /** 3711699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 37120fbd95aaSTejun Heo * @pwq: target pool_workqueue 37130fbd95aaSTejun Heo * 3714699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3715*f97a4a1aSLai Jiangshan * workqueue's saved_max_active and activate inactive work items 3716699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 37170fbd95aaSTejun Heo */ 3718699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 37190fbd95aaSTejun Heo { 3720699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3721699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 37223347fa09STejun Heo unsigned long flags; 3723699ce097STejun Heo 3724699ce097STejun Heo /* for @wq->saved_max_active */ 3725a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3726699ce097STejun Heo 3727699ce097STejun Heo /* fast exit for non-freezable wqs */ 3728699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3729699ce097STejun Heo return; 3730699ce097STejun Heo 37313347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 3732a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 3733699ce097STejun Heo 373474b414eaSLai Jiangshan /* 373574b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 373674b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 373774b414eaSLai Jiangshan * is updated and visible. 373874b414eaSLai Jiangshan */ 373974b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 374001341fbdSYunfeng Ye bool kick = false; 374101341fbdSYunfeng Ye 3742699ce097STejun Heo pwq->max_active = wq->saved_max_active; 37430fbd95aaSTejun Heo 3744*f97a4a1aSLai Jiangshan while (!list_empty(&pwq->inactive_works) && 374501341fbdSYunfeng Ye pwq->nr_active < pwq->max_active) { 3746*f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 374701341fbdSYunfeng Ye kick = true; 374801341fbdSYunfeng Ye } 3749951a078aSLai Jiangshan 3750951a078aSLai Jiangshan /* 3751951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 375201341fbdSYunfeng Ye * max_active is bumped. In realtime scenarios, always kicking a 375301341fbdSYunfeng Ye * worker will cause interference on the isolated cpu cores, so 375401341fbdSYunfeng Ye * let's kick iff work items were activated. 3755951a078aSLai Jiangshan */ 375601341fbdSYunfeng Ye if (kick) 3757951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3758699ce097STejun Heo } else { 3759699ce097STejun Heo pwq->max_active = 0; 3760699ce097STejun Heo } 3761699ce097STejun Heo 3762a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 37630fbd95aaSTejun Heo } 37640fbd95aaSTejun Heo 376567dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 3766f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3767f147f29eSTejun Heo struct worker_pool *pool) 3768d2c1d404STejun Heo { 3769d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3770d2c1d404STejun Heo 3771e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3772e50aba9aSTejun Heo 3773d2c1d404STejun Heo pwq->pool = pool; 3774d2c1d404STejun Heo pwq->wq = wq; 3775d2c1d404STejun Heo pwq->flush_color = -1; 37768864b4e5STejun Heo pwq->refcnt = 1; 3777*f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 37781befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3779d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 37808864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3781f147f29eSTejun Heo } 3782d2c1d404STejun Heo 3783f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 37841befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3785f147f29eSTejun Heo { 3786f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3787f147f29eSTejun Heo 3788f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 378975ccf595STejun Heo 37901befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 37911befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 37921befcf30STejun Heo return; 37931befcf30STejun Heo 379429b1cb41SLai Jiangshan /* set the matching work_color */ 379575ccf595STejun Heo pwq->work_color = wq->work_color; 3796983ca25eSTejun Heo 3797983ca25eSTejun Heo /* sync max_active to the current setting */ 3798983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3799983ca25eSTejun Heo 3800983ca25eSTejun Heo /* link in @pwq */ 38019e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3802df2d5ae4STejun Heo } 38036029a918STejun Heo 3804f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3805f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3806f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3807f147f29eSTejun Heo { 3808f147f29eSTejun Heo struct worker_pool *pool; 3809f147f29eSTejun Heo struct pool_workqueue *pwq; 3810f147f29eSTejun Heo 3811f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3812f147f29eSTejun Heo 3813f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3814f147f29eSTejun Heo if (!pool) 3815f147f29eSTejun Heo return NULL; 3816f147f29eSTejun Heo 3817e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3818f147f29eSTejun Heo if (!pwq) { 3819f147f29eSTejun Heo put_unbound_pool(pool); 3820f147f29eSTejun Heo return NULL; 3821f147f29eSTejun Heo } 3822f147f29eSTejun Heo 3823f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3824f147f29eSTejun Heo return pwq; 3825d2c1d404STejun Heo } 3826d2c1d404STejun Heo 38274c16bd32STejun Heo /** 382830186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 3829042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 38304c16bd32STejun Heo * @node: the target NUMA node 38314c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 38324c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 38334c16bd32STejun Heo * 38344c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 38354c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3836d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 38374c16bd32STejun Heo * 38384c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 38394c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 38404c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 38414c16bd32STejun Heo * @attrs->cpumask. 38424c16bd32STejun Heo * 38434c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 38444c16bd32STejun Heo * stable. 3845d185af30SYacine Belkadi * 3846d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 3847d185af30SYacine Belkadi * %false if equal. 38484c16bd32STejun Heo */ 38494c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 38504c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 38514c16bd32STejun Heo { 3852d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 38534c16bd32STejun Heo goto use_dfl; 38544c16bd32STejun Heo 38554c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 38564c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 38574c16bd32STejun Heo if (cpu_going_down >= 0) 38584c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 38594c16bd32STejun Heo 38604c16bd32STejun Heo if (cpumask_empty(cpumask)) 38614c16bd32STejun Heo goto use_dfl; 38624c16bd32STejun Heo 38634c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 38644c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 38651ad0f0a7SMichael Bringmann 38661ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 38671ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 38681ad0f0a7SMichael Bringmann "possible intersect\n"); 38691ad0f0a7SMichael Bringmann return false; 38701ad0f0a7SMichael Bringmann } 38711ad0f0a7SMichael Bringmann 38724c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 38734c16bd32STejun Heo 38744c16bd32STejun Heo use_dfl: 38754c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 38764c16bd32STejun Heo return false; 38774c16bd32STejun Heo } 38784c16bd32STejun Heo 38791befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 38801befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 38811befcf30STejun Heo int node, 38821befcf30STejun Heo struct pool_workqueue *pwq) 38831befcf30STejun Heo { 38841befcf30STejun Heo struct pool_workqueue *old_pwq; 38851befcf30STejun Heo 38865b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 38871befcf30STejun Heo lockdep_assert_held(&wq->mutex); 38881befcf30STejun Heo 38891befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 38901befcf30STejun Heo link_pwq(pwq); 38911befcf30STejun Heo 38921befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 38931befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 38941befcf30STejun Heo return old_pwq; 38951befcf30STejun Heo } 38961befcf30STejun Heo 38972d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 38982d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 38992d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 39002d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 3901042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 39022d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 39032d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 39042d5f0764SLai Jiangshan }; 39052d5f0764SLai Jiangshan 39062d5f0764SLai Jiangshan /* free the resources after success or abort */ 39072d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 39082d5f0764SLai Jiangshan { 39092d5f0764SLai Jiangshan if (ctx) { 39102d5f0764SLai Jiangshan int node; 39112d5f0764SLai Jiangshan 39122d5f0764SLai Jiangshan for_each_node(node) 39132d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 39142d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 39152d5f0764SLai Jiangshan 39162d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 39172d5f0764SLai Jiangshan 39182d5f0764SLai Jiangshan kfree(ctx); 39192d5f0764SLai Jiangshan } 39202d5f0764SLai Jiangshan } 39212d5f0764SLai Jiangshan 39222d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 39232d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 39242d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 39252d5f0764SLai Jiangshan const struct workqueue_attrs *attrs) 39262d5f0764SLai Jiangshan { 39272d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 39282d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 39292d5f0764SLai Jiangshan int node; 39302d5f0764SLai Jiangshan 39312d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 39322d5f0764SLai Jiangshan 3933acafe7e3SKees Cook ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL); 39342d5f0764SLai Jiangshan 3935be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 3936be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 39372d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 39382d5f0764SLai Jiangshan goto out_free; 39392d5f0764SLai Jiangshan 3940042f7df1SLai Jiangshan /* 3941042f7df1SLai Jiangshan * Calculate the attrs of the default pwq. 3942042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 3943042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 3944042f7df1SLai Jiangshan */ 39452d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3946b05a7928SFrederic Weisbecker cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); 3947042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 3948042f7df1SLai Jiangshan cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); 39492d5f0764SLai Jiangshan 39502d5f0764SLai Jiangshan /* 39512d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 39522d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 39532d5f0764SLai Jiangshan * pools. 39542d5f0764SLai Jiangshan */ 39552d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 39562d5f0764SLai Jiangshan 39572d5f0764SLai Jiangshan /* 39582d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 39592d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 39602d5f0764SLai Jiangshan * it even if we don't use it immediately. 39612d5f0764SLai Jiangshan */ 39622d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 39632d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 39642d5f0764SLai Jiangshan goto out_free; 39652d5f0764SLai Jiangshan 39662d5f0764SLai Jiangshan for_each_node(node) { 3967042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 39682d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 39692d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 39702d5f0764SLai Jiangshan goto out_free; 39712d5f0764SLai Jiangshan } else { 39722d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 39732d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 39742d5f0764SLai Jiangshan } 39752d5f0764SLai Jiangshan } 39762d5f0764SLai Jiangshan 3977042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 3978042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3979042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 39802d5f0764SLai Jiangshan ctx->attrs = new_attrs; 3981042f7df1SLai Jiangshan 39822d5f0764SLai Jiangshan ctx->wq = wq; 39832d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 39842d5f0764SLai Jiangshan return ctx; 39852d5f0764SLai Jiangshan 39862d5f0764SLai Jiangshan out_free: 39872d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 39882d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 39892d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 39902d5f0764SLai Jiangshan return NULL; 39912d5f0764SLai Jiangshan } 39922d5f0764SLai Jiangshan 39932d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 39942d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 39952d5f0764SLai Jiangshan { 39962d5f0764SLai Jiangshan int node; 39972d5f0764SLai Jiangshan 39982d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 39992d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 40002d5f0764SLai Jiangshan 40012d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 40022d5f0764SLai Jiangshan 40032d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 40042d5f0764SLai Jiangshan for_each_node(node) 40052d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 40062d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 40072d5f0764SLai Jiangshan 40082d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 40092d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 40102d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 40112d5f0764SLai Jiangshan 40122d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 40132d5f0764SLai Jiangshan } 40142d5f0764SLai Jiangshan 4015a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4016a0111cf6SLai Jiangshan { 4017a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4018ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4019a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4020a0111cf6SLai Jiangshan } 4021a0111cf6SLai Jiangshan 4022a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4023a0111cf6SLai Jiangshan { 4024a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4025ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4026a0111cf6SLai Jiangshan } 4027a0111cf6SLai Jiangshan 4028a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4029a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4030a0111cf6SLai Jiangshan { 4031a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4032a0111cf6SLai Jiangshan 4033a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4034a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4035a0111cf6SLai Jiangshan return -EINVAL; 4036a0111cf6SLai Jiangshan 4037a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 40380a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 40390a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4040a0111cf6SLai Jiangshan return -EINVAL; 4041a0111cf6SLai Jiangshan 40420a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 40430a94efb5STejun Heo } 40440a94efb5STejun Heo 4045a0111cf6SLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs); 40466201171eSwanghaibin if (!ctx) 40476201171eSwanghaibin return -ENOMEM; 4048a0111cf6SLai Jiangshan 4049a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4050a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4051a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4052a0111cf6SLai Jiangshan 40536201171eSwanghaibin return 0; 4054a0111cf6SLai Jiangshan } 4055a0111cf6SLai Jiangshan 40569e8cd2f5STejun Heo /** 40579e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 40589e8cd2f5STejun Heo * @wq: the target workqueue 40599e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 40609e8cd2f5STejun Heo * 40614c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 40624c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 40634c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 40644c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 40654c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 40664c16bd32STejun Heo * back-to-back will stay on its current pwq. 40679e8cd2f5STejun Heo * 4068d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4069d185af30SYacine Belkadi * 4070ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4071509b3204SDaniel Jordan * 4072d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 40739e8cd2f5STejun Heo */ 4074513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 40759e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 40769e8cd2f5STejun Heo { 4077a0111cf6SLai Jiangshan int ret; 40789e8cd2f5STejun Heo 4079509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4080509b3204SDaniel Jordan 4081509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4082a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4083509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 40842d5f0764SLai Jiangshan 40852d5f0764SLai Jiangshan return ret; 40869e8cd2f5STejun Heo } 40879e8cd2f5STejun Heo 40884c16bd32STejun Heo /** 40894c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 40904c16bd32STejun Heo * @wq: the target workqueue 40914c16bd32STejun Heo * @cpu: the CPU coming up or going down 40924c16bd32STejun Heo * @online: whether @cpu is coming up or going down 40934c16bd32STejun Heo * 40944c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 40954c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 40964c16bd32STejun Heo * @wq accordingly. 40974c16bd32STejun Heo * 40984c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 40994c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 41004c16bd32STejun Heo * correct. 41014c16bd32STejun Heo * 41024c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 41034c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 41044c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 41054c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 41064c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 41074c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 41084c16bd32STejun Heo * CPU_DOWN_PREPARE. 41094c16bd32STejun Heo */ 41104c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 41114c16bd32STejun Heo bool online) 41124c16bd32STejun Heo { 41134c16bd32STejun Heo int node = cpu_to_node(cpu); 41144c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 41154c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 41164c16bd32STejun Heo struct workqueue_attrs *target_attrs; 41174c16bd32STejun Heo cpumask_t *cpumask; 41184c16bd32STejun Heo 41194c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 41204c16bd32STejun Heo 4121f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 4122f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 41234c16bd32STejun Heo return; 41244c16bd32STejun Heo 41254c16bd32STejun Heo /* 41264c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 41274c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 41284c16bd32STejun Heo * CPU hotplug exclusion. 41294c16bd32STejun Heo */ 41304c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 41314c16bd32STejun Heo cpumask = target_attrs->cpumask; 41324c16bd32STejun Heo 41334c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 41344c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 41354c16bd32STejun Heo 41364c16bd32STejun Heo /* 41374c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 4138042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 4139042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 4140042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 41414c16bd32STejun Heo */ 4142042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 41434c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4144f7142ed4SLai Jiangshan return; 41454c16bd32STejun Heo } else { 41464c16bd32STejun Heo goto use_dfl_pwq; 41474c16bd32STejun Heo } 41484c16bd32STejun Heo 41494c16bd32STejun Heo /* create a new pwq */ 41504c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 41514c16bd32STejun Heo if (!pwq) { 41522d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 41534c16bd32STejun Heo wq->name); 415477f300b1SDaeseok Youn goto use_dfl_pwq; 41554c16bd32STejun Heo } 41564c16bd32STejun Heo 4157f7142ed4SLai Jiangshan /* Install the new pwq. */ 41584c16bd32STejun Heo mutex_lock(&wq->mutex); 41594c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 41604c16bd32STejun Heo goto out_unlock; 41614c16bd32STejun Heo 41624c16bd32STejun Heo use_dfl_pwq: 4163f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4164a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 41654c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4166a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 41674c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 41684c16bd32STejun Heo out_unlock: 41694c16bd32STejun Heo mutex_unlock(&wq->mutex); 41704c16bd32STejun Heo put_pwq_unlocked(old_pwq); 41714c16bd32STejun Heo } 41724c16bd32STejun Heo 417330cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 41741da177e4SLinus Torvalds { 417549e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 41768a2b7538STejun Heo int cpu, ret; 4177e1d8aa9fSFrederic Weisbecker 417830cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4179420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 4180420c0ddbSTejun Heo if (!wq->cpu_pwqs) 418130cdf249STejun Heo return -ENOMEM; 418230cdf249STejun Heo 418330cdf249STejun Heo for_each_possible_cpu(cpu) { 41847fb98ea7STejun Heo struct pool_workqueue *pwq = 41857fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 41867a62c2c8STejun Heo struct worker_pool *cpu_pools = 4187f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 418830cdf249STejun Heo 4189f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 4190f147f29eSTejun Heo 4191f147f29eSTejun Heo mutex_lock(&wq->mutex); 41921befcf30STejun Heo link_pwq(pwq); 4193f147f29eSTejun Heo mutex_unlock(&wq->mutex); 419430cdf249STejun Heo } 419530cdf249STejun Heo return 0; 4196509b3204SDaniel Jordan } 4197509b3204SDaniel Jordan 4198ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4199509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 42008a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 42018a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 42028a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 42038a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 42048a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 42059e8cd2f5STejun Heo } else { 4206509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 42079e8cd2f5STejun Heo } 4208ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4209509b3204SDaniel Jordan 4210509b3204SDaniel Jordan return ret; 42110f900049STejun Heo } 42120f900049STejun Heo 4213f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4214f3421797STejun Heo const char *name) 4215b71ab8c2STejun Heo { 4216f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 4217f3421797STejun Heo 4218f3421797STejun Heo if (max_active < 1 || max_active > lim) 4219044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4220f3421797STejun Heo max_active, name, 1, lim); 4221b71ab8c2STejun Heo 4222f3421797STejun Heo return clamp_val(max_active, 1, lim); 4223b71ab8c2STejun Heo } 4224b71ab8c2STejun Heo 4225983c7515STejun Heo /* 4226983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4227983c7515STejun Heo * to guarantee forward progress. 4228983c7515STejun Heo */ 4229983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4230983c7515STejun Heo { 4231983c7515STejun Heo struct worker *rescuer; 4232b92b36eaSDan Carpenter int ret; 4233983c7515STejun Heo 4234983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4235983c7515STejun Heo return 0; 4236983c7515STejun Heo 4237983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 4238983c7515STejun Heo if (!rescuer) 4239983c7515STejun Heo return -ENOMEM; 4240983c7515STejun Heo 4241983c7515STejun Heo rescuer->rescue_wq = wq; 4242983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4243f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4244b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 4245983c7515STejun Heo kfree(rescuer); 4246b92b36eaSDan Carpenter return ret; 4247983c7515STejun Heo } 4248983c7515STejun Heo 4249983c7515STejun Heo wq->rescuer = rescuer; 4250983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4251983c7515STejun Heo wake_up_process(rescuer->task); 4252983c7515STejun Heo 4253983c7515STejun Heo return 0; 4254983c7515STejun Heo } 4255983c7515STejun Heo 4256a2775bbcSMathieu Malaterre __printf(1, 4) 4257669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 425897e37d7bSTejun Heo unsigned int flags, 4259669de8bdSBart Van Assche int max_active, ...) 42603af24433SOleg Nesterov { 4261df2d5ae4STejun Heo size_t tbl_size = 0; 4262ecf6881fSTejun Heo va_list args; 42633af24433SOleg Nesterov struct workqueue_struct *wq; 426449e3cf44STejun Heo struct pool_workqueue *pwq; 4265b196be89STejun Heo 42665c0338c6STejun Heo /* 42675c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 42685c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 42695c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 42705c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 42715c0338c6STejun Heo * on NUMA. 42725c0338c6STejun Heo */ 42735c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 42745c0338c6STejun Heo flags |= __WQ_ORDERED; 42755c0338c6STejun Heo 4276cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4277cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4278cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4279cee22a15SViresh Kumar 4280ecf6881fSTejun Heo /* allocate wq and format name */ 4281df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 4282ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 4283df2d5ae4STejun Heo 4284df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 4285b196be89STejun Heo if (!wq) 4286d2c1d404STejun Heo return NULL; 4287b196be89STejun Heo 42886029a918STejun Heo if (flags & WQ_UNBOUND) { 4289be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 42906029a918STejun Heo if (!wq->unbound_attrs) 42916029a918STejun Heo goto err_free_wq; 42926029a918STejun Heo } 42936029a918STejun Heo 4294669de8bdSBart Van Assche va_start(args, max_active); 4295ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4296b196be89STejun Heo va_end(args); 42973af24433SOleg Nesterov 4298d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4299b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 43003af24433SOleg Nesterov 4301b196be89STejun Heo /* init wq */ 430297e37d7bSTejun Heo wq->flags = flags; 4303a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 43043c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4305112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 430630cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 430773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 430873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4309493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 43103af24433SOleg Nesterov 4311669de8bdSBart Van Assche wq_init_lockdep(wq); 4312cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 43133af24433SOleg Nesterov 431430cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 431582efcab3SBart Van Assche goto err_unreg_lockdep; 43161537663fSTejun Heo 431740c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4318d2c1d404STejun Heo goto err_destroy; 4319e22bee78STejun Heo 4320226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4321226223abSTejun Heo goto err_destroy; 4322226223abSTejun Heo 43236af8bf3dSOleg Nesterov /* 432468e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 432568e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 432668e13a67SLai Jiangshan * list. 43276af8bf3dSOleg Nesterov */ 432868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4329a0a1a5fdSTejun Heo 4330a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 433149e3cf44STejun Heo for_each_pwq(pwq, wq) 4332699ce097STejun Heo pwq_adjust_max_active(pwq); 4333a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4334a0a1a5fdSTejun Heo 4335e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4336a0a1a5fdSTejun Heo 433768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 43383af24433SOleg Nesterov 43393af24433SOleg Nesterov return wq; 4340d2c1d404STejun Heo 434182efcab3SBart Van Assche err_unreg_lockdep: 4342009bb421SBart Van Assche wq_unregister_lockdep(wq); 4343009bb421SBart Van Assche wq_free_lockdep(wq); 434482efcab3SBart Van Assche err_free_wq: 43456029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 43464690c4abSTejun Heo kfree(wq); 4347d2c1d404STejun Heo return NULL; 4348d2c1d404STejun Heo err_destroy: 4349d2c1d404STejun Heo destroy_workqueue(wq); 43504690c4abSTejun Heo return NULL; 43511da177e4SLinus Torvalds } 4352669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 43531da177e4SLinus Torvalds 4354c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4355c29eb853STejun Heo { 4356c29eb853STejun Heo int i; 4357c29eb853STejun Heo 4358c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4359c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4360c29eb853STejun Heo return true; 4361c29eb853STejun Heo 4362c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4363c29eb853STejun Heo return true; 4364*f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 4365c29eb853STejun Heo return true; 4366c29eb853STejun Heo 4367c29eb853STejun Heo return false; 4368c29eb853STejun Heo } 4369c29eb853STejun Heo 43703af24433SOleg Nesterov /** 43713af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 43723af24433SOleg Nesterov * @wq: target workqueue 43733af24433SOleg Nesterov * 43743af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 43753af24433SOleg Nesterov */ 43763af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 43773af24433SOleg Nesterov { 437849e3cf44STejun Heo struct pool_workqueue *pwq; 43794c16bd32STejun Heo int node; 43803af24433SOleg Nesterov 4381def98c84STejun Heo /* 4382def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4383def98c84STejun Heo * lead to sysfs name conflicts. 4384def98c84STejun Heo */ 4385def98c84STejun Heo workqueue_sysfs_unregister(wq); 4386def98c84STejun Heo 43879c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 43889c5a2ba7STejun Heo drain_workqueue(wq); 4389c8efcc25STejun Heo 4390def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4391def98c84STejun Heo if (wq->rescuer) { 4392def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4393def98c84STejun Heo 4394def98c84STejun Heo /* this prevents new queueing */ 4395a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4396def98c84STejun Heo wq->rescuer = NULL; 4397a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4398def98c84STejun Heo 4399def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4400def98c84STejun Heo kthread_stop(rescuer->task); 44018efe1223STejun Heo kfree(rescuer); 4402def98c84STejun Heo } 4403def98c84STejun Heo 4404c29eb853STejun Heo /* 4405c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4406c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4407c29eb853STejun Heo */ 4408c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4409b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 441049e3cf44STejun Heo for_each_pwq(pwq, wq) { 4411a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4412c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 44131d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4414e66b39afSTejun Heo __func__, wq->name); 4415c29eb853STejun Heo show_pwq(pwq); 4416a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4417b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4418c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 4419fa07fb6aSTejun Heo show_workqueue_state(); 44206183c009STejun Heo return; 442176af4d93STejun Heo } 4422a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 442376af4d93STejun Heo } 4424b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 44256183c009STejun Heo 4426a0a1a5fdSTejun Heo /* 4427a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4428a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4429a0a1a5fdSTejun Heo */ 4430e2dca7adSTejun Heo list_del_rcu(&wq->list); 443168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 44323af24433SOleg Nesterov 44338864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4434669de8bdSBart Van Assche wq_unregister_lockdep(wq); 443529c91e99STejun Heo /* 44368864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4437e2dca7adSTejun Heo * schedule RCU free. 443829c91e99STejun Heo */ 443925b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 44408864b4e5STejun Heo } else { 44418864b4e5STejun Heo /* 44428864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 44434c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 44444c16bd32STejun Heo * @wq will be freed when the last pwq is released. 44458864b4e5STejun Heo */ 44464c16bd32STejun Heo for_each_node(node) { 44474c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 44484c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 44494c16bd32STejun Heo put_pwq_unlocked(pwq); 44504c16bd32STejun Heo } 44514c16bd32STejun Heo 44524c16bd32STejun Heo /* 44534c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 44544c16bd32STejun Heo * put. Don't access it afterwards. 44554c16bd32STejun Heo */ 44564c16bd32STejun Heo pwq = wq->dfl_pwq; 44574c16bd32STejun Heo wq->dfl_pwq = NULL; 4458dce90d47STejun Heo put_pwq_unlocked(pwq); 445929c91e99STejun Heo } 44603af24433SOleg Nesterov } 44613af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 44623af24433SOleg Nesterov 4463dcd989cbSTejun Heo /** 4464dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4465dcd989cbSTejun Heo * @wq: target workqueue 4466dcd989cbSTejun Heo * @max_active: new max_active value. 4467dcd989cbSTejun Heo * 4468dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4469dcd989cbSTejun Heo * 4470dcd989cbSTejun Heo * CONTEXT: 4471dcd989cbSTejun Heo * Don't call from IRQ context. 4472dcd989cbSTejun Heo */ 4473dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4474dcd989cbSTejun Heo { 447549e3cf44STejun Heo struct pool_workqueue *pwq; 4476dcd989cbSTejun Heo 44778719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 44780a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 44798719dceaSTejun Heo return; 44808719dceaSTejun Heo 4481f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4482dcd989cbSTejun Heo 4483a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4484dcd989cbSTejun Heo 44850a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4486dcd989cbSTejun Heo wq->saved_max_active = max_active; 4487dcd989cbSTejun Heo 4488699ce097STejun Heo for_each_pwq(pwq, wq) 4489699ce097STejun Heo pwq_adjust_max_active(pwq); 4490dcd989cbSTejun Heo 4491a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4492dcd989cbSTejun Heo } 4493dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4494dcd989cbSTejun Heo 4495dcd989cbSTejun Heo /** 449627d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 449727d4ee03SLukas Wunner * 449827d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 449927d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 450027d4ee03SLukas Wunner * 450127d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 450227d4ee03SLukas Wunner */ 450327d4ee03SLukas Wunner struct work_struct *current_work(void) 450427d4ee03SLukas Wunner { 450527d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 450627d4ee03SLukas Wunner 450727d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 450827d4ee03SLukas Wunner } 450927d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 451027d4ee03SLukas Wunner 451127d4ee03SLukas Wunner /** 4512e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4513e6267616STejun Heo * 4514e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4515e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4516d185af30SYacine Belkadi * 4517d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4518e6267616STejun Heo */ 4519e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4520e6267616STejun Heo { 4521e6267616STejun Heo struct worker *worker = current_wq_worker(); 4522e6267616STejun Heo 45236a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4524e6267616STejun Heo } 4525e6267616STejun Heo 4526e6267616STejun Heo /** 4527dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4528dcd989cbSTejun Heo * @cpu: CPU in question 4529dcd989cbSTejun Heo * @wq: target workqueue 4530dcd989cbSTejun Heo * 4531dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4532dcd989cbSTejun Heo * no synchronization around this function and the test result is 4533dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4534dcd989cbSTejun Heo * 4535d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4536d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4537d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4538d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4539d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4540d3251859STejun Heo * 4541d185af30SYacine Belkadi * Return: 4542dcd989cbSTejun Heo * %true if congested, %false otherwise. 4543dcd989cbSTejun Heo */ 4544d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4545dcd989cbSTejun Heo { 45467fb98ea7STejun Heo struct pool_workqueue *pwq; 454776af4d93STejun Heo bool ret; 454876af4d93STejun Heo 454924acfb71SThomas Gleixner rcu_read_lock(); 455024acfb71SThomas Gleixner preempt_disable(); 45517fb98ea7STejun Heo 4552d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4553d3251859STejun Heo cpu = smp_processor_id(); 4554d3251859STejun Heo 45557fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 45567fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 45577fb98ea7STejun Heo else 4558df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4559dcd989cbSTejun Heo 4560*f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 456124acfb71SThomas Gleixner preempt_enable(); 456224acfb71SThomas Gleixner rcu_read_unlock(); 456376af4d93STejun Heo 456476af4d93STejun Heo return ret; 4565dcd989cbSTejun Heo } 4566dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4567dcd989cbSTejun Heo 4568dcd989cbSTejun Heo /** 4569dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4570dcd989cbSTejun Heo * @work: the work to be tested 4571dcd989cbSTejun Heo * 4572dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4573dcd989cbSTejun Heo * synchronization around this function and the test result is 4574dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4575dcd989cbSTejun Heo * 4576d185af30SYacine Belkadi * Return: 4577dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4578dcd989cbSTejun Heo */ 4579dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4580dcd989cbSTejun Heo { 4581fa1b54e6STejun Heo struct worker_pool *pool; 4582dcd989cbSTejun Heo unsigned long flags; 4583dcd989cbSTejun Heo unsigned int ret = 0; 4584dcd989cbSTejun Heo 4585dcd989cbSTejun Heo if (work_pending(work)) 4586dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4587038366c5SLai Jiangshan 458824acfb71SThomas Gleixner rcu_read_lock(); 4589fa1b54e6STejun Heo pool = get_work_pool(work); 4590038366c5SLai Jiangshan if (pool) { 4591a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 4592c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4593dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4594a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 4595038366c5SLai Jiangshan } 459624acfb71SThomas Gleixner rcu_read_unlock(); 4597dcd989cbSTejun Heo 4598dcd989cbSTejun Heo return ret; 4599dcd989cbSTejun Heo } 4600dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4601dcd989cbSTejun Heo 46023d1cb205STejun Heo /** 46033d1cb205STejun Heo * set_worker_desc - set description for the current work item 46043d1cb205STejun Heo * @fmt: printf-style format string 46053d1cb205STejun Heo * @...: arguments for the format string 46063d1cb205STejun Heo * 46073d1cb205STejun Heo * This function can be called by a running work function to describe what 46083d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 46093d1cb205STejun Heo * information will be printed out together to help debugging. The 46103d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 46113d1cb205STejun Heo */ 46123d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 46133d1cb205STejun Heo { 46143d1cb205STejun Heo struct worker *worker = current_wq_worker(); 46153d1cb205STejun Heo va_list args; 46163d1cb205STejun Heo 46173d1cb205STejun Heo if (worker) { 46183d1cb205STejun Heo va_start(args, fmt); 46193d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 46203d1cb205STejun Heo va_end(args); 46213d1cb205STejun Heo } 46223d1cb205STejun Heo } 46235c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 46243d1cb205STejun Heo 46253d1cb205STejun Heo /** 46263d1cb205STejun Heo * print_worker_info - print out worker information and description 46273d1cb205STejun Heo * @log_lvl: the log level to use when printing 46283d1cb205STejun Heo * @task: target task 46293d1cb205STejun Heo * 46303d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 46313d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 46323d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 46333d1cb205STejun Heo * 46343d1cb205STejun Heo * This function can be safely called on any task as long as the 46353d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 46363d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 46373d1cb205STejun Heo */ 46383d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 46393d1cb205STejun Heo { 46403d1cb205STejun Heo work_func_t *fn = NULL; 46413d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 46423d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 46433d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 46443d1cb205STejun Heo struct workqueue_struct *wq = NULL; 46453d1cb205STejun Heo struct worker *worker; 46463d1cb205STejun Heo 46473d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 46483d1cb205STejun Heo return; 46493d1cb205STejun Heo 46503d1cb205STejun Heo /* 46513d1cb205STejun Heo * This function is called without any synchronization and @task 46523d1cb205STejun Heo * could be in any state. Be careful with dereferences. 46533d1cb205STejun Heo */ 4654e700591aSPetr Mladek worker = kthread_probe_data(task); 46553d1cb205STejun Heo 46563d1cb205STejun Heo /* 46578bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 46588bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 46593d1cb205STejun Heo */ 4660fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 4661fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 4662fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 4663fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 4664fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 46653d1cb205STejun Heo 46663d1cb205STejun Heo if (fn || name[0] || desc[0]) { 4667d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 46688bf89593STejun Heo if (strcmp(name, desc)) 46693d1cb205STejun Heo pr_cont(" (%s)", desc); 46703d1cb205STejun Heo pr_cont("\n"); 46713d1cb205STejun Heo } 46723d1cb205STejun Heo } 46733d1cb205STejun Heo 46743494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 46753494fc30STejun Heo { 46763494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 46773494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 46783494fc30STejun Heo pr_cont(" node=%d", pool->node); 46793494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 46803494fc30STejun Heo } 46813494fc30STejun Heo 46823494fc30STejun Heo static void pr_cont_work(bool comma, struct work_struct *work) 46833494fc30STejun Heo { 46843494fc30STejun Heo if (work->func == wq_barrier_func) { 46853494fc30STejun Heo struct wq_barrier *barr; 46863494fc30STejun Heo 46873494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 46883494fc30STejun Heo 46893494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 46903494fc30STejun Heo task_pid_nr(barr->task)); 46913494fc30STejun Heo } else { 4692d75f773cSSakari Ailus pr_cont("%s %ps", comma ? "," : "", work->func); 46933494fc30STejun Heo } 46943494fc30STejun Heo } 46953494fc30STejun Heo 46963494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 46973494fc30STejun Heo { 46983494fc30STejun Heo struct worker_pool *pool = pwq->pool; 46993494fc30STejun Heo struct work_struct *work; 47003494fc30STejun Heo struct worker *worker; 47013494fc30STejun Heo bool has_in_flight = false, has_pending = false; 47023494fc30STejun Heo int bkt; 47033494fc30STejun Heo 47043494fc30STejun Heo pr_info(" pwq %d:", pool->id); 47053494fc30STejun Heo pr_cont_pool_info(pool); 47063494fc30STejun Heo 4707e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 4708e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 47093494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 47103494fc30STejun Heo 47113494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 47123494fc30STejun Heo if (worker->current_pwq == pwq) { 47133494fc30STejun Heo has_in_flight = true; 47143494fc30STejun Heo break; 47153494fc30STejun Heo } 47163494fc30STejun Heo } 47173494fc30STejun Heo if (has_in_flight) { 47183494fc30STejun Heo bool comma = false; 47193494fc30STejun Heo 47203494fc30STejun Heo pr_info(" in-flight:"); 47213494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 47223494fc30STejun Heo if (worker->current_pwq != pwq) 47233494fc30STejun Heo continue; 47243494fc30STejun Heo 4725d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 47263494fc30STejun Heo task_pid_nr(worker->task), 472730ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 47283494fc30STejun Heo worker->current_func); 47293494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 47303494fc30STejun Heo pr_cont_work(false, work); 47313494fc30STejun Heo comma = true; 47323494fc30STejun Heo } 47333494fc30STejun Heo pr_cont("\n"); 47343494fc30STejun Heo } 47353494fc30STejun Heo 47363494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 47373494fc30STejun Heo if (get_work_pwq(work) == pwq) { 47383494fc30STejun Heo has_pending = true; 47393494fc30STejun Heo break; 47403494fc30STejun Heo } 47413494fc30STejun Heo } 47423494fc30STejun Heo if (has_pending) { 47433494fc30STejun Heo bool comma = false; 47443494fc30STejun Heo 47453494fc30STejun Heo pr_info(" pending:"); 47463494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 47473494fc30STejun Heo if (get_work_pwq(work) != pwq) 47483494fc30STejun Heo continue; 47493494fc30STejun Heo 47503494fc30STejun Heo pr_cont_work(comma, work); 47513494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 47523494fc30STejun Heo } 47533494fc30STejun Heo pr_cont("\n"); 47543494fc30STejun Heo } 47553494fc30STejun Heo 4756*f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 47573494fc30STejun Heo bool comma = false; 47583494fc30STejun Heo 4759*f97a4a1aSLai Jiangshan pr_info(" inactive:"); 4760*f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 47613494fc30STejun Heo pr_cont_work(comma, work); 47623494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 47633494fc30STejun Heo } 47643494fc30STejun Heo pr_cont("\n"); 47653494fc30STejun Heo } 47663494fc30STejun Heo } 47673494fc30STejun Heo 47683494fc30STejun Heo /** 47693494fc30STejun Heo * show_workqueue_state - dump workqueue state 47703494fc30STejun Heo * 47717b776af6SRoger Lu * Called from a sysrq handler or try_to_freeze_tasks() and prints out 47727b776af6SRoger Lu * all busy workqueues and pools. 47733494fc30STejun Heo */ 47743494fc30STejun Heo void show_workqueue_state(void) 47753494fc30STejun Heo { 47763494fc30STejun Heo struct workqueue_struct *wq; 47773494fc30STejun Heo struct worker_pool *pool; 47783494fc30STejun Heo unsigned long flags; 47793494fc30STejun Heo int pi; 47803494fc30STejun Heo 478124acfb71SThomas Gleixner rcu_read_lock(); 47823494fc30STejun Heo 47833494fc30STejun Heo pr_info("Showing busy workqueues and worker pools:\n"); 47843494fc30STejun Heo 47853494fc30STejun Heo list_for_each_entry_rcu(wq, &workqueues, list) { 47863494fc30STejun Heo struct pool_workqueue *pwq; 47873494fc30STejun Heo bool idle = true; 47883494fc30STejun Heo 47893494fc30STejun Heo for_each_pwq(pwq, wq) { 4790*f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 47913494fc30STejun Heo idle = false; 47923494fc30STejun Heo break; 47933494fc30STejun Heo } 47943494fc30STejun Heo } 47953494fc30STejun Heo if (idle) 47963494fc30STejun Heo continue; 47973494fc30STejun Heo 47983494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 47993494fc30STejun Heo 48003494fc30STejun Heo for_each_pwq(pwq, wq) { 4801a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 4802*f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 48033494fc30STejun Heo show_pwq(pwq); 4804a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 480562635ea8SSergey Senozhatsky /* 480662635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 480762635ea8SSergey Senozhatsky * sysrq-t -> show_workqueue_state(). Avoid triggering 480862635ea8SSergey Senozhatsky * hard lockup. 480962635ea8SSergey Senozhatsky */ 481062635ea8SSergey Senozhatsky touch_nmi_watchdog(); 48113494fc30STejun Heo } 48123494fc30STejun Heo } 48133494fc30STejun Heo 48143494fc30STejun Heo for_each_pool(pool, pi) { 48153494fc30STejun Heo struct worker *worker; 48163494fc30STejun Heo bool first = true; 48173494fc30STejun Heo 4818a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 48193494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 48203494fc30STejun Heo goto next_pool; 48213494fc30STejun Heo 48223494fc30STejun Heo pr_info("pool %d:", pool->id); 48233494fc30STejun Heo pr_cont_pool_info(pool); 482482607adcSTejun Heo pr_cont(" hung=%us workers=%d", 482582607adcSTejun Heo jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000, 482682607adcSTejun Heo pool->nr_workers); 48273494fc30STejun Heo if (pool->manager) 48283494fc30STejun Heo pr_cont(" manager: %d", 48293494fc30STejun Heo task_pid_nr(pool->manager->task)); 48303494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 48313494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 48323494fc30STejun Heo task_pid_nr(worker->task)); 48333494fc30STejun Heo first = false; 48343494fc30STejun Heo } 48353494fc30STejun Heo pr_cont("\n"); 48363494fc30STejun Heo next_pool: 4837a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 483862635ea8SSergey Senozhatsky /* 483962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 484062635ea8SSergey Senozhatsky * sysrq-t -> show_workqueue_state(). Avoid triggering 484162635ea8SSergey Senozhatsky * hard lockup. 484262635ea8SSergey Senozhatsky */ 484362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 48443494fc30STejun Heo } 48453494fc30STejun Heo 484624acfb71SThomas Gleixner rcu_read_unlock(); 48473494fc30STejun Heo } 48483494fc30STejun Heo 48496b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 48506b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 48516b59808bSTejun Heo { 48526b59808bSTejun Heo int off; 48536b59808bSTejun Heo 48546b59808bSTejun Heo /* always show the actual comm */ 48556b59808bSTejun Heo off = strscpy(buf, task->comm, size); 48566b59808bSTejun Heo if (off < 0) 48576b59808bSTejun Heo return; 48586b59808bSTejun Heo 4859197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 48606b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 48616b59808bSTejun Heo 4862197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 4863197f6accSTejun Heo struct worker *worker = kthread_data(task); 4864197f6accSTejun Heo struct worker_pool *pool = worker->pool; 48656b59808bSTejun Heo 48666b59808bSTejun Heo if (pool) { 4867a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 48686b59808bSTejun Heo /* 4869197f6accSTejun Heo * ->desc tracks information (wq name or 4870197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 4871197f6accSTejun Heo * current, prepend '+', otherwise '-'. 48726b59808bSTejun Heo */ 48736b59808bSTejun Heo if (worker->desc[0] != '\0') { 48746b59808bSTejun Heo if (worker->current_work) 48756b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 48766b59808bSTejun Heo worker->desc); 48776b59808bSTejun Heo else 48786b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 48796b59808bSTejun Heo worker->desc); 48806b59808bSTejun Heo } 4881a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 48826b59808bSTejun Heo } 4883197f6accSTejun Heo } 48846b59808bSTejun Heo 48856b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 48866b59808bSTejun Heo } 48876b59808bSTejun Heo 488866448bc2SMathieu Malaterre #ifdef CONFIG_SMP 488966448bc2SMathieu Malaterre 4890db7bccf4STejun Heo /* 4891db7bccf4STejun Heo * CPU hotplug. 4892db7bccf4STejun Heo * 4893e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 4894112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 4895706026c2STejun Heo * pool which make migrating pending and scheduled works very 4896e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 489794cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 4898e22bee78STejun Heo * blocked draining impractical. 4899e22bee78STejun Heo * 490024647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 4901628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 4902628c78e7STejun Heo * cpu comes back online. 4903db7bccf4STejun Heo */ 4904db7bccf4STejun Heo 4905e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 4906db7bccf4STejun Heo { 49074ce62e9eSTejun Heo struct worker_pool *pool; 4908db7bccf4STejun Heo struct worker *worker; 4909db7bccf4STejun Heo 4910f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 49111258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 4912a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 4913e22bee78STejun Heo 4914f2d5a0eeSTejun Heo /* 491592f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 491694cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 491794cf58bbSTejun Heo * except for the ones which are still executing works from 491894cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 491994cf58bbSTejun Heo * this, they may become diasporas. 4920f2d5a0eeSTejun Heo */ 4921da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4922403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 4923db7bccf4STejun Heo 492424647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 4925f2d5a0eeSTejun Heo 4926a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 492706249738SPeter Zijlstra 49285c25b5ffSPeter Zijlstra for_each_pool_worker(worker, pool) { 49295c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 4930547a77d0SLai Jiangshan WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 49315c25b5ffSPeter Zijlstra } 493206249738SPeter Zijlstra 49331258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 4934e22bee78STejun Heo 4935e22bee78STejun Heo /* 4936eb283428SLai Jiangshan * Call schedule() so that we cross rq->lock and thus can 4937eb283428SLai Jiangshan * guarantee sched callbacks see the %WORKER_UNBOUND flag. 4938eb283428SLai Jiangshan * This is necessary as scheduler callbacks may be invoked 4939eb283428SLai Jiangshan * from other cpus. 4940628c78e7STejun Heo */ 4941628c78e7STejun Heo schedule(); 4942628c78e7STejun Heo 4943628c78e7STejun Heo /* 4944eb283428SLai Jiangshan * Sched callbacks are disabled now. Zap nr_running. 4945eb283428SLai Jiangshan * After this, nr_running stays zero and need_more_worker() 4946eb283428SLai Jiangshan * and keep_working() are always true as long as the 4947eb283428SLai Jiangshan * worklist is not empty. This pool now behaves as an 4948eb283428SLai Jiangshan * unbound (in terms of concurrency management) pool which 4949eb283428SLai Jiangshan * are served by workers tied to the pool. 4950e22bee78STejun Heo */ 4951e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 4952eb283428SLai Jiangshan 4953eb283428SLai Jiangshan /* 4954eb283428SLai Jiangshan * With concurrency management just turned off, a busy 4955eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 4956eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 4957eb283428SLai Jiangshan */ 4958a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 4959eb283428SLai Jiangshan wake_up_worker(pool); 4960a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4961eb283428SLai Jiangshan } 4962db7bccf4STejun Heo } 4963db7bccf4STejun Heo 4964bd7c089eSTejun Heo /** 4965bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 4966bd7c089eSTejun Heo * @pool: pool of interest 4967bd7c089eSTejun Heo * 4968a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 4969bd7c089eSTejun Heo */ 4970bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 4971bd7c089eSTejun Heo { 4972a9ab775bSTejun Heo struct worker *worker; 4973bd7c089eSTejun Heo 49741258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 4975bd7c089eSTejun Heo 4976bd7c089eSTejun Heo /* 4977a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 4978a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 4979402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 4980a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 4981a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 4982bd7c089eSTejun Heo */ 49835c25b5ffSPeter Zijlstra for_each_pool_worker(worker, pool) { 49845c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 4985a9ab775bSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 4986a9ab775bSTejun Heo pool->attrs->cpumask) < 0); 49875c25b5ffSPeter Zijlstra } 4988a9ab775bSTejun Heo 4989a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 4990f7c17d26SWanpeng Li 49913de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 4992a9ab775bSTejun Heo 4993da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 4994a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 4995a9ab775bSTejun Heo 4996a9ab775bSTejun Heo /* 4997a9ab775bSTejun Heo * A bound idle worker should actually be on the runqueue 4998a9ab775bSTejun Heo * of the associated CPU for local wake-ups targeting it to 4999a9ab775bSTejun Heo * work. Kick all idle workers so that they migrate to the 5000a9ab775bSTejun Heo * associated CPU. Doing this in the same loop as 5001a9ab775bSTejun Heo * replacing UNBOUND with REBOUND is safe as no worker will 5002a9ab775bSTejun Heo * be bound before @pool->lock is released. 5003a9ab775bSTejun Heo */ 5004a9ab775bSTejun Heo if (worker_flags & WORKER_IDLE) 5005bd7c089eSTejun Heo wake_up_process(worker->task); 5006bd7c089eSTejun Heo 5007bd7c089eSTejun Heo /* 5008a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5009a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5010a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5011a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5012a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5013a9ab775bSTejun Heo * concurrency management. Note that when or whether 5014a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5015a9ab775bSTejun Heo * 5016c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5017a9ab775bSTejun Heo * tested without holding any lock in 50186d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5019a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5020a9ab775bSTejun Heo * management operations. 5021bd7c089eSTejun Heo */ 5022a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5023a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5024a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5025c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5026bd7c089eSTejun Heo } 5027a9ab775bSTejun Heo 5028a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5029bd7c089eSTejun Heo } 5030bd7c089eSTejun Heo 50317dbc725eSTejun Heo /** 50327dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 50337dbc725eSTejun Heo * @pool: unbound pool of interest 50347dbc725eSTejun Heo * @cpu: the CPU which is coming up 50357dbc725eSTejun Heo * 50367dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 50377dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 50387dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 50397dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 50407dbc725eSTejun Heo */ 50417dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 50427dbc725eSTejun Heo { 50437dbc725eSTejun Heo static cpumask_t cpumask; 50447dbc725eSTejun Heo struct worker *worker; 50457dbc725eSTejun Heo 50461258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 50477dbc725eSTejun Heo 50487dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 50497dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 50507dbc725eSTejun Heo return; 50517dbc725eSTejun Heo 50527dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 50537dbc725eSTejun Heo 50547dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5055da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5056d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 50577dbc725eSTejun Heo } 50587dbc725eSTejun Heo 50597ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 50601da177e4SLinus Torvalds { 50614ce62e9eSTejun Heo struct worker_pool *pool; 50621da177e4SLinus Torvalds 5063f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 50643ce63377STejun Heo if (pool->nr_workers) 50653ce63377STejun Heo continue; 5066051e1850SLai Jiangshan if (!create_worker(pool)) 50677ee681b2SThomas Gleixner return -ENOMEM; 50683af24433SOleg Nesterov } 50697ee681b2SThomas Gleixner return 0; 50707ee681b2SThomas Gleixner } 50711da177e4SLinus Torvalds 50727ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 50737ee681b2SThomas Gleixner { 50747ee681b2SThomas Gleixner struct worker_pool *pool; 50757ee681b2SThomas Gleixner struct workqueue_struct *wq; 50767ee681b2SThomas Gleixner int pi; 50777ee681b2SThomas Gleixner 507868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 50797dbc725eSTejun Heo 50807dbc725eSTejun Heo for_each_pool(pool, pi) { 50811258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 508294cf58bbSTejun Heo 5083f05b558dSLai Jiangshan if (pool->cpu == cpu) 508494cf58bbSTejun Heo rebind_workers(pool); 5085f05b558dSLai Jiangshan else if (pool->cpu < 0) 50867dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 508794cf58bbSTejun Heo 50881258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 508994cf58bbSTejun Heo } 50907dbc725eSTejun Heo 50914c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 50924c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 50934c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 50944c16bd32STejun Heo 509568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 50967ee681b2SThomas Gleixner return 0; 509765758202STejun Heo } 509865758202STejun Heo 50997ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 510065758202STejun Heo { 51014c16bd32STejun Heo struct workqueue_struct *wq; 51028db25e78STejun Heo 51034c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5104e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5105e8b3f8dbSLai Jiangshan return -1; 5106e8b3f8dbSLai Jiangshan 5107e8b3f8dbSLai Jiangshan unbind_workers(cpu); 51084c16bd32STejun Heo 51094c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 51104c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 51114c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 51124c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 51134c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 51144c16bd32STejun Heo 51157ee681b2SThomas Gleixner return 0; 511665758202STejun Heo } 511765758202STejun Heo 51182d3854a3SRusty Russell struct work_for_cpu { 5119ed48ece2STejun Heo struct work_struct work; 51202d3854a3SRusty Russell long (*fn)(void *); 51212d3854a3SRusty Russell void *arg; 51222d3854a3SRusty Russell long ret; 51232d3854a3SRusty Russell }; 51242d3854a3SRusty Russell 5125ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 51262d3854a3SRusty Russell { 5127ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5128ed48ece2STejun Heo 51292d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 51302d3854a3SRusty Russell } 51312d3854a3SRusty Russell 51322d3854a3SRusty Russell /** 513322aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 51342d3854a3SRusty Russell * @cpu: the cpu to run on 51352d3854a3SRusty Russell * @fn: the function to run 51362d3854a3SRusty Russell * @arg: the function arg 51372d3854a3SRusty Russell * 513831ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 51396b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5140d185af30SYacine Belkadi * 5141d185af30SYacine Belkadi * Return: The value @fn returns. 51422d3854a3SRusty Russell */ 5143d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 51442d3854a3SRusty Russell { 5145ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 51462d3854a3SRusty Russell 5147ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5148ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 514912997d1aSBjorn Helgaas flush_work(&wfc.work); 5150440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 51512d3854a3SRusty Russell return wfc.ret; 51522d3854a3SRusty Russell } 51532d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 51540e8d6a93SThomas Gleixner 51550e8d6a93SThomas Gleixner /** 51560e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 51570e8d6a93SThomas Gleixner * @cpu: the cpu to run on 51580e8d6a93SThomas Gleixner * @fn: the function to run 51590e8d6a93SThomas Gleixner * @arg: the function argument 51600e8d6a93SThomas Gleixner * 51610e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 51620e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 51630e8d6a93SThomas Gleixner * 51640e8d6a93SThomas Gleixner * Return: The value @fn returns. 51650e8d6a93SThomas Gleixner */ 51660e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 51670e8d6a93SThomas Gleixner { 51680e8d6a93SThomas Gleixner long ret = -ENODEV; 51690e8d6a93SThomas Gleixner 5170ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 51710e8d6a93SThomas Gleixner if (cpu_online(cpu)) 51720e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 5173ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 51740e8d6a93SThomas Gleixner return ret; 51750e8d6a93SThomas Gleixner } 51760e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 51772d3854a3SRusty Russell #endif /* CONFIG_SMP */ 51782d3854a3SRusty Russell 5179a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5180e7577c50SRusty Russell 5181a0a1a5fdSTejun Heo /** 5182a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5183a0a1a5fdSTejun Heo * 518458a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5185*f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5186706026c2STejun Heo * pool->worklist. 5187a0a1a5fdSTejun Heo * 5188a0a1a5fdSTejun Heo * CONTEXT: 5189a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5190a0a1a5fdSTejun Heo */ 5191a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5192a0a1a5fdSTejun Heo { 519324b8a847STejun Heo struct workqueue_struct *wq; 519424b8a847STejun Heo struct pool_workqueue *pwq; 5195a0a1a5fdSTejun Heo 519668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5197a0a1a5fdSTejun Heo 51986183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5199a0a1a5fdSTejun Heo workqueue_freezing = true; 5200a0a1a5fdSTejun Heo 520124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5202a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5203699ce097STejun Heo for_each_pwq(pwq, wq) 5204699ce097STejun Heo pwq_adjust_max_active(pwq); 5205a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5206a1056305STejun Heo } 52075bcab335STejun Heo 520868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5209a0a1a5fdSTejun Heo } 5210a0a1a5fdSTejun Heo 5211a0a1a5fdSTejun Heo /** 521258a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5213a0a1a5fdSTejun Heo * 5214a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5215a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5216a0a1a5fdSTejun Heo * 5217a0a1a5fdSTejun Heo * CONTEXT: 521868e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5219a0a1a5fdSTejun Heo * 5220d185af30SYacine Belkadi * Return: 522158a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 522258a69cb4STejun Heo * is complete. 5223a0a1a5fdSTejun Heo */ 5224a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5225a0a1a5fdSTejun Heo { 5226a0a1a5fdSTejun Heo bool busy = false; 522724b8a847STejun Heo struct workqueue_struct *wq; 522824b8a847STejun Heo struct pool_workqueue *pwq; 5229a0a1a5fdSTejun Heo 523068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5231a0a1a5fdSTejun Heo 52326183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5233a0a1a5fdSTejun Heo 523424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 523524b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 523624b8a847STejun Heo continue; 5237a0a1a5fdSTejun Heo /* 5238a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5239a0a1a5fdSTejun Heo * to peek without lock. 5240a0a1a5fdSTejun Heo */ 524124acfb71SThomas Gleixner rcu_read_lock(); 524224b8a847STejun Heo for_each_pwq(pwq, wq) { 52436183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5244112202d9STejun Heo if (pwq->nr_active) { 5245a0a1a5fdSTejun Heo busy = true; 524624acfb71SThomas Gleixner rcu_read_unlock(); 5247a0a1a5fdSTejun Heo goto out_unlock; 5248a0a1a5fdSTejun Heo } 5249a0a1a5fdSTejun Heo } 525024acfb71SThomas Gleixner rcu_read_unlock(); 5251a0a1a5fdSTejun Heo } 5252a0a1a5fdSTejun Heo out_unlock: 525368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5254a0a1a5fdSTejun Heo return busy; 5255a0a1a5fdSTejun Heo } 5256a0a1a5fdSTejun Heo 5257a0a1a5fdSTejun Heo /** 5258a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5259a0a1a5fdSTejun Heo * 5260a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5261706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5262a0a1a5fdSTejun Heo * 5263a0a1a5fdSTejun Heo * CONTEXT: 5264a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5265a0a1a5fdSTejun Heo */ 5266a0a1a5fdSTejun Heo void thaw_workqueues(void) 5267a0a1a5fdSTejun Heo { 526824b8a847STejun Heo struct workqueue_struct *wq; 526924b8a847STejun Heo struct pool_workqueue *pwq; 5270a0a1a5fdSTejun Heo 527168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5272a0a1a5fdSTejun Heo 5273a0a1a5fdSTejun Heo if (!workqueue_freezing) 5274a0a1a5fdSTejun Heo goto out_unlock; 5275a0a1a5fdSTejun Heo 527674b414eaSLai Jiangshan workqueue_freezing = false; 527724b8a847STejun Heo 527824b8a847STejun Heo /* restore max_active and repopulate worklist */ 527924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5280a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5281699ce097STejun Heo for_each_pwq(pwq, wq) 5282699ce097STejun Heo pwq_adjust_max_active(pwq); 5283a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 528424b8a847STejun Heo } 528524b8a847STejun Heo 5286a0a1a5fdSTejun Heo out_unlock: 528768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5288a0a1a5fdSTejun Heo } 5289a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5290a0a1a5fdSTejun Heo 5291042f7df1SLai Jiangshan static int workqueue_apply_unbound_cpumask(void) 5292042f7df1SLai Jiangshan { 5293042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5294042f7df1SLai Jiangshan int ret = 0; 5295042f7df1SLai Jiangshan struct workqueue_struct *wq; 5296042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5297042f7df1SLai Jiangshan 5298042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5299042f7df1SLai Jiangshan 5300042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5301042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5302042f7df1SLai Jiangshan continue; 5303042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5304042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5305042f7df1SLai Jiangshan continue; 5306042f7df1SLai Jiangshan 5307042f7df1SLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); 5308042f7df1SLai Jiangshan if (!ctx) { 5309042f7df1SLai Jiangshan ret = -ENOMEM; 5310042f7df1SLai Jiangshan break; 5311042f7df1SLai Jiangshan } 5312042f7df1SLai Jiangshan 5313042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5314042f7df1SLai Jiangshan } 5315042f7df1SLai Jiangshan 5316042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5317042f7df1SLai Jiangshan if (!ret) 5318042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5319042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5320042f7df1SLai Jiangshan } 5321042f7df1SLai Jiangshan 5322042f7df1SLai Jiangshan return ret; 5323042f7df1SLai Jiangshan } 5324042f7df1SLai Jiangshan 5325042f7df1SLai Jiangshan /** 5326042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5327042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5328042f7df1SLai Jiangshan * 5329042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5330042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5331042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5332042f7df1SLai Jiangshan * 533367dc8325SCai Huoqing * Return: 0 - Success 5334042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5335042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5336042f7df1SLai Jiangshan */ 5337042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5338042f7df1SLai Jiangshan { 5339042f7df1SLai Jiangshan int ret = -EINVAL; 5340042f7df1SLai Jiangshan cpumask_var_t saved_cpumask; 5341042f7df1SLai Jiangshan 5342042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) 5343042f7df1SLai Jiangshan return -ENOMEM; 5344042f7df1SLai Jiangshan 5345c98a9805STal Shorer /* 5346c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5347c98a9805STal Shorer * If the user wishes to include them, we allow that. 5348c98a9805STal Shorer */ 5349042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5350042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5351a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5352042f7df1SLai Jiangshan 5353042f7df1SLai Jiangshan /* save the old wq_unbound_cpumask. */ 5354042f7df1SLai Jiangshan cpumask_copy(saved_cpumask, wq_unbound_cpumask); 5355042f7df1SLai Jiangshan 5356042f7df1SLai Jiangshan /* update wq_unbound_cpumask at first and apply it to wqs. */ 5357042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, cpumask); 5358042f7df1SLai Jiangshan ret = workqueue_apply_unbound_cpumask(); 5359042f7df1SLai Jiangshan 5360042f7df1SLai Jiangshan /* restore the wq_unbound_cpumask when failed. */ 5361042f7df1SLai Jiangshan if (ret < 0) 5362042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, saved_cpumask); 5363042f7df1SLai Jiangshan 5364a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5365042f7df1SLai Jiangshan } 5366042f7df1SLai Jiangshan 5367042f7df1SLai Jiangshan free_cpumask_var(saved_cpumask); 5368042f7df1SLai Jiangshan return ret; 5369042f7df1SLai Jiangshan } 5370042f7df1SLai Jiangshan 53716ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 53726ba94429SFrederic Weisbecker /* 53736ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 53746ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 53756ba94429SFrederic Weisbecker * following attributes. 53766ba94429SFrederic Weisbecker * 53776ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 53786ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 53796ba94429SFrederic Weisbecker * 53806ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 53816ba94429SFrederic Weisbecker * 53829a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 53836ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 53846ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 53859a19b463SWang Long * numa RW bool : whether enable NUMA affinity 53866ba94429SFrederic Weisbecker */ 53876ba94429SFrederic Weisbecker struct wq_device { 53886ba94429SFrederic Weisbecker struct workqueue_struct *wq; 53896ba94429SFrederic Weisbecker struct device dev; 53906ba94429SFrederic Weisbecker }; 53916ba94429SFrederic Weisbecker 53926ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 53936ba94429SFrederic Weisbecker { 53946ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 53956ba94429SFrederic Weisbecker 53966ba94429SFrederic Weisbecker return wq_dev->wq; 53976ba94429SFrederic Weisbecker } 53986ba94429SFrederic Weisbecker 53996ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 54006ba94429SFrederic Weisbecker char *buf) 54016ba94429SFrederic Weisbecker { 54026ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54036ba94429SFrederic Weisbecker 54046ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 54056ba94429SFrederic Weisbecker } 54066ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 54076ba94429SFrederic Weisbecker 54086ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 54096ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 54106ba94429SFrederic Weisbecker { 54116ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54126ba94429SFrederic Weisbecker 54136ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 54146ba94429SFrederic Weisbecker } 54156ba94429SFrederic Weisbecker 54166ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 54176ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 54186ba94429SFrederic Weisbecker size_t count) 54196ba94429SFrederic Weisbecker { 54206ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54216ba94429SFrederic Weisbecker int val; 54226ba94429SFrederic Weisbecker 54236ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 54246ba94429SFrederic Weisbecker return -EINVAL; 54256ba94429SFrederic Weisbecker 54266ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 54276ba94429SFrederic Weisbecker return count; 54286ba94429SFrederic Weisbecker } 54296ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 54306ba94429SFrederic Weisbecker 54316ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 54326ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 54336ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 54346ba94429SFrederic Weisbecker NULL, 54356ba94429SFrederic Weisbecker }; 54366ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 54376ba94429SFrederic Weisbecker 54386ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 54396ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 54406ba94429SFrederic Weisbecker { 54416ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54426ba94429SFrederic Weisbecker const char *delim = ""; 54436ba94429SFrederic Weisbecker int node, written = 0; 54446ba94429SFrederic Weisbecker 5445ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 544624acfb71SThomas Gleixner rcu_read_lock(); 54476ba94429SFrederic Weisbecker for_each_node(node) { 54486ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 54496ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 54506ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 54516ba94429SFrederic Weisbecker delim = " "; 54526ba94429SFrederic Weisbecker } 54536ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 545424acfb71SThomas Gleixner rcu_read_unlock(); 5455ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 54566ba94429SFrederic Weisbecker 54576ba94429SFrederic Weisbecker return written; 54586ba94429SFrederic Weisbecker } 54596ba94429SFrederic Weisbecker 54606ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 54616ba94429SFrederic Weisbecker char *buf) 54626ba94429SFrederic Weisbecker { 54636ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54646ba94429SFrederic Weisbecker int written; 54656ba94429SFrederic Weisbecker 54666ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 54676ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 54686ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 54696ba94429SFrederic Weisbecker 54706ba94429SFrederic Weisbecker return written; 54716ba94429SFrederic Weisbecker } 54726ba94429SFrederic Weisbecker 54736ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 54746ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 54756ba94429SFrederic Weisbecker { 54766ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 54776ba94429SFrederic Weisbecker 5478899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5479899a94feSLai Jiangshan 5480be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 54816ba94429SFrederic Weisbecker if (!attrs) 54826ba94429SFrederic Weisbecker return NULL; 54836ba94429SFrederic Weisbecker 54846ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 54856ba94429SFrederic Weisbecker return attrs; 54866ba94429SFrederic Weisbecker } 54876ba94429SFrederic Weisbecker 54886ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 54896ba94429SFrederic Weisbecker const char *buf, size_t count) 54906ba94429SFrederic Weisbecker { 54916ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54926ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5493d4d3e257SLai Jiangshan int ret = -ENOMEM; 5494d4d3e257SLai Jiangshan 5495d4d3e257SLai Jiangshan apply_wqattrs_lock(); 54966ba94429SFrederic Weisbecker 54976ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 54986ba94429SFrederic Weisbecker if (!attrs) 5499d4d3e257SLai Jiangshan goto out_unlock; 55006ba94429SFrederic Weisbecker 55016ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 55026ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5503d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 55046ba94429SFrederic Weisbecker else 55056ba94429SFrederic Weisbecker ret = -EINVAL; 55066ba94429SFrederic Weisbecker 5507d4d3e257SLai Jiangshan out_unlock: 5508d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 55096ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 55106ba94429SFrederic Weisbecker return ret ?: count; 55116ba94429SFrederic Weisbecker } 55126ba94429SFrederic Weisbecker 55136ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 55146ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 55156ba94429SFrederic Weisbecker { 55166ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 55176ba94429SFrederic Weisbecker int written; 55186ba94429SFrederic Weisbecker 55196ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 55206ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 55216ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 55226ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 55236ba94429SFrederic Weisbecker return written; 55246ba94429SFrederic Weisbecker } 55256ba94429SFrederic Weisbecker 55266ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 55276ba94429SFrederic Weisbecker struct device_attribute *attr, 55286ba94429SFrederic Weisbecker const char *buf, size_t count) 55296ba94429SFrederic Weisbecker { 55306ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 55316ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5532d4d3e257SLai Jiangshan int ret = -ENOMEM; 5533d4d3e257SLai Jiangshan 5534d4d3e257SLai Jiangshan apply_wqattrs_lock(); 55356ba94429SFrederic Weisbecker 55366ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 55376ba94429SFrederic Weisbecker if (!attrs) 5538d4d3e257SLai Jiangshan goto out_unlock; 55396ba94429SFrederic Weisbecker 55406ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 55416ba94429SFrederic Weisbecker if (!ret) 5542d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 55436ba94429SFrederic Weisbecker 5544d4d3e257SLai Jiangshan out_unlock: 5545d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 55466ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 55476ba94429SFrederic Weisbecker return ret ?: count; 55486ba94429SFrederic Weisbecker } 55496ba94429SFrederic Weisbecker 55506ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 55516ba94429SFrederic Weisbecker char *buf) 55526ba94429SFrederic Weisbecker { 55536ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 55546ba94429SFrederic Weisbecker int written; 55556ba94429SFrederic Weisbecker 55566ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 55576ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 55586ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 55596ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 55606ba94429SFrederic Weisbecker 55616ba94429SFrederic Weisbecker return written; 55626ba94429SFrederic Weisbecker } 55636ba94429SFrederic Weisbecker 55646ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 55656ba94429SFrederic Weisbecker const char *buf, size_t count) 55666ba94429SFrederic Weisbecker { 55676ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 55686ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5569d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5570d4d3e257SLai Jiangshan 5571d4d3e257SLai Jiangshan apply_wqattrs_lock(); 55726ba94429SFrederic Weisbecker 55736ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 55746ba94429SFrederic Weisbecker if (!attrs) 5575d4d3e257SLai Jiangshan goto out_unlock; 55766ba94429SFrederic Weisbecker 55776ba94429SFrederic Weisbecker ret = -EINVAL; 55786ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 55796ba94429SFrederic Weisbecker attrs->no_numa = !v; 5580d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 55816ba94429SFrederic Weisbecker } 55826ba94429SFrederic Weisbecker 5583d4d3e257SLai Jiangshan out_unlock: 5584d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 55856ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 55866ba94429SFrederic Weisbecker return ret ?: count; 55876ba94429SFrederic Weisbecker } 55886ba94429SFrederic Weisbecker 55896ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 55906ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 55916ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 55926ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 55936ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 55946ba94429SFrederic Weisbecker __ATTR_NULL, 55956ba94429SFrederic Weisbecker }; 55966ba94429SFrederic Weisbecker 55976ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 55986ba94429SFrederic Weisbecker .name = "workqueue", 55996ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 56006ba94429SFrederic Weisbecker }; 56016ba94429SFrederic Weisbecker 5602b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5603b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5604b05a7928SFrederic Weisbecker { 5605b05a7928SFrederic Weisbecker int written; 5606b05a7928SFrederic Weisbecker 5607042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5608b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5609b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5610042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5611b05a7928SFrederic Weisbecker 5612b05a7928SFrederic Weisbecker return written; 5613b05a7928SFrederic Weisbecker } 5614b05a7928SFrederic Weisbecker 5615042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5616042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5617042f7df1SLai Jiangshan { 5618042f7df1SLai Jiangshan cpumask_var_t cpumask; 5619042f7df1SLai Jiangshan int ret; 5620042f7df1SLai Jiangshan 5621042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5622042f7df1SLai Jiangshan return -ENOMEM; 5623042f7df1SLai Jiangshan 5624042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5625042f7df1SLai Jiangshan if (!ret) 5626042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5627042f7df1SLai Jiangshan 5628042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5629042f7df1SLai Jiangshan return ret ? ret : count; 5630042f7df1SLai Jiangshan } 5631042f7df1SLai Jiangshan 5632b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5633042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5634042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5635b05a7928SFrederic Weisbecker 56366ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 56376ba94429SFrederic Weisbecker { 5638b05a7928SFrederic Weisbecker int err; 5639b05a7928SFrederic Weisbecker 5640b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5641b05a7928SFrederic Weisbecker if (err) 5642b05a7928SFrederic Weisbecker return err; 5643b05a7928SFrederic Weisbecker 5644b05a7928SFrederic Weisbecker return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr); 56456ba94429SFrederic Weisbecker } 56466ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 56476ba94429SFrederic Weisbecker 56486ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 56496ba94429SFrederic Weisbecker { 56506ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 56516ba94429SFrederic Weisbecker 56526ba94429SFrederic Weisbecker kfree(wq_dev); 56536ba94429SFrederic Weisbecker } 56546ba94429SFrederic Weisbecker 56556ba94429SFrederic Weisbecker /** 56566ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 56576ba94429SFrederic Weisbecker * @wq: the workqueue to register 56586ba94429SFrederic Weisbecker * 56596ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 56606ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 56616ba94429SFrederic Weisbecker * which is the preferred method. 56626ba94429SFrederic Weisbecker * 56636ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 56646ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 56656ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 56666ba94429SFrederic Weisbecker * attributes. 56676ba94429SFrederic Weisbecker * 56686ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 56696ba94429SFrederic Weisbecker */ 56706ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 56716ba94429SFrederic Weisbecker { 56726ba94429SFrederic Weisbecker struct wq_device *wq_dev; 56736ba94429SFrederic Weisbecker int ret; 56746ba94429SFrederic Weisbecker 56756ba94429SFrederic Weisbecker /* 5676402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 56776ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 56786ba94429SFrederic Weisbecker * workqueues. 56796ba94429SFrederic Weisbecker */ 56800a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 56816ba94429SFrederic Weisbecker return -EINVAL; 56826ba94429SFrederic Weisbecker 56836ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 56846ba94429SFrederic Weisbecker if (!wq_dev) 56856ba94429SFrederic Weisbecker return -ENOMEM; 56866ba94429SFrederic Weisbecker 56876ba94429SFrederic Weisbecker wq_dev->wq = wq; 56886ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 56896ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 569023217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 56916ba94429SFrederic Weisbecker 56926ba94429SFrederic Weisbecker /* 56936ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 56946ba94429SFrederic Weisbecker * everything is ready. 56956ba94429SFrederic Weisbecker */ 56966ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 56976ba94429SFrederic Weisbecker 56986ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 56996ba94429SFrederic Weisbecker if (ret) { 5700537f4146SArvind Yadav put_device(&wq_dev->dev); 57016ba94429SFrederic Weisbecker wq->wq_dev = NULL; 57026ba94429SFrederic Weisbecker return ret; 57036ba94429SFrederic Weisbecker } 57046ba94429SFrederic Weisbecker 57056ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 57066ba94429SFrederic Weisbecker struct device_attribute *attr; 57076ba94429SFrederic Weisbecker 57086ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 57096ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 57106ba94429SFrederic Weisbecker if (ret) { 57116ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 57126ba94429SFrederic Weisbecker wq->wq_dev = NULL; 57136ba94429SFrederic Weisbecker return ret; 57146ba94429SFrederic Weisbecker } 57156ba94429SFrederic Weisbecker } 57166ba94429SFrederic Weisbecker } 57176ba94429SFrederic Weisbecker 57186ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 57196ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 57206ba94429SFrederic Weisbecker return 0; 57216ba94429SFrederic Weisbecker } 57226ba94429SFrederic Weisbecker 57236ba94429SFrederic Weisbecker /** 57246ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 57256ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 57266ba94429SFrederic Weisbecker * 57276ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 57286ba94429SFrederic Weisbecker */ 57296ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 57306ba94429SFrederic Weisbecker { 57316ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 57326ba94429SFrederic Weisbecker 57336ba94429SFrederic Weisbecker if (!wq->wq_dev) 57346ba94429SFrederic Weisbecker return; 57356ba94429SFrederic Weisbecker 57366ba94429SFrederic Weisbecker wq->wq_dev = NULL; 57376ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 57386ba94429SFrederic Weisbecker } 57396ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 57406ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 57416ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 57426ba94429SFrederic Weisbecker 574382607adcSTejun Heo /* 574482607adcSTejun Heo * Workqueue watchdog. 574582607adcSTejun Heo * 574682607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 574782607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 574882607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 574982607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 575082607adcSTejun Heo * largely opaque. 575182607adcSTejun Heo * 575282607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 575382607adcSTejun Heo * state if some pools failed to make forward progress for a while where 575482607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 575582607adcSTejun Heo * 575682607adcSTejun Heo * This mechanism is controlled through the kernel parameter 575782607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 575882607adcSTejun Heo * corresponding sysfs parameter file. 575982607adcSTejun Heo */ 576082607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 576182607adcSTejun Heo 576282607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 57635cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 576482607adcSTejun Heo 576582607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 576682607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 576782607adcSTejun Heo 576882607adcSTejun Heo static void wq_watchdog_reset_touched(void) 576982607adcSTejun Heo { 577082607adcSTejun Heo int cpu; 577182607adcSTejun Heo 577282607adcSTejun Heo wq_watchdog_touched = jiffies; 577382607adcSTejun Heo for_each_possible_cpu(cpu) 577482607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 577582607adcSTejun Heo } 577682607adcSTejun Heo 57775cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 577882607adcSTejun Heo { 577982607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 578082607adcSTejun Heo bool lockup_detected = false; 5781940d71c6SSergey Senozhatsky unsigned long now = jiffies; 578282607adcSTejun Heo struct worker_pool *pool; 578382607adcSTejun Heo int pi; 578482607adcSTejun Heo 578582607adcSTejun Heo if (!thresh) 578682607adcSTejun Heo return; 578782607adcSTejun Heo 578882607adcSTejun Heo rcu_read_lock(); 578982607adcSTejun Heo 579082607adcSTejun Heo for_each_pool(pool, pi) { 579182607adcSTejun Heo unsigned long pool_ts, touched, ts; 579282607adcSTejun Heo 579382607adcSTejun Heo if (list_empty(&pool->worklist)) 579482607adcSTejun Heo continue; 579582607adcSTejun Heo 5796940d71c6SSergey Senozhatsky /* 5797940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 5798940d71c6SSergey Senozhatsky * the watchdog like a stall. 5799940d71c6SSergey Senozhatsky */ 5800940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 5801940d71c6SSergey Senozhatsky 580282607adcSTejun Heo /* get the latest of pool and touched timestamps */ 580389e28ce6SWang Qing if (pool->cpu >= 0) 580489e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 580589e28ce6SWang Qing else 580682607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 580789e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 580882607adcSTejun Heo 580982607adcSTejun Heo if (time_after(pool_ts, touched)) 581082607adcSTejun Heo ts = pool_ts; 581182607adcSTejun Heo else 581282607adcSTejun Heo ts = touched; 581382607adcSTejun Heo 581482607adcSTejun Heo /* did we stall? */ 5815940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 581682607adcSTejun Heo lockup_detected = true; 581782607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 581882607adcSTejun Heo pr_cont_pool_info(pool); 581982607adcSTejun Heo pr_cont(" stuck for %us!\n", 5820940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 582182607adcSTejun Heo } 582282607adcSTejun Heo } 582382607adcSTejun Heo 582482607adcSTejun Heo rcu_read_unlock(); 582582607adcSTejun Heo 582682607adcSTejun Heo if (lockup_detected) 582782607adcSTejun Heo show_workqueue_state(); 582882607adcSTejun Heo 582982607adcSTejun Heo wq_watchdog_reset_touched(); 583082607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 583182607adcSTejun Heo } 583282607adcSTejun Heo 5833cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 583482607adcSTejun Heo { 583582607adcSTejun Heo if (cpu >= 0) 583682607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 583789e28ce6SWang Qing 583882607adcSTejun Heo wq_watchdog_touched = jiffies; 583982607adcSTejun Heo } 584082607adcSTejun Heo 584182607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 584282607adcSTejun Heo { 584382607adcSTejun Heo wq_watchdog_thresh = 0; 584482607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 584582607adcSTejun Heo 584682607adcSTejun Heo if (thresh) { 584782607adcSTejun Heo wq_watchdog_thresh = thresh; 584882607adcSTejun Heo wq_watchdog_reset_touched(); 584982607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 585082607adcSTejun Heo } 585182607adcSTejun Heo } 585282607adcSTejun Heo 585382607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 585482607adcSTejun Heo const struct kernel_param *kp) 585582607adcSTejun Heo { 585682607adcSTejun Heo unsigned long thresh; 585782607adcSTejun Heo int ret; 585882607adcSTejun Heo 585982607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 586082607adcSTejun Heo if (ret) 586182607adcSTejun Heo return ret; 586282607adcSTejun Heo 586382607adcSTejun Heo if (system_wq) 586482607adcSTejun Heo wq_watchdog_set_thresh(thresh); 586582607adcSTejun Heo else 586682607adcSTejun Heo wq_watchdog_thresh = thresh; 586782607adcSTejun Heo 586882607adcSTejun Heo return 0; 586982607adcSTejun Heo } 587082607adcSTejun Heo 587182607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 587282607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 587382607adcSTejun Heo .get = param_get_ulong, 587482607adcSTejun Heo }; 587582607adcSTejun Heo 587682607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 587782607adcSTejun Heo 0644); 587882607adcSTejun Heo 587982607adcSTejun Heo static void wq_watchdog_init(void) 588082607adcSTejun Heo { 58815cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 588282607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 588382607adcSTejun Heo } 588482607adcSTejun Heo 588582607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 588682607adcSTejun Heo 588782607adcSTejun Heo static inline void wq_watchdog_init(void) { } 588882607adcSTejun Heo 588982607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 589082607adcSTejun Heo 5891bce90380STejun Heo static void __init wq_numa_init(void) 5892bce90380STejun Heo { 5893bce90380STejun Heo cpumask_var_t *tbl; 5894bce90380STejun Heo int node, cpu; 5895bce90380STejun Heo 5896bce90380STejun Heo if (num_possible_nodes() <= 1) 5897bce90380STejun Heo return; 5898bce90380STejun Heo 5899d55262c4STejun Heo if (wq_disable_numa) { 5900d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 5901d55262c4STejun Heo return; 5902d55262c4STejun Heo } 5903d55262c4STejun Heo 5904f728c4a9SZhen Lei for_each_possible_cpu(cpu) { 5905f728c4a9SZhen Lei if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) { 5906f728c4a9SZhen Lei pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 5907f728c4a9SZhen Lei return; 5908f728c4a9SZhen Lei } 5909f728c4a9SZhen Lei } 5910f728c4a9SZhen Lei 5911be69d00dSThomas Gleixner wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(); 59124c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 59134c16bd32STejun Heo 5914bce90380STejun Heo /* 5915bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 5916bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 5917bce90380STejun Heo * fully initialized by now. 5918bce90380STejun Heo */ 59196396bb22SKees Cook tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL); 5920bce90380STejun Heo BUG_ON(!tbl); 5921bce90380STejun Heo 5922bce90380STejun Heo for_each_node(node) 59235a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 59241be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 5925bce90380STejun Heo 5926bce90380STejun Heo for_each_possible_cpu(cpu) { 5927bce90380STejun Heo node = cpu_to_node(cpu); 5928bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 5929bce90380STejun Heo } 5930bce90380STejun Heo 5931bce90380STejun Heo wq_numa_possible_cpumask = tbl; 5932bce90380STejun Heo wq_numa_enabled = true; 5933bce90380STejun Heo } 5934bce90380STejun Heo 59353347fa09STejun Heo /** 59363347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 59373347fa09STejun Heo * 59383347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 59393347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 59403347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 59413347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 59423347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 59433347fa09STejun Heo * created and scheduled right before early initcalls. 59443347fa09STejun Heo */ 59452333e829SYu Chen void __init workqueue_init_early(void) 59461da177e4SLinus Torvalds { 59477a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 59481bda3f80SFrederic Weisbecker int hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ; 59497a4e344cSTejun Heo int i, cpu; 5950c34056a3STejun Heo 595110cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 5952e904e6c2STejun Heo 5953b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 59541bda3f80SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(hk_flags)); 5955b05a7928SFrederic Weisbecker 5956e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 5957e904e6c2STejun Heo 5958706026c2STejun Heo /* initialize CPU pools */ 595929c91e99STejun Heo for_each_possible_cpu(cpu) { 59604ce62e9eSTejun Heo struct worker_pool *pool; 59618b03ae3cSTejun Heo 59627a4e344cSTejun Heo i = 0; 5963f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 59647a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 5965ec22ca5eSTejun Heo pool->cpu = cpu; 59667a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 59677a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 5968f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 59697a4e344cSTejun Heo 59709daf9e67STejun Heo /* alloc pool ID */ 597168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 59729daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 597368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 59744ce62e9eSTejun Heo } 59758b03ae3cSTejun Heo } 59768b03ae3cSTejun Heo 59778a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 597829c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 597929c91e99STejun Heo struct workqueue_attrs *attrs; 598029c91e99STejun Heo 5981be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 598229c91e99STejun Heo attrs->nice = std_nice[i]; 598329c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 59848a2b7538STejun Heo 59858a2b7538STejun Heo /* 59868a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 59878a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 59888a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 59898a2b7538STejun Heo */ 5990be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 59918a2b7538STejun Heo attrs->nice = std_nice[i]; 59928a2b7538STejun Heo attrs->no_numa = true; 59938a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 599429c91e99STejun Heo } 599529c91e99STejun Heo 5996d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 59971aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 5998d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 5999f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6000f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 600124d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 600224d51addSTejun Heo WQ_FREEZABLE, 0); 60030668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 60040668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 60050668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 60060668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 60070668106cSViresh Kumar 0); 60081aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 60090668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 60100668106cSViresh Kumar !system_power_efficient_wq || 60110668106cSViresh Kumar !system_freezable_power_efficient_wq); 60123347fa09STejun Heo } 60133347fa09STejun Heo 60143347fa09STejun Heo /** 60153347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 60163347fa09STejun Heo * 60173347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 60183347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 60193347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 60203347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 60213347fa09STejun Heo * with the initial workers and enable future kworker creations. 60223347fa09STejun Heo */ 60232333e829SYu Chen void __init workqueue_init(void) 60243347fa09STejun Heo { 60252186d9f9STejun Heo struct workqueue_struct *wq; 60263347fa09STejun Heo struct worker_pool *pool; 60273347fa09STejun Heo int cpu, bkt; 60283347fa09STejun Heo 60292186d9f9STejun Heo /* 60302186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 60312186d9f9STejun Heo * CPU to node mapping may not be available that early on some 60322186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 60332186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 60342186d9f9STejun Heo * affinity, fix them up. 603540c17f75STejun Heo * 603640c17f75STejun Heo * Also, while iterating workqueues, create rescuers if requested. 60372186d9f9STejun Heo */ 60382186d9f9STejun Heo wq_numa_init(); 60392186d9f9STejun Heo 60402186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 60412186d9f9STejun Heo 60422186d9f9STejun Heo for_each_possible_cpu(cpu) { 60432186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 60442186d9f9STejun Heo pool->node = cpu_to_node(cpu); 60452186d9f9STejun Heo } 60462186d9f9STejun Heo } 60472186d9f9STejun Heo 604840c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 60492186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 605040c17f75STejun Heo WARN(init_rescuer(wq), 605140c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 605240c17f75STejun Heo wq->name); 605340c17f75STejun Heo } 60542186d9f9STejun Heo 60552186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 60562186d9f9STejun Heo 60573347fa09STejun Heo /* create the initial workers */ 60583347fa09STejun Heo for_each_online_cpu(cpu) { 60593347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 60603347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 60613347fa09STejun Heo BUG_ON(!create_worker(pool)); 60623347fa09STejun Heo } 60633347fa09STejun Heo } 60643347fa09STejun Heo 60653347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 60663347fa09STejun Heo BUG_ON(!create_worker(pool)); 60673347fa09STejun Heo 60683347fa09STejun Heo wq_online = true; 606982607adcSTejun Heo wq_watchdog_init(); 60701da177e4SLinus Torvalds } 6071