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 <[email protected]> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <[email protected]> 111da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 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 <[email protected]> 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> 53e22bee78STejun Heo 54ea138446STejun Heo #include "workqueue_internal.h" 551da177e4SLinus Torvalds 56c8e55f36STejun Heo enum { 57bc2ae0f5STejun Heo /* 5824647570STejun Heo * worker_pool flags 59bc2ae0f5STejun Heo * 6024647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 61bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 62bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 63bc2ae0f5STejun Heo * is in effect. 64bc2ae0f5STejun Heo * 65bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 66bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6724647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 68bc2ae0f5STejun Heo * 69bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 701258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 714736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 72bc2ae0f5STejun Heo */ 73692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7424647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 75db7bccf4STejun Heo 76c8e55f36STejun Heo /* worker flags */ 77c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 78c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 79e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 80fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 81f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 82a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 83e22bee78STejun Heo 84a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 85a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 86db7bccf4STejun Heo 87e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 884ce62e9eSTejun Heo 8929c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 90c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 91db7bccf4STejun Heo 92e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 93e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 94e22bee78STejun Heo 953233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 963233cdbdSTejun Heo /* call for help after 10ms 973233cdbdSTejun Heo (min two ticks) */ 98e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 99e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* 102e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1038698a745SDongsheng Yang * all cpus. Give MIN_NICE. 104e22bee78STejun Heo */ 1058698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1068698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 107ecf6881fSTejun Heo 108ecf6881fSTejun Heo WQ_NAME_LEN = 24, 109c8e55f36STejun Heo }; 110c8e55f36STejun Heo 1111da177e4SLinus Torvalds /* 1124690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1134690c4abSTejun Heo * 114e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 115e41e704bSTejun Heo * everyone else. 1164690c4abSTejun Heo * 117e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 118e22bee78STejun Heo * only be modified and accessed from the local cpu. 119e22bee78STejun Heo * 120d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1214690c4abSTejun Heo * 122d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 123d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 124d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 125d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 126e22bee78STejun Heo * 1271258fae7STejun Heo * A: wq_pool_attach_mutex protected. 128822d8405STejun Heo * 12968e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13076af4d93STejun Heo * 13124acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1325bcab335STejun Heo * 1335b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1345b95e1afSLai Jiangshan * 1355b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 13624acfb71SThomas Gleixner * RCU for reads. 1375b95e1afSLai Jiangshan * 1383c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1393c25a55dSLai Jiangshan * 14024acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1412e109a28STejun Heo * 1422e109a28STejun Heo * MD: wq_mayday_lock protected. 1434690c4abSTejun Heo */ 1444690c4abSTejun Heo 1452eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 146c34056a3STejun Heo 147bd7bdd43STejun Heo struct worker_pool { 148d565ed63STejun Heo spinlock_t lock; /* the pool lock */ 149d84ff051STejun Heo int cpu; /* I: the associated cpu */ 150f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1519daf9e67STejun Heo int id; /* I: pool ID */ 15211ebea50STejun Heo unsigned int flags; /* X: flags */ 153bd7bdd43STejun Heo 15482607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 15582607adcSTejun Heo 156bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 157ea1abd61SLai Jiangshan 1585826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1595826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 160bd7bdd43STejun Heo 161bd7bdd43STejun Heo struct list_head idle_list; /* X: list of idle workers */ 162bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 163bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 164bd7bdd43STejun Heo 165c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 166c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 167c9e7cf27STejun Heo /* L: hash of busy workers */ 168c9e7cf27STejun Heo 1692607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 17092f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 17160f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 172e19e397aSTejun Heo 1737cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 174e19e397aSTejun Heo 1757a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 17668e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 17768e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1787a4e344cSTejun Heo 179e19e397aSTejun Heo /* 180e19e397aSTejun Heo * The current concurrency level. As it's likely to be accessed 181e19e397aSTejun Heo * from other CPUs during try_to_wake_up(), put it in a separate 182e19e397aSTejun Heo * cacheline. 183e19e397aSTejun Heo */ 184e19e397aSTejun Heo atomic_t nr_running ____cacheline_aligned_in_smp; 18529c91e99STejun Heo 18629c91e99STejun Heo /* 18724acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 18829c91e99STejun Heo * from get_work_pool(). 18929c91e99STejun Heo */ 19029c91e99STejun Heo struct rcu_head rcu; 1918b03ae3cSTejun Heo } ____cacheline_aligned_in_smp; 1928b03ae3cSTejun Heo 1938b03ae3cSTejun Heo /* 194112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 195112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 196112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 197112202d9STejun Heo * number of flag bits. 1981da177e4SLinus Torvalds */ 199112202d9STejun Heo struct pool_workqueue { 200bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2014690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 20273f53c4aSTejun Heo int work_color; /* L: current color */ 20373f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2048864b4e5STejun Heo int refcnt; /* L: reference count */ 20573f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 20673f53c4aSTejun Heo /* L: nr of in_flight works */ 2071e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 208a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 2091e19ffc6STejun Heo struct list_head delayed_works; /* L: delayed works */ 2103c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2112e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2128864b4e5STejun Heo 2138864b4e5STejun Heo /* 2148864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2158864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 21624acfb71SThomas Gleixner * itself is also RCU protected so that the first pwq can be 217b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2188864b4e5STejun Heo */ 2198864b4e5STejun Heo struct work_struct unbound_release_work; 2208864b4e5STejun Heo struct rcu_head rcu; 221e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds /* 22473f53c4aSTejun Heo * Structure used to wait for workqueue flush. 22573f53c4aSTejun Heo */ 22673f53c4aSTejun Heo struct wq_flusher { 2273c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2283c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 22973f53c4aSTejun Heo struct completion done; /* flush completion */ 23073f53c4aSTejun Heo }; 2311da177e4SLinus Torvalds 232226223abSTejun Heo struct wq_device; 233226223abSTejun Heo 23473f53c4aSTejun Heo /* 235c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 236c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2371da177e4SLinus Torvalds */ 2381da177e4SLinus Torvalds struct workqueue_struct { 2393c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 240e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 24173f53c4aSTejun Heo 2423c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2433c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2443c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 245112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2463c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2473c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2483c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 24973f53c4aSTejun Heo 2502e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 25130ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 252e22bee78STejun Heo 25387fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 254a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 255226223abSTejun Heo 2565b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 2575b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 2586029a918STejun Heo 259226223abSTejun Heo #ifdef CONFIG_SYSFS 260226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 261226223abSTejun Heo #endif 2624e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 263669de8bdSBart Van Assche char *lock_name; 264669de8bdSBart Van Assche struct lock_class_key key; 2654e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2664e6045f1SJohannes Berg #endif 267ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2682728fd2fSTejun Heo 269e2dca7adSTejun Heo /* 27024acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 27124acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 272e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 273e2dca7adSTejun Heo */ 274e2dca7adSTejun Heo struct rcu_head rcu; 275e2dca7adSTejun Heo 2762728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 2772728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 2782728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 2795b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 2801da177e4SLinus Torvalds }; 2811da177e4SLinus Torvalds 282e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 283e904e6c2STejun Heo 284bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 285bce90380STejun Heo /* possible CPUs of each node */ 286bce90380STejun Heo 287d55262c4STejun Heo static bool wq_disable_numa; 288d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 289d55262c4STejun Heo 290cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 291552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 292cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 293cee22a15SViresh Kumar 294863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 2953347fa09STejun Heo 296bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 297bce90380STejun Heo 2984c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 2994c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 3004c16bd32STejun Heo 30168e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3021258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 3032e109a28STejun Heo static DEFINE_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 304692b4825STejun Heo static DECLARE_WAIT_QUEUE_HEAD(wq_manager_wait); /* wait for manager to go away */ 3055bcab335STejun Heo 306e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 30768e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3087d19c5ceSTejun Heo 309ef557180SMike Galbraith /* PL: allowable cpus for unbound wqs and work items */ 310ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 311ef557180SMike Galbraith 312ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 313ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 314b05a7928SFrederic Weisbecker 315f303fccbSTejun Heo /* 316f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 317f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 318f303fccbSTejun Heo * to uncover usages which depend on it. 319f303fccbSTejun Heo */ 320f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 321f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 322f303fccbSTejun Heo #else 323f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 324f303fccbSTejun Heo #endif 325f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 326f303fccbSTejun Heo 3277d19c5ceSTejun Heo /* the per-cpu worker pools */ 32825528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3297d19c5ceSTejun Heo 33068e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3317d19c5ceSTejun Heo 33268e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 33329c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 33429c91e99STejun Heo 335c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 33629c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 33729c91e99STejun Heo 3388a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3398a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3408a2b7538STejun Heo 341d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 342ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 343044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3441aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 345044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 346d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 347044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 348f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 349044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 35024d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3510668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3520668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3530668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3540668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 355d320c038STejun Heo 3567d19c5ceSTejun Heo static int worker_thread(void *__worker); 3576ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 358c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 3597d19c5ceSTejun Heo 36097bd2347STejun Heo #define CREATE_TRACE_POINTS 36197bd2347STejun Heo #include <trace/events/workqueue.h> 36297bd2347STejun Heo 36368e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 36424acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 365f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 36624acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 3675bcab335STejun Heo 3685b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 36924acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 370f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 371f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 37224acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 3735b95e1afSLai Jiangshan 374f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 375f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 376f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 3777a62c2c8STejun Heo (pool)++) 3784ce62e9eSTejun Heo 37949e3cf44STejun Heo /** 38017116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 38117116969STejun Heo * @pool: iteration cursor 382611c92a0STejun Heo * @pi: integer used for iteration 383fa1b54e6STejun Heo * 38424acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 38568e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 38668e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 387fa1b54e6STejun Heo * 388fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 389fa1b54e6STejun Heo * ignored. 39017116969STejun Heo */ 391611c92a0STejun Heo #define for_each_pool(pool, pi) \ 392611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 39368e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 394fa1b54e6STejun Heo else 39517116969STejun Heo 39617116969STejun Heo /** 397822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 398822d8405STejun Heo * @worker: iteration cursor 399822d8405STejun Heo * @pool: worker_pool to iterate workers of 400822d8405STejun Heo * 4011258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 402822d8405STejun Heo * 403822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 404822d8405STejun Heo * ignored. 405822d8405STejun Heo */ 406da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 407da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4081258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 409822d8405STejun Heo else 410822d8405STejun Heo 411822d8405STejun Heo /** 41249e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 41349e3cf44STejun Heo * @pwq: iteration cursor 41449e3cf44STejun Heo * @wq: the target workqueue 41576af4d93STejun Heo * 41624acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 417794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 418794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 41976af4d93STejun Heo * 42076af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 42176af4d93STejun Heo * ignored. 42249e3cf44STejun Heo */ 42349e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 42449e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 4255a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 426f3421797STejun Heo 427dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 428dc186ad7SThomas Gleixner 429dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr; 430dc186ad7SThomas Gleixner 43199777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 43299777288SStanislaw Gruszka { 43399777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 43499777288SStanislaw Gruszka } 43599777288SStanislaw Gruszka 436b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 437b9fdac7fSDu, Changbin { 438b9fdac7fSDu, Changbin struct work_struct *work = addr; 439b9fdac7fSDu, Changbin 440b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 441b9fdac7fSDu, Changbin } 442b9fdac7fSDu, Changbin 443dc186ad7SThomas Gleixner /* 444dc186ad7SThomas Gleixner * fixup_init is called when: 445dc186ad7SThomas Gleixner * - an active object is initialized 446dc186ad7SThomas Gleixner */ 44702a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 448dc186ad7SThomas Gleixner { 449dc186ad7SThomas Gleixner struct work_struct *work = addr; 450dc186ad7SThomas Gleixner 451dc186ad7SThomas Gleixner switch (state) { 452dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 453dc186ad7SThomas Gleixner cancel_work_sync(work); 454dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 45502a982a6SDu, Changbin return true; 456dc186ad7SThomas Gleixner default: 45702a982a6SDu, Changbin return false; 458dc186ad7SThomas Gleixner } 459dc186ad7SThomas Gleixner } 460dc186ad7SThomas Gleixner 461dc186ad7SThomas Gleixner /* 462dc186ad7SThomas Gleixner * fixup_free is called when: 463dc186ad7SThomas Gleixner * - an active object is freed 464dc186ad7SThomas Gleixner */ 46502a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 466dc186ad7SThomas Gleixner { 467dc186ad7SThomas Gleixner struct work_struct *work = addr; 468dc186ad7SThomas Gleixner 469dc186ad7SThomas Gleixner switch (state) { 470dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 471dc186ad7SThomas Gleixner cancel_work_sync(work); 472dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 47302a982a6SDu, Changbin return true; 474dc186ad7SThomas Gleixner default: 47502a982a6SDu, Changbin return false; 476dc186ad7SThomas Gleixner } 477dc186ad7SThomas Gleixner } 478dc186ad7SThomas Gleixner 479dc186ad7SThomas Gleixner static struct debug_obj_descr work_debug_descr = { 480dc186ad7SThomas Gleixner .name = "work_struct", 48199777288SStanislaw Gruszka .debug_hint = work_debug_hint, 482b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 483dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 484dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 485dc186ad7SThomas Gleixner }; 486dc186ad7SThomas Gleixner 487dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 488dc186ad7SThomas Gleixner { 489dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 490dc186ad7SThomas Gleixner } 491dc186ad7SThomas Gleixner 492dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 493dc186ad7SThomas Gleixner { 494dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 495dc186ad7SThomas Gleixner } 496dc186ad7SThomas Gleixner 497dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 498dc186ad7SThomas Gleixner { 499dc186ad7SThomas Gleixner if (onstack) 500dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 501dc186ad7SThomas Gleixner else 502dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 503dc186ad7SThomas Gleixner } 504dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 505dc186ad7SThomas Gleixner 506dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 507dc186ad7SThomas Gleixner { 508dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 509dc186ad7SThomas Gleixner } 510dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 511dc186ad7SThomas Gleixner 512ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 513ea2e64f2SThomas Gleixner { 514ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 515ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 516ea2e64f2SThomas Gleixner } 517ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 518ea2e64f2SThomas Gleixner 519dc186ad7SThomas Gleixner #else 520dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 521dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 522dc186ad7SThomas Gleixner #endif 523dc186ad7SThomas Gleixner 5244e8b22bdSLi Bin /** 5254e8b22bdSLi Bin * worker_pool_assign_id - allocate ID and assing it to @pool 5264e8b22bdSLi Bin * @pool: the pool pointer of interest 5274e8b22bdSLi Bin * 5284e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5294e8b22bdSLi Bin * successfully, -errno on failure. 5304e8b22bdSLi Bin */ 5319daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5329daf9e67STejun Heo { 5339daf9e67STejun Heo int ret; 5349daf9e67STejun Heo 53568e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5365bcab335STejun Heo 5374e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5384e8b22bdSLi Bin GFP_KERNEL); 539229641a6STejun Heo if (ret >= 0) { 540e68035fbSTejun Heo pool->id = ret; 541229641a6STejun Heo return 0; 542229641a6STejun Heo } 5439daf9e67STejun Heo return ret; 5449daf9e67STejun Heo } 5459daf9e67STejun Heo 54676af4d93STejun Heo /** 547df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 548df2d5ae4STejun Heo * @wq: the target workqueue 549df2d5ae4STejun Heo * @node: the node ID 550df2d5ae4STejun Heo * 55124acfb71SThomas Gleixner * This must be called with any of wq_pool_mutex, wq->mutex or RCU 5525b95e1afSLai Jiangshan * read locked. 553df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 554df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 555d185af30SYacine Belkadi * 556d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 557df2d5ae4STejun Heo */ 558df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 559df2d5ae4STejun Heo int node) 560df2d5ae4STejun Heo { 5615b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 562d6e022f1STejun Heo 563d6e022f1STejun Heo /* 564d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 565d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 566d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 567d6e022f1STejun Heo * happens, this workaround can be removed. 568d6e022f1STejun Heo */ 569d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 570d6e022f1STejun Heo return wq->dfl_pwq; 571d6e022f1STejun Heo 572df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 573df2d5ae4STejun Heo } 574df2d5ae4STejun Heo 57573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 57673f53c4aSTejun Heo { 57773f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 57873f53c4aSTejun Heo } 57973f53c4aSTejun Heo 58073f53c4aSTejun Heo static int get_work_color(struct work_struct *work) 58173f53c4aSTejun Heo { 58273f53c4aSTejun Heo return (*work_data_bits(work) >> WORK_STRUCT_COLOR_SHIFT) & 58373f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 58473f53c4aSTejun Heo } 58573f53c4aSTejun Heo 58673f53c4aSTejun Heo static int work_next_color(int color) 58773f53c4aSTejun Heo { 58873f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 589a848e3b6SOleg Nesterov } 590a848e3b6SOleg Nesterov 5914594bf15SDavid Howells /* 592112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 593112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 5947c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 5957a22ad75STejun Heo * 596112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 597112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 598bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 599bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6007a22ad75STejun Heo * 601112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6027c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 603112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6047c3eed5cSTejun Heo * available only while the work item is queued. 605bbb68dfaSTejun Heo * 606bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 607bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 608bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 609bbb68dfaSTejun Heo * try to steal the PENDING bit. 6104594bf15SDavid Howells */ 6117a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6127a22ad75STejun Heo unsigned long flags) 6137a22ad75STejun Heo { 6146183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6157a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6167a22ad75STejun Heo } 6177a22ad75STejun Heo 618112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6194690c4abSTejun Heo unsigned long extra_flags) 620365970a1SDavid Howells { 621112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 622112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 623365970a1SDavid Howells } 624365970a1SDavid Howells 6254468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6264468a00fSLai Jiangshan int pool_id) 6274468a00fSLai Jiangshan { 6284468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6294468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6304468a00fSLai Jiangshan } 6314468a00fSLai Jiangshan 6327c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6337c3eed5cSTejun Heo int pool_id) 6344d707b9fSOleg Nesterov { 63523657bb1STejun Heo /* 63623657bb1STejun Heo * The following wmb is paired with the implied mb in 63723657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 63823657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 63923657bb1STejun Heo * owner. 64023657bb1STejun Heo */ 64123657bb1STejun Heo smp_wmb(); 6427c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 643346c09f8SRoman Pen /* 644346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 645346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 646346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 6478bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 648346c09f8SRoman Pen * the same @work. E.g. consider this case: 649346c09f8SRoman Pen * 650346c09f8SRoman Pen * CPU#0 CPU#1 651346c09f8SRoman Pen * ---------------------------- -------------------------------- 652346c09f8SRoman Pen * 653346c09f8SRoman Pen * 1 STORE event_indicated 654346c09f8SRoman Pen * 2 queue_work_on() { 655346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 656346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 657346c09f8SRoman Pen * 5 set_work_data() # clear bit 658346c09f8SRoman Pen * 6 smp_mb() 659346c09f8SRoman Pen * 7 work->current_func() { 660346c09f8SRoman Pen * 8 LOAD event_indicated 661346c09f8SRoman Pen * } 662346c09f8SRoman Pen * 663346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 664346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 665346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 666346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 667346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 668346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 669346c09f8SRoman Pen * before actual STORE. 670346c09f8SRoman Pen */ 671346c09f8SRoman Pen smp_mb(); 6724d707b9fSOleg Nesterov } 6734d707b9fSOleg Nesterov 6747a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 675365970a1SDavid Howells { 6767c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 6777c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 6787a22ad75STejun Heo } 6797a22ad75STejun Heo 680112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 6817a22ad75STejun Heo { 682e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 6837a22ad75STejun Heo 684112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 685e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 686e120153dSTejun Heo else 687e120153dSTejun Heo return NULL; 6887a22ad75STejun Heo } 6897a22ad75STejun Heo 6907c3eed5cSTejun Heo /** 6917c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 6927c3eed5cSTejun Heo * @work: the work item of interest 6937c3eed5cSTejun Heo * 69468e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 69524acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 69624acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 697fa1b54e6STejun Heo * 698fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 699fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 700fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 701fa1b54e6STejun Heo * returned pool is and stays online. 702d185af30SYacine Belkadi * 703d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7047c3eed5cSTejun Heo */ 7057c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7067a22ad75STejun Heo { 707e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7087c3eed5cSTejun Heo int pool_id; 7097a22ad75STejun Heo 71068e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 711fa1b54e6STejun Heo 712112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 713112202d9STejun Heo return ((struct pool_workqueue *) 7147c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7157a22ad75STejun Heo 7167c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7177c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7187a22ad75STejun Heo return NULL; 7197a22ad75STejun Heo 720fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7217c3eed5cSTejun Heo } 7227c3eed5cSTejun Heo 7237c3eed5cSTejun Heo /** 7247c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7257c3eed5cSTejun Heo * @work: the work item of interest 7267c3eed5cSTejun Heo * 727d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7287c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7297c3eed5cSTejun Heo */ 7307c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7317c3eed5cSTejun Heo { 73254d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7337c3eed5cSTejun Heo 734112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 735112202d9STejun Heo return ((struct pool_workqueue *) 73654d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 73754d5b7d0SLai Jiangshan 73854d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7397c3eed5cSTejun Heo } 7407c3eed5cSTejun Heo 741bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 742bbb68dfaSTejun Heo { 7437c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 744bbb68dfaSTejun Heo 7457c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7467c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 747bbb68dfaSTejun Heo } 748bbb68dfaSTejun Heo 749bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 750bbb68dfaSTejun Heo { 751bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 752bbb68dfaSTejun Heo 753112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 754bbb68dfaSTejun Heo } 755bbb68dfaSTejun Heo 756e22bee78STejun Heo /* 7573270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7583270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 759d565ed63STejun Heo * they're being called with pool->lock held. 760e22bee78STejun Heo */ 761e22bee78STejun Heo 76263d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 763649027d7STejun Heo { 764e19e397aSTejun Heo return !atomic_read(&pool->nr_running); 765649027d7STejun Heo } 766649027d7STejun Heo 767e22bee78STejun Heo /* 768e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 769e22bee78STejun Heo * running workers. 770974271c4STejun Heo * 771974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 772706026c2STejun Heo * function will always return %true for unbound pools as long as the 773974271c4STejun Heo * worklist isn't empty. 774e22bee78STejun Heo */ 77563d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 776e22bee78STejun Heo { 77763d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 778e22bee78STejun Heo } 779e22bee78STejun Heo 780e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 78163d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 782e22bee78STejun Heo { 78363d95a91STejun Heo return pool->nr_idle; 784e22bee78STejun Heo } 785e22bee78STejun Heo 786e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 78763d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 788e22bee78STejun Heo { 789e19e397aSTejun Heo return !list_empty(&pool->worklist) && 790e19e397aSTejun Heo atomic_read(&pool->nr_running) <= 1; 791e22bee78STejun Heo } 792e22bee78STejun Heo 793e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 79463d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 795e22bee78STejun Heo { 79663d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 797e22bee78STejun Heo } 798e22bee78STejun Heo 799e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 80063d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 801e22bee78STejun Heo { 802692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 80363d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 80463d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 805e22bee78STejun Heo 806e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 807e22bee78STejun Heo } 808e22bee78STejun Heo 809e22bee78STejun Heo /* 810e22bee78STejun Heo * Wake up functions. 811e22bee78STejun Heo */ 812e22bee78STejun Heo 8131037de36SLai Jiangshan /* Return the first idle worker. Safe with preemption disabled */ 8141037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8157e11629dSTejun Heo { 81663d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8177e11629dSTejun Heo return NULL; 8187e11629dSTejun Heo 81963d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8207e11629dSTejun Heo } 8217e11629dSTejun Heo 8227e11629dSTejun Heo /** 8237e11629dSTejun Heo * wake_up_worker - wake up an idle worker 82463d95a91STejun Heo * @pool: worker pool to wake worker from 8257e11629dSTejun Heo * 82663d95a91STejun Heo * Wake up the first idle worker of @pool. 8277e11629dSTejun Heo * 8287e11629dSTejun Heo * CONTEXT: 829d565ed63STejun Heo * spin_lock_irq(pool->lock). 8307e11629dSTejun Heo */ 83163d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8327e11629dSTejun Heo { 8331037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8347e11629dSTejun Heo 8357e11629dSTejun Heo if (likely(worker)) 8367e11629dSTejun Heo wake_up_process(worker->task); 8377e11629dSTejun Heo } 8387e11629dSTejun Heo 8394690c4abSTejun Heo /** 8406d25be57SThomas Gleixner * wq_worker_running - a worker is running again 841e22bee78STejun Heo * @task: task waking up 842e22bee78STejun Heo * 8436d25be57SThomas Gleixner * This function is called when a worker returns from schedule() 844e22bee78STejun Heo */ 8456d25be57SThomas Gleixner void wq_worker_running(struct task_struct *task) 846e22bee78STejun Heo { 847e22bee78STejun Heo struct worker *worker = kthread_data(task); 848e22bee78STejun Heo 8496d25be57SThomas Gleixner if (!worker->sleeping) 8506d25be57SThomas Gleixner return; 8516d25be57SThomas Gleixner if (!(worker->flags & WORKER_NOT_RUNNING)) 852e19e397aSTejun Heo atomic_inc(&worker->pool->nr_running); 8536d25be57SThomas Gleixner worker->sleeping = 0; 85436576000SJoonsoo Kim } 855e22bee78STejun Heo 856e22bee78STejun Heo /** 857e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 858e22bee78STejun Heo * @task: task going to sleep 859e22bee78STejun Heo * 8606d25be57SThomas Gleixner * This function is called from schedule() when a busy worker is 86162849a96SSebastian Andrzej Siewior * going to sleep. Preemption needs to be disabled to protect ->sleeping 86262849a96SSebastian Andrzej Siewior * assignment. 863e22bee78STejun Heo */ 8646d25be57SThomas Gleixner void wq_worker_sleeping(struct task_struct *task) 865e22bee78STejun Heo { 8666d25be57SThomas Gleixner struct worker *next, *worker = kthread_data(task); 867111c225aSTejun Heo struct worker_pool *pool; 868e22bee78STejun Heo 869111c225aSTejun Heo /* 870111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 871111c225aSTejun Heo * workers, also reach here, let's not access anything before 872111c225aSTejun Heo * checking NOT_RUNNING. 873111c225aSTejun Heo */ 8742d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 8756d25be57SThomas Gleixner return; 876e22bee78STejun Heo 877111c225aSTejun Heo pool = worker->pool; 878111c225aSTejun Heo 87962849a96SSebastian Andrzej Siewior /* Return if preempted before wq_worker_running() was reached */ 88062849a96SSebastian Andrzej Siewior if (worker->sleeping) 8816d25be57SThomas Gleixner return; 8826d25be57SThomas Gleixner 8836d25be57SThomas Gleixner worker->sleeping = 1; 8846d25be57SThomas Gleixner spin_lock_irq(&pool->lock); 885e22bee78STejun Heo 886e22bee78STejun Heo /* 887e22bee78STejun Heo * The counterpart of the following dec_and_test, implied mb, 888e22bee78STejun Heo * worklist not empty test sequence is in insert_work(). 889e22bee78STejun Heo * Please read comment there. 890e22bee78STejun Heo * 891628c78e7STejun Heo * NOT_RUNNING is clear. This means that we're bound to and 892628c78e7STejun Heo * running on the local cpu w/ rq lock held and preemption 893628c78e7STejun Heo * disabled, which in turn means that none else could be 894d565ed63STejun Heo * manipulating idle_list, so dereferencing idle_list without pool 895628c78e7STejun Heo * lock is safe. 896e22bee78STejun Heo */ 897e19e397aSTejun Heo if (atomic_dec_and_test(&pool->nr_running) && 8986d25be57SThomas Gleixner !list_empty(&pool->worklist)) { 8996d25be57SThomas Gleixner next = first_idle_worker(pool); 9006d25be57SThomas Gleixner if (next) 9016d25be57SThomas Gleixner wake_up_process(next->task); 9026d25be57SThomas Gleixner } 9036d25be57SThomas Gleixner spin_unlock_irq(&pool->lock); 904e22bee78STejun Heo } 905e22bee78STejun Heo 906e22bee78STejun Heo /** 9071b69ac6bSJohannes Weiner * wq_worker_last_func - retrieve worker's last work function 9088194fe94SBart Van Assche * @task: Task to retrieve last work function of. 9091b69ac6bSJohannes Weiner * 9101b69ac6bSJohannes Weiner * Determine the last function a worker executed. This is called from 9111b69ac6bSJohannes Weiner * the scheduler to get a worker's last known identity. 9121b69ac6bSJohannes Weiner * 9131b69ac6bSJohannes Weiner * CONTEXT: 9141b69ac6bSJohannes Weiner * spin_lock_irq(rq->lock) 9151b69ac6bSJohannes Weiner * 9164b047002SJohannes Weiner * This function is called during schedule() when a kworker is going 9174b047002SJohannes Weiner * to sleep. It's used by psi to identify aggregation workers during 9184b047002SJohannes Weiner * dequeuing, to allow periodic aggregation to shut-off when that 9194b047002SJohannes Weiner * worker is the last task in the system or cgroup to go to sleep. 9204b047002SJohannes Weiner * 9214b047002SJohannes Weiner * As this function doesn't involve any workqueue-related locking, it 9224b047002SJohannes Weiner * only returns stable values when called from inside the scheduler's 9234b047002SJohannes Weiner * queuing and dequeuing paths, when @task, which must be a kworker, 9244b047002SJohannes Weiner * is guaranteed to not be processing any works. 9254b047002SJohannes Weiner * 9261b69ac6bSJohannes Weiner * Return: 9271b69ac6bSJohannes Weiner * The last work function %current executed as a worker, NULL if it 9281b69ac6bSJohannes Weiner * hasn't executed any work yet. 9291b69ac6bSJohannes Weiner */ 9301b69ac6bSJohannes Weiner work_func_t wq_worker_last_func(struct task_struct *task) 9311b69ac6bSJohannes Weiner { 9321b69ac6bSJohannes Weiner struct worker *worker = kthread_data(task); 9331b69ac6bSJohannes Weiner 9341b69ac6bSJohannes Weiner return worker->last_func; 9351b69ac6bSJohannes Weiner } 9361b69ac6bSJohannes Weiner 9371b69ac6bSJohannes Weiner /** 938e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 939cb444766STejun Heo * @worker: self 940d302f017STejun Heo * @flags: flags to set 941d302f017STejun Heo * 942228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 943d302f017STejun Heo * 944cb444766STejun Heo * CONTEXT: 945d565ed63STejun Heo * spin_lock_irq(pool->lock) 946d302f017STejun Heo */ 947228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 948d302f017STejun Heo { 949bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 950e22bee78STejun Heo 951cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 952cb444766STejun Heo 953228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 954e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 955e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 956e19e397aSTejun Heo atomic_dec(&pool->nr_running); 957e22bee78STejun Heo } 958e22bee78STejun Heo 959d302f017STejun Heo worker->flags |= flags; 960d302f017STejun Heo } 961d302f017STejun Heo 962d302f017STejun Heo /** 963e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 964cb444766STejun Heo * @worker: self 965d302f017STejun Heo * @flags: flags to clear 966d302f017STejun Heo * 967e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 968d302f017STejun Heo * 969cb444766STejun Heo * CONTEXT: 970d565ed63STejun Heo * spin_lock_irq(pool->lock) 971d302f017STejun Heo */ 972d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 973d302f017STejun Heo { 97463d95a91STejun Heo struct worker_pool *pool = worker->pool; 975e22bee78STejun Heo unsigned int oflags = worker->flags; 976e22bee78STejun Heo 977cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 978cb444766STejun Heo 979d302f017STejun Heo worker->flags &= ~flags; 980e22bee78STejun Heo 98142c025f3STejun Heo /* 98242c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 98342c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 98442c025f3STejun Heo * of multiple flags, not a single flag. 98542c025f3STejun Heo */ 986e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 987e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 988e19e397aSTejun Heo atomic_inc(&pool->nr_running); 989d302f017STejun Heo } 990d302f017STejun Heo 991d302f017STejun Heo /** 9928cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 993c9e7cf27STejun Heo * @pool: pool of interest 9948cca0eeaSTejun Heo * @work: work to find worker for 9958cca0eeaSTejun Heo * 996c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 997c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 998a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 999a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 1000a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 1001a2c1c57bSTejun Heo * being executed. 1002a2c1c57bSTejun Heo * 1003a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 1004a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 1005a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 1006a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 1007a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 1008a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 1009a2c1c57bSTejun Heo * 1010c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 1011c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 1012c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 1013c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1014c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1015c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 10168cca0eeaSTejun Heo * 10178cca0eeaSTejun Heo * CONTEXT: 1018d565ed63STejun Heo * spin_lock_irq(pool->lock). 10198cca0eeaSTejun Heo * 1020d185af30SYacine Belkadi * Return: 1021d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 10228cca0eeaSTejun Heo * otherwise. 10238cca0eeaSTejun Heo */ 1024c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 10258cca0eeaSTejun Heo struct work_struct *work) 10268cca0eeaSTejun Heo { 102742f8570fSSasha Levin struct worker *worker; 102842f8570fSSasha Levin 1029b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1030a2c1c57bSTejun Heo (unsigned long)work) 1031a2c1c57bSTejun Heo if (worker->current_work == work && 1032a2c1c57bSTejun Heo worker->current_func == work->func) 103342f8570fSSasha Levin return worker; 103442f8570fSSasha Levin 103542f8570fSSasha Levin return NULL; 10368cca0eeaSTejun Heo } 10378cca0eeaSTejun Heo 10388cca0eeaSTejun Heo /** 1039bf4ede01STejun Heo * move_linked_works - move linked works to a list 1040bf4ede01STejun Heo * @work: start of series of works to be scheduled 1041bf4ede01STejun Heo * @head: target list to append @work to 1042402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1043bf4ede01STejun Heo * 1044bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1045bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1046bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1047bf4ede01STejun Heo * 1048bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1049bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1050bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1051bf4ede01STejun Heo * 1052bf4ede01STejun Heo * CONTEXT: 1053d565ed63STejun Heo * spin_lock_irq(pool->lock). 1054bf4ede01STejun Heo */ 1055bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1056bf4ede01STejun Heo struct work_struct **nextp) 1057bf4ede01STejun Heo { 1058bf4ede01STejun Heo struct work_struct *n; 1059bf4ede01STejun Heo 1060bf4ede01STejun Heo /* 1061bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1062bf4ede01STejun Heo * use NULL for list head. 1063bf4ede01STejun Heo */ 1064bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1065bf4ede01STejun Heo list_move_tail(&work->entry, head); 1066bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1067bf4ede01STejun Heo break; 1068bf4ede01STejun Heo } 1069bf4ede01STejun Heo 1070bf4ede01STejun Heo /* 1071bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1072bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1073bf4ede01STejun Heo * needs to be updated. 1074bf4ede01STejun Heo */ 1075bf4ede01STejun Heo if (nextp) 1076bf4ede01STejun Heo *nextp = n; 1077bf4ede01STejun Heo } 1078bf4ede01STejun Heo 10798864b4e5STejun Heo /** 10808864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 10818864b4e5STejun Heo * @pwq: pool_workqueue to get 10828864b4e5STejun Heo * 10838864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 10848864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 10858864b4e5STejun Heo */ 10868864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 10878864b4e5STejun Heo { 10888864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 10898864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 10908864b4e5STejun Heo pwq->refcnt++; 10918864b4e5STejun Heo } 10928864b4e5STejun Heo 10938864b4e5STejun Heo /** 10948864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 10958864b4e5STejun Heo * @pwq: pool_workqueue to put 10968864b4e5STejun Heo * 10978864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 10988864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 10998864b4e5STejun Heo */ 11008864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 11018864b4e5STejun Heo { 11028864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11038864b4e5STejun Heo if (likely(--pwq->refcnt)) 11048864b4e5STejun Heo return; 11058864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 11068864b4e5STejun Heo return; 11078864b4e5STejun Heo /* 11088864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 11098864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 11108864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 11118864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 11128864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 11138864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 11148864b4e5STejun Heo */ 11158864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 11168864b4e5STejun Heo } 11178864b4e5STejun Heo 1118dce90d47STejun Heo /** 1119dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1120dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1121dce90d47STejun Heo * 1122dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1123dce90d47STejun Heo */ 1124dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1125dce90d47STejun Heo { 1126dce90d47STejun Heo if (pwq) { 1127dce90d47STejun Heo /* 112824acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1129dce90d47STejun Heo * following lock operations are safe. 1130dce90d47STejun Heo */ 1131dce90d47STejun Heo spin_lock_irq(&pwq->pool->lock); 1132dce90d47STejun Heo put_pwq(pwq); 1133dce90d47STejun Heo spin_unlock_irq(&pwq->pool->lock); 1134dce90d47STejun Heo } 1135dce90d47STejun Heo } 1136dce90d47STejun Heo 1137112202d9STejun Heo static void pwq_activate_delayed_work(struct work_struct *work) 1138bf4ede01STejun Heo { 1139112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1140bf4ede01STejun Heo 1141bf4ede01STejun Heo trace_workqueue_activate_work(work); 114282607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 114382607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1144112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1145bf4ede01STejun Heo __clear_bit(WORK_STRUCT_DELAYED_BIT, work_data_bits(work)); 1146112202d9STejun Heo pwq->nr_active++; 1147bf4ede01STejun Heo } 1148bf4ede01STejun Heo 1149112202d9STejun Heo static void pwq_activate_first_delayed(struct pool_workqueue *pwq) 11503aa62497SLai Jiangshan { 1151112202d9STejun Heo struct work_struct *work = list_first_entry(&pwq->delayed_works, 11523aa62497SLai Jiangshan struct work_struct, entry); 11533aa62497SLai Jiangshan 1154112202d9STejun Heo pwq_activate_delayed_work(work); 11553aa62497SLai Jiangshan } 11563aa62497SLai Jiangshan 1157bf4ede01STejun Heo /** 1158112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1159112202d9STejun Heo * @pwq: pwq of interest 1160bf4ede01STejun Heo * @color: color of work which left the queue 1161bf4ede01STejun Heo * 1162bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1163112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1164bf4ede01STejun Heo * 1165bf4ede01STejun Heo * CONTEXT: 1166d565ed63STejun Heo * spin_lock_irq(pool->lock). 1167bf4ede01STejun Heo */ 1168112202d9STejun Heo static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, int color) 1169bf4ede01STejun Heo { 11708864b4e5STejun Heo /* uncolored work items don't participate in flushing or nr_active */ 1171bf4ede01STejun Heo if (color == WORK_NO_COLOR) 11728864b4e5STejun Heo goto out_put; 1173bf4ede01STejun Heo 1174112202d9STejun Heo pwq->nr_in_flight[color]--; 1175bf4ede01STejun Heo 1176112202d9STejun Heo pwq->nr_active--; 1177112202d9STejun Heo if (!list_empty(&pwq->delayed_works)) { 1178bf4ede01STejun Heo /* one down, submit a delayed one */ 1179112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1180112202d9STejun Heo pwq_activate_first_delayed(pwq); 1181bf4ede01STejun Heo } 1182bf4ede01STejun Heo 1183bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1184112202d9STejun Heo if (likely(pwq->flush_color != color)) 11858864b4e5STejun Heo goto out_put; 1186bf4ede01STejun Heo 1187bf4ede01STejun Heo /* are there still in-flight works? */ 1188112202d9STejun Heo if (pwq->nr_in_flight[color]) 11898864b4e5STejun Heo goto out_put; 1190bf4ede01STejun Heo 1191112202d9STejun Heo /* this pwq is done, clear flush_color */ 1192112202d9STejun Heo pwq->flush_color = -1; 1193bf4ede01STejun Heo 1194bf4ede01STejun Heo /* 1195112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1196bf4ede01STejun Heo * will handle the rest. 1197bf4ede01STejun Heo */ 1198112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1199112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 12008864b4e5STejun Heo out_put: 12018864b4e5STejun Heo put_pwq(pwq); 1202bf4ede01STejun Heo } 1203bf4ede01STejun Heo 120436e227d2STejun Heo /** 1205bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 120636e227d2STejun Heo * @work: work item to steal 120736e227d2STejun Heo * @is_dwork: @work is a delayed_work 1208bbb68dfaSTejun Heo * @flags: place to store irq state 120936e227d2STejun Heo * 121036e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1211d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 121236e227d2STejun Heo * 1213d185af30SYacine Belkadi * Return: 121436e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 121536e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 121636e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1217bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1218bbb68dfaSTejun Heo * for arbitrarily long 121936e227d2STejun Heo * 1220d185af30SYacine Belkadi * Note: 1221bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1222e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1223e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1224e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1225bbb68dfaSTejun Heo * 1226bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1227bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1228bbb68dfaSTejun Heo * 1229e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1230bf4ede01STejun Heo */ 1231bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1232bbb68dfaSTejun Heo unsigned long *flags) 1233bf4ede01STejun Heo { 1234d565ed63STejun Heo struct worker_pool *pool; 1235112202d9STejun Heo struct pool_workqueue *pwq; 1236bf4ede01STejun Heo 1237bbb68dfaSTejun Heo local_irq_save(*flags); 1238bbb68dfaSTejun Heo 123936e227d2STejun Heo /* try to steal the timer if it exists */ 124036e227d2STejun Heo if (is_dwork) { 124136e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 124236e227d2STejun Heo 1243e0aecdd8STejun Heo /* 1244e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1245e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1246e0aecdd8STejun Heo * running on the local CPU. 1247e0aecdd8STejun Heo */ 124836e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 124936e227d2STejun Heo return 1; 125036e227d2STejun Heo } 125136e227d2STejun Heo 125236e227d2STejun Heo /* try to claim PENDING the normal way */ 1253bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1254bf4ede01STejun Heo return 0; 1255bf4ede01STejun Heo 125624acfb71SThomas Gleixner rcu_read_lock(); 1257bf4ede01STejun Heo /* 1258bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1259bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1260bf4ede01STejun Heo */ 1261d565ed63STejun Heo pool = get_work_pool(work); 1262d565ed63STejun Heo if (!pool) 1263bbb68dfaSTejun Heo goto fail; 1264bf4ede01STejun Heo 1265d565ed63STejun Heo spin_lock(&pool->lock); 1266bf4ede01STejun Heo /* 1267112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1268112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1269112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1270112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1271112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 12720b3dae68SLai Jiangshan * item is currently queued on that pool. 1273bf4ede01STejun Heo */ 1274112202d9STejun Heo pwq = get_work_pwq(work); 1275112202d9STejun Heo if (pwq && pwq->pool == pool) { 1276bf4ede01STejun Heo debug_work_deactivate(work); 12773aa62497SLai Jiangshan 12783aa62497SLai Jiangshan /* 127916062836STejun Heo * A delayed work item cannot be grabbed directly because 128016062836STejun Heo * it might have linked NO_COLOR work items which, if left 1281112202d9STejun Heo * on the delayed_list, will confuse pwq->nr_active 128216062836STejun Heo * management later on and cause stall. Make sure the work 128316062836STejun Heo * item is activated before grabbing. 12843aa62497SLai Jiangshan */ 12853aa62497SLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_DELAYED) 1286112202d9STejun Heo pwq_activate_delayed_work(work); 12873aa62497SLai Jiangshan 1288bf4ede01STejun Heo list_del_init(&work->entry); 12899c34a704SLai Jiangshan pwq_dec_nr_in_flight(pwq, get_work_color(work)); 129036e227d2STejun Heo 1291112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 12924468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 12934468a00fSLai Jiangshan 1294d565ed63STejun Heo spin_unlock(&pool->lock); 129524acfb71SThomas Gleixner rcu_read_unlock(); 129636e227d2STejun Heo return 1; 1297bf4ede01STejun Heo } 1298d565ed63STejun Heo spin_unlock(&pool->lock); 1299bbb68dfaSTejun Heo fail: 130024acfb71SThomas Gleixner rcu_read_unlock(); 1301bbb68dfaSTejun Heo local_irq_restore(*flags); 1302bbb68dfaSTejun Heo if (work_is_canceling(work)) 1303bbb68dfaSTejun Heo return -ENOENT; 1304bbb68dfaSTejun Heo cpu_relax(); 130536e227d2STejun Heo return -EAGAIN; 1306bf4ede01STejun Heo } 1307bf4ede01STejun Heo 1308bf4ede01STejun Heo /** 1309706026c2STejun Heo * insert_work - insert a work into a pool 1310112202d9STejun Heo * @pwq: pwq @work belongs to 13114690c4abSTejun Heo * @work: work to insert 13124690c4abSTejun Heo * @head: insertion point 13134690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 13144690c4abSTejun Heo * 1315112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1316706026c2STejun Heo * work_struct flags. 13174690c4abSTejun Heo * 13184690c4abSTejun Heo * CONTEXT: 1319d565ed63STejun Heo * spin_lock_irq(pool->lock). 1320365970a1SDavid Howells */ 1321112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1322112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1323b89deed3SOleg Nesterov { 1324112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1325e1d8aa9fSFrederic Weisbecker 13264690c4abSTejun Heo /* we own @work, set data and link */ 1327112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 13281a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 13298864b4e5STejun Heo get_pwq(pwq); 1330e22bee78STejun Heo 1331e22bee78STejun Heo /* 1332c5aa87bbSTejun Heo * Ensure either wq_worker_sleeping() sees the above 1333c5aa87bbSTejun Heo * list_add_tail() or we see zero nr_running to avoid workers lying 1334c5aa87bbSTejun Heo * around lazily while there are works to be processed. 1335e22bee78STejun Heo */ 1336e22bee78STejun Heo smp_mb(); 1337e22bee78STejun Heo 133863d95a91STejun Heo if (__need_more_worker(pool)) 133963d95a91STejun Heo wake_up_worker(pool); 1340b89deed3SOleg Nesterov } 1341b89deed3SOleg Nesterov 1342c8efcc25STejun Heo /* 1343c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 13448d03ecfeSTejun Heo * same workqueue. 1345c8efcc25STejun Heo */ 1346c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1347c8efcc25STejun Heo { 1348c8efcc25STejun Heo struct worker *worker; 1349c8efcc25STejun Heo 13508d03ecfeSTejun Heo worker = current_wq_worker(); 1351c8efcc25STejun Heo /* 1352bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 13538d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1354c8efcc25STejun Heo */ 1355112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1356c8efcc25STejun Heo } 1357c8efcc25STejun Heo 1358ef557180SMike Galbraith /* 1359ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1360ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1361ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1362ef557180SMike Galbraith */ 1363ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1364ef557180SMike Galbraith { 1365f303fccbSTejun Heo static bool printed_dbg_warning; 1366ef557180SMike Galbraith int new_cpu; 1367ef557180SMike Galbraith 1368f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1369ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1370ef557180SMike Galbraith return cpu; 1371f303fccbSTejun Heo } else if (!printed_dbg_warning) { 1372f303fccbSTejun Heo pr_warn("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1373f303fccbSTejun Heo printed_dbg_warning = true; 1374f303fccbSTejun Heo } 1375f303fccbSTejun Heo 1376ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1377ef557180SMike Galbraith return cpu; 1378ef557180SMike Galbraith 1379ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1380ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1381ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1382ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1383ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1384ef557180SMike Galbraith return cpu; 1385ef557180SMike Galbraith } 1386ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1387ef557180SMike Galbraith 1388ef557180SMike Galbraith return new_cpu; 1389ef557180SMike Galbraith } 1390ef557180SMike Galbraith 1391d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 13921da177e4SLinus Torvalds struct work_struct *work) 13931da177e4SLinus Torvalds { 1394112202d9STejun Heo struct pool_workqueue *pwq; 1395c9178087STejun Heo struct worker_pool *last_pool; 13961e19ffc6STejun Heo struct list_head *worklist; 13978a2e8e5dSTejun Heo unsigned int work_flags; 1398b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 13998930cabaSTejun Heo 14008930cabaSTejun Heo /* 14018930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 14028930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 14038930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 14048930cabaSTejun Heo * happen with IRQ disabled. 14058930cabaSTejun Heo */ 14068e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 14071da177e4SLinus Torvalds 1408dc186ad7SThomas Gleixner debug_work_activate(work); 14091e19ffc6STejun Heo 14109ef28a73SLi Bin /* if draining, only works from the same workqueue are allowed */ 1411618b01ebSTejun Heo if (unlikely(wq->flags & __WQ_DRAINING) && 1412c8efcc25STejun Heo WARN_ON_ONCE(!is_chained_work(wq))) 1413e41e704bSTejun Heo return; 141424acfb71SThomas Gleixner rcu_read_lock(); 14159e8cd2f5STejun Heo retry: 1416aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1417aa202f1fSHillf Danton if (wq->flags & WQ_UNBOUND) { 1418df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1419ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1420df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1421aa202f1fSHillf Danton } else { 1422aa202f1fSHillf Danton if (req_cpu == WORK_CPU_UNBOUND) 1423aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1424aa202f1fSHillf Danton pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1425aa202f1fSHillf Danton } 1426f3421797STejun Heo 142718aa9effSTejun Heo /* 1428c9178087STejun Heo * If @work was previously on a different pool, it might still be 1429c9178087STejun Heo * running there, in which case the work needs to be queued on that 1430c9178087STejun Heo * pool to guarantee non-reentrancy. 143118aa9effSTejun Heo */ 1432c9e7cf27STejun Heo last_pool = get_work_pool(work); 1433112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 143418aa9effSTejun Heo struct worker *worker; 143518aa9effSTejun Heo 1436d565ed63STejun Heo spin_lock(&last_pool->lock); 143718aa9effSTejun Heo 1438c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 143918aa9effSTejun Heo 1440112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1441c9178087STejun Heo pwq = worker->current_pwq; 14428594fadeSLai Jiangshan } else { 144318aa9effSTejun Heo /* meh... not running there, queue here */ 1444d565ed63STejun Heo spin_unlock(&last_pool->lock); 1445112202d9STejun Heo spin_lock(&pwq->pool->lock); 144618aa9effSTejun Heo } 14478930cabaSTejun Heo } else { 1448112202d9STejun Heo spin_lock(&pwq->pool->lock); 14498930cabaSTejun Heo } 1450502ca9d8STejun Heo 14519e8cd2f5STejun Heo /* 14529e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 14539e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 14549e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1455df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1456df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 14579e8cd2f5STejun Heo * make forward-progress. 14589e8cd2f5STejun Heo */ 14599e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 14609e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 14619e8cd2f5STejun Heo spin_unlock(&pwq->pool->lock); 14629e8cd2f5STejun Heo cpu_relax(); 14639e8cd2f5STejun Heo goto retry; 14649e8cd2f5STejun Heo } 14659e8cd2f5STejun Heo /* oops */ 14669e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 14679e8cd2f5STejun Heo wq->name, cpu); 14689e8cd2f5STejun Heo } 14699e8cd2f5STejun Heo 1470112202d9STejun Heo /* pwq determined, queue */ 1471112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1472502ca9d8STejun Heo 147324acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 147424acfb71SThomas Gleixner goto out; 14751e19ffc6STejun Heo 1476112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1477112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 14781e19ffc6STejun Heo 1479112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1480cdadf009STejun Heo trace_workqueue_activate_work(work); 1481112202d9STejun Heo pwq->nr_active++; 1482112202d9STejun Heo worklist = &pwq->pool->worklist; 148382607adcSTejun Heo if (list_empty(worklist)) 148482607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 14858a2e8e5dSTejun Heo } else { 14868a2e8e5dSTejun Heo work_flags |= WORK_STRUCT_DELAYED; 1487112202d9STejun Heo worklist = &pwq->delayed_works; 14888a2e8e5dSTejun Heo } 14891e19ffc6STejun Heo 1490112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 14911e19ffc6STejun Heo 149224acfb71SThomas Gleixner out: 1493112202d9STejun Heo spin_unlock(&pwq->pool->lock); 149424acfb71SThomas Gleixner rcu_read_unlock(); 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14970fcb78c2SRolf Eike Beer /** 1498c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1499c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1500c1a220e7SZhang Rui * @wq: workqueue to use 1501c1a220e7SZhang Rui * @work: work to queue 1502c1a220e7SZhang Rui * 1503c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1504c1a220e7SZhang Rui * can't go away. 1505d185af30SYacine Belkadi * 1506d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1507c1a220e7SZhang Rui */ 1508d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1509d4283e93STejun Heo struct work_struct *work) 1510c1a220e7SZhang Rui { 1511d4283e93STejun Heo bool ret = false; 15128930cabaSTejun Heo unsigned long flags; 15138930cabaSTejun Heo 15148930cabaSTejun Heo local_irq_save(flags); 1515c1a220e7SZhang Rui 151622df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15174690c4abSTejun Heo __queue_work(cpu, wq, work); 1518d4283e93STejun Heo ret = true; 1519c1a220e7SZhang Rui } 15208930cabaSTejun Heo 15218930cabaSTejun Heo local_irq_restore(flags); 1522c1a220e7SZhang Rui return ret; 1523c1a220e7SZhang Rui } 1524ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1525c1a220e7SZhang Rui 15268204e0c1SAlexander Duyck /** 15278204e0c1SAlexander Duyck * workqueue_select_cpu_near - Select a CPU based on NUMA node 15288204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 15298204e0c1SAlexander Duyck * 15308204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 15318204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 15328204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 15338204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 15348204e0c1SAlexander Duyck */ 15358204e0c1SAlexander Duyck static int workqueue_select_cpu_near(int node) 15368204e0c1SAlexander Duyck { 15378204e0c1SAlexander Duyck int cpu; 15388204e0c1SAlexander Duyck 15398204e0c1SAlexander Duyck /* No point in doing this if NUMA isn't enabled for workqueues */ 15408204e0c1SAlexander Duyck if (!wq_numa_enabled) 15418204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 15428204e0c1SAlexander Duyck 15438204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 15448204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 15458204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 15468204e0c1SAlexander Duyck 15478204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 15488204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 15498204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 15508204e0c1SAlexander Duyck return cpu; 15518204e0c1SAlexander Duyck 15528204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 15538204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 15548204e0c1SAlexander Duyck 15558204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 15568204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 15578204e0c1SAlexander Duyck } 15588204e0c1SAlexander Duyck 15598204e0c1SAlexander Duyck /** 15608204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 15618204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 15628204e0c1SAlexander Duyck * @wq: workqueue to use 15638204e0c1SAlexander Duyck * @work: work to queue 15648204e0c1SAlexander Duyck * 15658204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 15668204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 15678204e0c1SAlexander Duyck * NUMA node. 15688204e0c1SAlexander Duyck * 15698204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 15708204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 15718204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 15728204e0c1SAlexander Duyck * 15738204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 15748204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 15758204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 15768204e0c1SAlexander Duyck * 15778204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 15788204e0c1SAlexander Duyck */ 15798204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 15808204e0c1SAlexander Duyck struct work_struct *work) 15818204e0c1SAlexander Duyck { 15828204e0c1SAlexander Duyck unsigned long flags; 15838204e0c1SAlexander Duyck bool ret = false; 15848204e0c1SAlexander Duyck 15858204e0c1SAlexander Duyck /* 15868204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 15878204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 15888204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 15898204e0c1SAlexander Duyck * 15908204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 15918204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 15928204e0c1SAlexander Duyck * some round robin type logic. 15938204e0c1SAlexander Duyck */ 15948204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 15958204e0c1SAlexander Duyck 15968204e0c1SAlexander Duyck local_irq_save(flags); 15978204e0c1SAlexander Duyck 15988204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15998204e0c1SAlexander Duyck int cpu = workqueue_select_cpu_near(node); 16008204e0c1SAlexander Duyck 16018204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 16028204e0c1SAlexander Duyck ret = true; 16038204e0c1SAlexander Duyck } 16048204e0c1SAlexander Duyck 16058204e0c1SAlexander Duyck local_irq_restore(flags); 16068204e0c1SAlexander Duyck return ret; 16078204e0c1SAlexander Duyck } 16088204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 16098204e0c1SAlexander Duyck 16108c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 16111da177e4SLinus Torvalds { 16128c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 16131da177e4SLinus Torvalds 1614e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 161560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 16161da177e4SLinus Torvalds } 16171438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 16181da177e4SLinus Torvalds 16197beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 162052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 16211da177e4SLinus Torvalds { 16227beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 16237beb2edfSTejun Heo struct work_struct *work = &dwork->work; 16241da177e4SLinus Torvalds 1625637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 1626841b86f3SKees Cook WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1627fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1628fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 16297beb2edfSTejun Heo 16308852aac2STejun Heo /* 16318852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 16328852aac2STejun Heo * both optimization and correctness. The earliest @timer can 16338852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 16348852aac2STejun Heo * on that there's no such delay when @delay is 0. 16358852aac2STejun Heo */ 16368852aac2STejun Heo if (!delay) { 16378852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 16388852aac2STejun Heo return; 16398852aac2STejun Heo } 16408852aac2STejun Heo 164160c057bcSLai Jiangshan dwork->wq = wq; 16421265057fSTejun Heo dwork->cpu = cpu; 16437beb2edfSTejun Heo timer->expires = jiffies + delay; 16447beb2edfSTejun Heo 1645041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 16467beb2edfSTejun Heo add_timer_on(timer, cpu); 1647041bd12eSTejun Heo else 1648041bd12eSTejun Heo add_timer(timer); 16497beb2edfSTejun Heo } 16501da177e4SLinus Torvalds 16510fcb78c2SRolf Eike Beer /** 16520fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 16530fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 16540fcb78c2SRolf Eike Beer * @wq: workqueue to use 1655af9997e4SRandy Dunlap * @dwork: work to queue 16560fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 16570fcb78c2SRolf Eike Beer * 1658d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1659715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1660715f1300STejun Heo * execution. 16610fcb78c2SRolf Eike Beer */ 1662d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 166352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 16647a6bc1cdSVenkatesh Pallipadi { 166552bad64dSDavid Howells struct work_struct *work = &dwork->work; 1666d4283e93STejun Heo bool ret = false; 16678930cabaSTejun Heo unsigned long flags; 16688930cabaSTejun Heo 16698930cabaSTejun Heo /* read the comment in __queue_work() */ 16708930cabaSTejun Heo local_irq_save(flags); 16717a6bc1cdSVenkatesh Pallipadi 167222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 16737beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1674d4283e93STejun Heo ret = true; 16757a6bc1cdSVenkatesh Pallipadi } 16768930cabaSTejun Heo 16778930cabaSTejun Heo local_irq_restore(flags); 16787a6bc1cdSVenkatesh Pallipadi return ret; 16797a6bc1cdSVenkatesh Pallipadi } 1680ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 16811da177e4SLinus Torvalds 1682c8e55f36STejun Heo /** 16838376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 16848376fe22STejun Heo * @cpu: CPU number to execute work on 16858376fe22STejun Heo * @wq: workqueue to use 16868376fe22STejun Heo * @dwork: work to queue 16878376fe22STejun Heo * @delay: number of jiffies to wait before queueing 16888376fe22STejun Heo * 16898376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 16908376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 16918376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 16928376fe22STejun Heo * current state. 16938376fe22STejun Heo * 1694d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 16958376fe22STejun Heo * pending and its timer was modified. 16968376fe22STejun Heo * 1697e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 16988376fe22STejun Heo * See try_to_grab_pending() for details. 16998376fe22STejun Heo */ 17008376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 17018376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 17028376fe22STejun Heo { 17038376fe22STejun Heo unsigned long flags; 17048376fe22STejun Heo int ret; 17058376fe22STejun Heo 17068376fe22STejun Heo do { 17078376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 17088376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 17098376fe22STejun Heo 17108376fe22STejun Heo if (likely(ret >= 0)) { 17118376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 17128376fe22STejun Heo local_irq_restore(flags); 17138376fe22STejun Heo } 17148376fe22STejun Heo 17158376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 17168376fe22STejun Heo return ret; 17178376fe22STejun Heo } 17188376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 17198376fe22STejun Heo 172005f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 172105f0fe6bSTejun Heo { 172205f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 172305f0fe6bSTejun Heo 172405f0fe6bSTejun Heo /* read the comment in __queue_work() */ 172505f0fe6bSTejun Heo local_irq_disable(); 172605f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 172705f0fe6bSTejun Heo local_irq_enable(); 172805f0fe6bSTejun Heo } 172905f0fe6bSTejun Heo 173005f0fe6bSTejun Heo /** 173105f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 173205f0fe6bSTejun Heo * @wq: workqueue to use 173305f0fe6bSTejun Heo * @rwork: work to queue 173405f0fe6bSTejun Heo * 173505f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 173605f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 1737bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 173805f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 173905f0fe6bSTejun Heo */ 174005f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 174105f0fe6bSTejun Heo { 174205f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 174305f0fe6bSTejun Heo 174405f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 174505f0fe6bSTejun Heo rwork->wq = wq; 174605f0fe6bSTejun Heo call_rcu(&rwork->rcu, rcu_work_rcufn); 174705f0fe6bSTejun Heo return true; 174805f0fe6bSTejun Heo } 174905f0fe6bSTejun Heo 175005f0fe6bSTejun Heo return false; 175105f0fe6bSTejun Heo } 175205f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 175305f0fe6bSTejun Heo 17548376fe22STejun Heo /** 1755c8e55f36STejun Heo * worker_enter_idle - enter idle state 1756c8e55f36STejun Heo * @worker: worker which is entering idle state 1757c8e55f36STejun Heo * 1758c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1759c8e55f36STejun Heo * necessary. 1760c8e55f36STejun Heo * 1761c8e55f36STejun Heo * LOCKING: 1762d565ed63STejun Heo * spin_lock_irq(pool->lock). 1763c8e55f36STejun Heo */ 1764c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 17651da177e4SLinus Torvalds { 1766bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1767c8e55f36STejun Heo 17686183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 17696183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 17706183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 17716183c009STejun Heo return; 1772c8e55f36STejun Heo 1773051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1774cb444766STejun Heo worker->flags |= WORKER_IDLE; 1775bd7bdd43STejun Heo pool->nr_idle++; 1776e22bee78STejun Heo worker->last_active = jiffies; 1777c8e55f36STejun Heo 1778c8e55f36STejun Heo /* idle_list is LIFO */ 1779bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1780db7bccf4STejun Heo 178163d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1782628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1783cb444766STejun Heo 1784544ecf31STejun Heo /* 1785e8b3f8dbSLai Jiangshan * Sanity check nr_running. Because unbind_workers() releases 1786d565ed63STejun Heo * pool->lock between setting %WORKER_UNBOUND and zapping 1787628c78e7STejun Heo * nr_running, the warning may trigger spuriously. Check iff 1788628c78e7STejun Heo * unbind is not in progress. 1789544ecf31STejun Heo */ 179024647570STejun Heo WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 1791bd7bdd43STejun Heo pool->nr_workers == pool->nr_idle && 1792e19e397aSTejun Heo atomic_read(&pool->nr_running)); 1793c8e55f36STejun Heo } 1794c8e55f36STejun Heo 1795c8e55f36STejun Heo /** 1796c8e55f36STejun Heo * worker_leave_idle - leave idle state 1797c8e55f36STejun Heo * @worker: worker which is leaving idle state 1798c8e55f36STejun Heo * 1799c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1800c8e55f36STejun Heo * 1801c8e55f36STejun Heo * LOCKING: 1802d565ed63STejun Heo * spin_lock_irq(pool->lock). 1803c8e55f36STejun Heo */ 1804c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1805c8e55f36STejun Heo { 1806bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1807c8e55f36STejun Heo 18086183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 18096183c009STejun Heo return; 1810d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1811bd7bdd43STejun Heo pool->nr_idle--; 1812c8e55f36STejun Heo list_del_init(&worker->entry); 1813c8e55f36STejun Heo } 1814c8e55f36STejun Heo 1815f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1816c34056a3STejun Heo { 1817c34056a3STejun Heo struct worker *worker; 1818c34056a3STejun Heo 1819f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1820c8e55f36STejun Heo if (worker) { 1821c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1822affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1823da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1824e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1825e22bee78STejun Heo worker->flags = WORKER_PREP; 1826c8e55f36STejun Heo } 1827c34056a3STejun Heo return worker; 1828c34056a3STejun Heo } 1829c34056a3STejun Heo 1830c34056a3STejun Heo /** 18314736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 18324736cbf7SLai Jiangshan * @worker: worker to be attached 18334736cbf7SLai Jiangshan * @pool: the target pool 18344736cbf7SLai Jiangshan * 18354736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 18364736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 18374736cbf7SLai Jiangshan * cpu-[un]hotplugs. 18384736cbf7SLai Jiangshan */ 18394736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 18404736cbf7SLai Jiangshan struct worker_pool *pool) 18414736cbf7SLai Jiangshan { 18421258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 18434736cbf7SLai Jiangshan 18444736cbf7SLai Jiangshan /* 18454736cbf7SLai Jiangshan * set_cpus_allowed_ptr() will fail if the cpumask doesn't have any 18464736cbf7SLai Jiangshan * online CPUs. It'll be re-applied when any of the CPUs come up. 18474736cbf7SLai Jiangshan */ 18484736cbf7SLai Jiangshan set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 18494736cbf7SLai Jiangshan 18504736cbf7SLai Jiangshan /* 18511258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 18521258fae7STejun Heo * stable across this function. See the comments above the flag 18531258fae7STejun Heo * definition for details. 18544736cbf7SLai Jiangshan */ 18554736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 18564736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 18574736cbf7SLai Jiangshan 18584736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 1859a2d812a2STejun Heo worker->pool = pool; 18604736cbf7SLai Jiangshan 18611258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 18624736cbf7SLai Jiangshan } 18634736cbf7SLai Jiangshan 18644736cbf7SLai Jiangshan /** 186560f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 186660f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 186760f5a4bcSLai Jiangshan * 18684736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 18694736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 18704736cbf7SLai Jiangshan * other reference to the pool. 187160f5a4bcSLai Jiangshan */ 1872a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 187360f5a4bcSLai Jiangshan { 1874a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 187560f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 187660f5a4bcSLai Jiangshan 18771258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 1878a2d812a2STejun Heo 1879da028469SLai Jiangshan list_del(&worker->node); 1880a2d812a2STejun Heo worker->pool = NULL; 1881a2d812a2STejun Heo 1882da028469SLai Jiangshan if (list_empty(&pool->workers)) 188360f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 18841258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 188560f5a4bcSLai Jiangshan 1886b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1887b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1888b62c0751SLai Jiangshan 188960f5a4bcSLai Jiangshan if (detach_completion) 189060f5a4bcSLai Jiangshan complete(detach_completion); 189160f5a4bcSLai Jiangshan } 189260f5a4bcSLai Jiangshan 189360f5a4bcSLai Jiangshan /** 1894c34056a3STejun Heo * create_worker - create a new workqueue worker 189563d95a91STejun Heo * @pool: pool the new worker will belong to 1896c34056a3STejun Heo * 1897051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1898c34056a3STejun Heo * 1899c34056a3STejun Heo * CONTEXT: 1900c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1901c34056a3STejun Heo * 1902d185af30SYacine Belkadi * Return: 1903c34056a3STejun Heo * Pointer to the newly created worker. 1904c34056a3STejun Heo */ 1905bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1906c34056a3STejun Heo { 1907c34056a3STejun Heo struct worker *worker = NULL; 1908f3421797STejun Heo int id = -1; 1909e3c916a4STejun Heo char id_buf[16]; 1910c34056a3STejun Heo 19117cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 19127cda9aaeSLai Jiangshan id = ida_simple_get(&pool->worker_ida, 0, 0, GFP_KERNEL); 1913822d8405STejun Heo if (id < 0) 1914c34056a3STejun Heo goto fail; 1915c34056a3STejun Heo 1916f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 1917c34056a3STejun Heo if (!worker) 1918c34056a3STejun Heo goto fail; 1919c34056a3STejun Heo 1920c34056a3STejun Heo worker->id = id; 1921c34056a3STejun Heo 192229c91e99STejun Heo if (pool->cpu >= 0) 1923e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1924e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1925f3421797STejun Heo else 1926e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1927e3c916a4STejun Heo 1928f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1929e3c916a4STejun Heo "kworker/%s", id_buf); 1930c34056a3STejun Heo if (IS_ERR(worker->task)) 1931c34056a3STejun Heo goto fail; 1932c34056a3STejun Heo 193391151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 193425834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 193591151228SOleg Nesterov 1936da028469SLai Jiangshan /* successful, attach the worker to the pool */ 19374736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1938822d8405STejun Heo 1939051e1850SLai Jiangshan /* start the newly created worker */ 1940051e1850SLai Jiangshan spin_lock_irq(&pool->lock); 1941051e1850SLai Jiangshan worker->pool->nr_workers++; 1942051e1850SLai Jiangshan worker_enter_idle(worker); 1943051e1850SLai Jiangshan wake_up_process(worker->task); 1944051e1850SLai Jiangshan spin_unlock_irq(&pool->lock); 1945051e1850SLai Jiangshan 1946c34056a3STejun Heo return worker; 1947822d8405STejun Heo 1948c34056a3STejun Heo fail: 19499625ab17SLai Jiangshan if (id >= 0) 19507cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, id); 1951c34056a3STejun Heo kfree(worker); 1952c34056a3STejun Heo return NULL; 1953c34056a3STejun Heo } 1954c34056a3STejun Heo 1955c34056a3STejun Heo /** 1956c34056a3STejun Heo * destroy_worker - destroy a workqueue worker 1957c34056a3STejun Heo * @worker: worker to be destroyed 1958c34056a3STejun Heo * 195973eb7fe7SLai Jiangshan * Destroy @worker and adjust @pool stats accordingly. The worker should 196073eb7fe7SLai Jiangshan * be idle. 1961c8e55f36STejun Heo * 1962c8e55f36STejun Heo * CONTEXT: 196360f5a4bcSLai Jiangshan * spin_lock_irq(pool->lock). 1964c34056a3STejun Heo */ 1965c34056a3STejun Heo static void destroy_worker(struct worker *worker) 1966c34056a3STejun Heo { 1967bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1968c34056a3STejun Heo 1969cd549687STejun Heo lockdep_assert_held(&pool->lock); 1970cd549687STejun Heo 1971c34056a3STejun Heo /* sanity check frenzy */ 19726183c009STejun Heo if (WARN_ON(worker->current_work) || 197373eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 197473eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 19756183c009STejun Heo return; 1976c34056a3STejun Heo 1977bd7bdd43STejun Heo pool->nr_workers--; 1978bd7bdd43STejun Heo pool->nr_idle--; 1979c8e55f36STejun Heo 1980c8e55f36STejun Heo list_del_init(&worker->entry); 1981cb444766STejun Heo worker->flags |= WORKER_DIE; 198260f5a4bcSLai Jiangshan wake_up_process(worker->task); 1983c34056a3STejun Heo } 1984c34056a3STejun Heo 198532a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 1986e22bee78STejun Heo { 198732a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 1988e22bee78STejun Heo 1989d565ed63STejun Heo spin_lock_irq(&pool->lock); 1990e22bee78STejun Heo 19913347fc9fSLai Jiangshan while (too_many_workers(pool)) { 1992e22bee78STejun Heo struct worker *worker; 1993e22bee78STejun Heo unsigned long expires; 1994e22bee78STejun Heo 1995e22bee78STejun Heo /* idle_list is kept in LIFO order, check the last one */ 199663d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 1997e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 1998e22bee78STejun Heo 19993347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 200063d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 20013347fc9fSLai Jiangshan break; 2002e22bee78STejun Heo } 20033347fc9fSLai Jiangshan 20043347fc9fSLai Jiangshan destroy_worker(worker); 2005e22bee78STejun Heo } 2006e22bee78STejun Heo 2007d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2008e22bee78STejun Heo } 2009e22bee78STejun Heo 2010493a1724STejun Heo static void send_mayday(struct work_struct *work) 2011e22bee78STejun Heo { 2012112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2013112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2014493a1724STejun Heo 20152e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2016e22bee78STejun Heo 2017493008a8STejun Heo if (!wq->rescuer) 2018493a1724STejun Heo return; 2019e22bee78STejun Heo 2020e22bee78STejun Heo /* mayday mayday mayday */ 2021493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 202277668c8bSLai Jiangshan /* 202377668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 202477668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 202577668c8bSLai Jiangshan * rescuer is done with it. 202677668c8bSLai Jiangshan */ 202777668c8bSLai Jiangshan get_pwq(pwq); 2028493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2029e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2030493a1724STejun Heo } 2031e22bee78STejun Heo } 2032e22bee78STejun Heo 203332a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2034e22bee78STejun Heo { 203532a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2036e22bee78STejun Heo struct work_struct *work; 2037e22bee78STejun Heo 2038b2d82909STejun Heo spin_lock_irq(&pool->lock); 2039b2d82909STejun Heo spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2040e22bee78STejun Heo 204163d95a91STejun Heo if (need_to_create_worker(pool)) { 2042e22bee78STejun Heo /* 2043e22bee78STejun Heo * We've been trying to create a new worker but 2044e22bee78STejun Heo * haven't been successful. We might be hitting an 2045e22bee78STejun Heo * allocation deadlock. Send distress signals to 2046e22bee78STejun Heo * rescuers. 2047e22bee78STejun Heo */ 204863d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2049e22bee78STejun Heo send_mayday(work); 2050e22bee78STejun Heo } 2051e22bee78STejun Heo 2052b2d82909STejun Heo spin_unlock(&wq_mayday_lock); 2053b2d82909STejun Heo spin_unlock_irq(&pool->lock); 2054e22bee78STejun Heo 205563d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2056e22bee78STejun Heo } 2057e22bee78STejun Heo 2058e22bee78STejun Heo /** 2059e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 206063d95a91STejun Heo * @pool: pool to create a new worker for 2061e22bee78STejun Heo * 206263d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2063e22bee78STejun Heo * have at least one idle worker on return from this function. If 2064e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 206563d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2066e22bee78STejun Heo * possible allocation deadlock. 2067e22bee78STejun Heo * 2068c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2069c5aa87bbSTejun Heo * may_start_working() %true. 2070e22bee78STejun Heo * 2071e22bee78STejun Heo * LOCKING: 2072d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2073e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2074e22bee78STejun Heo * manager. 2075e22bee78STejun Heo */ 207629187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2077d565ed63STejun Heo __releases(&pool->lock) 2078d565ed63STejun Heo __acquires(&pool->lock) 2079e22bee78STejun Heo { 2080e22bee78STejun Heo restart: 2081d565ed63STejun Heo spin_unlock_irq(&pool->lock); 20829f9c2364STejun Heo 2083e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 208463d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2085e22bee78STejun Heo 2086e22bee78STejun Heo while (true) { 2087051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2088e22bee78STejun Heo break; 2089e22bee78STejun Heo 2090e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 20919f9c2364STejun Heo 209263d95a91STejun Heo if (!need_to_create_worker(pool)) 2093e22bee78STejun Heo break; 2094e22bee78STejun Heo } 2095e22bee78STejun Heo 209663d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2097d565ed63STejun Heo spin_lock_irq(&pool->lock); 2098051e1850SLai Jiangshan /* 2099051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2100051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2101051e1850SLai Jiangshan * already become busy. 2102051e1850SLai Jiangshan */ 210363d95a91STejun Heo if (need_to_create_worker(pool)) 2104e22bee78STejun Heo goto restart; 2105e22bee78STejun Heo } 2106e22bee78STejun Heo 2107e22bee78STejun Heo /** 2108e22bee78STejun Heo * manage_workers - manage worker pool 2109e22bee78STejun Heo * @worker: self 2110e22bee78STejun Heo * 2111706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2112e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2113706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2114e22bee78STejun Heo * 2115e22bee78STejun Heo * The caller can safely start processing works on false return. On 2116e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2117e22bee78STejun Heo * and may_start_working() is true. 2118e22bee78STejun Heo * 2119e22bee78STejun Heo * CONTEXT: 2120d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2121e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2122e22bee78STejun Heo * 2123d185af30SYacine Belkadi * Return: 212429187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 212529187a9eSTejun Heo * start processing works, %true if management function was performed and 212629187a9eSTejun Heo * the conditions that the caller verified before calling the function may 212729187a9eSTejun Heo * no longer be true. 2128e22bee78STejun Heo */ 2129e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2130e22bee78STejun Heo { 213163d95a91STejun Heo struct worker_pool *pool = worker->pool; 2132e22bee78STejun Heo 2133692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 213429187a9eSTejun Heo return false; 2135692b4825STejun Heo 2136692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 21372607d7a6STejun Heo pool->manager = worker; 2138e22bee78STejun Heo 213929187a9eSTejun Heo maybe_create_worker(pool); 2140e22bee78STejun Heo 21412607d7a6STejun Heo pool->manager = NULL; 2142692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2143692b4825STejun Heo wake_up(&wq_manager_wait); 214429187a9eSTejun Heo return true; 2145e22bee78STejun Heo } 2146e22bee78STejun Heo 2147a62428c0STejun Heo /** 2148a62428c0STejun Heo * process_one_work - process single work 2149c34056a3STejun Heo * @worker: self 2150a62428c0STejun Heo * @work: work to process 2151a62428c0STejun Heo * 2152a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2153a62428c0STejun Heo * process a single work including synchronization against and 2154a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2155a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2156a62428c0STejun Heo * call this function to process a work. 2157a62428c0STejun Heo * 2158a62428c0STejun Heo * CONTEXT: 2159d565ed63STejun Heo * spin_lock_irq(pool->lock) which is released and regrabbed. 2160a62428c0STejun Heo */ 2161c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2162d565ed63STejun Heo __releases(&pool->lock) 2163d565ed63STejun Heo __acquires(&pool->lock) 21641da177e4SLinus Torvalds { 2165112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2166bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2167112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 216873f53c4aSTejun Heo int work_color; 21697e11629dSTejun Heo struct worker *collision; 21704e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 21714e6045f1SJohannes Berg /* 2172a62428c0STejun Heo * It is permissible to free the struct work_struct from 2173a62428c0STejun Heo * inside the function that is called from it, this we need to 2174a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2175a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2176a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 21774e6045f1SJohannes Berg */ 21784d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 21794d82a1deSPeter Zijlstra 21804d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 21814e6045f1SJohannes Berg #endif 2182807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 218385327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2184ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 218525511a47STejun Heo 21867e11629dSTejun Heo /* 21877e11629dSTejun Heo * A single work shouldn't be executed concurrently by 21887e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 21897e11629dSTejun Heo * already processing the work. If so, defer the work to the 21907e11629dSTejun Heo * currently executing one. 21917e11629dSTejun Heo */ 2192c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 21937e11629dSTejun Heo if (unlikely(collision)) { 21947e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 21957e11629dSTejun Heo return; 21967e11629dSTejun Heo } 21971da177e4SLinus Torvalds 21988930cabaSTejun Heo /* claim and dequeue */ 21991da177e4SLinus Torvalds debug_work_deactivate(work); 2200c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2201c34056a3STejun Heo worker->current_work = work; 2202a2c1c57bSTejun Heo worker->current_func = work->func; 2203112202d9STejun Heo worker->current_pwq = pwq; 220473f53c4aSTejun Heo work_color = get_work_color(work); 22057a22ad75STejun Heo 22068bf89593STejun Heo /* 22078bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 22088bf89593STejun Heo * overridden through set_worker_desc(). 22098bf89593STejun Heo */ 22108bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 22118bf89593STejun Heo 2212a62428c0STejun Heo list_del_init(&work->entry); 2213a62428c0STejun Heo 2214649027d7STejun Heo /* 2215228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2216228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2217228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2218228f1d00SLai Jiangshan * execution of the pending work items. 2219fb0e7bebSTejun Heo */ 2220fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2221228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2222fb0e7bebSTejun Heo 2223974271c4STejun Heo /* 2224a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2225a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2226a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2227a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2228228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2229974271c4STejun Heo */ 2230a489a03eSLai Jiangshan if (need_more_worker(pool)) 223163d95a91STejun Heo wake_up_worker(pool); 2232974271c4STejun Heo 22338930cabaSTejun Heo /* 22347c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2235d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 223623657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 223723657bb1STejun Heo * disabled. 22388930cabaSTejun Heo */ 22397c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 22401da177e4SLinus Torvalds 2241d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2242365970a1SDavid Howells 2243a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 22443295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2245e6f3faa7SPeter Zijlstra /* 2246f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2247f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2248e6f3faa7SPeter Zijlstra * 2249e6f3faa7SPeter Zijlstra * However, that would result in: 2250e6f3faa7SPeter Zijlstra * 2251e6f3faa7SPeter Zijlstra * A(W1) 2252e6f3faa7SPeter Zijlstra * WFC(C) 2253e6f3faa7SPeter Zijlstra * A(W1) 2254e6f3faa7SPeter Zijlstra * C(C) 2255e6f3faa7SPeter Zijlstra * 2256e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2257e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2258e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2259f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2260e6f3faa7SPeter Zijlstra * these locks. 2261e6f3faa7SPeter Zijlstra * 2262e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2263e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2264e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2265e6f3faa7SPeter Zijlstra */ 2266f52be570SPeter Zijlstra lockdep_invariant_state(true); 2267e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2268a2c1c57bSTejun Heo worker->current_func(work); 2269e36c886aSArjan van de Ven /* 2270e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2271e36c886aSArjan van de Ven * point will only record its address. 2272e36c886aSArjan van de Ven */ 22731c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 22743295f0efSIngo Molnar lock_map_release(&lockdep_map); 2275112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 22761da177e4SLinus Torvalds 2277d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2278044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2279d75f773cSSakari Ailus " last function: %ps\n", 2280a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2281a2c1c57bSTejun Heo worker->current_func); 2282d5abe669SPeter Zijlstra debug_show_held_locks(current); 2283d5abe669SPeter Zijlstra dump_stack(); 2284d5abe669SPeter Zijlstra } 2285d5abe669SPeter Zijlstra 2286b22ce278STejun Heo /* 2287025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2288b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2289b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2290b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2291789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2292789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2293b22ce278STejun Heo */ 2294a7e6425eSPaul E. McKenney cond_resched(); 2295b22ce278STejun Heo 2296d565ed63STejun Heo spin_lock_irq(&pool->lock); 2297a62428c0STejun Heo 2298fb0e7bebSTejun Heo /* clear cpu intensive status */ 2299fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2300fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2301fb0e7bebSTejun Heo 23021b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 23031b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 23041b69ac6bSJohannes Weiner 2305a62428c0STejun Heo /* we're done with it, release */ 230642f8570fSSasha Levin hash_del(&worker->hentry); 2307c34056a3STejun Heo worker->current_work = NULL; 2308a2c1c57bSTejun Heo worker->current_func = NULL; 2309112202d9STejun Heo worker->current_pwq = NULL; 2310112202d9STejun Heo pwq_dec_nr_in_flight(pwq, work_color); 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 2313affee4b2STejun Heo /** 2314affee4b2STejun Heo * process_scheduled_works - process scheduled works 2315affee4b2STejun Heo * @worker: self 2316affee4b2STejun Heo * 2317affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2318affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2319affee4b2STejun Heo * fetches a work from the top and executes it. 2320affee4b2STejun Heo * 2321affee4b2STejun Heo * CONTEXT: 2322d565ed63STejun Heo * spin_lock_irq(pool->lock) which may be released and regrabbed 2323affee4b2STejun Heo * multiple times. 2324affee4b2STejun Heo */ 2325affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 23261da177e4SLinus Torvalds { 2327affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2328affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2329a62428c0STejun Heo struct work_struct, entry); 2330c34056a3STejun Heo process_one_work(worker, work); 2331a62428c0STejun Heo } 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 2334197f6accSTejun Heo static void set_pf_worker(bool val) 2335197f6accSTejun Heo { 2336197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2337197f6accSTejun Heo if (val) 2338197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2339197f6accSTejun Heo else 2340197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2341197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2342197f6accSTejun Heo } 2343197f6accSTejun Heo 23444690c4abSTejun Heo /** 23454690c4abSTejun Heo * worker_thread - the worker thread function 2346c34056a3STejun Heo * @__worker: self 23474690c4abSTejun Heo * 2348c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2349c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2350c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2351c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2352c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2353d185af30SYacine Belkadi * 2354d185af30SYacine Belkadi * Return: 0 23554690c4abSTejun Heo */ 2356c34056a3STejun Heo static int worker_thread(void *__worker) 23571da177e4SLinus Torvalds { 2358c34056a3STejun Heo struct worker *worker = __worker; 2359bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 23601da177e4SLinus Torvalds 2361e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2362197f6accSTejun Heo set_pf_worker(true); 2363c8e55f36STejun Heo woke_up: 2364d565ed63STejun Heo spin_lock_irq(&pool->lock); 2365affee4b2STejun Heo 2366a9ab775bSTejun Heo /* am I supposed to die? */ 2367a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2368d565ed63STejun Heo spin_unlock_irq(&pool->lock); 2369a9ab775bSTejun Heo WARN_ON_ONCE(!list_empty(&worker->entry)); 2370197f6accSTejun Heo set_pf_worker(false); 237160f5a4bcSLai Jiangshan 237260f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 23737cda9aaeSLai Jiangshan ida_simple_remove(&pool->worker_ida, worker->id); 2374a2d812a2STejun Heo worker_detach_from_pool(worker); 237560f5a4bcSLai Jiangshan kfree(worker); 2376c8e55f36STejun Heo return 0; 2377c8e55f36STejun Heo } 2378c8e55f36STejun Heo 2379c8e55f36STejun Heo worker_leave_idle(worker); 2380db7bccf4STejun Heo recheck: 2381e22bee78STejun Heo /* no more worker necessary? */ 238263d95a91STejun Heo if (!need_more_worker(pool)) 2383e22bee78STejun Heo goto sleep; 2384e22bee78STejun Heo 2385e22bee78STejun Heo /* do we need to manage? */ 238663d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2387e22bee78STejun Heo goto recheck; 2388e22bee78STejun Heo 2389c8e55f36STejun Heo /* 2390c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2391c8e55f36STejun Heo * preparing to process a work or actually processing it. 2392c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2393c8e55f36STejun Heo */ 23946183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2395c8e55f36STejun Heo 2396e22bee78STejun Heo /* 2397a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2398a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2399a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2400a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2401a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2402e22bee78STejun Heo */ 2403a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2404e22bee78STejun Heo 2405e22bee78STejun Heo do { 2406affee4b2STejun Heo struct work_struct *work = 2407bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2408affee4b2STejun Heo struct work_struct, entry); 2409affee4b2STejun Heo 241082607adcSTejun Heo pool->watchdog_ts = jiffies; 241182607adcSTejun Heo 2412c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2413affee4b2STejun Heo /* optimization path, not strictly necessary */ 2414affee4b2STejun Heo process_one_work(worker, work); 2415affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2416affee4b2STejun Heo process_scheduled_works(worker); 2417affee4b2STejun Heo } else { 2418c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2419affee4b2STejun Heo process_scheduled_works(worker); 2420affee4b2STejun Heo } 242163d95a91STejun Heo } while (keep_working(pool)); 2422affee4b2STejun Heo 2423228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2424d313dd85STejun Heo sleep: 2425c8e55f36STejun Heo /* 2426d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2427d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2428d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2429d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2430d565ed63STejun Heo * event. 2431c8e55f36STejun Heo */ 2432c8e55f36STejun Heo worker_enter_idle(worker); 2433c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2434d565ed63STejun Heo spin_unlock_irq(&pool->lock); 24351da177e4SLinus Torvalds schedule(); 2436c8e55f36STejun Heo goto woke_up; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds 2439e22bee78STejun Heo /** 2440e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2441111c225aSTejun Heo * @__rescuer: self 2442e22bee78STejun Heo * 2443e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2444493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2445e22bee78STejun Heo * 2446706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2447e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2448e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2449e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2450e22bee78STejun Heo * the problem rescuer solves. 2451e22bee78STejun Heo * 2452706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2453706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2454e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2455e22bee78STejun Heo * 2456e22bee78STejun Heo * This should happen rarely. 2457d185af30SYacine Belkadi * 2458d185af30SYacine Belkadi * Return: 0 2459e22bee78STejun Heo */ 2460111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2461e22bee78STejun Heo { 2462111c225aSTejun Heo struct worker *rescuer = __rescuer; 2463111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2464e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 24654d595b86SLai Jiangshan bool should_stop; 2466e22bee78STejun Heo 2467e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2468111c225aSTejun Heo 2469111c225aSTejun Heo /* 2470111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2471111c225aSTejun Heo * doesn't participate in concurrency management. 2472111c225aSTejun Heo */ 2473197f6accSTejun Heo set_pf_worker(true); 2474e22bee78STejun Heo repeat: 2475c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 24761da177e4SLinus Torvalds 24774d595b86SLai Jiangshan /* 24784d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 24794d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 24804d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 24814d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 24824d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 24834d595b86SLai Jiangshan * list is always empty on exit. 24844d595b86SLai Jiangshan */ 24854d595b86SLai Jiangshan should_stop = kthread_should_stop(); 24861da177e4SLinus Torvalds 2487493a1724STejun Heo /* see whether any pwq is asking for help */ 24882e109a28STejun Heo spin_lock_irq(&wq_mayday_lock); 2489493a1724STejun Heo 2490493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2491493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2492493a1724STejun Heo struct pool_workqueue, mayday_node); 2493112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2494e22bee78STejun Heo struct work_struct *work, *n; 249582607adcSTejun Heo bool first = true; 2496e22bee78STejun Heo 2497e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2498493a1724STejun Heo list_del_init(&pwq->mayday_node); 2499493a1724STejun Heo 25002e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2501e22bee78STejun Heo 250251697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 250351697d39SLai Jiangshan 250451697d39SLai Jiangshan spin_lock_irq(&pool->lock); 2505e22bee78STejun Heo 2506e22bee78STejun Heo /* 2507e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2508e22bee78STejun Heo * process'em. 2509e22bee78STejun Heo */ 25100479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 251182607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 251282607adcSTejun Heo if (get_work_pwq(work) == pwq) { 251382607adcSTejun Heo if (first) 251482607adcSTejun Heo pool->watchdog_ts = jiffies; 2515e22bee78STejun Heo move_linked_works(work, scheduled, &n); 251682607adcSTejun Heo } 251782607adcSTejun Heo first = false; 251882607adcSTejun Heo } 2519e22bee78STejun Heo 2520008847f6SNeilBrown if (!list_empty(scheduled)) { 2521e22bee78STejun Heo process_scheduled_works(rescuer); 25227576958aSTejun Heo 25237576958aSTejun Heo /* 2524008847f6SNeilBrown * The above execution of rescued work items could 2525008847f6SNeilBrown * have created more to rescue through 2526008847f6SNeilBrown * pwq_activate_first_delayed() or chained 2527008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2528008847f6SNeilBrown * that such back-to-back work items, which may be 2529008847f6SNeilBrown * being used to relieve memory pressure, don't 2530008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2531008847f6SNeilBrown */ 2532008847f6SNeilBrown if (need_to_create_worker(pool)) { 2533008847f6SNeilBrown spin_lock(&wq_mayday_lock); 2534e66b39afSTejun Heo /* 2535e66b39afSTejun Heo * Queue iff we aren't racing destruction 2536e66b39afSTejun Heo * and somebody else hasn't queued it already. 2537e66b39afSTejun Heo */ 2538e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2539008847f6SNeilBrown get_pwq(pwq); 2540e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2541e66b39afSTejun Heo } 2542008847f6SNeilBrown spin_unlock(&wq_mayday_lock); 2543008847f6SNeilBrown } 2544008847f6SNeilBrown } 2545008847f6SNeilBrown 2546008847f6SNeilBrown /* 254777668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 254813b1d625SLai Jiangshan * go away while we're still attached to it. 254977668c8bSLai Jiangshan */ 255077668c8bSLai Jiangshan put_pwq(pwq); 255177668c8bSLai Jiangshan 255277668c8bSLai Jiangshan /* 2553d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 25547576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 25557576958aSTejun Heo * and stalling the execution. 25567576958aSTejun Heo */ 2557d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 255863d95a91STejun Heo wake_up_worker(pool); 25597576958aSTejun Heo 256013b1d625SLai Jiangshan spin_unlock_irq(&pool->lock); 256113b1d625SLai Jiangshan 2562a2d812a2STejun Heo worker_detach_from_pool(rescuer); 256313b1d625SLai Jiangshan 256413b1d625SLai Jiangshan spin_lock_irq(&wq_mayday_lock); 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 25672e109a28STejun Heo spin_unlock_irq(&wq_mayday_lock); 2568493a1724STejun Heo 25694d595b86SLai Jiangshan if (should_stop) { 25704d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2571197f6accSTejun Heo set_pf_worker(false); 25724d595b86SLai Jiangshan return 0; 25734d595b86SLai Jiangshan } 25744d595b86SLai Jiangshan 2575111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2576111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2577e22bee78STejun Heo schedule(); 2578e22bee78STejun Heo goto repeat; 25791da177e4SLinus Torvalds } 25801da177e4SLinus Torvalds 2581fca839c0STejun Heo /** 2582fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2583fca839c0STejun Heo * @target_wq: workqueue being flushed 2584fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2585fca839c0STejun Heo * 2586fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2587fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2588fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2589fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2590fca839c0STejun Heo * a deadlock. 2591fca839c0STejun Heo */ 2592fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2593fca839c0STejun Heo struct work_struct *target_work) 2594fca839c0STejun Heo { 2595fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2596fca839c0STejun Heo struct worker *worker; 2597fca839c0STejun Heo 2598fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2599fca839c0STejun Heo return; 2600fca839c0STejun Heo 2601fca839c0STejun Heo worker = current_wq_worker(); 2602fca839c0STejun Heo 2603fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2604d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2605fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 260623d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 260723d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2608d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2609fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2610fca839c0STejun Heo target_wq->name, target_func); 2611fca839c0STejun Heo } 2612fca839c0STejun Heo 2613fc2e4d70SOleg Nesterov struct wq_barrier { 2614fc2e4d70SOleg Nesterov struct work_struct work; 2615fc2e4d70SOleg Nesterov struct completion done; 26162607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2617fc2e4d70SOleg Nesterov }; 2618fc2e4d70SOleg Nesterov 2619fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2620fc2e4d70SOleg Nesterov { 2621fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2622fc2e4d70SOleg Nesterov complete(&barr->done); 2623fc2e4d70SOleg Nesterov } 2624fc2e4d70SOleg Nesterov 26254690c4abSTejun Heo /** 26264690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2627112202d9STejun Heo * @pwq: pwq to insert barrier into 26284690c4abSTejun Heo * @barr: wq_barrier to insert 2629affee4b2STejun Heo * @target: target work to attach @barr to 2630affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 26314690c4abSTejun Heo * 2632affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2633affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2634affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2635affee4b2STejun Heo * cpu. 2636affee4b2STejun Heo * 2637affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2638affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2639affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2640affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2641affee4b2STejun Heo * after a work with LINKED flag set. 2642affee4b2STejun Heo * 2643affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2644112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 26454690c4abSTejun Heo * 26464690c4abSTejun Heo * CONTEXT: 2647d565ed63STejun Heo * spin_lock_irq(pool->lock). 26484690c4abSTejun Heo */ 2649112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2650affee4b2STejun Heo struct wq_barrier *barr, 2651affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2652fc2e4d70SOleg Nesterov { 2653affee4b2STejun Heo struct list_head *head; 2654affee4b2STejun Heo unsigned int linked = 0; 2655affee4b2STejun Heo 2656dc186ad7SThomas Gleixner /* 2657d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2658dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2659dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2660dc186ad7SThomas Gleixner * might deadlock. 2661dc186ad7SThomas Gleixner */ 2662ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 266322df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 266452fa5bc5SBoqun Feng 2665fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2666fd1a5b04SByungchul Park 26672607d7a6STejun Heo barr->task = current; 266883c22520SOleg Nesterov 2669affee4b2STejun Heo /* 2670affee4b2STejun Heo * If @target is currently being executed, schedule the 2671affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2672affee4b2STejun Heo */ 2673affee4b2STejun Heo if (worker) 2674affee4b2STejun Heo head = worker->scheduled.next; 2675affee4b2STejun Heo else { 2676affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2677affee4b2STejun Heo 2678affee4b2STejun Heo head = target->entry.next; 2679affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2680affee4b2STejun Heo linked = *bits & WORK_STRUCT_LINKED; 2681affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2682affee4b2STejun Heo } 2683affee4b2STejun Heo 2684dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2685112202d9STejun Heo insert_work(pwq, &barr->work, head, 2686affee4b2STejun Heo work_color_to_flags(WORK_NO_COLOR) | linked); 2687fc2e4d70SOleg Nesterov } 2688fc2e4d70SOleg Nesterov 268973f53c4aSTejun Heo /** 2690112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 269173f53c4aSTejun Heo * @wq: workqueue being flushed 269273f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 269373f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 269473f53c4aSTejun Heo * 2695112202d9STejun Heo * Prepare pwqs for workqueue flushing. 269673f53c4aSTejun Heo * 2697112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2698112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2699112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2700112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2701112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 270273f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 270373f53c4aSTejun Heo * 270473f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 270573f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 270673f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 270773f53c4aSTejun Heo * is returned. 270873f53c4aSTejun Heo * 2709112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 271073f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 271173f53c4aSTejun Heo * advanced to @work_color. 271273f53c4aSTejun Heo * 271373f53c4aSTejun Heo * CONTEXT: 27143c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 271573f53c4aSTejun Heo * 2716d185af30SYacine Belkadi * Return: 271773f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 271873f53c4aSTejun Heo * otherwise. 271973f53c4aSTejun Heo */ 2720112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 272173f53c4aSTejun Heo int flush_color, int work_color) 27221da177e4SLinus Torvalds { 272373f53c4aSTejun Heo bool wait = false; 272449e3cf44STejun Heo struct pool_workqueue *pwq; 27251da177e4SLinus Torvalds 272673f53c4aSTejun Heo if (flush_color >= 0) { 27276183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2728112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2729dc186ad7SThomas Gleixner } 273014441960SOleg Nesterov 273149e3cf44STejun Heo for_each_pwq(pwq, wq) { 2732112202d9STejun Heo struct worker_pool *pool = pwq->pool; 27331da177e4SLinus Torvalds 2734b09f4fd3SLai Jiangshan spin_lock_irq(&pool->lock); 273573f53c4aSTejun Heo 273673f53c4aSTejun Heo if (flush_color >= 0) { 27376183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 273873f53c4aSTejun Heo 2739112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2740112202d9STejun Heo pwq->flush_color = flush_color; 2741112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 274273f53c4aSTejun Heo wait = true; 27431da177e4SLinus Torvalds } 274473f53c4aSTejun Heo } 274573f53c4aSTejun Heo 274673f53c4aSTejun Heo if (work_color >= 0) { 27476183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2748112202d9STejun Heo pwq->work_color = work_color; 274973f53c4aSTejun Heo } 275073f53c4aSTejun Heo 2751b09f4fd3SLai Jiangshan spin_unlock_irq(&pool->lock); 27521da177e4SLinus Torvalds } 27531da177e4SLinus Torvalds 2754112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 275573f53c4aSTejun Heo complete(&wq->first_flusher->done); 275673f53c4aSTejun Heo 275773f53c4aSTejun Heo return wait; 275883c22520SOleg Nesterov } 27591da177e4SLinus Torvalds 27600fcb78c2SRolf Eike Beer /** 27611da177e4SLinus Torvalds * flush_workqueue - ensure that any scheduled work has run to completion. 27620fcb78c2SRolf Eike Beer * @wq: workqueue to flush 27631da177e4SLinus Torvalds * 2764c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2765c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 27661da177e4SLinus Torvalds */ 27677ad5b3a5SHarvey Harrison void flush_workqueue(struct workqueue_struct *wq) 27681da177e4SLinus Torvalds { 276973f53c4aSTejun Heo struct wq_flusher this_flusher = { 277073f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 277173f53c4aSTejun Heo .flush_color = -1, 2772fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 277373f53c4aSTejun Heo }; 277473f53c4aSTejun Heo int next_color; 2775b1f4ec17SOleg Nesterov 27763347fa09STejun Heo if (WARN_ON(!wq_online)) 27773347fa09STejun Heo return; 27783347fa09STejun Heo 277987915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 278087915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 278187915adcSJohannes Berg 27823c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 278373f53c4aSTejun Heo 278473f53c4aSTejun Heo /* 278573f53c4aSTejun Heo * Start-to-wait phase 278673f53c4aSTejun Heo */ 278773f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 278873f53c4aSTejun Heo 278973f53c4aSTejun Heo if (next_color != wq->flush_color) { 279073f53c4aSTejun Heo /* 279173f53c4aSTejun Heo * Color space is not full. The current work_color 279273f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 279373f53c4aSTejun Heo * by one. 279473f53c4aSTejun Heo */ 27956183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 279673f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 279773f53c4aSTejun Heo wq->work_color = next_color; 279873f53c4aSTejun Heo 279973f53c4aSTejun Heo if (!wq->first_flusher) { 280073f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 28016183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 280273f53c4aSTejun Heo 280373f53c4aSTejun Heo wq->first_flusher = &this_flusher; 280473f53c4aSTejun Heo 2805112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 280673f53c4aSTejun Heo wq->work_color)) { 280773f53c4aSTejun Heo /* nothing to flush, done */ 280873f53c4aSTejun Heo wq->flush_color = next_color; 280973f53c4aSTejun Heo wq->first_flusher = NULL; 281073f53c4aSTejun Heo goto out_unlock; 281173f53c4aSTejun Heo } 281273f53c4aSTejun Heo } else { 281373f53c4aSTejun Heo /* wait in queue */ 28146183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 281573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2816112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 281773f53c4aSTejun Heo } 281873f53c4aSTejun Heo } else { 281973f53c4aSTejun Heo /* 282073f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 282173f53c4aSTejun Heo * The next flush completion will assign us 282273f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 282373f53c4aSTejun Heo */ 282473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 282573f53c4aSTejun Heo } 282673f53c4aSTejun Heo 2827fca839c0STejun Heo check_flush_dependency(wq, NULL); 2828fca839c0STejun Heo 28293c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 283073f53c4aSTejun Heo 283173f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 283273f53c4aSTejun Heo 283373f53c4aSTejun Heo /* 283473f53c4aSTejun Heo * Wake-up-and-cascade phase 283573f53c4aSTejun Heo * 283673f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 283773f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 283873f53c4aSTejun Heo */ 283900d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 284073f53c4aSTejun Heo return; 284173f53c4aSTejun Heo 28423c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 284373f53c4aSTejun Heo 28444ce48b37STejun Heo /* we might have raced, check again with mutex held */ 28454ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 28464ce48b37STejun Heo goto out_unlock; 28474ce48b37STejun Heo 284800d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 284973f53c4aSTejun Heo 28506183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 28516183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 285273f53c4aSTejun Heo 285373f53c4aSTejun Heo while (true) { 285473f53c4aSTejun Heo struct wq_flusher *next, *tmp; 285573f53c4aSTejun Heo 285673f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 285773f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 285873f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 285973f53c4aSTejun Heo break; 286073f53c4aSTejun Heo list_del_init(&next->list); 286173f53c4aSTejun Heo complete(&next->done); 286273f53c4aSTejun Heo } 286373f53c4aSTejun Heo 28646183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 286573f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 286673f53c4aSTejun Heo 286773f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 286873f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 286973f53c4aSTejun Heo 287073f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 287173f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 287273f53c4aSTejun Heo /* 287373f53c4aSTejun Heo * Assign the same color to all overflowed 287473f53c4aSTejun Heo * flushers, advance work_color and append to 287573f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 287673f53c4aSTejun Heo * phase for these overflowed flushers. 287773f53c4aSTejun Heo */ 287873f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 287973f53c4aSTejun Heo tmp->flush_color = wq->work_color; 288073f53c4aSTejun Heo 288173f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 288273f53c4aSTejun Heo 288373f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 288473f53c4aSTejun Heo &wq->flusher_queue); 2885112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 288673f53c4aSTejun Heo } 288773f53c4aSTejun Heo 288873f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 28896183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 289073f53c4aSTejun Heo break; 289173f53c4aSTejun Heo } 289273f53c4aSTejun Heo 289373f53c4aSTejun Heo /* 289473f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 2895112202d9STejun Heo * the new first flusher and arm pwqs. 289673f53c4aSTejun Heo */ 28976183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 28986183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 289973f53c4aSTejun Heo 290073f53c4aSTejun Heo list_del_init(&next->list); 290173f53c4aSTejun Heo wq->first_flusher = next; 290273f53c4aSTejun Heo 2903112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 290473f53c4aSTejun Heo break; 290573f53c4aSTejun Heo 290673f53c4aSTejun Heo /* 290773f53c4aSTejun Heo * Meh... this color is already done, clear first 290873f53c4aSTejun Heo * flusher and repeat cascading. 290973f53c4aSTejun Heo */ 291073f53c4aSTejun Heo wq->first_flusher = NULL; 291173f53c4aSTejun Heo } 291273f53c4aSTejun Heo 291373f53c4aSTejun Heo out_unlock: 29143c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 29151da177e4SLinus Torvalds } 29161dadafa8STim Gardner EXPORT_SYMBOL(flush_workqueue); 29171da177e4SLinus Torvalds 29189c5a2ba7STejun Heo /** 29199c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 29209c5a2ba7STejun Heo * @wq: workqueue to drain 29219c5a2ba7STejun Heo * 29229c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 29239c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 29249c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 2925b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 29269c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 29279c5a2ba7STejun Heo * takes too long. 29289c5a2ba7STejun Heo */ 29299c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 29309c5a2ba7STejun Heo { 29319c5a2ba7STejun Heo unsigned int flush_cnt = 0; 293249e3cf44STejun Heo struct pool_workqueue *pwq; 29339c5a2ba7STejun Heo 29349c5a2ba7STejun Heo /* 29359c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 29369c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 2937618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 29389c5a2ba7STejun Heo */ 293987fc741eSLai Jiangshan mutex_lock(&wq->mutex); 29409c5a2ba7STejun Heo if (!wq->nr_drainers++) 2941618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 294287fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 29439c5a2ba7STejun Heo reflush: 29449c5a2ba7STejun Heo flush_workqueue(wq); 29459c5a2ba7STejun Heo 2946b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 294776af4d93STejun Heo 294849e3cf44STejun Heo for_each_pwq(pwq, wq) { 2949fa2563e4SThomas Tuttle bool drained; 29509c5a2ba7STejun Heo 2951b09f4fd3SLai Jiangshan spin_lock_irq(&pwq->pool->lock); 2952112202d9STejun Heo drained = !pwq->nr_active && list_empty(&pwq->delayed_works); 2953b09f4fd3SLai Jiangshan spin_unlock_irq(&pwq->pool->lock); 2954fa2563e4SThomas Tuttle 2955fa2563e4SThomas Tuttle if (drained) 29569c5a2ba7STejun Heo continue; 29579c5a2ba7STejun Heo 29589c5a2ba7STejun Heo if (++flush_cnt == 10 || 29599c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 2960c5aa87bbSTejun Heo pr_warn("workqueue %s: drain_workqueue() isn't complete after %u tries\n", 29619c5a2ba7STejun Heo wq->name, flush_cnt); 296276af4d93STejun Heo 2963b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 29649c5a2ba7STejun Heo goto reflush; 29659c5a2ba7STejun Heo } 29669c5a2ba7STejun Heo 29679c5a2ba7STejun Heo if (!--wq->nr_drainers) 2968618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 296987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 29709c5a2ba7STejun Heo } 29719c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 29729c5a2ba7STejun Heo 2973d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 2974d6e89786SJohannes Berg bool from_cancel) 2975baf59022STejun Heo { 2976baf59022STejun Heo struct worker *worker = NULL; 2977c9e7cf27STejun Heo struct worker_pool *pool; 2978112202d9STejun Heo struct pool_workqueue *pwq; 2979baf59022STejun Heo 2980baf59022STejun Heo might_sleep(); 2981baf59022STejun Heo 298224acfb71SThomas Gleixner rcu_read_lock(); 2983fa1b54e6STejun Heo pool = get_work_pool(work); 2984fa1b54e6STejun Heo if (!pool) { 298524acfb71SThomas Gleixner rcu_read_unlock(); 2986fa1b54e6STejun Heo return false; 2987fa1b54e6STejun Heo } 2988fa1b54e6STejun Heo 298924acfb71SThomas Gleixner spin_lock_irq(&pool->lock); 29900b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 2991112202d9STejun Heo pwq = get_work_pwq(work); 2992112202d9STejun Heo if (pwq) { 2993112202d9STejun Heo if (unlikely(pwq->pool != pool)) 2994baf59022STejun Heo goto already_gone; 2995606a5020STejun Heo } else { 2996c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 2997baf59022STejun Heo if (!worker) 2998baf59022STejun Heo goto already_gone; 2999112202d9STejun Heo pwq = worker->current_pwq; 3000606a5020STejun Heo } 3001baf59022STejun Heo 3002fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3003fca839c0STejun Heo 3004112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3005d565ed63STejun Heo spin_unlock_irq(&pool->lock); 3006baf59022STejun Heo 3007e159489bSTejun Heo /* 3008a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3009a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3010a1d14934SPeter Zijlstra * 3011a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3012a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3013a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3014a1d14934SPeter Zijlstra * forward progress. 3015e159489bSTejun Heo */ 3016d6e89786SJohannes Berg if (!from_cancel && 3017d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3018112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3019112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3020a1d14934SPeter Zijlstra } 302124acfb71SThomas Gleixner rcu_read_unlock(); 3022baf59022STejun Heo return true; 3023baf59022STejun Heo already_gone: 3024d565ed63STejun Heo spin_unlock_irq(&pool->lock); 302524acfb71SThomas Gleixner rcu_read_unlock(); 3026baf59022STejun Heo return false; 3027baf59022STejun Heo } 3028baf59022STejun Heo 3029d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3030d6e89786SJohannes Berg { 3031d6e89786SJohannes Berg struct wq_barrier barr; 3032d6e89786SJohannes Berg 3033d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3034d6e89786SJohannes Berg return false; 3035d6e89786SJohannes Berg 30364d43d395STetsuo Handa if (WARN_ON(!work->func)) 30374d43d395STetsuo Handa return false; 30384d43d395STetsuo Handa 303987915adcSJohannes Berg if (!from_cancel) { 304087915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 304187915adcSJohannes Berg lock_map_release(&work->lockdep_map); 304287915adcSJohannes Berg } 304387915adcSJohannes Berg 3044d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3045d6e89786SJohannes Berg wait_for_completion(&barr.done); 3046d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3047d6e89786SJohannes Berg return true; 3048d6e89786SJohannes Berg } else { 3049d6e89786SJohannes Berg return false; 3050d6e89786SJohannes Berg } 3051d6e89786SJohannes Berg } 3052d6e89786SJohannes Berg 3053db700897SOleg Nesterov /** 3054401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3055401a8d04STejun Heo * @work: the work to flush 3056db700897SOleg Nesterov * 3057606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3058606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3059401a8d04STejun Heo * 3060d185af30SYacine Belkadi * Return: 3061401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3062401a8d04STejun Heo * %false if it was already idle. 3063db700897SOleg Nesterov */ 3064401a8d04STejun Heo bool flush_work(struct work_struct *work) 3065db700897SOleg Nesterov { 3066d6e89786SJohannes Berg return __flush_work(work, false); 3067606a5020STejun Heo } 3068db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3069db700897SOleg Nesterov 30708603e1b3STejun Heo struct cwt_wait { 3071ac6424b9SIngo Molnar wait_queue_entry_t wait; 30728603e1b3STejun Heo struct work_struct *work; 30738603e1b3STejun Heo }; 30748603e1b3STejun Heo 3075ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 30768603e1b3STejun Heo { 30778603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 30788603e1b3STejun Heo 30798603e1b3STejun Heo if (cwait->work != key) 30808603e1b3STejun Heo return 0; 30818603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 30828603e1b3STejun Heo } 30838603e1b3STejun Heo 308436e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3085401a8d04STejun Heo { 30868603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3087bbb68dfaSTejun Heo unsigned long flags; 30881f1f642eSOleg Nesterov int ret; 30891f1f642eSOleg Nesterov 30901f1f642eSOleg Nesterov do { 3091bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3092bbb68dfaSTejun Heo /* 30938603e1b3STejun Heo * If someone else is already canceling, wait for it to 30948603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 30958603e1b3STejun Heo * because we may get scheduled between @work's completion 30968603e1b3STejun Heo * and the other canceling task resuming and clearing 30978603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 30988603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 30998603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 31008603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 31018603e1b3STejun Heo * we're hogging the CPU. 31028603e1b3STejun Heo * 31038603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 31048603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 31058603e1b3STejun Heo * wake function which matches @work along with exclusive 31068603e1b3STejun Heo * wait and wakeup. 3107bbb68dfaSTejun Heo */ 31088603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 31098603e1b3STejun Heo struct cwt_wait cwait; 31108603e1b3STejun Heo 31118603e1b3STejun Heo init_wait(&cwait.wait); 31128603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 31138603e1b3STejun Heo cwait.work = work; 31148603e1b3STejun Heo 31158603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 31168603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 31178603e1b3STejun Heo if (work_is_canceling(work)) 31188603e1b3STejun Heo schedule(); 31198603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 31208603e1b3STejun Heo } 31211f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 31221f1f642eSOleg Nesterov 3123bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3124bbb68dfaSTejun Heo mark_work_canceling(work); 3125bbb68dfaSTejun Heo local_irq_restore(flags); 3126bbb68dfaSTejun Heo 31273347fa09STejun Heo /* 31283347fa09STejun Heo * This allows canceling during early boot. We know that @work 31293347fa09STejun Heo * isn't executing. 31303347fa09STejun Heo */ 31313347fa09STejun Heo if (wq_online) 3132d6e89786SJohannes Berg __flush_work(work, true); 31333347fa09STejun Heo 31347a22ad75STejun Heo clear_work_data(work); 31358603e1b3STejun Heo 31368603e1b3STejun Heo /* 31378603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 31388603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 31398603e1b3STejun Heo * visible there. 31408603e1b3STejun Heo */ 31418603e1b3STejun Heo smp_mb(); 31428603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 31438603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 31448603e1b3STejun Heo 31451f1f642eSOleg Nesterov return ret; 31461f1f642eSOleg Nesterov } 31471f1f642eSOleg Nesterov 31486e84d644SOleg Nesterov /** 3149401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3150401a8d04STejun Heo * @work: the work to cancel 31516e84d644SOleg Nesterov * 3152401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3153401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3154401a8d04STejun Heo * another workqueue. On return from this function, @work is 3155401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 31561f1f642eSOleg Nesterov * 3157401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3158401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 31596e84d644SOleg Nesterov * 3160401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 31616e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3162401a8d04STejun Heo * 3163d185af30SYacine Belkadi * Return: 3164401a8d04STejun Heo * %true if @work was pending, %false otherwise. 31656e84d644SOleg Nesterov */ 3166401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 31676e84d644SOleg Nesterov { 316836e227d2STejun Heo return __cancel_work_timer(work, false); 3169b89deed3SOleg Nesterov } 317028e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3171b89deed3SOleg Nesterov 31726e84d644SOleg Nesterov /** 3173401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3174401a8d04STejun Heo * @dwork: the delayed work to flush 31756e84d644SOleg Nesterov * 3176401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3177401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3178401a8d04STejun Heo * considers the last queueing instance of @dwork. 31791f1f642eSOleg Nesterov * 3180d185af30SYacine Belkadi * Return: 3181401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3182401a8d04STejun Heo * %false if it was already idle. 31836e84d644SOleg Nesterov */ 3184401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3185401a8d04STejun Heo { 31868930cabaSTejun Heo local_irq_disable(); 3187401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 318860c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 31898930cabaSTejun Heo local_irq_enable(); 3190401a8d04STejun Heo return flush_work(&dwork->work); 3191401a8d04STejun Heo } 3192401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3193401a8d04STejun Heo 319405f0fe6bSTejun Heo /** 319505f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 319605f0fe6bSTejun Heo * @rwork: the rcu work to flush 319705f0fe6bSTejun Heo * 319805f0fe6bSTejun Heo * Return: 319905f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 320005f0fe6bSTejun Heo * %false if it was already idle. 320105f0fe6bSTejun Heo */ 320205f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 320305f0fe6bSTejun Heo { 320405f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 320505f0fe6bSTejun Heo rcu_barrier(); 320605f0fe6bSTejun Heo flush_work(&rwork->work); 320705f0fe6bSTejun Heo return true; 320805f0fe6bSTejun Heo } else { 320905f0fe6bSTejun Heo return flush_work(&rwork->work); 321005f0fe6bSTejun Heo } 321105f0fe6bSTejun Heo } 321205f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 321305f0fe6bSTejun Heo 3214f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3215f72b8792SJens Axboe { 3216f72b8792SJens Axboe unsigned long flags; 3217f72b8792SJens Axboe int ret; 3218f72b8792SJens Axboe 3219f72b8792SJens Axboe do { 3220f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3221f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3222f72b8792SJens Axboe 3223f72b8792SJens Axboe if (unlikely(ret < 0)) 3224f72b8792SJens Axboe return false; 3225f72b8792SJens Axboe 3226f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3227f72b8792SJens Axboe local_irq_restore(flags); 3228f72b8792SJens Axboe return ret; 3229f72b8792SJens Axboe } 3230f72b8792SJens Axboe 3231401a8d04STejun Heo /** 323257b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 323357b30ae7STejun Heo * @dwork: delayed_work to cancel 323409383498STejun Heo * 3235d185af30SYacine Belkadi * Kill off a pending delayed_work. 3236d185af30SYacine Belkadi * 3237d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3238d185af30SYacine Belkadi * pending. 3239d185af30SYacine Belkadi * 3240d185af30SYacine Belkadi * Note: 3241d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3242d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3243d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 324409383498STejun Heo * 324557b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 324609383498STejun Heo */ 324757b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 324809383498STejun Heo { 3249f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 325009383498STejun Heo } 325157b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 325209383498STejun Heo 325309383498STejun Heo /** 3254401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3255401a8d04STejun Heo * @dwork: the delayed work cancel 3256401a8d04STejun Heo * 3257401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3258401a8d04STejun Heo * 3259d185af30SYacine Belkadi * Return: 3260401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3261401a8d04STejun Heo */ 3262401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 32636e84d644SOleg Nesterov { 326436e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 32656e84d644SOleg Nesterov } 3266f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 32671da177e4SLinus Torvalds 32680fcb78c2SRolf Eike Beer /** 326931ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3270b6136773SAndrew Morton * @func: the function to call 3271b6136773SAndrew Morton * 327231ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 327331ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3274b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 327531ddd871STejun Heo * 3276d185af30SYacine Belkadi * Return: 327731ddd871STejun Heo * 0 on success, -errno on failure. 3278b6136773SAndrew Morton */ 327965f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 328015316ba8SChristoph Lameter { 328115316ba8SChristoph Lameter int cpu; 328238f51568SNamhyung Kim struct work_struct __percpu *works; 328315316ba8SChristoph Lameter 3284b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3285b6136773SAndrew Morton if (!works) 328615316ba8SChristoph Lameter return -ENOMEM; 3287b6136773SAndrew Morton 328895402b38SGautham R Shenoy get_online_cpus(); 328993981800STejun Heo 329015316ba8SChristoph Lameter for_each_online_cpu(cpu) { 32919bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 32929bfb1839SIngo Molnar 32939bfb1839SIngo Molnar INIT_WORK(work, func); 32948de6d308SOleg Nesterov schedule_work_on(cpu, work); 329515316ba8SChristoph Lameter } 329693981800STejun Heo 329793981800STejun Heo for_each_online_cpu(cpu) 32988616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 329993981800STejun Heo 330095402b38SGautham R Shenoy put_online_cpus(); 3301b6136773SAndrew Morton free_percpu(works); 330215316ba8SChristoph Lameter return 0; 330315316ba8SChristoph Lameter } 330415316ba8SChristoph Lameter 3305eef6a7d5SAlan Stern /** 33061fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 33071fa44ecaSJames Bottomley * @fn: the function to execute 33081fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 33091fa44ecaSJames Bottomley * be available when the work executes) 33101fa44ecaSJames Bottomley * 33111fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 33121fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 33131fa44ecaSJames Bottomley * 3314d185af30SYacine Belkadi * Return: 0 - function was executed 33151fa44ecaSJames Bottomley * 1 - function was scheduled for execution 33161fa44ecaSJames Bottomley */ 331765f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 33181fa44ecaSJames Bottomley { 33191fa44ecaSJames Bottomley if (!in_interrupt()) { 332065f27f38SDavid Howells fn(&ew->work); 33211fa44ecaSJames Bottomley return 0; 33221fa44ecaSJames Bottomley } 33231fa44ecaSJames Bottomley 332465f27f38SDavid Howells INIT_WORK(&ew->work, fn); 33251fa44ecaSJames Bottomley schedule_work(&ew->work); 33261fa44ecaSJames Bottomley 33271fa44ecaSJames Bottomley return 1; 33281fa44ecaSJames Bottomley } 33291fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 33301fa44ecaSJames Bottomley 33317a4e344cSTejun Heo /** 33327a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 33337a4e344cSTejun Heo * @attrs: workqueue_attrs to free 33347a4e344cSTejun Heo * 33357a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 33367a4e344cSTejun Heo */ 3337513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 33387a4e344cSTejun Heo { 33397a4e344cSTejun Heo if (attrs) { 33407a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 33417a4e344cSTejun Heo kfree(attrs); 33427a4e344cSTejun Heo } 33437a4e344cSTejun Heo } 33447a4e344cSTejun Heo 33457a4e344cSTejun Heo /** 33467a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 33477a4e344cSTejun Heo * 33487a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3349d185af30SYacine Belkadi * return it. 3350d185af30SYacine Belkadi * 3351d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 33527a4e344cSTejun Heo */ 3353513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 33547a4e344cSTejun Heo { 33557a4e344cSTejun Heo struct workqueue_attrs *attrs; 33567a4e344cSTejun Heo 3357be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 33587a4e344cSTejun Heo if (!attrs) 33597a4e344cSTejun Heo goto fail; 3360be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 33617a4e344cSTejun Heo goto fail; 33627a4e344cSTejun Heo 336313e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 33647a4e344cSTejun Heo return attrs; 33657a4e344cSTejun Heo fail: 33667a4e344cSTejun Heo free_workqueue_attrs(attrs); 33677a4e344cSTejun Heo return NULL; 33687a4e344cSTejun Heo } 33697a4e344cSTejun Heo 337029c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 337129c91e99STejun Heo const struct workqueue_attrs *from) 337229c91e99STejun Heo { 337329c91e99STejun Heo to->nice = from->nice; 337429c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 33752865a8fbSShaohua Li /* 33762865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 33772865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 33782865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 33792865a8fbSShaohua Li */ 33802865a8fbSShaohua Li to->no_numa = from->no_numa; 338129c91e99STejun Heo } 338229c91e99STejun Heo 338329c91e99STejun Heo /* hash value of the content of @attr */ 338429c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 338529c91e99STejun Heo { 338629c91e99STejun Heo u32 hash = 0; 338729c91e99STejun Heo 338829c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 338913e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 339013e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 339129c91e99STejun Heo return hash; 339229c91e99STejun Heo } 339329c91e99STejun Heo 339429c91e99STejun Heo /* content equality test */ 339529c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 339629c91e99STejun Heo const struct workqueue_attrs *b) 339729c91e99STejun Heo { 339829c91e99STejun Heo if (a->nice != b->nice) 339929c91e99STejun Heo return false; 340029c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 340129c91e99STejun Heo return false; 340229c91e99STejun Heo return true; 340329c91e99STejun Heo } 340429c91e99STejun Heo 34057a4e344cSTejun Heo /** 34067a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 34077a4e344cSTejun Heo * @pool: worker_pool to initialize 34087a4e344cSTejun Heo * 3409402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3410d185af30SYacine Belkadi * 3411d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 341229c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 341329c91e99STejun Heo * on @pool safely to release it. 34147a4e344cSTejun Heo */ 34157a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 34164e1a1f9aSTejun Heo { 34174e1a1f9aSTejun Heo spin_lock_init(&pool->lock); 341829c91e99STejun Heo pool->id = -1; 341929c91e99STejun Heo pool->cpu = -1; 3420f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 34214e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 342282607adcSTejun Heo pool->watchdog_ts = jiffies; 34234e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 34244e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 34254e1a1f9aSTejun Heo hash_init(pool->busy_hash); 34264e1a1f9aSTejun Heo 342732a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 34284e1a1f9aSTejun Heo 342932a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 34304e1a1f9aSTejun Heo 3431da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 34327a4e344cSTejun Heo 34337cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 343429c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 343529c91e99STejun Heo pool->refcnt = 1; 343629c91e99STejun Heo 343729c91e99STejun Heo /* shouldn't fail above this point */ 3438be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 34397a4e344cSTejun Heo if (!pool->attrs) 34407a4e344cSTejun Heo return -ENOMEM; 34417a4e344cSTejun Heo return 0; 34424e1a1f9aSTejun Heo } 34434e1a1f9aSTejun Heo 3444669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3445669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3446669de8bdSBart Van Assche { 3447669de8bdSBart Van Assche char *lock_name; 3448669de8bdSBart Van Assche 3449669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3450669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3451669de8bdSBart Van Assche if (!lock_name) 3452669de8bdSBart Van Assche lock_name = wq->name; 345369a106c0SQian Cai 345469a106c0SQian Cai wq->lock_name = lock_name; 3455669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3456669de8bdSBart Van Assche } 3457669de8bdSBart Van Assche 3458669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3459669de8bdSBart Van Assche { 3460669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3461669de8bdSBart Van Assche } 3462669de8bdSBart Van Assche 3463669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3464669de8bdSBart Van Assche { 3465669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3466669de8bdSBart Van Assche kfree(wq->lock_name); 3467669de8bdSBart Van Assche } 3468669de8bdSBart Van Assche #else 3469669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3470669de8bdSBart Van Assche { 3471669de8bdSBart Van Assche } 3472669de8bdSBart Van Assche 3473669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3474669de8bdSBart Van Assche { 3475669de8bdSBart Van Assche } 3476669de8bdSBart Van Assche 3477669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3478669de8bdSBart Van Assche { 3479669de8bdSBart Van Assche } 3480669de8bdSBart Van Assche #endif 3481669de8bdSBart Van Assche 3482e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3483e2dca7adSTejun Heo { 3484e2dca7adSTejun Heo struct workqueue_struct *wq = 3485e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3486e2dca7adSTejun Heo 3487669de8bdSBart Van Assche wq_free_lockdep(wq); 3488669de8bdSBart Van Assche 3489e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3490e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3491e2dca7adSTejun Heo else 3492e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3493e2dca7adSTejun Heo 3494e2dca7adSTejun Heo kfree(wq->rescuer); 3495e2dca7adSTejun Heo kfree(wq); 3496e2dca7adSTejun Heo } 3497e2dca7adSTejun Heo 349829c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 349929c91e99STejun Heo { 350029c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 350129c91e99STejun Heo 35027cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 350329c91e99STejun Heo free_workqueue_attrs(pool->attrs); 350429c91e99STejun Heo kfree(pool); 350529c91e99STejun Heo } 350629c91e99STejun Heo 350729c91e99STejun Heo /** 350829c91e99STejun Heo * put_unbound_pool - put a worker_pool 350929c91e99STejun Heo * @pool: worker_pool to put 351029c91e99STejun Heo * 351124acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3512c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3513c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3514c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3515a892caccSTejun Heo * 3516a892caccSTejun Heo * Should be called with wq_pool_mutex held. 351729c91e99STejun Heo */ 351829c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 351929c91e99STejun Heo { 352060f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 352129c91e99STejun Heo struct worker *worker; 352229c91e99STejun Heo 3523a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3524a892caccSTejun Heo 3525a892caccSTejun Heo if (--pool->refcnt) 352629c91e99STejun Heo return; 352729c91e99STejun Heo 352829c91e99STejun Heo /* sanity checks */ 352961d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3530a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 353129c91e99STejun Heo return; 353229c91e99STejun Heo 353329c91e99STejun Heo /* release id and unhash */ 353429c91e99STejun Heo if (pool->id >= 0) 353529c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 353629c91e99STejun Heo hash_del(&pool->hash_node); 353729c91e99STejun Heo 3538c5aa87bbSTejun Heo /* 3539692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3540692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3541692b4825STejun Heo * manager and @pool gets freed with the flag set. 3542c5aa87bbSTejun Heo */ 354360f5a4bcSLai Jiangshan spin_lock_irq(&pool->lock); 3544692b4825STejun Heo wait_event_lock_irq(wq_manager_wait, 3545692b4825STejun Heo !(pool->flags & POOL_MANAGER_ACTIVE), pool->lock); 3546692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 3547692b4825STejun Heo 35481037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 354929c91e99STejun Heo destroy_worker(worker); 355029c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 355129c91e99STejun Heo spin_unlock_irq(&pool->lock); 355260f5a4bcSLai Jiangshan 35531258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 3554da028469SLai Jiangshan if (!list_empty(&pool->workers)) 355560f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 35561258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 355760f5a4bcSLai Jiangshan 355860f5a4bcSLai Jiangshan if (pool->detach_completion) 355960f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 356060f5a4bcSLai Jiangshan 356129c91e99STejun Heo /* shut down the timers */ 356229c91e99STejun Heo del_timer_sync(&pool->idle_timer); 356329c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 356429c91e99STejun Heo 356524acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 356625b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 356729c91e99STejun Heo } 356829c91e99STejun Heo 356929c91e99STejun Heo /** 357029c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 357129c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 357229c91e99STejun Heo * 357329c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 357429c91e99STejun Heo * reference count and return it. If there already is a matching 357529c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3576d185af30SYacine Belkadi * create a new one. 3577a892caccSTejun Heo * 3578a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3579d185af30SYacine Belkadi * 3580d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3581d185af30SYacine Belkadi * On failure, %NULL. 358229c91e99STejun Heo */ 358329c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 358429c91e99STejun Heo { 358529c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 358629c91e99STejun Heo struct worker_pool *pool; 3587f3f90ad4STejun Heo int node; 3588e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 358929c91e99STejun Heo 3590a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 359129c91e99STejun Heo 359229c91e99STejun Heo /* do we already have a matching pool? */ 359329c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 359429c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 359529c91e99STejun Heo pool->refcnt++; 35963fb1823cSLai Jiangshan return pool; 359729c91e99STejun Heo } 359829c91e99STejun Heo } 359929c91e99STejun Heo 3600e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3601e2273584SXunlei Pang if (wq_numa_enabled) { 3602e2273584SXunlei Pang for_each_node(node) { 3603e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3604e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3605e2273584SXunlei Pang target_node = node; 3606e2273584SXunlei Pang break; 3607e2273584SXunlei Pang } 3608e2273584SXunlei Pang } 3609e2273584SXunlei Pang } 3610e2273584SXunlei Pang 361129c91e99STejun Heo /* nope, create a new one */ 3612e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 361329c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 361429c91e99STejun Heo goto fail; 361529c91e99STejun Heo 36168864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 361729c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3618e2273584SXunlei Pang pool->node = target_node; 361929c91e99STejun Heo 36202865a8fbSShaohua Li /* 36212865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 36222865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 36232865a8fbSShaohua Li */ 36242865a8fbSShaohua Li pool->attrs->no_numa = false; 36252865a8fbSShaohua Li 362629c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 362729c91e99STejun Heo goto fail; 362829c91e99STejun Heo 362929c91e99STejun Heo /* create and start the initial worker */ 36303347fa09STejun Heo if (wq_online && !create_worker(pool)) 363129c91e99STejun Heo goto fail; 363229c91e99STejun Heo 363329c91e99STejun Heo /* install */ 363429c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 36353fb1823cSLai Jiangshan 363629c91e99STejun Heo return pool; 363729c91e99STejun Heo fail: 363829c91e99STejun Heo if (pool) 363929c91e99STejun Heo put_unbound_pool(pool); 364029c91e99STejun Heo return NULL; 364129c91e99STejun Heo } 364229c91e99STejun Heo 36438864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 36448864b4e5STejun Heo { 36458864b4e5STejun Heo kmem_cache_free(pwq_cache, 36468864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 36478864b4e5STejun Heo } 36488864b4e5STejun Heo 36498864b4e5STejun Heo /* 36508864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 36518864b4e5STejun Heo * and needs to be destroyed. 36528864b4e5STejun Heo */ 36538864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 36548864b4e5STejun Heo { 36558864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 36568864b4e5STejun Heo unbound_release_work); 36578864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 36588864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3659bc0caf09STejun Heo bool is_last; 36608864b4e5STejun Heo 36618864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 36628864b4e5STejun Heo return; 36638864b4e5STejun Heo 36643c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 36658864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3666bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 36673c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 36688864b4e5STejun Heo 3669a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 36708864b4e5STejun Heo put_unbound_pool(pool); 3671a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3672a892caccSTejun Heo 367325b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 36748864b4e5STejun Heo 36758864b4e5STejun Heo /* 36768864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3677e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 36788864b4e5STejun Heo */ 3679669de8bdSBart Van Assche if (is_last) { 3680669de8bdSBart Van Assche wq_unregister_lockdep(wq); 368125b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 36826029a918STejun Heo } 3683669de8bdSBart Van Assche } 36848864b4e5STejun Heo 36850fbd95aaSTejun Heo /** 3686699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 36870fbd95aaSTejun Heo * @pwq: target pool_workqueue 36880fbd95aaSTejun Heo * 3689699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3690699ce097STejun Heo * workqueue's saved_max_active and activate delayed work items 3691699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 36920fbd95aaSTejun Heo */ 3693699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 36940fbd95aaSTejun Heo { 3695699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3696699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 36973347fa09STejun Heo unsigned long flags; 3698699ce097STejun Heo 3699699ce097STejun Heo /* for @wq->saved_max_active */ 3700a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3701699ce097STejun Heo 3702699ce097STejun Heo /* fast exit for non-freezable wqs */ 3703699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3704699ce097STejun Heo return; 3705699ce097STejun Heo 37063347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 37073347fa09STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 3708699ce097STejun Heo 370974b414eaSLai Jiangshan /* 371074b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 371174b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 371274b414eaSLai Jiangshan * is updated and visible. 371374b414eaSLai Jiangshan */ 371474b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 3715699ce097STejun Heo pwq->max_active = wq->saved_max_active; 37160fbd95aaSTejun Heo 37170fbd95aaSTejun Heo while (!list_empty(&pwq->delayed_works) && 37180fbd95aaSTejun Heo pwq->nr_active < pwq->max_active) 37190fbd95aaSTejun Heo pwq_activate_first_delayed(pwq); 3720951a078aSLai Jiangshan 3721951a078aSLai Jiangshan /* 3722951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 3723951a078aSLai Jiangshan * max_active is bumped. It's a slow path. Do it always. 3724951a078aSLai Jiangshan */ 3725951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3726699ce097STejun Heo } else { 3727699ce097STejun Heo pwq->max_active = 0; 3728699ce097STejun Heo } 3729699ce097STejun Heo 37303347fa09STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 37310fbd95aaSTejun Heo } 37320fbd95aaSTejun Heo 3733e50aba9aSTejun Heo /* initialize newly alloced @pwq which is associated with @wq and @pool */ 3734f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3735f147f29eSTejun Heo struct worker_pool *pool) 3736d2c1d404STejun Heo { 3737d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3738d2c1d404STejun Heo 3739e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3740e50aba9aSTejun Heo 3741d2c1d404STejun Heo pwq->pool = pool; 3742d2c1d404STejun Heo pwq->wq = wq; 3743d2c1d404STejun Heo pwq->flush_color = -1; 37448864b4e5STejun Heo pwq->refcnt = 1; 3745d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->delayed_works); 37461befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3747d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 37488864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3749f147f29eSTejun Heo } 3750d2c1d404STejun Heo 3751f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 37521befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3753f147f29eSTejun Heo { 3754f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3755f147f29eSTejun Heo 3756f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 375775ccf595STejun Heo 37581befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 37591befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 37601befcf30STejun Heo return; 37611befcf30STejun Heo 376229b1cb41SLai Jiangshan /* set the matching work_color */ 376375ccf595STejun Heo pwq->work_color = wq->work_color; 3764983ca25eSTejun Heo 3765983ca25eSTejun Heo /* sync max_active to the current setting */ 3766983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3767983ca25eSTejun Heo 3768983ca25eSTejun Heo /* link in @pwq */ 37699e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3770df2d5ae4STejun Heo } 37716029a918STejun Heo 3772f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3773f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3774f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3775f147f29eSTejun Heo { 3776f147f29eSTejun Heo struct worker_pool *pool; 3777f147f29eSTejun Heo struct pool_workqueue *pwq; 3778f147f29eSTejun Heo 3779f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3780f147f29eSTejun Heo 3781f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3782f147f29eSTejun Heo if (!pool) 3783f147f29eSTejun Heo return NULL; 3784f147f29eSTejun Heo 3785e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3786f147f29eSTejun Heo if (!pwq) { 3787f147f29eSTejun Heo put_unbound_pool(pool); 3788f147f29eSTejun Heo return NULL; 3789f147f29eSTejun Heo } 3790f147f29eSTejun Heo 3791f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3792f147f29eSTejun Heo return pwq; 3793d2c1d404STejun Heo } 3794d2c1d404STejun Heo 37954c16bd32STejun Heo /** 379630186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 3797042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 37984c16bd32STejun Heo * @node: the target NUMA node 37994c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 38004c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 38014c16bd32STejun Heo * 38024c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 38034c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3804d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 38054c16bd32STejun Heo * 38064c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 38074c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 38084c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 38094c16bd32STejun Heo * @attrs->cpumask. 38104c16bd32STejun Heo * 38114c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 38124c16bd32STejun Heo * stable. 3813d185af30SYacine Belkadi * 3814d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 3815d185af30SYacine Belkadi * %false if equal. 38164c16bd32STejun Heo */ 38174c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 38184c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 38194c16bd32STejun Heo { 3820d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 38214c16bd32STejun Heo goto use_dfl; 38224c16bd32STejun Heo 38234c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 38244c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 38254c16bd32STejun Heo if (cpu_going_down >= 0) 38264c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 38274c16bd32STejun Heo 38284c16bd32STejun Heo if (cpumask_empty(cpumask)) 38294c16bd32STejun Heo goto use_dfl; 38304c16bd32STejun Heo 38314c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 38324c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 38331ad0f0a7SMichael Bringmann 38341ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 38351ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 38361ad0f0a7SMichael Bringmann "possible intersect\n"); 38371ad0f0a7SMichael Bringmann return false; 38381ad0f0a7SMichael Bringmann } 38391ad0f0a7SMichael Bringmann 38404c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 38414c16bd32STejun Heo 38424c16bd32STejun Heo use_dfl: 38434c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 38444c16bd32STejun Heo return false; 38454c16bd32STejun Heo } 38464c16bd32STejun Heo 38471befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 38481befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 38491befcf30STejun Heo int node, 38501befcf30STejun Heo struct pool_workqueue *pwq) 38511befcf30STejun Heo { 38521befcf30STejun Heo struct pool_workqueue *old_pwq; 38531befcf30STejun Heo 38545b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 38551befcf30STejun Heo lockdep_assert_held(&wq->mutex); 38561befcf30STejun Heo 38571befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 38581befcf30STejun Heo link_pwq(pwq); 38591befcf30STejun Heo 38601befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 38611befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 38621befcf30STejun Heo return old_pwq; 38631befcf30STejun Heo } 38641befcf30STejun Heo 38652d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 38662d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 38672d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 38682d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 3869042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 38702d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 38712d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 38722d5f0764SLai Jiangshan }; 38732d5f0764SLai Jiangshan 38742d5f0764SLai Jiangshan /* free the resources after success or abort */ 38752d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 38762d5f0764SLai Jiangshan { 38772d5f0764SLai Jiangshan if (ctx) { 38782d5f0764SLai Jiangshan int node; 38792d5f0764SLai Jiangshan 38802d5f0764SLai Jiangshan for_each_node(node) 38812d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 38822d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 38832d5f0764SLai Jiangshan 38842d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 38852d5f0764SLai Jiangshan 38862d5f0764SLai Jiangshan kfree(ctx); 38872d5f0764SLai Jiangshan } 38882d5f0764SLai Jiangshan } 38892d5f0764SLai Jiangshan 38902d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 38912d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 38922d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 38932d5f0764SLai Jiangshan const struct workqueue_attrs *attrs) 38942d5f0764SLai Jiangshan { 38952d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 38962d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 38972d5f0764SLai Jiangshan int node; 38982d5f0764SLai Jiangshan 38992d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 39002d5f0764SLai Jiangshan 3901acafe7e3SKees Cook ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL); 39022d5f0764SLai Jiangshan 3903be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 3904be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 39052d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 39062d5f0764SLai Jiangshan goto out_free; 39072d5f0764SLai Jiangshan 3908042f7df1SLai Jiangshan /* 3909042f7df1SLai Jiangshan * Calculate the attrs of the default pwq. 3910042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 3911042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 3912042f7df1SLai Jiangshan */ 39132d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3914b05a7928SFrederic Weisbecker cpumask_and(new_attrs->cpumask, new_attrs->cpumask, wq_unbound_cpumask); 3915042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 3916042f7df1SLai Jiangshan cpumask_copy(new_attrs->cpumask, wq_unbound_cpumask); 39172d5f0764SLai Jiangshan 39182d5f0764SLai Jiangshan /* 39192d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 39202d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 39212d5f0764SLai Jiangshan * pools. 39222d5f0764SLai Jiangshan */ 39232d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 39242d5f0764SLai Jiangshan 39252d5f0764SLai Jiangshan /* 39262d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 39272d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 39282d5f0764SLai Jiangshan * it even if we don't use it immediately. 39292d5f0764SLai Jiangshan */ 39302d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 39312d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 39322d5f0764SLai Jiangshan goto out_free; 39332d5f0764SLai Jiangshan 39342d5f0764SLai Jiangshan for_each_node(node) { 3935042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 39362d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 39372d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 39382d5f0764SLai Jiangshan goto out_free; 39392d5f0764SLai Jiangshan } else { 39402d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 39412d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 39422d5f0764SLai Jiangshan } 39432d5f0764SLai Jiangshan } 39442d5f0764SLai Jiangshan 3945042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 3946042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 3947042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 39482d5f0764SLai Jiangshan ctx->attrs = new_attrs; 3949042f7df1SLai Jiangshan 39502d5f0764SLai Jiangshan ctx->wq = wq; 39512d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 39522d5f0764SLai Jiangshan return ctx; 39532d5f0764SLai Jiangshan 39542d5f0764SLai Jiangshan out_free: 39552d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 39562d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 39572d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 39582d5f0764SLai Jiangshan return NULL; 39592d5f0764SLai Jiangshan } 39602d5f0764SLai Jiangshan 39612d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 39622d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 39632d5f0764SLai Jiangshan { 39642d5f0764SLai Jiangshan int node; 39652d5f0764SLai Jiangshan 39662d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 39672d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 39682d5f0764SLai Jiangshan 39692d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 39702d5f0764SLai Jiangshan 39712d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 39722d5f0764SLai Jiangshan for_each_node(node) 39732d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 39742d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 39752d5f0764SLai Jiangshan 39762d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 39772d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 39782d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 39792d5f0764SLai Jiangshan 39802d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 39812d5f0764SLai Jiangshan } 39822d5f0764SLai Jiangshan 3983a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 3984a0111cf6SLai Jiangshan { 3985a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 3986a0111cf6SLai Jiangshan get_online_cpus(); 3987a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 3988a0111cf6SLai Jiangshan } 3989a0111cf6SLai Jiangshan 3990a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 3991a0111cf6SLai Jiangshan { 3992a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 3993a0111cf6SLai Jiangshan put_online_cpus(); 3994a0111cf6SLai Jiangshan } 3995a0111cf6SLai Jiangshan 3996a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 3997a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 3998a0111cf6SLai Jiangshan { 3999a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4000a0111cf6SLai Jiangshan 4001a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4002a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4003a0111cf6SLai Jiangshan return -EINVAL; 4004a0111cf6SLai Jiangshan 4005a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 40060a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 40070a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4008a0111cf6SLai Jiangshan return -EINVAL; 4009a0111cf6SLai Jiangshan 40100a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 40110a94efb5STejun Heo } 40120a94efb5STejun Heo 4013a0111cf6SLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs); 40146201171eSwanghaibin if (!ctx) 40156201171eSwanghaibin return -ENOMEM; 4016a0111cf6SLai Jiangshan 4017a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4018a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4019a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4020a0111cf6SLai Jiangshan 40216201171eSwanghaibin return 0; 4022a0111cf6SLai Jiangshan } 4023a0111cf6SLai Jiangshan 40249e8cd2f5STejun Heo /** 40259e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 40269e8cd2f5STejun Heo * @wq: the target workqueue 40279e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 40289e8cd2f5STejun Heo * 40294c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 40304c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 40314c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 40324c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 40334c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 40344c16bd32STejun Heo * back-to-back will stay on its current pwq. 40359e8cd2f5STejun Heo * 4036d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4037d185af30SYacine Belkadi * 4038509b3204SDaniel Jordan * Assumes caller has CPU hotplug read exclusion, i.e. get_online_cpus(). 4039509b3204SDaniel Jordan * 4040d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 40419e8cd2f5STejun Heo */ 4042513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 40439e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 40449e8cd2f5STejun Heo { 4045a0111cf6SLai Jiangshan int ret; 40469e8cd2f5STejun Heo 4047509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4048509b3204SDaniel Jordan 4049509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4050a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4051509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 40522d5f0764SLai Jiangshan 40532d5f0764SLai Jiangshan return ret; 40549e8cd2f5STejun Heo } 40559e8cd2f5STejun Heo 40564c16bd32STejun Heo /** 40574c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 40584c16bd32STejun Heo * @wq: the target workqueue 40594c16bd32STejun Heo * @cpu: the CPU coming up or going down 40604c16bd32STejun Heo * @online: whether @cpu is coming up or going down 40614c16bd32STejun Heo * 40624c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 40634c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 40644c16bd32STejun Heo * @wq accordingly. 40654c16bd32STejun Heo * 40664c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 40674c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 40684c16bd32STejun Heo * correct. 40694c16bd32STejun Heo * 40704c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 40714c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 40724c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 40734c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 40744c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 40754c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 40764c16bd32STejun Heo * CPU_DOWN_PREPARE. 40774c16bd32STejun Heo */ 40784c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 40794c16bd32STejun Heo bool online) 40804c16bd32STejun Heo { 40814c16bd32STejun Heo int node = cpu_to_node(cpu); 40824c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 40834c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 40844c16bd32STejun Heo struct workqueue_attrs *target_attrs; 40854c16bd32STejun Heo cpumask_t *cpumask; 40864c16bd32STejun Heo 40874c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 40884c16bd32STejun Heo 4089f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 4090f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 40914c16bd32STejun Heo return; 40924c16bd32STejun Heo 40934c16bd32STejun Heo /* 40944c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 40954c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 40964c16bd32STejun Heo * CPU hotplug exclusion. 40974c16bd32STejun Heo */ 40984c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 40994c16bd32STejun Heo cpumask = target_attrs->cpumask; 41004c16bd32STejun Heo 41014c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 41024c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 41034c16bd32STejun Heo 41044c16bd32STejun Heo /* 41054c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 4106042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 4107042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 4108042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 41094c16bd32STejun Heo */ 4110042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 41114c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4112f7142ed4SLai Jiangshan return; 41134c16bd32STejun Heo } else { 41144c16bd32STejun Heo goto use_dfl_pwq; 41154c16bd32STejun Heo } 41164c16bd32STejun Heo 41174c16bd32STejun Heo /* create a new pwq */ 41184c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 41194c16bd32STejun Heo if (!pwq) { 41202d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 41214c16bd32STejun Heo wq->name); 412277f300b1SDaeseok Youn goto use_dfl_pwq; 41234c16bd32STejun Heo } 41244c16bd32STejun Heo 4125f7142ed4SLai Jiangshan /* Install the new pwq. */ 41264c16bd32STejun Heo mutex_lock(&wq->mutex); 41274c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 41284c16bd32STejun Heo goto out_unlock; 41294c16bd32STejun Heo 41304c16bd32STejun Heo use_dfl_pwq: 4131f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 41324c16bd32STejun Heo spin_lock_irq(&wq->dfl_pwq->pool->lock); 41334c16bd32STejun Heo get_pwq(wq->dfl_pwq); 41344c16bd32STejun Heo spin_unlock_irq(&wq->dfl_pwq->pool->lock); 41354c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 41364c16bd32STejun Heo out_unlock: 41374c16bd32STejun Heo mutex_unlock(&wq->mutex); 41384c16bd32STejun Heo put_pwq_unlocked(old_pwq); 41394c16bd32STejun Heo } 41404c16bd32STejun Heo 414130cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 41421da177e4SLinus Torvalds { 414349e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 41448a2b7538STejun Heo int cpu, ret; 4145e1d8aa9fSFrederic Weisbecker 414630cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4147420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 4148420c0ddbSTejun Heo if (!wq->cpu_pwqs) 414930cdf249STejun Heo return -ENOMEM; 415030cdf249STejun Heo 415130cdf249STejun Heo for_each_possible_cpu(cpu) { 41527fb98ea7STejun Heo struct pool_workqueue *pwq = 41537fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 41547a62c2c8STejun Heo struct worker_pool *cpu_pools = 4155f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 415630cdf249STejun Heo 4157f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 4158f147f29eSTejun Heo 4159f147f29eSTejun Heo mutex_lock(&wq->mutex); 41601befcf30STejun Heo link_pwq(pwq); 4161f147f29eSTejun Heo mutex_unlock(&wq->mutex); 416230cdf249STejun Heo } 416330cdf249STejun Heo return 0; 4164509b3204SDaniel Jordan } 4165509b3204SDaniel Jordan 4166509b3204SDaniel Jordan get_online_cpus(); 4167509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 41688a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 41698a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 41708a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 41718a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 41728a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 41739e8cd2f5STejun Heo } else { 4174509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 41759e8cd2f5STejun Heo } 4176509b3204SDaniel Jordan put_online_cpus(); 4177509b3204SDaniel Jordan 4178509b3204SDaniel Jordan return ret; 41790f900049STejun Heo } 41800f900049STejun Heo 4181f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4182f3421797STejun Heo const char *name) 4183b71ab8c2STejun Heo { 4184f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 4185f3421797STejun Heo 4186f3421797STejun Heo if (max_active < 1 || max_active > lim) 4187044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4188f3421797STejun Heo max_active, name, 1, lim); 4189b71ab8c2STejun Heo 4190f3421797STejun Heo return clamp_val(max_active, 1, lim); 4191b71ab8c2STejun Heo } 4192b71ab8c2STejun Heo 4193983c7515STejun Heo /* 4194983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4195983c7515STejun Heo * to guarantee forward progress. 4196983c7515STejun Heo */ 4197983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4198983c7515STejun Heo { 4199983c7515STejun Heo struct worker *rescuer; 4200*b92b36eaSDan Carpenter int ret; 4201983c7515STejun Heo 4202983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4203983c7515STejun Heo return 0; 4204983c7515STejun Heo 4205983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 4206983c7515STejun Heo if (!rescuer) 4207983c7515STejun Heo return -ENOMEM; 4208983c7515STejun Heo 4209983c7515STejun Heo rescuer->rescue_wq = wq; 4210983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4211f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4212*b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 4213983c7515STejun Heo kfree(rescuer); 4214*b92b36eaSDan Carpenter return ret; 4215983c7515STejun Heo } 4216983c7515STejun Heo 4217983c7515STejun Heo wq->rescuer = rescuer; 4218983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4219983c7515STejun Heo wake_up_process(rescuer->task); 4220983c7515STejun Heo 4221983c7515STejun Heo return 0; 4222983c7515STejun Heo } 4223983c7515STejun Heo 4224a2775bbcSMathieu Malaterre __printf(1, 4) 4225669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 422697e37d7bSTejun Heo unsigned int flags, 4227669de8bdSBart Van Assche int max_active, ...) 42283af24433SOleg Nesterov { 4229df2d5ae4STejun Heo size_t tbl_size = 0; 4230ecf6881fSTejun Heo va_list args; 42313af24433SOleg Nesterov struct workqueue_struct *wq; 423249e3cf44STejun Heo struct pool_workqueue *pwq; 4233b196be89STejun Heo 42345c0338c6STejun Heo /* 42355c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 42365c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 42375c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 42385c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 42395c0338c6STejun Heo * on NUMA. 42405c0338c6STejun Heo */ 42415c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 42425c0338c6STejun Heo flags |= __WQ_ORDERED; 42435c0338c6STejun Heo 4244cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4245cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4246cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4247cee22a15SViresh Kumar 4248ecf6881fSTejun Heo /* allocate wq and format name */ 4249df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 4250ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 4251df2d5ae4STejun Heo 4252df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 4253b196be89STejun Heo if (!wq) 4254d2c1d404STejun Heo return NULL; 4255b196be89STejun Heo 42566029a918STejun Heo if (flags & WQ_UNBOUND) { 4257be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 42586029a918STejun Heo if (!wq->unbound_attrs) 42596029a918STejun Heo goto err_free_wq; 42606029a918STejun Heo } 42616029a918STejun Heo 4262669de8bdSBart Van Assche va_start(args, max_active); 4263ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4264b196be89STejun Heo va_end(args); 42653af24433SOleg Nesterov 4266d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4267b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 42683af24433SOleg Nesterov 4269b196be89STejun Heo /* init wq */ 427097e37d7bSTejun Heo wq->flags = flags; 4271a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 42723c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4273112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 427430cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 427573f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 427673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4277493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 42783af24433SOleg Nesterov 4279669de8bdSBart Van Assche wq_init_lockdep(wq); 4280cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 42813af24433SOleg Nesterov 428230cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 428382efcab3SBart Van Assche goto err_unreg_lockdep; 42841537663fSTejun Heo 428540c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4286d2c1d404STejun Heo goto err_destroy; 4287e22bee78STejun Heo 4288226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4289226223abSTejun Heo goto err_destroy; 4290226223abSTejun Heo 42916af8bf3dSOleg Nesterov /* 429268e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 429368e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 429468e13a67SLai Jiangshan * list. 42956af8bf3dSOleg Nesterov */ 429668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4297a0a1a5fdSTejun Heo 4298a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 429949e3cf44STejun Heo for_each_pwq(pwq, wq) 4300699ce097STejun Heo pwq_adjust_max_active(pwq); 4301a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4302a0a1a5fdSTejun Heo 4303e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4304a0a1a5fdSTejun Heo 430568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 43063af24433SOleg Nesterov 43073af24433SOleg Nesterov return wq; 4308d2c1d404STejun Heo 430982efcab3SBart Van Assche err_unreg_lockdep: 4310009bb421SBart Van Assche wq_unregister_lockdep(wq); 4311009bb421SBart Van Assche wq_free_lockdep(wq); 431282efcab3SBart Van Assche err_free_wq: 43136029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 43144690c4abSTejun Heo kfree(wq); 4315d2c1d404STejun Heo return NULL; 4316d2c1d404STejun Heo err_destroy: 4317d2c1d404STejun Heo destroy_workqueue(wq); 43184690c4abSTejun Heo return NULL; 43191da177e4SLinus Torvalds } 4320669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 43211da177e4SLinus Torvalds 4322c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4323c29eb853STejun Heo { 4324c29eb853STejun Heo int i; 4325c29eb853STejun Heo 4326c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4327c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4328c29eb853STejun Heo return true; 4329c29eb853STejun Heo 4330c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4331c29eb853STejun Heo return true; 4332c29eb853STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) 4333c29eb853STejun Heo return true; 4334c29eb853STejun Heo 4335c29eb853STejun Heo return false; 4336c29eb853STejun Heo } 4337c29eb853STejun Heo 43383af24433SOleg Nesterov /** 43393af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 43403af24433SOleg Nesterov * @wq: target workqueue 43413af24433SOleg Nesterov * 43423af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 43433af24433SOleg Nesterov */ 43443af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 43453af24433SOleg Nesterov { 434649e3cf44STejun Heo struct pool_workqueue *pwq; 43474c16bd32STejun Heo int node; 43483af24433SOleg Nesterov 4349def98c84STejun Heo /* 4350def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4351def98c84STejun Heo * lead to sysfs name conflicts. 4352def98c84STejun Heo */ 4353def98c84STejun Heo workqueue_sysfs_unregister(wq); 4354def98c84STejun Heo 43559c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 43569c5a2ba7STejun Heo drain_workqueue(wq); 4357c8efcc25STejun Heo 4358def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4359def98c84STejun Heo if (wq->rescuer) { 4360def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4361def98c84STejun Heo 4362def98c84STejun Heo /* this prevents new queueing */ 4363def98c84STejun Heo spin_lock_irq(&wq_mayday_lock); 4364def98c84STejun Heo wq->rescuer = NULL; 4365def98c84STejun Heo spin_unlock_irq(&wq_mayday_lock); 4366def98c84STejun Heo 4367def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4368def98c84STejun Heo kthread_stop(rescuer->task); 43698efe1223STejun Heo kfree(rescuer); 4370def98c84STejun Heo } 4371def98c84STejun Heo 4372c29eb853STejun Heo /* 4373c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4374c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4375c29eb853STejun Heo */ 4376c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4377b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 437849e3cf44STejun Heo for_each_pwq(pwq, wq) { 4379c29eb853STejun Heo spin_lock_irq(&pwq->pool->lock); 4380c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 43811d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4382e66b39afSTejun Heo __func__, wq->name); 4383c29eb853STejun Heo show_pwq(pwq); 4384c29eb853STejun Heo spin_unlock_irq(&pwq->pool->lock); 4385b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4386c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 4387fa07fb6aSTejun Heo show_workqueue_state(); 43886183c009STejun Heo return; 438976af4d93STejun Heo } 4390c29eb853STejun Heo spin_unlock_irq(&pwq->pool->lock); 439176af4d93STejun Heo } 4392b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4393c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 43946183c009STejun Heo 4395a0a1a5fdSTejun Heo /* 4396a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4397a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4398a0a1a5fdSTejun Heo */ 439968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4400e2dca7adSTejun Heo list_del_rcu(&wq->list); 440168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 44023af24433SOleg Nesterov 44038864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4404669de8bdSBart Van Assche wq_unregister_lockdep(wq); 440529c91e99STejun Heo /* 44068864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4407e2dca7adSTejun Heo * schedule RCU free. 440829c91e99STejun Heo */ 440925b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 44108864b4e5STejun Heo } else { 44118864b4e5STejun Heo /* 44128864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 44134c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 44144c16bd32STejun Heo * @wq will be freed when the last pwq is released. 44158864b4e5STejun Heo */ 44164c16bd32STejun Heo for_each_node(node) { 44174c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 44184c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 44194c16bd32STejun Heo put_pwq_unlocked(pwq); 44204c16bd32STejun Heo } 44214c16bd32STejun Heo 44224c16bd32STejun Heo /* 44234c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 44244c16bd32STejun Heo * put. Don't access it afterwards. 44254c16bd32STejun Heo */ 44264c16bd32STejun Heo pwq = wq->dfl_pwq; 44274c16bd32STejun Heo wq->dfl_pwq = NULL; 4428dce90d47STejun Heo put_pwq_unlocked(pwq); 442929c91e99STejun Heo } 44303af24433SOleg Nesterov } 44313af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 44323af24433SOleg Nesterov 4433dcd989cbSTejun Heo /** 4434dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4435dcd989cbSTejun Heo * @wq: target workqueue 4436dcd989cbSTejun Heo * @max_active: new max_active value. 4437dcd989cbSTejun Heo * 4438dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4439dcd989cbSTejun Heo * 4440dcd989cbSTejun Heo * CONTEXT: 4441dcd989cbSTejun Heo * Don't call from IRQ context. 4442dcd989cbSTejun Heo */ 4443dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4444dcd989cbSTejun Heo { 444549e3cf44STejun Heo struct pool_workqueue *pwq; 4446dcd989cbSTejun Heo 44478719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 44480a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 44498719dceaSTejun Heo return; 44508719dceaSTejun Heo 4451f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4452dcd989cbSTejun Heo 4453a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4454dcd989cbSTejun Heo 44550a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4456dcd989cbSTejun Heo wq->saved_max_active = max_active; 4457dcd989cbSTejun Heo 4458699ce097STejun Heo for_each_pwq(pwq, wq) 4459699ce097STejun Heo pwq_adjust_max_active(pwq); 4460dcd989cbSTejun Heo 4461a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4462dcd989cbSTejun Heo } 4463dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4464dcd989cbSTejun Heo 4465dcd989cbSTejun Heo /** 446627d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 446727d4ee03SLukas Wunner * 446827d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 446927d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 447027d4ee03SLukas Wunner * 447127d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 447227d4ee03SLukas Wunner */ 447327d4ee03SLukas Wunner struct work_struct *current_work(void) 447427d4ee03SLukas Wunner { 447527d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 447627d4ee03SLukas Wunner 447727d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 447827d4ee03SLukas Wunner } 447927d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 448027d4ee03SLukas Wunner 448127d4ee03SLukas Wunner /** 4482e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4483e6267616STejun Heo * 4484e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4485e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4486d185af30SYacine Belkadi * 4487d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4488e6267616STejun Heo */ 4489e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4490e6267616STejun Heo { 4491e6267616STejun Heo struct worker *worker = current_wq_worker(); 4492e6267616STejun Heo 44936a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4494e6267616STejun Heo } 4495e6267616STejun Heo 4496e6267616STejun Heo /** 4497dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4498dcd989cbSTejun Heo * @cpu: CPU in question 4499dcd989cbSTejun Heo * @wq: target workqueue 4500dcd989cbSTejun Heo * 4501dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4502dcd989cbSTejun Heo * no synchronization around this function and the test result is 4503dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4504dcd989cbSTejun Heo * 4505d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4506d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4507d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4508d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4509d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4510d3251859STejun Heo * 4511d185af30SYacine Belkadi * Return: 4512dcd989cbSTejun Heo * %true if congested, %false otherwise. 4513dcd989cbSTejun Heo */ 4514d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4515dcd989cbSTejun Heo { 45167fb98ea7STejun Heo struct pool_workqueue *pwq; 451776af4d93STejun Heo bool ret; 451876af4d93STejun Heo 451924acfb71SThomas Gleixner rcu_read_lock(); 452024acfb71SThomas Gleixner preempt_disable(); 45217fb98ea7STejun Heo 4522d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4523d3251859STejun Heo cpu = smp_processor_id(); 4524d3251859STejun Heo 45257fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 45267fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 45277fb98ea7STejun Heo else 4528df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4529dcd989cbSTejun Heo 453076af4d93STejun Heo ret = !list_empty(&pwq->delayed_works); 453124acfb71SThomas Gleixner preempt_enable(); 453224acfb71SThomas Gleixner rcu_read_unlock(); 453376af4d93STejun Heo 453476af4d93STejun Heo return ret; 4535dcd989cbSTejun Heo } 4536dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4537dcd989cbSTejun Heo 4538dcd989cbSTejun Heo /** 4539dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4540dcd989cbSTejun Heo * @work: the work to be tested 4541dcd989cbSTejun Heo * 4542dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4543dcd989cbSTejun Heo * synchronization around this function and the test result is 4544dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4545dcd989cbSTejun Heo * 4546d185af30SYacine Belkadi * Return: 4547dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4548dcd989cbSTejun Heo */ 4549dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4550dcd989cbSTejun Heo { 4551fa1b54e6STejun Heo struct worker_pool *pool; 4552dcd989cbSTejun Heo unsigned long flags; 4553dcd989cbSTejun Heo unsigned int ret = 0; 4554dcd989cbSTejun Heo 4555dcd989cbSTejun Heo if (work_pending(work)) 4556dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4557038366c5SLai Jiangshan 455824acfb71SThomas Gleixner rcu_read_lock(); 4559fa1b54e6STejun Heo pool = get_work_pool(work); 4560038366c5SLai Jiangshan if (pool) { 456124acfb71SThomas Gleixner spin_lock_irqsave(&pool->lock, flags); 4562c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4563dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 456424acfb71SThomas Gleixner spin_unlock_irqrestore(&pool->lock, flags); 4565038366c5SLai Jiangshan } 456624acfb71SThomas Gleixner rcu_read_unlock(); 4567dcd989cbSTejun Heo 4568dcd989cbSTejun Heo return ret; 4569dcd989cbSTejun Heo } 4570dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4571dcd989cbSTejun Heo 45723d1cb205STejun Heo /** 45733d1cb205STejun Heo * set_worker_desc - set description for the current work item 45743d1cb205STejun Heo * @fmt: printf-style format string 45753d1cb205STejun Heo * @...: arguments for the format string 45763d1cb205STejun Heo * 45773d1cb205STejun Heo * This function can be called by a running work function to describe what 45783d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 45793d1cb205STejun Heo * information will be printed out together to help debugging. The 45803d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 45813d1cb205STejun Heo */ 45823d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 45833d1cb205STejun Heo { 45843d1cb205STejun Heo struct worker *worker = current_wq_worker(); 45853d1cb205STejun Heo va_list args; 45863d1cb205STejun Heo 45873d1cb205STejun Heo if (worker) { 45883d1cb205STejun Heo va_start(args, fmt); 45893d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 45903d1cb205STejun Heo va_end(args); 45913d1cb205STejun Heo } 45923d1cb205STejun Heo } 45935c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 45943d1cb205STejun Heo 45953d1cb205STejun Heo /** 45963d1cb205STejun Heo * print_worker_info - print out worker information and description 45973d1cb205STejun Heo * @log_lvl: the log level to use when printing 45983d1cb205STejun Heo * @task: target task 45993d1cb205STejun Heo * 46003d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 46013d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 46023d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 46033d1cb205STejun Heo * 46043d1cb205STejun Heo * This function can be safely called on any task as long as the 46053d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 46063d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 46073d1cb205STejun Heo */ 46083d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 46093d1cb205STejun Heo { 46103d1cb205STejun Heo work_func_t *fn = NULL; 46113d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 46123d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 46133d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 46143d1cb205STejun Heo struct workqueue_struct *wq = NULL; 46153d1cb205STejun Heo struct worker *worker; 46163d1cb205STejun Heo 46173d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 46183d1cb205STejun Heo return; 46193d1cb205STejun Heo 46203d1cb205STejun Heo /* 46213d1cb205STejun Heo * This function is called without any synchronization and @task 46223d1cb205STejun Heo * could be in any state. Be careful with dereferences. 46233d1cb205STejun Heo */ 4624e700591aSPetr Mladek worker = kthread_probe_data(task); 46253d1cb205STejun Heo 46263d1cb205STejun Heo /* 46278bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 46288bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 46293d1cb205STejun Heo */ 46303d1cb205STejun Heo probe_kernel_read(&fn, &worker->current_func, sizeof(fn)); 46313d1cb205STejun Heo probe_kernel_read(&pwq, &worker->current_pwq, sizeof(pwq)); 46323d1cb205STejun Heo probe_kernel_read(&wq, &pwq->wq, sizeof(wq)); 46333d1cb205STejun Heo probe_kernel_read(name, wq->name, sizeof(name) - 1); 46343d1cb205STejun Heo probe_kernel_read(desc, worker->desc, sizeof(desc) - 1); 46353d1cb205STejun Heo 46363d1cb205STejun Heo if (fn || name[0] || desc[0]) { 4637d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 46388bf89593STejun Heo if (strcmp(name, desc)) 46393d1cb205STejun Heo pr_cont(" (%s)", desc); 46403d1cb205STejun Heo pr_cont("\n"); 46413d1cb205STejun Heo } 46423d1cb205STejun Heo } 46433d1cb205STejun Heo 46443494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 46453494fc30STejun Heo { 46463494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 46473494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 46483494fc30STejun Heo pr_cont(" node=%d", pool->node); 46493494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 46503494fc30STejun Heo } 46513494fc30STejun Heo 46523494fc30STejun Heo static void pr_cont_work(bool comma, struct work_struct *work) 46533494fc30STejun Heo { 46543494fc30STejun Heo if (work->func == wq_barrier_func) { 46553494fc30STejun Heo struct wq_barrier *barr; 46563494fc30STejun Heo 46573494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 46583494fc30STejun Heo 46593494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 46603494fc30STejun Heo task_pid_nr(barr->task)); 46613494fc30STejun Heo } else { 4662d75f773cSSakari Ailus pr_cont("%s %ps", comma ? "," : "", work->func); 46633494fc30STejun Heo } 46643494fc30STejun Heo } 46653494fc30STejun Heo 46663494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 46673494fc30STejun Heo { 46683494fc30STejun Heo struct worker_pool *pool = pwq->pool; 46693494fc30STejun Heo struct work_struct *work; 46703494fc30STejun Heo struct worker *worker; 46713494fc30STejun Heo bool has_in_flight = false, has_pending = false; 46723494fc30STejun Heo int bkt; 46733494fc30STejun Heo 46743494fc30STejun Heo pr_info(" pwq %d:", pool->id); 46753494fc30STejun Heo pr_cont_pool_info(pool); 46763494fc30STejun Heo 4677e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 4678e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 46793494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 46803494fc30STejun Heo 46813494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 46823494fc30STejun Heo if (worker->current_pwq == pwq) { 46833494fc30STejun Heo has_in_flight = true; 46843494fc30STejun Heo break; 46853494fc30STejun Heo } 46863494fc30STejun Heo } 46873494fc30STejun Heo if (has_in_flight) { 46883494fc30STejun Heo bool comma = false; 46893494fc30STejun Heo 46903494fc30STejun Heo pr_info(" in-flight:"); 46913494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 46923494fc30STejun Heo if (worker->current_pwq != pwq) 46933494fc30STejun Heo continue; 46943494fc30STejun Heo 4695d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 46963494fc30STejun Heo task_pid_nr(worker->task), 469730ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 46983494fc30STejun Heo worker->current_func); 46993494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 47003494fc30STejun Heo pr_cont_work(false, work); 47013494fc30STejun Heo comma = true; 47023494fc30STejun Heo } 47033494fc30STejun Heo pr_cont("\n"); 47043494fc30STejun Heo } 47053494fc30STejun Heo 47063494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 47073494fc30STejun Heo if (get_work_pwq(work) == pwq) { 47083494fc30STejun Heo has_pending = true; 47093494fc30STejun Heo break; 47103494fc30STejun Heo } 47113494fc30STejun Heo } 47123494fc30STejun Heo if (has_pending) { 47133494fc30STejun Heo bool comma = false; 47143494fc30STejun Heo 47153494fc30STejun Heo pr_info(" pending:"); 47163494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 47173494fc30STejun Heo if (get_work_pwq(work) != pwq) 47183494fc30STejun Heo continue; 47193494fc30STejun Heo 47203494fc30STejun Heo pr_cont_work(comma, work); 47213494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 47223494fc30STejun Heo } 47233494fc30STejun Heo pr_cont("\n"); 47243494fc30STejun Heo } 47253494fc30STejun Heo 47263494fc30STejun Heo if (!list_empty(&pwq->delayed_works)) { 47273494fc30STejun Heo bool comma = false; 47283494fc30STejun Heo 47293494fc30STejun Heo pr_info(" delayed:"); 47303494fc30STejun Heo list_for_each_entry(work, &pwq->delayed_works, entry) { 47313494fc30STejun Heo pr_cont_work(comma, work); 47323494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 47333494fc30STejun Heo } 47343494fc30STejun Heo pr_cont("\n"); 47353494fc30STejun Heo } 47363494fc30STejun Heo } 47373494fc30STejun Heo 47383494fc30STejun Heo /** 47393494fc30STejun Heo * show_workqueue_state - dump workqueue state 47403494fc30STejun Heo * 47417b776af6SRoger Lu * Called from a sysrq handler or try_to_freeze_tasks() and prints out 47427b776af6SRoger Lu * all busy workqueues and pools. 47433494fc30STejun Heo */ 47443494fc30STejun Heo void show_workqueue_state(void) 47453494fc30STejun Heo { 47463494fc30STejun Heo struct workqueue_struct *wq; 47473494fc30STejun Heo struct worker_pool *pool; 47483494fc30STejun Heo unsigned long flags; 47493494fc30STejun Heo int pi; 47503494fc30STejun Heo 475124acfb71SThomas Gleixner rcu_read_lock(); 47523494fc30STejun Heo 47533494fc30STejun Heo pr_info("Showing busy workqueues and worker pools:\n"); 47543494fc30STejun Heo 47553494fc30STejun Heo list_for_each_entry_rcu(wq, &workqueues, list) { 47563494fc30STejun Heo struct pool_workqueue *pwq; 47573494fc30STejun Heo bool idle = true; 47583494fc30STejun Heo 47593494fc30STejun Heo for_each_pwq(pwq, wq) { 47603494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) { 47613494fc30STejun Heo idle = false; 47623494fc30STejun Heo break; 47633494fc30STejun Heo } 47643494fc30STejun Heo } 47653494fc30STejun Heo if (idle) 47663494fc30STejun Heo continue; 47673494fc30STejun Heo 47683494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 47693494fc30STejun Heo 47703494fc30STejun Heo for_each_pwq(pwq, wq) { 47713494fc30STejun Heo spin_lock_irqsave(&pwq->pool->lock, flags); 47723494fc30STejun Heo if (pwq->nr_active || !list_empty(&pwq->delayed_works)) 47733494fc30STejun Heo show_pwq(pwq); 47743494fc30STejun Heo spin_unlock_irqrestore(&pwq->pool->lock, flags); 477562635ea8SSergey Senozhatsky /* 477662635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 477762635ea8SSergey Senozhatsky * sysrq-t -> show_workqueue_state(). Avoid triggering 477862635ea8SSergey Senozhatsky * hard lockup. 477962635ea8SSergey Senozhatsky */ 478062635ea8SSergey Senozhatsky touch_nmi_watchdog(); 47813494fc30STejun Heo } 47823494fc30STejun Heo } 47833494fc30STejun Heo 47843494fc30STejun Heo for_each_pool(pool, pi) { 47853494fc30STejun Heo struct worker *worker; 47863494fc30STejun Heo bool first = true; 47873494fc30STejun Heo 47883494fc30STejun Heo spin_lock_irqsave(&pool->lock, flags); 47893494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 47903494fc30STejun Heo goto next_pool; 47913494fc30STejun Heo 47923494fc30STejun Heo pr_info("pool %d:", pool->id); 47933494fc30STejun Heo pr_cont_pool_info(pool); 479482607adcSTejun Heo pr_cont(" hung=%us workers=%d", 479582607adcSTejun Heo jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000, 479682607adcSTejun Heo pool->nr_workers); 47973494fc30STejun Heo if (pool->manager) 47983494fc30STejun Heo pr_cont(" manager: %d", 47993494fc30STejun Heo task_pid_nr(pool->manager->task)); 48003494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 48013494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 48023494fc30STejun Heo task_pid_nr(worker->task)); 48033494fc30STejun Heo first = false; 48043494fc30STejun Heo } 48053494fc30STejun Heo pr_cont("\n"); 48063494fc30STejun Heo next_pool: 48073494fc30STejun Heo spin_unlock_irqrestore(&pool->lock, flags); 480862635ea8SSergey Senozhatsky /* 480962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 481062635ea8SSergey Senozhatsky * sysrq-t -> show_workqueue_state(). Avoid triggering 481162635ea8SSergey Senozhatsky * hard lockup. 481262635ea8SSergey Senozhatsky */ 481362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 48143494fc30STejun Heo } 48153494fc30STejun Heo 481624acfb71SThomas Gleixner rcu_read_unlock(); 48173494fc30STejun Heo } 48183494fc30STejun Heo 48196b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 48206b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 48216b59808bSTejun Heo { 48226b59808bSTejun Heo int off; 48236b59808bSTejun Heo 48246b59808bSTejun Heo /* always show the actual comm */ 48256b59808bSTejun Heo off = strscpy(buf, task->comm, size); 48266b59808bSTejun Heo if (off < 0) 48276b59808bSTejun Heo return; 48286b59808bSTejun Heo 4829197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 48306b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 48316b59808bSTejun Heo 4832197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 4833197f6accSTejun Heo struct worker *worker = kthread_data(task); 4834197f6accSTejun Heo struct worker_pool *pool = worker->pool; 48356b59808bSTejun Heo 48366b59808bSTejun Heo if (pool) { 48376b59808bSTejun Heo spin_lock_irq(&pool->lock); 48386b59808bSTejun Heo /* 4839197f6accSTejun Heo * ->desc tracks information (wq name or 4840197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 4841197f6accSTejun Heo * current, prepend '+', otherwise '-'. 48426b59808bSTejun Heo */ 48436b59808bSTejun Heo if (worker->desc[0] != '\0') { 48446b59808bSTejun Heo if (worker->current_work) 48456b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 48466b59808bSTejun Heo worker->desc); 48476b59808bSTejun Heo else 48486b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 48496b59808bSTejun Heo worker->desc); 48506b59808bSTejun Heo } 48516b59808bSTejun Heo spin_unlock_irq(&pool->lock); 48526b59808bSTejun Heo } 4853197f6accSTejun Heo } 48546b59808bSTejun Heo 48556b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 48566b59808bSTejun Heo } 48576b59808bSTejun Heo 485866448bc2SMathieu Malaterre #ifdef CONFIG_SMP 485966448bc2SMathieu Malaterre 4860db7bccf4STejun Heo /* 4861db7bccf4STejun Heo * CPU hotplug. 4862db7bccf4STejun Heo * 4863e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 4864112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 4865706026c2STejun Heo * pool which make migrating pending and scheduled works very 4866e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 486794cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 4868e22bee78STejun Heo * blocked draining impractical. 4869e22bee78STejun Heo * 487024647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 4871628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 4872628c78e7STejun Heo * cpu comes back online. 4873db7bccf4STejun Heo */ 4874db7bccf4STejun Heo 4875e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 4876db7bccf4STejun Heo { 48774ce62e9eSTejun Heo struct worker_pool *pool; 4878db7bccf4STejun Heo struct worker *worker; 4879db7bccf4STejun Heo 4880f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 48811258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 488294cf58bbSTejun Heo spin_lock_irq(&pool->lock); 4883e22bee78STejun Heo 4884f2d5a0eeSTejun Heo /* 488592f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 488694cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 488794cf58bbSTejun Heo * except for the ones which are still executing works from 488894cf58bbSTejun Heo * before the last CPU down must be on the cpu. After 488994cf58bbSTejun Heo * this, they may become diasporas. 4890f2d5a0eeSTejun Heo */ 4891da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4892403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 4893db7bccf4STejun Heo 489424647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 4895f2d5a0eeSTejun Heo 489694cf58bbSTejun Heo spin_unlock_irq(&pool->lock); 48971258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 4898e22bee78STejun Heo 4899e22bee78STejun Heo /* 4900eb283428SLai Jiangshan * Call schedule() so that we cross rq->lock and thus can 4901eb283428SLai Jiangshan * guarantee sched callbacks see the %WORKER_UNBOUND flag. 4902eb283428SLai Jiangshan * This is necessary as scheduler callbacks may be invoked 4903eb283428SLai Jiangshan * from other cpus. 4904628c78e7STejun Heo */ 4905628c78e7STejun Heo schedule(); 4906628c78e7STejun Heo 4907628c78e7STejun Heo /* 4908eb283428SLai Jiangshan * Sched callbacks are disabled now. Zap nr_running. 4909eb283428SLai Jiangshan * After this, nr_running stays zero and need_more_worker() 4910eb283428SLai Jiangshan * and keep_working() are always true as long as the 4911eb283428SLai Jiangshan * worklist is not empty. This pool now behaves as an 4912eb283428SLai Jiangshan * unbound (in terms of concurrency management) pool which 4913eb283428SLai Jiangshan * are served by workers tied to the pool. 4914e22bee78STejun Heo */ 4915e19e397aSTejun Heo atomic_set(&pool->nr_running, 0); 4916eb283428SLai Jiangshan 4917eb283428SLai Jiangshan /* 4918eb283428SLai Jiangshan * With concurrency management just turned off, a busy 4919eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 4920eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 4921eb283428SLai Jiangshan */ 4922eb283428SLai Jiangshan spin_lock_irq(&pool->lock); 4923eb283428SLai Jiangshan wake_up_worker(pool); 4924eb283428SLai Jiangshan spin_unlock_irq(&pool->lock); 4925eb283428SLai Jiangshan } 4926db7bccf4STejun Heo } 4927db7bccf4STejun Heo 4928bd7c089eSTejun Heo /** 4929bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 4930bd7c089eSTejun Heo * @pool: pool of interest 4931bd7c089eSTejun Heo * 4932a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 4933bd7c089eSTejun Heo */ 4934bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 4935bd7c089eSTejun Heo { 4936a9ab775bSTejun Heo struct worker *worker; 4937bd7c089eSTejun Heo 49381258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 4939bd7c089eSTejun Heo 4940bd7c089eSTejun Heo /* 4941a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 4942a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 4943402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 4944a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 4945a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 4946bd7c089eSTejun Heo */ 4947da028469SLai Jiangshan for_each_pool_worker(worker, pool) 4948a9ab775bSTejun Heo WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 4949a9ab775bSTejun Heo pool->attrs->cpumask) < 0); 4950a9ab775bSTejun Heo 4951a9ab775bSTejun Heo spin_lock_irq(&pool->lock); 4952f7c17d26SWanpeng Li 49533de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 4954a9ab775bSTejun Heo 4955da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 4956a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 4957a9ab775bSTejun Heo 4958a9ab775bSTejun Heo /* 4959a9ab775bSTejun Heo * A bound idle worker should actually be on the runqueue 4960a9ab775bSTejun Heo * of the associated CPU for local wake-ups targeting it to 4961a9ab775bSTejun Heo * work. Kick all idle workers so that they migrate to the 4962a9ab775bSTejun Heo * associated CPU. Doing this in the same loop as 4963a9ab775bSTejun Heo * replacing UNBOUND with REBOUND is safe as no worker will 4964a9ab775bSTejun Heo * be bound before @pool->lock is released. 4965a9ab775bSTejun Heo */ 4966a9ab775bSTejun Heo if (worker_flags & WORKER_IDLE) 4967bd7c089eSTejun Heo wake_up_process(worker->task); 4968bd7c089eSTejun Heo 4969bd7c089eSTejun Heo /* 4970a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 4971a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 4972a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 4973a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 4974a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 4975a9ab775bSTejun Heo * concurrency management. Note that when or whether 4976a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 4977a9ab775bSTejun Heo * 4978c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 4979a9ab775bSTejun Heo * tested without holding any lock in 49806d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 4981a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 4982a9ab775bSTejun Heo * management operations. 4983bd7c089eSTejun Heo */ 4984a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 4985a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 4986a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 4987c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 4988bd7c089eSTejun Heo } 4989a9ab775bSTejun Heo 4990a9ab775bSTejun Heo spin_unlock_irq(&pool->lock); 4991bd7c089eSTejun Heo } 4992bd7c089eSTejun Heo 49937dbc725eSTejun Heo /** 49947dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 49957dbc725eSTejun Heo * @pool: unbound pool of interest 49967dbc725eSTejun Heo * @cpu: the CPU which is coming up 49977dbc725eSTejun Heo * 49987dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 49997dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 50007dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 50017dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 50027dbc725eSTejun Heo */ 50037dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 50047dbc725eSTejun Heo { 50057dbc725eSTejun Heo static cpumask_t cpumask; 50067dbc725eSTejun Heo struct worker *worker; 50077dbc725eSTejun Heo 50081258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 50097dbc725eSTejun Heo 50107dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 50117dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 50127dbc725eSTejun Heo return; 50137dbc725eSTejun Heo 50147dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 50157dbc725eSTejun Heo 50167dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5017da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5018d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 50197dbc725eSTejun Heo } 50207dbc725eSTejun Heo 50217ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 50221da177e4SLinus Torvalds { 50234ce62e9eSTejun Heo struct worker_pool *pool; 50241da177e4SLinus Torvalds 5025f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 50263ce63377STejun Heo if (pool->nr_workers) 50273ce63377STejun Heo continue; 5028051e1850SLai Jiangshan if (!create_worker(pool)) 50297ee681b2SThomas Gleixner return -ENOMEM; 50303af24433SOleg Nesterov } 50317ee681b2SThomas Gleixner return 0; 50327ee681b2SThomas Gleixner } 50331da177e4SLinus Torvalds 50347ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 50357ee681b2SThomas Gleixner { 50367ee681b2SThomas Gleixner struct worker_pool *pool; 50377ee681b2SThomas Gleixner struct workqueue_struct *wq; 50387ee681b2SThomas Gleixner int pi; 50397ee681b2SThomas Gleixner 504068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 50417dbc725eSTejun Heo 50427dbc725eSTejun Heo for_each_pool(pool, pi) { 50431258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 504494cf58bbSTejun Heo 5045f05b558dSLai Jiangshan if (pool->cpu == cpu) 504694cf58bbSTejun Heo rebind_workers(pool); 5047f05b558dSLai Jiangshan else if (pool->cpu < 0) 50487dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 504994cf58bbSTejun Heo 50501258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 505194cf58bbSTejun Heo } 50527dbc725eSTejun Heo 50534c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 50544c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 50554c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 50564c16bd32STejun Heo 505768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 50587ee681b2SThomas Gleixner return 0; 505965758202STejun Heo } 506065758202STejun Heo 50617ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 506265758202STejun Heo { 50634c16bd32STejun Heo struct workqueue_struct *wq; 50648db25e78STejun Heo 50654c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5066e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5067e8b3f8dbSLai Jiangshan return -1; 5068e8b3f8dbSLai Jiangshan 5069e8b3f8dbSLai Jiangshan unbind_workers(cpu); 50704c16bd32STejun Heo 50714c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 50724c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 50734c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 50744c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 50754c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 50764c16bd32STejun Heo 50777ee681b2SThomas Gleixner return 0; 507865758202STejun Heo } 507965758202STejun Heo 50802d3854a3SRusty Russell struct work_for_cpu { 5081ed48ece2STejun Heo struct work_struct work; 50822d3854a3SRusty Russell long (*fn)(void *); 50832d3854a3SRusty Russell void *arg; 50842d3854a3SRusty Russell long ret; 50852d3854a3SRusty Russell }; 50862d3854a3SRusty Russell 5087ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 50882d3854a3SRusty Russell { 5089ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5090ed48ece2STejun Heo 50912d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 50922d3854a3SRusty Russell } 50932d3854a3SRusty Russell 50942d3854a3SRusty Russell /** 509522aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 50962d3854a3SRusty Russell * @cpu: the cpu to run on 50972d3854a3SRusty Russell * @fn: the function to run 50982d3854a3SRusty Russell * @arg: the function arg 50992d3854a3SRusty Russell * 510031ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 51016b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5102d185af30SYacine Belkadi * 5103d185af30SYacine Belkadi * Return: The value @fn returns. 51042d3854a3SRusty Russell */ 5105d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 51062d3854a3SRusty Russell { 5107ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 51082d3854a3SRusty Russell 5109ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5110ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 511112997d1aSBjorn Helgaas flush_work(&wfc.work); 5112440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 51132d3854a3SRusty Russell return wfc.ret; 51142d3854a3SRusty Russell } 51152d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 51160e8d6a93SThomas Gleixner 51170e8d6a93SThomas Gleixner /** 51180e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 51190e8d6a93SThomas Gleixner * @cpu: the cpu to run on 51200e8d6a93SThomas Gleixner * @fn: the function to run 51210e8d6a93SThomas Gleixner * @arg: the function argument 51220e8d6a93SThomas Gleixner * 51230e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 51240e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 51250e8d6a93SThomas Gleixner * 51260e8d6a93SThomas Gleixner * Return: The value @fn returns. 51270e8d6a93SThomas Gleixner */ 51280e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 51290e8d6a93SThomas Gleixner { 51300e8d6a93SThomas Gleixner long ret = -ENODEV; 51310e8d6a93SThomas Gleixner 51320e8d6a93SThomas Gleixner get_online_cpus(); 51330e8d6a93SThomas Gleixner if (cpu_online(cpu)) 51340e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 51350e8d6a93SThomas Gleixner put_online_cpus(); 51360e8d6a93SThomas Gleixner return ret; 51370e8d6a93SThomas Gleixner } 51380e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 51392d3854a3SRusty Russell #endif /* CONFIG_SMP */ 51402d3854a3SRusty Russell 5141a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5142e7577c50SRusty Russell 5143a0a1a5fdSTejun Heo /** 5144a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5145a0a1a5fdSTejun Heo * 514658a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5147c5aa87bbSTejun Heo * workqueues will queue new works to their delayed_works list instead of 5148706026c2STejun Heo * pool->worklist. 5149a0a1a5fdSTejun Heo * 5150a0a1a5fdSTejun Heo * CONTEXT: 5151a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5152a0a1a5fdSTejun Heo */ 5153a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5154a0a1a5fdSTejun Heo { 515524b8a847STejun Heo struct workqueue_struct *wq; 515624b8a847STejun Heo struct pool_workqueue *pwq; 5157a0a1a5fdSTejun Heo 515868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5159a0a1a5fdSTejun Heo 51606183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5161a0a1a5fdSTejun Heo workqueue_freezing = true; 5162a0a1a5fdSTejun Heo 516324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5164a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5165699ce097STejun Heo for_each_pwq(pwq, wq) 5166699ce097STejun Heo pwq_adjust_max_active(pwq); 5167a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5168a1056305STejun Heo } 51695bcab335STejun Heo 517068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5171a0a1a5fdSTejun Heo } 5172a0a1a5fdSTejun Heo 5173a0a1a5fdSTejun Heo /** 517458a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5175a0a1a5fdSTejun Heo * 5176a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5177a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5178a0a1a5fdSTejun Heo * 5179a0a1a5fdSTejun Heo * CONTEXT: 518068e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5181a0a1a5fdSTejun Heo * 5182d185af30SYacine Belkadi * Return: 518358a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 518458a69cb4STejun Heo * is complete. 5185a0a1a5fdSTejun Heo */ 5186a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5187a0a1a5fdSTejun Heo { 5188a0a1a5fdSTejun Heo bool busy = false; 518924b8a847STejun Heo struct workqueue_struct *wq; 519024b8a847STejun Heo struct pool_workqueue *pwq; 5191a0a1a5fdSTejun Heo 519268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5193a0a1a5fdSTejun Heo 51946183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5195a0a1a5fdSTejun Heo 519624b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 519724b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 519824b8a847STejun Heo continue; 5199a0a1a5fdSTejun Heo /* 5200a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5201a0a1a5fdSTejun Heo * to peek without lock. 5202a0a1a5fdSTejun Heo */ 520324acfb71SThomas Gleixner rcu_read_lock(); 520424b8a847STejun Heo for_each_pwq(pwq, wq) { 52056183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5206112202d9STejun Heo if (pwq->nr_active) { 5207a0a1a5fdSTejun Heo busy = true; 520824acfb71SThomas Gleixner rcu_read_unlock(); 5209a0a1a5fdSTejun Heo goto out_unlock; 5210a0a1a5fdSTejun Heo } 5211a0a1a5fdSTejun Heo } 521224acfb71SThomas Gleixner rcu_read_unlock(); 5213a0a1a5fdSTejun Heo } 5214a0a1a5fdSTejun Heo out_unlock: 521568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5216a0a1a5fdSTejun Heo return busy; 5217a0a1a5fdSTejun Heo } 5218a0a1a5fdSTejun Heo 5219a0a1a5fdSTejun Heo /** 5220a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5221a0a1a5fdSTejun Heo * 5222a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5223706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5224a0a1a5fdSTejun Heo * 5225a0a1a5fdSTejun Heo * CONTEXT: 5226a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5227a0a1a5fdSTejun Heo */ 5228a0a1a5fdSTejun Heo void thaw_workqueues(void) 5229a0a1a5fdSTejun Heo { 523024b8a847STejun Heo struct workqueue_struct *wq; 523124b8a847STejun Heo struct pool_workqueue *pwq; 5232a0a1a5fdSTejun Heo 523368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5234a0a1a5fdSTejun Heo 5235a0a1a5fdSTejun Heo if (!workqueue_freezing) 5236a0a1a5fdSTejun Heo goto out_unlock; 5237a0a1a5fdSTejun Heo 523874b414eaSLai Jiangshan workqueue_freezing = false; 523924b8a847STejun Heo 524024b8a847STejun Heo /* restore max_active and repopulate worklist */ 524124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5242a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5243699ce097STejun Heo for_each_pwq(pwq, wq) 5244699ce097STejun Heo pwq_adjust_max_active(pwq); 5245a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 524624b8a847STejun Heo } 524724b8a847STejun Heo 5248a0a1a5fdSTejun Heo out_unlock: 524968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5250a0a1a5fdSTejun Heo } 5251a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5252a0a1a5fdSTejun Heo 5253042f7df1SLai Jiangshan static int workqueue_apply_unbound_cpumask(void) 5254042f7df1SLai Jiangshan { 5255042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5256042f7df1SLai Jiangshan int ret = 0; 5257042f7df1SLai Jiangshan struct workqueue_struct *wq; 5258042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5259042f7df1SLai Jiangshan 5260042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5261042f7df1SLai Jiangshan 5262042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5263042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5264042f7df1SLai Jiangshan continue; 5265042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5266042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5267042f7df1SLai Jiangshan continue; 5268042f7df1SLai Jiangshan 5269042f7df1SLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs); 5270042f7df1SLai Jiangshan if (!ctx) { 5271042f7df1SLai Jiangshan ret = -ENOMEM; 5272042f7df1SLai Jiangshan break; 5273042f7df1SLai Jiangshan } 5274042f7df1SLai Jiangshan 5275042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5276042f7df1SLai Jiangshan } 5277042f7df1SLai Jiangshan 5278042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5279042f7df1SLai Jiangshan if (!ret) 5280042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5281042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5282042f7df1SLai Jiangshan } 5283042f7df1SLai Jiangshan 5284042f7df1SLai Jiangshan return ret; 5285042f7df1SLai Jiangshan } 5286042f7df1SLai Jiangshan 5287042f7df1SLai Jiangshan /** 5288042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5289042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5290042f7df1SLai Jiangshan * 5291042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5292042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5293042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5294042f7df1SLai Jiangshan * 5295042f7df1SLai Jiangshan * Retun: 0 - Success 5296042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5297042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5298042f7df1SLai Jiangshan */ 5299042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5300042f7df1SLai Jiangshan { 5301042f7df1SLai Jiangshan int ret = -EINVAL; 5302042f7df1SLai Jiangshan cpumask_var_t saved_cpumask; 5303042f7df1SLai Jiangshan 5304042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&saved_cpumask, GFP_KERNEL)) 5305042f7df1SLai Jiangshan return -ENOMEM; 5306042f7df1SLai Jiangshan 5307c98a9805STal Shorer /* 5308c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5309c98a9805STal Shorer * If the user wishes to include them, we allow that. 5310c98a9805STal Shorer */ 5311042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5312042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5313a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5314042f7df1SLai Jiangshan 5315042f7df1SLai Jiangshan /* save the old wq_unbound_cpumask. */ 5316042f7df1SLai Jiangshan cpumask_copy(saved_cpumask, wq_unbound_cpumask); 5317042f7df1SLai Jiangshan 5318042f7df1SLai Jiangshan /* update wq_unbound_cpumask at first and apply it to wqs. */ 5319042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, cpumask); 5320042f7df1SLai Jiangshan ret = workqueue_apply_unbound_cpumask(); 5321042f7df1SLai Jiangshan 5322042f7df1SLai Jiangshan /* restore the wq_unbound_cpumask when failed. */ 5323042f7df1SLai Jiangshan if (ret < 0) 5324042f7df1SLai Jiangshan cpumask_copy(wq_unbound_cpumask, saved_cpumask); 5325042f7df1SLai Jiangshan 5326a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5327042f7df1SLai Jiangshan } 5328042f7df1SLai Jiangshan 5329042f7df1SLai Jiangshan free_cpumask_var(saved_cpumask); 5330042f7df1SLai Jiangshan return ret; 5331042f7df1SLai Jiangshan } 5332042f7df1SLai Jiangshan 53336ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 53346ba94429SFrederic Weisbecker /* 53356ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 53366ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 53376ba94429SFrederic Weisbecker * following attributes. 53386ba94429SFrederic Weisbecker * 53396ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 53406ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 53416ba94429SFrederic Weisbecker * 53426ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 53436ba94429SFrederic Weisbecker * 53449a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 53456ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 53466ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 53479a19b463SWang Long * numa RW bool : whether enable NUMA affinity 53486ba94429SFrederic Weisbecker */ 53496ba94429SFrederic Weisbecker struct wq_device { 53506ba94429SFrederic Weisbecker struct workqueue_struct *wq; 53516ba94429SFrederic Weisbecker struct device dev; 53526ba94429SFrederic Weisbecker }; 53536ba94429SFrederic Weisbecker 53546ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 53556ba94429SFrederic Weisbecker { 53566ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 53576ba94429SFrederic Weisbecker 53586ba94429SFrederic Weisbecker return wq_dev->wq; 53596ba94429SFrederic Weisbecker } 53606ba94429SFrederic Weisbecker 53616ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 53626ba94429SFrederic Weisbecker char *buf) 53636ba94429SFrederic Weisbecker { 53646ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 53656ba94429SFrederic Weisbecker 53666ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 53676ba94429SFrederic Weisbecker } 53686ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 53696ba94429SFrederic Weisbecker 53706ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 53716ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 53726ba94429SFrederic Weisbecker { 53736ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 53746ba94429SFrederic Weisbecker 53756ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 53766ba94429SFrederic Weisbecker } 53776ba94429SFrederic Weisbecker 53786ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 53796ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 53806ba94429SFrederic Weisbecker size_t count) 53816ba94429SFrederic Weisbecker { 53826ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 53836ba94429SFrederic Weisbecker int val; 53846ba94429SFrederic Weisbecker 53856ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 53866ba94429SFrederic Weisbecker return -EINVAL; 53876ba94429SFrederic Weisbecker 53886ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 53896ba94429SFrederic Weisbecker return count; 53906ba94429SFrederic Weisbecker } 53916ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 53926ba94429SFrederic Weisbecker 53936ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 53946ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 53956ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 53966ba94429SFrederic Weisbecker NULL, 53976ba94429SFrederic Weisbecker }; 53986ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 53996ba94429SFrederic Weisbecker 54006ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 54016ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 54026ba94429SFrederic Weisbecker { 54036ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54046ba94429SFrederic Weisbecker const char *delim = ""; 54056ba94429SFrederic Weisbecker int node, written = 0; 54066ba94429SFrederic Weisbecker 540724acfb71SThomas Gleixner get_online_cpus(); 540824acfb71SThomas Gleixner rcu_read_lock(); 54096ba94429SFrederic Weisbecker for_each_node(node) { 54106ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 54116ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 54126ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 54136ba94429SFrederic Weisbecker delim = " "; 54146ba94429SFrederic Weisbecker } 54156ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 541624acfb71SThomas Gleixner rcu_read_unlock(); 541724acfb71SThomas Gleixner put_online_cpus(); 54186ba94429SFrederic Weisbecker 54196ba94429SFrederic Weisbecker return written; 54206ba94429SFrederic Weisbecker } 54216ba94429SFrederic Weisbecker 54226ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 54236ba94429SFrederic Weisbecker char *buf) 54246ba94429SFrederic Weisbecker { 54256ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54266ba94429SFrederic Weisbecker int written; 54276ba94429SFrederic Weisbecker 54286ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 54296ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 54306ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 54316ba94429SFrederic Weisbecker 54326ba94429SFrederic Weisbecker return written; 54336ba94429SFrederic Weisbecker } 54346ba94429SFrederic Weisbecker 54356ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 54366ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 54376ba94429SFrederic Weisbecker { 54386ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 54396ba94429SFrederic Weisbecker 5440899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5441899a94feSLai Jiangshan 5442be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 54436ba94429SFrederic Weisbecker if (!attrs) 54446ba94429SFrederic Weisbecker return NULL; 54456ba94429SFrederic Weisbecker 54466ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 54476ba94429SFrederic Weisbecker return attrs; 54486ba94429SFrederic Weisbecker } 54496ba94429SFrederic Weisbecker 54506ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 54516ba94429SFrederic Weisbecker const char *buf, size_t count) 54526ba94429SFrederic Weisbecker { 54536ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54546ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5455d4d3e257SLai Jiangshan int ret = -ENOMEM; 5456d4d3e257SLai Jiangshan 5457d4d3e257SLai Jiangshan apply_wqattrs_lock(); 54586ba94429SFrederic Weisbecker 54596ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 54606ba94429SFrederic Weisbecker if (!attrs) 5461d4d3e257SLai Jiangshan goto out_unlock; 54626ba94429SFrederic Weisbecker 54636ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 54646ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5465d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 54666ba94429SFrederic Weisbecker else 54676ba94429SFrederic Weisbecker ret = -EINVAL; 54686ba94429SFrederic Weisbecker 5469d4d3e257SLai Jiangshan out_unlock: 5470d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 54716ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 54726ba94429SFrederic Weisbecker return ret ?: count; 54736ba94429SFrederic Weisbecker } 54746ba94429SFrederic Weisbecker 54756ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 54766ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 54776ba94429SFrederic Weisbecker { 54786ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54796ba94429SFrederic Weisbecker int written; 54806ba94429SFrederic Weisbecker 54816ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 54826ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 54836ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 54846ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 54856ba94429SFrederic Weisbecker return written; 54866ba94429SFrederic Weisbecker } 54876ba94429SFrederic Weisbecker 54886ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 54896ba94429SFrederic Weisbecker struct device_attribute *attr, 54906ba94429SFrederic Weisbecker const char *buf, size_t count) 54916ba94429SFrederic Weisbecker { 54926ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 54936ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5494d4d3e257SLai Jiangshan int ret = -ENOMEM; 5495d4d3e257SLai Jiangshan 5496d4d3e257SLai Jiangshan apply_wqattrs_lock(); 54976ba94429SFrederic Weisbecker 54986ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 54996ba94429SFrederic Weisbecker if (!attrs) 5500d4d3e257SLai Jiangshan goto out_unlock; 55016ba94429SFrederic Weisbecker 55026ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 55036ba94429SFrederic Weisbecker if (!ret) 5504d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 55056ba94429SFrederic Weisbecker 5506d4d3e257SLai Jiangshan out_unlock: 5507d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 55086ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 55096ba94429SFrederic Weisbecker return ret ?: count; 55106ba94429SFrederic Weisbecker } 55116ba94429SFrederic Weisbecker 55126ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 55136ba94429SFrederic Weisbecker char *buf) 55146ba94429SFrederic Weisbecker { 55156ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 55166ba94429SFrederic Weisbecker int written; 55176ba94429SFrederic Weisbecker 55186ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 55196ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 55206ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 55216ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 55226ba94429SFrederic Weisbecker 55236ba94429SFrederic Weisbecker return written; 55246ba94429SFrederic Weisbecker } 55256ba94429SFrederic Weisbecker 55266ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 55276ba94429SFrederic Weisbecker const char *buf, size_t count) 55286ba94429SFrederic Weisbecker { 55296ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 55306ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5531d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5532d4d3e257SLai Jiangshan 5533d4d3e257SLai Jiangshan apply_wqattrs_lock(); 55346ba94429SFrederic Weisbecker 55356ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 55366ba94429SFrederic Weisbecker if (!attrs) 5537d4d3e257SLai Jiangshan goto out_unlock; 55386ba94429SFrederic Weisbecker 55396ba94429SFrederic Weisbecker ret = -EINVAL; 55406ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 55416ba94429SFrederic Weisbecker attrs->no_numa = !v; 5542d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 55436ba94429SFrederic Weisbecker } 55446ba94429SFrederic Weisbecker 5545d4d3e257SLai Jiangshan out_unlock: 5546d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 55476ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 55486ba94429SFrederic Weisbecker return ret ?: count; 55496ba94429SFrederic Weisbecker } 55506ba94429SFrederic Weisbecker 55516ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 55526ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 55536ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 55546ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 55556ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 55566ba94429SFrederic Weisbecker __ATTR_NULL, 55576ba94429SFrederic Weisbecker }; 55586ba94429SFrederic Weisbecker 55596ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 55606ba94429SFrederic Weisbecker .name = "workqueue", 55616ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 55626ba94429SFrederic Weisbecker }; 55636ba94429SFrederic Weisbecker 5564b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5565b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5566b05a7928SFrederic Weisbecker { 5567b05a7928SFrederic Weisbecker int written; 5568b05a7928SFrederic Weisbecker 5569042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5570b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5571b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5572042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5573b05a7928SFrederic Weisbecker 5574b05a7928SFrederic Weisbecker return written; 5575b05a7928SFrederic Weisbecker } 5576b05a7928SFrederic Weisbecker 5577042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5578042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5579042f7df1SLai Jiangshan { 5580042f7df1SLai Jiangshan cpumask_var_t cpumask; 5581042f7df1SLai Jiangshan int ret; 5582042f7df1SLai Jiangshan 5583042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5584042f7df1SLai Jiangshan return -ENOMEM; 5585042f7df1SLai Jiangshan 5586042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5587042f7df1SLai Jiangshan if (!ret) 5588042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5589042f7df1SLai Jiangshan 5590042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5591042f7df1SLai Jiangshan return ret ? ret : count; 5592042f7df1SLai Jiangshan } 5593042f7df1SLai Jiangshan 5594b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5595042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5596042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5597b05a7928SFrederic Weisbecker 55986ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 55996ba94429SFrederic Weisbecker { 5600b05a7928SFrederic Weisbecker int err; 5601b05a7928SFrederic Weisbecker 5602b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5603b05a7928SFrederic Weisbecker if (err) 5604b05a7928SFrederic Weisbecker return err; 5605b05a7928SFrederic Weisbecker 5606b05a7928SFrederic Weisbecker return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr); 56076ba94429SFrederic Weisbecker } 56086ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 56096ba94429SFrederic Weisbecker 56106ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 56116ba94429SFrederic Weisbecker { 56126ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 56136ba94429SFrederic Weisbecker 56146ba94429SFrederic Weisbecker kfree(wq_dev); 56156ba94429SFrederic Weisbecker } 56166ba94429SFrederic Weisbecker 56176ba94429SFrederic Weisbecker /** 56186ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 56196ba94429SFrederic Weisbecker * @wq: the workqueue to register 56206ba94429SFrederic Weisbecker * 56216ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 56226ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 56236ba94429SFrederic Weisbecker * which is the preferred method. 56246ba94429SFrederic Weisbecker * 56256ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 56266ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 56276ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 56286ba94429SFrederic Weisbecker * attributes. 56296ba94429SFrederic Weisbecker * 56306ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 56316ba94429SFrederic Weisbecker */ 56326ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 56336ba94429SFrederic Weisbecker { 56346ba94429SFrederic Weisbecker struct wq_device *wq_dev; 56356ba94429SFrederic Weisbecker int ret; 56366ba94429SFrederic Weisbecker 56376ba94429SFrederic Weisbecker /* 5638402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 56396ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 56406ba94429SFrederic Weisbecker * workqueues. 56416ba94429SFrederic Weisbecker */ 56420a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 56436ba94429SFrederic Weisbecker return -EINVAL; 56446ba94429SFrederic Weisbecker 56456ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 56466ba94429SFrederic Weisbecker if (!wq_dev) 56476ba94429SFrederic Weisbecker return -ENOMEM; 56486ba94429SFrederic Weisbecker 56496ba94429SFrederic Weisbecker wq_dev->wq = wq; 56506ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 56516ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 565223217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 56536ba94429SFrederic Weisbecker 56546ba94429SFrederic Weisbecker /* 56556ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 56566ba94429SFrederic Weisbecker * everything is ready. 56576ba94429SFrederic Weisbecker */ 56586ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 56596ba94429SFrederic Weisbecker 56606ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 56616ba94429SFrederic Weisbecker if (ret) { 5662537f4146SArvind Yadav put_device(&wq_dev->dev); 56636ba94429SFrederic Weisbecker wq->wq_dev = NULL; 56646ba94429SFrederic Weisbecker return ret; 56656ba94429SFrederic Weisbecker } 56666ba94429SFrederic Weisbecker 56676ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 56686ba94429SFrederic Weisbecker struct device_attribute *attr; 56696ba94429SFrederic Weisbecker 56706ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 56716ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 56726ba94429SFrederic Weisbecker if (ret) { 56736ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 56746ba94429SFrederic Weisbecker wq->wq_dev = NULL; 56756ba94429SFrederic Weisbecker return ret; 56766ba94429SFrederic Weisbecker } 56776ba94429SFrederic Weisbecker } 56786ba94429SFrederic Weisbecker } 56796ba94429SFrederic Weisbecker 56806ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 56816ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 56826ba94429SFrederic Weisbecker return 0; 56836ba94429SFrederic Weisbecker } 56846ba94429SFrederic Weisbecker 56856ba94429SFrederic Weisbecker /** 56866ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 56876ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 56886ba94429SFrederic Weisbecker * 56896ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 56906ba94429SFrederic Weisbecker */ 56916ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 56926ba94429SFrederic Weisbecker { 56936ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 56946ba94429SFrederic Weisbecker 56956ba94429SFrederic Weisbecker if (!wq->wq_dev) 56966ba94429SFrederic Weisbecker return; 56976ba94429SFrederic Weisbecker 56986ba94429SFrederic Weisbecker wq->wq_dev = NULL; 56996ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 57006ba94429SFrederic Weisbecker } 57016ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 57026ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 57036ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 57046ba94429SFrederic Weisbecker 570582607adcSTejun Heo /* 570682607adcSTejun Heo * Workqueue watchdog. 570782607adcSTejun Heo * 570882607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 570982607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 571082607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 571182607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 571282607adcSTejun Heo * largely opaque. 571382607adcSTejun Heo * 571482607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 571582607adcSTejun Heo * state if some pools failed to make forward progress for a while where 571682607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 571782607adcSTejun Heo * 571882607adcSTejun Heo * This mechanism is controlled through the kernel parameter 571982607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 572082607adcSTejun Heo * corresponding sysfs parameter file. 572182607adcSTejun Heo */ 572282607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 572382607adcSTejun Heo 572482607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 57255cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 572682607adcSTejun Heo 572782607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 572882607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 572982607adcSTejun Heo 573082607adcSTejun Heo static void wq_watchdog_reset_touched(void) 573182607adcSTejun Heo { 573282607adcSTejun Heo int cpu; 573382607adcSTejun Heo 573482607adcSTejun Heo wq_watchdog_touched = jiffies; 573582607adcSTejun Heo for_each_possible_cpu(cpu) 573682607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 573782607adcSTejun Heo } 573882607adcSTejun Heo 57395cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 574082607adcSTejun Heo { 574182607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 574282607adcSTejun Heo bool lockup_detected = false; 574382607adcSTejun Heo struct worker_pool *pool; 574482607adcSTejun Heo int pi; 574582607adcSTejun Heo 574682607adcSTejun Heo if (!thresh) 574782607adcSTejun Heo return; 574882607adcSTejun Heo 574982607adcSTejun Heo rcu_read_lock(); 575082607adcSTejun Heo 575182607adcSTejun Heo for_each_pool(pool, pi) { 575282607adcSTejun Heo unsigned long pool_ts, touched, ts; 575382607adcSTejun Heo 575482607adcSTejun Heo if (list_empty(&pool->worklist)) 575582607adcSTejun Heo continue; 575682607adcSTejun Heo 575782607adcSTejun Heo /* get the latest of pool and touched timestamps */ 575882607adcSTejun Heo pool_ts = READ_ONCE(pool->watchdog_ts); 575982607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 576082607adcSTejun Heo 576182607adcSTejun Heo if (time_after(pool_ts, touched)) 576282607adcSTejun Heo ts = pool_ts; 576382607adcSTejun Heo else 576482607adcSTejun Heo ts = touched; 576582607adcSTejun Heo 576682607adcSTejun Heo if (pool->cpu >= 0) { 576782607adcSTejun Heo unsigned long cpu_touched = 576882607adcSTejun Heo READ_ONCE(per_cpu(wq_watchdog_touched_cpu, 576982607adcSTejun Heo pool->cpu)); 577082607adcSTejun Heo if (time_after(cpu_touched, ts)) 577182607adcSTejun Heo ts = cpu_touched; 577282607adcSTejun Heo } 577382607adcSTejun Heo 577482607adcSTejun Heo /* did we stall? */ 577582607adcSTejun Heo if (time_after(jiffies, ts + thresh)) { 577682607adcSTejun Heo lockup_detected = true; 577782607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 577882607adcSTejun Heo pr_cont_pool_info(pool); 577982607adcSTejun Heo pr_cont(" stuck for %us!\n", 578082607adcSTejun Heo jiffies_to_msecs(jiffies - pool_ts) / 1000); 578182607adcSTejun Heo } 578282607adcSTejun Heo } 578382607adcSTejun Heo 578482607adcSTejun Heo rcu_read_unlock(); 578582607adcSTejun Heo 578682607adcSTejun Heo if (lockup_detected) 578782607adcSTejun Heo show_workqueue_state(); 578882607adcSTejun Heo 578982607adcSTejun Heo wq_watchdog_reset_touched(); 579082607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 579182607adcSTejun Heo } 579282607adcSTejun Heo 5793cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 579482607adcSTejun Heo { 579582607adcSTejun Heo if (cpu >= 0) 579682607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 579782607adcSTejun Heo else 579882607adcSTejun Heo wq_watchdog_touched = jiffies; 579982607adcSTejun Heo } 580082607adcSTejun Heo 580182607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 580282607adcSTejun Heo { 580382607adcSTejun Heo wq_watchdog_thresh = 0; 580482607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 580582607adcSTejun Heo 580682607adcSTejun Heo if (thresh) { 580782607adcSTejun Heo wq_watchdog_thresh = thresh; 580882607adcSTejun Heo wq_watchdog_reset_touched(); 580982607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 581082607adcSTejun Heo } 581182607adcSTejun Heo } 581282607adcSTejun Heo 581382607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 581482607adcSTejun Heo const struct kernel_param *kp) 581582607adcSTejun Heo { 581682607adcSTejun Heo unsigned long thresh; 581782607adcSTejun Heo int ret; 581882607adcSTejun Heo 581982607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 582082607adcSTejun Heo if (ret) 582182607adcSTejun Heo return ret; 582282607adcSTejun Heo 582382607adcSTejun Heo if (system_wq) 582482607adcSTejun Heo wq_watchdog_set_thresh(thresh); 582582607adcSTejun Heo else 582682607adcSTejun Heo wq_watchdog_thresh = thresh; 582782607adcSTejun Heo 582882607adcSTejun Heo return 0; 582982607adcSTejun Heo } 583082607adcSTejun Heo 583182607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 583282607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 583382607adcSTejun Heo .get = param_get_ulong, 583482607adcSTejun Heo }; 583582607adcSTejun Heo 583682607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 583782607adcSTejun Heo 0644); 583882607adcSTejun Heo 583982607adcSTejun Heo static void wq_watchdog_init(void) 584082607adcSTejun Heo { 58415cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 584282607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 584382607adcSTejun Heo } 584482607adcSTejun Heo 584582607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 584682607adcSTejun Heo 584782607adcSTejun Heo static inline void wq_watchdog_init(void) { } 584882607adcSTejun Heo 584982607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 585082607adcSTejun Heo 5851bce90380STejun Heo static void __init wq_numa_init(void) 5852bce90380STejun Heo { 5853bce90380STejun Heo cpumask_var_t *tbl; 5854bce90380STejun Heo int node, cpu; 5855bce90380STejun Heo 5856bce90380STejun Heo if (num_possible_nodes() <= 1) 5857bce90380STejun Heo return; 5858bce90380STejun Heo 5859d55262c4STejun Heo if (wq_disable_numa) { 5860d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 5861d55262c4STejun Heo return; 5862d55262c4STejun Heo } 5863d55262c4STejun Heo 5864be69d00dSThomas Gleixner wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(); 58654c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 58664c16bd32STejun Heo 5867bce90380STejun Heo /* 5868bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 5869bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 5870bce90380STejun Heo * fully initialized by now. 5871bce90380STejun Heo */ 58726396bb22SKees Cook tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL); 5873bce90380STejun Heo BUG_ON(!tbl); 5874bce90380STejun Heo 5875bce90380STejun Heo for_each_node(node) 58765a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 58771be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 5878bce90380STejun Heo 5879bce90380STejun Heo for_each_possible_cpu(cpu) { 5880bce90380STejun Heo node = cpu_to_node(cpu); 5881bce90380STejun Heo if (WARN_ON(node == NUMA_NO_NODE)) { 5882bce90380STejun Heo pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 5883bce90380STejun Heo /* happens iff arch is bonkers, let's just proceed */ 5884bce90380STejun Heo return; 5885bce90380STejun Heo } 5886bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 5887bce90380STejun Heo } 5888bce90380STejun Heo 5889bce90380STejun Heo wq_numa_possible_cpumask = tbl; 5890bce90380STejun Heo wq_numa_enabled = true; 5891bce90380STejun Heo } 5892bce90380STejun Heo 58933347fa09STejun Heo /** 58943347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 58953347fa09STejun Heo * 58963347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 58973347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 58983347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 58993347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 59003347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 59013347fa09STejun Heo * created and scheduled right before early initcalls. 59023347fa09STejun Heo */ 59032333e829SYu Chen void __init workqueue_init_early(void) 59041da177e4SLinus Torvalds { 59057a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 59061bda3f80SFrederic Weisbecker int hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ; 59077a4e344cSTejun Heo int i, cpu; 5908c34056a3STejun Heo 5909e904e6c2STejun Heo WARN_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 5910e904e6c2STejun Heo 5911b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 59121bda3f80SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(hk_flags)); 5913b05a7928SFrederic Weisbecker 5914e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 5915e904e6c2STejun Heo 5916706026c2STejun Heo /* initialize CPU pools */ 591729c91e99STejun Heo for_each_possible_cpu(cpu) { 59184ce62e9eSTejun Heo struct worker_pool *pool; 59198b03ae3cSTejun Heo 59207a4e344cSTejun Heo i = 0; 5921f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 59227a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 5923ec22ca5eSTejun Heo pool->cpu = cpu; 59247a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 59257a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 5926f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 59277a4e344cSTejun Heo 59289daf9e67STejun Heo /* alloc pool ID */ 592968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 59309daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 593168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 59324ce62e9eSTejun Heo } 59338b03ae3cSTejun Heo } 59348b03ae3cSTejun Heo 59358a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 593629c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 593729c91e99STejun Heo struct workqueue_attrs *attrs; 593829c91e99STejun Heo 5939be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 594029c91e99STejun Heo attrs->nice = std_nice[i]; 594129c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 59428a2b7538STejun Heo 59438a2b7538STejun Heo /* 59448a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 59458a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 59468a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 59478a2b7538STejun Heo */ 5948be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 59498a2b7538STejun Heo attrs->nice = std_nice[i]; 59508a2b7538STejun Heo attrs->no_numa = true; 59518a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 595229c91e99STejun Heo } 595329c91e99STejun Heo 5954d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 59551aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 5956d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 5957f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 5958f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 595924d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 596024d51addSTejun Heo WQ_FREEZABLE, 0); 59610668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 59620668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 59630668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 59640668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 59650668106cSViresh Kumar 0); 59661aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 59670668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 59680668106cSViresh Kumar !system_power_efficient_wq || 59690668106cSViresh Kumar !system_freezable_power_efficient_wq); 59703347fa09STejun Heo } 59713347fa09STejun Heo 59723347fa09STejun Heo /** 59733347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 59743347fa09STejun Heo * 59753347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 59763347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 59773347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 59783347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 59793347fa09STejun Heo * with the initial workers and enable future kworker creations. 59803347fa09STejun Heo */ 59812333e829SYu Chen void __init workqueue_init(void) 59823347fa09STejun Heo { 59832186d9f9STejun Heo struct workqueue_struct *wq; 59843347fa09STejun Heo struct worker_pool *pool; 59853347fa09STejun Heo int cpu, bkt; 59863347fa09STejun Heo 59872186d9f9STejun Heo /* 59882186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 59892186d9f9STejun Heo * CPU to node mapping may not be available that early on some 59902186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 59912186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 59922186d9f9STejun Heo * affinity, fix them up. 599340c17f75STejun Heo * 599440c17f75STejun Heo * Also, while iterating workqueues, create rescuers if requested. 59952186d9f9STejun Heo */ 59962186d9f9STejun Heo wq_numa_init(); 59972186d9f9STejun Heo 59982186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 59992186d9f9STejun Heo 60002186d9f9STejun Heo for_each_possible_cpu(cpu) { 60012186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 60022186d9f9STejun Heo pool->node = cpu_to_node(cpu); 60032186d9f9STejun Heo } 60042186d9f9STejun Heo } 60052186d9f9STejun Heo 600640c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 60072186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 600840c17f75STejun Heo WARN(init_rescuer(wq), 600940c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 601040c17f75STejun Heo wq->name); 601140c17f75STejun Heo } 60122186d9f9STejun Heo 60132186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 60142186d9f9STejun Heo 60153347fa09STejun Heo /* create the initial workers */ 60163347fa09STejun Heo for_each_online_cpu(cpu) { 60173347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 60183347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 60193347fa09STejun Heo BUG_ON(!create_worker(pool)); 60203347fa09STejun Heo } 60213347fa09STejun Heo } 60223347fa09STejun Heo 60233347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 60243347fa09STejun Heo BUG_ON(!create_worker(pool)); 60253347fa09STejun Heo 60263347fa09STejun Heo wq_online = true; 602782607adcSTejun Heo wq_watchdog_init(); 60281da177e4SLinus Torvalds } 6029