1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 41da177e4SLinus Torvalds * 5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 81da177e4SLinus Torvalds * David Woodhouse <dwmw2@infradead.org> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 111da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 1289ada679SChristoph Lameter * 13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 14c54fce6eSTejun Heo * 15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <tj@kernel.org> 17c54fce6eSTejun Heo * 18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 21b11895c4SLibin * normal work items and the other for high priority ones) and some extra 22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 23b11895c4SLibin * number of these backing pools is dynamic. 24c54fce6eSTejun Heo * 259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 289984de1aSPaul Gortmaker #include <linux/export.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/signal.h> 331da177e4SLinus Torvalds #include <linux/completion.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/notifier.h> 381da177e4SLinus Torvalds #include <linux/kthread.h> 391fa44ecaSJames Bottomley #include <linux/hardirq.h> 4046934023SChristoph Lameter #include <linux/mempolicy.h> 41341a5958SRafael J. Wysocki #include <linux/freezer.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 52cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5362635ea8SSergey Senozhatsky #include <linux/nmi.h> 54940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 55e22bee78STejun Heo 56ea138446STejun Heo #include "workqueue_internal.h" 571da177e4SLinus Torvalds 58c8e55f36STejun Heo enum { 59bc2ae0f5STejun Heo /* 6024647570STejun Heo * worker_pool flags 61bc2ae0f5STejun Heo * 6224647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 63bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 64bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 65bc2ae0f5STejun Heo * is in effect. 66bc2ae0f5STejun Heo * 67bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 68bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6924647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 70bc2ae0f5STejun Heo * 71bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 721258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 734736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 74bc2ae0f5STejun Heo */ 75692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7624647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 77db7bccf4STejun Heo 78c8e55f36STejun Heo /* worker flags */ 79c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 80c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 81e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 82fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 83f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 84a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 85e22bee78STejun Heo 86a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 87a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 88db7bccf4STejun Heo 89e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 904ce62e9eSTejun Heo 9129c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 92c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 93db7bccf4STejun Heo 94e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 95e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 96e22bee78STejun Heo 973233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 983233cdbdSTejun Heo /* call for help after 10ms 993233cdbdSTejun Heo (min two ticks) */ 100e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 101e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 104e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1058698a745SDongsheng Yang * all cpus. Give MIN_NICE. 106e22bee78STejun Heo */ 1078698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1088698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 109ecf6881fSTejun Heo 110ecf6881fSTejun Heo WQ_NAME_LEN = 24, 111c8e55f36STejun Heo }; 112c8e55f36STejun Heo 1131da177e4SLinus Torvalds /* 1144690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1154690c4abSTejun Heo * 116e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 117e41e704bSTejun Heo * everyone else. 1184690c4abSTejun Heo * 119e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 120e22bee78STejun Heo * only be modified and accessed from the local cpu. 121e22bee78STejun Heo * 122d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1234690c4abSTejun Heo * 124d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 125d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 126d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 127d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 128e22bee78STejun Heo * 1291258fae7STejun Heo * A: wq_pool_attach_mutex protected. 130822d8405STejun Heo * 13168e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13276af4d93STejun Heo * 13324acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1345bcab335STejun Heo * 1355b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1365b95e1afSLai Jiangshan * 1375b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 13824acfb71SThomas Gleixner * RCU for reads. 1395b95e1afSLai Jiangshan * 1403c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1413c25a55dSLai Jiangshan * 14224acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1432e109a28STejun Heo * 1442e109a28STejun Heo * MD: wq_mayday_lock protected. 145cd2440d6SPetr Mladek * 146cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1474690c4abSTejun Heo */ 1484690c4abSTejun Heo 1492eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 150c34056a3STejun Heo 151bd7bdd43STejun Heo struct worker_pool { 152a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 153d84ff051STejun Heo int cpu; /* I: the associated cpu */ 154f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1559daf9e67STejun Heo int id; /* I: pool ID */ 15611ebea50STejun Heo unsigned int flags; /* X: flags */ 157bd7bdd43STejun Heo 15882607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 159cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 16082607adcSTejun Heo 161bc35f7efSLai Jiangshan /* 162bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 163bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 164bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 165bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 166bc35f7efSLai Jiangshan */ 167bc35f7efSLai Jiangshan int nr_running; 16884f91c62SLai Jiangshan 169bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 170ea1abd61SLai Jiangshan 1715826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1725826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 173bd7bdd43STejun Heo 1742c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 175bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1763f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1773f959aa3SValentin Schneider 178bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 179bd7bdd43STejun Heo 180c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 181c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 182c9e7cf27STejun Heo /* L: hash of busy workers */ 183c9e7cf27STejun Heo 1842607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 18592f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 186e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 18760f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 188e19e397aSTejun Heo 1897cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 190e19e397aSTejun Heo 1917a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 19268e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 19368e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1947a4e344cSTejun Heo 195e19e397aSTejun Heo /* 19624acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 19729c91e99STejun Heo * from get_work_pool(). 19829c91e99STejun Heo */ 19929c91e99STejun Heo struct rcu_head rcu; 20084f91c62SLai Jiangshan }; 2018b03ae3cSTejun Heo 2028b03ae3cSTejun Heo /* 203112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 204112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 205112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 206112202d9STejun Heo * number of flag bits. 2071da177e4SLinus Torvalds */ 208112202d9STejun Heo struct pool_workqueue { 209bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2104690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 21173f53c4aSTejun Heo int work_color; /* L: current color */ 21273f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2138864b4e5STejun Heo int refcnt; /* L: reference count */ 21473f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 21573f53c4aSTejun Heo /* L: nr of in_flight works */ 216018f3a13SLai Jiangshan 217018f3a13SLai Jiangshan /* 218018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 219018f3a13SLai Jiangshan * 220018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 221018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 222018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 223018f3a13SLai Jiangshan * 224018f3a13SLai Jiangshan * All work items marked with WORK_STRUCT_INACTIVE do not participate 225018f3a13SLai Jiangshan * in pwq->nr_active and all work items in pwq->inactive_works are 226018f3a13SLai Jiangshan * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE 227018f3a13SLai Jiangshan * work items are in pwq->inactive_works. Some of them are ready to 228018f3a13SLai Jiangshan * run in pool->worklist or worker->scheduled. Those work itmes are 229018f3a13SLai Jiangshan * only struct wq_barrier which is used for flush_work() and should 230018f3a13SLai Jiangshan * not participate in pwq->nr_active. For non-barrier work item, it 231018f3a13SLai Jiangshan * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 232018f3a13SLai Jiangshan */ 2331e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 234a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 235f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2363c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2372e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2388864b4e5STejun Heo 2398864b4e5STejun Heo /* 2408864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2418864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 24224acfb71SThomas Gleixner * itself is also RCU protected so that the first pwq can be 243b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2448864b4e5STejun Heo */ 2458864b4e5STejun Heo struct work_struct unbound_release_work; 2468864b4e5STejun Heo struct rcu_head rcu; 247e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds /* 25073f53c4aSTejun Heo * Structure used to wait for workqueue flush. 25173f53c4aSTejun Heo */ 25273f53c4aSTejun Heo struct wq_flusher { 2533c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2543c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 25573f53c4aSTejun Heo struct completion done; /* flush completion */ 25673f53c4aSTejun Heo }; 2571da177e4SLinus Torvalds 258226223abSTejun Heo struct wq_device; 259226223abSTejun Heo 26073f53c4aSTejun Heo /* 261c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 262c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds struct workqueue_struct { 2653c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 266e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 26773f53c4aSTejun Heo 2683c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2693c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2703c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 271112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2723c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2733c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2743c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 27573f53c4aSTejun Heo 2762e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 27730ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 278e22bee78STejun Heo 27987fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 280a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 281226223abSTejun Heo 2825b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 2835b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 2846029a918STejun Heo 285226223abSTejun Heo #ifdef CONFIG_SYSFS 286226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 287226223abSTejun Heo #endif 2884e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 289669de8bdSBart Van Assche char *lock_name; 290669de8bdSBart Van Assche struct lock_class_key key; 2914e6045f1SJohannes Berg struct lockdep_map lockdep_map; 2924e6045f1SJohannes Berg #endif 293ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 2942728fd2fSTejun Heo 295e2dca7adSTejun Heo /* 29624acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 29724acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 298e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 299e2dca7adSTejun Heo */ 300e2dca7adSTejun Heo struct rcu_head rcu; 301e2dca7adSTejun Heo 3022728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3032728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 3042728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 3055b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 3061da177e4SLinus Torvalds }; 3071da177e4SLinus Torvalds 308e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 309e904e6c2STejun Heo 310bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 311bce90380STejun Heo /* possible CPUs of each node */ 312bce90380STejun Heo 313d55262c4STejun Heo static bool wq_disable_numa; 314d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 315d55262c4STejun Heo 316cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 317552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 318cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 319cee22a15SViresh Kumar 320863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3213347fa09STejun Heo 322bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 323bce90380STejun Heo 3244c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 3254c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 3264c16bd32STejun Heo 32768e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3281258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 329a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 330d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 331d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3325bcab335STejun Heo 333e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 33468e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3357d19c5ceSTejun Heo 33699c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 337ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 338ef557180SMike Galbraith 339ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 340ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 341b05a7928SFrederic Weisbecker 342f303fccbSTejun Heo /* 343f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 344f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 345f303fccbSTejun Heo * to uncover usages which depend on it. 346f303fccbSTejun Heo */ 347f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 348f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 349f303fccbSTejun Heo #else 350f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 351f303fccbSTejun Heo #endif 352f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 353f303fccbSTejun Heo 3547d19c5ceSTejun Heo /* the per-cpu worker pools */ 35525528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3567d19c5ceSTejun Heo 35768e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3587d19c5ceSTejun Heo 35968e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 36029c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 36129c91e99STejun Heo 362c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 36329c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 36429c91e99STejun Heo 3658a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3668a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3678a2b7538STejun Heo 368d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 369ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 370044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 3711aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 372044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 373d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 374044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 375f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 376044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 37724d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 3780668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 3790668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 3800668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 3810668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 382d320c038STejun Heo 3837d19c5ceSTejun Heo static int worker_thread(void *__worker); 3846ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 385c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 38655df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 3877d19c5ceSTejun Heo 38897bd2347STejun Heo #define CREATE_TRACE_POINTS 38997bd2347STejun Heo #include <trace/events/workqueue.h> 39097bd2347STejun Heo 39168e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 39224acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 393f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 39424acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 3955bcab335STejun Heo 3965b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 39724acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 398f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 399f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 40024acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4015b95e1afSLai Jiangshan 402f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 403f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 404f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4057a62c2c8STejun Heo (pool)++) 4064ce62e9eSTejun Heo 40749e3cf44STejun Heo /** 40817116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 40917116969STejun Heo * @pool: iteration cursor 410611c92a0STejun Heo * @pi: integer used for iteration 411fa1b54e6STejun Heo * 41224acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 41368e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 41468e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 415fa1b54e6STejun Heo * 416fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 417fa1b54e6STejun Heo * ignored. 41817116969STejun Heo */ 419611c92a0STejun Heo #define for_each_pool(pool, pi) \ 420611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 42168e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 422fa1b54e6STejun Heo else 42317116969STejun Heo 42417116969STejun Heo /** 425822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 426822d8405STejun Heo * @worker: iteration cursor 427822d8405STejun Heo * @pool: worker_pool to iterate workers of 428822d8405STejun Heo * 4291258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 430822d8405STejun Heo * 431822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 432822d8405STejun Heo * ignored. 433822d8405STejun Heo */ 434da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 435da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4361258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 437822d8405STejun Heo else 438822d8405STejun Heo 439822d8405STejun Heo /** 44049e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 44149e3cf44STejun Heo * @pwq: iteration cursor 44249e3cf44STejun Heo * @wq: the target workqueue 44376af4d93STejun Heo * 44424acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 445794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 446794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 44776af4d93STejun Heo * 44876af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 44976af4d93STejun Heo * ignored. 45049e3cf44STejun Heo */ 45149e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 45249e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 4535a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 454f3421797STejun Heo 455dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 456dc186ad7SThomas Gleixner 457f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 458dc186ad7SThomas Gleixner 45999777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 46099777288SStanislaw Gruszka { 46199777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 46299777288SStanislaw Gruszka } 46399777288SStanislaw Gruszka 464b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 465b9fdac7fSDu, Changbin { 466b9fdac7fSDu, Changbin struct work_struct *work = addr; 467b9fdac7fSDu, Changbin 468b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 469b9fdac7fSDu, Changbin } 470b9fdac7fSDu, Changbin 471dc186ad7SThomas Gleixner /* 472dc186ad7SThomas Gleixner * fixup_init is called when: 473dc186ad7SThomas Gleixner * - an active object is initialized 474dc186ad7SThomas Gleixner */ 47502a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 476dc186ad7SThomas Gleixner { 477dc186ad7SThomas Gleixner struct work_struct *work = addr; 478dc186ad7SThomas Gleixner 479dc186ad7SThomas Gleixner switch (state) { 480dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 481dc186ad7SThomas Gleixner cancel_work_sync(work); 482dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 48302a982a6SDu, Changbin return true; 484dc186ad7SThomas Gleixner default: 48502a982a6SDu, Changbin return false; 486dc186ad7SThomas Gleixner } 487dc186ad7SThomas Gleixner } 488dc186ad7SThomas Gleixner 489dc186ad7SThomas Gleixner /* 490dc186ad7SThomas Gleixner * fixup_free is called when: 491dc186ad7SThomas Gleixner * - an active object is freed 492dc186ad7SThomas Gleixner */ 49302a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 494dc186ad7SThomas Gleixner { 495dc186ad7SThomas Gleixner struct work_struct *work = addr; 496dc186ad7SThomas Gleixner 497dc186ad7SThomas Gleixner switch (state) { 498dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 499dc186ad7SThomas Gleixner cancel_work_sync(work); 500dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 50102a982a6SDu, Changbin return true; 502dc186ad7SThomas Gleixner default: 50302a982a6SDu, Changbin return false; 504dc186ad7SThomas Gleixner } 505dc186ad7SThomas Gleixner } 506dc186ad7SThomas Gleixner 507f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 508dc186ad7SThomas Gleixner .name = "work_struct", 50999777288SStanislaw Gruszka .debug_hint = work_debug_hint, 510b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 511dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 512dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 513dc186ad7SThomas Gleixner }; 514dc186ad7SThomas Gleixner 515dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 516dc186ad7SThomas Gleixner { 517dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 518dc186ad7SThomas Gleixner } 519dc186ad7SThomas Gleixner 520dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 521dc186ad7SThomas Gleixner { 522dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 523dc186ad7SThomas Gleixner } 524dc186ad7SThomas Gleixner 525dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 526dc186ad7SThomas Gleixner { 527dc186ad7SThomas Gleixner if (onstack) 528dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 529dc186ad7SThomas Gleixner else 530dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 531dc186ad7SThomas Gleixner } 532dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 533dc186ad7SThomas Gleixner 534dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 535dc186ad7SThomas Gleixner { 536dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 537dc186ad7SThomas Gleixner } 538dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 539dc186ad7SThomas Gleixner 540ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 541ea2e64f2SThomas Gleixner { 542ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 543ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 544ea2e64f2SThomas Gleixner } 545ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 546ea2e64f2SThomas Gleixner 547dc186ad7SThomas Gleixner #else 548dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 549dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 550dc186ad7SThomas Gleixner #endif 551dc186ad7SThomas Gleixner 5524e8b22bdSLi Bin /** 55367dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 5544e8b22bdSLi Bin * @pool: the pool pointer of interest 5554e8b22bdSLi Bin * 5564e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5574e8b22bdSLi Bin * successfully, -errno on failure. 5584e8b22bdSLi Bin */ 5599daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5609daf9e67STejun Heo { 5619daf9e67STejun Heo int ret; 5629daf9e67STejun Heo 56368e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5645bcab335STejun Heo 5654e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5664e8b22bdSLi Bin GFP_KERNEL); 567229641a6STejun Heo if (ret >= 0) { 568e68035fbSTejun Heo pool->id = ret; 569229641a6STejun Heo return 0; 570229641a6STejun Heo } 5719daf9e67STejun Heo return ret; 5729daf9e67STejun Heo } 5739daf9e67STejun Heo 57476af4d93STejun Heo /** 575df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 576df2d5ae4STejun Heo * @wq: the target workqueue 577df2d5ae4STejun Heo * @node: the node ID 578df2d5ae4STejun Heo * 57924acfb71SThomas Gleixner * This must be called with any of wq_pool_mutex, wq->mutex or RCU 5805b95e1afSLai Jiangshan * read locked. 581df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 582df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 583d185af30SYacine Belkadi * 584d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 585df2d5ae4STejun Heo */ 586df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 587df2d5ae4STejun Heo int node) 588df2d5ae4STejun Heo { 5895b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 590d6e022f1STejun Heo 591d6e022f1STejun Heo /* 592d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 593d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 594d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 595d6e022f1STejun Heo * happens, this workaround can be removed. 596d6e022f1STejun Heo */ 597d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 598d6e022f1STejun Heo return wq->dfl_pwq; 599d6e022f1STejun Heo 600df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 601df2d5ae4STejun Heo } 602df2d5ae4STejun Heo 60373f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 60473f53c4aSTejun Heo { 60573f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 60673f53c4aSTejun Heo } 60773f53c4aSTejun Heo 608c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 60973f53c4aSTejun Heo { 610c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 61173f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 61273f53c4aSTejun Heo } 61373f53c4aSTejun Heo 61473f53c4aSTejun Heo static int work_next_color(int color) 61573f53c4aSTejun Heo { 61673f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 617a848e3b6SOleg Nesterov } 618a848e3b6SOleg Nesterov 6194594bf15SDavid Howells /* 620112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 621112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6227c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6237a22ad75STejun Heo * 624112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 625112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 626bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 627bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6287a22ad75STejun Heo * 629112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6307c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 631112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6327c3eed5cSTejun Heo * available only while the work item is queued. 633bbb68dfaSTejun Heo * 634bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 635bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 636bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 637bbb68dfaSTejun Heo * try to steal the PENDING bit. 6384594bf15SDavid Howells */ 6397a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6407a22ad75STejun Heo unsigned long flags) 6417a22ad75STejun Heo { 6426183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6437a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6447a22ad75STejun Heo } 6457a22ad75STejun Heo 646112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6474690c4abSTejun Heo unsigned long extra_flags) 648365970a1SDavid Howells { 649112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 650112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 651365970a1SDavid Howells } 652365970a1SDavid Howells 6534468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6544468a00fSLai Jiangshan int pool_id) 6554468a00fSLai Jiangshan { 6564468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6574468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6584468a00fSLai Jiangshan } 6594468a00fSLai Jiangshan 6607c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6617c3eed5cSTejun Heo int pool_id) 6624d707b9fSOleg Nesterov { 66323657bb1STejun Heo /* 66423657bb1STejun Heo * The following wmb is paired with the implied mb in 66523657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 66623657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 66723657bb1STejun Heo * owner. 66823657bb1STejun Heo */ 66923657bb1STejun Heo smp_wmb(); 6707c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 671346c09f8SRoman Pen /* 672346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 673346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 674346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 6758bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 676346c09f8SRoman Pen * the same @work. E.g. consider this case: 677346c09f8SRoman Pen * 678346c09f8SRoman Pen * CPU#0 CPU#1 679346c09f8SRoman Pen * ---------------------------- -------------------------------- 680346c09f8SRoman Pen * 681346c09f8SRoman Pen * 1 STORE event_indicated 682346c09f8SRoman Pen * 2 queue_work_on() { 683346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 684346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 685346c09f8SRoman Pen * 5 set_work_data() # clear bit 686346c09f8SRoman Pen * 6 smp_mb() 687346c09f8SRoman Pen * 7 work->current_func() { 688346c09f8SRoman Pen * 8 LOAD event_indicated 689346c09f8SRoman Pen * } 690346c09f8SRoman Pen * 691346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 692346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 693346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 694346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 695346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 696346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 697346c09f8SRoman Pen * before actual STORE. 698346c09f8SRoman Pen */ 699346c09f8SRoman Pen smp_mb(); 7004d707b9fSOleg Nesterov } 7014d707b9fSOleg Nesterov 7027a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 703365970a1SDavid Howells { 7047c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7057c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7067a22ad75STejun Heo } 7077a22ad75STejun Heo 708112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7097a22ad75STejun Heo { 710e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7117a22ad75STejun Heo 712112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 713e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 714e120153dSTejun Heo else 715e120153dSTejun Heo return NULL; 7167a22ad75STejun Heo } 7177a22ad75STejun Heo 7187c3eed5cSTejun Heo /** 7197c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7207c3eed5cSTejun Heo * @work: the work item of interest 7217c3eed5cSTejun Heo * 72268e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 72324acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 72424acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 725fa1b54e6STejun Heo * 726fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 727fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 728fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 729fa1b54e6STejun Heo * returned pool is and stays online. 730d185af30SYacine Belkadi * 731d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7327c3eed5cSTejun Heo */ 7337c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7347a22ad75STejun Heo { 735e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7367c3eed5cSTejun Heo int pool_id; 7377a22ad75STejun Heo 73868e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 739fa1b54e6STejun Heo 740112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 741112202d9STejun Heo return ((struct pool_workqueue *) 7427c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7437a22ad75STejun Heo 7447c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7457c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7467a22ad75STejun Heo return NULL; 7477a22ad75STejun Heo 748fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7497c3eed5cSTejun Heo } 7507c3eed5cSTejun Heo 7517c3eed5cSTejun Heo /** 7527c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7537c3eed5cSTejun Heo * @work: the work item of interest 7547c3eed5cSTejun Heo * 755d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7567c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7577c3eed5cSTejun Heo */ 7587c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7597c3eed5cSTejun Heo { 76054d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7617c3eed5cSTejun Heo 762112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 763112202d9STejun Heo return ((struct pool_workqueue *) 76454d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 76554d5b7d0SLai Jiangshan 76654d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7677c3eed5cSTejun Heo } 7687c3eed5cSTejun Heo 769bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 770bbb68dfaSTejun Heo { 7717c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 772bbb68dfaSTejun Heo 7737c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 7747c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 775bbb68dfaSTejun Heo } 776bbb68dfaSTejun Heo 777bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 778bbb68dfaSTejun Heo { 779bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 780bbb68dfaSTejun Heo 781112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 782bbb68dfaSTejun Heo } 783bbb68dfaSTejun Heo 784e22bee78STejun Heo /* 7853270476aSTejun Heo * Policy functions. These define the policies on how the global worker 7863270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 787d565ed63STejun Heo * they're being called with pool->lock held. 788e22bee78STejun Heo */ 789e22bee78STejun Heo 79063d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 791649027d7STejun Heo { 792bc35f7efSLai Jiangshan return !pool->nr_running; 793649027d7STejun Heo } 794649027d7STejun Heo 795e22bee78STejun Heo /* 796e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 797e22bee78STejun Heo * running workers. 798974271c4STejun Heo * 799974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 800706026c2STejun Heo * function will always return %true for unbound pools as long as the 801974271c4STejun Heo * worklist isn't empty. 802e22bee78STejun Heo */ 80363d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 804e22bee78STejun Heo { 80563d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 806e22bee78STejun Heo } 807e22bee78STejun Heo 808e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 80963d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 810e22bee78STejun Heo { 81163d95a91STejun Heo return pool->nr_idle; 812e22bee78STejun Heo } 813e22bee78STejun Heo 814e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 81563d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 816e22bee78STejun Heo { 817bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 818e22bee78STejun Heo } 819e22bee78STejun Heo 820e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 82163d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 822e22bee78STejun Heo { 82363d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 824e22bee78STejun Heo } 825e22bee78STejun Heo 826e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 82763d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 828e22bee78STejun Heo { 829692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 83063d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 83163d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 832e22bee78STejun Heo 833e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 834e22bee78STejun Heo } 835e22bee78STejun Heo 836e22bee78STejun Heo /* 837e22bee78STejun Heo * Wake up functions. 838e22bee78STejun Heo */ 839e22bee78STejun Heo 8402c1f1a91SLai Jiangshan /* Return the first idle worker. Called with pool->lock held. */ 8411037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8427e11629dSTejun Heo { 84363d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8447e11629dSTejun Heo return NULL; 8457e11629dSTejun Heo 84663d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8477e11629dSTejun Heo } 8487e11629dSTejun Heo 8497e11629dSTejun Heo /** 8507e11629dSTejun Heo * wake_up_worker - wake up an idle worker 85163d95a91STejun Heo * @pool: worker pool to wake worker from 8527e11629dSTejun Heo * 85363d95a91STejun Heo * Wake up the first idle worker of @pool. 8547e11629dSTejun Heo * 8557e11629dSTejun Heo * CONTEXT: 856a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 8577e11629dSTejun Heo */ 85863d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8597e11629dSTejun Heo { 8601037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8617e11629dSTejun Heo 8627e11629dSTejun Heo if (likely(worker)) 8637e11629dSTejun Heo wake_up_process(worker->task); 8647e11629dSTejun Heo } 8657e11629dSTejun Heo 8664690c4abSTejun Heo /** 8676d25be57SThomas Gleixner * wq_worker_running - a worker is running again 868e22bee78STejun Heo * @task: task waking up 869e22bee78STejun Heo * 8706d25be57SThomas Gleixner * This function is called when a worker returns from schedule() 871e22bee78STejun Heo */ 8726d25be57SThomas Gleixner void wq_worker_running(struct task_struct *task) 873e22bee78STejun Heo { 874e22bee78STejun Heo struct worker *worker = kthread_data(task); 875e22bee78STejun Heo 8766d25be57SThomas Gleixner if (!worker->sleeping) 8776d25be57SThomas Gleixner return; 87807edfeceSFrederic Weisbecker 87907edfeceSFrederic Weisbecker /* 88007edfeceSFrederic Weisbecker * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 88107edfeceSFrederic Weisbecker * and the nr_running increment below, we may ruin the nr_running reset 88207edfeceSFrederic Weisbecker * and leave with an unexpected pool->nr_running == 1 on the newly unbound 88307edfeceSFrederic Weisbecker * pool. Protect against such race. 88407edfeceSFrederic Weisbecker */ 88507edfeceSFrederic Weisbecker preempt_disable(); 8866d25be57SThomas Gleixner if (!(worker->flags & WORKER_NOT_RUNNING)) 887bc35f7efSLai Jiangshan worker->pool->nr_running++; 88807edfeceSFrederic Weisbecker preempt_enable(); 8896d25be57SThomas Gleixner worker->sleeping = 0; 89036576000SJoonsoo Kim } 891e22bee78STejun Heo 892e22bee78STejun Heo /** 893e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 894e22bee78STejun Heo * @task: task going to sleep 895e22bee78STejun Heo * 8966d25be57SThomas Gleixner * This function is called from schedule() when a busy worker is 897ccf45156SLai Jiangshan * going to sleep. 898e22bee78STejun Heo */ 8996d25be57SThomas Gleixner void wq_worker_sleeping(struct task_struct *task) 900e22bee78STejun Heo { 901cc5bff38SLai Jiangshan struct worker *worker = kthread_data(task); 902111c225aSTejun Heo struct worker_pool *pool; 903e22bee78STejun Heo 904111c225aSTejun Heo /* 905111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 906111c225aSTejun Heo * workers, also reach here, let's not access anything before 907111c225aSTejun Heo * checking NOT_RUNNING. 908111c225aSTejun Heo */ 9092d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 9106d25be57SThomas Gleixner return; 911e22bee78STejun Heo 912111c225aSTejun Heo pool = worker->pool; 913111c225aSTejun Heo 91462849a96SSebastian Andrzej Siewior /* Return if preempted before wq_worker_running() was reached */ 91562849a96SSebastian Andrzej Siewior if (worker->sleeping) 9166d25be57SThomas Gleixner return; 9176d25be57SThomas Gleixner 9186d25be57SThomas Gleixner worker->sleeping = 1; 919a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 920e22bee78STejun Heo 921e22bee78STejun Heo /* 92245c753f5SFrederic Weisbecker * Recheck in case unbind_workers() preempted us. We don't 92345c753f5SFrederic Weisbecker * want to decrement nr_running after the worker is unbound 92445c753f5SFrederic Weisbecker * and nr_running has been reset. 92545c753f5SFrederic Weisbecker */ 92645c753f5SFrederic Weisbecker if (worker->flags & WORKER_NOT_RUNNING) { 92745c753f5SFrederic Weisbecker raw_spin_unlock_irq(&pool->lock); 92845c753f5SFrederic Weisbecker return; 92945c753f5SFrederic Weisbecker } 93045c753f5SFrederic Weisbecker 931bc35f7efSLai Jiangshan pool->nr_running--; 932bc35f7efSLai Jiangshan if (need_more_worker(pool)) 933cc5bff38SLai Jiangshan wake_up_worker(pool); 934a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 935e22bee78STejun Heo } 936e22bee78STejun Heo 937e22bee78STejun Heo /** 9381b69ac6bSJohannes Weiner * wq_worker_last_func - retrieve worker's last work function 9398194fe94SBart Van Assche * @task: Task to retrieve last work function of. 9401b69ac6bSJohannes Weiner * 9411b69ac6bSJohannes Weiner * Determine the last function a worker executed. This is called from 9421b69ac6bSJohannes Weiner * the scheduler to get a worker's last known identity. 9431b69ac6bSJohannes Weiner * 9441b69ac6bSJohannes Weiner * CONTEXT: 945a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(rq->lock) 9461b69ac6bSJohannes Weiner * 9474b047002SJohannes Weiner * This function is called during schedule() when a kworker is going 9484b047002SJohannes Weiner * to sleep. It's used by psi to identify aggregation workers during 9494b047002SJohannes Weiner * dequeuing, to allow periodic aggregation to shut-off when that 9504b047002SJohannes Weiner * worker is the last task in the system or cgroup to go to sleep. 9514b047002SJohannes Weiner * 9524b047002SJohannes Weiner * As this function doesn't involve any workqueue-related locking, it 9534b047002SJohannes Weiner * only returns stable values when called from inside the scheduler's 9544b047002SJohannes Weiner * queuing and dequeuing paths, when @task, which must be a kworker, 9554b047002SJohannes Weiner * is guaranteed to not be processing any works. 9564b047002SJohannes Weiner * 9571b69ac6bSJohannes Weiner * Return: 9581b69ac6bSJohannes Weiner * The last work function %current executed as a worker, NULL if it 9591b69ac6bSJohannes Weiner * hasn't executed any work yet. 9601b69ac6bSJohannes Weiner */ 9611b69ac6bSJohannes Weiner work_func_t wq_worker_last_func(struct task_struct *task) 9621b69ac6bSJohannes Weiner { 9631b69ac6bSJohannes Weiner struct worker *worker = kthread_data(task); 9641b69ac6bSJohannes Weiner 9651b69ac6bSJohannes Weiner return worker->last_func; 9661b69ac6bSJohannes Weiner } 9671b69ac6bSJohannes Weiner 9681b69ac6bSJohannes Weiner /** 969e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 970cb444766STejun Heo * @worker: self 971d302f017STejun Heo * @flags: flags to set 972d302f017STejun Heo * 973228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 974d302f017STejun Heo * 975cb444766STejun Heo * CONTEXT: 976a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) 977d302f017STejun Heo */ 978228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 979d302f017STejun Heo { 980bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 981e22bee78STejun Heo 982cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 983cb444766STejun Heo 984228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 985e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 986e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 987bc35f7efSLai Jiangshan pool->nr_running--; 988e22bee78STejun Heo } 989e22bee78STejun Heo 990d302f017STejun Heo worker->flags |= flags; 991d302f017STejun Heo } 992d302f017STejun Heo 993d302f017STejun Heo /** 994e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 995cb444766STejun Heo * @worker: self 996d302f017STejun Heo * @flags: flags to clear 997d302f017STejun Heo * 998e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 999d302f017STejun Heo * 1000cb444766STejun Heo * CONTEXT: 1001a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) 1002d302f017STejun Heo */ 1003d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 1004d302f017STejun Heo { 100563d95a91STejun Heo struct worker_pool *pool = worker->pool; 1006e22bee78STejun Heo unsigned int oflags = worker->flags; 1007e22bee78STejun Heo 1008cb444766STejun Heo WARN_ON_ONCE(worker->task != current); 1009cb444766STejun Heo 1010d302f017STejun Heo worker->flags &= ~flags; 1011e22bee78STejun Heo 101242c025f3STejun Heo /* 101342c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 101442c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 101542c025f3STejun Heo * of multiple flags, not a single flag. 101642c025f3STejun Heo */ 1017e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1018e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1019bc35f7efSLai Jiangshan pool->nr_running++; 1020d302f017STejun Heo } 1021d302f017STejun Heo 1022d302f017STejun Heo /** 10238cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 1024c9e7cf27STejun Heo * @pool: pool of interest 10258cca0eeaSTejun Heo * @work: work to find worker for 10268cca0eeaSTejun Heo * 1027c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 1028c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1029a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 1030a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 1031a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 1032a2c1c57bSTejun Heo * being executed. 1033a2c1c57bSTejun Heo * 1034a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 1035a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 1036a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 1037a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 1038a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 1039a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 1040a2c1c57bSTejun Heo * 1041c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 1042c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 1043c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 1044c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1045c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1046c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 10478cca0eeaSTejun Heo * 10488cca0eeaSTejun Heo * CONTEXT: 1049a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 10508cca0eeaSTejun Heo * 1051d185af30SYacine Belkadi * Return: 1052d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 10538cca0eeaSTejun Heo * otherwise. 10548cca0eeaSTejun Heo */ 1055c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 10568cca0eeaSTejun Heo struct work_struct *work) 10578cca0eeaSTejun Heo { 105842f8570fSSasha Levin struct worker *worker; 105942f8570fSSasha Levin 1060b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1061a2c1c57bSTejun Heo (unsigned long)work) 1062a2c1c57bSTejun Heo if (worker->current_work == work && 1063a2c1c57bSTejun Heo worker->current_func == work->func) 106442f8570fSSasha Levin return worker; 106542f8570fSSasha Levin 106642f8570fSSasha Levin return NULL; 10678cca0eeaSTejun Heo } 10688cca0eeaSTejun Heo 10698cca0eeaSTejun Heo /** 1070bf4ede01STejun Heo * move_linked_works - move linked works to a list 1071bf4ede01STejun Heo * @work: start of series of works to be scheduled 1072bf4ede01STejun Heo * @head: target list to append @work to 1073402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1074bf4ede01STejun Heo * 1075bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1076bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1077bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1078bf4ede01STejun Heo * 1079bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1080bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1081bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1082bf4ede01STejun Heo * 1083bf4ede01STejun Heo * CONTEXT: 1084a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1085bf4ede01STejun Heo */ 1086bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1087bf4ede01STejun Heo struct work_struct **nextp) 1088bf4ede01STejun Heo { 1089bf4ede01STejun Heo struct work_struct *n; 1090bf4ede01STejun Heo 1091bf4ede01STejun Heo /* 1092bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1093bf4ede01STejun Heo * use NULL for list head. 1094bf4ede01STejun Heo */ 1095bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1096bf4ede01STejun Heo list_move_tail(&work->entry, head); 1097bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1098bf4ede01STejun Heo break; 1099bf4ede01STejun Heo } 1100bf4ede01STejun Heo 1101bf4ede01STejun Heo /* 1102bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1103bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1104bf4ede01STejun Heo * needs to be updated. 1105bf4ede01STejun Heo */ 1106bf4ede01STejun Heo if (nextp) 1107bf4ede01STejun Heo *nextp = n; 1108bf4ede01STejun Heo } 1109bf4ede01STejun Heo 11108864b4e5STejun Heo /** 11118864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 11128864b4e5STejun Heo * @pwq: pool_workqueue to get 11138864b4e5STejun Heo * 11148864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 11158864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 11168864b4e5STejun Heo */ 11178864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 11188864b4e5STejun Heo { 11198864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11208864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 11218864b4e5STejun Heo pwq->refcnt++; 11228864b4e5STejun Heo } 11238864b4e5STejun Heo 11248864b4e5STejun Heo /** 11258864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 11268864b4e5STejun Heo * @pwq: pool_workqueue to put 11278864b4e5STejun Heo * 11288864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 11298864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 11308864b4e5STejun Heo */ 11318864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 11328864b4e5STejun Heo { 11338864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11348864b4e5STejun Heo if (likely(--pwq->refcnt)) 11358864b4e5STejun Heo return; 11368864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 11378864b4e5STejun Heo return; 11388864b4e5STejun Heo /* 11398864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 11408864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 11418864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 11428864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 11438864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 11448864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 11458864b4e5STejun Heo */ 11468864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 11478864b4e5STejun Heo } 11488864b4e5STejun Heo 1149dce90d47STejun Heo /** 1150dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1151dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1152dce90d47STejun Heo * 1153dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1154dce90d47STejun Heo */ 1155dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1156dce90d47STejun Heo { 1157dce90d47STejun Heo if (pwq) { 1158dce90d47STejun Heo /* 115924acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1160dce90d47STejun Heo * following lock operations are safe. 1161dce90d47STejun Heo */ 1162a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1163dce90d47STejun Heo put_pwq(pwq); 1164a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1165dce90d47STejun Heo } 1166dce90d47STejun Heo } 1167dce90d47STejun Heo 1168f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1169bf4ede01STejun Heo { 1170112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1171bf4ede01STejun Heo 1172bf4ede01STejun Heo trace_workqueue_activate_work(work); 117382607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 117482607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1175112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1176f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1177112202d9STejun Heo pwq->nr_active++; 1178bf4ede01STejun Heo } 1179bf4ede01STejun Heo 1180f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 11813aa62497SLai Jiangshan { 1182f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 11833aa62497SLai Jiangshan struct work_struct, entry); 11843aa62497SLai Jiangshan 1185f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 11863aa62497SLai Jiangshan } 11873aa62497SLai Jiangshan 1188bf4ede01STejun Heo /** 1189112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1190112202d9STejun Heo * @pwq: pwq of interest 1191c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1192bf4ede01STejun Heo * 1193bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1194112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1195bf4ede01STejun Heo * 1196bf4ede01STejun Heo * CONTEXT: 1197a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1198bf4ede01STejun Heo */ 1199c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1200bf4ede01STejun Heo { 1201c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1202c4560c2cSLai Jiangshan 1203018f3a13SLai Jiangshan if (!(work_data & WORK_STRUCT_INACTIVE)) { 1204112202d9STejun Heo pwq->nr_active--; 1205f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1206f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1207112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1208f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1209bf4ede01STejun Heo } 1210018f3a13SLai Jiangshan } 1211018f3a13SLai Jiangshan 1212018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1213bf4ede01STejun Heo 1214bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1215112202d9STejun Heo if (likely(pwq->flush_color != color)) 12168864b4e5STejun Heo goto out_put; 1217bf4ede01STejun Heo 1218bf4ede01STejun Heo /* are there still in-flight works? */ 1219112202d9STejun Heo if (pwq->nr_in_flight[color]) 12208864b4e5STejun Heo goto out_put; 1221bf4ede01STejun Heo 1222112202d9STejun Heo /* this pwq is done, clear flush_color */ 1223112202d9STejun Heo pwq->flush_color = -1; 1224bf4ede01STejun Heo 1225bf4ede01STejun Heo /* 1226112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1227bf4ede01STejun Heo * will handle the rest. 1228bf4ede01STejun Heo */ 1229112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1230112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 12318864b4e5STejun Heo out_put: 12328864b4e5STejun Heo put_pwq(pwq); 1233bf4ede01STejun Heo } 1234bf4ede01STejun Heo 123536e227d2STejun Heo /** 1236bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 123736e227d2STejun Heo * @work: work item to steal 123836e227d2STejun Heo * @is_dwork: @work is a delayed_work 1239bbb68dfaSTejun Heo * @flags: place to store irq state 124036e227d2STejun Heo * 124136e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1242d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 124336e227d2STejun Heo * 1244d185af30SYacine Belkadi * Return: 12453eb6b31bSMauro Carvalho Chehab * 12463eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 124736e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 124836e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 124936e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1250bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1251bbb68dfaSTejun Heo * for arbitrarily long 12523eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 125336e227d2STejun Heo * 1254d185af30SYacine Belkadi * Note: 1255bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1256e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1257e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1258e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1259bbb68dfaSTejun Heo * 1260bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1261bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1262bbb68dfaSTejun Heo * 1263e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1264bf4ede01STejun Heo */ 1265bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1266bbb68dfaSTejun Heo unsigned long *flags) 1267bf4ede01STejun Heo { 1268d565ed63STejun Heo struct worker_pool *pool; 1269112202d9STejun Heo struct pool_workqueue *pwq; 1270bf4ede01STejun Heo 1271bbb68dfaSTejun Heo local_irq_save(*flags); 1272bbb68dfaSTejun Heo 127336e227d2STejun Heo /* try to steal the timer if it exists */ 127436e227d2STejun Heo if (is_dwork) { 127536e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 127636e227d2STejun Heo 1277e0aecdd8STejun Heo /* 1278e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1279e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1280e0aecdd8STejun Heo * running on the local CPU. 1281e0aecdd8STejun Heo */ 128236e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 128336e227d2STejun Heo return 1; 128436e227d2STejun Heo } 128536e227d2STejun Heo 128636e227d2STejun Heo /* try to claim PENDING the normal way */ 1287bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1288bf4ede01STejun Heo return 0; 1289bf4ede01STejun Heo 129024acfb71SThomas Gleixner rcu_read_lock(); 1291bf4ede01STejun Heo /* 1292bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1293bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1294bf4ede01STejun Heo */ 1295d565ed63STejun Heo pool = get_work_pool(work); 1296d565ed63STejun Heo if (!pool) 1297bbb68dfaSTejun Heo goto fail; 1298bf4ede01STejun Heo 1299a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1300bf4ede01STejun Heo /* 1301112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1302112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1303112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1304112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1305112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 13060b3dae68SLai Jiangshan * item is currently queued on that pool. 1307bf4ede01STejun Heo */ 1308112202d9STejun Heo pwq = get_work_pwq(work); 1309112202d9STejun Heo if (pwq && pwq->pool == pool) { 1310bf4ede01STejun Heo debug_work_deactivate(work); 13113aa62497SLai Jiangshan 13123aa62497SLai Jiangshan /* 1313018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1314018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1315018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1316018f3a13SLai Jiangshan * 1317f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1318d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1319f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 132016062836STejun Heo * management later on and cause stall. Make sure the work 132116062836STejun Heo * item is activated before grabbing. 13223aa62497SLai Jiangshan */ 1323f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1324f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 13253aa62497SLai Jiangshan 1326bf4ede01STejun Heo list_del_init(&work->entry); 1327c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 132836e227d2STejun Heo 1329112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 13304468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 13314468a00fSLai Jiangshan 1332a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 133324acfb71SThomas Gleixner rcu_read_unlock(); 133436e227d2STejun Heo return 1; 1335bf4ede01STejun Heo } 1336a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1337bbb68dfaSTejun Heo fail: 133824acfb71SThomas Gleixner rcu_read_unlock(); 1339bbb68dfaSTejun Heo local_irq_restore(*flags); 1340bbb68dfaSTejun Heo if (work_is_canceling(work)) 1341bbb68dfaSTejun Heo return -ENOENT; 1342bbb68dfaSTejun Heo cpu_relax(); 134336e227d2STejun Heo return -EAGAIN; 1344bf4ede01STejun Heo } 1345bf4ede01STejun Heo 1346bf4ede01STejun Heo /** 1347706026c2STejun Heo * insert_work - insert a work into a pool 1348112202d9STejun Heo * @pwq: pwq @work belongs to 13494690c4abSTejun Heo * @work: work to insert 13504690c4abSTejun Heo * @head: insertion point 13514690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 13524690c4abSTejun Heo * 1353112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1354706026c2STejun Heo * work_struct flags. 13554690c4abSTejun Heo * 13564690c4abSTejun Heo * CONTEXT: 1357a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1358365970a1SDavid Howells */ 1359112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1360112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1361b89deed3SOleg Nesterov { 1362112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1363e1d8aa9fSFrederic Weisbecker 1364e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1365f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1366e89a85d6SWalter Wu 13674690c4abSTejun Heo /* we own @work, set data and link */ 1368112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 13691a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 13708864b4e5STejun Heo get_pwq(pwq); 1371e22bee78STejun Heo 137263d95a91STejun Heo if (__need_more_worker(pool)) 137363d95a91STejun Heo wake_up_worker(pool); 1374b89deed3SOleg Nesterov } 1375b89deed3SOleg Nesterov 1376c8efcc25STejun Heo /* 1377c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 13788d03ecfeSTejun Heo * same workqueue. 1379c8efcc25STejun Heo */ 1380c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1381c8efcc25STejun Heo { 1382c8efcc25STejun Heo struct worker *worker; 1383c8efcc25STejun Heo 13848d03ecfeSTejun Heo worker = current_wq_worker(); 1385c8efcc25STejun Heo /* 1386bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 13878d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1388c8efcc25STejun Heo */ 1389112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1390c8efcc25STejun Heo } 1391c8efcc25STejun Heo 1392ef557180SMike Galbraith /* 1393ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1394ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1395ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1396ef557180SMike Galbraith */ 1397ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1398ef557180SMike Galbraith { 1399ef557180SMike Galbraith int new_cpu; 1400ef557180SMike Galbraith 1401f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1402ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1403ef557180SMike Galbraith return cpu; 1404a8ec5880SAmmar Faizi } else { 1405a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1406f303fccbSTejun Heo } 1407f303fccbSTejun Heo 1408ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1409ef557180SMike Galbraith return cpu; 1410ef557180SMike Galbraith 1411ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1412ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1413ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1414ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1415ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1416ef557180SMike Galbraith return cpu; 1417ef557180SMike Galbraith } 1418ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1419ef557180SMike Galbraith 1420ef557180SMike Galbraith return new_cpu; 1421ef557180SMike Galbraith } 1422ef557180SMike Galbraith 1423d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 14241da177e4SLinus Torvalds struct work_struct *work) 14251da177e4SLinus Torvalds { 1426112202d9STejun Heo struct pool_workqueue *pwq; 1427c9178087STejun Heo struct worker_pool *last_pool; 14281e19ffc6STejun Heo struct list_head *worklist; 14298a2e8e5dSTejun Heo unsigned int work_flags; 1430b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 14318930cabaSTejun Heo 14328930cabaSTejun Heo /* 14338930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 14348930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 14358930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 14368930cabaSTejun Heo * happen with IRQ disabled. 14378930cabaSTejun Heo */ 14388e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 14391da177e4SLinus Torvalds 14401e19ffc6STejun Heo 144133e3f0a3SRichard Clark /* 144233e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 144333e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 144433e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 144533e3f0a3SRichard Clark */ 144633e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 144733e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1448e41e704bSTejun Heo return; 144924acfb71SThomas Gleixner rcu_read_lock(); 14509e8cd2f5STejun Heo retry: 1451aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1452aa202f1fSHillf Danton if (wq->flags & WQ_UNBOUND) { 1453df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1454ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1455df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1456aa202f1fSHillf Danton } else { 1457aa202f1fSHillf Danton if (req_cpu == WORK_CPU_UNBOUND) 1458aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1459aa202f1fSHillf Danton pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1460aa202f1fSHillf Danton } 1461f3421797STejun Heo 146218aa9effSTejun Heo /* 1463c9178087STejun Heo * If @work was previously on a different pool, it might still be 1464c9178087STejun Heo * running there, in which case the work needs to be queued on that 1465c9178087STejun Heo * pool to guarantee non-reentrancy. 146618aa9effSTejun Heo */ 1467c9e7cf27STejun Heo last_pool = get_work_pool(work); 1468112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 146918aa9effSTejun Heo struct worker *worker; 147018aa9effSTejun Heo 1471a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 147218aa9effSTejun Heo 1473c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 147418aa9effSTejun Heo 1475112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1476c9178087STejun Heo pwq = worker->current_pwq; 14778594fadeSLai Jiangshan } else { 147818aa9effSTejun Heo /* meh... not running there, queue here */ 1479a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1480a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 148118aa9effSTejun Heo } 14828930cabaSTejun Heo } else { 1483a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 14848930cabaSTejun Heo } 1485502ca9d8STejun Heo 14869e8cd2f5STejun Heo /* 14879e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 14889e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 14899e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1490df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1491df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 14929e8cd2f5STejun Heo * make forward-progress. 14939e8cd2f5STejun Heo */ 14949e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 14959e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1496a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 14979e8cd2f5STejun Heo cpu_relax(); 14989e8cd2f5STejun Heo goto retry; 14999e8cd2f5STejun Heo } 15009e8cd2f5STejun Heo /* oops */ 15019e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 15029e8cd2f5STejun Heo wq->name, cpu); 15039e8cd2f5STejun Heo } 15049e8cd2f5STejun Heo 1505112202d9STejun Heo /* pwq determined, queue */ 1506112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1507502ca9d8STejun Heo 150824acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 150924acfb71SThomas Gleixner goto out; 15101e19ffc6STejun Heo 1511112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1512112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 15131e19ffc6STejun Heo 1514112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1515cdadf009STejun Heo trace_workqueue_activate_work(work); 1516112202d9STejun Heo pwq->nr_active++; 1517112202d9STejun Heo worklist = &pwq->pool->worklist; 151882607adcSTejun Heo if (list_empty(worklist)) 151982607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 15208a2e8e5dSTejun Heo } else { 1521f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1522f97a4a1aSLai Jiangshan worklist = &pwq->inactive_works; 15238a2e8e5dSTejun Heo } 15241e19ffc6STejun Heo 15250687c66bSZqiang debug_work_activate(work); 1526112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 15271e19ffc6STejun Heo 152824acfb71SThomas Gleixner out: 1529a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 153024acfb71SThomas Gleixner rcu_read_unlock(); 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 15330fcb78c2SRolf Eike Beer /** 1534c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1535c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1536c1a220e7SZhang Rui * @wq: workqueue to use 1537c1a220e7SZhang Rui * @work: work to queue 1538c1a220e7SZhang Rui * 1539c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1540443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1541443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1542d185af30SYacine Belkadi * 1543d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1544c1a220e7SZhang Rui */ 1545d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1546d4283e93STejun Heo struct work_struct *work) 1547c1a220e7SZhang Rui { 1548d4283e93STejun Heo bool ret = false; 15498930cabaSTejun Heo unsigned long flags; 15508930cabaSTejun Heo 15518930cabaSTejun Heo local_irq_save(flags); 1552c1a220e7SZhang Rui 155322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 15544690c4abSTejun Heo __queue_work(cpu, wq, work); 1555d4283e93STejun Heo ret = true; 1556c1a220e7SZhang Rui } 15578930cabaSTejun Heo 15588930cabaSTejun Heo local_irq_restore(flags); 1559c1a220e7SZhang Rui return ret; 1560c1a220e7SZhang Rui } 1561ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1562c1a220e7SZhang Rui 15638204e0c1SAlexander Duyck /** 15648204e0c1SAlexander Duyck * workqueue_select_cpu_near - Select a CPU based on NUMA node 15658204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 15668204e0c1SAlexander Duyck * 15678204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 15688204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 15698204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 15708204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 15718204e0c1SAlexander Duyck */ 15728204e0c1SAlexander Duyck static int workqueue_select_cpu_near(int node) 15738204e0c1SAlexander Duyck { 15748204e0c1SAlexander Duyck int cpu; 15758204e0c1SAlexander Duyck 15768204e0c1SAlexander Duyck /* No point in doing this if NUMA isn't enabled for workqueues */ 15778204e0c1SAlexander Duyck if (!wq_numa_enabled) 15788204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 15798204e0c1SAlexander Duyck 15808204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 15818204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 15828204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 15838204e0c1SAlexander Duyck 15848204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 15858204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 15868204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 15878204e0c1SAlexander Duyck return cpu; 15888204e0c1SAlexander Duyck 15898204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 15908204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 15918204e0c1SAlexander Duyck 15928204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 15938204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 15948204e0c1SAlexander Duyck } 15958204e0c1SAlexander Duyck 15968204e0c1SAlexander Duyck /** 15978204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 15988204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 15998204e0c1SAlexander Duyck * @wq: workqueue to use 16008204e0c1SAlexander Duyck * @work: work to queue 16018204e0c1SAlexander Duyck * 16028204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 16038204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 16048204e0c1SAlexander Duyck * NUMA node. 16058204e0c1SAlexander Duyck * 16068204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 16078204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 16088204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 16098204e0c1SAlexander Duyck * 16108204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 16118204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 16128204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 16138204e0c1SAlexander Duyck * 16148204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 16158204e0c1SAlexander Duyck */ 16168204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 16178204e0c1SAlexander Duyck struct work_struct *work) 16188204e0c1SAlexander Duyck { 16198204e0c1SAlexander Duyck unsigned long flags; 16208204e0c1SAlexander Duyck bool ret = false; 16218204e0c1SAlexander Duyck 16228204e0c1SAlexander Duyck /* 16238204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 16248204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 16258204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 16268204e0c1SAlexander Duyck * 16278204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 16288204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 16298204e0c1SAlexander Duyck * some round robin type logic. 16308204e0c1SAlexander Duyck */ 16318204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 16328204e0c1SAlexander Duyck 16338204e0c1SAlexander Duyck local_irq_save(flags); 16348204e0c1SAlexander Duyck 16358204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 16368204e0c1SAlexander Duyck int cpu = workqueue_select_cpu_near(node); 16378204e0c1SAlexander Duyck 16388204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 16398204e0c1SAlexander Duyck ret = true; 16408204e0c1SAlexander Duyck } 16418204e0c1SAlexander Duyck 16428204e0c1SAlexander Duyck local_irq_restore(flags); 16438204e0c1SAlexander Duyck return ret; 16448204e0c1SAlexander Duyck } 16458204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 16468204e0c1SAlexander Duyck 16478c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 16481da177e4SLinus Torvalds { 16498c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 16501da177e4SLinus Torvalds 1651e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 165260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 16531da177e4SLinus Torvalds } 16541438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 16551da177e4SLinus Torvalds 16567beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 165752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 16581da177e4SLinus Torvalds { 16597beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 16607beb2edfSTejun Heo struct work_struct *work = &dwork->work; 16611da177e4SLinus Torvalds 1662637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 16634b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1664fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1665fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 16667beb2edfSTejun Heo 16678852aac2STejun Heo /* 16688852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 16698852aac2STejun Heo * both optimization and correctness. The earliest @timer can 16708852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 16718852aac2STejun Heo * on that there's no such delay when @delay is 0. 16728852aac2STejun Heo */ 16738852aac2STejun Heo if (!delay) { 16748852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 16758852aac2STejun Heo return; 16768852aac2STejun Heo } 16778852aac2STejun Heo 167860c057bcSLai Jiangshan dwork->wq = wq; 16791265057fSTejun Heo dwork->cpu = cpu; 16807beb2edfSTejun Heo timer->expires = jiffies + delay; 16817beb2edfSTejun Heo 1682041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 16837beb2edfSTejun Heo add_timer_on(timer, cpu); 1684041bd12eSTejun Heo else 1685041bd12eSTejun Heo add_timer(timer); 16867beb2edfSTejun Heo } 16871da177e4SLinus Torvalds 16880fcb78c2SRolf Eike Beer /** 16890fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 16900fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 16910fcb78c2SRolf Eike Beer * @wq: workqueue to use 1692af9997e4SRandy Dunlap * @dwork: work to queue 16930fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 16940fcb78c2SRolf Eike Beer * 1695d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1696715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1697715f1300STejun Heo * execution. 16980fcb78c2SRolf Eike Beer */ 1699d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 170052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 17017a6bc1cdSVenkatesh Pallipadi { 170252bad64dSDavid Howells struct work_struct *work = &dwork->work; 1703d4283e93STejun Heo bool ret = false; 17048930cabaSTejun Heo unsigned long flags; 17058930cabaSTejun Heo 17068930cabaSTejun Heo /* read the comment in __queue_work() */ 17078930cabaSTejun Heo local_irq_save(flags); 17087a6bc1cdSVenkatesh Pallipadi 170922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 17107beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1711d4283e93STejun Heo ret = true; 17127a6bc1cdSVenkatesh Pallipadi } 17138930cabaSTejun Heo 17148930cabaSTejun Heo local_irq_restore(flags); 17157a6bc1cdSVenkatesh Pallipadi return ret; 17167a6bc1cdSVenkatesh Pallipadi } 1717ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 17181da177e4SLinus Torvalds 1719c8e55f36STejun Heo /** 17208376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 17218376fe22STejun Heo * @cpu: CPU number to execute work on 17228376fe22STejun Heo * @wq: workqueue to use 17238376fe22STejun Heo * @dwork: work to queue 17248376fe22STejun Heo * @delay: number of jiffies to wait before queueing 17258376fe22STejun Heo * 17268376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 17278376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 17288376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 17298376fe22STejun Heo * current state. 17308376fe22STejun Heo * 1731d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 17328376fe22STejun Heo * pending and its timer was modified. 17338376fe22STejun Heo * 1734e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 17358376fe22STejun Heo * See try_to_grab_pending() for details. 17368376fe22STejun Heo */ 17378376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 17388376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 17398376fe22STejun Heo { 17408376fe22STejun Heo unsigned long flags; 17418376fe22STejun Heo int ret; 17428376fe22STejun Heo 17438376fe22STejun Heo do { 17448376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 17458376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 17468376fe22STejun Heo 17478376fe22STejun Heo if (likely(ret >= 0)) { 17488376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 17498376fe22STejun Heo local_irq_restore(flags); 17508376fe22STejun Heo } 17518376fe22STejun Heo 17528376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 17538376fe22STejun Heo return ret; 17548376fe22STejun Heo } 17558376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 17568376fe22STejun Heo 175705f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 175805f0fe6bSTejun Heo { 175905f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 176005f0fe6bSTejun Heo 176105f0fe6bSTejun Heo /* read the comment in __queue_work() */ 176205f0fe6bSTejun Heo local_irq_disable(); 176305f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 176405f0fe6bSTejun Heo local_irq_enable(); 176505f0fe6bSTejun Heo } 176605f0fe6bSTejun Heo 176705f0fe6bSTejun Heo /** 176805f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 176905f0fe6bSTejun Heo * @wq: workqueue to use 177005f0fe6bSTejun Heo * @rwork: work to queue 177105f0fe6bSTejun Heo * 177205f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 177305f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 1774bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 177505f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 177605f0fe6bSTejun Heo */ 177705f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 177805f0fe6bSTejun Heo { 177905f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 178005f0fe6bSTejun Heo 178105f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 178205f0fe6bSTejun Heo rwork->wq = wq; 1783a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 178405f0fe6bSTejun Heo return true; 178505f0fe6bSTejun Heo } 178605f0fe6bSTejun Heo 178705f0fe6bSTejun Heo return false; 178805f0fe6bSTejun Heo } 178905f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 179005f0fe6bSTejun Heo 17918376fe22STejun Heo /** 1792c8e55f36STejun Heo * worker_enter_idle - enter idle state 1793c8e55f36STejun Heo * @worker: worker which is entering idle state 1794c8e55f36STejun Heo * 1795c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1796c8e55f36STejun Heo * necessary. 1797c8e55f36STejun Heo * 1798c8e55f36STejun Heo * LOCKING: 1799a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1800c8e55f36STejun Heo */ 1801c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 18021da177e4SLinus Torvalds { 1803bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1804c8e55f36STejun Heo 18056183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 18066183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 18076183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 18086183c009STejun Heo return; 1809c8e55f36STejun Heo 1810051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1811cb444766STejun Heo worker->flags |= WORKER_IDLE; 1812bd7bdd43STejun Heo pool->nr_idle++; 1813e22bee78STejun Heo worker->last_active = jiffies; 1814c8e55f36STejun Heo 1815c8e55f36STejun Heo /* idle_list is LIFO */ 1816bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1817db7bccf4STejun Heo 181863d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1819628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1820cb444766STejun Heo 1821989442d7SLai Jiangshan /* Sanity check nr_running. */ 1822bc35f7efSLai Jiangshan WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1823c8e55f36STejun Heo } 1824c8e55f36STejun Heo 1825c8e55f36STejun Heo /** 1826c8e55f36STejun Heo * worker_leave_idle - leave idle state 1827c8e55f36STejun Heo * @worker: worker which is leaving idle state 1828c8e55f36STejun Heo * 1829c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1830c8e55f36STejun Heo * 1831c8e55f36STejun Heo * LOCKING: 1832a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1833c8e55f36STejun Heo */ 1834c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1835c8e55f36STejun Heo { 1836bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1837c8e55f36STejun Heo 18386183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 18396183c009STejun Heo return; 1840d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1841bd7bdd43STejun Heo pool->nr_idle--; 1842c8e55f36STejun Heo list_del_init(&worker->entry); 1843c8e55f36STejun Heo } 1844c8e55f36STejun Heo 1845f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1846c34056a3STejun Heo { 1847c34056a3STejun Heo struct worker *worker; 1848c34056a3STejun Heo 1849f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1850c8e55f36STejun Heo if (worker) { 1851c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1852affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1853da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1854e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1855e22bee78STejun Heo worker->flags = WORKER_PREP; 1856c8e55f36STejun Heo } 1857c34056a3STejun Heo return worker; 1858c34056a3STejun Heo } 1859c34056a3STejun Heo 1860c34056a3STejun Heo /** 18614736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 18624736cbf7SLai Jiangshan * @worker: worker to be attached 18634736cbf7SLai Jiangshan * @pool: the target pool 18644736cbf7SLai Jiangshan * 18654736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 18664736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 18674736cbf7SLai Jiangshan * cpu-[un]hotplugs. 18684736cbf7SLai Jiangshan */ 18694736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 18704736cbf7SLai Jiangshan struct worker_pool *pool) 18714736cbf7SLai Jiangshan { 18721258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 18734736cbf7SLai Jiangshan 18744736cbf7SLai Jiangshan /* 18751258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 18761258fae7STejun Heo * stable across this function. See the comments above the flag 18771258fae7STejun Heo * definition for details. 18784736cbf7SLai Jiangshan */ 18794736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 18804736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 18815c25b5ffSPeter Zijlstra else 18825c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 18834736cbf7SLai Jiangshan 1884640f17c8SPeter Zijlstra if (worker->rescue_wq) 1885640f17c8SPeter Zijlstra set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 1886640f17c8SPeter Zijlstra 18874736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 1888a2d812a2STejun Heo worker->pool = pool; 18894736cbf7SLai Jiangshan 18901258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 18914736cbf7SLai Jiangshan } 18924736cbf7SLai Jiangshan 18934736cbf7SLai Jiangshan /** 189460f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 189560f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 189660f5a4bcSLai Jiangshan * 18974736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 18984736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 18994736cbf7SLai Jiangshan * other reference to the pool. 190060f5a4bcSLai Jiangshan */ 1901a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 190260f5a4bcSLai Jiangshan { 1903a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 190460f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 190560f5a4bcSLai Jiangshan 19061258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 1907a2d812a2STejun Heo 19085c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 1909da028469SLai Jiangshan list_del(&worker->node); 1910a2d812a2STejun Heo worker->pool = NULL; 1911a2d812a2STejun Heo 1912e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 191360f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 19141258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 191560f5a4bcSLai Jiangshan 1916b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1917b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1918b62c0751SLai Jiangshan 191960f5a4bcSLai Jiangshan if (detach_completion) 192060f5a4bcSLai Jiangshan complete(detach_completion); 192160f5a4bcSLai Jiangshan } 192260f5a4bcSLai Jiangshan 192360f5a4bcSLai Jiangshan /** 1924c34056a3STejun Heo * create_worker - create a new workqueue worker 192563d95a91STejun Heo * @pool: pool the new worker will belong to 1926c34056a3STejun Heo * 1927051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 1928c34056a3STejun Heo * 1929c34056a3STejun Heo * CONTEXT: 1930c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 1931c34056a3STejun Heo * 1932d185af30SYacine Belkadi * Return: 1933c34056a3STejun Heo * Pointer to the newly created worker. 1934c34056a3STejun Heo */ 1935bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 1936c34056a3STejun Heo { 1937e441b56fSZhen Lei struct worker *worker; 1938e441b56fSZhen Lei int id; 1939e3c916a4STejun Heo char id_buf[16]; 1940c34056a3STejun Heo 19417cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 1942e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 19433f0ea0b8SPetr Mladek if (id < 0) { 19443f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 19453f0ea0b8SPetr Mladek ERR_PTR(id)); 1946e441b56fSZhen Lei return NULL; 19473f0ea0b8SPetr Mladek } 1948c34056a3STejun Heo 1949f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 19503f0ea0b8SPetr Mladek if (!worker) { 19513f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 1952c34056a3STejun Heo goto fail; 19533f0ea0b8SPetr Mladek } 1954c34056a3STejun Heo 1955c34056a3STejun Heo worker->id = id; 1956c34056a3STejun Heo 195729c91e99STejun Heo if (pool->cpu >= 0) 1958e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 1959e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 1960f3421797STejun Heo else 1961e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 1962e3c916a4STejun Heo 1963f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 1964e3c916a4STejun Heo "kworker/%s", id_buf); 19653f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 196660f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 196760f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 196860f54038SPetr Mladek id_buf); 196960f54038SPetr Mladek } else { 19703f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 19713f0ea0b8SPetr Mladek worker->task); 197260f54038SPetr Mladek } 1973c34056a3STejun Heo goto fail; 19743f0ea0b8SPetr Mladek } 1975c34056a3STejun Heo 197691151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 197725834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 197891151228SOleg Nesterov 1979da028469SLai Jiangshan /* successful, attach the worker to the pool */ 19804736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 1981822d8405STejun Heo 1982051e1850SLai Jiangshan /* start the newly created worker */ 1983a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 1984051e1850SLai Jiangshan worker->pool->nr_workers++; 1985051e1850SLai Jiangshan worker_enter_idle(worker); 1986051e1850SLai Jiangshan wake_up_process(worker->task); 1987a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 1988051e1850SLai Jiangshan 1989c34056a3STejun Heo return worker; 1990822d8405STejun Heo 1991c34056a3STejun Heo fail: 1992e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 1993c34056a3STejun Heo kfree(worker); 1994c34056a3STejun Heo return NULL; 1995c34056a3STejun Heo } 1996c34056a3STejun Heo 1997793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 1998793777bcSValentin Schneider { 1999793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2000793777bcSValentin Schneider 2001793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2002793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2003793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2004793777bcSValentin Schneider else 2005793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2006793777bcSValentin Schneider } 2007793777bcSValentin Schneider 2008e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2009e02b9312SValentin Schneider { 2010e02b9312SValentin Schneider struct worker *worker, *tmp; 2011e02b9312SValentin Schneider 2012e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2013e02b9312SValentin Schneider list_del_init(&worker->entry); 2014e02b9312SValentin Schneider unbind_worker(worker); 2015e02b9312SValentin Schneider /* 2016e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2017e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2018e02b9312SValentin Schneider * wouldn't have gotten here. 2019c34056a3STejun Heo * 2020e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2021e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2022e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2023e02b9312SValentin Schneider * outside of pool->lock. 2024e02b9312SValentin Schneider */ 2025e02b9312SValentin Schneider wake_up_process(worker->task); 2026e02b9312SValentin Schneider } 2027e02b9312SValentin Schneider } 2028e02b9312SValentin Schneider 2029e02b9312SValentin Schneider /** 2030e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2031e02b9312SValentin Schneider * @worker: worker to be destroyed 2032e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2033e02b9312SValentin Schneider * 2034e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2035e02b9312SValentin Schneider * should be idle. 2036c8e55f36STejun Heo * 2037c8e55f36STejun Heo * CONTEXT: 2038a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2039c34056a3STejun Heo */ 2040e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2041c34056a3STejun Heo { 2042bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2043c34056a3STejun Heo 2044cd549687STejun Heo lockdep_assert_held(&pool->lock); 2045e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2046cd549687STejun Heo 2047c34056a3STejun Heo /* sanity check frenzy */ 20486183c009STejun Heo if (WARN_ON(worker->current_work) || 204973eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 205073eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 20516183c009STejun Heo return; 2052c34056a3STejun Heo 2053bd7bdd43STejun Heo pool->nr_workers--; 2054bd7bdd43STejun Heo pool->nr_idle--; 2055c8e55f36STejun Heo 2056cb444766STejun Heo worker->flags |= WORKER_DIE; 2057e02b9312SValentin Schneider 2058e02b9312SValentin Schneider list_move(&worker->entry, list); 2059e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2060c34056a3STejun Heo } 2061c34056a3STejun Heo 20623f959aa3SValentin Schneider /** 20633f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 20643f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 20653f959aa3SValentin Schneider * 20663f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 20673f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 20683f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 20693f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 20703f959aa3SValentin Schneider * it expire and re-evaluate things from there. 20713f959aa3SValentin Schneider */ 207232a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2073e22bee78STejun Heo { 207432a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 20753f959aa3SValentin Schneider bool do_cull = false; 20763f959aa3SValentin Schneider 20773f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 20783f959aa3SValentin Schneider return; 20793f959aa3SValentin Schneider 20803f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 20813f959aa3SValentin Schneider 20823f959aa3SValentin Schneider if (too_many_workers(pool)) { 20833f959aa3SValentin Schneider struct worker *worker; 20843f959aa3SValentin Schneider unsigned long expires; 20853f959aa3SValentin Schneider 20863f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 20873f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 20883f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 20893f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 20903f959aa3SValentin Schneider 20913f959aa3SValentin Schneider if (!do_cull) 20923f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 20933f959aa3SValentin Schneider } 20943f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 20953f959aa3SValentin Schneider 20963f959aa3SValentin Schneider if (do_cull) 20973f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 20983f959aa3SValentin Schneider } 20993f959aa3SValentin Schneider 21003f959aa3SValentin Schneider /** 21013f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 21023f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 21033f959aa3SValentin Schneider * 21043f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 21053f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2106e02b9312SValentin Schneider * 2107e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2108e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2109e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 21103f959aa3SValentin Schneider */ 21113f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 21123f959aa3SValentin Schneider { 21133f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 2114e02b9312SValentin Schneider struct list_head cull_list; 2115e22bee78STejun Heo 2116e02b9312SValentin Schneider INIT_LIST_HEAD(&cull_list); 2117e02b9312SValentin Schneider /* 2118e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2119e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2120e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2121e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2122e02b9312SValentin Schneider */ 2123e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2124a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2125e22bee78STejun Heo 21263347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2127e22bee78STejun Heo struct worker *worker; 2128e22bee78STejun Heo unsigned long expires; 2129e22bee78STejun Heo 213063d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2131e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2132e22bee78STejun Heo 21333347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 213463d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 21353347fc9fSLai Jiangshan break; 2136e22bee78STejun Heo } 21373347fc9fSLai Jiangshan 2138e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2139e22bee78STejun Heo } 2140e22bee78STejun Heo 2141a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2142e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2143e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2144e22bee78STejun Heo } 2145e22bee78STejun Heo 2146493a1724STejun Heo static void send_mayday(struct work_struct *work) 2147e22bee78STejun Heo { 2148112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2149112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2150493a1724STejun Heo 21512e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2152e22bee78STejun Heo 2153493008a8STejun Heo if (!wq->rescuer) 2154493a1724STejun Heo return; 2155e22bee78STejun Heo 2156e22bee78STejun Heo /* mayday mayday mayday */ 2157493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 215877668c8bSLai Jiangshan /* 215977668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 216077668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 216177668c8bSLai Jiangshan * rescuer is done with it. 216277668c8bSLai Jiangshan */ 216377668c8bSLai Jiangshan get_pwq(pwq); 2164493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2165e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2166493a1724STejun Heo } 2167e22bee78STejun Heo } 2168e22bee78STejun Heo 216932a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2170e22bee78STejun Heo { 217132a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2172e22bee78STejun Heo struct work_struct *work; 2173e22bee78STejun Heo 2174a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2175a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2176e22bee78STejun Heo 217763d95a91STejun Heo if (need_to_create_worker(pool)) { 2178e22bee78STejun Heo /* 2179e22bee78STejun Heo * We've been trying to create a new worker but 2180e22bee78STejun Heo * haven't been successful. We might be hitting an 2181e22bee78STejun Heo * allocation deadlock. Send distress signals to 2182e22bee78STejun Heo * rescuers. 2183e22bee78STejun Heo */ 218463d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2185e22bee78STejun Heo send_mayday(work); 2186e22bee78STejun Heo } 2187e22bee78STejun Heo 2188a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2189a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2190e22bee78STejun Heo 219163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2192e22bee78STejun Heo } 2193e22bee78STejun Heo 2194e22bee78STejun Heo /** 2195e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 219663d95a91STejun Heo * @pool: pool to create a new worker for 2197e22bee78STejun Heo * 219863d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2199e22bee78STejun Heo * have at least one idle worker on return from this function. If 2200e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 220163d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2202e22bee78STejun Heo * possible allocation deadlock. 2203e22bee78STejun Heo * 2204c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2205c5aa87bbSTejun Heo * may_start_working() %true. 2206e22bee78STejun Heo * 2207e22bee78STejun Heo * LOCKING: 2208a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2209e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2210e22bee78STejun Heo * manager. 2211e22bee78STejun Heo */ 221229187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2213d565ed63STejun Heo __releases(&pool->lock) 2214d565ed63STejun Heo __acquires(&pool->lock) 2215e22bee78STejun Heo { 2216e22bee78STejun Heo restart: 2217a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 22189f9c2364STejun Heo 2219e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 222063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2221e22bee78STejun Heo 2222e22bee78STejun Heo while (true) { 2223051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2224e22bee78STejun Heo break; 2225e22bee78STejun Heo 2226e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 22279f9c2364STejun Heo 222863d95a91STejun Heo if (!need_to_create_worker(pool)) 2229e22bee78STejun Heo break; 2230e22bee78STejun Heo } 2231e22bee78STejun Heo 223263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2233a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2234051e1850SLai Jiangshan /* 2235051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2236051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2237051e1850SLai Jiangshan * already become busy. 2238051e1850SLai Jiangshan */ 223963d95a91STejun Heo if (need_to_create_worker(pool)) 2240e22bee78STejun Heo goto restart; 2241e22bee78STejun Heo } 2242e22bee78STejun Heo 2243e22bee78STejun Heo /** 2244e22bee78STejun Heo * manage_workers - manage worker pool 2245e22bee78STejun Heo * @worker: self 2246e22bee78STejun Heo * 2247706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2248e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2249706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2250e22bee78STejun Heo * 2251e22bee78STejun Heo * The caller can safely start processing works on false return. On 2252e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2253e22bee78STejun Heo * and may_start_working() is true. 2254e22bee78STejun Heo * 2255e22bee78STejun Heo * CONTEXT: 2256a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2257e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2258e22bee78STejun Heo * 2259d185af30SYacine Belkadi * Return: 226029187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 226129187a9eSTejun Heo * start processing works, %true if management function was performed and 226229187a9eSTejun Heo * the conditions that the caller verified before calling the function may 226329187a9eSTejun Heo * no longer be true. 2264e22bee78STejun Heo */ 2265e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2266e22bee78STejun Heo { 226763d95a91STejun Heo struct worker_pool *pool = worker->pool; 2268e22bee78STejun Heo 2269692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 227029187a9eSTejun Heo return false; 2271692b4825STejun Heo 2272692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 22732607d7a6STejun Heo pool->manager = worker; 2274e22bee78STejun Heo 227529187a9eSTejun Heo maybe_create_worker(pool); 2276e22bee78STejun Heo 22772607d7a6STejun Heo pool->manager = NULL; 2278692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2279d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 228029187a9eSTejun Heo return true; 2281e22bee78STejun Heo } 2282e22bee78STejun Heo 2283a62428c0STejun Heo /** 2284a62428c0STejun Heo * process_one_work - process single work 2285c34056a3STejun Heo * @worker: self 2286a62428c0STejun Heo * @work: work to process 2287a62428c0STejun Heo * 2288a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2289a62428c0STejun Heo * process a single work including synchronization against and 2290a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2291a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2292a62428c0STejun Heo * call this function to process a work. 2293a62428c0STejun Heo * 2294a62428c0STejun Heo * CONTEXT: 2295a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2296a62428c0STejun Heo */ 2297c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2298d565ed63STejun Heo __releases(&pool->lock) 2299d565ed63STejun Heo __acquires(&pool->lock) 23001da177e4SLinus Torvalds { 2301112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2302bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2303112202d9STejun Heo bool cpu_intensive = pwq->wq->flags & WQ_CPU_INTENSIVE; 2304c4560c2cSLai Jiangshan unsigned long work_data; 23057e11629dSTejun Heo struct worker *collision; 23064e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 23074e6045f1SJohannes Berg /* 2308a62428c0STejun Heo * It is permissible to free the struct work_struct from 2309a62428c0STejun Heo * inside the function that is called from it, this we need to 2310a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2311a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2312a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 23134e6045f1SJohannes Berg */ 23144d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 23154d82a1deSPeter Zijlstra 23164d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 23174e6045f1SJohannes Berg #endif 2318807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 231985327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2320ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 232125511a47STejun Heo 23227e11629dSTejun Heo /* 23237e11629dSTejun Heo * A single work shouldn't be executed concurrently by 23247e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 23257e11629dSTejun Heo * already processing the work. If so, defer the work to the 23267e11629dSTejun Heo * currently executing one. 23277e11629dSTejun Heo */ 2328c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 23297e11629dSTejun Heo if (unlikely(collision)) { 23307e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 23317e11629dSTejun Heo return; 23327e11629dSTejun Heo } 23331da177e4SLinus Torvalds 23348930cabaSTejun Heo /* claim and dequeue */ 23351da177e4SLinus Torvalds debug_work_deactivate(work); 2336c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2337c34056a3STejun Heo worker->current_work = work; 2338a2c1c57bSTejun Heo worker->current_func = work->func; 2339112202d9STejun Heo worker->current_pwq = pwq; 2340c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2341d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 23427a22ad75STejun Heo 23438bf89593STejun Heo /* 23448bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 23458bf89593STejun Heo * overridden through set_worker_desc(). 23468bf89593STejun Heo */ 23478bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 23488bf89593STejun Heo 2349a62428c0STejun Heo list_del_init(&work->entry); 2350a62428c0STejun Heo 2351649027d7STejun Heo /* 2352228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2353228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2354228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2355228f1d00SLai Jiangshan * execution of the pending work items. 2356fb0e7bebSTejun Heo */ 2357fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2358228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2359fb0e7bebSTejun Heo 2360974271c4STejun Heo /* 2361a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2362a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2363a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2364a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2365228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2366974271c4STejun Heo */ 2367a489a03eSLai Jiangshan if (need_more_worker(pool)) 236863d95a91STejun Heo wake_up_worker(pool); 2369974271c4STejun Heo 23708930cabaSTejun Heo /* 23717c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2372d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 237323657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 237423657bb1STejun Heo * disabled. 23758930cabaSTejun Heo */ 23767c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 23771da177e4SLinus Torvalds 2378a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2379365970a1SDavid Howells 2380a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 23813295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2382e6f3faa7SPeter Zijlstra /* 2383f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2384f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2385e6f3faa7SPeter Zijlstra * 2386e6f3faa7SPeter Zijlstra * However, that would result in: 2387e6f3faa7SPeter Zijlstra * 2388e6f3faa7SPeter Zijlstra * A(W1) 2389e6f3faa7SPeter Zijlstra * WFC(C) 2390e6f3faa7SPeter Zijlstra * A(W1) 2391e6f3faa7SPeter Zijlstra * C(C) 2392e6f3faa7SPeter Zijlstra * 2393e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2394e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2395e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2396f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2397e6f3faa7SPeter Zijlstra * these locks. 2398e6f3faa7SPeter Zijlstra * 2399e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2400e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2401e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2402e6f3faa7SPeter Zijlstra */ 2403f52be570SPeter Zijlstra lockdep_invariant_state(true); 2404e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2405a2c1c57bSTejun Heo worker->current_func(work); 2406e36c886aSArjan van de Ven /* 2407e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2408e36c886aSArjan van de Ven * point will only record its address. 2409e36c886aSArjan van de Ven */ 24101c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 24113295f0efSIngo Molnar lock_map_release(&lockdep_map); 2412112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 24131da177e4SLinus Torvalds 2414d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2415044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2416d75f773cSSakari Ailus " last function: %ps\n", 2417a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2418a2c1c57bSTejun Heo worker->current_func); 2419d5abe669SPeter Zijlstra debug_show_held_locks(current); 2420d5abe669SPeter Zijlstra dump_stack(); 2421d5abe669SPeter Zijlstra } 2422d5abe669SPeter Zijlstra 2423b22ce278STejun Heo /* 2424025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2425b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2426b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2427b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2428789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2429789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2430b22ce278STejun Heo */ 2431a7e6425eSPaul E. McKenney cond_resched(); 2432b22ce278STejun Heo 2433a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2434a62428c0STejun Heo 2435fb0e7bebSTejun Heo /* clear cpu intensive status */ 2436fb0e7bebSTejun Heo if (unlikely(cpu_intensive)) 2437fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2438fb0e7bebSTejun Heo 24391b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 24401b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 24411b69ac6bSJohannes Weiner 2442a62428c0STejun Heo /* we're done with it, release */ 244342f8570fSSasha Levin hash_del(&worker->hentry); 2444c34056a3STejun Heo worker->current_work = NULL; 2445a2c1c57bSTejun Heo worker->current_func = NULL; 2446112202d9STejun Heo worker->current_pwq = NULL; 2447d812796eSLai Jiangshan worker->current_color = INT_MAX; 2448c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 24491da177e4SLinus Torvalds } 24501da177e4SLinus Torvalds 2451affee4b2STejun Heo /** 2452affee4b2STejun Heo * process_scheduled_works - process scheduled works 2453affee4b2STejun Heo * @worker: self 2454affee4b2STejun Heo * 2455affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2456affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2457affee4b2STejun Heo * fetches a work from the top and executes it. 2458affee4b2STejun Heo * 2459affee4b2STejun Heo * CONTEXT: 2460a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2461affee4b2STejun Heo * multiple times. 2462affee4b2STejun Heo */ 2463affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 24641da177e4SLinus Torvalds { 2465affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2466affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2467a62428c0STejun Heo struct work_struct, entry); 2468c34056a3STejun Heo process_one_work(worker, work); 2469a62428c0STejun Heo } 24701da177e4SLinus Torvalds } 24711da177e4SLinus Torvalds 2472197f6accSTejun Heo static void set_pf_worker(bool val) 2473197f6accSTejun Heo { 2474197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2475197f6accSTejun Heo if (val) 2476197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2477197f6accSTejun Heo else 2478197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2479197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2480197f6accSTejun Heo } 2481197f6accSTejun Heo 24824690c4abSTejun Heo /** 24834690c4abSTejun Heo * worker_thread - the worker thread function 2484c34056a3STejun Heo * @__worker: self 24854690c4abSTejun Heo * 2486c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2487c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2488c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2489c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2490c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2491d185af30SYacine Belkadi * 2492d185af30SYacine Belkadi * Return: 0 24934690c4abSTejun Heo */ 2494c34056a3STejun Heo static int worker_thread(void *__worker) 24951da177e4SLinus Torvalds { 2496c34056a3STejun Heo struct worker *worker = __worker; 2497bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 24981da177e4SLinus Torvalds 2499e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2500197f6accSTejun Heo set_pf_worker(true); 2501c8e55f36STejun Heo woke_up: 2502a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2503affee4b2STejun Heo 2504a9ab775bSTejun Heo /* am I supposed to die? */ 2505a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2506a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2507197f6accSTejun Heo set_pf_worker(false); 250860f5a4bcSLai Jiangshan 250960f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2510e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2511a2d812a2STejun Heo worker_detach_from_pool(worker); 2512e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 251360f5a4bcSLai Jiangshan kfree(worker); 2514c8e55f36STejun Heo return 0; 2515c8e55f36STejun Heo } 2516c8e55f36STejun Heo 2517c8e55f36STejun Heo worker_leave_idle(worker); 2518db7bccf4STejun Heo recheck: 2519e22bee78STejun Heo /* no more worker necessary? */ 252063d95a91STejun Heo if (!need_more_worker(pool)) 2521e22bee78STejun Heo goto sleep; 2522e22bee78STejun Heo 2523e22bee78STejun Heo /* do we need to manage? */ 252463d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2525e22bee78STejun Heo goto recheck; 2526e22bee78STejun Heo 2527c8e55f36STejun Heo /* 2528c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2529c8e55f36STejun Heo * preparing to process a work or actually processing it. 2530c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2531c8e55f36STejun Heo */ 25326183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2533c8e55f36STejun Heo 2534e22bee78STejun Heo /* 2535a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2536a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2537a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2538a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2539a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2540e22bee78STejun Heo */ 2541a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2542e22bee78STejun Heo 2543e22bee78STejun Heo do { 2544affee4b2STejun Heo struct work_struct *work = 2545bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2546affee4b2STejun Heo struct work_struct, entry); 2547affee4b2STejun Heo 254882607adcSTejun Heo pool->watchdog_ts = jiffies; 254982607adcSTejun Heo 2550c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2551affee4b2STejun Heo /* optimization path, not strictly necessary */ 2552affee4b2STejun Heo process_one_work(worker, work); 2553affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2554affee4b2STejun Heo process_scheduled_works(worker); 2555affee4b2STejun Heo } else { 2556c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2557affee4b2STejun Heo process_scheduled_works(worker); 2558affee4b2STejun Heo } 255963d95a91STejun Heo } while (keep_working(pool)); 2560affee4b2STejun Heo 2561228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2562d313dd85STejun Heo sleep: 2563c8e55f36STejun Heo /* 2564d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2565d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2566d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2567d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2568d565ed63STejun Heo * event. 2569c8e55f36STejun Heo */ 2570c8e55f36STejun Heo worker_enter_idle(worker); 2571c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2572a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 25731da177e4SLinus Torvalds schedule(); 2574c8e55f36STejun Heo goto woke_up; 25751da177e4SLinus Torvalds } 25761da177e4SLinus Torvalds 2577e22bee78STejun Heo /** 2578e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2579111c225aSTejun Heo * @__rescuer: self 2580e22bee78STejun Heo * 2581e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2582493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2583e22bee78STejun Heo * 2584706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2585e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2586e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2587e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2588e22bee78STejun Heo * the problem rescuer solves. 2589e22bee78STejun Heo * 2590706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2591706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2592e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2593e22bee78STejun Heo * 2594e22bee78STejun Heo * This should happen rarely. 2595d185af30SYacine Belkadi * 2596d185af30SYacine Belkadi * Return: 0 2597e22bee78STejun Heo */ 2598111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2599e22bee78STejun Heo { 2600111c225aSTejun Heo struct worker *rescuer = __rescuer; 2601111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2602e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 26034d595b86SLai Jiangshan bool should_stop; 2604e22bee78STejun Heo 2605e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2606111c225aSTejun Heo 2607111c225aSTejun Heo /* 2608111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2609111c225aSTejun Heo * doesn't participate in concurrency management. 2610111c225aSTejun Heo */ 2611197f6accSTejun Heo set_pf_worker(true); 2612e22bee78STejun Heo repeat: 2613c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 26141da177e4SLinus Torvalds 26154d595b86SLai Jiangshan /* 26164d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 26174d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 26184d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 26194d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 26204d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 26214d595b86SLai Jiangshan * list is always empty on exit. 26224d595b86SLai Jiangshan */ 26234d595b86SLai Jiangshan should_stop = kthread_should_stop(); 26241da177e4SLinus Torvalds 2625493a1724STejun Heo /* see whether any pwq is asking for help */ 2626a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2627493a1724STejun Heo 2628493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2629493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2630493a1724STejun Heo struct pool_workqueue, mayday_node); 2631112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2632e22bee78STejun Heo struct work_struct *work, *n; 263382607adcSTejun Heo bool first = true; 2634e22bee78STejun Heo 2635e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2636493a1724STejun Heo list_del_init(&pwq->mayday_node); 2637493a1724STejun Heo 2638a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2639e22bee78STejun Heo 264051697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 264151697d39SLai Jiangshan 2642a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2643e22bee78STejun Heo 2644e22bee78STejun Heo /* 2645e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2646e22bee78STejun Heo * process'em. 2647e22bee78STejun Heo */ 26480479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 264982607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 265082607adcSTejun Heo if (get_work_pwq(work) == pwq) { 265182607adcSTejun Heo if (first) 265282607adcSTejun Heo pool->watchdog_ts = jiffies; 2653e22bee78STejun Heo move_linked_works(work, scheduled, &n); 265482607adcSTejun Heo } 265582607adcSTejun Heo first = false; 265682607adcSTejun Heo } 2657e22bee78STejun Heo 2658008847f6SNeilBrown if (!list_empty(scheduled)) { 2659e22bee78STejun Heo process_scheduled_works(rescuer); 26607576958aSTejun Heo 26617576958aSTejun Heo /* 2662008847f6SNeilBrown * The above execution of rescued work items could 2663008847f6SNeilBrown * have created more to rescue through 2664f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2665008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2666008847f6SNeilBrown * that such back-to-back work items, which may be 2667008847f6SNeilBrown * being used to relieve memory pressure, don't 2668008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2669008847f6SNeilBrown */ 26704f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2671a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2672e66b39afSTejun Heo /* 2673e66b39afSTejun Heo * Queue iff we aren't racing destruction 2674e66b39afSTejun Heo * and somebody else hasn't queued it already. 2675e66b39afSTejun Heo */ 2676e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2677008847f6SNeilBrown get_pwq(pwq); 2678e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2679e66b39afSTejun Heo } 2680a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2681008847f6SNeilBrown } 2682008847f6SNeilBrown } 2683008847f6SNeilBrown 2684008847f6SNeilBrown /* 268577668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 268613b1d625SLai Jiangshan * go away while we're still attached to it. 268777668c8bSLai Jiangshan */ 268877668c8bSLai Jiangshan put_pwq(pwq); 268977668c8bSLai Jiangshan 269077668c8bSLai Jiangshan /* 2691d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 26927576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 26937576958aSTejun Heo * and stalling the execution. 26947576958aSTejun Heo */ 2695d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 269663d95a91STejun Heo wake_up_worker(pool); 26977576958aSTejun Heo 2698a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 269913b1d625SLai Jiangshan 2700a2d812a2STejun Heo worker_detach_from_pool(rescuer); 270113b1d625SLai Jiangshan 2702a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 2705a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2706493a1724STejun Heo 27074d595b86SLai Jiangshan if (should_stop) { 27084d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2709197f6accSTejun Heo set_pf_worker(false); 27104d595b86SLai Jiangshan return 0; 27114d595b86SLai Jiangshan } 27124d595b86SLai Jiangshan 2713111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2714111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2715e22bee78STejun Heo schedule(); 2716e22bee78STejun Heo goto repeat; 27171da177e4SLinus Torvalds } 27181da177e4SLinus Torvalds 2719fca839c0STejun Heo /** 2720fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2721fca839c0STejun Heo * @target_wq: workqueue being flushed 2722fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2723fca839c0STejun Heo * 2724fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2725fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2726fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2727fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2728fca839c0STejun Heo * a deadlock. 2729fca839c0STejun Heo */ 2730fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2731fca839c0STejun Heo struct work_struct *target_work) 2732fca839c0STejun Heo { 2733fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2734fca839c0STejun Heo struct worker *worker; 2735fca839c0STejun Heo 2736fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2737fca839c0STejun Heo return; 2738fca839c0STejun Heo 2739fca839c0STejun Heo worker = current_wq_worker(); 2740fca839c0STejun Heo 2741fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2742d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2743fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 274423d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 274523d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2746d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2747fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2748fca839c0STejun Heo target_wq->name, target_func); 2749fca839c0STejun Heo } 2750fca839c0STejun Heo 2751fc2e4d70SOleg Nesterov struct wq_barrier { 2752fc2e4d70SOleg Nesterov struct work_struct work; 2753fc2e4d70SOleg Nesterov struct completion done; 27542607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2755fc2e4d70SOleg Nesterov }; 2756fc2e4d70SOleg Nesterov 2757fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2758fc2e4d70SOleg Nesterov { 2759fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2760fc2e4d70SOleg Nesterov complete(&barr->done); 2761fc2e4d70SOleg Nesterov } 2762fc2e4d70SOleg Nesterov 27634690c4abSTejun Heo /** 27644690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2765112202d9STejun Heo * @pwq: pwq to insert barrier into 27664690c4abSTejun Heo * @barr: wq_barrier to insert 2767affee4b2STejun Heo * @target: target work to attach @barr to 2768affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 27694690c4abSTejun Heo * 2770affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2771affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2772affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2773affee4b2STejun Heo * cpu. 2774affee4b2STejun Heo * 2775affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2776affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2777affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2778affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2779affee4b2STejun Heo * after a work with LINKED flag set. 2780affee4b2STejun Heo * 2781affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2782112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 27834690c4abSTejun Heo * 27844690c4abSTejun Heo * CONTEXT: 2785a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 27864690c4abSTejun Heo */ 2787112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2788affee4b2STejun Heo struct wq_barrier *barr, 2789affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2790fc2e4d70SOleg Nesterov { 2791d812796eSLai Jiangshan unsigned int work_flags = 0; 2792d812796eSLai Jiangshan unsigned int work_color; 2793affee4b2STejun Heo struct list_head *head; 2794affee4b2STejun Heo 2795dc186ad7SThomas Gleixner /* 2796d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2797dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2798dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2799dc186ad7SThomas Gleixner * might deadlock. 2800dc186ad7SThomas Gleixner */ 2801ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 280222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 280352fa5bc5SBoqun Feng 2804fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2805fd1a5b04SByungchul Park 28062607d7a6STejun Heo barr->task = current; 280783c22520SOleg Nesterov 2808018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 2809018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2810018f3a13SLai Jiangshan 2811affee4b2STejun Heo /* 2812affee4b2STejun Heo * If @target is currently being executed, schedule the 2813affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2814affee4b2STejun Heo */ 2815d812796eSLai Jiangshan if (worker) { 2816affee4b2STejun Heo head = worker->scheduled.next; 2817d812796eSLai Jiangshan work_color = worker->current_color; 2818d812796eSLai Jiangshan } else { 2819affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2820affee4b2STejun Heo 2821affee4b2STejun Heo head = target->entry.next; 2822affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2823d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 2824d812796eSLai Jiangshan work_color = get_work_color(*bits); 2825affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2826affee4b2STejun Heo } 2827affee4b2STejun Heo 2828d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 2829d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 2830d812796eSLai Jiangshan 2831dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2832d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 2833fc2e4d70SOleg Nesterov } 2834fc2e4d70SOleg Nesterov 283573f53c4aSTejun Heo /** 2836112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 283773f53c4aSTejun Heo * @wq: workqueue being flushed 283873f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 283973f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 284073f53c4aSTejun Heo * 2841112202d9STejun Heo * Prepare pwqs for workqueue flushing. 284273f53c4aSTejun Heo * 2843112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2844112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2845112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2846112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2847112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 284873f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 284973f53c4aSTejun Heo * 285073f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 285173f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 285273f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 285373f53c4aSTejun Heo * is returned. 285473f53c4aSTejun Heo * 2855112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 285673f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 285773f53c4aSTejun Heo * advanced to @work_color. 285873f53c4aSTejun Heo * 285973f53c4aSTejun Heo * CONTEXT: 28603c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 286173f53c4aSTejun Heo * 2862d185af30SYacine Belkadi * Return: 286373f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 286473f53c4aSTejun Heo * otherwise. 286573f53c4aSTejun Heo */ 2866112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 286773f53c4aSTejun Heo int flush_color, int work_color) 28681da177e4SLinus Torvalds { 286973f53c4aSTejun Heo bool wait = false; 287049e3cf44STejun Heo struct pool_workqueue *pwq; 28711da177e4SLinus Torvalds 287273f53c4aSTejun Heo if (flush_color >= 0) { 28736183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2874112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2875dc186ad7SThomas Gleixner } 287614441960SOleg Nesterov 287749e3cf44STejun Heo for_each_pwq(pwq, wq) { 2878112202d9STejun Heo struct worker_pool *pool = pwq->pool; 28791da177e4SLinus Torvalds 2880a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 288173f53c4aSTejun Heo 288273f53c4aSTejun Heo if (flush_color >= 0) { 28836183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 288473f53c4aSTejun Heo 2885112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2886112202d9STejun Heo pwq->flush_color = flush_color; 2887112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 288873f53c4aSTejun Heo wait = true; 28891da177e4SLinus Torvalds } 289073f53c4aSTejun Heo } 289173f53c4aSTejun Heo 289273f53c4aSTejun Heo if (work_color >= 0) { 28936183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2894112202d9STejun Heo pwq->work_color = work_color; 289573f53c4aSTejun Heo } 289673f53c4aSTejun Heo 2897a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 28981da177e4SLinus Torvalds } 28991da177e4SLinus Torvalds 2900112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 290173f53c4aSTejun Heo complete(&wq->first_flusher->done); 290273f53c4aSTejun Heo 290373f53c4aSTejun Heo return wait; 290483c22520SOleg Nesterov } 29051da177e4SLinus Torvalds 29060fcb78c2SRolf Eike Beer /** 2907c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 29080fcb78c2SRolf Eike Beer * @wq: workqueue to flush 29091da177e4SLinus Torvalds * 2910c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2911c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 29121da177e4SLinus Torvalds */ 2913c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 29141da177e4SLinus Torvalds { 291573f53c4aSTejun Heo struct wq_flusher this_flusher = { 291673f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 291773f53c4aSTejun Heo .flush_color = -1, 2918fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 291973f53c4aSTejun Heo }; 292073f53c4aSTejun Heo int next_color; 2921b1f4ec17SOleg Nesterov 29223347fa09STejun Heo if (WARN_ON(!wq_online)) 29233347fa09STejun Heo return; 29243347fa09STejun Heo 292587915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 292687915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 292787915adcSJohannes Berg 29283c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 292973f53c4aSTejun Heo 293073f53c4aSTejun Heo /* 293173f53c4aSTejun Heo * Start-to-wait phase 293273f53c4aSTejun Heo */ 293373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 293473f53c4aSTejun Heo 293573f53c4aSTejun Heo if (next_color != wq->flush_color) { 293673f53c4aSTejun Heo /* 293773f53c4aSTejun Heo * Color space is not full. The current work_color 293873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 293973f53c4aSTejun Heo * by one. 294073f53c4aSTejun Heo */ 29416183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 294273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 294373f53c4aSTejun Heo wq->work_color = next_color; 294473f53c4aSTejun Heo 294573f53c4aSTejun Heo if (!wq->first_flusher) { 294673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 29476183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 294873f53c4aSTejun Heo 294973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 295073f53c4aSTejun Heo 2951112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 295273f53c4aSTejun Heo wq->work_color)) { 295373f53c4aSTejun Heo /* nothing to flush, done */ 295473f53c4aSTejun Heo wq->flush_color = next_color; 295573f53c4aSTejun Heo wq->first_flusher = NULL; 295673f53c4aSTejun Heo goto out_unlock; 295773f53c4aSTejun Heo } 295873f53c4aSTejun Heo } else { 295973f53c4aSTejun Heo /* wait in queue */ 29606183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 296173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 2962112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 296373f53c4aSTejun Heo } 296473f53c4aSTejun Heo } else { 296573f53c4aSTejun Heo /* 296673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 296773f53c4aSTejun Heo * The next flush completion will assign us 296873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 296973f53c4aSTejun Heo */ 297073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 297173f53c4aSTejun Heo } 297273f53c4aSTejun Heo 2973fca839c0STejun Heo check_flush_dependency(wq, NULL); 2974fca839c0STejun Heo 29753c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 297673f53c4aSTejun Heo 297773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 297873f53c4aSTejun Heo 297973f53c4aSTejun Heo /* 298073f53c4aSTejun Heo * Wake-up-and-cascade phase 298173f53c4aSTejun Heo * 298273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 298373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 298473f53c4aSTejun Heo */ 298500d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 298673f53c4aSTejun Heo return; 298773f53c4aSTejun Heo 29883c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 298973f53c4aSTejun Heo 29904ce48b37STejun Heo /* we might have raced, check again with mutex held */ 29914ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 29924ce48b37STejun Heo goto out_unlock; 29934ce48b37STejun Heo 299400d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 299573f53c4aSTejun Heo 29966183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 29976183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 299873f53c4aSTejun Heo 299973f53c4aSTejun Heo while (true) { 300073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 300173f53c4aSTejun Heo 300273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 300373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 300473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 300573f53c4aSTejun Heo break; 300673f53c4aSTejun Heo list_del_init(&next->list); 300773f53c4aSTejun Heo complete(&next->done); 300873f53c4aSTejun Heo } 300973f53c4aSTejun Heo 30106183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 301173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 301273f53c4aSTejun Heo 301373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 301473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 301573f53c4aSTejun Heo 301673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 301773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 301873f53c4aSTejun Heo /* 301973f53c4aSTejun Heo * Assign the same color to all overflowed 302073f53c4aSTejun Heo * flushers, advance work_color and append to 302173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 302273f53c4aSTejun Heo * phase for these overflowed flushers. 302373f53c4aSTejun Heo */ 302473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 302573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 302673f53c4aSTejun Heo 302773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 302873f53c4aSTejun Heo 302973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 303073f53c4aSTejun Heo &wq->flusher_queue); 3031112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 303273f53c4aSTejun Heo } 303373f53c4aSTejun Heo 303473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 30356183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 303673f53c4aSTejun Heo break; 303773f53c4aSTejun Heo } 303873f53c4aSTejun Heo 303973f53c4aSTejun Heo /* 304073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3041112202d9STejun Heo * the new first flusher and arm pwqs. 304273f53c4aSTejun Heo */ 30436183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 30446183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 304573f53c4aSTejun Heo 304673f53c4aSTejun Heo list_del_init(&next->list); 304773f53c4aSTejun Heo wq->first_flusher = next; 304873f53c4aSTejun Heo 3049112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 305073f53c4aSTejun Heo break; 305173f53c4aSTejun Heo 305273f53c4aSTejun Heo /* 305373f53c4aSTejun Heo * Meh... this color is already done, clear first 305473f53c4aSTejun Heo * flusher and repeat cascading. 305573f53c4aSTejun Heo */ 305673f53c4aSTejun Heo wq->first_flusher = NULL; 305773f53c4aSTejun Heo } 305873f53c4aSTejun Heo 305973f53c4aSTejun Heo out_unlock: 30603c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 30611da177e4SLinus Torvalds } 3062c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 30631da177e4SLinus Torvalds 30649c5a2ba7STejun Heo /** 30659c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 30669c5a2ba7STejun Heo * @wq: workqueue to drain 30679c5a2ba7STejun Heo * 30689c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 30699c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 30709c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3071b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 30729c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 30739c5a2ba7STejun Heo * takes too long. 30749c5a2ba7STejun Heo */ 30759c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 30769c5a2ba7STejun Heo { 30779c5a2ba7STejun Heo unsigned int flush_cnt = 0; 307849e3cf44STejun Heo struct pool_workqueue *pwq; 30799c5a2ba7STejun Heo 30809c5a2ba7STejun Heo /* 30819c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 30829c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3083618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 30849c5a2ba7STejun Heo */ 308587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 30869c5a2ba7STejun Heo if (!wq->nr_drainers++) 3087618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 308887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 30899c5a2ba7STejun Heo reflush: 3090c4f135d6STetsuo Handa __flush_workqueue(wq); 30919c5a2ba7STejun Heo 3092b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 309376af4d93STejun Heo 309449e3cf44STejun Heo for_each_pwq(pwq, wq) { 3095fa2563e4SThomas Tuttle bool drained; 30969c5a2ba7STejun Heo 3097a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3098f97a4a1aSLai Jiangshan drained = !pwq->nr_active && list_empty(&pwq->inactive_works); 3099a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3100fa2563e4SThomas Tuttle 3101fa2563e4SThomas Tuttle if (drained) 31029c5a2ba7STejun Heo continue; 31039c5a2ba7STejun Heo 31049c5a2ba7STejun Heo if (++flush_cnt == 10 || 31059c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3106e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3107e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 310876af4d93STejun Heo 3109b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 31109c5a2ba7STejun Heo goto reflush; 31119c5a2ba7STejun Heo } 31129c5a2ba7STejun Heo 31139c5a2ba7STejun Heo if (!--wq->nr_drainers) 3114618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 311587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 31169c5a2ba7STejun Heo } 31179c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 31189c5a2ba7STejun Heo 3119d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3120d6e89786SJohannes Berg bool from_cancel) 3121baf59022STejun Heo { 3122baf59022STejun Heo struct worker *worker = NULL; 3123c9e7cf27STejun Heo struct worker_pool *pool; 3124112202d9STejun Heo struct pool_workqueue *pwq; 3125baf59022STejun Heo 3126baf59022STejun Heo might_sleep(); 3127baf59022STejun Heo 312824acfb71SThomas Gleixner rcu_read_lock(); 3129fa1b54e6STejun Heo pool = get_work_pool(work); 3130fa1b54e6STejun Heo if (!pool) { 313124acfb71SThomas Gleixner rcu_read_unlock(); 3132fa1b54e6STejun Heo return false; 3133fa1b54e6STejun Heo } 3134fa1b54e6STejun Heo 3135a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 31360b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3137112202d9STejun Heo pwq = get_work_pwq(work); 3138112202d9STejun Heo if (pwq) { 3139112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3140baf59022STejun Heo goto already_gone; 3141606a5020STejun Heo } else { 3142c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3143baf59022STejun Heo if (!worker) 3144baf59022STejun Heo goto already_gone; 3145112202d9STejun Heo pwq = worker->current_pwq; 3146606a5020STejun Heo } 3147baf59022STejun Heo 3148fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3149fca839c0STejun Heo 3150112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3151a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3152baf59022STejun Heo 3153e159489bSTejun Heo /* 3154a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3155a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3156a1d14934SPeter Zijlstra * 3157a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3158a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3159a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3160a1d14934SPeter Zijlstra * forward progress. 3161e159489bSTejun Heo */ 3162d6e89786SJohannes Berg if (!from_cancel && 3163d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3164112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3165112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3166a1d14934SPeter Zijlstra } 316724acfb71SThomas Gleixner rcu_read_unlock(); 3168baf59022STejun Heo return true; 3169baf59022STejun Heo already_gone: 3170a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 317124acfb71SThomas Gleixner rcu_read_unlock(); 3172baf59022STejun Heo return false; 3173baf59022STejun Heo } 3174baf59022STejun Heo 3175d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3176d6e89786SJohannes Berg { 3177d6e89786SJohannes Berg struct wq_barrier barr; 3178d6e89786SJohannes Berg 3179d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3180d6e89786SJohannes Berg return false; 3181d6e89786SJohannes Berg 31824d43d395STetsuo Handa if (WARN_ON(!work->func)) 31834d43d395STetsuo Handa return false; 31844d43d395STetsuo Handa 318587915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 318687915adcSJohannes Berg lock_map_release(&work->lockdep_map); 318787915adcSJohannes Berg 3188d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3189d6e89786SJohannes Berg wait_for_completion(&barr.done); 3190d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3191d6e89786SJohannes Berg return true; 3192d6e89786SJohannes Berg } else { 3193d6e89786SJohannes Berg return false; 3194d6e89786SJohannes Berg } 3195d6e89786SJohannes Berg } 3196d6e89786SJohannes Berg 3197db700897SOleg Nesterov /** 3198401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3199401a8d04STejun Heo * @work: the work to flush 3200db700897SOleg Nesterov * 3201606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3202606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3203401a8d04STejun Heo * 3204d185af30SYacine Belkadi * Return: 3205401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3206401a8d04STejun Heo * %false if it was already idle. 3207db700897SOleg Nesterov */ 3208401a8d04STejun Heo bool flush_work(struct work_struct *work) 3209db700897SOleg Nesterov { 3210d6e89786SJohannes Berg return __flush_work(work, false); 3211606a5020STejun Heo } 3212db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3213db700897SOleg Nesterov 32148603e1b3STejun Heo struct cwt_wait { 3215ac6424b9SIngo Molnar wait_queue_entry_t wait; 32168603e1b3STejun Heo struct work_struct *work; 32178603e1b3STejun Heo }; 32188603e1b3STejun Heo 3219ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 32208603e1b3STejun Heo { 32218603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 32228603e1b3STejun Heo 32238603e1b3STejun Heo if (cwait->work != key) 32248603e1b3STejun Heo return 0; 32258603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 32268603e1b3STejun Heo } 32278603e1b3STejun Heo 322836e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3229401a8d04STejun Heo { 32308603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3231bbb68dfaSTejun Heo unsigned long flags; 32321f1f642eSOleg Nesterov int ret; 32331f1f642eSOleg Nesterov 32341f1f642eSOleg Nesterov do { 3235bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3236bbb68dfaSTejun Heo /* 32378603e1b3STejun Heo * If someone else is already canceling, wait for it to 32388603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 32398603e1b3STejun Heo * because we may get scheduled between @work's completion 32408603e1b3STejun Heo * and the other canceling task resuming and clearing 32418603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 32428603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 32438603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 32448603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 32458603e1b3STejun Heo * we're hogging the CPU. 32468603e1b3STejun Heo * 32478603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 32488603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 32498603e1b3STejun Heo * wake function which matches @work along with exclusive 32508603e1b3STejun Heo * wait and wakeup. 3251bbb68dfaSTejun Heo */ 32528603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 32538603e1b3STejun Heo struct cwt_wait cwait; 32548603e1b3STejun Heo 32558603e1b3STejun Heo init_wait(&cwait.wait); 32568603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 32578603e1b3STejun Heo cwait.work = work; 32588603e1b3STejun Heo 32598603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 32608603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 32618603e1b3STejun Heo if (work_is_canceling(work)) 32628603e1b3STejun Heo schedule(); 32638603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 32648603e1b3STejun Heo } 32651f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 32661f1f642eSOleg Nesterov 3267bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3268bbb68dfaSTejun Heo mark_work_canceling(work); 3269bbb68dfaSTejun Heo local_irq_restore(flags); 3270bbb68dfaSTejun Heo 32713347fa09STejun Heo /* 32723347fa09STejun Heo * This allows canceling during early boot. We know that @work 32733347fa09STejun Heo * isn't executing. 32743347fa09STejun Heo */ 32753347fa09STejun Heo if (wq_online) 3276d6e89786SJohannes Berg __flush_work(work, true); 32773347fa09STejun Heo 32787a22ad75STejun Heo clear_work_data(work); 32798603e1b3STejun Heo 32808603e1b3STejun Heo /* 32818603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 32828603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 32838603e1b3STejun Heo * visible there. 32848603e1b3STejun Heo */ 32858603e1b3STejun Heo smp_mb(); 32868603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 32878603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 32888603e1b3STejun Heo 32891f1f642eSOleg Nesterov return ret; 32901f1f642eSOleg Nesterov } 32911f1f642eSOleg Nesterov 32926e84d644SOleg Nesterov /** 3293401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3294401a8d04STejun Heo * @work: the work to cancel 32956e84d644SOleg Nesterov * 3296401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3297401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3298401a8d04STejun Heo * another workqueue. On return from this function, @work is 3299401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 33001f1f642eSOleg Nesterov * 3301401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3302401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 33036e84d644SOleg Nesterov * 3304401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 33056e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3306401a8d04STejun Heo * 3307d185af30SYacine Belkadi * Return: 3308401a8d04STejun Heo * %true if @work was pending, %false otherwise. 33096e84d644SOleg Nesterov */ 3310401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 33116e84d644SOleg Nesterov { 331236e227d2STejun Heo return __cancel_work_timer(work, false); 3313b89deed3SOleg Nesterov } 331428e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3315b89deed3SOleg Nesterov 33166e84d644SOleg Nesterov /** 3317401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3318401a8d04STejun Heo * @dwork: the delayed work to flush 33196e84d644SOleg Nesterov * 3320401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3321401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3322401a8d04STejun Heo * considers the last queueing instance of @dwork. 33231f1f642eSOleg Nesterov * 3324d185af30SYacine Belkadi * Return: 3325401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3326401a8d04STejun Heo * %false if it was already idle. 33276e84d644SOleg Nesterov */ 3328401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3329401a8d04STejun Heo { 33308930cabaSTejun Heo local_irq_disable(); 3331401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 333260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 33338930cabaSTejun Heo local_irq_enable(); 3334401a8d04STejun Heo return flush_work(&dwork->work); 3335401a8d04STejun Heo } 3336401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3337401a8d04STejun Heo 333805f0fe6bSTejun Heo /** 333905f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 334005f0fe6bSTejun Heo * @rwork: the rcu work to flush 334105f0fe6bSTejun Heo * 334205f0fe6bSTejun Heo * Return: 334305f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 334405f0fe6bSTejun Heo * %false if it was already idle. 334505f0fe6bSTejun Heo */ 334605f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 334705f0fe6bSTejun Heo { 334805f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 334905f0fe6bSTejun Heo rcu_barrier(); 335005f0fe6bSTejun Heo flush_work(&rwork->work); 335105f0fe6bSTejun Heo return true; 335205f0fe6bSTejun Heo } else { 335305f0fe6bSTejun Heo return flush_work(&rwork->work); 335405f0fe6bSTejun Heo } 335505f0fe6bSTejun Heo } 335605f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 335705f0fe6bSTejun Heo 3358f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3359f72b8792SJens Axboe { 3360f72b8792SJens Axboe unsigned long flags; 3361f72b8792SJens Axboe int ret; 3362f72b8792SJens Axboe 3363f72b8792SJens Axboe do { 3364f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3365f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3366f72b8792SJens Axboe 3367f72b8792SJens Axboe if (unlikely(ret < 0)) 3368f72b8792SJens Axboe return false; 3369f72b8792SJens Axboe 3370f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3371f72b8792SJens Axboe local_irq_restore(flags); 3372f72b8792SJens Axboe return ret; 3373f72b8792SJens Axboe } 3374f72b8792SJens Axboe 337573b4b532SAndrey Grodzovsky /* 337673b4b532SAndrey Grodzovsky * See cancel_delayed_work() 337773b4b532SAndrey Grodzovsky */ 337873b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 337973b4b532SAndrey Grodzovsky { 338073b4b532SAndrey Grodzovsky return __cancel_work(work, false); 338173b4b532SAndrey Grodzovsky } 338273b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 338373b4b532SAndrey Grodzovsky 3384401a8d04STejun Heo /** 338557b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 338657b30ae7STejun Heo * @dwork: delayed_work to cancel 338709383498STejun Heo * 3388d185af30SYacine Belkadi * Kill off a pending delayed_work. 3389d185af30SYacine Belkadi * 3390d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3391d185af30SYacine Belkadi * pending. 3392d185af30SYacine Belkadi * 3393d185af30SYacine Belkadi * Note: 3394d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3395d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3396d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 339709383498STejun Heo * 339857b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 339909383498STejun Heo */ 340057b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 340109383498STejun Heo { 3402f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 340309383498STejun Heo } 340457b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 340509383498STejun Heo 340609383498STejun Heo /** 3407401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3408401a8d04STejun Heo * @dwork: the delayed work cancel 3409401a8d04STejun Heo * 3410401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3411401a8d04STejun Heo * 3412d185af30SYacine Belkadi * Return: 3413401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3414401a8d04STejun Heo */ 3415401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 34166e84d644SOleg Nesterov { 341736e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 34186e84d644SOleg Nesterov } 3419f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 34201da177e4SLinus Torvalds 34210fcb78c2SRolf Eike Beer /** 342231ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3423b6136773SAndrew Morton * @func: the function to call 3424b6136773SAndrew Morton * 342531ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 342631ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3427b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 342831ddd871STejun Heo * 3429d185af30SYacine Belkadi * Return: 343031ddd871STejun Heo * 0 on success, -errno on failure. 3431b6136773SAndrew Morton */ 343265f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 343315316ba8SChristoph Lameter { 343415316ba8SChristoph Lameter int cpu; 343538f51568SNamhyung Kim struct work_struct __percpu *works; 343615316ba8SChristoph Lameter 3437b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3438b6136773SAndrew Morton if (!works) 343915316ba8SChristoph Lameter return -ENOMEM; 3440b6136773SAndrew Morton 3441ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 344293981800STejun Heo 344315316ba8SChristoph Lameter for_each_online_cpu(cpu) { 34449bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 34459bfb1839SIngo Molnar 34469bfb1839SIngo Molnar INIT_WORK(work, func); 34478de6d308SOleg Nesterov schedule_work_on(cpu, work); 344815316ba8SChristoph Lameter } 344993981800STejun Heo 345093981800STejun Heo for_each_online_cpu(cpu) 34518616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 345293981800STejun Heo 3453ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3454b6136773SAndrew Morton free_percpu(works); 345515316ba8SChristoph Lameter return 0; 345615316ba8SChristoph Lameter } 345715316ba8SChristoph Lameter 3458eef6a7d5SAlan Stern /** 34591fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 34601fa44ecaSJames Bottomley * @fn: the function to execute 34611fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 34621fa44ecaSJames Bottomley * be available when the work executes) 34631fa44ecaSJames Bottomley * 34641fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 34651fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 34661fa44ecaSJames Bottomley * 3467d185af30SYacine Belkadi * Return: 0 - function was executed 34681fa44ecaSJames Bottomley * 1 - function was scheduled for execution 34691fa44ecaSJames Bottomley */ 347065f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 34711fa44ecaSJames Bottomley { 34721fa44ecaSJames Bottomley if (!in_interrupt()) { 347365f27f38SDavid Howells fn(&ew->work); 34741fa44ecaSJames Bottomley return 0; 34751fa44ecaSJames Bottomley } 34761fa44ecaSJames Bottomley 347765f27f38SDavid Howells INIT_WORK(&ew->work, fn); 34781fa44ecaSJames Bottomley schedule_work(&ew->work); 34791fa44ecaSJames Bottomley 34801fa44ecaSJames Bottomley return 1; 34811fa44ecaSJames Bottomley } 34821fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 34831fa44ecaSJames Bottomley 34847a4e344cSTejun Heo /** 34857a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 34867a4e344cSTejun Heo * @attrs: workqueue_attrs to free 34877a4e344cSTejun Heo * 34887a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 34897a4e344cSTejun Heo */ 3490513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 34917a4e344cSTejun Heo { 34927a4e344cSTejun Heo if (attrs) { 34937a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 34947a4e344cSTejun Heo kfree(attrs); 34957a4e344cSTejun Heo } 34967a4e344cSTejun Heo } 34977a4e344cSTejun Heo 34987a4e344cSTejun Heo /** 34997a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 35007a4e344cSTejun Heo * 35017a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3502d185af30SYacine Belkadi * return it. 3503d185af30SYacine Belkadi * 3504d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 35057a4e344cSTejun Heo */ 3506513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 35077a4e344cSTejun Heo { 35087a4e344cSTejun Heo struct workqueue_attrs *attrs; 35097a4e344cSTejun Heo 3510be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 35117a4e344cSTejun Heo if (!attrs) 35127a4e344cSTejun Heo goto fail; 3513be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 35147a4e344cSTejun Heo goto fail; 35157a4e344cSTejun Heo 351613e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 35177a4e344cSTejun Heo return attrs; 35187a4e344cSTejun Heo fail: 35197a4e344cSTejun Heo free_workqueue_attrs(attrs); 35207a4e344cSTejun Heo return NULL; 35217a4e344cSTejun Heo } 35227a4e344cSTejun Heo 352329c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 352429c91e99STejun Heo const struct workqueue_attrs *from) 352529c91e99STejun Heo { 352629c91e99STejun Heo to->nice = from->nice; 352729c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 35282865a8fbSShaohua Li /* 35292865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 35302865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 35312865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 35322865a8fbSShaohua Li */ 35332865a8fbSShaohua Li to->no_numa = from->no_numa; 353429c91e99STejun Heo } 353529c91e99STejun Heo 353629c91e99STejun Heo /* hash value of the content of @attr */ 353729c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 353829c91e99STejun Heo { 353929c91e99STejun Heo u32 hash = 0; 354029c91e99STejun Heo 354129c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 354213e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 354313e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 354429c91e99STejun Heo return hash; 354529c91e99STejun Heo } 354629c91e99STejun Heo 354729c91e99STejun Heo /* content equality test */ 354829c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 354929c91e99STejun Heo const struct workqueue_attrs *b) 355029c91e99STejun Heo { 355129c91e99STejun Heo if (a->nice != b->nice) 355229c91e99STejun Heo return false; 355329c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 355429c91e99STejun Heo return false; 355529c91e99STejun Heo return true; 355629c91e99STejun Heo } 355729c91e99STejun Heo 35587a4e344cSTejun Heo /** 35597a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 35607a4e344cSTejun Heo * @pool: worker_pool to initialize 35617a4e344cSTejun Heo * 3562402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3563d185af30SYacine Belkadi * 3564d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 356529c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 356629c91e99STejun Heo * on @pool safely to release it. 35677a4e344cSTejun Heo */ 35687a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 35694e1a1f9aSTejun Heo { 3570a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 357129c91e99STejun Heo pool->id = -1; 357229c91e99STejun Heo pool->cpu = -1; 3573f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 35744e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 357582607adcSTejun Heo pool->watchdog_ts = jiffies; 35764e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 35774e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 35784e1a1f9aSTejun Heo hash_init(pool->busy_hash); 35794e1a1f9aSTejun Heo 358032a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 35813f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 35824e1a1f9aSTejun Heo 358332a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 35844e1a1f9aSTejun Heo 3585da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 3586e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 35877a4e344cSTejun Heo 35887cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 358929c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 359029c91e99STejun Heo pool->refcnt = 1; 359129c91e99STejun Heo 359229c91e99STejun Heo /* shouldn't fail above this point */ 3593be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 35947a4e344cSTejun Heo if (!pool->attrs) 35957a4e344cSTejun Heo return -ENOMEM; 35967a4e344cSTejun Heo return 0; 35974e1a1f9aSTejun Heo } 35984e1a1f9aSTejun Heo 3599669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3600669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3601669de8bdSBart Van Assche { 3602669de8bdSBart Van Assche char *lock_name; 3603669de8bdSBart Van Assche 3604669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3605669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3606669de8bdSBart Van Assche if (!lock_name) 3607669de8bdSBart Van Assche lock_name = wq->name; 360869a106c0SQian Cai 360969a106c0SQian Cai wq->lock_name = lock_name; 3610669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3611669de8bdSBart Van Assche } 3612669de8bdSBart Van Assche 3613669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3614669de8bdSBart Van Assche { 3615669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3616669de8bdSBart Van Assche } 3617669de8bdSBart Van Assche 3618669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3619669de8bdSBart Van Assche { 3620669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3621669de8bdSBart Van Assche kfree(wq->lock_name); 3622669de8bdSBart Van Assche } 3623669de8bdSBart Van Assche #else 3624669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3625669de8bdSBart Van Assche { 3626669de8bdSBart Van Assche } 3627669de8bdSBart Van Assche 3628669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3629669de8bdSBart Van Assche { 3630669de8bdSBart Van Assche } 3631669de8bdSBart Van Assche 3632669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3633669de8bdSBart Van Assche { 3634669de8bdSBart Van Assche } 3635669de8bdSBart Van Assche #endif 3636669de8bdSBart Van Assche 3637e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3638e2dca7adSTejun Heo { 3639e2dca7adSTejun Heo struct workqueue_struct *wq = 3640e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3641e2dca7adSTejun Heo 3642669de8bdSBart Van Assche wq_free_lockdep(wq); 3643669de8bdSBart Van Assche 3644e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3645e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3646e2dca7adSTejun Heo else 3647e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3648e2dca7adSTejun Heo 3649e2dca7adSTejun Heo kfree(wq); 3650e2dca7adSTejun Heo } 3651e2dca7adSTejun Heo 365229c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 365329c91e99STejun Heo { 365429c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 365529c91e99STejun Heo 36567cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 365729c91e99STejun Heo free_workqueue_attrs(pool->attrs); 365829c91e99STejun Heo kfree(pool); 365929c91e99STejun Heo } 366029c91e99STejun Heo 366129c91e99STejun Heo /** 366229c91e99STejun Heo * put_unbound_pool - put a worker_pool 366329c91e99STejun Heo * @pool: worker_pool to put 366429c91e99STejun Heo * 366524acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3666c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3667c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3668c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3669a892caccSTejun Heo * 3670a892caccSTejun Heo * Should be called with wq_pool_mutex held. 367129c91e99STejun Heo */ 367229c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 367329c91e99STejun Heo { 367460f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 3675e02b9312SValentin Schneider struct list_head cull_list; 367629c91e99STejun Heo struct worker *worker; 367729c91e99STejun Heo 3678e02b9312SValentin Schneider INIT_LIST_HEAD(&cull_list); 3679e02b9312SValentin Schneider 3680a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3681a892caccSTejun Heo 3682a892caccSTejun Heo if (--pool->refcnt) 368329c91e99STejun Heo return; 368429c91e99STejun Heo 368529c91e99STejun Heo /* sanity checks */ 368661d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3687a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 368829c91e99STejun Heo return; 368929c91e99STejun Heo 369029c91e99STejun Heo /* release id and unhash */ 369129c91e99STejun Heo if (pool->id >= 0) 369229c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 369329c91e99STejun Heo hash_del(&pool->hash_node); 369429c91e99STejun Heo 3695c5aa87bbSTejun Heo /* 3696692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3697692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3698692b4825STejun Heo * manager and @pool gets freed with the flag set. 36999ab03be4SValentin Schneider * 37009ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 37019ab03be4SValentin Schneider * only get here with 37029ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 37039ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 37049ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 37059ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 37069ab03be4SValentin Schneider * drops pool->lock 3707c5aa87bbSTejun Heo */ 37089ab03be4SValentin Schneider while (true) { 37099ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 37109ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 3711d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 3712e02b9312SValentin Schneider 3713e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 37149ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 37159ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 3716692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 37179ab03be4SValentin Schneider break; 37189ab03be4SValentin Schneider } 37199ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 3720e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 37219ab03be4SValentin Schneider } 3722692b4825STejun Heo 37231037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 3724e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 372529c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 3726a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 372760f5a4bcSLai Jiangshan 3728e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3729e02b9312SValentin Schneider 3730e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 373160f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 37321258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 373360f5a4bcSLai Jiangshan 373460f5a4bcSLai Jiangshan if (pool->detach_completion) 373560f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 373660f5a4bcSLai Jiangshan 373729c91e99STejun Heo /* shut down the timers */ 373829c91e99STejun Heo del_timer_sync(&pool->idle_timer); 37393f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 374029c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 374129c91e99STejun Heo 374224acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 374325b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 374429c91e99STejun Heo } 374529c91e99STejun Heo 374629c91e99STejun Heo /** 374729c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 374829c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 374929c91e99STejun Heo * 375029c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 375129c91e99STejun Heo * reference count and return it. If there already is a matching 375229c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3753d185af30SYacine Belkadi * create a new one. 3754a892caccSTejun Heo * 3755a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3756d185af30SYacine Belkadi * 3757d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3758d185af30SYacine Belkadi * On failure, %NULL. 375929c91e99STejun Heo */ 376029c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 376129c91e99STejun Heo { 376229c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 376329c91e99STejun Heo struct worker_pool *pool; 3764f3f90ad4STejun Heo int node; 3765e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 376629c91e99STejun Heo 3767a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 376829c91e99STejun Heo 376929c91e99STejun Heo /* do we already have a matching pool? */ 377029c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 377129c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 377229c91e99STejun Heo pool->refcnt++; 37733fb1823cSLai Jiangshan return pool; 377429c91e99STejun Heo } 377529c91e99STejun Heo } 377629c91e99STejun Heo 3777e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3778e2273584SXunlei Pang if (wq_numa_enabled) { 3779e2273584SXunlei Pang for_each_node(node) { 3780e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3781e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3782e2273584SXunlei Pang target_node = node; 3783e2273584SXunlei Pang break; 3784e2273584SXunlei Pang } 3785e2273584SXunlei Pang } 3786e2273584SXunlei Pang } 3787e2273584SXunlei Pang 378829c91e99STejun Heo /* nope, create a new one */ 3789e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 379029c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 379129c91e99STejun Heo goto fail; 379229c91e99STejun Heo 37938864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 379429c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3795e2273584SXunlei Pang pool->node = target_node; 379629c91e99STejun Heo 37972865a8fbSShaohua Li /* 37982865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 37992865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 38002865a8fbSShaohua Li */ 38012865a8fbSShaohua Li pool->attrs->no_numa = false; 38022865a8fbSShaohua Li 380329c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 380429c91e99STejun Heo goto fail; 380529c91e99STejun Heo 380629c91e99STejun Heo /* create and start the initial worker */ 38073347fa09STejun Heo if (wq_online && !create_worker(pool)) 380829c91e99STejun Heo goto fail; 380929c91e99STejun Heo 381029c91e99STejun Heo /* install */ 381129c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 38123fb1823cSLai Jiangshan 381329c91e99STejun Heo return pool; 381429c91e99STejun Heo fail: 381529c91e99STejun Heo if (pool) 381629c91e99STejun Heo put_unbound_pool(pool); 381729c91e99STejun Heo return NULL; 381829c91e99STejun Heo } 381929c91e99STejun Heo 38208864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 38218864b4e5STejun Heo { 38228864b4e5STejun Heo kmem_cache_free(pwq_cache, 38238864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 38248864b4e5STejun Heo } 38258864b4e5STejun Heo 38268864b4e5STejun Heo /* 38278864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 38288864b4e5STejun Heo * and needs to be destroyed. 38298864b4e5STejun Heo */ 38308864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 38318864b4e5STejun Heo { 38328864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 38338864b4e5STejun Heo unbound_release_work); 38348864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 38358864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3836b42b0bddSYang Yingliang bool is_last = false; 38378864b4e5STejun Heo 3838b42b0bddSYang Yingliang /* 3839b42b0bddSYang Yingliang * when @pwq is not linked, it doesn't hold any reference to the 3840b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 3841b42b0bddSYang Yingliang */ 3842b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 38438864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 38448864b4e5STejun Heo return; 38458864b4e5STejun Heo 38463c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 38478864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3848bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 38493c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 3850b42b0bddSYang Yingliang } 38518864b4e5STejun Heo 3852a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 38538864b4e5STejun Heo put_unbound_pool(pool); 3854a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3855a892caccSTejun Heo 385625b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 38578864b4e5STejun Heo 38588864b4e5STejun Heo /* 38598864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3860e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 38618864b4e5STejun Heo */ 3862669de8bdSBart Van Assche if (is_last) { 3863669de8bdSBart Van Assche wq_unregister_lockdep(wq); 386425b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 38656029a918STejun Heo } 3866669de8bdSBart Van Assche } 38678864b4e5STejun Heo 38680fbd95aaSTejun Heo /** 3869699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 38700fbd95aaSTejun Heo * @pwq: target pool_workqueue 38710fbd95aaSTejun Heo * 3872699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3873f97a4a1aSLai Jiangshan * workqueue's saved_max_active and activate inactive work items 3874699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 38750fbd95aaSTejun Heo */ 3876699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 38770fbd95aaSTejun Heo { 3878699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3879699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 38803347fa09STejun Heo unsigned long flags; 3881699ce097STejun Heo 3882699ce097STejun Heo /* for @wq->saved_max_active */ 3883a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3884699ce097STejun Heo 3885699ce097STejun Heo /* fast exit for non-freezable wqs */ 3886699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3887699ce097STejun Heo return; 3888699ce097STejun Heo 38893347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 3890a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 3891699ce097STejun Heo 389274b414eaSLai Jiangshan /* 389374b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 389474b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 389574b414eaSLai Jiangshan * is updated and visible. 389674b414eaSLai Jiangshan */ 389774b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 389801341fbdSYunfeng Ye bool kick = false; 389901341fbdSYunfeng Ye 3900699ce097STejun Heo pwq->max_active = wq->saved_max_active; 39010fbd95aaSTejun Heo 3902f97a4a1aSLai Jiangshan while (!list_empty(&pwq->inactive_works) && 390301341fbdSYunfeng Ye pwq->nr_active < pwq->max_active) { 3904f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 390501341fbdSYunfeng Ye kick = true; 390601341fbdSYunfeng Ye } 3907951a078aSLai Jiangshan 3908951a078aSLai Jiangshan /* 3909951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 391001341fbdSYunfeng Ye * max_active is bumped. In realtime scenarios, always kicking a 391101341fbdSYunfeng Ye * worker will cause interference on the isolated cpu cores, so 391201341fbdSYunfeng Ye * let's kick iff work items were activated. 3913951a078aSLai Jiangshan */ 391401341fbdSYunfeng Ye if (kick) 3915951a078aSLai Jiangshan wake_up_worker(pwq->pool); 3916699ce097STejun Heo } else { 3917699ce097STejun Heo pwq->max_active = 0; 3918699ce097STejun Heo } 3919699ce097STejun Heo 3920a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 39210fbd95aaSTejun Heo } 39220fbd95aaSTejun Heo 392367dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 3924f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 3925f147f29eSTejun Heo struct worker_pool *pool) 3926d2c1d404STejun Heo { 3927d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 3928d2c1d404STejun Heo 3929e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 3930e50aba9aSTejun Heo 3931d2c1d404STejun Heo pwq->pool = pool; 3932d2c1d404STejun Heo pwq->wq = wq; 3933d2c1d404STejun Heo pwq->flush_color = -1; 39348864b4e5STejun Heo pwq->refcnt = 1; 3935f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 39361befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 3937d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 39388864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 3939f147f29eSTejun Heo } 3940d2c1d404STejun Heo 3941f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 39421befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 3943f147f29eSTejun Heo { 3944f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 3945f147f29eSTejun Heo 3946f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 394775ccf595STejun Heo 39481befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 39491befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 39501befcf30STejun Heo return; 39511befcf30STejun Heo 395229b1cb41SLai Jiangshan /* set the matching work_color */ 395375ccf595STejun Heo pwq->work_color = wq->work_color; 3954983ca25eSTejun Heo 3955983ca25eSTejun Heo /* sync max_active to the current setting */ 3956983ca25eSTejun Heo pwq_adjust_max_active(pwq); 3957983ca25eSTejun Heo 3958983ca25eSTejun Heo /* link in @pwq */ 39599e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 3960df2d5ae4STejun Heo } 39616029a918STejun Heo 3962f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 3963f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 3964f147f29eSTejun Heo const struct workqueue_attrs *attrs) 3965f147f29eSTejun Heo { 3966f147f29eSTejun Heo struct worker_pool *pool; 3967f147f29eSTejun Heo struct pool_workqueue *pwq; 3968f147f29eSTejun Heo 3969f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3970f147f29eSTejun Heo 3971f147f29eSTejun Heo pool = get_unbound_pool(attrs); 3972f147f29eSTejun Heo if (!pool) 3973f147f29eSTejun Heo return NULL; 3974f147f29eSTejun Heo 3975e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 3976f147f29eSTejun Heo if (!pwq) { 3977f147f29eSTejun Heo put_unbound_pool(pool); 3978f147f29eSTejun Heo return NULL; 3979f147f29eSTejun Heo } 3980f147f29eSTejun Heo 3981f147f29eSTejun Heo init_pwq(pwq, wq, pool); 3982f147f29eSTejun Heo return pwq; 3983d2c1d404STejun Heo } 3984d2c1d404STejun Heo 39854c16bd32STejun Heo /** 398630186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 3987042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 39884c16bd32STejun Heo * @node: the target NUMA node 39894c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 39904c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 39914c16bd32STejun Heo * 39924c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 39934c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 3994d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 39954c16bd32STejun Heo * 39964c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 39974c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 39984c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 39994c16bd32STejun Heo * @attrs->cpumask. 40004c16bd32STejun Heo * 40014c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 40024c16bd32STejun Heo * stable. 4003d185af30SYacine Belkadi * 4004d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 4005d185af30SYacine Belkadi * %false if equal. 40064c16bd32STejun Heo */ 40074c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 40084c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 40094c16bd32STejun Heo { 4010d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 40114c16bd32STejun Heo goto use_dfl; 40124c16bd32STejun Heo 40134c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 40144c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 40154c16bd32STejun Heo if (cpu_going_down >= 0) 40164c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 40174c16bd32STejun Heo 40184c16bd32STejun Heo if (cpumask_empty(cpumask)) 40194c16bd32STejun Heo goto use_dfl; 40204c16bd32STejun Heo 40214c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 40224c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 40231ad0f0a7SMichael Bringmann 40241ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 40251ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 40261ad0f0a7SMichael Bringmann "possible intersect\n"); 40271ad0f0a7SMichael Bringmann return false; 40281ad0f0a7SMichael Bringmann } 40291ad0f0a7SMichael Bringmann 40304c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 40314c16bd32STejun Heo 40324c16bd32STejun Heo use_dfl: 40334c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 40344c16bd32STejun Heo return false; 40354c16bd32STejun Heo } 40364c16bd32STejun Heo 40371befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 40381befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 40391befcf30STejun Heo int node, 40401befcf30STejun Heo struct pool_workqueue *pwq) 40411befcf30STejun Heo { 40421befcf30STejun Heo struct pool_workqueue *old_pwq; 40431befcf30STejun Heo 40445b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 40451befcf30STejun Heo lockdep_assert_held(&wq->mutex); 40461befcf30STejun Heo 40471befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 40481befcf30STejun Heo link_pwq(pwq); 40491befcf30STejun Heo 40501befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 40511befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 40521befcf30STejun Heo return old_pwq; 40531befcf30STejun Heo } 40541befcf30STejun Heo 40552d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 40562d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 40572d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 40582d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4059042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 40602d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 40612d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 40622d5f0764SLai Jiangshan }; 40632d5f0764SLai Jiangshan 40642d5f0764SLai Jiangshan /* free the resources after success or abort */ 40652d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 40662d5f0764SLai Jiangshan { 40672d5f0764SLai Jiangshan if (ctx) { 40682d5f0764SLai Jiangshan int node; 40692d5f0764SLai Jiangshan 40702d5f0764SLai Jiangshan for_each_node(node) 40712d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 40722d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 40732d5f0764SLai Jiangshan 40742d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 40752d5f0764SLai Jiangshan 40762d5f0764SLai Jiangshan kfree(ctx); 40772d5f0764SLai Jiangshan } 40782d5f0764SLai Jiangshan } 40792d5f0764SLai Jiangshan 40802d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 40812d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 40822d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 408399c621efSLai Jiangshan const struct workqueue_attrs *attrs, 408499c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 40852d5f0764SLai Jiangshan { 40862d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 40872d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 40882d5f0764SLai Jiangshan int node; 40892d5f0764SLai Jiangshan 40902d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 40912d5f0764SLai Jiangshan 4092acafe7e3SKees Cook ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL); 40932d5f0764SLai Jiangshan 4094be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 4095be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 40962d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 40972d5f0764SLai Jiangshan goto out_free; 40982d5f0764SLai Jiangshan 4099042f7df1SLai Jiangshan /* 410099c621efSLai Jiangshan * Calculate the attrs of the default pwq with unbound_cpumask 410199c621efSLai Jiangshan * which is wq_unbound_cpumask or to set to wq_unbound_cpumask. 4102042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 4103042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 4104042f7df1SLai Jiangshan */ 41052d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 410699c621efSLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, unbound_cpumask); 4107042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 410899c621efSLai Jiangshan cpumask_copy(new_attrs->cpumask, unbound_cpumask); 41092d5f0764SLai Jiangshan 41102d5f0764SLai Jiangshan /* 41112d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 41122d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 41132d5f0764SLai Jiangshan * pools. 41142d5f0764SLai Jiangshan */ 41152d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 41162d5f0764SLai Jiangshan 41172d5f0764SLai Jiangshan /* 41182d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 41192d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 41202d5f0764SLai Jiangshan * it even if we don't use it immediately. 41212d5f0764SLai Jiangshan */ 41222d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 41232d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 41242d5f0764SLai Jiangshan goto out_free; 41252d5f0764SLai Jiangshan 41262d5f0764SLai Jiangshan for_each_node(node) { 4127042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 41282d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 41292d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 41302d5f0764SLai Jiangshan goto out_free; 41312d5f0764SLai Jiangshan } else { 41322d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 41332d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 41342d5f0764SLai Jiangshan } 41352d5f0764SLai Jiangshan } 41362d5f0764SLai Jiangshan 4137042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4138042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4139042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 41402d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4141042f7df1SLai Jiangshan 41422d5f0764SLai Jiangshan ctx->wq = wq; 41432d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 41442d5f0764SLai Jiangshan return ctx; 41452d5f0764SLai Jiangshan 41462d5f0764SLai Jiangshan out_free: 41472d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 41482d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 41492d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 41502d5f0764SLai Jiangshan return NULL; 41512d5f0764SLai Jiangshan } 41522d5f0764SLai Jiangshan 41532d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 41542d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 41552d5f0764SLai Jiangshan { 41562d5f0764SLai Jiangshan int node; 41572d5f0764SLai Jiangshan 41582d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 41592d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 41602d5f0764SLai Jiangshan 41612d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 41622d5f0764SLai Jiangshan 41632d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 41642d5f0764SLai Jiangshan for_each_node(node) 41652d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 41662d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 41672d5f0764SLai Jiangshan 41682d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 41692d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 41702d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 41712d5f0764SLai Jiangshan 41722d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 41732d5f0764SLai Jiangshan } 41742d5f0764SLai Jiangshan 4175a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4176a0111cf6SLai Jiangshan { 4177a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4178ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4179a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4180a0111cf6SLai Jiangshan } 4181a0111cf6SLai Jiangshan 4182a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4183a0111cf6SLai Jiangshan { 4184a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4185ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4186a0111cf6SLai Jiangshan } 4187a0111cf6SLai Jiangshan 4188a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4189a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4190a0111cf6SLai Jiangshan { 4191a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4192a0111cf6SLai Jiangshan 4193a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4194a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4195a0111cf6SLai Jiangshan return -EINVAL; 4196a0111cf6SLai Jiangshan 4197a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 41980a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 41990a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4200a0111cf6SLai Jiangshan return -EINVAL; 4201a0111cf6SLai Jiangshan 42020a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 42030a94efb5STejun Heo } 42040a94efb5STejun Heo 420599c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 42066201171eSwanghaibin if (!ctx) 42076201171eSwanghaibin return -ENOMEM; 4208a0111cf6SLai Jiangshan 4209a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4210a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4211a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4212a0111cf6SLai Jiangshan 42136201171eSwanghaibin return 0; 4214a0111cf6SLai Jiangshan } 4215a0111cf6SLai Jiangshan 42169e8cd2f5STejun Heo /** 42179e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 42189e8cd2f5STejun Heo * @wq: the target workqueue 42199e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 42209e8cd2f5STejun Heo * 42214c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 42224c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 42234c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 42244c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 42254c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 42264c16bd32STejun Heo * back-to-back will stay on its current pwq. 42279e8cd2f5STejun Heo * 4228d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4229d185af30SYacine Belkadi * 4230ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4231509b3204SDaniel Jordan * 4232d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 42339e8cd2f5STejun Heo */ 4234513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 42359e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 42369e8cd2f5STejun Heo { 4237a0111cf6SLai Jiangshan int ret; 42389e8cd2f5STejun Heo 4239509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4240509b3204SDaniel Jordan 4241509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4242a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4243509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 42442d5f0764SLai Jiangshan 42452d5f0764SLai Jiangshan return ret; 42469e8cd2f5STejun Heo } 42479e8cd2f5STejun Heo 42484c16bd32STejun Heo /** 42494c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 42504c16bd32STejun Heo * @wq: the target workqueue 42514c16bd32STejun Heo * @cpu: the CPU coming up or going down 42524c16bd32STejun Heo * @online: whether @cpu is coming up or going down 42534c16bd32STejun Heo * 42544c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 42554c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 42564c16bd32STejun Heo * @wq accordingly. 42574c16bd32STejun Heo * 42584c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 42594c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 42604c16bd32STejun Heo * correct. 42614c16bd32STejun Heo * 42624c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 42634c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 42644c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 42654c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 42664c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 42674c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 42684c16bd32STejun Heo * CPU_DOWN_PREPARE. 42694c16bd32STejun Heo */ 42704c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 42714c16bd32STejun Heo bool online) 42724c16bd32STejun Heo { 42734c16bd32STejun Heo int node = cpu_to_node(cpu); 42744c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 42754c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 42764c16bd32STejun Heo struct workqueue_attrs *target_attrs; 42774c16bd32STejun Heo cpumask_t *cpumask; 42784c16bd32STejun Heo 42794c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 42804c16bd32STejun Heo 4281f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 4282f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 42834c16bd32STejun Heo return; 42844c16bd32STejun Heo 42854c16bd32STejun Heo /* 42864c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 42874c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 42884c16bd32STejun Heo * CPU hotplug exclusion. 42894c16bd32STejun Heo */ 42904c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 42914c16bd32STejun Heo cpumask = target_attrs->cpumask; 42924c16bd32STejun Heo 42934c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 42944c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 42954c16bd32STejun Heo 42964c16bd32STejun Heo /* 42974c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 4298042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 4299042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 4300042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 43014c16bd32STejun Heo */ 4302042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 43034c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4304f7142ed4SLai Jiangshan return; 43054c16bd32STejun Heo } else { 43064c16bd32STejun Heo goto use_dfl_pwq; 43074c16bd32STejun Heo } 43084c16bd32STejun Heo 43094c16bd32STejun Heo /* create a new pwq */ 43104c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 43114c16bd32STejun Heo if (!pwq) { 43122d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 43134c16bd32STejun Heo wq->name); 431477f300b1SDaeseok Youn goto use_dfl_pwq; 43154c16bd32STejun Heo } 43164c16bd32STejun Heo 4317f7142ed4SLai Jiangshan /* Install the new pwq. */ 43184c16bd32STejun Heo mutex_lock(&wq->mutex); 43194c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 43204c16bd32STejun Heo goto out_unlock; 43214c16bd32STejun Heo 43224c16bd32STejun Heo use_dfl_pwq: 4323f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4324a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 43254c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4326a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 43274c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 43284c16bd32STejun Heo out_unlock: 43294c16bd32STejun Heo mutex_unlock(&wq->mutex); 43304c16bd32STejun Heo put_pwq_unlocked(old_pwq); 43314c16bd32STejun Heo } 43324c16bd32STejun Heo 433330cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 43341da177e4SLinus Torvalds { 433549e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 43368a2b7538STejun Heo int cpu, ret; 4337e1d8aa9fSFrederic Weisbecker 433830cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4339420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 4340420c0ddbSTejun Heo if (!wq->cpu_pwqs) 434130cdf249STejun Heo return -ENOMEM; 434230cdf249STejun Heo 434330cdf249STejun Heo for_each_possible_cpu(cpu) { 43447fb98ea7STejun Heo struct pool_workqueue *pwq = 43457fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 43467a62c2c8STejun Heo struct worker_pool *cpu_pools = 4347f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 434830cdf249STejun Heo 4349f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 4350f147f29eSTejun Heo 4351f147f29eSTejun Heo mutex_lock(&wq->mutex); 43521befcf30STejun Heo link_pwq(pwq); 4353f147f29eSTejun Heo mutex_unlock(&wq->mutex); 435430cdf249STejun Heo } 435530cdf249STejun Heo return 0; 4356509b3204SDaniel Jordan } 4357509b3204SDaniel Jordan 4358ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4359509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 43608a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 43618a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 43628a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 43638a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 43648a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 43659e8cd2f5STejun Heo } else { 4366509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 43679e8cd2f5STejun Heo } 4368ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4369509b3204SDaniel Jordan 4370509b3204SDaniel Jordan return ret; 43710f900049STejun Heo } 43720f900049STejun Heo 4373f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4374f3421797STejun Heo const char *name) 4375b71ab8c2STejun Heo { 4376f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 4377f3421797STejun Heo 4378f3421797STejun Heo if (max_active < 1 || max_active > lim) 4379044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4380f3421797STejun Heo max_active, name, 1, lim); 4381b71ab8c2STejun Heo 4382f3421797STejun Heo return clamp_val(max_active, 1, lim); 4383b71ab8c2STejun Heo } 4384b71ab8c2STejun Heo 4385983c7515STejun Heo /* 4386983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4387983c7515STejun Heo * to guarantee forward progress. 4388983c7515STejun Heo */ 4389983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4390983c7515STejun Heo { 4391983c7515STejun Heo struct worker *rescuer; 4392b92b36eaSDan Carpenter int ret; 4393983c7515STejun Heo 4394983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4395983c7515STejun Heo return 0; 4396983c7515STejun Heo 4397983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 43984c0736a7SPetr Mladek if (!rescuer) { 43994c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 44004c0736a7SPetr Mladek wq->name); 4401983c7515STejun Heo return -ENOMEM; 44024c0736a7SPetr Mladek } 4403983c7515STejun Heo 4404983c7515STejun Heo rescuer->rescue_wq = wq; 4405983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4406f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4407b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 44084c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 44094c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4410983c7515STejun Heo kfree(rescuer); 4411b92b36eaSDan Carpenter return ret; 4412983c7515STejun Heo } 4413983c7515STejun Heo 4414983c7515STejun Heo wq->rescuer = rescuer; 4415983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4416983c7515STejun Heo wake_up_process(rescuer->task); 4417983c7515STejun Heo 4418983c7515STejun Heo return 0; 4419983c7515STejun Heo } 4420983c7515STejun Heo 4421a2775bbcSMathieu Malaterre __printf(1, 4) 4422669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 442397e37d7bSTejun Heo unsigned int flags, 4424669de8bdSBart Van Assche int max_active, ...) 44253af24433SOleg Nesterov { 4426df2d5ae4STejun Heo size_t tbl_size = 0; 4427ecf6881fSTejun Heo va_list args; 44283af24433SOleg Nesterov struct workqueue_struct *wq; 442949e3cf44STejun Heo struct pool_workqueue *pwq; 4430b196be89STejun Heo 44315c0338c6STejun Heo /* 44325c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 44335c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 44345c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 44355c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 44365c0338c6STejun Heo * on NUMA. 44375c0338c6STejun Heo */ 44385c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 44395c0338c6STejun Heo flags |= __WQ_ORDERED; 44405c0338c6STejun Heo 4441cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4442cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4443cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4444cee22a15SViresh Kumar 4445ecf6881fSTejun Heo /* allocate wq and format name */ 4446df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 4447ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 4448df2d5ae4STejun Heo 4449df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 4450b196be89STejun Heo if (!wq) 4451d2c1d404STejun Heo return NULL; 4452b196be89STejun Heo 44536029a918STejun Heo if (flags & WQ_UNBOUND) { 4454be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 44556029a918STejun Heo if (!wq->unbound_attrs) 44566029a918STejun Heo goto err_free_wq; 44576029a918STejun Heo } 44586029a918STejun Heo 4459669de8bdSBart Van Assche va_start(args, max_active); 4460ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4461b196be89STejun Heo va_end(args); 44623af24433SOleg Nesterov 4463d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4464b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 44653af24433SOleg Nesterov 4466b196be89STejun Heo /* init wq */ 446797e37d7bSTejun Heo wq->flags = flags; 4468a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 44693c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4470112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 447130cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 447273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 447373f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4474493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 44753af24433SOleg Nesterov 4476669de8bdSBart Van Assche wq_init_lockdep(wq); 4477cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 44783af24433SOleg Nesterov 447930cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 448082efcab3SBart Van Assche goto err_unreg_lockdep; 44811537663fSTejun Heo 448240c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4483d2c1d404STejun Heo goto err_destroy; 4484e22bee78STejun Heo 4485226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4486226223abSTejun Heo goto err_destroy; 4487226223abSTejun Heo 44886af8bf3dSOleg Nesterov /* 448968e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 449068e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 449168e13a67SLai Jiangshan * list. 44926af8bf3dSOleg Nesterov */ 449368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4494a0a1a5fdSTejun Heo 4495a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 449649e3cf44STejun Heo for_each_pwq(pwq, wq) 4497699ce097STejun Heo pwq_adjust_max_active(pwq); 4498a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4499a0a1a5fdSTejun Heo 4500e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4501a0a1a5fdSTejun Heo 450268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 45033af24433SOleg Nesterov 45043af24433SOleg Nesterov return wq; 4505d2c1d404STejun Heo 450682efcab3SBart Van Assche err_unreg_lockdep: 4507009bb421SBart Van Assche wq_unregister_lockdep(wq); 4508009bb421SBart Van Assche wq_free_lockdep(wq); 450982efcab3SBart Van Assche err_free_wq: 45106029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 45114690c4abSTejun Heo kfree(wq); 4512d2c1d404STejun Heo return NULL; 4513d2c1d404STejun Heo err_destroy: 4514d2c1d404STejun Heo destroy_workqueue(wq); 45154690c4abSTejun Heo return NULL; 45161da177e4SLinus Torvalds } 4517669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 45181da177e4SLinus Torvalds 4519c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4520c29eb853STejun Heo { 4521c29eb853STejun Heo int i; 4522c29eb853STejun Heo 4523c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4524c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4525c29eb853STejun Heo return true; 4526c29eb853STejun Heo 4527c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4528c29eb853STejun Heo return true; 4529f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 4530c29eb853STejun Heo return true; 4531c29eb853STejun Heo 4532c29eb853STejun Heo return false; 4533c29eb853STejun Heo } 4534c29eb853STejun Heo 45353af24433SOleg Nesterov /** 45363af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 45373af24433SOleg Nesterov * @wq: target workqueue 45383af24433SOleg Nesterov * 45393af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 45403af24433SOleg Nesterov */ 45413af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 45423af24433SOleg Nesterov { 454349e3cf44STejun Heo struct pool_workqueue *pwq; 45444c16bd32STejun Heo int node; 45453af24433SOleg Nesterov 4546def98c84STejun Heo /* 4547def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4548def98c84STejun Heo * lead to sysfs name conflicts. 4549def98c84STejun Heo */ 4550def98c84STejun Heo workqueue_sysfs_unregister(wq); 4551def98c84STejun Heo 455233e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 455333e3f0a3SRichard Clark mutex_lock(&wq->mutex); 455433e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 455533e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 455633e3f0a3SRichard Clark 45579c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 45589c5a2ba7STejun Heo drain_workqueue(wq); 4559c8efcc25STejun Heo 4560def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4561def98c84STejun Heo if (wq->rescuer) { 4562def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4563def98c84STejun Heo 4564def98c84STejun Heo /* this prevents new queueing */ 4565a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4566def98c84STejun Heo wq->rescuer = NULL; 4567a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4568def98c84STejun Heo 4569def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4570def98c84STejun Heo kthread_stop(rescuer->task); 45718efe1223STejun Heo kfree(rescuer); 4572def98c84STejun Heo } 4573def98c84STejun Heo 4574c29eb853STejun Heo /* 4575c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4576c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4577c29eb853STejun Heo */ 4578c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4579b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 458049e3cf44STejun Heo for_each_pwq(pwq, wq) { 4581a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4582c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 45831d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4584e66b39afSTejun Heo __func__, wq->name); 4585c29eb853STejun Heo show_pwq(pwq); 4586a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4587b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4588c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 458955df0933SImran Khan show_one_workqueue(wq); 45906183c009STejun Heo return; 459176af4d93STejun Heo } 4592a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 459376af4d93STejun Heo } 4594b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 45956183c009STejun Heo 4596a0a1a5fdSTejun Heo /* 4597a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4598a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4599a0a1a5fdSTejun Heo */ 4600e2dca7adSTejun Heo list_del_rcu(&wq->list); 460168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 46023af24433SOleg Nesterov 46038864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4604669de8bdSBart Van Assche wq_unregister_lockdep(wq); 460529c91e99STejun Heo /* 46068864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4607e2dca7adSTejun Heo * schedule RCU free. 460829c91e99STejun Heo */ 460925b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 46108864b4e5STejun Heo } else { 46118864b4e5STejun Heo /* 46128864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 46134c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 46144c16bd32STejun Heo * @wq will be freed when the last pwq is released. 46158864b4e5STejun Heo */ 46164c16bd32STejun Heo for_each_node(node) { 46174c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 46184c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 46194c16bd32STejun Heo put_pwq_unlocked(pwq); 46204c16bd32STejun Heo } 46214c16bd32STejun Heo 46224c16bd32STejun Heo /* 46234c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 46244c16bd32STejun Heo * put. Don't access it afterwards. 46254c16bd32STejun Heo */ 46264c16bd32STejun Heo pwq = wq->dfl_pwq; 46274c16bd32STejun Heo wq->dfl_pwq = NULL; 4628dce90d47STejun Heo put_pwq_unlocked(pwq); 462929c91e99STejun Heo } 46303af24433SOleg Nesterov } 46313af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 46323af24433SOleg Nesterov 4633dcd989cbSTejun Heo /** 4634dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4635dcd989cbSTejun Heo * @wq: target workqueue 4636dcd989cbSTejun Heo * @max_active: new max_active value. 4637dcd989cbSTejun Heo * 4638dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4639dcd989cbSTejun Heo * 4640dcd989cbSTejun Heo * CONTEXT: 4641dcd989cbSTejun Heo * Don't call from IRQ context. 4642dcd989cbSTejun Heo */ 4643dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4644dcd989cbSTejun Heo { 464549e3cf44STejun Heo struct pool_workqueue *pwq; 4646dcd989cbSTejun Heo 46478719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 46480a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 46498719dceaSTejun Heo return; 46508719dceaSTejun Heo 4651f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4652dcd989cbSTejun Heo 4653a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4654dcd989cbSTejun Heo 46550a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4656dcd989cbSTejun Heo wq->saved_max_active = max_active; 4657dcd989cbSTejun Heo 4658699ce097STejun Heo for_each_pwq(pwq, wq) 4659699ce097STejun Heo pwq_adjust_max_active(pwq); 4660dcd989cbSTejun Heo 4661a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4662dcd989cbSTejun Heo } 4663dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4664dcd989cbSTejun Heo 4665dcd989cbSTejun Heo /** 466627d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 466727d4ee03SLukas Wunner * 466827d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 466927d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 467027d4ee03SLukas Wunner * 467127d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 467227d4ee03SLukas Wunner */ 467327d4ee03SLukas Wunner struct work_struct *current_work(void) 467427d4ee03SLukas Wunner { 467527d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 467627d4ee03SLukas Wunner 467727d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 467827d4ee03SLukas Wunner } 467927d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 468027d4ee03SLukas Wunner 468127d4ee03SLukas Wunner /** 4682e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4683e6267616STejun Heo * 4684e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4685e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4686d185af30SYacine Belkadi * 4687d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4688e6267616STejun Heo */ 4689e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4690e6267616STejun Heo { 4691e6267616STejun Heo struct worker *worker = current_wq_worker(); 4692e6267616STejun Heo 46936a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4694e6267616STejun Heo } 4695e6267616STejun Heo 4696e6267616STejun Heo /** 4697dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4698dcd989cbSTejun Heo * @cpu: CPU in question 4699dcd989cbSTejun Heo * @wq: target workqueue 4700dcd989cbSTejun Heo * 4701dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4702dcd989cbSTejun Heo * no synchronization around this function and the test result is 4703dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4704dcd989cbSTejun Heo * 4705d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4706d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4707d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4708d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4709d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4710d3251859STejun Heo * 4711d185af30SYacine Belkadi * Return: 4712dcd989cbSTejun Heo * %true if congested, %false otherwise. 4713dcd989cbSTejun Heo */ 4714d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4715dcd989cbSTejun Heo { 47167fb98ea7STejun Heo struct pool_workqueue *pwq; 471776af4d93STejun Heo bool ret; 471876af4d93STejun Heo 471924acfb71SThomas Gleixner rcu_read_lock(); 472024acfb71SThomas Gleixner preempt_disable(); 47217fb98ea7STejun Heo 4722d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4723d3251859STejun Heo cpu = smp_processor_id(); 4724d3251859STejun Heo 47257fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 47267fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 47277fb98ea7STejun Heo else 4728df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4729dcd989cbSTejun Heo 4730f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 473124acfb71SThomas Gleixner preempt_enable(); 473224acfb71SThomas Gleixner rcu_read_unlock(); 473376af4d93STejun Heo 473476af4d93STejun Heo return ret; 4735dcd989cbSTejun Heo } 4736dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4737dcd989cbSTejun Heo 4738dcd989cbSTejun Heo /** 4739dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4740dcd989cbSTejun Heo * @work: the work to be tested 4741dcd989cbSTejun Heo * 4742dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4743dcd989cbSTejun Heo * synchronization around this function and the test result is 4744dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4745dcd989cbSTejun Heo * 4746d185af30SYacine Belkadi * Return: 4747dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4748dcd989cbSTejun Heo */ 4749dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4750dcd989cbSTejun Heo { 4751fa1b54e6STejun Heo struct worker_pool *pool; 4752dcd989cbSTejun Heo unsigned long flags; 4753dcd989cbSTejun Heo unsigned int ret = 0; 4754dcd989cbSTejun Heo 4755dcd989cbSTejun Heo if (work_pending(work)) 4756dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4757038366c5SLai Jiangshan 475824acfb71SThomas Gleixner rcu_read_lock(); 4759fa1b54e6STejun Heo pool = get_work_pool(work); 4760038366c5SLai Jiangshan if (pool) { 4761a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 4762c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4763dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4764a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 4765038366c5SLai Jiangshan } 476624acfb71SThomas Gleixner rcu_read_unlock(); 4767dcd989cbSTejun Heo 4768dcd989cbSTejun Heo return ret; 4769dcd989cbSTejun Heo } 4770dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4771dcd989cbSTejun Heo 47723d1cb205STejun Heo /** 47733d1cb205STejun Heo * set_worker_desc - set description for the current work item 47743d1cb205STejun Heo * @fmt: printf-style format string 47753d1cb205STejun Heo * @...: arguments for the format string 47763d1cb205STejun Heo * 47773d1cb205STejun Heo * This function can be called by a running work function to describe what 47783d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 47793d1cb205STejun Heo * information will be printed out together to help debugging. The 47803d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 47813d1cb205STejun Heo */ 47823d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 47833d1cb205STejun Heo { 47843d1cb205STejun Heo struct worker *worker = current_wq_worker(); 47853d1cb205STejun Heo va_list args; 47863d1cb205STejun Heo 47873d1cb205STejun Heo if (worker) { 47883d1cb205STejun Heo va_start(args, fmt); 47893d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 47903d1cb205STejun Heo va_end(args); 47913d1cb205STejun Heo } 47923d1cb205STejun Heo } 47935c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 47943d1cb205STejun Heo 47953d1cb205STejun Heo /** 47963d1cb205STejun Heo * print_worker_info - print out worker information and description 47973d1cb205STejun Heo * @log_lvl: the log level to use when printing 47983d1cb205STejun Heo * @task: target task 47993d1cb205STejun Heo * 48003d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 48013d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 48023d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 48033d1cb205STejun Heo * 48043d1cb205STejun Heo * This function can be safely called on any task as long as the 48053d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 48063d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 48073d1cb205STejun Heo */ 48083d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 48093d1cb205STejun Heo { 48103d1cb205STejun Heo work_func_t *fn = NULL; 48113d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 48123d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 48133d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 48143d1cb205STejun Heo struct workqueue_struct *wq = NULL; 48153d1cb205STejun Heo struct worker *worker; 48163d1cb205STejun Heo 48173d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 48183d1cb205STejun Heo return; 48193d1cb205STejun Heo 48203d1cb205STejun Heo /* 48213d1cb205STejun Heo * This function is called without any synchronization and @task 48223d1cb205STejun Heo * could be in any state. Be careful with dereferences. 48233d1cb205STejun Heo */ 4824e700591aSPetr Mladek worker = kthread_probe_data(task); 48253d1cb205STejun Heo 48263d1cb205STejun Heo /* 48278bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 48288bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 48293d1cb205STejun Heo */ 4830fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 4831fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 4832fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 4833fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 4834fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 48353d1cb205STejun Heo 48363d1cb205STejun Heo if (fn || name[0] || desc[0]) { 4837d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 48388bf89593STejun Heo if (strcmp(name, desc)) 48393d1cb205STejun Heo pr_cont(" (%s)", desc); 48403d1cb205STejun Heo pr_cont("\n"); 48413d1cb205STejun Heo } 48423d1cb205STejun Heo } 48433d1cb205STejun Heo 48443494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 48453494fc30STejun Heo { 48463494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 48473494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 48483494fc30STejun Heo pr_cont(" node=%d", pool->node); 48493494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 48503494fc30STejun Heo } 48513494fc30STejun Heo 4852c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 4853c76feb0dSPaul E. McKenney bool comma; 4854c76feb0dSPaul E. McKenney work_func_t func; 4855c76feb0dSPaul E. McKenney long ctr; 4856c76feb0dSPaul E. McKenney }; 4857c76feb0dSPaul E. McKenney 4858c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 4859c76feb0dSPaul E. McKenney { 4860c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 4861c76feb0dSPaul E. McKenney goto out_record; 4862c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 4863c76feb0dSPaul E. McKenney pcwsp->ctr++; 4864c76feb0dSPaul E. McKenney return; 4865c76feb0dSPaul E. McKenney } 4866c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 4867c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 4868c76feb0dSPaul E. McKenney else 4869c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 4870c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 4871c76feb0dSPaul E. McKenney out_record: 4872c76feb0dSPaul E. McKenney if ((long)func == -1L) 4873c76feb0dSPaul E. McKenney return; 4874c76feb0dSPaul E. McKenney pcwsp->comma = comma; 4875c76feb0dSPaul E. McKenney pcwsp->func = func; 4876c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 4877c76feb0dSPaul E. McKenney } 4878c76feb0dSPaul E. McKenney 4879c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 48803494fc30STejun Heo { 48813494fc30STejun Heo if (work->func == wq_barrier_func) { 48823494fc30STejun Heo struct wq_barrier *barr; 48833494fc30STejun Heo 48843494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 48853494fc30STejun Heo 4886c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 48873494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 48883494fc30STejun Heo task_pid_nr(barr->task)); 48893494fc30STejun Heo } else { 4890c76feb0dSPaul E. McKenney if (!comma) 4891c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 4892c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 48933494fc30STejun Heo } 48943494fc30STejun Heo } 48953494fc30STejun Heo 48963494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 48973494fc30STejun Heo { 4898c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 48993494fc30STejun Heo struct worker_pool *pool = pwq->pool; 49003494fc30STejun Heo struct work_struct *work; 49013494fc30STejun Heo struct worker *worker; 49023494fc30STejun Heo bool has_in_flight = false, has_pending = false; 49033494fc30STejun Heo int bkt; 49043494fc30STejun Heo 49053494fc30STejun Heo pr_info(" pwq %d:", pool->id); 49063494fc30STejun Heo pr_cont_pool_info(pool); 49073494fc30STejun Heo 4908e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 4909e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 49103494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 49113494fc30STejun Heo 49123494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 49133494fc30STejun Heo if (worker->current_pwq == pwq) { 49143494fc30STejun Heo has_in_flight = true; 49153494fc30STejun Heo break; 49163494fc30STejun Heo } 49173494fc30STejun Heo } 49183494fc30STejun Heo if (has_in_flight) { 49193494fc30STejun Heo bool comma = false; 49203494fc30STejun Heo 49213494fc30STejun Heo pr_info(" in-flight:"); 49223494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 49233494fc30STejun Heo if (worker->current_pwq != pwq) 49243494fc30STejun Heo continue; 49253494fc30STejun Heo 4926d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 49273494fc30STejun Heo task_pid_nr(worker->task), 492830ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 49293494fc30STejun Heo worker->current_func); 49303494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 4931c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 4932c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 49333494fc30STejun Heo comma = true; 49343494fc30STejun Heo } 49353494fc30STejun Heo pr_cont("\n"); 49363494fc30STejun Heo } 49373494fc30STejun Heo 49383494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 49393494fc30STejun Heo if (get_work_pwq(work) == pwq) { 49403494fc30STejun Heo has_pending = true; 49413494fc30STejun Heo break; 49423494fc30STejun Heo } 49433494fc30STejun Heo } 49443494fc30STejun Heo if (has_pending) { 49453494fc30STejun Heo bool comma = false; 49463494fc30STejun Heo 49473494fc30STejun Heo pr_info(" pending:"); 49483494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 49493494fc30STejun Heo if (get_work_pwq(work) != pwq) 49503494fc30STejun Heo continue; 49513494fc30STejun Heo 4952c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 49533494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 49543494fc30STejun Heo } 4955c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 49563494fc30STejun Heo pr_cont("\n"); 49573494fc30STejun Heo } 49583494fc30STejun Heo 4959f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 49603494fc30STejun Heo bool comma = false; 49613494fc30STejun Heo 4962f97a4a1aSLai Jiangshan pr_info(" inactive:"); 4963f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 4964c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 49653494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 49663494fc30STejun Heo } 4967c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 49683494fc30STejun Heo pr_cont("\n"); 49693494fc30STejun Heo } 49703494fc30STejun Heo } 49713494fc30STejun Heo 49723494fc30STejun Heo /** 497355df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 497455df0933SImran Khan * @wq: workqueue whose state will be printed 49753494fc30STejun Heo */ 497655df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 49773494fc30STejun Heo { 49783494fc30STejun Heo struct pool_workqueue *pwq; 49793494fc30STejun Heo bool idle = true; 498055df0933SImran Khan unsigned long flags; 49813494fc30STejun Heo 49823494fc30STejun Heo for_each_pwq(pwq, wq) { 4983f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 49843494fc30STejun Heo idle = false; 49853494fc30STejun Heo break; 49863494fc30STejun Heo } 49873494fc30STejun Heo } 498855df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 498955df0933SImran Khan return; 49903494fc30STejun Heo 49913494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 49923494fc30STejun Heo 49933494fc30STejun Heo for_each_pwq(pwq, wq) { 4994a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 499557116ce1SJohan Hovold if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 499657116ce1SJohan Hovold /* 499757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 499857116ce1SJohan Hovold * drivers that queue work while holding locks 499957116ce1SJohan Hovold * also taken in their write paths. 500057116ce1SJohan Hovold */ 500157116ce1SJohan Hovold printk_deferred_enter(); 50023494fc30STejun Heo show_pwq(pwq); 500357116ce1SJohan Hovold printk_deferred_exit(); 500457116ce1SJohan Hovold } 5005a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 500662635ea8SSergey Senozhatsky /* 500762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 500855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 500962635ea8SSergey Senozhatsky * hard lockup. 501062635ea8SSergey Senozhatsky */ 501162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 50123494fc30STejun Heo } 501355df0933SImran Khan 50143494fc30STejun Heo } 50153494fc30STejun Heo 501655df0933SImran Khan /** 501755df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 501855df0933SImran Khan * @pool: worker pool whose state will be printed 501955df0933SImran Khan */ 502055df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 502155df0933SImran Khan { 50223494fc30STejun Heo struct worker *worker; 50233494fc30STejun Heo bool first = true; 502455df0933SImran Khan unsigned long flags; 5025335a42ebSPetr Mladek unsigned long hung = 0; 50263494fc30STejun Heo 5027a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 50283494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 50293494fc30STejun Heo goto next_pool; 5030335a42ebSPetr Mladek 5031335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5032335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5033335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5034335a42ebSPetr Mladek 503557116ce1SJohan Hovold /* 503657116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 503757116ce1SJohan Hovold * queue work while holding locks also taken in their write 503857116ce1SJohan Hovold * paths. 503957116ce1SJohan Hovold */ 504057116ce1SJohan Hovold printk_deferred_enter(); 50413494fc30STejun Heo pr_info("pool %d:", pool->id); 50423494fc30STejun Heo pr_cont_pool_info(pool); 5043335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 50443494fc30STejun Heo if (pool->manager) 50453494fc30STejun Heo pr_cont(" manager: %d", 50463494fc30STejun Heo task_pid_nr(pool->manager->task)); 50473494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 50483494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 50493494fc30STejun Heo task_pid_nr(worker->task)); 50503494fc30STejun Heo first = false; 50513494fc30STejun Heo } 50523494fc30STejun Heo pr_cont("\n"); 505357116ce1SJohan Hovold printk_deferred_exit(); 50543494fc30STejun Heo next_pool: 5055a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 505662635ea8SSergey Senozhatsky /* 505762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 505855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 505962635ea8SSergey Senozhatsky * hard lockup. 506062635ea8SSergey Senozhatsky */ 506162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 506255df0933SImran Khan 50633494fc30STejun Heo } 50643494fc30STejun Heo 506555df0933SImran Khan /** 506655df0933SImran Khan * show_all_workqueues - dump workqueue state 506755df0933SImran Khan * 5068*704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 506955df0933SImran Khan */ 507055df0933SImran Khan void show_all_workqueues(void) 507155df0933SImran Khan { 507255df0933SImran Khan struct workqueue_struct *wq; 507355df0933SImran Khan struct worker_pool *pool; 507455df0933SImran Khan int pi; 507555df0933SImran Khan 507655df0933SImran Khan rcu_read_lock(); 507755df0933SImran Khan 507855df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 507955df0933SImran Khan 508055df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 508155df0933SImran Khan show_one_workqueue(wq); 508255df0933SImran Khan 508355df0933SImran Khan for_each_pool(pool, pi) 508455df0933SImran Khan show_one_worker_pool(pool); 508555df0933SImran Khan 508624acfb71SThomas Gleixner rcu_read_unlock(); 50873494fc30STejun Heo } 50883494fc30STejun Heo 5089*704bc669SJungseung Lee /** 5090*704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5091*704bc669SJungseung Lee * 5092*704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5093*704bc669SJungseung Lee * still busy. 5094*704bc669SJungseung Lee */ 5095*704bc669SJungseung Lee void show_freezable_workqueues(void) 5096*704bc669SJungseung Lee { 5097*704bc669SJungseung Lee struct workqueue_struct *wq; 5098*704bc669SJungseung Lee 5099*704bc669SJungseung Lee rcu_read_lock(); 5100*704bc669SJungseung Lee 5101*704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5102*704bc669SJungseung Lee 5103*704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5104*704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5105*704bc669SJungseung Lee continue; 5106*704bc669SJungseung Lee show_one_workqueue(wq); 5107*704bc669SJungseung Lee } 5108*704bc669SJungseung Lee 5109*704bc669SJungseung Lee rcu_read_unlock(); 5110*704bc669SJungseung Lee } 5111*704bc669SJungseung Lee 51126b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 51136b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 51146b59808bSTejun Heo { 51156b59808bSTejun Heo int off; 51166b59808bSTejun Heo 51176b59808bSTejun Heo /* always show the actual comm */ 51186b59808bSTejun Heo off = strscpy(buf, task->comm, size); 51196b59808bSTejun Heo if (off < 0) 51206b59808bSTejun Heo return; 51216b59808bSTejun Heo 5122197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 51236b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 51246b59808bSTejun Heo 5125197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5126197f6accSTejun Heo struct worker *worker = kthread_data(task); 5127197f6accSTejun Heo struct worker_pool *pool = worker->pool; 51286b59808bSTejun Heo 51296b59808bSTejun Heo if (pool) { 5130a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 51316b59808bSTejun Heo /* 5132197f6accSTejun Heo * ->desc tracks information (wq name or 5133197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5134197f6accSTejun Heo * current, prepend '+', otherwise '-'. 51356b59808bSTejun Heo */ 51366b59808bSTejun Heo if (worker->desc[0] != '\0') { 51376b59808bSTejun Heo if (worker->current_work) 51386b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 51396b59808bSTejun Heo worker->desc); 51406b59808bSTejun Heo else 51416b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 51426b59808bSTejun Heo worker->desc); 51436b59808bSTejun Heo } 5144a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 51456b59808bSTejun Heo } 5146197f6accSTejun Heo } 51476b59808bSTejun Heo 51486b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 51496b59808bSTejun Heo } 51506b59808bSTejun Heo 515166448bc2SMathieu Malaterre #ifdef CONFIG_SMP 515266448bc2SMathieu Malaterre 5153db7bccf4STejun Heo /* 5154db7bccf4STejun Heo * CPU hotplug. 5155db7bccf4STejun Heo * 5156e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5157112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5158706026c2STejun Heo * pool which make migrating pending and scheduled works very 5159e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 516094cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5161e22bee78STejun Heo * blocked draining impractical. 5162e22bee78STejun Heo * 516324647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5164628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5165628c78e7STejun Heo * cpu comes back online. 5166db7bccf4STejun Heo */ 5167db7bccf4STejun Heo 5168e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5169db7bccf4STejun Heo { 51704ce62e9eSTejun Heo struct worker_pool *pool; 5171db7bccf4STejun Heo struct worker *worker; 5172db7bccf4STejun Heo 5173f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 51741258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5175a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5176e22bee78STejun Heo 5177f2d5a0eeSTejun Heo /* 517892f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 517994cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 518011b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5181b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5182b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5183b4ac9384SLai Jiangshan * is on the same cpu. 5184f2d5a0eeSTejun Heo */ 5185da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5186403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5187db7bccf4STejun Heo 518824647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5189f2d5a0eeSTejun Heo 5190e22bee78STejun Heo /* 5191989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5192989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5193989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5194989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5195989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5196eb283428SLai Jiangshan * are served by workers tied to the pool. 5197e22bee78STejun Heo */ 5198bc35f7efSLai Jiangshan pool->nr_running = 0; 5199eb283428SLai Jiangshan 5200eb283428SLai Jiangshan /* 5201eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5202eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5203eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5204eb283428SLai Jiangshan */ 5205eb283428SLai Jiangshan wake_up_worker(pool); 5206989442d7SLai Jiangshan 5207a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5208989442d7SLai Jiangshan 5209793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5210793777bcSValentin Schneider unbind_worker(worker); 5211989442d7SLai Jiangshan 5212989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5213eb283428SLai Jiangshan } 5214db7bccf4STejun Heo } 5215db7bccf4STejun Heo 5216bd7c089eSTejun Heo /** 5217bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5218bd7c089eSTejun Heo * @pool: pool of interest 5219bd7c089eSTejun Heo * 5220a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5221bd7c089eSTejun Heo */ 5222bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5223bd7c089eSTejun Heo { 5224a9ab775bSTejun Heo struct worker *worker; 5225bd7c089eSTejun Heo 52261258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5227bd7c089eSTejun Heo 5228bd7c089eSTejun Heo /* 5229a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5230a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5231402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5232a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5233a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5234bd7c089eSTejun Heo */ 5235c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5236c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5237c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 5238c63a2e52SValentin Schneider pool->attrs->cpumask) < 0); 5239c63a2e52SValentin Schneider } 5240a9ab775bSTejun Heo 5241a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5242f7c17d26SWanpeng Li 52433de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5244a9ab775bSTejun Heo 5245da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5246a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5247a9ab775bSTejun Heo 5248a9ab775bSTejun Heo /* 5249a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5250a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5251a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5252a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5253a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5254a9ab775bSTejun Heo * concurrency management. Note that when or whether 5255a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5256a9ab775bSTejun Heo * 5257c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5258a9ab775bSTejun Heo * tested without holding any lock in 52596d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5260a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5261a9ab775bSTejun Heo * management operations. 5262bd7c089eSTejun Heo */ 5263a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5264a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5265a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5266c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5267bd7c089eSTejun Heo } 5268a9ab775bSTejun Heo 5269a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5270bd7c089eSTejun Heo } 5271bd7c089eSTejun Heo 52727dbc725eSTejun Heo /** 52737dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 52747dbc725eSTejun Heo * @pool: unbound pool of interest 52757dbc725eSTejun Heo * @cpu: the CPU which is coming up 52767dbc725eSTejun Heo * 52777dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 52787dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 52797dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 52807dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 52817dbc725eSTejun Heo */ 52827dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 52837dbc725eSTejun Heo { 52847dbc725eSTejun Heo static cpumask_t cpumask; 52857dbc725eSTejun Heo struct worker *worker; 52867dbc725eSTejun Heo 52871258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 52887dbc725eSTejun Heo 52897dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 52907dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 52917dbc725eSTejun Heo return; 52927dbc725eSTejun Heo 52937dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 52947dbc725eSTejun Heo 52957dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5296da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5297d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 52987dbc725eSTejun Heo } 52997dbc725eSTejun Heo 53007ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 53011da177e4SLinus Torvalds { 53024ce62e9eSTejun Heo struct worker_pool *pool; 53031da177e4SLinus Torvalds 5304f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 53053ce63377STejun Heo if (pool->nr_workers) 53063ce63377STejun Heo continue; 5307051e1850SLai Jiangshan if (!create_worker(pool)) 53087ee681b2SThomas Gleixner return -ENOMEM; 53093af24433SOleg Nesterov } 53107ee681b2SThomas Gleixner return 0; 53117ee681b2SThomas Gleixner } 53121da177e4SLinus Torvalds 53137ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 53147ee681b2SThomas Gleixner { 53157ee681b2SThomas Gleixner struct worker_pool *pool; 53167ee681b2SThomas Gleixner struct workqueue_struct *wq; 53177ee681b2SThomas Gleixner int pi; 53187ee681b2SThomas Gleixner 531968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 53207dbc725eSTejun Heo 53217dbc725eSTejun Heo for_each_pool(pool, pi) { 53221258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 532394cf58bbSTejun Heo 5324f05b558dSLai Jiangshan if (pool->cpu == cpu) 532594cf58bbSTejun Heo rebind_workers(pool); 5326f05b558dSLai Jiangshan else if (pool->cpu < 0) 53277dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 532894cf58bbSTejun Heo 53291258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 533094cf58bbSTejun Heo } 53317dbc725eSTejun Heo 53324c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 53334c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 53344c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 53354c16bd32STejun Heo 533668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 53377ee681b2SThomas Gleixner return 0; 533865758202STejun Heo } 533965758202STejun Heo 53407ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 534165758202STejun Heo { 53424c16bd32STejun Heo struct workqueue_struct *wq; 53438db25e78STejun Heo 53444c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5345e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5346e8b3f8dbSLai Jiangshan return -1; 5347e8b3f8dbSLai Jiangshan 5348e8b3f8dbSLai Jiangshan unbind_workers(cpu); 53494c16bd32STejun Heo 53504c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 53514c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 53524c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 53534c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 53544c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 53554c16bd32STejun Heo 53567ee681b2SThomas Gleixner return 0; 535765758202STejun Heo } 535865758202STejun Heo 53592d3854a3SRusty Russell struct work_for_cpu { 5360ed48ece2STejun Heo struct work_struct work; 53612d3854a3SRusty Russell long (*fn)(void *); 53622d3854a3SRusty Russell void *arg; 53632d3854a3SRusty Russell long ret; 53642d3854a3SRusty Russell }; 53652d3854a3SRusty Russell 5366ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 53672d3854a3SRusty Russell { 5368ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5369ed48ece2STejun Heo 53702d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 53712d3854a3SRusty Russell } 53722d3854a3SRusty Russell 53732d3854a3SRusty Russell /** 537422aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 53752d3854a3SRusty Russell * @cpu: the cpu to run on 53762d3854a3SRusty Russell * @fn: the function to run 53772d3854a3SRusty Russell * @arg: the function arg 53782d3854a3SRusty Russell * 537931ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 53806b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5381d185af30SYacine Belkadi * 5382d185af30SYacine Belkadi * Return: The value @fn returns. 53832d3854a3SRusty Russell */ 5384d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 53852d3854a3SRusty Russell { 5386ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 53872d3854a3SRusty Russell 5388ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5389ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 539012997d1aSBjorn Helgaas flush_work(&wfc.work); 5391440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 53922d3854a3SRusty Russell return wfc.ret; 53932d3854a3SRusty Russell } 53942d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 53950e8d6a93SThomas Gleixner 53960e8d6a93SThomas Gleixner /** 53970e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 53980e8d6a93SThomas Gleixner * @cpu: the cpu to run on 53990e8d6a93SThomas Gleixner * @fn: the function to run 54000e8d6a93SThomas Gleixner * @arg: the function argument 54010e8d6a93SThomas Gleixner * 54020e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 54030e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 54040e8d6a93SThomas Gleixner * 54050e8d6a93SThomas Gleixner * Return: The value @fn returns. 54060e8d6a93SThomas Gleixner */ 54070e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 54080e8d6a93SThomas Gleixner { 54090e8d6a93SThomas Gleixner long ret = -ENODEV; 54100e8d6a93SThomas Gleixner 5411ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 54120e8d6a93SThomas Gleixner if (cpu_online(cpu)) 54130e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 5414ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 54150e8d6a93SThomas Gleixner return ret; 54160e8d6a93SThomas Gleixner } 54170e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 54182d3854a3SRusty Russell #endif /* CONFIG_SMP */ 54192d3854a3SRusty Russell 5420a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5421e7577c50SRusty Russell 5422a0a1a5fdSTejun Heo /** 5423a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5424a0a1a5fdSTejun Heo * 542558a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5426f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5427706026c2STejun Heo * pool->worklist. 5428a0a1a5fdSTejun Heo * 5429a0a1a5fdSTejun Heo * CONTEXT: 5430a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5431a0a1a5fdSTejun Heo */ 5432a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5433a0a1a5fdSTejun Heo { 543424b8a847STejun Heo struct workqueue_struct *wq; 543524b8a847STejun Heo struct pool_workqueue *pwq; 5436a0a1a5fdSTejun Heo 543768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5438a0a1a5fdSTejun Heo 54396183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5440a0a1a5fdSTejun Heo workqueue_freezing = true; 5441a0a1a5fdSTejun Heo 544224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5443a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5444699ce097STejun Heo for_each_pwq(pwq, wq) 5445699ce097STejun Heo pwq_adjust_max_active(pwq); 5446a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5447a1056305STejun Heo } 54485bcab335STejun Heo 544968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5450a0a1a5fdSTejun Heo } 5451a0a1a5fdSTejun Heo 5452a0a1a5fdSTejun Heo /** 545358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5454a0a1a5fdSTejun Heo * 5455a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5456a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5457a0a1a5fdSTejun Heo * 5458a0a1a5fdSTejun Heo * CONTEXT: 545968e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5460a0a1a5fdSTejun Heo * 5461d185af30SYacine Belkadi * Return: 546258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 546358a69cb4STejun Heo * is complete. 5464a0a1a5fdSTejun Heo */ 5465a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5466a0a1a5fdSTejun Heo { 5467a0a1a5fdSTejun Heo bool busy = false; 546824b8a847STejun Heo struct workqueue_struct *wq; 546924b8a847STejun Heo struct pool_workqueue *pwq; 5470a0a1a5fdSTejun Heo 547168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5472a0a1a5fdSTejun Heo 54736183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5474a0a1a5fdSTejun Heo 547524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 547624b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 547724b8a847STejun Heo continue; 5478a0a1a5fdSTejun Heo /* 5479a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5480a0a1a5fdSTejun Heo * to peek without lock. 5481a0a1a5fdSTejun Heo */ 548224acfb71SThomas Gleixner rcu_read_lock(); 548324b8a847STejun Heo for_each_pwq(pwq, wq) { 54846183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5485112202d9STejun Heo if (pwq->nr_active) { 5486a0a1a5fdSTejun Heo busy = true; 548724acfb71SThomas Gleixner rcu_read_unlock(); 5488a0a1a5fdSTejun Heo goto out_unlock; 5489a0a1a5fdSTejun Heo } 5490a0a1a5fdSTejun Heo } 549124acfb71SThomas Gleixner rcu_read_unlock(); 5492a0a1a5fdSTejun Heo } 5493a0a1a5fdSTejun Heo out_unlock: 549468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5495a0a1a5fdSTejun Heo return busy; 5496a0a1a5fdSTejun Heo } 5497a0a1a5fdSTejun Heo 5498a0a1a5fdSTejun Heo /** 5499a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5500a0a1a5fdSTejun Heo * 5501a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5502706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5503a0a1a5fdSTejun Heo * 5504a0a1a5fdSTejun Heo * CONTEXT: 5505a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5506a0a1a5fdSTejun Heo */ 5507a0a1a5fdSTejun Heo void thaw_workqueues(void) 5508a0a1a5fdSTejun Heo { 550924b8a847STejun Heo struct workqueue_struct *wq; 551024b8a847STejun Heo struct pool_workqueue *pwq; 5511a0a1a5fdSTejun Heo 551268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5513a0a1a5fdSTejun Heo 5514a0a1a5fdSTejun Heo if (!workqueue_freezing) 5515a0a1a5fdSTejun Heo goto out_unlock; 5516a0a1a5fdSTejun Heo 551774b414eaSLai Jiangshan workqueue_freezing = false; 551824b8a847STejun Heo 551924b8a847STejun Heo /* restore max_active and repopulate worklist */ 552024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5521a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5522699ce097STejun Heo for_each_pwq(pwq, wq) 5523699ce097STejun Heo pwq_adjust_max_active(pwq); 5524a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 552524b8a847STejun Heo } 552624b8a847STejun Heo 5527a0a1a5fdSTejun Heo out_unlock: 552868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5529a0a1a5fdSTejun Heo } 5530a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5531a0a1a5fdSTejun Heo 553299c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 5533042f7df1SLai Jiangshan { 5534042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5535042f7df1SLai Jiangshan int ret = 0; 5536042f7df1SLai Jiangshan struct workqueue_struct *wq; 5537042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5538042f7df1SLai Jiangshan 5539042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5540042f7df1SLai Jiangshan 5541042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5542042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5543042f7df1SLai Jiangshan continue; 5544042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5545042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5546042f7df1SLai Jiangshan continue; 5547042f7df1SLai Jiangshan 554899c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 5549042f7df1SLai Jiangshan if (!ctx) { 5550042f7df1SLai Jiangshan ret = -ENOMEM; 5551042f7df1SLai Jiangshan break; 5552042f7df1SLai Jiangshan } 5553042f7df1SLai Jiangshan 5554042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5555042f7df1SLai Jiangshan } 5556042f7df1SLai Jiangshan 5557042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5558042f7df1SLai Jiangshan if (!ret) 5559042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5560042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5561042f7df1SLai Jiangshan } 5562042f7df1SLai Jiangshan 556399c621efSLai Jiangshan if (!ret) { 556499c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 556599c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 556699c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 556799c621efSLai Jiangshan } 5568042f7df1SLai Jiangshan return ret; 5569042f7df1SLai Jiangshan } 5570042f7df1SLai Jiangshan 5571042f7df1SLai Jiangshan /** 5572042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5573042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5574042f7df1SLai Jiangshan * 5575042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5576042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5577042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5578042f7df1SLai Jiangshan * 557967dc8325SCai Huoqing * Return: 0 - Success 5580042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5581042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5582042f7df1SLai Jiangshan */ 5583042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5584042f7df1SLai Jiangshan { 5585042f7df1SLai Jiangshan int ret = -EINVAL; 5586042f7df1SLai Jiangshan 5587c98a9805STal Shorer /* 5588c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5589c98a9805STal Shorer * If the user wishes to include them, we allow that. 5590c98a9805STal Shorer */ 5591042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5592042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5593a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5594d25302e4SMenglong Dong if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 5595d25302e4SMenglong Dong ret = 0; 5596d25302e4SMenglong Dong goto out_unlock; 5597d25302e4SMenglong Dong } 5598d25302e4SMenglong Dong 559999c621efSLai Jiangshan ret = workqueue_apply_unbound_cpumask(cpumask); 5600042f7df1SLai Jiangshan 5601d25302e4SMenglong Dong out_unlock: 5602a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5603042f7df1SLai Jiangshan } 5604042f7df1SLai Jiangshan 5605042f7df1SLai Jiangshan return ret; 5606042f7df1SLai Jiangshan } 5607042f7df1SLai Jiangshan 56086ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 56096ba94429SFrederic Weisbecker /* 56106ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 56116ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 56126ba94429SFrederic Weisbecker * following attributes. 56136ba94429SFrederic Weisbecker * 56146ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 56156ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 56166ba94429SFrederic Weisbecker * 56176ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 56186ba94429SFrederic Weisbecker * 56199a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 56206ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 56216ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 56229a19b463SWang Long * numa RW bool : whether enable NUMA affinity 56236ba94429SFrederic Weisbecker */ 56246ba94429SFrederic Weisbecker struct wq_device { 56256ba94429SFrederic Weisbecker struct workqueue_struct *wq; 56266ba94429SFrederic Weisbecker struct device dev; 56276ba94429SFrederic Weisbecker }; 56286ba94429SFrederic Weisbecker 56296ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 56306ba94429SFrederic Weisbecker { 56316ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 56326ba94429SFrederic Weisbecker 56336ba94429SFrederic Weisbecker return wq_dev->wq; 56346ba94429SFrederic Weisbecker } 56356ba94429SFrederic Weisbecker 56366ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 56376ba94429SFrederic Weisbecker char *buf) 56386ba94429SFrederic Weisbecker { 56396ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56406ba94429SFrederic Weisbecker 56416ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 56426ba94429SFrederic Weisbecker } 56436ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 56446ba94429SFrederic Weisbecker 56456ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 56466ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 56476ba94429SFrederic Weisbecker { 56486ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56496ba94429SFrederic Weisbecker 56506ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 56516ba94429SFrederic Weisbecker } 56526ba94429SFrederic Weisbecker 56536ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 56546ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 56556ba94429SFrederic Weisbecker size_t count) 56566ba94429SFrederic Weisbecker { 56576ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56586ba94429SFrederic Weisbecker int val; 56596ba94429SFrederic Weisbecker 56606ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 56616ba94429SFrederic Weisbecker return -EINVAL; 56626ba94429SFrederic Weisbecker 56636ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 56646ba94429SFrederic Weisbecker return count; 56656ba94429SFrederic Weisbecker } 56666ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 56676ba94429SFrederic Weisbecker 56686ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 56696ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 56706ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 56716ba94429SFrederic Weisbecker NULL, 56726ba94429SFrederic Weisbecker }; 56736ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 56746ba94429SFrederic Weisbecker 56756ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 56766ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 56776ba94429SFrederic Weisbecker { 56786ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 56796ba94429SFrederic Weisbecker const char *delim = ""; 56806ba94429SFrederic Weisbecker int node, written = 0; 56816ba94429SFrederic Weisbecker 5682ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 568324acfb71SThomas Gleixner rcu_read_lock(); 56846ba94429SFrederic Weisbecker for_each_node(node) { 56856ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 56866ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 56876ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 56886ba94429SFrederic Weisbecker delim = " "; 56896ba94429SFrederic Weisbecker } 56906ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 569124acfb71SThomas Gleixner rcu_read_unlock(); 5692ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 56936ba94429SFrederic Weisbecker 56946ba94429SFrederic Weisbecker return written; 56956ba94429SFrederic Weisbecker } 56966ba94429SFrederic Weisbecker 56976ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 56986ba94429SFrederic Weisbecker char *buf) 56996ba94429SFrederic Weisbecker { 57006ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57016ba94429SFrederic Weisbecker int written; 57026ba94429SFrederic Weisbecker 57036ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 57046ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 57056ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 57066ba94429SFrederic Weisbecker 57076ba94429SFrederic Weisbecker return written; 57086ba94429SFrederic Weisbecker } 57096ba94429SFrederic Weisbecker 57106ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 57116ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 57126ba94429SFrederic Weisbecker { 57136ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 57146ba94429SFrederic Weisbecker 5715899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5716899a94feSLai Jiangshan 5717be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 57186ba94429SFrederic Weisbecker if (!attrs) 57196ba94429SFrederic Weisbecker return NULL; 57206ba94429SFrederic Weisbecker 57216ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 57226ba94429SFrederic Weisbecker return attrs; 57236ba94429SFrederic Weisbecker } 57246ba94429SFrederic Weisbecker 57256ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 57266ba94429SFrederic Weisbecker const char *buf, size_t count) 57276ba94429SFrederic Weisbecker { 57286ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57296ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5730d4d3e257SLai Jiangshan int ret = -ENOMEM; 5731d4d3e257SLai Jiangshan 5732d4d3e257SLai Jiangshan apply_wqattrs_lock(); 57336ba94429SFrederic Weisbecker 57346ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 57356ba94429SFrederic Weisbecker if (!attrs) 5736d4d3e257SLai Jiangshan goto out_unlock; 57376ba94429SFrederic Weisbecker 57386ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 57396ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5740d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 57416ba94429SFrederic Weisbecker else 57426ba94429SFrederic Weisbecker ret = -EINVAL; 57436ba94429SFrederic Weisbecker 5744d4d3e257SLai Jiangshan out_unlock: 5745d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 57466ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 57476ba94429SFrederic Weisbecker return ret ?: count; 57486ba94429SFrederic Weisbecker } 57496ba94429SFrederic Weisbecker 57506ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 57516ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 57526ba94429SFrederic Weisbecker { 57536ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57546ba94429SFrederic Weisbecker int written; 57556ba94429SFrederic Weisbecker 57566ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 57576ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 57586ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 57596ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 57606ba94429SFrederic Weisbecker return written; 57616ba94429SFrederic Weisbecker } 57626ba94429SFrederic Weisbecker 57636ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 57646ba94429SFrederic Weisbecker struct device_attribute *attr, 57656ba94429SFrederic Weisbecker const char *buf, size_t count) 57666ba94429SFrederic Weisbecker { 57676ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57686ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5769d4d3e257SLai Jiangshan int ret = -ENOMEM; 5770d4d3e257SLai Jiangshan 5771d4d3e257SLai Jiangshan apply_wqattrs_lock(); 57726ba94429SFrederic Weisbecker 57736ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 57746ba94429SFrederic Weisbecker if (!attrs) 5775d4d3e257SLai Jiangshan goto out_unlock; 57766ba94429SFrederic Weisbecker 57776ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 57786ba94429SFrederic Weisbecker if (!ret) 5779d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 57806ba94429SFrederic Weisbecker 5781d4d3e257SLai Jiangshan out_unlock: 5782d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 57836ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 57846ba94429SFrederic Weisbecker return ret ?: count; 57856ba94429SFrederic Weisbecker } 57866ba94429SFrederic Weisbecker 57876ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 57886ba94429SFrederic Weisbecker char *buf) 57896ba94429SFrederic Weisbecker { 57906ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57916ba94429SFrederic Weisbecker int written; 57926ba94429SFrederic Weisbecker 57936ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 57946ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 57956ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 57966ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 57976ba94429SFrederic Weisbecker 57986ba94429SFrederic Weisbecker return written; 57996ba94429SFrederic Weisbecker } 58006ba94429SFrederic Weisbecker 58016ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 58026ba94429SFrederic Weisbecker const char *buf, size_t count) 58036ba94429SFrederic Weisbecker { 58046ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58056ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5806d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5807d4d3e257SLai Jiangshan 5808d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58096ba94429SFrederic Weisbecker 58106ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58116ba94429SFrederic Weisbecker if (!attrs) 5812d4d3e257SLai Jiangshan goto out_unlock; 58136ba94429SFrederic Weisbecker 58146ba94429SFrederic Weisbecker ret = -EINVAL; 58156ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 58166ba94429SFrederic Weisbecker attrs->no_numa = !v; 5817d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 58186ba94429SFrederic Weisbecker } 58196ba94429SFrederic Weisbecker 5820d4d3e257SLai Jiangshan out_unlock: 5821d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 58226ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 58236ba94429SFrederic Weisbecker return ret ?: count; 58246ba94429SFrederic Weisbecker } 58256ba94429SFrederic Weisbecker 58266ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 58276ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 58286ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 58296ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 58306ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 58316ba94429SFrederic Weisbecker __ATTR_NULL, 58326ba94429SFrederic Weisbecker }; 58336ba94429SFrederic Weisbecker 58346ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 58356ba94429SFrederic Weisbecker .name = "workqueue", 58366ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 58376ba94429SFrederic Weisbecker }; 58386ba94429SFrederic Weisbecker 5839b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5840b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5841b05a7928SFrederic Weisbecker { 5842b05a7928SFrederic Weisbecker int written; 5843b05a7928SFrederic Weisbecker 5844042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5845b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5846b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5847042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5848b05a7928SFrederic Weisbecker 5849b05a7928SFrederic Weisbecker return written; 5850b05a7928SFrederic Weisbecker } 5851b05a7928SFrederic Weisbecker 5852042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5853042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5854042f7df1SLai Jiangshan { 5855042f7df1SLai Jiangshan cpumask_var_t cpumask; 5856042f7df1SLai Jiangshan int ret; 5857042f7df1SLai Jiangshan 5858042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5859042f7df1SLai Jiangshan return -ENOMEM; 5860042f7df1SLai Jiangshan 5861042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5862042f7df1SLai Jiangshan if (!ret) 5863042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5864042f7df1SLai Jiangshan 5865042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5866042f7df1SLai Jiangshan return ret ? ret : count; 5867042f7df1SLai Jiangshan } 5868042f7df1SLai Jiangshan 5869b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5870042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5871042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5872b05a7928SFrederic Weisbecker 58736ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 58746ba94429SFrederic Weisbecker { 5875b05a7928SFrederic Weisbecker int err; 5876b05a7928SFrederic Weisbecker 5877b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5878b05a7928SFrederic Weisbecker if (err) 5879b05a7928SFrederic Weisbecker return err; 5880b05a7928SFrederic Weisbecker 5881b05a7928SFrederic Weisbecker return device_create_file(wq_subsys.dev_root, &wq_sysfs_cpumask_attr); 58826ba94429SFrederic Weisbecker } 58836ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 58846ba94429SFrederic Weisbecker 58856ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 58866ba94429SFrederic Weisbecker { 58876ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 58886ba94429SFrederic Weisbecker 58896ba94429SFrederic Weisbecker kfree(wq_dev); 58906ba94429SFrederic Weisbecker } 58916ba94429SFrederic Weisbecker 58926ba94429SFrederic Weisbecker /** 58936ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 58946ba94429SFrederic Weisbecker * @wq: the workqueue to register 58956ba94429SFrederic Weisbecker * 58966ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 58976ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 58986ba94429SFrederic Weisbecker * which is the preferred method. 58996ba94429SFrederic Weisbecker * 59006ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 59016ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 59026ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 59036ba94429SFrederic Weisbecker * attributes. 59046ba94429SFrederic Weisbecker * 59056ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 59066ba94429SFrederic Weisbecker */ 59076ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 59086ba94429SFrederic Weisbecker { 59096ba94429SFrederic Weisbecker struct wq_device *wq_dev; 59106ba94429SFrederic Weisbecker int ret; 59116ba94429SFrederic Weisbecker 59126ba94429SFrederic Weisbecker /* 5913402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 59146ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 59156ba94429SFrederic Weisbecker * workqueues. 59166ba94429SFrederic Weisbecker */ 59170a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 59186ba94429SFrederic Weisbecker return -EINVAL; 59196ba94429SFrederic Weisbecker 59206ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 59216ba94429SFrederic Weisbecker if (!wq_dev) 59226ba94429SFrederic Weisbecker return -ENOMEM; 59236ba94429SFrederic Weisbecker 59246ba94429SFrederic Weisbecker wq_dev->wq = wq; 59256ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 59266ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 592723217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 59286ba94429SFrederic Weisbecker 59296ba94429SFrederic Weisbecker /* 59306ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 59316ba94429SFrederic Weisbecker * everything is ready. 59326ba94429SFrederic Weisbecker */ 59336ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 59346ba94429SFrederic Weisbecker 59356ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 59366ba94429SFrederic Weisbecker if (ret) { 5937537f4146SArvind Yadav put_device(&wq_dev->dev); 59386ba94429SFrederic Weisbecker wq->wq_dev = NULL; 59396ba94429SFrederic Weisbecker return ret; 59406ba94429SFrederic Weisbecker } 59416ba94429SFrederic Weisbecker 59426ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 59436ba94429SFrederic Weisbecker struct device_attribute *attr; 59446ba94429SFrederic Weisbecker 59456ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 59466ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 59476ba94429SFrederic Weisbecker if (ret) { 59486ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 59496ba94429SFrederic Weisbecker wq->wq_dev = NULL; 59506ba94429SFrederic Weisbecker return ret; 59516ba94429SFrederic Weisbecker } 59526ba94429SFrederic Weisbecker } 59536ba94429SFrederic Weisbecker } 59546ba94429SFrederic Weisbecker 59556ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 59566ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 59576ba94429SFrederic Weisbecker return 0; 59586ba94429SFrederic Weisbecker } 59596ba94429SFrederic Weisbecker 59606ba94429SFrederic Weisbecker /** 59616ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 59626ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 59636ba94429SFrederic Weisbecker * 59646ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 59656ba94429SFrederic Weisbecker */ 59666ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 59676ba94429SFrederic Weisbecker { 59686ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 59696ba94429SFrederic Weisbecker 59706ba94429SFrederic Weisbecker if (!wq->wq_dev) 59716ba94429SFrederic Weisbecker return; 59726ba94429SFrederic Weisbecker 59736ba94429SFrederic Weisbecker wq->wq_dev = NULL; 59746ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 59756ba94429SFrederic Weisbecker } 59766ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 59776ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 59786ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 59796ba94429SFrederic Weisbecker 598082607adcSTejun Heo /* 598182607adcSTejun Heo * Workqueue watchdog. 598282607adcSTejun Heo * 598382607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 598482607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 598582607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 598682607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 598782607adcSTejun Heo * largely opaque. 598882607adcSTejun Heo * 598982607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 599082607adcSTejun Heo * state if some pools failed to make forward progress for a while where 599182607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 599282607adcSTejun Heo * 599382607adcSTejun Heo * This mechanism is controlled through the kernel parameter 599482607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 599582607adcSTejun Heo * corresponding sysfs parameter file. 599682607adcSTejun Heo */ 599782607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 599882607adcSTejun Heo 599982607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 60005cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 600182607adcSTejun Heo 600282607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 600382607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 600482607adcSTejun Heo 6005cd2440d6SPetr Mladek /* 6006cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6007cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6008cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6009cd2440d6SPetr Mladek * in all other situations. 6010cd2440d6SPetr Mladek */ 6011cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6012cd2440d6SPetr Mladek { 6013cd2440d6SPetr Mladek struct worker *worker; 6014cd2440d6SPetr Mladek unsigned long flags; 6015cd2440d6SPetr Mladek int bkt; 6016cd2440d6SPetr Mladek 6017cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6018cd2440d6SPetr Mladek 6019cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6020cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6021cd2440d6SPetr Mladek /* 6022cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6023cd2440d6SPetr Mladek * drivers that queue work while holding locks 6024cd2440d6SPetr Mladek * also taken in their write paths. 6025cd2440d6SPetr Mladek */ 6026cd2440d6SPetr Mladek printk_deferred_enter(); 6027cd2440d6SPetr Mladek 6028cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6029cd2440d6SPetr Mladek sched_show_task(worker->task); 6030cd2440d6SPetr Mladek 6031cd2440d6SPetr Mladek printk_deferred_exit(); 6032cd2440d6SPetr Mladek } 6033cd2440d6SPetr Mladek } 6034cd2440d6SPetr Mladek 6035cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6036cd2440d6SPetr Mladek } 6037cd2440d6SPetr Mladek 6038cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6039cd2440d6SPetr Mladek { 6040cd2440d6SPetr Mladek struct worker_pool *pool; 6041cd2440d6SPetr Mladek int pi; 6042cd2440d6SPetr Mladek 6043cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6044cd2440d6SPetr Mladek 6045cd2440d6SPetr Mladek rcu_read_lock(); 6046cd2440d6SPetr Mladek 6047cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6048cd2440d6SPetr Mladek if (pool->cpu_stall) 6049cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6050cd2440d6SPetr Mladek 6051cd2440d6SPetr Mladek } 6052cd2440d6SPetr Mladek 6053cd2440d6SPetr Mladek rcu_read_unlock(); 6054cd2440d6SPetr Mladek } 6055cd2440d6SPetr Mladek 605682607adcSTejun Heo static void wq_watchdog_reset_touched(void) 605782607adcSTejun Heo { 605882607adcSTejun Heo int cpu; 605982607adcSTejun Heo 606082607adcSTejun Heo wq_watchdog_touched = jiffies; 606182607adcSTejun Heo for_each_possible_cpu(cpu) 606282607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 606382607adcSTejun Heo } 606482607adcSTejun Heo 60655cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 606682607adcSTejun Heo { 606782607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 606882607adcSTejun Heo bool lockup_detected = false; 6069cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6070940d71c6SSergey Senozhatsky unsigned long now = jiffies; 607182607adcSTejun Heo struct worker_pool *pool; 607282607adcSTejun Heo int pi; 607382607adcSTejun Heo 607482607adcSTejun Heo if (!thresh) 607582607adcSTejun Heo return; 607682607adcSTejun Heo 607782607adcSTejun Heo rcu_read_lock(); 607882607adcSTejun Heo 607982607adcSTejun Heo for_each_pool(pool, pi) { 608082607adcSTejun Heo unsigned long pool_ts, touched, ts; 608182607adcSTejun Heo 6082cd2440d6SPetr Mladek pool->cpu_stall = false; 608382607adcSTejun Heo if (list_empty(&pool->worklist)) 608482607adcSTejun Heo continue; 608582607adcSTejun Heo 6086940d71c6SSergey Senozhatsky /* 6087940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6088940d71c6SSergey Senozhatsky * the watchdog like a stall. 6089940d71c6SSergey Senozhatsky */ 6090940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6091940d71c6SSergey Senozhatsky 609282607adcSTejun Heo /* get the latest of pool and touched timestamps */ 609389e28ce6SWang Qing if (pool->cpu >= 0) 609489e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 609589e28ce6SWang Qing else 609682607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 609789e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 609882607adcSTejun Heo 609982607adcSTejun Heo if (time_after(pool_ts, touched)) 610082607adcSTejun Heo ts = pool_ts; 610182607adcSTejun Heo else 610282607adcSTejun Heo ts = touched; 610382607adcSTejun Heo 610482607adcSTejun Heo /* did we stall? */ 6105940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 610682607adcSTejun Heo lockup_detected = true; 6107cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6108cd2440d6SPetr Mladek pool->cpu_stall = true; 6109cd2440d6SPetr Mladek cpu_pool_stall = true; 6110cd2440d6SPetr Mladek } 611182607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 611282607adcSTejun Heo pr_cont_pool_info(pool); 611382607adcSTejun Heo pr_cont(" stuck for %us!\n", 6114940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 611582607adcSTejun Heo } 6116cd2440d6SPetr Mladek 6117cd2440d6SPetr Mladek 611882607adcSTejun Heo } 611982607adcSTejun Heo 612082607adcSTejun Heo rcu_read_unlock(); 612182607adcSTejun Heo 612282607adcSTejun Heo if (lockup_detected) 612355df0933SImran Khan show_all_workqueues(); 612482607adcSTejun Heo 6125cd2440d6SPetr Mladek if (cpu_pool_stall) 6126cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6127cd2440d6SPetr Mladek 612882607adcSTejun Heo wq_watchdog_reset_touched(); 612982607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 613082607adcSTejun Heo } 613182607adcSTejun Heo 6132cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 613382607adcSTejun Heo { 613482607adcSTejun Heo if (cpu >= 0) 613582607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 613689e28ce6SWang Qing 613782607adcSTejun Heo wq_watchdog_touched = jiffies; 613882607adcSTejun Heo } 613982607adcSTejun Heo 614082607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 614182607adcSTejun Heo { 614282607adcSTejun Heo wq_watchdog_thresh = 0; 614382607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 614482607adcSTejun Heo 614582607adcSTejun Heo if (thresh) { 614682607adcSTejun Heo wq_watchdog_thresh = thresh; 614782607adcSTejun Heo wq_watchdog_reset_touched(); 614882607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 614982607adcSTejun Heo } 615082607adcSTejun Heo } 615182607adcSTejun Heo 615282607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 615382607adcSTejun Heo const struct kernel_param *kp) 615482607adcSTejun Heo { 615582607adcSTejun Heo unsigned long thresh; 615682607adcSTejun Heo int ret; 615782607adcSTejun Heo 615882607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 615982607adcSTejun Heo if (ret) 616082607adcSTejun Heo return ret; 616182607adcSTejun Heo 616282607adcSTejun Heo if (system_wq) 616382607adcSTejun Heo wq_watchdog_set_thresh(thresh); 616482607adcSTejun Heo else 616582607adcSTejun Heo wq_watchdog_thresh = thresh; 616682607adcSTejun Heo 616782607adcSTejun Heo return 0; 616882607adcSTejun Heo } 616982607adcSTejun Heo 617082607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 617182607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 617282607adcSTejun Heo .get = param_get_ulong, 617382607adcSTejun Heo }; 617482607adcSTejun Heo 617582607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 617682607adcSTejun Heo 0644); 617782607adcSTejun Heo 617882607adcSTejun Heo static void wq_watchdog_init(void) 617982607adcSTejun Heo { 61805cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 618182607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 618282607adcSTejun Heo } 618382607adcSTejun Heo 618482607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 618582607adcSTejun Heo 618682607adcSTejun Heo static inline void wq_watchdog_init(void) { } 618782607adcSTejun Heo 618882607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 618982607adcSTejun Heo 6190bce90380STejun Heo static void __init wq_numa_init(void) 6191bce90380STejun Heo { 6192bce90380STejun Heo cpumask_var_t *tbl; 6193bce90380STejun Heo int node, cpu; 6194bce90380STejun Heo 6195bce90380STejun Heo if (num_possible_nodes() <= 1) 6196bce90380STejun Heo return; 6197bce90380STejun Heo 6198d55262c4STejun Heo if (wq_disable_numa) { 6199d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 6200d55262c4STejun Heo return; 6201d55262c4STejun Heo } 6202d55262c4STejun Heo 6203f728c4a9SZhen Lei for_each_possible_cpu(cpu) { 6204f728c4a9SZhen Lei if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) { 6205f728c4a9SZhen Lei pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 6206f728c4a9SZhen Lei return; 6207f728c4a9SZhen Lei } 6208f728c4a9SZhen Lei } 6209f728c4a9SZhen Lei 6210be69d00dSThomas Gleixner wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(); 62114c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 62124c16bd32STejun Heo 6213bce90380STejun Heo /* 6214bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 6215bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 6216bce90380STejun Heo * fully initialized by now. 6217bce90380STejun Heo */ 62186396bb22SKees Cook tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL); 6219bce90380STejun Heo BUG_ON(!tbl); 6220bce90380STejun Heo 6221bce90380STejun Heo for_each_node(node) 62225a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 62231be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 6224bce90380STejun Heo 6225bce90380STejun Heo for_each_possible_cpu(cpu) { 6226bce90380STejun Heo node = cpu_to_node(cpu); 6227bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 6228bce90380STejun Heo } 6229bce90380STejun Heo 6230bce90380STejun Heo wq_numa_possible_cpumask = tbl; 6231bce90380STejun Heo wq_numa_enabled = true; 6232bce90380STejun Heo } 6233bce90380STejun Heo 62343347fa09STejun Heo /** 62353347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 62363347fa09STejun Heo * 62373347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 62383347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 62393347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 62403347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 62413347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 62423347fa09STejun Heo * created and scheduled right before early initcalls. 62433347fa09STejun Heo */ 62442333e829SYu Chen void __init workqueue_init_early(void) 62451da177e4SLinus Torvalds { 62467a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 62477a4e344cSTejun Heo int i, cpu; 6248c34056a3STejun Heo 624910cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6250e904e6c2STejun Heo 6251b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 625204d4e665SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_WQ)); 625304d4e665SFrederic Weisbecker cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN)); 6254b05a7928SFrederic Weisbecker 6255e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6256e904e6c2STejun Heo 6257706026c2STejun Heo /* initialize CPU pools */ 625829c91e99STejun Heo for_each_possible_cpu(cpu) { 62594ce62e9eSTejun Heo struct worker_pool *pool; 62608b03ae3cSTejun Heo 62617a4e344cSTejun Heo i = 0; 6262f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 62637a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 6264ec22ca5eSTejun Heo pool->cpu = cpu; 62657a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 62667a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 6267f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 62687a4e344cSTejun Heo 62699daf9e67STejun Heo /* alloc pool ID */ 627068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 62719daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 627268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 62734ce62e9eSTejun Heo } 62748b03ae3cSTejun Heo } 62758b03ae3cSTejun Heo 62768a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 627729c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 627829c91e99STejun Heo struct workqueue_attrs *attrs; 627929c91e99STejun Heo 6280be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 628129c91e99STejun Heo attrs->nice = std_nice[i]; 628229c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 62838a2b7538STejun Heo 62848a2b7538STejun Heo /* 62858a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 62868a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 62878a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 62888a2b7538STejun Heo */ 6289be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 62908a2b7538STejun Heo attrs->nice = std_nice[i]; 62918a2b7538STejun Heo attrs->no_numa = true; 62928a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 629329c91e99STejun Heo } 629429c91e99STejun Heo 6295d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 62961aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 6297d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 6298f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6299f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 630024d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 630124d51addSTejun Heo WQ_FREEZABLE, 0); 63020668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 63030668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 63040668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 63050668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 63060668106cSViresh Kumar 0); 63071aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 63080668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 63090668106cSViresh Kumar !system_power_efficient_wq || 63100668106cSViresh Kumar !system_freezable_power_efficient_wq); 63113347fa09STejun Heo } 63123347fa09STejun Heo 63133347fa09STejun Heo /** 63143347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 63153347fa09STejun Heo * 63163347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 63173347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 63183347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 63193347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 63203347fa09STejun Heo * with the initial workers and enable future kworker creations. 63213347fa09STejun Heo */ 63222333e829SYu Chen void __init workqueue_init(void) 63233347fa09STejun Heo { 63242186d9f9STejun Heo struct workqueue_struct *wq; 63253347fa09STejun Heo struct worker_pool *pool; 63263347fa09STejun Heo int cpu, bkt; 63273347fa09STejun Heo 63282186d9f9STejun Heo /* 63292186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 63302186d9f9STejun Heo * CPU to node mapping may not be available that early on some 63312186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 63322186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 63332186d9f9STejun Heo * affinity, fix them up. 633440c17f75STejun Heo * 633540c17f75STejun Heo * Also, while iterating workqueues, create rescuers if requested. 63362186d9f9STejun Heo */ 63372186d9f9STejun Heo wq_numa_init(); 63382186d9f9STejun Heo 63392186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 63402186d9f9STejun Heo 63412186d9f9STejun Heo for_each_possible_cpu(cpu) { 63422186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 63432186d9f9STejun Heo pool->node = cpu_to_node(cpu); 63442186d9f9STejun Heo } 63452186d9f9STejun Heo } 63462186d9f9STejun Heo 634740c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 63482186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 634940c17f75STejun Heo WARN(init_rescuer(wq), 635040c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 635140c17f75STejun Heo wq->name); 635240c17f75STejun Heo } 63532186d9f9STejun Heo 63542186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 63552186d9f9STejun Heo 63563347fa09STejun Heo /* create the initial workers */ 63573347fa09STejun Heo for_each_online_cpu(cpu) { 63583347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 63593347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 63603347fa09STejun Heo BUG_ON(!create_worker(pool)); 63613347fa09STejun Heo } 63623347fa09STejun Heo } 63633347fa09STejun Heo 63643347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 63653347fa09STejun Heo BUG_ON(!create_worker(pool)); 63663347fa09STejun Heo 63673347fa09STejun Heo wq_online = true; 636882607adcSTejun Heo wq_watchdog_init(); 63691da177e4SLinus Torvalds } 6370c4f135d6STetsuo Handa 6371c4f135d6STetsuo Handa /* 6372c4f135d6STetsuo Handa * Despite the naming, this is a no-op function which is here only for avoiding 6373c4f135d6STetsuo Handa * link error. Since compile-time warning may fail to catch, we will need to 6374c4f135d6STetsuo Handa * emit run-time warning from __flush_workqueue(). 6375c4f135d6STetsuo Handa */ 6376c4f135d6STetsuo Handa void __warn_flushing_systemwide_wq(void) { } 6377c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 6378