1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 41da177e4SLinus Torvalds * 5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 81da177e4SLinus Torvalds * David Woodhouse <[email protected]> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <[email protected]> 111da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1289ada679SChristoph Lameter * 13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 14c54fce6eSTejun Heo * 15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 17c54fce6eSTejun Heo * 18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 21b11895c4SLibin * normal work items and the other for high priority ones) and some extra 22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 23b11895c4SLibin * number of these backing pools is dynamic. 24c54fce6eSTejun Heo * 259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 289984de1aSPaul Gortmaker #include <linux/export.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/signal.h> 331da177e4SLinus Torvalds #include <linux/completion.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/notifier.h> 381da177e4SLinus Torvalds #include <linux/kthread.h> 391fa44ecaSJames Bottomley #include <linux/hardirq.h> 4046934023SChristoph Lameter #include <linux/mempolicy.h> 41341a5958SRafael J. Wysocki #include <linux/freezer.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 52cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5362635ea8SSergey Senozhatsky #include <linux/nmi.h> 54940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 55aa6fde93STejun Heo #include <linux/delay.h> 56e22bee78STejun Heo 57ea138446STejun Heo #include "workqueue_internal.h" 581da177e4SLinus Torvalds 59e563d0a7STejun Heo enum worker_pool_flags { 60bc2ae0f5STejun Heo /* 6124647570STejun Heo * worker_pool flags 62bc2ae0f5STejun Heo * 6324647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 64bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 65bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 66bc2ae0f5STejun Heo * is in effect. 67bc2ae0f5STejun Heo * 68bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 69bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7024647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 71bc2ae0f5STejun Heo * 72bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 731258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 744736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 75bc2ae0f5STejun Heo */ 76692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7724647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 78e563d0a7STejun Heo }; 79db7bccf4STejun Heo 80e563d0a7STejun Heo enum worker_flags { 81c8e55f36STejun Heo /* worker flags */ 82c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 83c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 84e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 85fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 86f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 87a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 88e22bee78STejun Heo 89a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 90a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 91e563d0a7STejun Heo }; 92db7bccf4STejun Heo 93e563d0a7STejun Heo enum wq_internal_consts { 94e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 954ce62e9eSTejun Heo 9629c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 97c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 98db7bccf4STejun Heo 99e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 100e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 101e22bee78STejun Heo 1023233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1033233cdbdSTejun Heo /* call for help after 10ms 1043233cdbdSTejun Heo (min two ticks) */ 105e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 106e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 109e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1108698a745SDongsheng Yang * all cpus. Give MIN_NICE. 111e22bee78STejun Heo */ 1128698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1138698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 114ecf6881fSTejun Heo 11531c89007SAudra Mitchell WQ_NAME_LEN = 32, 116c8e55f36STejun Heo }; 117c8e55f36STejun Heo 1181da177e4SLinus Torvalds /* 1194690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1204690c4abSTejun Heo * 121e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 122e41e704bSTejun Heo * everyone else. 1234690c4abSTejun Heo * 124e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 125e22bee78STejun Heo * only be modified and accessed from the local cpu. 126e22bee78STejun Heo * 127d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1284690c4abSTejun Heo * 129bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 130bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 131bdf8b9bfSTejun Heo * kworker. 132bdf8b9bfSTejun Heo * 133bdf8b9bfSTejun Heo * S: Only modified by worker self. 134bdf8b9bfSTejun Heo * 1351258fae7STejun Heo * A: wq_pool_attach_mutex protected. 136822d8405STejun Heo * 13768e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13876af4d93STejun Heo * 13924acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1405bcab335STejun Heo * 1415b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1425b95e1afSLai Jiangshan * 1435b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 14424acfb71SThomas Gleixner * RCU for reads. 1455b95e1afSLai Jiangshan * 1463c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1473c25a55dSLai Jiangshan * 14824acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1492e109a28STejun Heo * 150a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 151a045a272STejun Heo * with READ_ONCE() without locking. 152a045a272STejun Heo * 1532e109a28STejun Heo * MD: wq_mayday_lock protected. 154cd2440d6SPetr Mladek * 155cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1564690c4abSTejun Heo */ 1574690c4abSTejun Heo 1582eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 159c34056a3STejun Heo 160bd7bdd43STejun Heo struct worker_pool { 161a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 162d84ff051STejun Heo int cpu; /* I: the associated cpu */ 163f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1649daf9e67STejun Heo int id; /* I: pool ID */ 165bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 166bd7bdd43STejun Heo 16782607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 168cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 16982607adcSTejun Heo 170bc35f7efSLai Jiangshan /* 171bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 172bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 173bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 174bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 175bc35f7efSLai Jiangshan */ 176bc35f7efSLai Jiangshan int nr_running; 17784f91c62SLai Jiangshan 178bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 179ea1abd61SLai Jiangshan 1805826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1815826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 182bd7bdd43STejun Heo 1832c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 184bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1853f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1863f959aa3SValentin Schneider 187bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 188bd7bdd43STejun Heo 189c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 190c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 191c9e7cf27STejun Heo /* L: hash of busy workers */ 192c9e7cf27STejun Heo 1932607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 19492f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 195e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 19660f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 197e19e397aSTejun Heo 1987cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 199e19e397aSTejun Heo 2007a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 20168e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 20268e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2037a4e344cSTejun Heo 204e19e397aSTejun Heo /* 20524acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 20629c91e99STejun Heo * from get_work_pool(). 20729c91e99STejun Heo */ 20829c91e99STejun Heo struct rcu_head rcu; 20984f91c62SLai Jiangshan }; 2108b03ae3cSTejun Heo 2118b03ae3cSTejun Heo /* 212725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 213725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 214725e8ec5STejun Heo */ 215725e8ec5STejun Heo enum pool_workqueue_stats { 216725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 217725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2188a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 219616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 220725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2218639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 222725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 223725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 224725e8ec5STejun Heo 225725e8ec5STejun Heo PWQ_NR_STATS, 226725e8ec5STejun Heo }; 227725e8ec5STejun Heo 228725e8ec5STejun Heo /* 229112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 230112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 231112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 232112202d9STejun Heo * number of flag bits. 2331da177e4SLinus Torvalds */ 234112202d9STejun Heo struct pool_workqueue { 235bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2364690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 23773f53c4aSTejun Heo int work_color; /* L: current color */ 23873f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2398864b4e5STejun Heo int refcnt; /* L: reference count */ 24073f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 24173f53c4aSTejun Heo /* L: nr of in_flight works */ 242018f3a13SLai Jiangshan 243018f3a13SLai Jiangshan /* 244018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 245018f3a13SLai Jiangshan * 246018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 247018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 248018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 249018f3a13SLai Jiangshan * 250018f3a13SLai Jiangshan * All work items marked with WORK_STRUCT_INACTIVE do not participate 251018f3a13SLai Jiangshan * in pwq->nr_active and all work items in pwq->inactive_works are 252018f3a13SLai Jiangshan * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE 253018f3a13SLai Jiangshan * work items are in pwq->inactive_works. Some of them are ready to 254018f3a13SLai Jiangshan * run in pool->worklist or worker->scheduled. Those work itmes are 255018f3a13SLai Jiangshan * only struct wq_barrier which is used for flush_work() and should 256018f3a13SLai Jiangshan * not participate in pwq->nr_active. For non-barrier work item, it 257018f3a13SLai Jiangshan * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 258018f3a13SLai Jiangshan */ 2591e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 260f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2613c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2622e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2638864b4e5STejun Heo 264725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 265725e8ec5STejun Heo 2668864b4e5STejun Heo /* 267967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 268687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 269687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 270967b494eSTejun Heo * grabbing wq->mutex. 2718864b4e5STejun Heo */ 272687a9aa5STejun Heo struct kthread_work release_work; 2738864b4e5STejun Heo struct rcu_head rcu; 274e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* 27773f53c4aSTejun Heo * Structure used to wait for workqueue flush. 27873f53c4aSTejun Heo */ 27973f53c4aSTejun Heo struct wq_flusher { 2803c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2813c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 28273f53c4aSTejun Heo struct completion done; /* flush completion */ 28373f53c4aSTejun Heo }; 2841da177e4SLinus Torvalds 285226223abSTejun Heo struct wq_device; 286226223abSTejun Heo 28773f53c4aSTejun Heo /* 288*91ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 289*91ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 290*91ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 291*91ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 292*91ccc6e7STejun Heo */ 293*91ccc6e7STejun Heo struct wq_node_nr_active { 294*91ccc6e7STejun Heo atomic_t nr; /* per-node nr_active count */ 295*91ccc6e7STejun Heo }; 296*91ccc6e7STejun Heo 297*91ccc6e7STejun Heo /* 298c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 299c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3001da177e4SLinus Torvalds */ 3011da177e4SLinus Torvalds struct workqueue_struct { 3023c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 303e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 30473f53c4aSTejun Heo 3053c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3063c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3073c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 308112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3093c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3103c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3113c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 31273f53c4aSTejun Heo 3132e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 31430ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 315e22bee78STejun Heo 31687fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 317a045a272STejun Heo int max_active; /* WO: max active works */ 318a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 319226223abSTejun Heo 3205b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3219f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3226029a918STejun Heo 323226223abSTejun Heo #ifdef CONFIG_SYSFS 324226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 325226223abSTejun Heo #endif 3264e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 327669de8bdSBart Van Assche char *lock_name; 328669de8bdSBart Van Assche struct lock_class_key key; 3294e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3304e6045f1SJohannes Berg #endif 331ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3322728fd2fSTejun Heo 333e2dca7adSTejun Heo /* 33424acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 33524acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 336e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 337e2dca7adSTejun Heo */ 338e2dca7adSTejun Heo struct rcu_head rcu; 339e2dca7adSTejun Heo 3402728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3412728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 342636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 343*91ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3441da177e4SLinus Torvalds }; 3451da177e4SLinus Torvalds 346e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 347e904e6c2STejun Heo 34884193c07STejun Heo /* 34984193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 35084193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 35184193c07STejun Heo */ 35284193c07STejun Heo struct wq_pod_type { 35384193c07STejun Heo int nr_pods; /* number of pods */ 35484193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 35584193c07STejun Heo int *pod_node; /* pod -> node */ 35684193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 35784193c07STejun Heo }; 35884193c07STejun Heo 35984193c07STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 360523a301eSTejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 36163c5484eSTejun Heo 36263c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 363523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 36463c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 36563c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 36663c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 36763c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 36863c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 36963c5484eSTejun Heo }; 370bce90380STejun Heo 371616db877STejun Heo /* 372616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 373616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 374616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 375aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 376aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 377616db877STejun Heo */ 378aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 379616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 380616db877STejun Heo 381cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 382552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 383cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 384cee22a15SViresh Kumar 385863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3863347fa09STejun Heo 387fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 388fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 3894c16bd32STejun Heo 39068e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3911258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 392a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 393d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 394d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3955bcab335STejun Heo 396e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 39768e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3987d19c5ceSTejun Heo 39999c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 400ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 401ef557180SMike Galbraith 402fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 403fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 404fe28f631SWaiman Long 405fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 406fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 407fe28f631SWaiman Long 408ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 409ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 410ace3c549Stiozhang 411ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 412ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 413b05a7928SFrederic Weisbecker 414f303fccbSTejun Heo /* 415f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 416f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 417f303fccbSTejun Heo * to uncover usages which depend on it. 418f303fccbSTejun Heo */ 419f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 420f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 421f303fccbSTejun Heo #else 422f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 423f303fccbSTejun Heo #endif 424f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 425f303fccbSTejun Heo 4267d19c5ceSTejun Heo /* the per-cpu worker pools */ 42725528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 4287d19c5ceSTejun Heo 42968e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4307d19c5ceSTejun Heo 43168e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 43229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 43329c91e99STejun Heo 434c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 43529c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 43629c91e99STejun Heo 4378a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4388a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4398a2b7538STejun Heo 440967b494eSTejun Heo /* 441967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 442967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 443967b494eSTejun Heo * worker to avoid A-A deadlocks. 444967b494eSTejun Heo */ 44568279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 446967b494eSTejun Heo 44768279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 448ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 44968279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 4501aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 45168279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 452d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 45368279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 454f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 45568279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 45624d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 45768279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 4580668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 45968279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 4600668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 461d320c038STejun Heo 4627d19c5ceSTejun Heo static int worker_thread(void *__worker); 4636ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 464c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 46555df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4667d19c5ceSTejun Heo 46797bd2347STejun Heo #define CREATE_TRACE_POINTS 46897bd2347STejun Heo #include <trace/events/workqueue.h> 46997bd2347STejun Heo 47068e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 47124acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 472f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 47324acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4745bcab335STejun Heo 4755b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 47624acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 477f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 478f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 47924acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4805b95e1afSLai Jiangshan 481f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 482f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 483f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4847a62c2c8STejun Heo (pool)++) 4854ce62e9eSTejun Heo 48649e3cf44STejun Heo /** 48717116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 48817116969STejun Heo * @pool: iteration cursor 489611c92a0STejun Heo * @pi: integer used for iteration 490fa1b54e6STejun Heo * 49124acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 49268e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 49368e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 494fa1b54e6STejun Heo * 495fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 496fa1b54e6STejun Heo * ignored. 49717116969STejun Heo */ 498611c92a0STejun Heo #define for_each_pool(pool, pi) \ 499611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 50068e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 501fa1b54e6STejun Heo else 50217116969STejun Heo 50317116969STejun Heo /** 504822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 505822d8405STejun Heo * @worker: iteration cursor 506822d8405STejun Heo * @pool: worker_pool to iterate workers of 507822d8405STejun Heo * 5081258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 509822d8405STejun Heo * 510822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 511822d8405STejun Heo * ignored. 512822d8405STejun Heo */ 513da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 514da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5151258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 516822d8405STejun Heo else 517822d8405STejun Heo 518822d8405STejun Heo /** 51949e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 52049e3cf44STejun Heo * @pwq: iteration cursor 52149e3cf44STejun Heo * @wq: the target workqueue 52276af4d93STejun Heo * 52324acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 524794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 525794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 52676af4d93STejun Heo * 52776af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 52876af4d93STejun Heo * ignored. 52949e3cf44STejun Heo */ 53049e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 53149e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5325a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 533f3421797STejun Heo 534dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 535dc186ad7SThomas Gleixner 536f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 537dc186ad7SThomas Gleixner 53899777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 53999777288SStanislaw Gruszka { 54099777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 54199777288SStanislaw Gruszka } 54299777288SStanislaw Gruszka 543b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 544b9fdac7fSDu, Changbin { 545b9fdac7fSDu, Changbin struct work_struct *work = addr; 546b9fdac7fSDu, Changbin 547b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 548b9fdac7fSDu, Changbin } 549b9fdac7fSDu, Changbin 550dc186ad7SThomas Gleixner /* 551dc186ad7SThomas Gleixner * fixup_init is called when: 552dc186ad7SThomas Gleixner * - an active object is initialized 553dc186ad7SThomas Gleixner */ 55402a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 555dc186ad7SThomas Gleixner { 556dc186ad7SThomas Gleixner struct work_struct *work = addr; 557dc186ad7SThomas Gleixner 558dc186ad7SThomas Gleixner switch (state) { 559dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 560dc186ad7SThomas Gleixner cancel_work_sync(work); 561dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 56202a982a6SDu, Changbin return true; 563dc186ad7SThomas Gleixner default: 56402a982a6SDu, Changbin return false; 565dc186ad7SThomas Gleixner } 566dc186ad7SThomas Gleixner } 567dc186ad7SThomas Gleixner 568dc186ad7SThomas Gleixner /* 569dc186ad7SThomas Gleixner * fixup_free is called when: 570dc186ad7SThomas Gleixner * - an active object is freed 571dc186ad7SThomas Gleixner */ 57202a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 573dc186ad7SThomas Gleixner { 574dc186ad7SThomas Gleixner struct work_struct *work = addr; 575dc186ad7SThomas Gleixner 576dc186ad7SThomas Gleixner switch (state) { 577dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 578dc186ad7SThomas Gleixner cancel_work_sync(work); 579dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 58002a982a6SDu, Changbin return true; 581dc186ad7SThomas Gleixner default: 58202a982a6SDu, Changbin return false; 583dc186ad7SThomas Gleixner } 584dc186ad7SThomas Gleixner } 585dc186ad7SThomas Gleixner 586f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 587dc186ad7SThomas Gleixner .name = "work_struct", 58899777288SStanislaw Gruszka .debug_hint = work_debug_hint, 589b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 590dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 591dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 592dc186ad7SThomas Gleixner }; 593dc186ad7SThomas Gleixner 594dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 595dc186ad7SThomas Gleixner { 596dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 597dc186ad7SThomas Gleixner } 598dc186ad7SThomas Gleixner 599dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 600dc186ad7SThomas Gleixner { 601dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 602dc186ad7SThomas Gleixner } 603dc186ad7SThomas Gleixner 604dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 605dc186ad7SThomas Gleixner { 606dc186ad7SThomas Gleixner if (onstack) 607dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 608dc186ad7SThomas Gleixner else 609dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 610dc186ad7SThomas Gleixner } 611dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 612dc186ad7SThomas Gleixner 613dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 614dc186ad7SThomas Gleixner { 615dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 616dc186ad7SThomas Gleixner } 617dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 618dc186ad7SThomas Gleixner 619ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 620ea2e64f2SThomas Gleixner { 621ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 622ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 623ea2e64f2SThomas Gleixner } 624ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 625ea2e64f2SThomas Gleixner 626dc186ad7SThomas Gleixner #else 627dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 628dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 629dc186ad7SThomas Gleixner #endif 630dc186ad7SThomas Gleixner 6314e8b22bdSLi Bin /** 63267dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6334e8b22bdSLi Bin * @pool: the pool pointer of interest 6344e8b22bdSLi Bin * 6354e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6364e8b22bdSLi Bin * successfully, -errno on failure. 6374e8b22bdSLi Bin */ 6389daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6399daf9e67STejun Heo { 6409daf9e67STejun Heo int ret; 6419daf9e67STejun Heo 64268e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6435bcab335STejun Heo 6444e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6454e8b22bdSLi Bin GFP_KERNEL); 646229641a6STejun Heo if (ret >= 0) { 647e68035fbSTejun Heo pool->id = ret; 648229641a6STejun Heo return 0; 649229641a6STejun Heo } 6509daf9e67STejun Heo return ret; 6519daf9e67STejun Heo } 6529daf9e67STejun Heo 6539f66cff2STejun Heo static struct pool_workqueue __rcu ** 6549f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 6559f66cff2STejun Heo { 6569f66cff2STejun Heo if (cpu >= 0) 6579f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 6589f66cff2STejun Heo else 6599f66cff2STejun Heo return &wq->dfl_pwq; 6609f66cff2STejun Heo } 6619f66cff2STejun Heo 6629f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 6639f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 6649f66cff2STejun Heo { 6659f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 6669f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 6679f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 6689f66cff2STejun Heo } 6699f66cff2STejun Heo 67073f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 67173f53c4aSTejun Heo { 67273f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 67373f53c4aSTejun Heo } 67473f53c4aSTejun Heo 675c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 67673f53c4aSTejun Heo { 677c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 67873f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 67973f53c4aSTejun Heo } 68073f53c4aSTejun Heo 68173f53c4aSTejun Heo static int work_next_color(int color) 68273f53c4aSTejun Heo { 68373f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 684a848e3b6SOleg Nesterov } 685a848e3b6SOleg Nesterov 6864594bf15SDavid Howells /* 687112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 688112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6897c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6907a22ad75STejun Heo * 691112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 692112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 693bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 694bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6957a22ad75STejun Heo * 696112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6977c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 698112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6997c3eed5cSTejun Heo * available only while the work item is queued. 700bbb68dfaSTejun Heo * 701bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 702bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 703bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 704bbb68dfaSTejun Heo * try to steal the PENDING bit. 7054594bf15SDavid Howells */ 7067a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 7077a22ad75STejun Heo unsigned long flags) 7087a22ad75STejun Heo { 7096183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7107a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7117a22ad75STejun Heo } 7127a22ad75STejun Heo 713112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7144690c4abSTejun Heo unsigned long extra_flags) 715365970a1SDavid Howells { 716112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 717112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 718365970a1SDavid Howells } 719365970a1SDavid Howells 7204468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7214468a00fSLai Jiangshan int pool_id) 7224468a00fSLai Jiangshan { 7234468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7244468a00fSLai Jiangshan WORK_STRUCT_PENDING); 7254468a00fSLai Jiangshan } 7264468a00fSLai Jiangshan 7277c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7287c3eed5cSTejun Heo int pool_id) 7294d707b9fSOleg Nesterov { 73023657bb1STejun Heo /* 73123657bb1STejun Heo * The following wmb is paired with the implied mb in 73223657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 73323657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 73423657bb1STejun Heo * owner. 73523657bb1STejun Heo */ 73623657bb1STejun Heo smp_wmb(); 7377c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 738346c09f8SRoman Pen /* 739346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 740346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 741346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7428bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 743346c09f8SRoman Pen * the same @work. E.g. consider this case: 744346c09f8SRoman Pen * 745346c09f8SRoman Pen * CPU#0 CPU#1 746346c09f8SRoman Pen * ---------------------------- -------------------------------- 747346c09f8SRoman Pen * 748346c09f8SRoman Pen * 1 STORE event_indicated 749346c09f8SRoman Pen * 2 queue_work_on() { 750346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 751346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 752346c09f8SRoman Pen * 5 set_work_data() # clear bit 753346c09f8SRoman Pen * 6 smp_mb() 754346c09f8SRoman Pen * 7 work->current_func() { 755346c09f8SRoman Pen * 8 LOAD event_indicated 756346c09f8SRoman Pen * } 757346c09f8SRoman Pen * 758346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 759346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 760346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 761346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 762346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 763346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 764346c09f8SRoman Pen * before actual STORE. 765346c09f8SRoman Pen */ 766346c09f8SRoman Pen smp_mb(); 7674d707b9fSOleg Nesterov } 7684d707b9fSOleg Nesterov 7697a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 770365970a1SDavid Howells { 7717c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7727c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7737a22ad75STejun Heo } 7747a22ad75STejun Heo 775afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 776afa4bb77SLinus Torvalds { 777afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 778afa4bb77SLinus Torvalds } 779afa4bb77SLinus Torvalds 780112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7817a22ad75STejun Heo { 782e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7837a22ad75STejun Heo 784112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 785afa4bb77SLinus Torvalds return work_struct_pwq(data); 786e120153dSTejun Heo else 787e120153dSTejun Heo return NULL; 7887a22ad75STejun Heo } 7897a22ad75STejun Heo 7907c3eed5cSTejun Heo /** 7917c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7927c3eed5cSTejun Heo * @work: the work item of interest 7937c3eed5cSTejun Heo * 79468e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 79524acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 79624acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 797fa1b54e6STejun Heo * 798fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 799fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 800fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 801fa1b54e6STejun Heo * returned pool is and stays online. 802d185af30SYacine Belkadi * 803d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8047c3eed5cSTejun Heo */ 8057c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8067a22ad75STejun Heo { 807e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8087c3eed5cSTejun Heo int pool_id; 8097a22ad75STejun Heo 81068e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 811fa1b54e6STejun Heo 812112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 813afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8147a22ad75STejun Heo 8157c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8167c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8177a22ad75STejun Heo return NULL; 8187a22ad75STejun Heo 819fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8207c3eed5cSTejun Heo } 8217c3eed5cSTejun Heo 8227c3eed5cSTejun Heo /** 8237c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8247c3eed5cSTejun Heo * @work: the work item of interest 8257c3eed5cSTejun Heo * 826d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8277c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8287c3eed5cSTejun Heo */ 8297c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8307c3eed5cSTejun Heo { 83154d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 8327c3eed5cSTejun Heo 833112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 834afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 83554d5b7d0SLai Jiangshan 83654d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 8377c3eed5cSTejun Heo } 8387c3eed5cSTejun Heo 839bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 840bbb68dfaSTejun Heo { 8417c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 842bbb68dfaSTejun Heo 8437c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8447c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 845bbb68dfaSTejun Heo } 846bbb68dfaSTejun Heo 847bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 848bbb68dfaSTejun Heo { 849bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 850bbb68dfaSTejun Heo 851112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 852bbb68dfaSTejun Heo } 853bbb68dfaSTejun Heo 854e22bee78STejun Heo /* 8553270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8563270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 857d565ed63STejun Heo * they're being called with pool->lock held. 858e22bee78STejun Heo */ 859e22bee78STejun Heo 860e22bee78STejun Heo /* 861e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 862e22bee78STejun Heo * running workers. 863974271c4STejun Heo * 864974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 865706026c2STejun Heo * function will always return %true for unbound pools as long as the 866974271c4STejun Heo * worklist isn't empty. 867e22bee78STejun Heo */ 86863d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 869e22bee78STejun Heo { 8700219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 871e22bee78STejun Heo } 872e22bee78STejun Heo 873e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 87463d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 875e22bee78STejun Heo { 87663d95a91STejun Heo return pool->nr_idle; 877e22bee78STejun Heo } 878e22bee78STejun Heo 879e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 88063d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 881e22bee78STejun Heo { 882bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 883e22bee78STejun Heo } 884e22bee78STejun Heo 885e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 88663d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 887e22bee78STejun Heo { 88863d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 889e22bee78STejun Heo } 890e22bee78STejun Heo 891e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 89263d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 893e22bee78STejun Heo { 894692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 89563d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 89663d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 897e22bee78STejun Heo 898e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 899e22bee78STejun Heo } 900e22bee78STejun Heo 9014690c4abSTejun Heo /** 902e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 903cb444766STejun Heo * @worker: self 904d302f017STejun Heo * @flags: flags to set 905d302f017STejun Heo * 906228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 907d302f017STejun Heo */ 908228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 909d302f017STejun Heo { 910bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 911e22bee78STejun Heo 912bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 913cb444766STejun Heo 914228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 915e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 916e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 917bc35f7efSLai Jiangshan pool->nr_running--; 918e22bee78STejun Heo } 919e22bee78STejun Heo 920d302f017STejun Heo worker->flags |= flags; 921d302f017STejun Heo } 922d302f017STejun Heo 923d302f017STejun Heo /** 924e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 925cb444766STejun Heo * @worker: self 926d302f017STejun Heo * @flags: flags to clear 927d302f017STejun Heo * 928e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 929d302f017STejun Heo */ 930d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 931d302f017STejun Heo { 93263d95a91STejun Heo struct worker_pool *pool = worker->pool; 933e22bee78STejun Heo unsigned int oflags = worker->flags; 934e22bee78STejun Heo 935bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 936cb444766STejun Heo 937d302f017STejun Heo worker->flags &= ~flags; 938e22bee78STejun Heo 93942c025f3STejun Heo /* 94042c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 94142c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 94242c025f3STejun Heo * of multiple flags, not a single flag. 94342c025f3STejun Heo */ 944e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 945e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 946bc35f7efSLai Jiangshan pool->nr_running++; 947d302f017STejun Heo } 948d302f017STejun Heo 949797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 950797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 951797e8345STejun Heo { 952797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 953797e8345STejun Heo return NULL; 954797e8345STejun Heo 955797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 956797e8345STejun Heo } 957797e8345STejun Heo 958797e8345STejun Heo /** 959797e8345STejun Heo * worker_enter_idle - enter idle state 960797e8345STejun Heo * @worker: worker which is entering idle state 961797e8345STejun Heo * 962797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 963797e8345STejun Heo * necessary. 964797e8345STejun Heo * 965797e8345STejun Heo * LOCKING: 966797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 967797e8345STejun Heo */ 968797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 969797e8345STejun Heo { 970797e8345STejun Heo struct worker_pool *pool = worker->pool; 971797e8345STejun Heo 972797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 973797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 974797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 975797e8345STejun Heo return; 976797e8345STejun Heo 977797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 978797e8345STejun Heo worker->flags |= WORKER_IDLE; 979797e8345STejun Heo pool->nr_idle++; 980797e8345STejun Heo worker->last_active = jiffies; 981797e8345STejun Heo 982797e8345STejun Heo /* idle_list is LIFO */ 983797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 984797e8345STejun Heo 985797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 986797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 987797e8345STejun Heo 988797e8345STejun Heo /* Sanity check nr_running. */ 989797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 990797e8345STejun Heo } 991797e8345STejun Heo 992797e8345STejun Heo /** 993797e8345STejun Heo * worker_leave_idle - leave idle state 994797e8345STejun Heo * @worker: worker which is leaving idle state 995797e8345STejun Heo * 996797e8345STejun Heo * @worker is leaving idle state. Update stats. 997797e8345STejun Heo * 998797e8345STejun Heo * LOCKING: 999797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1000797e8345STejun Heo */ 1001797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1002797e8345STejun Heo { 1003797e8345STejun Heo struct worker_pool *pool = worker->pool; 1004797e8345STejun Heo 1005797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1006797e8345STejun Heo return; 1007797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1008797e8345STejun Heo pool->nr_idle--; 1009797e8345STejun Heo list_del_init(&worker->entry); 1010797e8345STejun Heo } 1011797e8345STejun Heo 1012797e8345STejun Heo /** 1013797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1014797e8345STejun Heo * @pool: pool of interest 1015797e8345STejun Heo * @work: work to find worker for 1016797e8345STejun Heo * 1017797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1018797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1019797e8345STejun Heo * to match, its current execution should match the address of @work and 1020797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1021797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1022797e8345STejun Heo * being executed. 1023797e8345STejun Heo * 1024797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1025797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1026797e8345STejun Heo * another work item. If the same work item address ends up being reused 1027797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1028797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1029797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1030797e8345STejun Heo * 1031797e8345STejun Heo * This function checks the work item address and work function to avoid 1032797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1033797e8345STejun Heo * work function which can introduce dependency onto itself through a 1034797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1035797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1036797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1037797e8345STejun Heo * 1038797e8345STejun Heo * CONTEXT: 1039797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1040797e8345STejun Heo * 1041797e8345STejun Heo * Return: 1042797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1043797e8345STejun Heo * otherwise. 1044797e8345STejun Heo */ 1045797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1046797e8345STejun Heo struct work_struct *work) 1047797e8345STejun Heo { 1048797e8345STejun Heo struct worker *worker; 1049797e8345STejun Heo 1050797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1051797e8345STejun Heo (unsigned long)work) 1052797e8345STejun Heo if (worker->current_work == work && 1053797e8345STejun Heo worker->current_func == work->func) 1054797e8345STejun Heo return worker; 1055797e8345STejun Heo 1056797e8345STejun Heo return NULL; 1057797e8345STejun Heo } 1058797e8345STejun Heo 1059797e8345STejun Heo /** 1060797e8345STejun Heo * move_linked_works - move linked works to a list 1061797e8345STejun Heo * @work: start of series of works to be scheduled 1062797e8345STejun Heo * @head: target list to append @work to 1063797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1064797e8345STejun Heo * 1065873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1066873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1067873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1068873eaca6STejun Heo * @nextp. 1069797e8345STejun Heo * 1070797e8345STejun Heo * CONTEXT: 1071797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1072797e8345STejun Heo */ 1073797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1074797e8345STejun Heo struct work_struct **nextp) 1075797e8345STejun Heo { 1076797e8345STejun Heo struct work_struct *n; 1077797e8345STejun Heo 1078797e8345STejun Heo /* 1079797e8345STejun Heo * Linked worklist will always end before the end of the list, 1080797e8345STejun Heo * use NULL for list head. 1081797e8345STejun Heo */ 1082797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1083797e8345STejun Heo list_move_tail(&work->entry, head); 1084797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1085797e8345STejun Heo break; 1086797e8345STejun Heo } 1087797e8345STejun Heo 1088797e8345STejun Heo /* 1089797e8345STejun Heo * If we're already inside safe list traversal and have moved 1090797e8345STejun Heo * multiple works to the scheduled queue, the next position 1091797e8345STejun Heo * needs to be updated. 1092797e8345STejun Heo */ 1093797e8345STejun Heo if (nextp) 1094797e8345STejun Heo *nextp = n; 1095797e8345STejun Heo } 1096797e8345STejun Heo 1097797e8345STejun Heo /** 1098873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1099873eaca6STejun Heo * @work: work to assign 1100873eaca6STejun Heo * @worker: worker to assign to 1101873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1102873eaca6STejun Heo * 1103873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1104873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1105873eaca6STejun Heo * 1106873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1107873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1108873eaca6STejun Heo * list_for_each_entry_safe(). 1109873eaca6STejun Heo * 1110873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1111873eaca6STejun Heo * was punted to another worker already executing it. 1112873eaca6STejun Heo */ 1113873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1114873eaca6STejun Heo struct work_struct **nextp) 1115873eaca6STejun Heo { 1116873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1117873eaca6STejun Heo struct worker *collision; 1118873eaca6STejun Heo 1119873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1120873eaca6STejun Heo 1121873eaca6STejun Heo /* 1122873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1123873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1124873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1125873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1126873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1127873eaca6STejun Heo * defer the work to the currently executing one. 1128873eaca6STejun Heo */ 1129873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1130873eaca6STejun Heo if (unlikely(collision)) { 1131873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1132873eaca6STejun Heo return false; 1133873eaca6STejun Heo } 1134873eaca6STejun Heo 1135873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1136873eaca6STejun Heo return true; 1137873eaca6STejun Heo } 1138873eaca6STejun Heo 1139873eaca6STejun Heo /** 11400219a352STejun Heo * kick_pool - wake up an idle worker if necessary 11410219a352STejun Heo * @pool: pool to kick 1142797e8345STejun Heo * 11430219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 11440219a352STejun Heo * whether a worker was woken up. 1145797e8345STejun Heo */ 11460219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1147797e8345STejun Heo { 1148797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 11498639ecebSTejun Heo struct task_struct *p; 1150797e8345STejun Heo 11510219a352STejun Heo lockdep_assert_held(&pool->lock); 11520219a352STejun Heo 11530219a352STejun Heo if (!need_more_worker(pool) || !worker) 11540219a352STejun Heo return false; 11550219a352STejun Heo 11568639ecebSTejun Heo p = worker->task; 11578639ecebSTejun Heo 11588639ecebSTejun Heo #ifdef CONFIG_SMP 11598639ecebSTejun Heo /* 11608639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 11618639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 11628639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 11638639ecebSTejun Heo * execution locality. 11648639ecebSTejun Heo * 11658639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 11668639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 11678639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 11688639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 11698639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 11708639ecebSTejun Heo * still on cpu when picking an idle worker. 11718639ecebSTejun Heo * 11728639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 11738639ecebSTejun Heo * its affinity scope. Repatriate. 11748639ecebSTejun Heo */ 11758639ecebSTejun Heo if (!pool->attrs->affn_strict && 11768639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 11778639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 11788639ecebSTejun Heo struct work_struct, entry); 11798639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 11808639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 11818639ecebSTejun Heo } 11828639ecebSTejun Heo #endif 11838639ecebSTejun Heo wake_up_process(p); 11840219a352STejun Heo return true; 1185797e8345STejun Heo } 1186797e8345STejun Heo 118763638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 118863638450STejun Heo 118963638450STejun Heo /* 119063638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 119163638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 119263638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 119363638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 119463638450STejun Heo * should be using an unbound workqueue instead. 119563638450STejun Heo * 119663638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 119763638450STejun Heo * and report them so that they can be examined and converted to use unbound 119863638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 119963638450STejun Heo * function is tracked and reported with exponential backoff. 120063638450STejun Heo */ 120163638450STejun Heo #define WCI_MAX_ENTS 128 120263638450STejun Heo 120363638450STejun Heo struct wci_ent { 120463638450STejun Heo work_func_t func; 120563638450STejun Heo atomic64_t cnt; 120663638450STejun Heo struct hlist_node hash_node; 120763638450STejun Heo }; 120863638450STejun Heo 120963638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 121063638450STejun Heo static int wci_nr_ents; 121163638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 121263638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 121363638450STejun Heo 121463638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 121563638450STejun Heo { 121663638450STejun Heo struct wci_ent *ent; 121763638450STejun Heo 121863638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 121963638450STejun Heo (unsigned long)func) { 122063638450STejun Heo if (ent->func == func) 122163638450STejun Heo return ent; 122263638450STejun Heo } 122363638450STejun Heo return NULL; 122463638450STejun Heo } 122563638450STejun Heo 122663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 122763638450STejun Heo { 122863638450STejun Heo struct wci_ent *ent; 122963638450STejun Heo 123063638450STejun Heo restart: 123163638450STejun Heo ent = wci_find_ent(func); 123263638450STejun Heo if (ent) { 123363638450STejun Heo u64 cnt; 123463638450STejun Heo 123563638450STejun Heo /* 123663638450STejun Heo * Start reporting from the fourth time and back off 123763638450STejun Heo * exponentially. 123863638450STejun Heo */ 123963638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 124063638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 124163638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 124263638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 124363638450STejun Heo atomic64_read(&ent->cnt)); 124463638450STejun Heo return; 124563638450STejun Heo } 124663638450STejun Heo 124763638450STejun Heo /* 124863638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 124963638450STejun Heo * is exhausted, something went really wrong and we probably made enough 125063638450STejun Heo * noise already. 125163638450STejun Heo */ 125263638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 125363638450STejun Heo return; 125463638450STejun Heo 125563638450STejun Heo raw_spin_lock(&wci_lock); 125663638450STejun Heo 125763638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 125863638450STejun Heo raw_spin_unlock(&wci_lock); 125963638450STejun Heo return; 126063638450STejun Heo } 126163638450STejun Heo 126263638450STejun Heo if (wci_find_ent(func)) { 126363638450STejun Heo raw_spin_unlock(&wci_lock); 126463638450STejun Heo goto restart; 126563638450STejun Heo } 126663638450STejun Heo 126763638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 126863638450STejun Heo ent->func = func; 126963638450STejun Heo atomic64_set(&ent->cnt, 1); 127063638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 127163638450STejun Heo 127263638450STejun Heo raw_spin_unlock(&wci_lock); 127363638450STejun Heo } 127463638450STejun Heo 127563638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 127663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 127763638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 127863638450STejun Heo 1279c54d5046STejun Heo /** 12801da177e4SLinus Torvalds * wq_worker_running - a worker is running again 12811da177e4SLinus Torvalds * @task: task waking up 12821da177e4SLinus Torvalds * 12831da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 12841da177e4SLinus Torvalds */ 12851da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 12861da177e4SLinus Torvalds { 12871da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12881da177e4SLinus Torvalds 1289c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 12901da177e4SLinus Torvalds return; 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds /* 12931da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 12941da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 12951da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 12961da177e4SLinus Torvalds * pool. Protect against such race. 12971da177e4SLinus Torvalds */ 12981da177e4SLinus Torvalds preempt_disable(); 12991da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13001da177e4SLinus Torvalds worker->pool->nr_running++; 13011da177e4SLinus Torvalds preempt_enable(); 1302616db877STejun Heo 1303616db877STejun Heo /* 1304616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1305616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1306616db877STejun Heo */ 1307616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1308616db877STejun Heo 1309c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds /** 13131da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 13141da177e4SLinus Torvalds * @task: task going to sleep 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 13171da177e4SLinus Torvalds * going to sleep. 13181da177e4SLinus Torvalds */ 13191da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 13201da177e4SLinus Torvalds { 13211da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13221da177e4SLinus Torvalds struct worker_pool *pool; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* 13251da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 13261da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 13271da177e4SLinus Torvalds * checking NOT_RUNNING. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 13301da177e4SLinus Torvalds return; 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds pool = worker->pool; 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1335c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 13361da177e4SLinus Torvalds return; 13371da177e4SLinus Torvalds 1338c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 13391da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds /* 13421da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 13431da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 13441da177e4SLinus Torvalds * and nr_running has been reset. 13451da177e4SLinus Torvalds */ 13461da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 13471da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13481da177e4SLinus Torvalds return; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds pool->nr_running--; 13520219a352STejun Heo if (kick_pool(pool)) 1353725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 13540219a352STejun Heo 13551da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13561da177e4SLinus Torvalds } 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds /** 1359616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1360616db877STejun Heo * @task: task currently running 1361616db877STejun Heo * 1362616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1363616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1364616db877STejun Heo */ 1365616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1366616db877STejun Heo { 1367616db877STejun Heo struct worker *worker = kthread_data(task); 1368616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1369616db877STejun Heo struct worker_pool *pool = worker->pool; 1370616db877STejun Heo 1371616db877STejun Heo if (!pwq) 1372616db877STejun Heo return; 1373616db877STejun Heo 13748a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 13758a1dd1e5STejun Heo 137618c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 137718c8ae81SZqiang return; 137818c8ae81SZqiang 1379616db877STejun Heo /* 1380616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1381616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1382616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1383c8f6219bSZqiang * 1384c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1385c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1386c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1387c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1388c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1389c8f6219bSZqiang * We probably want to make this prettier in the future. 1390616db877STejun Heo */ 1391c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1392616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1393616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1394616db877STejun Heo return; 1395616db877STejun Heo 1396616db877STejun Heo raw_spin_lock(&pool->lock); 1397616db877STejun Heo 1398616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 139963638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1400616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1401616db877STejun Heo 14020219a352STejun Heo if (kick_pool(pool)) 1403616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1404616db877STejun Heo 1405616db877STejun Heo raw_spin_unlock(&pool->lock); 1406616db877STejun Heo } 1407616db877STejun Heo 1408616db877STejun Heo /** 14091da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 14101da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 14111da177e4SLinus Torvalds * 14121da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 14131da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 14141da177e4SLinus Torvalds * 14151da177e4SLinus Torvalds * CONTEXT: 14169b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 14171da177e4SLinus Torvalds * 14181da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1419f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1420f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 14211da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 14221da177e4SLinus Torvalds * 14231da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 14241da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 14251da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 14261da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1427365970a1SDavid Howells * 1428365970a1SDavid Howells * Return: 1429365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1430365970a1SDavid Howells * hasn't executed any work yet. 1431365970a1SDavid Howells */ 1432365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1433365970a1SDavid Howells { 1434365970a1SDavid Howells struct worker *worker = kthread_data(task); 1435365970a1SDavid Howells 1436365970a1SDavid Howells return worker->last_func; 1437365970a1SDavid Howells } 1438365970a1SDavid Howells 1439d302f017STejun Heo /** 1440*91ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 1441*91ccc6e7STejun Heo * @wq: workqueue of interest 1442*91ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 1443*91ccc6e7STejun Heo * 1444*91ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 1445*91ccc6e7STejun Heo * 1446*91ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 1447*91ccc6e7STejun Heo * 1448*91ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 1449*91ccc6e7STejun Heo * 1450*91ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 1451*91ccc6e7STejun Heo */ 1452*91ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 1453*91ccc6e7STejun Heo int node) 1454*91ccc6e7STejun Heo { 1455*91ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 1456*91ccc6e7STejun Heo return NULL; 1457*91ccc6e7STejun Heo 1458*91ccc6e7STejun Heo if (node == NUMA_NO_NODE) 1459*91ccc6e7STejun Heo node = nr_node_ids; 1460*91ccc6e7STejun Heo 1461*91ccc6e7STejun Heo return wq->node_nr_active[node]; 1462*91ccc6e7STejun Heo } 1463*91ccc6e7STejun Heo 1464*91ccc6e7STejun Heo /** 14658864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 14668864b4e5STejun Heo * @pwq: pool_workqueue to get 14678864b4e5STejun Heo * 14688864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 14698864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 14708864b4e5STejun Heo */ 14718864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 14728864b4e5STejun Heo { 14738864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 14748864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 14758864b4e5STejun Heo pwq->refcnt++; 14768864b4e5STejun Heo } 14778864b4e5STejun Heo 14788864b4e5STejun Heo /** 14798864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 14808864b4e5STejun Heo * @pwq: pool_workqueue to put 14818864b4e5STejun Heo * 14828864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 14838864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 14848864b4e5STejun Heo */ 14858864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 14868864b4e5STejun Heo { 14878864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 14888864b4e5STejun Heo if (likely(--pwq->refcnt)) 14898864b4e5STejun Heo return; 14908864b4e5STejun Heo /* 1491967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1492967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 14938864b4e5STejun Heo */ 1494687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 14958864b4e5STejun Heo } 14968864b4e5STejun Heo 1497dce90d47STejun Heo /** 1498dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1499dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1500dce90d47STejun Heo * 1501dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1502dce90d47STejun Heo */ 1503dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1504dce90d47STejun Heo { 1505dce90d47STejun Heo if (pwq) { 1506dce90d47STejun Heo /* 150724acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1508dce90d47STejun Heo * following lock operations are safe. 1509dce90d47STejun Heo */ 1510a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1511dce90d47STejun Heo put_pwq(pwq); 1512a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1513dce90d47STejun Heo } 1514dce90d47STejun Heo } 1515dce90d47STejun Heo 1516afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1517afa87ce8STejun Heo { 1518afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1519afa87ce8STejun Heo } 1520afa87ce8STejun Heo 15214c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 15224c638030STejun Heo struct work_struct *work) 1523bf4ede01STejun Heo { 15241c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 15251c270b79STejun Heo 15261c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1527bf4ede01STejun Heo trace_workqueue_activate_work(work); 152882607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 152982607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1530112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 15311c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 15324c638030STejun Heo } 15334c638030STejun Heo 15344c638030STejun Heo /** 15354c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 15364c638030STejun Heo * @pwq: pool_workqueue @work belongs to 15374c638030STejun Heo * @work: work item to activate 15384c638030STejun Heo * 15394c638030STejun Heo * Returns %true if activated. %false if already active. 15404c638030STejun Heo */ 15414c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 15424c638030STejun Heo struct work_struct *work) 15434c638030STejun Heo { 15444c638030STejun Heo struct worker_pool *pool = pwq->pool; 1545*91ccc6e7STejun Heo struct wq_node_nr_active *nna; 15464c638030STejun Heo 15474c638030STejun Heo lockdep_assert_held(&pool->lock); 15484c638030STejun Heo 15494c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 15504c638030STejun Heo return false; 15514c638030STejun Heo 1552*91ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 1553*91ccc6e7STejun Heo if (nna) 1554*91ccc6e7STejun Heo atomic_inc(&nna->nr); 1555*91ccc6e7STejun Heo 1556112202d9STejun Heo pwq->nr_active++; 15574c638030STejun Heo __pwq_activate_work(pwq, work); 15584c638030STejun Heo return true; 1559bf4ede01STejun Heo } 1560bf4ede01STejun Heo 15611c270b79STejun Heo /** 15621c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 15631c270b79STejun Heo * @pwq: pool_workqueue of interest 15641c270b79STejun Heo * 15651c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 15661c270b79STejun Heo * successfully obtained. %false otherwise. 15671c270b79STejun Heo */ 15681c270b79STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq) 15693aa62497SLai Jiangshan { 15701c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 15711c270b79STejun Heo struct worker_pool *pool = pwq->pool; 1572*91ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 15731c270b79STejun Heo bool obtained; 15741c270b79STejun Heo 15751c270b79STejun Heo lockdep_assert_held(&pool->lock); 15761c270b79STejun Heo 15771c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 15781c270b79STejun Heo 1579*91ccc6e7STejun Heo if (obtained) { 15801c270b79STejun Heo pwq->nr_active++; 1581*91ccc6e7STejun Heo if (nna) 1582*91ccc6e7STejun Heo atomic_inc(&nna->nr); 1583*91ccc6e7STejun Heo } 15841c270b79STejun Heo return obtained; 15851c270b79STejun Heo } 15861c270b79STejun Heo 15871c270b79STejun Heo /** 15881c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 15891c270b79STejun Heo * @pwq: pool_workqueue of interest 15901c270b79STejun Heo * 15911c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 15921c270b79STejun Heo * max_active limit. 15931c270b79STejun Heo * 15941c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 15951c270b79STejun Heo * inactive work item is found or max_active limit is reached. 15961c270b79STejun Heo */ 15971c270b79STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq) 15981c270b79STejun Heo { 15991c270b79STejun Heo struct work_struct *work = 16001c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 16013aa62497SLai Jiangshan struct work_struct, entry); 16023aa62497SLai Jiangshan 16031c270b79STejun Heo if (work && pwq_tryinc_nr_active(pwq)) { 16041c270b79STejun Heo __pwq_activate_work(pwq, work); 16051c270b79STejun Heo return true; 16061c270b79STejun Heo } else { 16071c270b79STejun Heo return false; 16081c270b79STejun Heo } 16091c270b79STejun Heo } 16101c270b79STejun Heo 16111c270b79STejun Heo /** 16121c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 16131c270b79STejun Heo * @pwq: pool_workqueue of interest 16141c270b79STejun Heo * 16151c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 16161c270b79STejun Heo */ 16171c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 16181c270b79STejun Heo { 16191c270b79STejun Heo struct worker_pool *pool = pwq->pool; 1620*91ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 16211c270b79STejun Heo 16221c270b79STejun Heo lockdep_assert_held(&pool->lock); 16231c270b79STejun Heo 1624*91ccc6e7STejun Heo /* 1625*91ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 1626*91ccc6e7STejun Heo * workqueues. 1627*91ccc6e7STejun Heo */ 16281c270b79STejun Heo pwq->nr_active--; 1629*91ccc6e7STejun Heo 1630*91ccc6e7STejun Heo /* 1631*91ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 1632*91ccc6e7STejun Heo * inactive work item on @pwq itself. 1633*91ccc6e7STejun Heo */ 1634*91ccc6e7STejun Heo if (!nna) { 1635*91ccc6e7STejun Heo pwq_activate_first_inactive(pwq); 1636*91ccc6e7STejun Heo return; 1637*91ccc6e7STejun Heo } 1638*91ccc6e7STejun Heo 1639*91ccc6e7STejun Heo atomic_dec(&nna->nr); 16401c270b79STejun Heo pwq_activate_first_inactive(pwq); 16413aa62497SLai Jiangshan } 16423aa62497SLai Jiangshan 1643bf4ede01STejun Heo /** 1644112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1645112202d9STejun Heo * @pwq: pwq of interest 1646c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1647bf4ede01STejun Heo * 1648bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1649112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1650bf4ede01STejun Heo * 1651dd6c3c54STejun Heo * NOTE: 1652dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1653dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1654dd6c3c54STejun Heo * work item is complete. 1655dd6c3c54STejun Heo * 1656bf4ede01STejun Heo * CONTEXT: 1657a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1658bf4ede01STejun Heo */ 1659c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1660bf4ede01STejun Heo { 1661c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1662c4560c2cSLai Jiangshan 16631c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 16641c270b79STejun Heo pwq_dec_nr_active(pwq); 1665018f3a13SLai Jiangshan 1666018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1667bf4ede01STejun Heo 1668bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1669112202d9STejun Heo if (likely(pwq->flush_color != color)) 16708864b4e5STejun Heo goto out_put; 1671bf4ede01STejun Heo 1672bf4ede01STejun Heo /* are there still in-flight works? */ 1673112202d9STejun Heo if (pwq->nr_in_flight[color]) 16748864b4e5STejun Heo goto out_put; 1675bf4ede01STejun Heo 1676112202d9STejun Heo /* this pwq is done, clear flush_color */ 1677112202d9STejun Heo pwq->flush_color = -1; 1678bf4ede01STejun Heo 1679bf4ede01STejun Heo /* 1680112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1681bf4ede01STejun Heo * will handle the rest. 1682bf4ede01STejun Heo */ 1683112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1684112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 16858864b4e5STejun Heo out_put: 16868864b4e5STejun Heo put_pwq(pwq); 1687bf4ede01STejun Heo } 1688bf4ede01STejun Heo 168936e227d2STejun Heo /** 1690bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 169136e227d2STejun Heo * @work: work item to steal 169236e227d2STejun Heo * @is_dwork: @work is a delayed_work 1693bbb68dfaSTejun Heo * @flags: place to store irq state 169436e227d2STejun Heo * 169536e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1696d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 169736e227d2STejun Heo * 1698d185af30SYacine Belkadi * Return: 16993eb6b31bSMauro Carvalho Chehab * 17003eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 170136e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 170236e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 170336e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1704bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1705bbb68dfaSTejun Heo * for arbitrarily long 17063eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 170736e227d2STejun Heo * 1708d185af30SYacine Belkadi * Note: 1709bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1710e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1711e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1712e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1713bbb68dfaSTejun Heo * 1714bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1715bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1716bbb68dfaSTejun Heo * 1717e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1718bf4ede01STejun Heo */ 1719bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1720bbb68dfaSTejun Heo unsigned long *flags) 1721bf4ede01STejun Heo { 1722d565ed63STejun Heo struct worker_pool *pool; 1723112202d9STejun Heo struct pool_workqueue *pwq; 1724bf4ede01STejun Heo 1725bbb68dfaSTejun Heo local_irq_save(*flags); 1726bbb68dfaSTejun Heo 172736e227d2STejun Heo /* try to steal the timer if it exists */ 172836e227d2STejun Heo if (is_dwork) { 172936e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 173036e227d2STejun Heo 1731e0aecdd8STejun Heo /* 1732e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1733e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1734e0aecdd8STejun Heo * running on the local CPU. 1735e0aecdd8STejun Heo */ 173636e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 173736e227d2STejun Heo return 1; 173836e227d2STejun Heo } 173936e227d2STejun Heo 174036e227d2STejun Heo /* try to claim PENDING the normal way */ 1741bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1742bf4ede01STejun Heo return 0; 1743bf4ede01STejun Heo 174424acfb71SThomas Gleixner rcu_read_lock(); 1745bf4ede01STejun Heo /* 1746bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1747bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1748bf4ede01STejun Heo */ 1749d565ed63STejun Heo pool = get_work_pool(work); 1750d565ed63STejun Heo if (!pool) 1751bbb68dfaSTejun Heo goto fail; 1752bf4ede01STejun Heo 1753a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1754bf4ede01STejun Heo /* 1755112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1756112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1757112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1758112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1759112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 17600b3dae68SLai Jiangshan * item is currently queued on that pool. 1761bf4ede01STejun Heo */ 1762112202d9STejun Heo pwq = get_work_pwq(work); 1763112202d9STejun Heo if (pwq && pwq->pool == pool) { 1764bf4ede01STejun Heo debug_work_deactivate(work); 17653aa62497SLai Jiangshan 17663aa62497SLai Jiangshan /* 1767018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1768018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1769018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1770018f3a13SLai Jiangshan * 1771f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1772d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1773f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 177416062836STejun Heo * management later on and cause stall. Make sure the work 177516062836STejun Heo * item is activated before grabbing. 17763aa62497SLai Jiangshan */ 17774c638030STejun Heo pwq_activate_work(pwq, work); 17783aa62497SLai Jiangshan 1779bf4ede01STejun Heo list_del_init(&work->entry); 178036e227d2STejun Heo 1781112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 17824468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 17834468a00fSLai Jiangshan 1784dd6c3c54STejun Heo /* must be the last step, see the function comment */ 1785dd6c3c54STejun Heo pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 1786dd6c3c54STejun Heo 1787a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 178824acfb71SThomas Gleixner rcu_read_unlock(); 178936e227d2STejun Heo return 1; 1790bf4ede01STejun Heo } 1791a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1792bbb68dfaSTejun Heo fail: 179324acfb71SThomas Gleixner rcu_read_unlock(); 1794bbb68dfaSTejun Heo local_irq_restore(*flags); 1795bbb68dfaSTejun Heo if (work_is_canceling(work)) 1796bbb68dfaSTejun Heo return -ENOENT; 1797bbb68dfaSTejun Heo cpu_relax(); 179836e227d2STejun Heo return -EAGAIN; 1799bf4ede01STejun Heo } 1800bf4ede01STejun Heo 1801bf4ede01STejun Heo /** 1802706026c2STejun Heo * insert_work - insert a work into a pool 1803112202d9STejun Heo * @pwq: pwq @work belongs to 18044690c4abSTejun Heo * @work: work to insert 18054690c4abSTejun Heo * @head: insertion point 18064690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 18074690c4abSTejun Heo * 1808112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1809706026c2STejun Heo * work_struct flags. 18104690c4abSTejun Heo * 18114690c4abSTejun Heo * CONTEXT: 1812a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1813365970a1SDavid Howells */ 1814112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1815112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1816b89deed3SOleg Nesterov { 1817fe089f87STejun Heo debug_work_activate(work); 1818e1d8aa9fSFrederic Weisbecker 1819e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1820f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1821e89a85d6SWalter Wu 18224690c4abSTejun Heo /* we own @work, set data and link */ 1823112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 18241a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 18258864b4e5STejun Heo get_pwq(pwq); 1826b89deed3SOleg Nesterov } 1827b89deed3SOleg Nesterov 1828c8efcc25STejun Heo /* 1829c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 18308d03ecfeSTejun Heo * same workqueue. 1831c8efcc25STejun Heo */ 1832c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1833c8efcc25STejun Heo { 1834c8efcc25STejun Heo struct worker *worker; 1835c8efcc25STejun Heo 18368d03ecfeSTejun Heo worker = current_wq_worker(); 1837c8efcc25STejun Heo /* 1838bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 18398d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1840c8efcc25STejun Heo */ 1841112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1842c8efcc25STejun Heo } 1843c8efcc25STejun Heo 1844ef557180SMike Galbraith /* 1845ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1846ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1847ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1848ef557180SMike Galbraith */ 1849ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1850ef557180SMike Galbraith { 1851ef557180SMike Galbraith int new_cpu; 1852ef557180SMike Galbraith 1853f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1854ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1855ef557180SMike Galbraith return cpu; 1856a8ec5880SAmmar Faizi } else { 1857a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1858f303fccbSTejun Heo } 1859f303fccbSTejun Heo 1860ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1861ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1862ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1863ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1864ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1865ef557180SMike Galbraith return cpu; 1866ef557180SMike Galbraith } 1867ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1868ef557180SMike Galbraith 1869ef557180SMike Galbraith return new_cpu; 1870ef557180SMike Galbraith } 1871ef557180SMike Galbraith 1872d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 18731da177e4SLinus Torvalds struct work_struct *work) 18741da177e4SLinus Torvalds { 1875112202d9STejun Heo struct pool_workqueue *pwq; 1876fe089f87STejun Heo struct worker_pool *last_pool, *pool; 18778a2e8e5dSTejun Heo unsigned int work_flags; 1878b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 18798930cabaSTejun Heo 18808930cabaSTejun Heo /* 18818930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 18828930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 18838930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 18848930cabaSTejun Heo * happen with IRQ disabled. 18858930cabaSTejun Heo */ 18868e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 18871da177e4SLinus Torvalds 18881e19ffc6STejun Heo 188933e3f0a3SRichard Clark /* 189033e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 189133e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 189233e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 189333e3f0a3SRichard Clark */ 189433e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 189533e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1896e41e704bSTejun Heo return; 189724acfb71SThomas Gleixner rcu_read_lock(); 18989e8cd2f5STejun Heo retry: 1899aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1900636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 1901636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 1902ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1903636b927eSTejun Heo else 1904aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1905aa202f1fSHillf Danton } 1906f3421797STejun Heo 1907636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 1908fe089f87STejun Heo pool = pwq->pool; 1909fe089f87STejun Heo 191018aa9effSTejun Heo /* 1911c9178087STejun Heo * If @work was previously on a different pool, it might still be 1912c9178087STejun Heo * running there, in which case the work needs to be queued on that 1913c9178087STejun Heo * pool to guarantee non-reentrancy. 191418aa9effSTejun Heo */ 1915c9e7cf27STejun Heo last_pool = get_work_pool(work); 1916fe089f87STejun Heo if (last_pool && last_pool != pool) { 191718aa9effSTejun Heo struct worker *worker; 191818aa9effSTejun Heo 1919a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 192018aa9effSTejun Heo 1921c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 192218aa9effSTejun Heo 1923112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1924c9178087STejun Heo pwq = worker->current_pwq; 1925fe089f87STejun Heo pool = pwq->pool; 1926fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 19278594fadeSLai Jiangshan } else { 192818aa9effSTejun Heo /* meh... not running there, queue here */ 1929a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1930fe089f87STejun Heo raw_spin_lock(&pool->lock); 193118aa9effSTejun Heo } 19328930cabaSTejun Heo } else { 1933fe089f87STejun Heo raw_spin_lock(&pool->lock); 19348930cabaSTejun Heo } 1935502ca9d8STejun Heo 19369e8cd2f5STejun Heo /* 1937636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 1938636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 1939636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 1940636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 1941636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 19429e8cd2f5STejun Heo */ 19439e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 19449e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1945fe089f87STejun Heo raw_spin_unlock(&pool->lock); 19469e8cd2f5STejun Heo cpu_relax(); 19479e8cd2f5STejun Heo goto retry; 19489e8cd2f5STejun Heo } 19499e8cd2f5STejun Heo /* oops */ 19509e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 19519e8cd2f5STejun Heo wq->name, cpu); 19529e8cd2f5STejun Heo } 19539e8cd2f5STejun Heo 1954112202d9STejun Heo /* pwq determined, queue */ 1955112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1956502ca9d8STejun Heo 195724acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 195824acfb71SThomas Gleixner goto out; 19591e19ffc6STejun Heo 1960112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1961112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 19621e19ffc6STejun Heo 1963a045a272STejun Heo /* 1964a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 1965a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 1966a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 1967a045a272STejun Heo */ 19681c270b79STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq)) { 1969fe089f87STejun Heo if (list_empty(&pool->worklist)) 1970fe089f87STejun Heo pool->watchdog_ts = jiffies; 1971fe089f87STejun Heo 1972cdadf009STejun Heo trace_workqueue_activate_work(work); 1973fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 19740219a352STejun Heo kick_pool(pool); 19758a2e8e5dSTejun Heo } else { 1976f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1977fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 19788a2e8e5dSTejun Heo } 19791e19ffc6STejun Heo 198024acfb71SThomas Gleixner out: 1981fe089f87STejun Heo raw_spin_unlock(&pool->lock); 198224acfb71SThomas Gleixner rcu_read_unlock(); 19831da177e4SLinus Torvalds } 19841da177e4SLinus Torvalds 19850fcb78c2SRolf Eike Beer /** 1986c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1987c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1988c1a220e7SZhang Rui * @wq: workqueue to use 1989c1a220e7SZhang Rui * @work: work to queue 1990c1a220e7SZhang Rui * 1991c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1992443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1993443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1994854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1995854f5cc5SPaul E. McKenney * online will get a splat. 1996d185af30SYacine Belkadi * 1997d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1998c1a220e7SZhang Rui */ 1999d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2000d4283e93STejun Heo struct work_struct *work) 2001c1a220e7SZhang Rui { 2002d4283e93STejun Heo bool ret = false; 20038930cabaSTejun Heo unsigned long flags; 20048930cabaSTejun Heo 20058930cabaSTejun Heo local_irq_save(flags); 2006c1a220e7SZhang Rui 200722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 20084690c4abSTejun Heo __queue_work(cpu, wq, work); 2009d4283e93STejun Heo ret = true; 2010c1a220e7SZhang Rui } 20118930cabaSTejun Heo 20128930cabaSTejun Heo local_irq_restore(flags); 2013c1a220e7SZhang Rui return ret; 2014c1a220e7SZhang Rui } 2015ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2016c1a220e7SZhang Rui 20178204e0c1SAlexander Duyck /** 2018fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 20198204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 20208204e0c1SAlexander Duyck * 20218204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 20228204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 20238204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 20248204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 20258204e0c1SAlexander Duyck */ 2026fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 20278204e0c1SAlexander Duyck { 20288204e0c1SAlexander Duyck int cpu; 20298204e0c1SAlexander Duyck 20308204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 20318204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 20328204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 20338204e0c1SAlexander Duyck 20348204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 20358204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 20368204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 20378204e0c1SAlexander Duyck return cpu; 20388204e0c1SAlexander Duyck 20398204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 20408204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 20418204e0c1SAlexander Duyck 20428204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 20438204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 20448204e0c1SAlexander Duyck } 20458204e0c1SAlexander Duyck 20468204e0c1SAlexander Duyck /** 20478204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 20488204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 20498204e0c1SAlexander Duyck * @wq: workqueue to use 20508204e0c1SAlexander Duyck * @work: work to queue 20518204e0c1SAlexander Duyck * 20528204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 20538204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 20548204e0c1SAlexander Duyck * NUMA node. 20558204e0c1SAlexander Duyck * 20568204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 20578204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 20588204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 20598204e0c1SAlexander Duyck * 20608204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 20618204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 20628204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 20638204e0c1SAlexander Duyck * 20648204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 20658204e0c1SAlexander Duyck */ 20668204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 20678204e0c1SAlexander Duyck struct work_struct *work) 20688204e0c1SAlexander Duyck { 20698204e0c1SAlexander Duyck unsigned long flags; 20708204e0c1SAlexander Duyck bool ret = false; 20718204e0c1SAlexander Duyck 20728204e0c1SAlexander Duyck /* 20738204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 20748204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 20758204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 20768204e0c1SAlexander Duyck * 20778204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 20788204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 20798204e0c1SAlexander Duyck * some round robin type logic. 20808204e0c1SAlexander Duyck */ 20818204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 20828204e0c1SAlexander Duyck 20838204e0c1SAlexander Duyck local_irq_save(flags); 20848204e0c1SAlexander Duyck 20858204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2086fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 20878204e0c1SAlexander Duyck 20888204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 20898204e0c1SAlexander Duyck ret = true; 20908204e0c1SAlexander Duyck } 20918204e0c1SAlexander Duyck 20928204e0c1SAlexander Duyck local_irq_restore(flags); 20938204e0c1SAlexander Duyck return ret; 20948204e0c1SAlexander Duyck } 20958204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 20968204e0c1SAlexander Duyck 20978c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 20981da177e4SLinus Torvalds { 20998c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 21001da177e4SLinus Torvalds 2101e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 210260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 21031da177e4SLinus Torvalds } 21041438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 21051da177e4SLinus Torvalds 21067beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 210752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 21081da177e4SLinus Torvalds { 21097beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 21107beb2edfSTejun Heo struct work_struct *work = &dwork->work; 21111da177e4SLinus Torvalds 2112637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 21134b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2114fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2115fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 21167beb2edfSTejun Heo 21178852aac2STejun Heo /* 21188852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 21198852aac2STejun Heo * both optimization and correctness. The earliest @timer can 21208852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 21218852aac2STejun Heo * on that there's no such delay when @delay is 0. 21228852aac2STejun Heo */ 21238852aac2STejun Heo if (!delay) { 21248852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 21258852aac2STejun Heo return; 21268852aac2STejun Heo } 21278852aac2STejun Heo 212860c057bcSLai Jiangshan dwork->wq = wq; 21291265057fSTejun Heo dwork->cpu = cpu; 21307beb2edfSTejun Heo timer->expires = jiffies + delay; 21317beb2edfSTejun Heo 2132041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 21337beb2edfSTejun Heo add_timer_on(timer, cpu); 2134041bd12eSTejun Heo else 2135041bd12eSTejun Heo add_timer(timer); 21367beb2edfSTejun Heo } 21371da177e4SLinus Torvalds 21380fcb78c2SRolf Eike Beer /** 21390fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 21400fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 21410fcb78c2SRolf Eike Beer * @wq: workqueue to use 2142af9997e4SRandy Dunlap * @dwork: work to queue 21430fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 21440fcb78c2SRolf Eike Beer * 2145d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2146715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2147715f1300STejun Heo * execution. 21480fcb78c2SRolf Eike Beer */ 2149d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 215052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 21517a6bc1cdSVenkatesh Pallipadi { 215252bad64dSDavid Howells struct work_struct *work = &dwork->work; 2153d4283e93STejun Heo bool ret = false; 21548930cabaSTejun Heo unsigned long flags; 21558930cabaSTejun Heo 21568930cabaSTejun Heo /* read the comment in __queue_work() */ 21578930cabaSTejun Heo local_irq_save(flags); 21587a6bc1cdSVenkatesh Pallipadi 215922df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 21607beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2161d4283e93STejun Heo ret = true; 21627a6bc1cdSVenkatesh Pallipadi } 21638930cabaSTejun Heo 21648930cabaSTejun Heo local_irq_restore(flags); 21657a6bc1cdSVenkatesh Pallipadi return ret; 21667a6bc1cdSVenkatesh Pallipadi } 2167ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 21681da177e4SLinus Torvalds 2169c8e55f36STejun Heo /** 21708376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 21718376fe22STejun Heo * @cpu: CPU number to execute work on 21728376fe22STejun Heo * @wq: workqueue to use 21738376fe22STejun Heo * @dwork: work to queue 21748376fe22STejun Heo * @delay: number of jiffies to wait before queueing 21758376fe22STejun Heo * 21768376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 21778376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 21788376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 21798376fe22STejun Heo * current state. 21808376fe22STejun Heo * 2181d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 21828376fe22STejun Heo * pending and its timer was modified. 21838376fe22STejun Heo * 2184e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 21858376fe22STejun Heo * See try_to_grab_pending() for details. 21868376fe22STejun Heo */ 21878376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 21888376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 21898376fe22STejun Heo { 21908376fe22STejun Heo unsigned long flags; 21918376fe22STejun Heo int ret; 21928376fe22STejun Heo 21938376fe22STejun Heo do { 21948376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 21958376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 21968376fe22STejun Heo 21978376fe22STejun Heo if (likely(ret >= 0)) { 21988376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 21998376fe22STejun Heo local_irq_restore(flags); 22008376fe22STejun Heo } 22018376fe22STejun Heo 22028376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 22038376fe22STejun Heo return ret; 22048376fe22STejun Heo } 22058376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 22068376fe22STejun Heo 220705f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 220805f0fe6bSTejun Heo { 220905f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 221005f0fe6bSTejun Heo 221105f0fe6bSTejun Heo /* read the comment in __queue_work() */ 221205f0fe6bSTejun Heo local_irq_disable(); 221305f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 221405f0fe6bSTejun Heo local_irq_enable(); 221505f0fe6bSTejun Heo } 221605f0fe6bSTejun Heo 221705f0fe6bSTejun Heo /** 221805f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 221905f0fe6bSTejun Heo * @wq: workqueue to use 222005f0fe6bSTejun Heo * @rwork: work to queue 222105f0fe6bSTejun Heo * 222205f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 222305f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2224bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 222505f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 222605f0fe6bSTejun Heo */ 222705f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 222805f0fe6bSTejun Heo { 222905f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 223005f0fe6bSTejun Heo 223105f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 223205f0fe6bSTejun Heo rwork->wq = wq; 2233a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 223405f0fe6bSTejun Heo return true; 223505f0fe6bSTejun Heo } 223605f0fe6bSTejun Heo 223705f0fe6bSTejun Heo return false; 223805f0fe6bSTejun Heo } 223905f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 224005f0fe6bSTejun Heo 2241f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2242c34056a3STejun Heo { 2243c34056a3STejun Heo struct worker *worker; 2244c34056a3STejun Heo 2245f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2246c8e55f36STejun Heo if (worker) { 2247c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2248affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2249da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2250e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2251e22bee78STejun Heo worker->flags = WORKER_PREP; 2252c8e55f36STejun Heo } 2253c34056a3STejun Heo return worker; 2254c34056a3STejun Heo } 2255c34056a3STejun Heo 22569546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 22579546b29eSTejun Heo { 22588639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 22599546b29eSTejun Heo return pool->attrs->__pod_cpumask; 22608639ecebSTejun Heo else 22618639ecebSTejun Heo return pool->attrs->cpumask; 22629546b29eSTejun Heo } 22639546b29eSTejun Heo 2264c34056a3STejun Heo /** 22654736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 22664736cbf7SLai Jiangshan * @worker: worker to be attached 22674736cbf7SLai Jiangshan * @pool: the target pool 22684736cbf7SLai Jiangshan * 22694736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 22704736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 22714736cbf7SLai Jiangshan * cpu-[un]hotplugs. 22724736cbf7SLai Jiangshan */ 22734736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 22744736cbf7SLai Jiangshan struct worker_pool *pool) 22754736cbf7SLai Jiangshan { 22761258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 22774736cbf7SLai Jiangshan 22784736cbf7SLai Jiangshan /* 22791258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 22801258fae7STejun Heo * stable across this function. See the comments above the flag 22811258fae7STejun Heo * definition for details. 22824736cbf7SLai Jiangshan */ 22834736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 22844736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 22855c25b5ffSPeter Zijlstra else 22865c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 22874736cbf7SLai Jiangshan 2288640f17c8SPeter Zijlstra if (worker->rescue_wq) 22899546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2290640f17c8SPeter Zijlstra 22914736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2292a2d812a2STejun Heo worker->pool = pool; 22934736cbf7SLai Jiangshan 22941258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 22954736cbf7SLai Jiangshan } 22964736cbf7SLai Jiangshan 22974736cbf7SLai Jiangshan /** 229860f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 229960f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 230060f5a4bcSLai Jiangshan * 23014736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 23024736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 23034736cbf7SLai Jiangshan * other reference to the pool. 230460f5a4bcSLai Jiangshan */ 2305a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 230660f5a4bcSLai Jiangshan { 2307a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 230860f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 230960f5a4bcSLai Jiangshan 23101258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2311a2d812a2STejun Heo 23125c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2313da028469SLai Jiangshan list_del(&worker->node); 2314a2d812a2STejun Heo worker->pool = NULL; 2315a2d812a2STejun Heo 2316e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 231760f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 23181258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 231960f5a4bcSLai Jiangshan 2320b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2321b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2322b62c0751SLai Jiangshan 232360f5a4bcSLai Jiangshan if (detach_completion) 232460f5a4bcSLai Jiangshan complete(detach_completion); 232560f5a4bcSLai Jiangshan } 232660f5a4bcSLai Jiangshan 232760f5a4bcSLai Jiangshan /** 2328c34056a3STejun Heo * create_worker - create a new workqueue worker 232963d95a91STejun Heo * @pool: pool the new worker will belong to 2330c34056a3STejun Heo * 2331051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2332c34056a3STejun Heo * 2333c34056a3STejun Heo * CONTEXT: 2334c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2335c34056a3STejun Heo * 2336d185af30SYacine Belkadi * Return: 2337c34056a3STejun Heo * Pointer to the newly created worker. 2338c34056a3STejun Heo */ 2339bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2340c34056a3STejun Heo { 2341e441b56fSZhen Lei struct worker *worker; 2342e441b56fSZhen Lei int id; 23435d9c7a1eSLucy Mielke char id_buf[23]; 2344c34056a3STejun Heo 23457cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2346e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 23473f0ea0b8SPetr Mladek if (id < 0) { 23483f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 23493f0ea0b8SPetr Mladek ERR_PTR(id)); 2350e441b56fSZhen Lei return NULL; 23513f0ea0b8SPetr Mladek } 2352c34056a3STejun Heo 2353f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 23543f0ea0b8SPetr Mladek if (!worker) { 23553f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2356c34056a3STejun Heo goto fail; 23573f0ea0b8SPetr Mladek } 2358c34056a3STejun Heo 2359c34056a3STejun Heo worker->id = id; 2360c34056a3STejun Heo 236129c91e99STejun Heo if (pool->cpu >= 0) 2362e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2363e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2364f3421797STejun Heo else 2365e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2366e3c916a4STejun Heo 2367f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2368e3c916a4STejun Heo "kworker/%s", id_buf); 23693f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 237060f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 237160f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 237260f54038SPetr Mladek id_buf); 237360f54038SPetr Mladek } else { 23743f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 23753f0ea0b8SPetr Mladek worker->task); 237660f54038SPetr Mladek } 2377c34056a3STejun Heo goto fail; 23783f0ea0b8SPetr Mladek } 2379c34056a3STejun Heo 238091151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 23819546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 238291151228SOleg Nesterov 2383da028469SLai Jiangshan /* successful, attach the worker to the pool */ 23844736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2385822d8405STejun Heo 2386051e1850SLai Jiangshan /* start the newly created worker */ 2387a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 23880219a352STejun Heo 2389051e1850SLai Jiangshan worker->pool->nr_workers++; 2390051e1850SLai Jiangshan worker_enter_idle(worker); 23910219a352STejun Heo 23920219a352STejun Heo /* 23930219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 23946a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 23956a229b0eSTejun Heo * wake it up explicitly. 23960219a352STejun Heo */ 2397051e1850SLai Jiangshan wake_up_process(worker->task); 23980219a352STejun Heo 2399a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2400051e1850SLai Jiangshan 2401c34056a3STejun Heo return worker; 2402822d8405STejun Heo 2403c34056a3STejun Heo fail: 2404e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2405c34056a3STejun Heo kfree(worker); 2406c34056a3STejun Heo return NULL; 2407c34056a3STejun Heo } 2408c34056a3STejun Heo 2409793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2410793777bcSValentin Schneider { 2411793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2412793777bcSValentin Schneider 2413793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2414793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2415793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2416793777bcSValentin Schneider else 2417793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2418793777bcSValentin Schneider } 2419793777bcSValentin Schneider 2420e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2421e02b9312SValentin Schneider { 2422e02b9312SValentin Schneider struct worker *worker, *tmp; 2423e02b9312SValentin Schneider 2424e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2425e02b9312SValentin Schneider list_del_init(&worker->entry); 2426e02b9312SValentin Schneider unbind_worker(worker); 2427e02b9312SValentin Schneider /* 2428e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2429e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2430e02b9312SValentin Schneider * wouldn't have gotten here. 2431c34056a3STejun Heo * 2432e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2433e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2434e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2435e02b9312SValentin Schneider * outside of pool->lock. 2436e02b9312SValentin Schneider */ 2437e02b9312SValentin Schneider wake_up_process(worker->task); 2438e02b9312SValentin Schneider } 2439e02b9312SValentin Schneider } 2440e02b9312SValentin Schneider 2441e02b9312SValentin Schneider /** 2442e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2443e02b9312SValentin Schneider * @worker: worker to be destroyed 2444e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2445e02b9312SValentin Schneider * 2446e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2447e02b9312SValentin Schneider * should be idle. 2448c8e55f36STejun Heo * 2449c8e55f36STejun Heo * CONTEXT: 2450a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2451c34056a3STejun Heo */ 2452e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2453c34056a3STejun Heo { 2454bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2455c34056a3STejun Heo 2456cd549687STejun Heo lockdep_assert_held(&pool->lock); 2457e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2458cd549687STejun Heo 2459c34056a3STejun Heo /* sanity check frenzy */ 24606183c009STejun Heo if (WARN_ON(worker->current_work) || 246173eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 246273eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 24636183c009STejun Heo return; 2464c34056a3STejun Heo 2465bd7bdd43STejun Heo pool->nr_workers--; 2466bd7bdd43STejun Heo pool->nr_idle--; 2467c8e55f36STejun Heo 2468cb444766STejun Heo worker->flags |= WORKER_DIE; 2469e02b9312SValentin Schneider 2470e02b9312SValentin Schneider list_move(&worker->entry, list); 2471e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2472c34056a3STejun Heo } 2473c34056a3STejun Heo 24743f959aa3SValentin Schneider /** 24753f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 24763f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 24773f959aa3SValentin Schneider * 24783f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 24793f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 24803f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 24813f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 24823f959aa3SValentin Schneider * it expire and re-evaluate things from there. 24833f959aa3SValentin Schneider */ 248432a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2485e22bee78STejun Heo { 248632a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 24873f959aa3SValentin Schneider bool do_cull = false; 24883f959aa3SValentin Schneider 24893f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 24903f959aa3SValentin Schneider return; 24913f959aa3SValentin Schneider 24923f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 24933f959aa3SValentin Schneider 24943f959aa3SValentin Schneider if (too_many_workers(pool)) { 24953f959aa3SValentin Schneider struct worker *worker; 24963f959aa3SValentin Schneider unsigned long expires; 24973f959aa3SValentin Schneider 24983f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 24993f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 25003f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 25013f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 25023f959aa3SValentin Schneider 25033f959aa3SValentin Schneider if (!do_cull) 25043f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 25053f959aa3SValentin Schneider } 25063f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 25073f959aa3SValentin Schneider 25083f959aa3SValentin Schneider if (do_cull) 25093f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 25103f959aa3SValentin Schneider } 25113f959aa3SValentin Schneider 25123f959aa3SValentin Schneider /** 25133f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 25143f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 25153f959aa3SValentin Schneider * 25163f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 25173f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2518e02b9312SValentin Schneider * 2519e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2520e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2521e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 25223f959aa3SValentin Schneider */ 25233f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 25243f959aa3SValentin Schneider { 25253f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 25269680540cSYang Yingliang LIST_HEAD(cull_list); 2527e22bee78STejun Heo 2528e02b9312SValentin Schneider /* 2529e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2530e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2531e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2532e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2533e02b9312SValentin Schneider */ 2534e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2535a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2536e22bee78STejun Heo 25373347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2538e22bee78STejun Heo struct worker *worker; 2539e22bee78STejun Heo unsigned long expires; 2540e22bee78STejun Heo 254163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2542e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2543e22bee78STejun Heo 25443347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 254563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 25463347fc9fSLai Jiangshan break; 2547e22bee78STejun Heo } 25483347fc9fSLai Jiangshan 2549e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2550e22bee78STejun Heo } 2551e22bee78STejun Heo 2552a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2553e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2554e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2555e22bee78STejun Heo } 2556e22bee78STejun Heo 2557493a1724STejun Heo static void send_mayday(struct work_struct *work) 2558e22bee78STejun Heo { 2559112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2560112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2561493a1724STejun Heo 25622e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2563e22bee78STejun Heo 2564493008a8STejun Heo if (!wq->rescuer) 2565493a1724STejun Heo return; 2566e22bee78STejun Heo 2567e22bee78STejun Heo /* mayday mayday mayday */ 2568493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 256977668c8bSLai Jiangshan /* 257077668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 257177668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 257277668c8bSLai Jiangshan * rescuer is done with it. 257377668c8bSLai Jiangshan */ 257477668c8bSLai Jiangshan get_pwq(pwq); 2575493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2576e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2577725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2578493a1724STejun Heo } 2579e22bee78STejun Heo } 2580e22bee78STejun Heo 258132a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2582e22bee78STejun Heo { 258332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2584e22bee78STejun Heo struct work_struct *work; 2585e22bee78STejun Heo 2586a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2587a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2588e22bee78STejun Heo 258963d95a91STejun Heo if (need_to_create_worker(pool)) { 2590e22bee78STejun Heo /* 2591e22bee78STejun Heo * We've been trying to create a new worker but 2592e22bee78STejun Heo * haven't been successful. We might be hitting an 2593e22bee78STejun Heo * allocation deadlock. Send distress signals to 2594e22bee78STejun Heo * rescuers. 2595e22bee78STejun Heo */ 259663d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2597e22bee78STejun Heo send_mayday(work); 2598e22bee78STejun Heo } 2599e22bee78STejun Heo 2600a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2601a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2602e22bee78STejun Heo 260363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2604e22bee78STejun Heo } 2605e22bee78STejun Heo 2606e22bee78STejun Heo /** 2607e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 260863d95a91STejun Heo * @pool: pool to create a new worker for 2609e22bee78STejun Heo * 261063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2611e22bee78STejun Heo * have at least one idle worker on return from this function. If 2612e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 261363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2614e22bee78STejun Heo * possible allocation deadlock. 2615e22bee78STejun Heo * 2616c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2617c5aa87bbSTejun Heo * may_start_working() %true. 2618e22bee78STejun Heo * 2619e22bee78STejun Heo * LOCKING: 2620a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2621e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2622e22bee78STejun Heo * manager. 2623e22bee78STejun Heo */ 262429187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2625d565ed63STejun Heo __releases(&pool->lock) 2626d565ed63STejun Heo __acquires(&pool->lock) 2627e22bee78STejun Heo { 2628e22bee78STejun Heo restart: 2629a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 26309f9c2364STejun Heo 2631e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 263263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2633e22bee78STejun Heo 2634e22bee78STejun Heo while (true) { 2635051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2636e22bee78STejun Heo break; 2637e22bee78STejun Heo 2638e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 26399f9c2364STejun Heo 264063d95a91STejun Heo if (!need_to_create_worker(pool)) 2641e22bee78STejun Heo break; 2642e22bee78STejun Heo } 2643e22bee78STejun Heo 264463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2645a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2646051e1850SLai Jiangshan /* 2647051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2648051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2649051e1850SLai Jiangshan * already become busy. 2650051e1850SLai Jiangshan */ 265163d95a91STejun Heo if (need_to_create_worker(pool)) 2652e22bee78STejun Heo goto restart; 2653e22bee78STejun Heo } 2654e22bee78STejun Heo 2655e22bee78STejun Heo /** 2656e22bee78STejun Heo * manage_workers - manage worker pool 2657e22bee78STejun Heo * @worker: self 2658e22bee78STejun Heo * 2659706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2660e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2661706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2662e22bee78STejun Heo * 2663e22bee78STejun Heo * The caller can safely start processing works on false return. On 2664e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2665e22bee78STejun Heo * and may_start_working() is true. 2666e22bee78STejun Heo * 2667e22bee78STejun Heo * CONTEXT: 2668a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2669e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2670e22bee78STejun Heo * 2671d185af30SYacine Belkadi * Return: 267229187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 267329187a9eSTejun Heo * start processing works, %true if management function was performed and 267429187a9eSTejun Heo * the conditions that the caller verified before calling the function may 267529187a9eSTejun Heo * no longer be true. 2676e22bee78STejun Heo */ 2677e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2678e22bee78STejun Heo { 267963d95a91STejun Heo struct worker_pool *pool = worker->pool; 2680e22bee78STejun Heo 2681692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 268229187a9eSTejun Heo return false; 2683692b4825STejun Heo 2684692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 26852607d7a6STejun Heo pool->manager = worker; 2686e22bee78STejun Heo 268729187a9eSTejun Heo maybe_create_worker(pool); 2688e22bee78STejun Heo 26892607d7a6STejun Heo pool->manager = NULL; 2690692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2691d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 269229187a9eSTejun Heo return true; 2693e22bee78STejun Heo } 2694e22bee78STejun Heo 2695a62428c0STejun Heo /** 2696a62428c0STejun Heo * process_one_work - process single work 2697c34056a3STejun Heo * @worker: self 2698a62428c0STejun Heo * @work: work to process 2699a62428c0STejun Heo * 2700a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2701a62428c0STejun Heo * process a single work including synchronization against and 2702a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2703a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2704a62428c0STejun Heo * call this function to process a work. 2705a62428c0STejun Heo * 2706a62428c0STejun Heo * CONTEXT: 2707a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2708a62428c0STejun Heo */ 2709c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2710d565ed63STejun Heo __releases(&pool->lock) 2711d565ed63STejun Heo __acquires(&pool->lock) 27121da177e4SLinus Torvalds { 2713112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2714bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2715c4560c2cSLai Jiangshan unsigned long work_data; 27164e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 27174e6045f1SJohannes Berg /* 2718a62428c0STejun Heo * It is permissible to free the struct work_struct from 2719a62428c0STejun Heo * inside the function that is called from it, this we need to 2720a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2721a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2722a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 27234e6045f1SJohannes Berg */ 27244d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 27254d82a1deSPeter Zijlstra 27264d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 27274e6045f1SJohannes Berg #endif 2728807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 272985327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2730ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 273125511a47STejun Heo 27328930cabaSTejun Heo /* claim and dequeue */ 2733dc186ad7SThomas Gleixner debug_work_deactivate(work); 2734c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2735c34056a3STejun Heo worker->current_work = work; 2736a2c1c57bSTejun Heo worker->current_func = work->func; 2737112202d9STejun Heo worker->current_pwq = pwq; 2738616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2739c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2740d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 27417a22ad75STejun Heo 27428bf89593STejun Heo /* 27438bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 27448bf89593STejun Heo * overridden through set_worker_desc(). 27458bf89593STejun Heo */ 27468bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 27478bf89593STejun Heo 2748a62428c0STejun Heo list_del_init(&work->entry); 2749a62428c0STejun Heo 2750649027d7STejun Heo /* 2751228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2752228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2753228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2754228f1d00SLai Jiangshan * execution of the pending work items. 2755fb0e7bebSTejun Heo */ 2756616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2757228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2758fb0e7bebSTejun Heo 2759974271c4STejun Heo /* 27600219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 27610219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 27620219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 27630219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 2764974271c4STejun Heo */ 27650219a352STejun Heo kick_pool(pool); 2766974271c4STejun Heo 27678930cabaSTejun Heo /* 27687c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2769d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 277023657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 277123657bb1STejun Heo * disabled. 27728930cabaSTejun Heo */ 27737c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 27741da177e4SLinus Torvalds 2775fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 2776a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2777365970a1SDavid Howells 2778a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 27793295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2780e6f3faa7SPeter Zijlstra /* 2781f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2782f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2783e6f3faa7SPeter Zijlstra * 2784e6f3faa7SPeter Zijlstra * However, that would result in: 2785e6f3faa7SPeter Zijlstra * 2786e6f3faa7SPeter Zijlstra * A(W1) 2787e6f3faa7SPeter Zijlstra * WFC(C) 2788e6f3faa7SPeter Zijlstra * A(W1) 2789e6f3faa7SPeter Zijlstra * C(C) 2790e6f3faa7SPeter Zijlstra * 2791e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2792e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2793e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2794f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2795e6f3faa7SPeter Zijlstra * these locks. 2796e6f3faa7SPeter Zijlstra * 2797e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2798e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2799e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2800e6f3faa7SPeter Zijlstra */ 2801f52be570SPeter Zijlstra lockdep_invariant_state(true); 2802e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2803a2c1c57bSTejun Heo worker->current_func(work); 2804e36c886aSArjan van de Ven /* 2805e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2806e36c886aSArjan van de Ven * point will only record its address. 2807e36c886aSArjan van de Ven */ 28081c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2809725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 28103295f0efSIngo Molnar lock_map_release(&lockdep_map); 2811112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 28121da177e4SLinus Torvalds 28131a65a6d1SXuewen Yan if (unlikely(in_atomic() || lockdep_depth(current) > 0 || 28141a65a6d1SXuewen Yan rcu_preempt_depth() > 0)) { 28151a65a6d1SXuewen Yan pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d/%d\n" 2816d75f773cSSakari Ailus " last function: %ps\n", 28171a65a6d1SXuewen Yan current->comm, preempt_count(), rcu_preempt_depth(), 28181a65a6d1SXuewen Yan task_pid_nr(current), worker->current_func); 2819d5abe669SPeter Zijlstra debug_show_held_locks(current); 2820d5abe669SPeter Zijlstra dump_stack(); 2821d5abe669SPeter Zijlstra } 2822d5abe669SPeter Zijlstra 2823b22ce278STejun Heo /* 2824025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2825b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2826b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2827b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2828789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2829789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2830b22ce278STejun Heo */ 2831a7e6425eSPaul E. McKenney cond_resched(); 2832b22ce278STejun Heo 2833a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2834a62428c0STejun Heo 2835616db877STejun Heo /* 2836616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 2837616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 2838616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 2839616db877STejun Heo */ 2840fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2841fb0e7bebSTejun Heo 28421b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 28431b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 28441b69ac6bSJohannes Weiner 2845a62428c0STejun Heo /* we're done with it, release */ 284642f8570fSSasha Levin hash_del(&worker->hentry); 2847c34056a3STejun Heo worker->current_work = NULL; 2848a2c1c57bSTejun Heo worker->current_func = NULL; 2849112202d9STejun Heo worker->current_pwq = NULL; 2850d812796eSLai Jiangshan worker->current_color = INT_MAX; 2851dd6c3c54STejun Heo 2852dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2853c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 28541da177e4SLinus Torvalds } 28551da177e4SLinus Torvalds 2856affee4b2STejun Heo /** 2857affee4b2STejun Heo * process_scheduled_works - process scheduled works 2858affee4b2STejun Heo * @worker: self 2859affee4b2STejun Heo * 2860affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2861affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2862affee4b2STejun Heo * fetches a work from the top and executes it. 2863affee4b2STejun Heo * 2864affee4b2STejun Heo * CONTEXT: 2865a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2866affee4b2STejun Heo * multiple times. 2867affee4b2STejun Heo */ 2868affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 28691da177e4SLinus Torvalds { 2870c0ab017dSTejun Heo struct work_struct *work; 2871c0ab017dSTejun Heo bool first = true; 2872c0ab017dSTejun Heo 2873c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 2874c0ab017dSTejun Heo struct work_struct, entry))) { 2875c0ab017dSTejun Heo if (first) { 2876c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 2877c0ab017dSTejun Heo first = false; 2878c0ab017dSTejun Heo } 2879c34056a3STejun Heo process_one_work(worker, work); 2880a62428c0STejun Heo } 28811da177e4SLinus Torvalds } 28821da177e4SLinus Torvalds 2883197f6accSTejun Heo static void set_pf_worker(bool val) 2884197f6accSTejun Heo { 2885197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2886197f6accSTejun Heo if (val) 2887197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2888197f6accSTejun Heo else 2889197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2890197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2891197f6accSTejun Heo } 2892197f6accSTejun Heo 28934690c4abSTejun Heo /** 28944690c4abSTejun Heo * worker_thread - the worker thread function 2895c34056a3STejun Heo * @__worker: self 28964690c4abSTejun Heo * 2897c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2898c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2899c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2900c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2901c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2902d185af30SYacine Belkadi * 2903d185af30SYacine Belkadi * Return: 0 29044690c4abSTejun Heo */ 2905c34056a3STejun Heo static int worker_thread(void *__worker) 29061da177e4SLinus Torvalds { 2907c34056a3STejun Heo struct worker *worker = __worker; 2908bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 29091da177e4SLinus Torvalds 2910e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2911197f6accSTejun Heo set_pf_worker(true); 2912c8e55f36STejun Heo woke_up: 2913a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2914affee4b2STejun Heo 2915a9ab775bSTejun Heo /* am I supposed to die? */ 2916a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2917a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2918197f6accSTejun Heo set_pf_worker(false); 291960f5a4bcSLai Jiangshan 292060f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2921e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2922a2d812a2STejun Heo worker_detach_from_pool(worker); 2923e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 292460f5a4bcSLai Jiangshan kfree(worker); 2925c8e55f36STejun Heo return 0; 2926c8e55f36STejun Heo } 2927c8e55f36STejun Heo 2928c8e55f36STejun Heo worker_leave_idle(worker); 2929db7bccf4STejun Heo recheck: 2930e22bee78STejun Heo /* no more worker necessary? */ 293163d95a91STejun Heo if (!need_more_worker(pool)) 2932e22bee78STejun Heo goto sleep; 2933e22bee78STejun Heo 2934e22bee78STejun Heo /* do we need to manage? */ 293563d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2936e22bee78STejun Heo goto recheck; 2937e22bee78STejun Heo 2938c8e55f36STejun Heo /* 2939c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2940c8e55f36STejun Heo * preparing to process a work or actually processing it. 2941c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2942c8e55f36STejun Heo */ 29436183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2944c8e55f36STejun Heo 2945e22bee78STejun Heo /* 2946a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2947a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2948a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2949a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2950a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2951e22bee78STejun Heo */ 2952a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2953e22bee78STejun Heo 2954e22bee78STejun Heo do { 2955affee4b2STejun Heo struct work_struct *work = 2956bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2957affee4b2STejun Heo struct work_struct, entry); 2958affee4b2STejun Heo 2959873eaca6STejun Heo if (assign_work(work, worker, NULL)) 2960affee4b2STejun Heo process_scheduled_works(worker); 296163d95a91STejun Heo } while (keep_working(pool)); 2962affee4b2STejun Heo 2963228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2964d313dd85STejun Heo sleep: 2965c8e55f36STejun Heo /* 2966d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2967d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2968d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2969d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2970d565ed63STejun Heo * event. 2971c8e55f36STejun Heo */ 2972c8e55f36STejun Heo worker_enter_idle(worker); 2973c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2974a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 29751da177e4SLinus Torvalds schedule(); 2976c8e55f36STejun Heo goto woke_up; 29771da177e4SLinus Torvalds } 29781da177e4SLinus Torvalds 2979e22bee78STejun Heo /** 2980e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2981111c225aSTejun Heo * @__rescuer: self 2982e22bee78STejun Heo * 2983e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2984493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2985e22bee78STejun Heo * 2986706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2987e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2988e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2989e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2990e22bee78STejun Heo * the problem rescuer solves. 2991e22bee78STejun Heo * 2992706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2993706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2994e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2995e22bee78STejun Heo * 2996e22bee78STejun Heo * This should happen rarely. 2997d185af30SYacine Belkadi * 2998d185af30SYacine Belkadi * Return: 0 2999e22bee78STejun Heo */ 3000111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3001e22bee78STejun Heo { 3002111c225aSTejun Heo struct worker *rescuer = __rescuer; 3003111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 30044d595b86SLai Jiangshan bool should_stop; 3005e22bee78STejun Heo 3006e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3007111c225aSTejun Heo 3008111c225aSTejun Heo /* 3009111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3010111c225aSTejun Heo * doesn't participate in concurrency management. 3011111c225aSTejun Heo */ 3012197f6accSTejun Heo set_pf_worker(true); 3013e22bee78STejun Heo repeat: 3014c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 30151da177e4SLinus Torvalds 30164d595b86SLai Jiangshan /* 30174d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 30184d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 30194d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 30204d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 30214d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 30224d595b86SLai Jiangshan * list is always empty on exit. 30234d595b86SLai Jiangshan */ 30244d595b86SLai Jiangshan should_stop = kthread_should_stop(); 30251da177e4SLinus Torvalds 3026493a1724STejun Heo /* see whether any pwq is asking for help */ 3027a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3028493a1724STejun Heo 3029493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3030493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3031493a1724STejun Heo struct pool_workqueue, mayday_node); 3032112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3033e22bee78STejun Heo struct work_struct *work, *n; 3034e22bee78STejun Heo 3035e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3036493a1724STejun Heo list_del_init(&pwq->mayday_node); 3037493a1724STejun Heo 3038a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3039e22bee78STejun Heo 304051697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 304151697d39SLai Jiangshan 3042a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3043e22bee78STejun Heo 3044e22bee78STejun Heo /* 3045e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3046e22bee78STejun Heo * process'em. 3047e22bee78STejun Heo */ 3048873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 304982607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3050873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3051873eaca6STejun Heo assign_work(work, rescuer, &n)) 3052725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 305382607adcSTejun Heo } 3054e22bee78STejun Heo 3055873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3056e22bee78STejun Heo process_scheduled_works(rescuer); 30577576958aSTejun Heo 30587576958aSTejun Heo /* 3059008847f6SNeilBrown * The above execution of rescued work items could 3060008847f6SNeilBrown * have created more to rescue through 3061f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3062008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3063008847f6SNeilBrown * that such back-to-back work items, which may be 3064008847f6SNeilBrown * being used to relieve memory pressure, don't 3065008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3066008847f6SNeilBrown */ 30674f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3068a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3069e66b39afSTejun Heo /* 3070e66b39afSTejun Heo * Queue iff we aren't racing destruction 3071e66b39afSTejun Heo * and somebody else hasn't queued it already. 3072e66b39afSTejun Heo */ 3073e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3074008847f6SNeilBrown get_pwq(pwq); 3075e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3076e66b39afSTejun Heo } 3077a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3078008847f6SNeilBrown } 3079008847f6SNeilBrown } 3080008847f6SNeilBrown 3081008847f6SNeilBrown /* 308277668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 308313b1d625SLai Jiangshan * go away while we're still attached to it. 308477668c8bSLai Jiangshan */ 308577668c8bSLai Jiangshan put_pwq(pwq); 308677668c8bSLai Jiangshan 308777668c8bSLai Jiangshan /* 30880219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 30890219a352STejun Heo * with 0 concurrency and stalling the execution. 30907576958aSTejun Heo */ 30910219a352STejun Heo kick_pool(pool); 30927576958aSTejun Heo 3093a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 309413b1d625SLai Jiangshan 3095a2d812a2STejun Heo worker_detach_from_pool(rescuer); 309613b1d625SLai Jiangshan 3097a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 30981da177e4SLinus Torvalds } 30991da177e4SLinus Torvalds 3100a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3101493a1724STejun Heo 31024d595b86SLai Jiangshan if (should_stop) { 31034d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3104197f6accSTejun Heo set_pf_worker(false); 31054d595b86SLai Jiangshan return 0; 31064d595b86SLai Jiangshan } 31074d595b86SLai Jiangshan 3108111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3109111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3110e22bee78STejun Heo schedule(); 3111e22bee78STejun Heo goto repeat; 31121da177e4SLinus Torvalds } 31131da177e4SLinus Torvalds 3114fca839c0STejun Heo /** 3115fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3116fca839c0STejun Heo * @target_wq: workqueue being flushed 3117fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3118fca839c0STejun Heo * 3119fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3120fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3121fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3122fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3123fca839c0STejun Heo * a deadlock. 3124fca839c0STejun Heo */ 3125fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3126fca839c0STejun Heo struct work_struct *target_work) 3127fca839c0STejun Heo { 3128fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3129fca839c0STejun Heo struct worker *worker; 3130fca839c0STejun Heo 3131fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3132fca839c0STejun Heo return; 3133fca839c0STejun Heo 3134fca839c0STejun Heo worker = current_wq_worker(); 3135fca839c0STejun Heo 3136fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3137d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3138fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 313923d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 314023d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3141d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3142fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3143fca839c0STejun Heo target_wq->name, target_func); 3144fca839c0STejun Heo } 3145fca839c0STejun Heo 3146fc2e4d70SOleg Nesterov struct wq_barrier { 3147fc2e4d70SOleg Nesterov struct work_struct work; 3148fc2e4d70SOleg Nesterov struct completion done; 31492607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3150fc2e4d70SOleg Nesterov }; 3151fc2e4d70SOleg Nesterov 3152fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3153fc2e4d70SOleg Nesterov { 3154fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3155fc2e4d70SOleg Nesterov complete(&barr->done); 3156fc2e4d70SOleg Nesterov } 3157fc2e4d70SOleg Nesterov 31584690c4abSTejun Heo /** 31594690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3160112202d9STejun Heo * @pwq: pwq to insert barrier into 31614690c4abSTejun Heo * @barr: wq_barrier to insert 3162affee4b2STejun Heo * @target: target work to attach @barr to 3163affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 31644690c4abSTejun Heo * 3165affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3166affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3167affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3168affee4b2STejun Heo * cpu. 3169affee4b2STejun Heo * 3170affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3171affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3172affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3173affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3174affee4b2STejun Heo * after a work with LINKED flag set. 3175affee4b2STejun Heo * 3176affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3177112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 31784690c4abSTejun Heo * 31794690c4abSTejun Heo * CONTEXT: 3180a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 31814690c4abSTejun Heo */ 3182112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3183affee4b2STejun Heo struct wq_barrier *barr, 3184affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3185fc2e4d70SOleg Nesterov { 3186d812796eSLai Jiangshan unsigned int work_flags = 0; 3187d812796eSLai Jiangshan unsigned int work_color; 3188affee4b2STejun Heo struct list_head *head; 3189affee4b2STejun Heo 3190dc186ad7SThomas Gleixner /* 3191d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3192dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3193dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3194dc186ad7SThomas Gleixner * might deadlock. 3195dc186ad7SThomas Gleixner */ 3196ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 319722df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 319852fa5bc5SBoqun Feng 3199fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3200fd1a5b04SByungchul Park 32012607d7a6STejun Heo barr->task = current; 320283c22520SOleg Nesterov 3203018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 3204018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3205018f3a13SLai Jiangshan 3206affee4b2STejun Heo /* 3207affee4b2STejun Heo * If @target is currently being executed, schedule the 3208affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3209affee4b2STejun Heo */ 3210d812796eSLai Jiangshan if (worker) { 3211affee4b2STejun Heo head = worker->scheduled.next; 3212d812796eSLai Jiangshan work_color = worker->current_color; 3213d812796eSLai Jiangshan } else { 3214affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3215affee4b2STejun Heo 3216affee4b2STejun Heo head = target->entry.next; 3217affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3218d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3219d812796eSLai Jiangshan work_color = get_work_color(*bits); 3220affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3221affee4b2STejun Heo } 3222affee4b2STejun Heo 3223d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3224d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3225d812796eSLai Jiangshan 3226d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3227fc2e4d70SOleg Nesterov } 3228fc2e4d70SOleg Nesterov 322973f53c4aSTejun Heo /** 3230112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 323173f53c4aSTejun Heo * @wq: workqueue being flushed 323273f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 323373f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 323473f53c4aSTejun Heo * 3235112202d9STejun Heo * Prepare pwqs for workqueue flushing. 323673f53c4aSTejun Heo * 3237112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3238112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3239112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3240112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3241112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 324273f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 324373f53c4aSTejun Heo * 324473f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 324573f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 324673f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 324773f53c4aSTejun Heo * is returned. 324873f53c4aSTejun Heo * 3249112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 325073f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 325173f53c4aSTejun Heo * advanced to @work_color. 325273f53c4aSTejun Heo * 325373f53c4aSTejun Heo * CONTEXT: 32543c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 325573f53c4aSTejun Heo * 3256d185af30SYacine Belkadi * Return: 325773f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 325873f53c4aSTejun Heo * otherwise. 325973f53c4aSTejun Heo */ 3260112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 326173f53c4aSTejun Heo int flush_color, int work_color) 32621da177e4SLinus Torvalds { 326373f53c4aSTejun Heo bool wait = false; 326449e3cf44STejun Heo struct pool_workqueue *pwq; 32651da177e4SLinus Torvalds 326673f53c4aSTejun Heo if (flush_color >= 0) { 32676183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3268112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3269dc186ad7SThomas Gleixner } 327014441960SOleg Nesterov 327149e3cf44STejun Heo for_each_pwq(pwq, wq) { 3272112202d9STejun Heo struct worker_pool *pool = pwq->pool; 32731da177e4SLinus Torvalds 3274a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 327573f53c4aSTejun Heo 327673f53c4aSTejun Heo if (flush_color >= 0) { 32776183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 327873f53c4aSTejun Heo 3279112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3280112202d9STejun Heo pwq->flush_color = flush_color; 3281112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 328273f53c4aSTejun Heo wait = true; 32831da177e4SLinus Torvalds } 328473f53c4aSTejun Heo } 328573f53c4aSTejun Heo 328673f53c4aSTejun Heo if (work_color >= 0) { 32876183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3288112202d9STejun Heo pwq->work_color = work_color; 328973f53c4aSTejun Heo } 329073f53c4aSTejun Heo 3291a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 32921da177e4SLinus Torvalds } 32931da177e4SLinus Torvalds 3294112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 329573f53c4aSTejun Heo complete(&wq->first_flusher->done); 329673f53c4aSTejun Heo 329773f53c4aSTejun Heo return wait; 329883c22520SOleg Nesterov } 32991da177e4SLinus Torvalds 33000fcb78c2SRolf Eike Beer /** 3301c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 33020fcb78c2SRolf Eike Beer * @wq: workqueue to flush 33031da177e4SLinus Torvalds * 3304c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3305c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 33061da177e4SLinus Torvalds */ 3307c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 33081da177e4SLinus Torvalds { 330973f53c4aSTejun Heo struct wq_flusher this_flusher = { 331073f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 331173f53c4aSTejun Heo .flush_color = -1, 3312fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 331373f53c4aSTejun Heo }; 331473f53c4aSTejun Heo int next_color; 3315b1f4ec17SOleg Nesterov 33163347fa09STejun Heo if (WARN_ON(!wq_online)) 33173347fa09STejun Heo return; 33183347fa09STejun Heo 331987915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 332087915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 332187915adcSJohannes Berg 33223c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 332373f53c4aSTejun Heo 332473f53c4aSTejun Heo /* 332573f53c4aSTejun Heo * Start-to-wait phase 332673f53c4aSTejun Heo */ 332773f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 332873f53c4aSTejun Heo 332973f53c4aSTejun Heo if (next_color != wq->flush_color) { 333073f53c4aSTejun Heo /* 333173f53c4aSTejun Heo * Color space is not full. The current work_color 333273f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 333373f53c4aSTejun Heo * by one. 333473f53c4aSTejun Heo */ 33356183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 333673f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 333773f53c4aSTejun Heo wq->work_color = next_color; 333873f53c4aSTejun Heo 333973f53c4aSTejun Heo if (!wq->first_flusher) { 334073f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 33416183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 334273f53c4aSTejun Heo 334373f53c4aSTejun Heo wq->first_flusher = &this_flusher; 334473f53c4aSTejun Heo 3345112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 334673f53c4aSTejun Heo wq->work_color)) { 334773f53c4aSTejun Heo /* nothing to flush, done */ 334873f53c4aSTejun Heo wq->flush_color = next_color; 334973f53c4aSTejun Heo wq->first_flusher = NULL; 335073f53c4aSTejun Heo goto out_unlock; 335173f53c4aSTejun Heo } 335273f53c4aSTejun Heo } else { 335373f53c4aSTejun Heo /* wait in queue */ 33546183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 335573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3356112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 335773f53c4aSTejun Heo } 335873f53c4aSTejun Heo } else { 335973f53c4aSTejun Heo /* 336073f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 336173f53c4aSTejun Heo * The next flush completion will assign us 336273f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 336373f53c4aSTejun Heo */ 336473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 336573f53c4aSTejun Heo } 336673f53c4aSTejun Heo 3367fca839c0STejun Heo check_flush_dependency(wq, NULL); 3368fca839c0STejun Heo 33693c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 337073f53c4aSTejun Heo 337173f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 337273f53c4aSTejun Heo 337373f53c4aSTejun Heo /* 337473f53c4aSTejun Heo * Wake-up-and-cascade phase 337573f53c4aSTejun Heo * 337673f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 337773f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 337873f53c4aSTejun Heo */ 337900d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 338073f53c4aSTejun Heo return; 338173f53c4aSTejun Heo 33823c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 338373f53c4aSTejun Heo 33844ce48b37STejun Heo /* we might have raced, check again with mutex held */ 33854ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 33864ce48b37STejun Heo goto out_unlock; 33874ce48b37STejun Heo 338800d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 338973f53c4aSTejun Heo 33906183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 33916183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 339273f53c4aSTejun Heo 339373f53c4aSTejun Heo while (true) { 339473f53c4aSTejun Heo struct wq_flusher *next, *tmp; 339573f53c4aSTejun Heo 339673f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 339773f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 339873f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 339973f53c4aSTejun Heo break; 340073f53c4aSTejun Heo list_del_init(&next->list); 340173f53c4aSTejun Heo complete(&next->done); 340273f53c4aSTejun Heo } 340373f53c4aSTejun Heo 34046183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 340573f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 340673f53c4aSTejun Heo 340773f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 340873f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 340973f53c4aSTejun Heo 341073f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 341173f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 341273f53c4aSTejun Heo /* 341373f53c4aSTejun Heo * Assign the same color to all overflowed 341473f53c4aSTejun Heo * flushers, advance work_color and append to 341573f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 341673f53c4aSTejun Heo * phase for these overflowed flushers. 341773f53c4aSTejun Heo */ 341873f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 341973f53c4aSTejun Heo tmp->flush_color = wq->work_color; 342073f53c4aSTejun Heo 342173f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 342273f53c4aSTejun Heo 342373f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 342473f53c4aSTejun Heo &wq->flusher_queue); 3425112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 342673f53c4aSTejun Heo } 342773f53c4aSTejun Heo 342873f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 34296183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 343073f53c4aSTejun Heo break; 343173f53c4aSTejun Heo } 343273f53c4aSTejun Heo 343373f53c4aSTejun Heo /* 343473f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3435112202d9STejun Heo * the new first flusher and arm pwqs. 343673f53c4aSTejun Heo */ 34376183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 34386183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 343973f53c4aSTejun Heo 344073f53c4aSTejun Heo list_del_init(&next->list); 344173f53c4aSTejun Heo wq->first_flusher = next; 344273f53c4aSTejun Heo 3443112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 344473f53c4aSTejun Heo break; 344573f53c4aSTejun Heo 344673f53c4aSTejun Heo /* 344773f53c4aSTejun Heo * Meh... this color is already done, clear first 344873f53c4aSTejun Heo * flusher and repeat cascading. 344973f53c4aSTejun Heo */ 345073f53c4aSTejun Heo wq->first_flusher = NULL; 345173f53c4aSTejun Heo } 345273f53c4aSTejun Heo 345373f53c4aSTejun Heo out_unlock: 34543c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 34551da177e4SLinus Torvalds } 3456c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 34571da177e4SLinus Torvalds 34589c5a2ba7STejun Heo /** 34599c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 34609c5a2ba7STejun Heo * @wq: workqueue to drain 34619c5a2ba7STejun Heo * 34629c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 34639c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 34649c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3465b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 34669c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 34679c5a2ba7STejun Heo * takes too long. 34689c5a2ba7STejun Heo */ 34699c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 34709c5a2ba7STejun Heo { 34719c5a2ba7STejun Heo unsigned int flush_cnt = 0; 347249e3cf44STejun Heo struct pool_workqueue *pwq; 34739c5a2ba7STejun Heo 34749c5a2ba7STejun Heo /* 34759c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 34769c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3477618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 34789c5a2ba7STejun Heo */ 347987fc741eSLai Jiangshan mutex_lock(&wq->mutex); 34809c5a2ba7STejun Heo if (!wq->nr_drainers++) 3481618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 348287fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 34839c5a2ba7STejun Heo reflush: 3484c4f135d6STetsuo Handa __flush_workqueue(wq); 34859c5a2ba7STejun Heo 3486b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 348776af4d93STejun Heo 348849e3cf44STejun Heo for_each_pwq(pwq, wq) { 3489fa2563e4SThomas Tuttle bool drained; 34909c5a2ba7STejun Heo 3491a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3492afa87ce8STejun Heo drained = pwq_is_empty(pwq); 3493a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3494fa2563e4SThomas Tuttle 3495fa2563e4SThomas Tuttle if (drained) 34969c5a2ba7STejun Heo continue; 34979c5a2ba7STejun Heo 34989c5a2ba7STejun Heo if (++flush_cnt == 10 || 34999c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3500e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3501e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 350276af4d93STejun Heo 3503b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 35049c5a2ba7STejun Heo goto reflush; 35059c5a2ba7STejun Heo } 35069c5a2ba7STejun Heo 35079c5a2ba7STejun Heo if (!--wq->nr_drainers) 3508618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 350987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 35109c5a2ba7STejun Heo } 35119c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 35129c5a2ba7STejun Heo 3513d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3514d6e89786SJohannes Berg bool from_cancel) 3515baf59022STejun Heo { 3516baf59022STejun Heo struct worker *worker = NULL; 3517c9e7cf27STejun Heo struct worker_pool *pool; 3518112202d9STejun Heo struct pool_workqueue *pwq; 3519baf59022STejun Heo 3520baf59022STejun Heo might_sleep(); 3521baf59022STejun Heo 352224acfb71SThomas Gleixner rcu_read_lock(); 3523fa1b54e6STejun Heo pool = get_work_pool(work); 3524fa1b54e6STejun Heo if (!pool) { 352524acfb71SThomas Gleixner rcu_read_unlock(); 3526fa1b54e6STejun Heo return false; 3527fa1b54e6STejun Heo } 3528fa1b54e6STejun Heo 3529a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 35300b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3531112202d9STejun Heo pwq = get_work_pwq(work); 3532112202d9STejun Heo if (pwq) { 3533112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3534baf59022STejun Heo goto already_gone; 3535606a5020STejun Heo } else { 3536c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3537baf59022STejun Heo if (!worker) 3538baf59022STejun Heo goto already_gone; 3539112202d9STejun Heo pwq = worker->current_pwq; 3540606a5020STejun Heo } 3541baf59022STejun Heo 3542fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3543fca839c0STejun Heo 3544112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3545a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3546baf59022STejun Heo 3547e159489bSTejun Heo /* 3548a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3549a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3550a1d14934SPeter Zijlstra * 3551a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3552a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3553a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3554a1d14934SPeter Zijlstra * forward progress. 3555e159489bSTejun Heo */ 3556d6e89786SJohannes Berg if (!from_cancel && 3557d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3558112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3559112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3560a1d14934SPeter Zijlstra } 356124acfb71SThomas Gleixner rcu_read_unlock(); 3562baf59022STejun Heo return true; 3563baf59022STejun Heo already_gone: 3564a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 356524acfb71SThomas Gleixner rcu_read_unlock(); 3566baf59022STejun Heo return false; 3567baf59022STejun Heo } 3568baf59022STejun Heo 3569d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3570d6e89786SJohannes Berg { 3571d6e89786SJohannes Berg struct wq_barrier barr; 3572d6e89786SJohannes Berg 3573d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3574d6e89786SJohannes Berg return false; 3575d6e89786SJohannes Berg 35764d43d395STetsuo Handa if (WARN_ON(!work->func)) 35774d43d395STetsuo Handa return false; 35784d43d395STetsuo Handa 357987915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 358087915adcSJohannes Berg lock_map_release(&work->lockdep_map); 358187915adcSJohannes Berg 3582d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3583d6e89786SJohannes Berg wait_for_completion(&barr.done); 3584d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3585d6e89786SJohannes Berg return true; 3586d6e89786SJohannes Berg } else { 3587d6e89786SJohannes Berg return false; 3588d6e89786SJohannes Berg } 3589d6e89786SJohannes Berg } 3590d6e89786SJohannes Berg 3591db700897SOleg Nesterov /** 3592401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3593401a8d04STejun Heo * @work: the work to flush 3594db700897SOleg Nesterov * 3595606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3596606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3597401a8d04STejun Heo * 3598d185af30SYacine Belkadi * Return: 3599401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3600401a8d04STejun Heo * %false if it was already idle. 3601db700897SOleg Nesterov */ 3602401a8d04STejun Heo bool flush_work(struct work_struct *work) 3603db700897SOleg Nesterov { 3604d6e89786SJohannes Berg return __flush_work(work, false); 3605606a5020STejun Heo } 3606db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3607db700897SOleg Nesterov 36088603e1b3STejun Heo struct cwt_wait { 3609ac6424b9SIngo Molnar wait_queue_entry_t wait; 36108603e1b3STejun Heo struct work_struct *work; 36118603e1b3STejun Heo }; 36128603e1b3STejun Heo 3613ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 36148603e1b3STejun Heo { 36158603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 36168603e1b3STejun Heo 36178603e1b3STejun Heo if (cwait->work != key) 36188603e1b3STejun Heo return 0; 36198603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 36208603e1b3STejun Heo } 36218603e1b3STejun Heo 362236e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3623401a8d04STejun Heo { 36248603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3625bbb68dfaSTejun Heo unsigned long flags; 36261f1f642eSOleg Nesterov int ret; 36271f1f642eSOleg Nesterov 36281f1f642eSOleg Nesterov do { 3629bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3630bbb68dfaSTejun Heo /* 36318603e1b3STejun Heo * If someone else is already canceling, wait for it to 36328603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 36338603e1b3STejun Heo * because we may get scheduled between @work's completion 36348603e1b3STejun Heo * and the other canceling task resuming and clearing 36358603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 36368603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 36378603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 36388603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 36398603e1b3STejun Heo * we're hogging the CPU. 36408603e1b3STejun Heo * 36418603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 36428603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 36438603e1b3STejun Heo * wake function which matches @work along with exclusive 36448603e1b3STejun Heo * wait and wakeup. 3645bbb68dfaSTejun Heo */ 36468603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 36478603e1b3STejun Heo struct cwt_wait cwait; 36488603e1b3STejun Heo 36498603e1b3STejun Heo init_wait(&cwait.wait); 36508603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 36518603e1b3STejun Heo cwait.work = work; 36528603e1b3STejun Heo 36538603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 36548603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 36558603e1b3STejun Heo if (work_is_canceling(work)) 36568603e1b3STejun Heo schedule(); 36578603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 36588603e1b3STejun Heo } 36591f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 36601f1f642eSOleg Nesterov 3661bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3662bbb68dfaSTejun Heo mark_work_canceling(work); 3663bbb68dfaSTejun Heo local_irq_restore(flags); 3664bbb68dfaSTejun Heo 36653347fa09STejun Heo /* 36663347fa09STejun Heo * This allows canceling during early boot. We know that @work 36673347fa09STejun Heo * isn't executing. 36683347fa09STejun Heo */ 36693347fa09STejun Heo if (wq_online) 3670d6e89786SJohannes Berg __flush_work(work, true); 36713347fa09STejun Heo 36727a22ad75STejun Heo clear_work_data(work); 36738603e1b3STejun Heo 36748603e1b3STejun Heo /* 36758603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 36768603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 36778603e1b3STejun Heo * visible there. 36788603e1b3STejun Heo */ 36798603e1b3STejun Heo smp_mb(); 36808603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 36818603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 36828603e1b3STejun Heo 36831f1f642eSOleg Nesterov return ret; 36841f1f642eSOleg Nesterov } 36851f1f642eSOleg Nesterov 36866e84d644SOleg Nesterov /** 3687401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3688401a8d04STejun Heo * @work: the work to cancel 36896e84d644SOleg Nesterov * 3690401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3691401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3692401a8d04STejun Heo * another workqueue. On return from this function, @work is 3693401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 36941f1f642eSOleg Nesterov * 3695401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3696401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 36976e84d644SOleg Nesterov * 3698401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 36996e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3700401a8d04STejun Heo * 3701d185af30SYacine Belkadi * Return: 3702401a8d04STejun Heo * %true if @work was pending, %false otherwise. 37036e84d644SOleg Nesterov */ 3704401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 37056e84d644SOleg Nesterov { 370636e227d2STejun Heo return __cancel_work_timer(work, false); 3707b89deed3SOleg Nesterov } 370828e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3709b89deed3SOleg Nesterov 37106e84d644SOleg Nesterov /** 3711401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3712401a8d04STejun Heo * @dwork: the delayed work to flush 37136e84d644SOleg Nesterov * 3714401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3715401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3716401a8d04STejun Heo * considers the last queueing instance of @dwork. 37171f1f642eSOleg Nesterov * 3718d185af30SYacine Belkadi * Return: 3719401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3720401a8d04STejun Heo * %false if it was already idle. 37216e84d644SOleg Nesterov */ 3722401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3723401a8d04STejun Heo { 37248930cabaSTejun Heo local_irq_disable(); 3725401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 372660c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 37278930cabaSTejun Heo local_irq_enable(); 3728401a8d04STejun Heo return flush_work(&dwork->work); 3729401a8d04STejun Heo } 3730401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3731401a8d04STejun Heo 373205f0fe6bSTejun Heo /** 373305f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 373405f0fe6bSTejun Heo * @rwork: the rcu work to flush 373505f0fe6bSTejun Heo * 373605f0fe6bSTejun Heo * Return: 373705f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 373805f0fe6bSTejun Heo * %false if it was already idle. 373905f0fe6bSTejun Heo */ 374005f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 374105f0fe6bSTejun Heo { 374205f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 374305f0fe6bSTejun Heo rcu_barrier(); 374405f0fe6bSTejun Heo flush_work(&rwork->work); 374505f0fe6bSTejun Heo return true; 374605f0fe6bSTejun Heo } else { 374705f0fe6bSTejun Heo return flush_work(&rwork->work); 374805f0fe6bSTejun Heo } 374905f0fe6bSTejun Heo } 375005f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 375105f0fe6bSTejun Heo 3752f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3753f72b8792SJens Axboe { 3754f72b8792SJens Axboe unsigned long flags; 3755f72b8792SJens Axboe int ret; 3756f72b8792SJens Axboe 3757f72b8792SJens Axboe do { 3758f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3759f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3760f72b8792SJens Axboe 3761f72b8792SJens Axboe if (unlikely(ret < 0)) 3762f72b8792SJens Axboe return false; 3763f72b8792SJens Axboe 3764f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3765f72b8792SJens Axboe local_irq_restore(flags); 3766f72b8792SJens Axboe return ret; 3767f72b8792SJens Axboe } 3768f72b8792SJens Axboe 376973b4b532SAndrey Grodzovsky /* 377073b4b532SAndrey Grodzovsky * See cancel_delayed_work() 377173b4b532SAndrey Grodzovsky */ 377273b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 377373b4b532SAndrey Grodzovsky { 377473b4b532SAndrey Grodzovsky return __cancel_work(work, false); 377573b4b532SAndrey Grodzovsky } 377673b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 377773b4b532SAndrey Grodzovsky 3778401a8d04STejun Heo /** 377957b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 378057b30ae7STejun Heo * @dwork: delayed_work to cancel 378109383498STejun Heo * 3782d185af30SYacine Belkadi * Kill off a pending delayed_work. 3783d185af30SYacine Belkadi * 3784d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3785d185af30SYacine Belkadi * pending. 3786d185af30SYacine Belkadi * 3787d185af30SYacine Belkadi * Note: 3788d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3789d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3790d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 379109383498STejun Heo * 379257b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 379309383498STejun Heo */ 379457b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 379509383498STejun Heo { 3796f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 379709383498STejun Heo } 379857b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 379909383498STejun Heo 380009383498STejun Heo /** 3801401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3802401a8d04STejun Heo * @dwork: the delayed work cancel 3803401a8d04STejun Heo * 3804401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3805401a8d04STejun Heo * 3806d185af30SYacine Belkadi * Return: 3807401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3808401a8d04STejun Heo */ 3809401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 38106e84d644SOleg Nesterov { 381136e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 38126e84d644SOleg Nesterov } 3813f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 38141da177e4SLinus Torvalds 38150fcb78c2SRolf Eike Beer /** 381631ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3817b6136773SAndrew Morton * @func: the function to call 3818b6136773SAndrew Morton * 381931ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 382031ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3821b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 382231ddd871STejun Heo * 3823d185af30SYacine Belkadi * Return: 382431ddd871STejun Heo * 0 on success, -errno on failure. 3825b6136773SAndrew Morton */ 382665f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 382715316ba8SChristoph Lameter { 382815316ba8SChristoph Lameter int cpu; 382938f51568SNamhyung Kim struct work_struct __percpu *works; 383015316ba8SChristoph Lameter 3831b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3832b6136773SAndrew Morton if (!works) 383315316ba8SChristoph Lameter return -ENOMEM; 3834b6136773SAndrew Morton 3835ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 383693981800STejun Heo 383715316ba8SChristoph Lameter for_each_online_cpu(cpu) { 38389bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 38399bfb1839SIngo Molnar 38409bfb1839SIngo Molnar INIT_WORK(work, func); 38418de6d308SOleg Nesterov schedule_work_on(cpu, work); 384215316ba8SChristoph Lameter } 384393981800STejun Heo 384493981800STejun Heo for_each_online_cpu(cpu) 38458616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 384693981800STejun Heo 3847ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3848b6136773SAndrew Morton free_percpu(works); 384915316ba8SChristoph Lameter return 0; 385015316ba8SChristoph Lameter } 385115316ba8SChristoph Lameter 3852eef6a7d5SAlan Stern /** 38531fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 38541fa44ecaSJames Bottomley * @fn: the function to execute 38551fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 38561fa44ecaSJames Bottomley * be available when the work executes) 38571fa44ecaSJames Bottomley * 38581fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 38591fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 38601fa44ecaSJames Bottomley * 3861d185af30SYacine Belkadi * Return: 0 - function was executed 38621fa44ecaSJames Bottomley * 1 - function was scheduled for execution 38631fa44ecaSJames Bottomley */ 386465f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 38651fa44ecaSJames Bottomley { 38661fa44ecaSJames Bottomley if (!in_interrupt()) { 386765f27f38SDavid Howells fn(&ew->work); 38681fa44ecaSJames Bottomley return 0; 38691fa44ecaSJames Bottomley } 38701fa44ecaSJames Bottomley 387165f27f38SDavid Howells INIT_WORK(&ew->work, fn); 38721fa44ecaSJames Bottomley schedule_work(&ew->work); 38731fa44ecaSJames Bottomley 38741fa44ecaSJames Bottomley return 1; 38751fa44ecaSJames Bottomley } 38761fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 38771fa44ecaSJames Bottomley 38787a4e344cSTejun Heo /** 38797a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 38807a4e344cSTejun Heo * @attrs: workqueue_attrs to free 38817a4e344cSTejun Heo * 38827a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 38837a4e344cSTejun Heo */ 3884513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 38857a4e344cSTejun Heo { 38867a4e344cSTejun Heo if (attrs) { 38877a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 38889546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 38897a4e344cSTejun Heo kfree(attrs); 38907a4e344cSTejun Heo } 38917a4e344cSTejun Heo } 38927a4e344cSTejun Heo 38937a4e344cSTejun Heo /** 38947a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 38957a4e344cSTejun Heo * 38967a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3897d185af30SYacine Belkadi * return it. 3898d185af30SYacine Belkadi * 3899d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 39007a4e344cSTejun Heo */ 3901513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 39027a4e344cSTejun Heo { 39037a4e344cSTejun Heo struct workqueue_attrs *attrs; 39047a4e344cSTejun Heo 3905be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 39067a4e344cSTejun Heo if (!attrs) 39077a4e344cSTejun Heo goto fail; 3908be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 39097a4e344cSTejun Heo goto fail; 39109546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 39119546b29eSTejun Heo goto fail; 39127a4e344cSTejun Heo 391313e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 3914523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 39157a4e344cSTejun Heo return attrs; 39167a4e344cSTejun Heo fail: 39177a4e344cSTejun Heo free_workqueue_attrs(attrs); 39187a4e344cSTejun Heo return NULL; 39197a4e344cSTejun Heo } 39207a4e344cSTejun Heo 392129c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 392229c91e99STejun Heo const struct workqueue_attrs *from) 392329c91e99STejun Heo { 392429c91e99STejun Heo to->nice = from->nice; 392529c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 39269546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 39278639ecebSTejun Heo to->affn_strict = from->affn_strict; 392884193c07STejun Heo 39292865a8fbSShaohua Li /* 393084193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 393184193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 393284193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 39332865a8fbSShaohua Li */ 393484193c07STejun Heo to->affn_scope = from->affn_scope; 3935af73f5c9STejun Heo to->ordered = from->ordered; 393629c91e99STejun Heo } 393729c91e99STejun Heo 39385de7a03cSTejun Heo /* 39395de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 39405de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 39415de7a03cSTejun Heo */ 39425de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 39435de7a03cSTejun Heo { 394484193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 39455de7a03cSTejun Heo attrs->ordered = false; 39465de7a03cSTejun Heo } 39475de7a03cSTejun Heo 394829c91e99STejun Heo /* hash value of the content of @attr */ 394929c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 395029c91e99STejun Heo { 395129c91e99STejun Heo u32 hash = 0; 395229c91e99STejun Heo 395329c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 395413e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 395513e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 39569546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 39579546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 39588639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 395929c91e99STejun Heo return hash; 396029c91e99STejun Heo } 396129c91e99STejun Heo 396229c91e99STejun Heo /* content equality test */ 396329c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 396429c91e99STejun Heo const struct workqueue_attrs *b) 396529c91e99STejun Heo { 396629c91e99STejun Heo if (a->nice != b->nice) 396729c91e99STejun Heo return false; 396829c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 396929c91e99STejun Heo return false; 39709546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 39719546b29eSTejun Heo return false; 39728639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 39738639ecebSTejun Heo return false; 397429c91e99STejun Heo return true; 397529c91e99STejun Heo } 397629c91e99STejun Heo 39770f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 39780f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 39790f36ee24STejun Heo const cpumask_t *unbound_cpumask) 39800f36ee24STejun Heo { 39810f36ee24STejun Heo /* 39820f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 39830f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 39840f36ee24STejun Heo * @unbound_cpumask. 39850f36ee24STejun Heo */ 39860f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 39870f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 39880f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 39890f36ee24STejun Heo } 39900f36ee24STejun Heo 399184193c07STejun Heo /* find wq_pod_type to use for @attrs */ 399284193c07STejun Heo static const struct wq_pod_type * 399384193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 399484193c07STejun Heo { 3995523a301eSTejun Heo enum wq_affn_scope scope; 3996523a301eSTejun Heo struct wq_pod_type *pt; 3997523a301eSTejun Heo 3998523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 3999523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4000523a301eSTejun Heo 4001523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4002523a301eSTejun Heo scope = wq_affn_dfl; 4003523a301eSTejun Heo else 4004523a301eSTejun Heo scope = attrs->affn_scope; 4005523a301eSTejun Heo 4006523a301eSTejun Heo pt = &wq_pod_types[scope]; 400784193c07STejun Heo 400884193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 400984193c07STejun Heo likely(pt->nr_pods)) 401084193c07STejun Heo return pt; 401184193c07STejun Heo 401284193c07STejun Heo /* 401384193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 401484193c07STejun Heo * initialized in workqueue_init_early(). 401584193c07STejun Heo */ 401684193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 401784193c07STejun Heo BUG_ON(!pt->nr_pods); 401884193c07STejun Heo return pt; 401984193c07STejun Heo } 402084193c07STejun Heo 40217a4e344cSTejun Heo /** 40227a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 40237a4e344cSTejun Heo * @pool: worker_pool to initialize 40247a4e344cSTejun Heo * 4025402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4026d185af30SYacine Belkadi * 4027d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 402829c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 402929c91e99STejun Heo * on @pool safely to release it. 40307a4e344cSTejun Heo */ 40317a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 40324e1a1f9aSTejun Heo { 4033a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 403429c91e99STejun Heo pool->id = -1; 403529c91e99STejun Heo pool->cpu = -1; 4036f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 40374e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 403882607adcSTejun Heo pool->watchdog_ts = jiffies; 40394e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 40404e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 40414e1a1f9aSTejun Heo hash_init(pool->busy_hash); 40424e1a1f9aSTejun Heo 404332a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 40443f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 40454e1a1f9aSTejun Heo 404632a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 40474e1a1f9aSTejun Heo 4048da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4049e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 40507a4e344cSTejun Heo 40517cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 405229c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 405329c91e99STejun Heo pool->refcnt = 1; 405429c91e99STejun Heo 405529c91e99STejun Heo /* shouldn't fail above this point */ 4056be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 40577a4e344cSTejun Heo if (!pool->attrs) 40587a4e344cSTejun Heo return -ENOMEM; 40595de7a03cSTejun Heo 40605de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 40615de7a03cSTejun Heo 40627a4e344cSTejun Heo return 0; 40634e1a1f9aSTejun Heo } 40644e1a1f9aSTejun Heo 4065669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4066669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4067669de8bdSBart Van Assche { 4068669de8bdSBart Van Assche char *lock_name; 4069669de8bdSBart Van Assche 4070669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4071669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4072669de8bdSBart Van Assche if (!lock_name) 4073669de8bdSBart Van Assche lock_name = wq->name; 407469a106c0SQian Cai 407569a106c0SQian Cai wq->lock_name = lock_name; 4076669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4077669de8bdSBart Van Assche } 4078669de8bdSBart Van Assche 4079669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4080669de8bdSBart Van Assche { 4081669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4082669de8bdSBart Van Assche } 4083669de8bdSBart Van Assche 4084669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4085669de8bdSBart Van Assche { 4086669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4087669de8bdSBart Van Assche kfree(wq->lock_name); 4088669de8bdSBart Van Assche } 4089669de8bdSBart Van Assche #else 4090669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4091669de8bdSBart Van Assche { 4092669de8bdSBart Van Assche } 4093669de8bdSBart Van Assche 4094669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4095669de8bdSBart Van Assche { 4096669de8bdSBart Van Assche } 4097669de8bdSBart Van Assche 4098669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4099669de8bdSBart Van Assche { 4100669de8bdSBart Van Assche } 4101669de8bdSBart Van Assche #endif 4102669de8bdSBart Van Assche 4103*91ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 4104*91ccc6e7STejun Heo { 4105*91ccc6e7STejun Heo int node; 4106*91ccc6e7STejun Heo 4107*91ccc6e7STejun Heo for_each_node(node) { 4108*91ccc6e7STejun Heo kfree(nna_ar[node]); 4109*91ccc6e7STejun Heo nna_ar[node] = NULL; 4110*91ccc6e7STejun Heo } 4111*91ccc6e7STejun Heo 4112*91ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 4113*91ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 4114*91ccc6e7STejun Heo } 4115*91ccc6e7STejun Heo 4116*91ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 4117*91ccc6e7STejun Heo { 4118*91ccc6e7STejun Heo atomic_set(&nna->nr, 0); 4119*91ccc6e7STejun Heo } 4120*91ccc6e7STejun Heo 4121*91ccc6e7STejun Heo /* 4122*91ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 4123*91ccc6e7STejun Heo * should be allocated in the node. 4124*91ccc6e7STejun Heo */ 4125*91ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 4126*91ccc6e7STejun Heo { 4127*91ccc6e7STejun Heo struct wq_node_nr_active *nna; 4128*91ccc6e7STejun Heo int node; 4129*91ccc6e7STejun Heo 4130*91ccc6e7STejun Heo for_each_node(node) { 4131*91ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 4132*91ccc6e7STejun Heo if (!nna) 4133*91ccc6e7STejun Heo goto err_free; 4134*91ccc6e7STejun Heo init_node_nr_active(nna); 4135*91ccc6e7STejun Heo nna_ar[node] = nna; 4136*91ccc6e7STejun Heo } 4137*91ccc6e7STejun Heo 4138*91ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 4139*91ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 4140*91ccc6e7STejun Heo if (!nna) 4141*91ccc6e7STejun Heo goto err_free; 4142*91ccc6e7STejun Heo init_node_nr_active(nna); 4143*91ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 4144*91ccc6e7STejun Heo 4145*91ccc6e7STejun Heo return 0; 4146*91ccc6e7STejun Heo 4147*91ccc6e7STejun Heo err_free: 4148*91ccc6e7STejun Heo free_node_nr_active(nna_ar); 4149*91ccc6e7STejun Heo return -ENOMEM; 4150*91ccc6e7STejun Heo } 4151*91ccc6e7STejun Heo 4152e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4153e2dca7adSTejun Heo { 4154e2dca7adSTejun Heo struct workqueue_struct *wq = 4155e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4156e2dca7adSTejun Heo 4157*91ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 4158*91ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 4159*91ccc6e7STejun Heo 4160669de8bdSBart Van Assche wq_free_lockdep(wq); 4161ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4162e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4163e2dca7adSTejun Heo kfree(wq); 4164e2dca7adSTejun Heo } 4165e2dca7adSTejun Heo 416629c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 416729c91e99STejun Heo { 416829c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 416929c91e99STejun Heo 41707cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 417129c91e99STejun Heo free_workqueue_attrs(pool->attrs); 417229c91e99STejun Heo kfree(pool); 417329c91e99STejun Heo } 417429c91e99STejun Heo 417529c91e99STejun Heo /** 417629c91e99STejun Heo * put_unbound_pool - put a worker_pool 417729c91e99STejun Heo * @pool: worker_pool to put 417829c91e99STejun Heo * 417924acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4180c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4181c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4182c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4183a892caccSTejun Heo * 4184a892caccSTejun Heo * Should be called with wq_pool_mutex held. 418529c91e99STejun Heo */ 418629c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 418729c91e99STejun Heo { 418860f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 418929c91e99STejun Heo struct worker *worker; 41909680540cSYang Yingliang LIST_HEAD(cull_list); 4191e02b9312SValentin Schneider 4192a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4193a892caccSTejun Heo 4194a892caccSTejun Heo if (--pool->refcnt) 419529c91e99STejun Heo return; 419629c91e99STejun Heo 419729c91e99STejun Heo /* sanity checks */ 419861d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4199a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 420029c91e99STejun Heo return; 420129c91e99STejun Heo 420229c91e99STejun Heo /* release id and unhash */ 420329c91e99STejun Heo if (pool->id >= 0) 420429c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 420529c91e99STejun Heo hash_del(&pool->hash_node); 420629c91e99STejun Heo 4207c5aa87bbSTejun Heo /* 4208692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4209692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4210692b4825STejun Heo * manager and @pool gets freed with the flag set. 42119ab03be4SValentin Schneider * 42129ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 42139ab03be4SValentin Schneider * only get here with 42149ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 42159ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 42169ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 42179ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 42189ab03be4SValentin Schneider * drops pool->lock 4219c5aa87bbSTejun Heo */ 42209ab03be4SValentin Schneider while (true) { 42219ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 42229ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4223d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4224e02b9312SValentin Schneider 4225e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 42269ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 42279ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4228692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 42299ab03be4SValentin Schneider break; 42309ab03be4SValentin Schneider } 42319ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4232e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 42339ab03be4SValentin Schneider } 4234692b4825STejun Heo 42351037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4236e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 423729c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4238a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 423960f5a4bcSLai Jiangshan 4240e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4241e02b9312SValentin Schneider 4242e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 424360f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 42441258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 424560f5a4bcSLai Jiangshan 424660f5a4bcSLai Jiangshan if (pool->detach_completion) 424760f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 424860f5a4bcSLai Jiangshan 424929c91e99STejun Heo /* shut down the timers */ 425029c91e99STejun Heo del_timer_sync(&pool->idle_timer); 42513f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 425229c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 425329c91e99STejun Heo 425424acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 425525b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 425629c91e99STejun Heo } 425729c91e99STejun Heo 425829c91e99STejun Heo /** 425929c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 426029c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 426129c91e99STejun Heo * 426229c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 426329c91e99STejun Heo * reference count and return it. If there already is a matching 426429c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4265d185af30SYacine Belkadi * create a new one. 4266a892caccSTejun Heo * 4267a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4268d185af30SYacine Belkadi * 4269d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4270d185af30SYacine Belkadi * On failure, %NULL. 427129c91e99STejun Heo */ 427229c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 427329c91e99STejun Heo { 427484193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 427529c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 427629c91e99STejun Heo struct worker_pool *pool; 427784193c07STejun Heo int pod, node = NUMA_NO_NODE; 427829c91e99STejun Heo 4279a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 428029c91e99STejun Heo 428129c91e99STejun Heo /* do we already have a matching pool? */ 428229c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 428329c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 428429c91e99STejun Heo pool->refcnt++; 42853fb1823cSLai Jiangshan return pool; 428629c91e99STejun Heo } 428729c91e99STejun Heo } 428829c91e99STejun Heo 42899546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 429084193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 42919546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 429284193c07STejun Heo node = pt->pod_node[pod]; 4293e2273584SXunlei Pang break; 4294e2273584SXunlei Pang } 4295e2273584SXunlei Pang } 4296e2273584SXunlei Pang 429729c91e99STejun Heo /* nope, create a new one */ 429884193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 429929c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 430029c91e99STejun Heo goto fail; 430129c91e99STejun Heo 430284193c07STejun Heo pool->node = node; 43035de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 43045de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 43052865a8fbSShaohua Li 430629c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 430729c91e99STejun Heo goto fail; 430829c91e99STejun Heo 430929c91e99STejun Heo /* create and start the initial worker */ 43103347fa09STejun Heo if (wq_online && !create_worker(pool)) 431129c91e99STejun Heo goto fail; 431229c91e99STejun Heo 431329c91e99STejun Heo /* install */ 431429c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 43153fb1823cSLai Jiangshan 431629c91e99STejun Heo return pool; 431729c91e99STejun Heo fail: 431829c91e99STejun Heo if (pool) 431929c91e99STejun Heo put_unbound_pool(pool); 432029c91e99STejun Heo return NULL; 432129c91e99STejun Heo } 432229c91e99STejun Heo 43238864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 43248864b4e5STejun Heo { 43258864b4e5STejun Heo kmem_cache_free(pwq_cache, 43268864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 43278864b4e5STejun Heo } 43288864b4e5STejun Heo 43298864b4e5STejun Heo /* 4330967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4331967b494eSTejun Heo * refcnt and needs to be destroyed. 43328864b4e5STejun Heo */ 4333687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 43348864b4e5STejun Heo { 43358864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4336687a9aa5STejun Heo release_work); 43378864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 43388864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4339b42b0bddSYang Yingliang bool is_last = false; 43408864b4e5STejun Heo 4341b42b0bddSYang Yingliang /* 4342687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4343b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4344b42b0bddSYang Yingliang */ 4345b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 43463c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 43478864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4348bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 43493c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4350b42b0bddSYang Yingliang } 43518864b4e5STejun Heo 4352687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4353a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 43548864b4e5STejun Heo put_unbound_pool(pool); 4355a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4356687a9aa5STejun Heo } 4357a892caccSTejun Heo 435825b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 43598864b4e5STejun Heo 43608864b4e5STejun Heo /* 43618864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4362e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 43638864b4e5STejun Heo */ 4364669de8bdSBart Van Assche if (is_last) { 4365669de8bdSBart Van Assche wq_unregister_lockdep(wq); 436625b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 43676029a918STejun Heo } 4368669de8bdSBart Van Assche } 43698864b4e5STejun Heo 437067dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4371f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4372f147f29eSTejun Heo struct worker_pool *pool) 4373d2c1d404STejun Heo { 4374d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4375d2c1d404STejun Heo 4376e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4377e50aba9aSTejun Heo 4378d2c1d404STejun Heo pwq->pool = pool; 4379d2c1d404STejun Heo pwq->wq = wq; 4380d2c1d404STejun Heo pwq->flush_color = -1; 43818864b4e5STejun Heo pwq->refcnt = 1; 4382f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 43831befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4384d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4385687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4386f147f29eSTejun Heo } 4387d2c1d404STejun Heo 4388f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 43891befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4390f147f29eSTejun Heo { 4391f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4392f147f29eSTejun Heo 4393f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 439475ccf595STejun Heo 43951befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 43961befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 43971befcf30STejun Heo return; 43981befcf30STejun Heo 439929b1cb41SLai Jiangshan /* set the matching work_color */ 440075ccf595STejun Heo pwq->work_color = wq->work_color; 4401983ca25eSTejun Heo 4402983ca25eSTejun Heo /* link in @pwq */ 44039e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4404df2d5ae4STejun Heo } 44056029a918STejun Heo 4406f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4407f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4408f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4409f147f29eSTejun Heo { 4410f147f29eSTejun Heo struct worker_pool *pool; 4411f147f29eSTejun Heo struct pool_workqueue *pwq; 4412f147f29eSTejun Heo 4413f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4414f147f29eSTejun Heo 4415f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4416f147f29eSTejun Heo if (!pool) 4417f147f29eSTejun Heo return NULL; 4418f147f29eSTejun Heo 4419e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4420f147f29eSTejun Heo if (!pwq) { 4421f147f29eSTejun Heo put_unbound_pool(pool); 4422f147f29eSTejun Heo return NULL; 4423f147f29eSTejun Heo } 4424f147f29eSTejun Heo 4425f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4426f147f29eSTejun Heo return pwq; 4427d2c1d404STejun Heo } 4428d2c1d404STejun Heo 44294c16bd32STejun Heo /** 4430fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4431042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 443284193c07STejun Heo * @cpu: the target CPU 44334c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 44344c16bd32STejun Heo * 4435fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4436fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 44379546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 44384c16bd32STejun Heo * 4439fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4440fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4441fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 44424c16bd32STejun Heo * 4443fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 44444c16bd32STejun Heo */ 44459546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 44469546b29eSTejun Heo int cpu_going_down) 44474c16bd32STejun Heo { 444884193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 444984193c07STejun Heo int pod = pt->cpu_pod[cpu]; 44504c16bd32STejun Heo 4451fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 44529546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 44539546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 44544c16bd32STejun Heo if (cpu_going_down >= 0) 44559546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 44564c16bd32STejun Heo 44579546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 44589546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 445984193c07STejun Heo return; 446084193c07STejun Heo } 44614c16bd32STejun Heo 4462fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 44639546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 44641ad0f0a7SMichael Bringmann 44659546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 44661ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 44671ad0f0a7SMichael Bringmann "possible intersect\n"); 44684c16bd32STejun Heo } 44694c16bd32STejun Heo 44709f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4471636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4472636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 44731befcf30STejun Heo { 44749f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 44751befcf30STejun Heo struct pool_workqueue *old_pwq; 44761befcf30STejun Heo 44775b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 44781befcf30STejun Heo lockdep_assert_held(&wq->mutex); 44791befcf30STejun Heo 44801befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 44811befcf30STejun Heo link_pwq(pwq); 44821befcf30STejun Heo 44839f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 44849f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 44851befcf30STejun Heo return old_pwq; 44861befcf30STejun Heo } 44871befcf30STejun Heo 44882d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 44892d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 44902d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 44912d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4492042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 44932d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 44942d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 44952d5f0764SLai Jiangshan }; 44962d5f0764SLai Jiangshan 44972d5f0764SLai Jiangshan /* free the resources after success or abort */ 44982d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 44992d5f0764SLai Jiangshan { 45002d5f0764SLai Jiangshan if (ctx) { 4501636b927eSTejun Heo int cpu; 45022d5f0764SLai Jiangshan 4503636b927eSTejun Heo for_each_possible_cpu(cpu) 4504636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 45052d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 45062d5f0764SLai Jiangshan 45072d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 45082d5f0764SLai Jiangshan 45092d5f0764SLai Jiangshan kfree(ctx); 45102d5f0764SLai Jiangshan } 45112d5f0764SLai Jiangshan } 45122d5f0764SLai Jiangshan 45132d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 45142d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 45152d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 451699c621efSLai Jiangshan const struct workqueue_attrs *attrs, 451799c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 45182d5f0764SLai Jiangshan { 45192d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 45209546b29eSTejun Heo struct workqueue_attrs *new_attrs; 4521636b927eSTejun Heo int cpu; 45222d5f0764SLai Jiangshan 45232d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 45242d5f0764SLai Jiangshan 452584193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 452684193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 452784193c07STejun Heo return ERR_PTR(-EINVAL); 452884193c07STejun Heo 4529636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 45302d5f0764SLai Jiangshan 4531be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 45329546b29eSTejun Heo if (!ctx || !new_attrs) 45332d5f0764SLai Jiangshan goto out_free; 45342d5f0764SLai Jiangshan 4535042f7df1SLai Jiangshan /* 45362d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 45372d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 45382d5f0764SLai Jiangshan * it even if we don't use it immediately. 45392d5f0764SLai Jiangshan */ 45400f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 45410f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 45429546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 45432d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 45442d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 45452d5f0764SLai Jiangshan goto out_free; 45462d5f0764SLai Jiangshan 4547636b927eSTejun Heo for_each_possible_cpu(cpu) { 4548af73f5c9STejun Heo if (new_attrs->ordered) { 45492d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4550636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4551636b927eSTejun Heo } else { 45529546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 45539546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 4554636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4555636b927eSTejun Heo goto out_free; 45562d5f0764SLai Jiangshan } 45572d5f0764SLai Jiangshan } 45582d5f0764SLai Jiangshan 4559042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4560042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4561042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 45629546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 45632d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4564042f7df1SLai Jiangshan 45652d5f0764SLai Jiangshan ctx->wq = wq; 45662d5f0764SLai Jiangshan return ctx; 45672d5f0764SLai Jiangshan 45682d5f0764SLai Jiangshan out_free: 45692d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 45702d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 457184193c07STejun Heo return ERR_PTR(-ENOMEM); 45722d5f0764SLai Jiangshan } 45732d5f0764SLai Jiangshan 45742d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 45752d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 45762d5f0764SLai Jiangshan { 4577636b927eSTejun Heo int cpu; 45782d5f0764SLai Jiangshan 45792d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 45802d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 45812d5f0764SLai Jiangshan 45822d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 45832d5f0764SLai Jiangshan 45849f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 4585636b927eSTejun Heo for_each_possible_cpu(cpu) 4586636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4587636b927eSTejun Heo ctx->pwq_tbl[cpu]); 45889f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 45892d5f0764SLai Jiangshan 45902d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 45912d5f0764SLai Jiangshan } 45922d5f0764SLai Jiangshan 4593a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4594a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4595a0111cf6SLai Jiangshan { 4596a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4597a0111cf6SLai Jiangshan 4598a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4599a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4600a0111cf6SLai Jiangshan return -EINVAL; 4601a0111cf6SLai Jiangshan 4602a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 46030a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 46040a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4605a0111cf6SLai Jiangshan return -EINVAL; 4606a0111cf6SLai Jiangshan 46070a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 46080a94efb5STejun Heo } 46090a94efb5STejun Heo 461099c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 461184193c07STejun Heo if (IS_ERR(ctx)) 461284193c07STejun Heo return PTR_ERR(ctx); 4613a0111cf6SLai Jiangshan 4614a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4615a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4616a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4617a0111cf6SLai Jiangshan 46186201171eSwanghaibin return 0; 4619a0111cf6SLai Jiangshan } 4620a0111cf6SLai Jiangshan 46219e8cd2f5STejun Heo /** 46229e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 46239e8cd2f5STejun Heo * @wq: the target workqueue 46249e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 46259e8cd2f5STejun Heo * 4626fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4627fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4628fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4629fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4630fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 46319e8cd2f5STejun Heo * 4632d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4633d185af30SYacine Belkadi * 4634ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4635509b3204SDaniel Jordan * 4636d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 46379e8cd2f5STejun Heo */ 4638513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 46399e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 46409e8cd2f5STejun Heo { 4641a0111cf6SLai Jiangshan int ret; 46429e8cd2f5STejun Heo 4643509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4644509b3204SDaniel Jordan 4645509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4646a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4647509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 46482d5f0764SLai Jiangshan 46492d5f0764SLai Jiangshan return ret; 46509e8cd2f5STejun Heo } 46519e8cd2f5STejun Heo 46524c16bd32STejun Heo /** 4653fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 46544c16bd32STejun Heo * @wq: the target workqueue 46554cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 46564cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 46574c16bd32STejun Heo * @online: whether @cpu is coming up or going down 46584c16bd32STejun Heo * 46594c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4660fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 46614c16bd32STejun Heo * @wq accordingly. 46624c16bd32STejun Heo * 46634c16bd32STejun Heo * 4664fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4665fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4666fef59c9cSTejun Heo * 4667fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4668fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4669fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4670fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4671fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4672fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 46734c16bd32STejun Heo */ 4674fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 46754cbfd3deSTejun Heo int hotplug_cpu, bool online) 46764c16bd32STejun Heo { 46774cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 46784c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 46794c16bd32STejun Heo struct workqueue_attrs *target_attrs; 46804c16bd32STejun Heo 46814c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 46824c16bd32STejun Heo 468384193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 46844c16bd32STejun Heo return; 46854c16bd32STejun Heo 46864c16bd32STejun Heo /* 46874c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 46884c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 46894c16bd32STejun Heo * CPU hotplug exclusion. 46904c16bd32STejun Heo */ 4691fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 46924c16bd32STejun Heo 46934c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 46940f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 46954c16bd32STejun Heo 4696636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 46979546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 46989f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 4699f7142ed4SLai Jiangshan return; 47004c16bd32STejun Heo 47014c16bd32STejun Heo /* create a new pwq */ 47024c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 47034c16bd32STejun Heo if (!pwq) { 4704fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 47054c16bd32STejun Heo wq->name); 470677f300b1SDaeseok Youn goto use_dfl_pwq; 47074c16bd32STejun Heo } 47084c16bd32STejun Heo 4709f7142ed4SLai Jiangshan /* Install the new pwq. */ 47104c16bd32STejun Heo mutex_lock(&wq->mutex); 4711636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 47124c16bd32STejun Heo goto out_unlock; 47134c16bd32STejun Heo 47144c16bd32STejun Heo use_dfl_pwq: 4715f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 47169f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 47179f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 47189f66cff2STejun Heo get_pwq(pwq); 47199f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 47209f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 47214c16bd32STejun Heo out_unlock: 47224c16bd32STejun Heo mutex_unlock(&wq->mutex); 47234c16bd32STejun Heo put_pwq_unlocked(old_pwq); 47244c16bd32STejun Heo } 47254c16bd32STejun Heo 472630cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 47271da177e4SLinus Torvalds { 472849e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 47298a2b7538STejun Heo int cpu, ret; 4730e1d8aa9fSFrederic Weisbecker 4731687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 4732ee1ceef7STejun Heo if (!wq->cpu_pwq) 4733687a9aa5STejun Heo goto enomem; 473430cdf249STejun Heo 4735636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 473630cdf249STejun Heo for_each_possible_cpu(cpu) { 4737687a9aa5STejun Heo struct pool_workqueue **pwq_p = 4738ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 4739687a9aa5STejun Heo struct worker_pool *pool = 4740687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 474130cdf249STejun Heo 4742687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 4743687a9aa5STejun Heo pool->node); 4744687a9aa5STejun Heo if (!*pwq_p) 4745687a9aa5STejun Heo goto enomem; 4746687a9aa5STejun Heo 4747687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 4748f147f29eSTejun Heo 4749f147f29eSTejun Heo mutex_lock(&wq->mutex); 4750687a9aa5STejun Heo link_pwq(*pwq_p); 4751f147f29eSTejun Heo mutex_unlock(&wq->mutex); 475230cdf249STejun Heo } 475330cdf249STejun Heo return 0; 4754509b3204SDaniel Jordan } 4755509b3204SDaniel Jordan 4756ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4757509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 47589f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 47599f66cff2STejun Heo 47608a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 47618a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 47629f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 47639f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 47649f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 47658a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 47669e8cd2f5STejun Heo } else { 4767509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 47689e8cd2f5STejun Heo } 4769ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4770509b3204SDaniel Jordan 477164344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 477264344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 477364344553SZqiang */ 477464344553SZqiang if (ret) 477564344553SZqiang kthread_flush_worker(pwq_release_worker); 477664344553SZqiang 4777509b3204SDaniel Jordan return ret; 4778687a9aa5STejun Heo 4779687a9aa5STejun Heo enomem: 4780687a9aa5STejun Heo if (wq->cpu_pwq) { 47817b42f401SZqiang for_each_possible_cpu(cpu) { 47827b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 47837b42f401SZqiang 47847b42f401SZqiang if (pwq) 47857b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 47867b42f401SZqiang } 4787687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 4788687a9aa5STejun Heo wq->cpu_pwq = NULL; 4789687a9aa5STejun Heo } 4790687a9aa5STejun Heo return -ENOMEM; 47910f900049STejun Heo } 47920f900049STejun Heo 4793f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4794f3421797STejun Heo const char *name) 4795b71ab8c2STejun Heo { 4796636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 4797044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4798636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 4799b71ab8c2STejun Heo 4800636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 4801b71ab8c2STejun Heo } 4802b71ab8c2STejun Heo 4803983c7515STejun Heo /* 4804983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4805983c7515STejun Heo * to guarantee forward progress. 4806983c7515STejun Heo */ 4807983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4808983c7515STejun Heo { 4809983c7515STejun Heo struct worker *rescuer; 4810b92b36eaSDan Carpenter int ret; 4811983c7515STejun Heo 4812983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4813983c7515STejun Heo return 0; 4814983c7515STejun Heo 4815983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 48164c0736a7SPetr Mladek if (!rescuer) { 48174c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 48184c0736a7SPetr Mladek wq->name); 4819983c7515STejun Heo return -ENOMEM; 48204c0736a7SPetr Mladek } 4821983c7515STejun Heo 4822983c7515STejun Heo rescuer->rescue_wq = wq; 4823b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 4824f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4825b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 48264c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 48274c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4828983c7515STejun Heo kfree(rescuer); 4829b92b36eaSDan Carpenter return ret; 4830983c7515STejun Heo } 4831983c7515STejun Heo 4832983c7515STejun Heo wq->rescuer = rescuer; 483385f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 483485f0ab43SJuri Lelli kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); 483585f0ab43SJuri Lelli else 4836983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4837983c7515STejun Heo wake_up_process(rescuer->task); 4838983c7515STejun Heo 4839983c7515STejun Heo return 0; 4840983c7515STejun Heo } 4841983c7515STejun Heo 4842a045a272STejun Heo /** 4843a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 4844a045a272STejun Heo * @wq: target workqueue 4845a045a272STejun Heo * 4846a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 4847a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 4848a045a272STejun Heo * @wq->max_active to zero. 4849a045a272STejun Heo */ 4850a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 4851a045a272STejun Heo { 4852c5404d4eSTejun Heo bool activated; 4853a045a272STejun Heo 4854a045a272STejun Heo lockdep_assert_held(&wq->mutex); 4855a045a272STejun Heo 4856a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 4857a045a272STejun Heo WRITE_ONCE(wq->max_active, 0); 4858a045a272STejun Heo return; 4859a045a272STejun Heo } 4860a045a272STejun Heo 4861a045a272STejun Heo if (wq->max_active == wq->saved_max_active) 4862a045a272STejun Heo return; 4863a045a272STejun Heo 4864a045a272STejun Heo /* 4865a045a272STejun Heo * Update @wq->max_active and then kick inactive work items if more 4866a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 4867a045a272STejun Heo * because new work items are always queued behind existing inactive 4868a045a272STejun Heo * work items if there are any. 4869a045a272STejun Heo */ 4870a045a272STejun Heo WRITE_ONCE(wq->max_active, wq->saved_max_active); 4871a045a272STejun Heo 4872c5404d4eSTejun Heo /* 4873c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 4874c5404d4eSTejun Heo * until max_active is filled. 4875c5404d4eSTejun Heo */ 4876c5404d4eSTejun Heo do { 4877c5404d4eSTejun Heo struct pool_workqueue *pwq; 4878c5404d4eSTejun Heo 4879c5404d4eSTejun Heo activated = false; 4880a045a272STejun Heo for_each_pwq(pwq, wq) { 4881a045a272STejun Heo unsigned long flags; 4882a045a272STejun Heo 4883c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 4884a045a272STejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, flags); 4885c5404d4eSTejun Heo if (pwq_activate_first_inactive(pwq)) { 4886c5404d4eSTejun Heo activated = true; 4887a045a272STejun Heo kick_pool(pwq->pool); 4888c5404d4eSTejun Heo } 4889a045a272STejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 4890a045a272STejun Heo } 4891c5404d4eSTejun Heo } while (activated); 4892a045a272STejun Heo } 4893a045a272STejun Heo 4894a2775bbcSMathieu Malaterre __printf(1, 4) 4895669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 489697e37d7bSTejun Heo unsigned int flags, 4897669de8bdSBart Van Assche int max_active, ...) 48983af24433SOleg Nesterov { 4899ecf6881fSTejun Heo va_list args; 49003af24433SOleg Nesterov struct workqueue_struct *wq; 4901*91ccc6e7STejun Heo size_t wq_size; 4902*91ccc6e7STejun Heo int name_len; 4903b196be89STejun Heo 49045c0338c6STejun Heo /* 4905fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 4906fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 49075c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 4908fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 49095c0338c6STejun Heo */ 49105c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 49115c0338c6STejun Heo flags |= __WQ_ORDERED; 49125c0338c6STejun Heo 4913cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4914cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4915cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4916cee22a15SViresh Kumar 4917ecf6881fSTejun Heo /* allocate wq and format name */ 4918*91ccc6e7STejun Heo if (flags & WQ_UNBOUND) 4919*91ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 4920*91ccc6e7STejun Heo else 4921*91ccc6e7STejun Heo wq_size = sizeof(*wq); 4922*91ccc6e7STejun Heo 4923*91ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 4924b196be89STejun Heo if (!wq) 4925d2c1d404STejun Heo return NULL; 4926b196be89STejun Heo 49276029a918STejun Heo if (flags & WQ_UNBOUND) { 4928be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 49296029a918STejun Heo if (!wq->unbound_attrs) 49306029a918STejun Heo goto err_free_wq; 49316029a918STejun Heo } 49326029a918STejun Heo 4933669de8bdSBart Van Assche va_start(args, max_active); 4934*91ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4935b196be89STejun Heo va_end(args); 49363af24433SOleg Nesterov 4937*91ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 4938*91ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 4939*91ccc6e7STejun Heo wq->name); 494031c89007SAudra Mitchell 4941d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4942b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 49433af24433SOleg Nesterov 4944b196be89STejun Heo /* init wq */ 494597e37d7bSTejun Heo wq->flags = flags; 4946a045a272STejun Heo wq->max_active = max_active; 4947a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 49483c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4949112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 495030cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 495173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 495273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4953493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 49543af24433SOleg Nesterov 4955669de8bdSBart Van Assche wq_init_lockdep(wq); 4956cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 49573af24433SOleg Nesterov 4958*91ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 4959*91ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 496082efcab3SBart Van Assche goto err_unreg_lockdep; 4961*91ccc6e7STejun Heo } 4962*91ccc6e7STejun Heo 4963*91ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 4964*91ccc6e7STejun Heo goto err_free_node_nr_active; 49651537663fSTejun Heo 496640c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4967d2c1d404STejun Heo goto err_destroy; 4968e22bee78STejun Heo 4969226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4970226223abSTejun Heo goto err_destroy; 4971226223abSTejun Heo 49726af8bf3dSOleg Nesterov /* 497368e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 497468e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 497568e13a67SLai Jiangshan * list. 49766af8bf3dSOleg Nesterov */ 497768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4978a0a1a5fdSTejun Heo 4979a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4980a045a272STejun Heo wq_adjust_max_active(wq); 4981a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4982a0a1a5fdSTejun Heo 4983e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4984a0a1a5fdSTejun Heo 498568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 49863af24433SOleg Nesterov 49873af24433SOleg Nesterov return wq; 4988d2c1d404STejun Heo 4989*91ccc6e7STejun Heo err_free_node_nr_active: 4990*91ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 4991*91ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 499282efcab3SBart Van Assche err_unreg_lockdep: 4993009bb421SBart Van Assche wq_unregister_lockdep(wq); 4994009bb421SBart Van Assche wq_free_lockdep(wq); 499582efcab3SBart Van Assche err_free_wq: 49966029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 49974690c4abSTejun Heo kfree(wq); 4998d2c1d404STejun Heo return NULL; 4999d2c1d404STejun Heo err_destroy: 5000d2c1d404STejun Heo destroy_workqueue(wq); 50014690c4abSTejun Heo return NULL; 50021da177e4SLinus Torvalds } 5003669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 50041da177e4SLinus Torvalds 5005c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5006c29eb853STejun Heo { 5007c29eb853STejun Heo int i; 5008c29eb853STejun Heo 5009c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5010c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5011c29eb853STejun Heo return true; 5012c29eb853STejun Heo 50139f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5014c29eb853STejun Heo return true; 5015afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5016c29eb853STejun Heo return true; 5017c29eb853STejun Heo 5018c29eb853STejun Heo return false; 5019c29eb853STejun Heo } 5020c29eb853STejun Heo 50213af24433SOleg Nesterov /** 50223af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 50233af24433SOleg Nesterov * @wq: target workqueue 50243af24433SOleg Nesterov * 50253af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 50263af24433SOleg Nesterov */ 50273af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 50283af24433SOleg Nesterov { 502949e3cf44STejun Heo struct pool_workqueue *pwq; 5030636b927eSTejun Heo int cpu; 50313af24433SOleg Nesterov 5032def98c84STejun Heo /* 5033def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5034def98c84STejun Heo * lead to sysfs name conflicts. 5035def98c84STejun Heo */ 5036def98c84STejun Heo workqueue_sysfs_unregister(wq); 5037def98c84STejun Heo 503833e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 503933e3f0a3SRichard Clark mutex_lock(&wq->mutex); 504033e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 504133e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 504233e3f0a3SRichard Clark 50439c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 50449c5a2ba7STejun Heo drain_workqueue(wq); 5045c8efcc25STejun Heo 5046def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5047def98c84STejun Heo if (wq->rescuer) { 5048def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5049def98c84STejun Heo 5050def98c84STejun Heo /* this prevents new queueing */ 5051a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5052def98c84STejun Heo wq->rescuer = NULL; 5053a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5054def98c84STejun Heo 5055def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5056def98c84STejun Heo kthread_stop(rescuer->task); 50578efe1223STejun Heo kfree(rescuer); 5058def98c84STejun Heo } 5059def98c84STejun Heo 5060c29eb853STejun Heo /* 5061c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5062c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5063c29eb853STejun Heo */ 5064c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5065b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 506649e3cf44STejun Heo for_each_pwq(pwq, wq) { 5067a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5068c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 50691d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5070e66b39afSTejun Heo __func__, wq->name); 5071c29eb853STejun Heo show_pwq(pwq); 5072a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5073b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5074c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 507555df0933SImran Khan show_one_workqueue(wq); 50766183c009STejun Heo return; 507776af4d93STejun Heo } 5078a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 507976af4d93STejun Heo } 5080b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 50816183c009STejun Heo 5082a0a1a5fdSTejun Heo /* 5083a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5084a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5085a0a1a5fdSTejun Heo */ 5086e2dca7adSTejun Heo list_del_rcu(&wq->list); 508768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 50883af24433SOleg Nesterov 50898864b4e5STejun Heo /* 5090636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5091636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5092636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 50938864b4e5STejun Heo */ 5094636b927eSTejun Heo rcu_read_lock(); 5095636b927eSTejun Heo 5096636b927eSTejun Heo for_each_possible_cpu(cpu) { 50979f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 50989f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 50994c16bd32STejun Heo } 51004c16bd32STejun Heo 51019f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 51029f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5103636b927eSTejun Heo 5104636b927eSTejun Heo rcu_read_unlock(); 51053af24433SOleg Nesterov } 51063af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 51073af24433SOleg Nesterov 5108dcd989cbSTejun Heo /** 5109dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5110dcd989cbSTejun Heo * @wq: target workqueue 5111dcd989cbSTejun Heo * @max_active: new max_active value. 5112dcd989cbSTejun Heo * 5113dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 5114dcd989cbSTejun Heo * 5115dcd989cbSTejun Heo * CONTEXT: 5116dcd989cbSTejun Heo * Don't call from IRQ context. 5117dcd989cbSTejun Heo */ 5118dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5119dcd989cbSTejun Heo { 51208719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 51210a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 51228719dceaSTejun Heo return; 51238719dceaSTejun Heo 5124f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5125dcd989cbSTejun Heo 5126a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5127dcd989cbSTejun Heo 51280a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 5129dcd989cbSTejun Heo wq->saved_max_active = max_active; 5130a045a272STejun Heo wq_adjust_max_active(wq); 5131dcd989cbSTejun Heo 5132a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5133dcd989cbSTejun Heo } 5134dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5135dcd989cbSTejun Heo 5136dcd989cbSTejun Heo /** 513727d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 513827d4ee03SLukas Wunner * 513927d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 514027d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 514127d4ee03SLukas Wunner * 514227d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 514327d4ee03SLukas Wunner */ 514427d4ee03SLukas Wunner struct work_struct *current_work(void) 514527d4ee03SLukas Wunner { 514627d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 514727d4ee03SLukas Wunner 514827d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 514927d4ee03SLukas Wunner } 515027d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 515127d4ee03SLukas Wunner 515227d4ee03SLukas Wunner /** 5153e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5154e6267616STejun Heo * 5155e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5156e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5157d185af30SYacine Belkadi * 5158d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5159e6267616STejun Heo */ 5160e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5161e6267616STejun Heo { 5162e6267616STejun Heo struct worker *worker = current_wq_worker(); 5163e6267616STejun Heo 51646a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5165e6267616STejun Heo } 5166e6267616STejun Heo 5167e6267616STejun Heo /** 5168dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5169dcd989cbSTejun Heo * @cpu: CPU in question 5170dcd989cbSTejun Heo * @wq: target workqueue 5171dcd989cbSTejun Heo * 5172dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5173dcd989cbSTejun Heo * no synchronization around this function and the test result is 5174dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5175dcd989cbSTejun Heo * 5176d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5177636b927eSTejun Heo * 5178636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5179636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5180636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5181636b927eSTejun Heo * other CPUs. 5182d3251859STejun Heo * 5183d185af30SYacine Belkadi * Return: 5184dcd989cbSTejun Heo * %true if congested, %false otherwise. 5185dcd989cbSTejun Heo */ 5186d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5187dcd989cbSTejun Heo { 51887fb98ea7STejun Heo struct pool_workqueue *pwq; 518976af4d93STejun Heo bool ret; 519076af4d93STejun Heo 519124acfb71SThomas Gleixner rcu_read_lock(); 519224acfb71SThomas Gleixner preempt_disable(); 51937fb98ea7STejun Heo 5194d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5195d3251859STejun Heo cpu = smp_processor_id(); 5196d3251859STejun Heo 5197687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5198f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5199636b927eSTejun Heo 520024acfb71SThomas Gleixner preempt_enable(); 520124acfb71SThomas Gleixner rcu_read_unlock(); 520276af4d93STejun Heo 520376af4d93STejun Heo return ret; 5204dcd989cbSTejun Heo } 5205dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5206dcd989cbSTejun Heo 5207dcd989cbSTejun Heo /** 5208dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5209dcd989cbSTejun Heo * @work: the work to be tested 5210dcd989cbSTejun Heo * 5211dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5212dcd989cbSTejun Heo * synchronization around this function and the test result is 5213dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5214dcd989cbSTejun Heo * 5215d185af30SYacine Belkadi * Return: 5216dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5217dcd989cbSTejun Heo */ 5218dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5219dcd989cbSTejun Heo { 5220fa1b54e6STejun Heo struct worker_pool *pool; 5221dcd989cbSTejun Heo unsigned long flags; 5222dcd989cbSTejun Heo unsigned int ret = 0; 5223dcd989cbSTejun Heo 5224dcd989cbSTejun Heo if (work_pending(work)) 5225dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5226038366c5SLai Jiangshan 522724acfb71SThomas Gleixner rcu_read_lock(); 5228fa1b54e6STejun Heo pool = get_work_pool(work); 5229038366c5SLai Jiangshan if (pool) { 5230a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 5231c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5232dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5233a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 5234038366c5SLai Jiangshan } 523524acfb71SThomas Gleixner rcu_read_unlock(); 5236dcd989cbSTejun Heo 5237dcd989cbSTejun Heo return ret; 5238dcd989cbSTejun Heo } 5239dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5240dcd989cbSTejun Heo 52413d1cb205STejun Heo /** 52423d1cb205STejun Heo * set_worker_desc - set description for the current work item 52433d1cb205STejun Heo * @fmt: printf-style format string 52443d1cb205STejun Heo * @...: arguments for the format string 52453d1cb205STejun Heo * 52463d1cb205STejun Heo * This function can be called by a running work function to describe what 52473d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 52483d1cb205STejun Heo * information will be printed out together to help debugging. The 52493d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 52503d1cb205STejun Heo */ 52513d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 52523d1cb205STejun Heo { 52533d1cb205STejun Heo struct worker *worker = current_wq_worker(); 52543d1cb205STejun Heo va_list args; 52553d1cb205STejun Heo 52563d1cb205STejun Heo if (worker) { 52573d1cb205STejun Heo va_start(args, fmt); 52583d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 52593d1cb205STejun Heo va_end(args); 52603d1cb205STejun Heo } 52613d1cb205STejun Heo } 52625c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 52633d1cb205STejun Heo 52643d1cb205STejun Heo /** 52653d1cb205STejun Heo * print_worker_info - print out worker information and description 52663d1cb205STejun Heo * @log_lvl: the log level to use when printing 52673d1cb205STejun Heo * @task: target task 52683d1cb205STejun Heo * 52693d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 52703d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 52713d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 52723d1cb205STejun Heo * 52733d1cb205STejun Heo * This function can be safely called on any task as long as the 52743d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 52753d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 52763d1cb205STejun Heo */ 52773d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 52783d1cb205STejun Heo { 52793d1cb205STejun Heo work_func_t *fn = NULL; 52803d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 52813d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 52823d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 52833d1cb205STejun Heo struct workqueue_struct *wq = NULL; 52843d1cb205STejun Heo struct worker *worker; 52853d1cb205STejun Heo 52863d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 52873d1cb205STejun Heo return; 52883d1cb205STejun Heo 52893d1cb205STejun Heo /* 52903d1cb205STejun Heo * This function is called without any synchronization and @task 52913d1cb205STejun Heo * could be in any state. Be careful with dereferences. 52923d1cb205STejun Heo */ 5293e700591aSPetr Mladek worker = kthread_probe_data(task); 52943d1cb205STejun Heo 52953d1cb205STejun Heo /* 52968bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 52978bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 52983d1cb205STejun Heo */ 5299fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5300fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5301fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5302fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5303fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 53043d1cb205STejun Heo 53053d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5306d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 53078bf89593STejun Heo if (strcmp(name, desc)) 53083d1cb205STejun Heo pr_cont(" (%s)", desc); 53093d1cb205STejun Heo pr_cont("\n"); 53103d1cb205STejun Heo } 53113d1cb205STejun Heo } 53123d1cb205STejun Heo 53133494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 53143494fc30STejun Heo { 53153494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 53163494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 53173494fc30STejun Heo pr_cont(" node=%d", pool->node); 53183494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 53193494fc30STejun Heo } 53203494fc30STejun Heo 5321c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5322c76feb0dSPaul E. McKenney bool comma; 5323c76feb0dSPaul E. McKenney work_func_t func; 5324c76feb0dSPaul E. McKenney long ctr; 5325c76feb0dSPaul E. McKenney }; 5326c76feb0dSPaul E. McKenney 5327c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5328c76feb0dSPaul E. McKenney { 5329c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5330c76feb0dSPaul E. McKenney goto out_record; 5331c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5332c76feb0dSPaul E. McKenney pcwsp->ctr++; 5333c76feb0dSPaul E. McKenney return; 5334c76feb0dSPaul E. McKenney } 5335c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5336c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5337c76feb0dSPaul E. McKenney else 5338c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5339c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5340c76feb0dSPaul E. McKenney out_record: 5341c76feb0dSPaul E. McKenney if ((long)func == -1L) 5342c76feb0dSPaul E. McKenney return; 5343c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5344c76feb0dSPaul E. McKenney pcwsp->func = func; 5345c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5346c76feb0dSPaul E. McKenney } 5347c76feb0dSPaul E. McKenney 5348c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 53493494fc30STejun Heo { 53503494fc30STejun Heo if (work->func == wq_barrier_func) { 53513494fc30STejun Heo struct wq_barrier *barr; 53523494fc30STejun Heo 53533494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 53543494fc30STejun Heo 5355c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 53563494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 53573494fc30STejun Heo task_pid_nr(barr->task)); 53583494fc30STejun Heo } else { 5359c76feb0dSPaul E. McKenney if (!comma) 5360c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5361c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 53623494fc30STejun Heo } 53633494fc30STejun Heo } 53643494fc30STejun Heo 53653494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 53663494fc30STejun Heo { 5367c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 53683494fc30STejun Heo struct worker_pool *pool = pwq->pool; 53693494fc30STejun Heo struct work_struct *work; 53703494fc30STejun Heo struct worker *worker; 53713494fc30STejun Heo bool has_in_flight = false, has_pending = false; 53723494fc30STejun Heo int bkt; 53733494fc30STejun Heo 53743494fc30STejun Heo pr_info(" pwq %d:", pool->id); 53753494fc30STejun Heo pr_cont_pool_info(pool); 53763494fc30STejun Heo 5377a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5378a045a272STejun Heo pwq->nr_active, pwq->refcnt, 53793494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 53803494fc30STejun Heo 53813494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 53823494fc30STejun Heo if (worker->current_pwq == pwq) { 53833494fc30STejun Heo has_in_flight = true; 53843494fc30STejun Heo break; 53853494fc30STejun Heo } 53863494fc30STejun Heo } 53873494fc30STejun Heo if (has_in_flight) { 53883494fc30STejun Heo bool comma = false; 53893494fc30STejun Heo 53903494fc30STejun Heo pr_info(" in-flight:"); 53913494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 53923494fc30STejun Heo if (worker->current_pwq != pwq) 53933494fc30STejun Heo continue; 53943494fc30STejun Heo 5395d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 53963494fc30STejun Heo task_pid_nr(worker->task), 539730ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 53983494fc30STejun Heo worker->current_func); 53993494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5400c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5401c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 54023494fc30STejun Heo comma = true; 54033494fc30STejun Heo } 54043494fc30STejun Heo pr_cont("\n"); 54053494fc30STejun Heo } 54063494fc30STejun Heo 54073494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 54083494fc30STejun Heo if (get_work_pwq(work) == pwq) { 54093494fc30STejun Heo has_pending = true; 54103494fc30STejun Heo break; 54113494fc30STejun Heo } 54123494fc30STejun Heo } 54133494fc30STejun Heo if (has_pending) { 54143494fc30STejun Heo bool comma = false; 54153494fc30STejun Heo 54163494fc30STejun Heo pr_info(" pending:"); 54173494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 54183494fc30STejun Heo if (get_work_pwq(work) != pwq) 54193494fc30STejun Heo continue; 54203494fc30STejun Heo 5421c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 54223494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 54233494fc30STejun Heo } 5424c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 54253494fc30STejun Heo pr_cont("\n"); 54263494fc30STejun Heo } 54273494fc30STejun Heo 5428f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 54293494fc30STejun Heo bool comma = false; 54303494fc30STejun Heo 5431f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5432f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5433c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 54343494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 54353494fc30STejun Heo } 5436c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 54373494fc30STejun Heo pr_cont("\n"); 54383494fc30STejun Heo } 54393494fc30STejun Heo } 54403494fc30STejun Heo 54413494fc30STejun Heo /** 544255df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 544355df0933SImran Khan * @wq: workqueue whose state will be printed 54443494fc30STejun Heo */ 544555df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 54463494fc30STejun Heo { 54473494fc30STejun Heo struct pool_workqueue *pwq; 54483494fc30STejun Heo bool idle = true; 544955df0933SImran Khan unsigned long flags; 54503494fc30STejun Heo 54513494fc30STejun Heo for_each_pwq(pwq, wq) { 5452afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 54533494fc30STejun Heo idle = false; 54543494fc30STejun Heo break; 54553494fc30STejun Heo } 54563494fc30STejun Heo } 545755df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 545855df0933SImran Khan return; 54593494fc30STejun Heo 54603494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 54613494fc30STejun Heo 54623494fc30STejun Heo for_each_pwq(pwq, wq) { 5463a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 5464afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 546557116ce1SJohan Hovold /* 546657116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 546757116ce1SJohan Hovold * drivers that queue work while holding locks 546857116ce1SJohan Hovold * also taken in their write paths. 546957116ce1SJohan Hovold */ 547057116ce1SJohan Hovold printk_deferred_enter(); 54713494fc30STejun Heo show_pwq(pwq); 547257116ce1SJohan Hovold printk_deferred_exit(); 547357116ce1SJohan Hovold } 5474a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 547562635ea8SSergey Senozhatsky /* 547662635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 547755df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 547862635ea8SSergey Senozhatsky * hard lockup. 547962635ea8SSergey Senozhatsky */ 548062635ea8SSergey Senozhatsky touch_nmi_watchdog(); 54813494fc30STejun Heo } 548255df0933SImran Khan 54833494fc30STejun Heo } 54843494fc30STejun Heo 548555df0933SImran Khan /** 548655df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 548755df0933SImran Khan * @pool: worker pool whose state will be printed 548855df0933SImran Khan */ 548955df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 549055df0933SImran Khan { 54913494fc30STejun Heo struct worker *worker; 54923494fc30STejun Heo bool first = true; 549355df0933SImran Khan unsigned long flags; 5494335a42ebSPetr Mladek unsigned long hung = 0; 54953494fc30STejun Heo 5496a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 54973494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 54983494fc30STejun Heo goto next_pool; 5499335a42ebSPetr Mladek 5500335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5501335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5502335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5503335a42ebSPetr Mladek 550457116ce1SJohan Hovold /* 550557116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 550657116ce1SJohan Hovold * queue work while holding locks also taken in their write 550757116ce1SJohan Hovold * paths. 550857116ce1SJohan Hovold */ 550957116ce1SJohan Hovold printk_deferred_enter(); 55103494fc30STejun Heo pr_info("pool %d:", pool->id); 55113494fc30STejun Heo pr_cont_pool_info(pool); 5512335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 55133494fc30STejun Heo if (pool->manager) 55143494fc30STejun Heo pr_cont(" manager: %d", 55153494fc30STejun Heo task_pid_nr(pool->manager->task)); 55163494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 55173494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 55183494fc30STejun Heo task_pid_nr(worker->task)); 55193494fc30STejun Heo first = false; 55203494fc30STejun Heo } 55213494fc30STejun Heo pr_cont("\n"); 552257116ce1SJohan Hovold printk_deferred_exit(); 55233494fc30STejun Heo next_pool: 5524a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 552562635ea8SSergey Senozhatsky /* 552662635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 552755df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 552862635ea8SSergey Senozhatsky * hard lockup. 552962635ea8SSergey Senozhatsky */ 553062635ea8SSergey Senozhatsky touch_nmi_watchdog(); 553155df0933SImran Khan 55323494fc30STejun Heo } 55333494fc30STejun Heo 553455df0933SImran Khan /** 553555df0933SImran Khan * show_all_workqueues - dump workqueue state 553655df0933SImran Khan * 5537704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 553855df0933SImran Khan */ 553955df0933SImran Khan void show_all_workqueues(void) 554055df0933SImran Khan { 554155df0933SImran Khan struct workqueue_struct *wq; 554255df0933SImran Khan struct worker_pool *pool; 554355df0933SImran Khan int pi; 554455df0933SImran Khan 554555df0933SImran Khan rcu_read_lock(); 554655df0933SImran Khan 554755df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 554855df0933SImran Khan 554955df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 555055df0933SImran Khan show_one_workqueue(wq); 555155df0933SImran Khan 555255df0933SImran Khan for_each_pool(pool, pi) 555355df0933SImran Khan show_one_worker_pool(pool); 555455df0933SImran Khan 555524acfb71SThomas Gleixner rcu_read_unlock(); 55563494fc30STejun Heo } 55573494fc30STejun Heo 5558704bc669SJungseung Lee /** 5559704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5560704bc669SJungseung Lee * 5561704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5562704bc669SJungseung Lee * still busy. 5563704bc669SJungseung Lee */ 5564704bc669SJungseung Lee void show_freezable_workqueues(void) 5565704bc669SJungseung Lee { 5566704bc669SJungseung Lee struct workqueue_struct *wq; 5567704bc669SJungseung Lee 5568704bc669SJungseung Lee rcu_read_lock(); 5569704bc669SJungseung Lee 5570704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5571704bc669SJungseung Lee 5572704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5573704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5574704bc669SJungseung Lee continue; 5575704bc669SJungseung Lee show_one_workqueue(wq); 5576704bc669SJungseung Lee } 5577704bc669SJungseung Lee 5578704bc669SJungseung Lee rcu_read_unlock(); 5579704bc669SJungseung Lee } 5580704bc669SJungseung Lee 55816b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 55826b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 55836b59808bSTejun Heo { 55846b59808bSTejun Heo int off; 55856b59808bSTejun Heo 55866b59808bSTejun Heo /* always show the actual comm */ 55876b59808bSTejun Heo off = strscpy(buf, task->comm, size); 55886b59808bSTejun Heo if (off < 0) 55896b59808bSTejun Heo return; 55906b59808bSTejun Heo 5591197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 55926b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 55936b59808bSTejun Heo 5594197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5595197f6accSTejun Heo struct worker *worker = kthread_data(task); 5596197f6accSTejun Heo struct worker_pool *pool = worker->pool; 55976b59808bSTejun Heo 55986b59808bSTejun Heo if (pool) { 5599a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 56006b59808bSTejun Heo /* 5601197f6accSTejun Heo * ->desc tracks information (wq name or 5602197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5603197f6accSTejun Heo * current, prepend '+', otherwise '-'. 56046b59808bSTejun Heo */ 56056b59808bSTejun Heo if (worker->desc[0] != '\0') { 56066b59808bSTejun Heo if (worker->current_work) 56076b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 56086b59808bSTejun Heo worker->desc); 56096b59808bSTejun Heo else 56106b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 56116b59808bSTejun Heo worker->desc); 56126b59808bSTejun Heo } 5613a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 56146b59808bSTejun Heo } 5615197f6accSTejun Heo } 56166b59808bSTejun Heo 56176b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 56186b59808bSTejun Heo } 56196b59808bSTejun Heo 562066448bc2SMathieu Malaterre #ifdef CONFIG_SMP 562166448bc2SMathieu Malaterre 5622db7bccf4STejun Heo /* 5623db7bccf4STejun Heo * CPU hotplug. 5624db7bccf4STejun Heo * 5625e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5626112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5627706026c2STejun Heo * pool which make migrating pending and scheduled works very 5628e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 562994cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5630e22bee78STejun Heo * blocked draining impractical. 5631e22bee78STejun Heo * 563224647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5633628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5634628c78e7STejun Heo * cpu comes back online. 5635db7bccf4STejun Heo */ 5636db7bccf4STejun Heo 5637e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5638db7bccf4STejun Heo { 56394ce62e9eSTejun Heo struct worker_pool *pool; 5640db7bccf4STejun Heo struct worker *worker; 5641db7bccf4STejun Heo 5642f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 56431258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5644a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5645e22bee78STejun Heo 5646f2d5a0eeSTejun Heo /* 564792f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 564894cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 564911b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5650b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5651b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5652b4ac9384SLai Jiangshan * is on the same cpu. 5653f2d5a0eeSTejun Heo */ 5654da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5655403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5656db7bccf4STejun Heo 565724647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5658f2d5a0eeSTejun Heo 5659e22bee78STejun Heo /* 5660989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5661989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5662989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5663989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5664989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5665eb283428SLai Jiangshan * are served by workers tied to the pool. 5666e22bee78STejun Heo */ 5667bc35f7efSLai Jiangshan pool->nr_running = 0; 5668eb283428SLai Jiangshan 5669eb283428SLai Jiangshan /* 5670eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5671eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5672eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5673eb283428SLai Jiangshan */ 56740219a352STejun Heo kick_pool(pool); 5675989442d7SLai Jiangshan 5676a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5677989442d7SLai Jiangshan 5678793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5679793777bcSValentin Schneider unbind_worker(worker); 5680989442d7SLai Jiangshan 5681989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5682eb283428SLai Jiangshan } 5683db7bccf4STejun Heo } 5684db7bccf4STejun Heo 5685bd7c089eSTejun Heo /** 5686bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5687bd7c089eSTejun Heo * @pool: pool of interest 5688bd7c089eSTejun Heo * 5689a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5690bd7c089eSTejun Heo */ 5691bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5692bd7c089eSTejun Heo { 5693a9ab775bSTejun Heo struct worker *worker; 5694bd7c089eSTejun Heo 56951258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5696bd7c089eSTejun Heo 5697bd7c089eSTejun Heo /* 5698a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5699a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5700402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5701a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5702a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5703bd7c089eSTejun Heo */ 5704c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5705c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5706c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 57079546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 5708c63a2e52SValentin Schneider } 5709a9ab775bSTejun Heo 5710a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5711f7c17d26SWanpeng Li 57123de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5713a9ab775bSTejun Heo 5714da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5715a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5716a9ab775bSTejun Heo 5717a9ab775bSTejun Heo /* 5718a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5719a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5720a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5721a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5722a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5723a9ab775bSTejun Heo * concurrency management. Note that when or whether 5724a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5725a9ab775bSTejun Heo * 5726c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5727a9ab775bSTejun Heo * tested without holding any lock in 57286d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5729a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5730a9ab775bSTejun Heo * management operations. 5731bd7c089eSTejun Heo */ 5732a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5733a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5734a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5735c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5736bd7c089eSTejun Heo } 5737a9ab775bSTejun Heo 5738a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5739bd7c089eSTejun Heo } 5740bd7c089eSTejun Heo 57417dbc725eSTejun Heo /** 57427dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 57437dbc725eSTejun Heo * @pool: unbound pool of interest 57447dbc725eSTejun Heo * @cpu: the CPU which is coming up 57457dbc725eSTejun Heo * 57467dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 57477dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 57487dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 57497dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 57507dbc725eSTejun Heo */ 57517dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 57527dbc725eSTejun Heo { 57537dbc725eSTejun Heo static cpumask_t cpumask; 57547dbc725eSTejun Heo struct worker *worker; 57557dbc725eSTejun Heo 57561258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 57577dbc725eSTejun Heo 57587dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 57597dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 57607dbc725eSTejun Heo return; 57617dbc725eSTejun Heo 57627dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 57637dbc725eSTejun Heo 57647dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5765da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5766d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 57677dbc725eSTejun Heo } 57687dbc725eSTejun Heo 57697ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 57701da177e4SLinus Torvalds { 57714ce62e9eSTejun Heo struct worker_pool *pool; 57721da177e4SLinus Torvalds 5773f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 57743ce63377STejun Heo if (pool->nr_workers) 57753ce63377STejun Heo continue; 5776051e1850SLai Jiangshan if (!create_worker(pool)) 57777ee681b2SThomas Gleixner return -ENOMEM; 57783af24433SOleg Nesterov } 57797ee681b2SThomas Gleixner return 0; 57807ee681b2SThomas Gleixner } 57811da177e4SLinus Torvalds 57827ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 57837ee681b2SThomas Gleixner { 57847ee681b2SThomas Gleixner struct worker_pool *pool; 57857ee681b2SThomas Gleixner struct workqueue_struct *wq; 57867ee681b2SThomas Gleixner int pi; 57877ee681b2SThomas Gleixner 578868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 57897dbc725eSTejun Heo 57907dbc725eSTejun Heo for_each_pool(pool, pi) { 57911258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 579294cf58bbSTejun Heo 5793f05b558dSLai Jiangshan if (pool->cpu == cpu) 579494cf58bbSTejun Heo rebind_workers(pool); 5795f05b558dSLai Jiangshan else if (pool->cpu < 0) 57967dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 579794cf58bbSTejun Heo 57981258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 579994cf58bbSTejun Heo } 58007dbc725eSTejun Heo 5801fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 58024cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 580384193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 580484193c07STejun Heo 580584193c07STejun Heo if (attrs) { 580684193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 58074cbfd3deSTejun Heo int tcpu; 58084cbfd3deSTejun Heo 580984193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5810fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 58114cbfd3deSTejun Heo } 58124cbfd3deSTejun Heo } 58134c16bd32STejun Heo 581468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 58157ee681b2SThomas Gleixner return 0; 581665758202STejun Heo } 581765758202STejun Heo 58187ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 581965758202STejun Heo { 58204c16bd32STejun Heo struct workqueue_struct *wq; 58218db25e78STejun Heo 58224c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5823e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5824e8b3f8dbSLai Jiangshan return -1; 5825e8b3f8dbSLai Jiangshan 5826e8b3f8dbSLai Jiangshan unbind_workers(cpu); 58274c16bd32STejun Heo 5828fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 58294c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 58304cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 583184193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 583284193c07STejun Heo 583384193c07STejun Heo if (attrs) { 583484193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 58354cbfd3deSTejun Heo int tcpu; 58364cbfd3deSTejun Heo 583784193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5838fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 58394cbfd3deSTejun Heo } 58404cbfd3deSTejun Heo } 58414c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 58424c16bd32STejun Heo 58437ee681b2SThomas Gleixner return 0; 584465758202STejun Heo } 584565758202STejun Heo 58462d3854a3SRusty Russell struct work_for_cpu { 5847ed48ece2STejun Heo struct work_struct work; 58482d3854a3SRusty Russell long (*fn)(void *); 58492d3854a3SRusty Russell void *arg; 58502d3854a3SRusty Russell long ret; 58512d3854a3SRusty Russell }; 58522d3854a3SRusty Russell 5853ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 58542d3854a3SRusty Russell { 5855ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5856ed48ece2STejun Heo 58572d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 58582d3854a3SRusty Russell } 58592d3854a3SRusty Russell 58602d3854a3SRusty Russell /** 5861265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 58622d3854a3SRusty Russell * @cpu: the cpu to run on 58632d3854a3SRusty Russell * @fn: the function to run 58642d3854a3SRusty Russell * @arg: the function arg 5865265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 58662d3854a3SRusty Russell * 586731ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 58686b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5869d185af30SYacine Belkadi * 5870d185af30SYacine Belkadi * Return: The value @fn returns. 58712d3854a3SRusty Russell */ 5872265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 5873265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 58742d3854a3SRusty Russell { 5875ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 58762d3854a3SRusty Russell 5877265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 5878ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 587912997d1aSBjorn Helgaas flush_work(&wfc.work); 5880440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 58812d3854a3SRusty Russell return wfc.ret; 58822d3854a3SRusty Russell } 5883265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 58840e8d6a93SThomas Gleixner 58850e8d6a93SThomas Gleixner /** 5886265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 58870e8d6a93SThomas Gleixner * @cpu: the cpu to run on 58880e8d6a93SThomas Gleixner * @fn: the function to run 58890e8d6a93SThomas Gleixner * @arg: the function argument 5890265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 58910e8d6a93SThomas Gleixner * 58920e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 58930e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 58940e8d6a93SThomas Gleixner * 58950e8d6a93SThomas Gleixner * Return: The value @fn returns. 58960e8d6a93SThomas Gleixner */ 5897265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 5898265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 58990e8d6a93SThomas Gleixner { 59000e8d6a93SThomas Gleixner long ret = -ENODEV; 59010e8d6a93SThomas Gleixner 5902ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 59030e8d6a93SThomas Gleixner if (cpu_online(cpu)) 5904265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 5905ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 59060e8d6a93SThomas Gleixner return ret; 59070e8d6a93SThomas Gleixner } 5908265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 59092d3854a3SRusty Russell #endif /* CONFIG_SMP */ 59102d3854a3SRusty Russell 5911a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5912e7577c50SRusty Russell 5913a0a1a5fdSTejun Heo /** 5914a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5915a0a1a5fdSTejun Heo * 591658a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5917f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5918706026c2STejun Heo * pool->worklist. 5919a0a1a5fdSTejun Heo * 5920a0a1a5fdSTejun Heo * CONTEXT: 5921a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5922a0a1a5fdSTejun Heo */ 5923a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5924a0a1a5fdSTejun Heo { 592524b8a847STejun Heo struct workqueue_struct *wq; 5926a0a1a5fdSTejun Heo 592768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5928a0a1a5fdSTejun Heo 59296183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5930a0a1a5fdSTejun Heo workqueue_freezing = true; 5931a0a1a5fdSTejun Heo 593224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5933a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5934a045a272STejun Heo wq_adjust_max_active(wq); 5935a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5936a1056305STejun Heo } 59375bcab335STejun Heo 593868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5939a0a1a5fdSTejun Heo } 5940a0a1a5fdSTejun Heo 5941a0a1a5fdSTejun Heo /** 594258a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5943a0a1a5fdSTejun Heo * 5944a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5945a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5946a0a1a5fdSTejun Heo * 5947a0a1a5fdSTejun Heo * CONTEXT: 594868e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5949a0a1a5fdSTejun Heo * 5950d185af30SYacine Belkadi * Return: 595158a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 595258a69cb4STejun Heo * is complete. 5953a0a1a5fdSTejun Heo */ 5954a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5955a0a1a5fdSTejun Heo { 5956a0a1a5fdSTejun Heo bool busy = false; 595724b8a847STejun Heo struct workqueue_struct *wq; 595824b8a847STejun Heo struct pool_workqueue *pwq; 5959a0a1a5fdSTejun Heo 596068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5961a0a1a5fdSTejun Heo 59626183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5963a0a1a5fdSTejun Heo 596424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 596524b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 596624b8a847STejun Heo continue; 5967a0a1a5fdSTejun Heo /* 5968a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5969a0a1a5fdSTejun Heo * to peek without lock. 5970a0a1a5fdSTejun Heo */ 597124acfb71SThomas Gleixner rcu_read_lock(); 597224b8a847STejun Heo for_each_pwq(pwq, wq) { 59736183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5974112202d9STejun Heo if (pwq->nr_active) { 5975a0a1a5fdSTejun Heo busy = true; 597624acfb71SThomas Gleixner rcu_read_unlock(); 5977a0a1a5fdSTejun Heo goto out_unlock; 5978a0a1a5fdSTejun Heo } 5979a0a1a5fdSTejun Heo } 598024acfb71SThomas Gleixner rcu_read_unlock(); 5981a0a1a5fdSTejun Heo } 5982a0a1a5fdSTejun Heo out_unlock: 598368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5984a0a1a5fdSTejun Heo return busy; 5985a0a1a5fdSTejun Heo } 5986a0a1a5fdSTejun Heo 5987a0a1a5fdSTejun Heo /** 5988a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5989a0a1a5fdSTejun Heo * 5990a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5991706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5992a0a1a5fdSTejun Heo * 5993a0a1a5fdSTejun Heo * CONTEXT: 5994a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5995a0a1a5fdSTejun Heo */ 5996a0a1a5fdSTejun Heo void thaw_workqueues(void) 5997a0a1a5fdSTejun Heo { 599824b8a847STejun Heo struct workqueue_struct *wq; 5999a0a1a5fdSTejun Heo 600068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6001a0a1a5fdSTejun Heo 6002a0a1a5fdSTejun Heo if (!workqueue_freezing) 6003a0a1a5fdSTejun Heo goto out_unlock; 6004a0a1a5fdSTejun Heo 600574b414eaSLai Jiangshan workqueue_freezing = false; 600624b8a847STejun Heo 600724b8a847STejun Heo /* restore max_active and repopulate worklist */ 600824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6009a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6010a045a272STejun Heo wq_adjust_max_active(wq); 6011a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 601224b8a847STejun Heo } 601324b8a847STejun Heo 6014a0a1a5fdSTejun Heo out_unlock: 601568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6016a0a1a5fdSTejun Heo } 6017a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6018a0a1a5fdSTejun Heo 601999c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6020042f7df1SLai Jiangshan { 6021042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6022042f7df1SLai Jiangshan int ret = 0; 6023042f7df1SLai Jiangshan struct workqueue_struct *wq; 6024042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6025042f7df1SLai Jiangshan 6026042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6027042f7df1SLai Jiangshan 6028042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 6029042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 6030042f7df1SLai Jiangshan continue; 6031ca10d851SWaiman Long 6032042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 6033ca10d851SWaiman Long if (!list_empty(&wq->pwqs)) { 6034ca10d851SWaiman Long if (wq->flags & __WQ_ORDERED_EXPLICIT) 6035042f7df1SLai Jiangshan continue; 6036ca10d851SWaiman Long wq->flags &= ~__WQ_ORDERED; 6037ca10d851SWaiman Long } 6038042f7df1SLai Jiangshan 603999c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 604084193c07STejun Heo if (IS_ERR(ctx)) { 604184193c07STejun Heo ret = PTR_ERR(ctx); 6042042f7df1SLai Jiangshan break; 6043042f7df1SLai Jiangshan } 6044042f7df1SLai Jiangshan 6045042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6046042f7df1SLai Jiangshan } 6047042f7df1SLai Jiangshan 6048042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6049042f7df1SLai Jiangshan if (!ret) 6050042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6051042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6052042f7df1SLai Jiangshan } 6053042f7df1SLai Jiangshan 605499c621efSLai Jiangshan if (!ret) { 605599c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 605699c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 605799c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 605899c621efSLai Jiangshan } 6059042f7df1SLai Jiangshan return ret; 6060042f7df1SLai Jiangshan } 6061042f7df1SLai Jiangshan 6062042f7df1SLai Jiangshan /** 6063fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6064fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6065fe28f631SWaiman Long * 6066fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6067fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6068fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6069fe28f631SWaiman Long */ 6070fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6071fe28f631SWaiman Long { 6072fe28f631SWaiman Long cpumask_var_t cpumask; 6073fe28f631SWaiman Long int ret = 0; 6074fe28f631SWaiman Long 6075fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6076fe28f631SWaiman Long return -ENOMEM; 6077fe28f631SWaiman Long 6078fe28f631SWaiman Long lockdep_assert_cpus_held(); 6079fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6080fe28f631SWaiman Long 6081fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6082fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6083fe28f631SWaiman Long 6084fe28f631SWaiman Long /* 6085fe28f631SWaiman Long * If the operation fails, it will fall back to 6086fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6087fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6088fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6089fe28f631SWaiman Long */ 6090fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6091fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6092fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6093fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6094fe28f631SWaiman Long 6095fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6096fe28f631SWaiman Long free_cpumask_var(cpumask); 6097fe28f631SWaiman Long return ret; 6098fe28f631SWaiman Long } 6099fe28f631SWaiman Long 610063c5484eSTejun Heo static int parse_affn_scope(const char *val) 610163c5484eSTejun Heo { 610263c5484eSTejun Heo int i; 610363c5484eSTejun Heo 610463c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 610563c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 610663c5484eSTejun Heo return i; 610763c5484eSTejun Heo } 610863c5484eSTejun Heo return -EINVAL; 610963c5484eSTejun Heo } 611063c5484eSTejun Heo 611163c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 611263c5484eSTejun Heo { 6113523a301eSTejun Heo struct workqueue_struct *wq; 6114523a301eSTejun Heo int affn, cpu; 611563c5484eSTejun Heo 611663c5484eSTejun Heo affn = parse_affn_scope(val); 611763c5484eSTejun Heo if (affn < 0) 611863c5484eSTejun Heo return affn; 6119523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6120523a301eSTejun Heo return -EINVAL; 6121523a301eSTejun Heo 6122523a301eSTejun Heo cpus_read_lock(); 6123523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 612463c5484eSTejun Heo 612563c5484eSTejun Heo wq_affn_dfl = affn; 6126523a301eSTejun Heo 6127523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6128523a301eSTejun Heo for_each_online_cpu(cpu) { 6129523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6130523a301eSTejun Heo } 6131523a301eSTejun Heo } 6132523a301eSTejun Heo 6133523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6134523a301eSTejun Heo cpus_read_unlock(); 6135523a301eSTejun Heo 613663c5484eSTejun Heo return 0; 613763c5484eSTejun Heo } 613863c5484eSTejun Heo 613963c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 614063c5484eSTejun Heo { 614163c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 614263c5484eSTejun Heo } 614363c5484eSTejun Heo 614463c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 614563c5484eSTejun Heo .set = wq_affn_dfl_set, 614663c5484eSTejun Heo .get = wq_affn_dfl_get, 614763c5484eSTejun Heo }; 614863c5484eSTejun Heo 614963c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 615063c5484eSTejun Heo 61516ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 61526ba94429SFrederic Weisbecker /* 61536ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 61546ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 61556ba94429SFrederic Weisbecker * following attributes. 61566ba94429SFrederic Weisbecker * 61576ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 61586ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 61596ba94429SFrederic Weisbecker * 61606ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 61616ba94429SFrederic Weisbecker * 61626ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 61636ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 616463c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 61658639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 61666ba94429SFrederic Weisbecker */ 61676ba94429SFrederic Weisbecker struct wq_device { 61686ba94429SFrederic Weisbecker struct workqueue_struct *wq; 61696ba94429SFrederic Weisbecker struct device dev; 61706ba94429SFrederic Weisbecker }; 61716ba94429SFrederic Weisbecker 61726ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 61736ba94429SFrederic Weisbecker { 61746ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 61756ba94429SFrederic Weisbecker 61766ba94429SFrederic Weisbecker return wq_dev->wq; 61776ba94429SFrederic Weisbecker } 61786ba94429SFrederic Weisbecker 61796ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 61806ba94429SFrederic Weisbecker char *buf) 61816ba94429SFrederic Weisbecker { 61826ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 61836ba94429SFrederic Weisbecker 61846ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 61856ba94429SFrederic Weisbecker } 61866ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 61876ba94429SFrederic Weisbecker 61886ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 61896ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 61906ba94429SFrederic Weisbecker { 61916ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 61926ba94429SFrederic Weisbecker 61936ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 61946ba94429SFrederic Weisbecker } 61956ba94429SFrederic Weisbecker 61966ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 61976ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 61986ba94429SFrederic Weisbecker size_t count) 61996ba94429SFrederic Weisbecker { 62006ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62016ba94429SFrederic Weisbecker int val; 62026ba94429SFrederic Weisbecker 62036ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 62046ba94429SFrederic Weisbecker return -EINVAL; 62056ba94429SFrederic Weisbecker 62066ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 62076ba94429SFrederic Weisbecker return count; 62086ba94429SFrederic Weisbecker } 62096ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 62106ba94429SFrederic Weisbecker 62116ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 62126ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 62136ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 62146ba94429SFrederic Weisbecker NULL, 62156ba94429SFrederic Weisbecker }; 62166ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 62176ba94429SFrederic Weisbecker 621849277a5bSWaiman Long static void apply_wqattrs_lock(void) 621949277a5bSWaiman Long { 622049277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 622149277a5bSWaiman Long cpus_read_lock(); 622249277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 622349277a5bSWaiman Long } 622449277a5bSWaiman Long 622549277a5bSWaiman Long static void apply_wqattrs_unlock(void) 622649277a5bSWaiman Long { 622749277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 622849277a5bSWaiman Long cpus_read_unlock(); 622949277a5bSWaiman Long } 623049277a5bSWaiman Long 62316ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 62326ba94429SFrederic Weisbecker char *buf) 62336ba94429SFrederic Weisbecker { 62346ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62356ba94429SFrederic Weisbecker int written; 62366ba94429SFrederic Weisbecker 62376ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 62386ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 62396ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 62406ba94429SFrederic Weisbecker 62416ba94429SFrederic Weisbecker return written; 62426ba94429SFrederic Weisbecker } 62436ba94429SFrederic Weisbecker 62446ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 62456ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 62466ba94429SFrederic Weisbecker { 62476ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 62486ba94429SFrederic Weisbecker 6249899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6250899a94feSLai Jiangshan 6251be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 62526ba94429SFrederic Weisbecker if (!attrs) 62536ba94429SFrederic Weisbecker return NULL; 62546ba94429SFrederic Weisbecker 62556ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 62566ba94429SFrederic Weisbecker return attrs; 62576ba94429SFrederic Weisbecker } 62586ba94429SFrederic Weisbecker 62596ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 62606ba94429SFrederic Weisbecker const char *buf, size_t count) 62616ba94429SFrederic Weisbecker { 62626ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62636ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6264d4d3e257SLai Jiangshan int ret = -ENOMEM; 6265d4d3e257SLai Jiangshan 6266d4d3e257SLai Jiangshan apply_wqattrs_lock(); 62676ba94429SFrederic Weisbecker 62686ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 62696ba94429SFrederic Weisbecker if (!attrs) 6270d4d3e257SLai Jiangshan goto out_unlock; 62716ba94429SFrederic Weisbecker 62726ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 62736ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6274d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 62756ba94429SFrederic Weisbecker else 62766ba94429SFrederic Weisbecker ret = -EINVAL; 62776ba94429SFrederic Weisbecker 6278d4d3e257SLai Jiangshan out_unlock: 6279d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 62806ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 62816ba94429SFrederic Weisbecker return ret ?: count; 62826ba94429SFrederic Weisbecker } 62836ba94429SFrederic Weisbecker 62846ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 62856ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 62866ba94429SFrederic Weisbecker { 62876ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62886ba94429SFrederic Weisbecker int written; 62896ba94429SFrederic Weisbecker 62906ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 62916ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 62926ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 62936ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 62946ba94429SFrederic Weisbecker return written; 62956ba94429SFrederic Weisbecker } 62966ba94429SFrederic Weisbecker 62976ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 62986ba94429SFrederic Weisbecker struct device_attribute *attr, 62996ba94429SFrederic Weisbecker const char *buf, size_t count) 63006ba94429SFrederic Weisbecker { 63016ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 63026ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6303d4d3e257SLai Jiangshan int ret = -ENOMEM; 6304d4d3e257SLai Jiangshan 6305d4d3e257SLai Jiangshan apply_wqattrs_lock(); 63066ba94429SFrederic Weisbecker 63076ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 63086ba94429SFrederic Weisbecker if (!attrs) 6309d4d3e257SLai Jiangshan goto out_unlock; 63106ba94429SFrederic Weisbecker 63116ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 63126ba94429SFrederic Weisbecker if (!ret) 6313d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 63146ba94429SFrederic Weisbecker 6315d4d3e257SLai Jiangshan out_unlock: 6316d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 63176ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 63186ba94429SFrederic Weisbecker return ret ?: count; 63196ba94429SFrederic Weisbecker } 63206ba94429SFrederic Weisbecker 632163c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 632263c5484eSTejun Heo struct device_attribute *attr, char *buf) 632363c5484eSTejun Heo { 632463c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 632563c5484eSTejun Heo int written; 632663c5484eSTejun Heo 632763c5484eSTejun Heo mutex_lock(&wq->mutex); 6328523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6329523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6330523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6331523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6332523a301eSTejun Heo else 633363c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 633463c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 633563c5484eSTejun Heo mutex_unlock(&wq->mutex); 633663c5484eSTejun Heo 633763c5484eSTejun Heo return written; 633863c5484eSTejun Heo } 633963c5484eSTejun Heo 634063c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 634163c5484eSTejun Heo struct device_attribute *attr, 634263c5484eSTejun Heo const char *buf, size_t count) 634363c5484eSTejun Heo { 634463c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 634563c5484eSTejun Heo struct workqueue_attrs *attrs; 634663c5484eSTejun Heo int affn, ret = -ENOMEM; 634763c5484eSTejun Heo 634863c5484eSTejun Heo affn = parse_affn_scope(buf); 634963c5484eSTejun Heo if (affn < 0) 635063c5484eSTejun Heo return affn; 635163c5484eSTejun Heo 635263c5484eSTejun Heo apply_wqattrs_lock(); 635363c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 635463c5484eSTejun Heo if (attrs) { 635563c5484eSTejun Heo attrs->affn_scope = affn; 635663c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 635763c5484eSTejun Heo } 635863c5484eSTejun Heo apply_wqattrs_unlock(); 635963c5484eSTejun Heo free_workqueue_attrs(attrs); 636063c5484eSTejun Heo return ret ?: count; 636163c5484eSTejun Heo } 636263c5484eSTejun Heo 63638639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 63648639ecebSTejun Heo struct device_attribute *attr, char *buf) 63658639ecebSTejun Heo { 63668639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 63678639ecebSTejun Heo 63688639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 63698639ecebSTejun Heo wq->unbound_attrs->affn_strict); 63708639ecebSTejun Heo } 63718639ecebSTejun Heo 63728639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 63738639ecebSTejun Heo struct device_attribute *attr, 63748639ecebSTejun Heo const char *buf, size_t count) 63758639ecebSTejun Heo { 63768639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 63778639ecebSTejun Heo struct workqueue_attrs *attrs; 63788639ecebSTejun Heo int v, ret = -ENOMEM; 63798639ecebSTejun Heo 63808639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 63818639ecebSTejun Heo return -EINVAL; 63828639ecebSTejun Heo 63838639ecebSTejun Heo apply_wqattrs_lock(); 63848639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 63858639ecebSTejun Heo if (attrs) { 63868639ecebSTejun Heo attrs->affn_strict = (bool)v; 63878639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 63888639ecebSTejun Heo } 63898639ecebSTejun Heo apply_wqattrs_unlock(); 63908639ecebSTejun Heo free_workqueue_attrs(attrs); 63918639ecebSTejun Heo return ret ?: count; 63928639ecebSTejun Heo } 63938639ecebSTejun Heo 63946ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 63956ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 63966ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 639763c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 63988639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 63996ba94429SFrederic Weisbecker __ATTR_NULL, 64006ba94429SFrederic Weisbecker }; 64016ba94429SFrederic Weisbecker 64026ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 64036ba94429SFrederic Weisbecker .name = "workqueue", 64046ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 64056ba94429SFrederic Weisbecker }; 64066ba94429SFrederic Weisbecker 640749277a5bSWaiman Long /** 640849277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 640949277a5bSWaiman Long * @cpumask: the cpumask to set 641049277a5bSWaiman Long * 641149277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 641249277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 641349277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 641449277a5bSWaiman Long * 641549277a5bSWaiman Long * Return: 0 - Success 641649277a5bSWaiman Long * -EINVAL - Invalid @cpumask 641749277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 641849277a5bSWaiman Long */ 641949277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 642049277a5bSWaiman Long { 642149277a5bSWaiman Long int ret = -EINVAL; 642249277a5bSWaiman Long 642349277a5bSWaiman Long /* 642449277a5bSWaiman Long * Not excluding isolated cpus on purpose. 642549277a5bSWaiman Long * If the user wishes to include them, we allow that. 642649277a5bSWaiman Long */ 642749277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 642849277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 642949277a5bSWaiman Long apply_wqattrs_lock(); 643049277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 643149277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 643249277a5bSWaiman Long ret = 0; 643349277a5bSWaiman Long goto out_unlock; 643449277a5bSWaiman Long } 643549277a5bSWaiman Long 643649277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 643749277a5bSWaiman Long 643849277a5bSWaiman Long out_unlock: 643949277a5bSWaiman Long apply_wqattrs_unlock(); 644049277a5bSWaiman Long } 644149277a5bSWaiman Long 644249277a5bSWaiman Long return ret; 644349277a5bSWaiman Long } 644449277a5bSWaiman Long 6445fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 6446fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 6447b05a7928SFrederic Weisbecker { 6448b05a7928SFrederic Weisbecker int written; 6449b05a7928SFrederic Weisbecker 6450042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6451fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 6452042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6453b05a7928SFrederic Weisbecker 6454b05a7928SFrederic Weisbecker return written; 6455b05a7928SFrederic Weisbecker } 6456b05a7928SFrederic Weisbecker 6457fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 6458fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6459fe28f631SWaiman Long { 6460fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 6461fe28f631SWaiman Long } 6462fe28f631SWaiman Long 6463fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 6464fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6465fe28f631SWaiman Long { 6466fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 6467fe28f631SWaiman Long } 6468fe28f631SWaiman Long 6469fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 6470fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6471fe28f631SWaiman Long { 6472fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 6473fe28f631SWaiman Long } 6474fe28f631SWaiman Long 6475042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6476042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6477042f7df1SLai Jiangshan { 6478042f7df1SLai Jiangshan cpumask_var_t cpumask; 6479042f7df1SLai Jiangshan int ret; 6480042f7df1SLai Jiangshan 6481042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6482042f7df1SLai Jiangshan return -ENOMEM; 6483042f7df1SLai Jiangshan 6484042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 6485042f7df1SLai Jiangshan if (!ret) 6486042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 6487042f7df1SLai Jiangshan 6488042f7df1SLai Jiangshan free_cpumask_var(cpumask); 6489042f7df1SLai Jiangshan return ret ? ret : count; 6490042f7df1SLai Jiangshan } 6491042f7df1SLai Jiangshan 6492fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 6493042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 6494fe28f631SWaiman Long wq_unbound_cpumask_store), 6495fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 6496fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 6497fe28f631SWaiman Long __ATTR_NULL, 6498fe28f631SWaiman Long }; 6499b05a7928SFrederic Weisbecker 65006ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 65016ba94429SFrederic Weisbecker { 6502686f6697SGreg Kroah-Hartman struct device *dev_root; 6503b05a7928SFrederic Weisbecker int err; 6504b05a7928SFrederic Weisbecker 6505b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 6506b05a7928SFrederic Weisbecker if (err) 6507b05a7928SFrederic Weisbecker return err; 6508b05a7928SFrederic Weisbecker 6509686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 6510686f6697SGreg Kroah-Hartman if (dev_root) { 6511fe28f631SWaiman Long struct device_attribute *attr; 6512fe28f631SWaiman Long 6513fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 6514fe28f631SWaiman Long err = device_create_file(dev_root, attr); 6515fe28f631SWaiman Long if (err) 6516fe28f631SWaiman Long break; 6517fe28f631SWaiman Long } 6518686f6697SGreg Kroah-Hartman put_device(dev_root); 6519686f6697SGreg Kroah-Hartman } 6520686f6697SGreg Kroah-Hartman return err; 65216ba94429SFrederic Weisbecker } 65226ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 65236ba94429SFrederic Weisbecker 65246ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 65256ba94429SFrederic Weisbecker { 65266ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 65276ba94429SFrederic Weisbecker 65286ba94429SFrederic Weisbecker kfree(wq_dev); 65296ba94429SFrederic Weisbecker } 65306ba94429SFrederic Weisbecker 65316ba94429SFrederic Weisbecker /** 65326ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 65336ba94429SFrederic Weisbecker * @wq: the workqueue to register 65346ba94429SFrederic Weisbecker * 65356ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 65366ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 65376ba94429SFrederic Weisbecker * which is the preferred method. 65386ba94429SFrederic Weisbecker * 65396ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 65406ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 65416ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 65426ba94429SFrederic Weisbecker * attributes. 65436ba94429SFrederic Weisbecker * 65446ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 65456ba94429SFrederic Weisbecker */ 65466ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 65476ba94429SFrederic Weisbecker { 65486ba94429SFrederic Weisbecker struct wq_device *wq_dev; 65496ba94429SFrederic Weisbecker int ret; 65506ba94429SFrederic Weisbecker 65516ba94429SFrederic Weisbecker /* 6552402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 65536ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 65546ba94429SFrederic Weisbecker * workqueues. 65556ba94429SFrederic Weisbecker */ 65560a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 65576ba94429SFrederic Weisbecker return -EINVAL; 65586ba94429SFrederic Weisbecker 65596ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 65606ba94429SFrederic Weisbecker if (!wq_dev) 65616ba94429SFrederic Weisbecker return -ENOMEM; 65626ba94429SFrederic Weisbecker 65636ba94429SFrederic Weisbecker wq_dev->wq = wq; 65646ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 65656ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 656623217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 65676ba94429SFrederic Weisbecker 65686ba94429SFrederic Weisbecker /* 65696ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 65706ba94429SFrederic Weisbecker * everything is ready. 65716ba94429SFrederic Weisbecker */ 65726ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 65736ba94429SFrederic Weisbecker 65746ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 65756ba94429SFrederic Weisbecker if (ret) { 6576537f4146SArvind Yadav put_device(&wq_dev->dev); 65776ba94429SFrederic Weisbecker wq->wq_dev = NULL; 65786ba94429SFrederic Weisbecker return ret; 65796ba94429SFrederic Weisbecker } 65806ba94429SFrederic Weisbecker 65816ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 65826ba94429SFrederic Weisbecker struct device_attribute *attr; 65836ba94429SFrederic Weisbecker 65846ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 65856ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 65866ba94429SFrederic Weisbecker if (ret) { 65876ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 65886ba94429SFrederic Weisbecker wq->wq_dev = NULL; 65896ba94429SFrederic Weisbecker return ret; 65906ba94429SFrederic Weisbecker } 65916ba94429SFrederic Weisbecker } 65926ba94429SFrederic Weisbecker } 65936ba94429SFrederic Weisbecker 65946ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 65956ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 65966ba94429SFrederic Weisbecker return 0; 65976ba94429SFrederic Weisbecker } 65986ba94429SFrederic Weisbecker 65996ba94429SFrederic Weisbecker /** 66006ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 66016ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 66026ba94429SFrederic Weisbecker * 66036ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 66046ba94429SFrederic Weisbecker */ 66056ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 66066ba94429SFrederic Weisbecker { 66076ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 66086ba94429SFrederic Weisbecker 66096ba94429SFrederic Weisbecker if (!wq->wq_dev) 66106ba94429SFrederic Weisbecker return; 66116ba94429SFrederic Weisbecker 66126ba94429SFrederic Weisbecker wq->wq_dev = NULL; 66136ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 66146ba94429SFrederic Weisbecker } 66156ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 66166ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 66176ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 66186ba94429SFrederic Weisbecker 661982607adcSTejun Heo /* 662082607adcSTejun Heo * Workqueue watchdog. 662182607adcSTejun Heo * 662282607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 662382607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 662482607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 662582607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 662682607adcSTejun Heo * largely opaque. 662782607adcSTejun Heo * 662882607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 662982607adcSTejun Heo * state if some pools failed to make forward progress for a while where 663082607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 663182607adcSTejun Heo * 663282607adcSTejun Heo * This mechanism is controlled through the kernel parameter 663382607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 663482607adcSTejun Heo * corresponding sysfs parameter file. 663582607adcSTejun Heo */ 663682607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 663782607adcSTejun Heo 663882607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 66395cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 664082607adcSTejun Heo 664182607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 664282607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 664382607adcSTejun Heo 6644cd2440d6SPetr Mladek /* 6645cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6646cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6647cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6648cd2440d6SPetr Mladek * in all other situations. 6649cd2440d6SPetr Mladek */ 6650cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6651cd2440d6SPetr Mladek { 6652cd2440d6SPetr Mladek struct worker *worker; 6653cd2440d6SPetr Mladek unsigned long flags; 6654cd2440d6SPetr Mladek int bkt; 6655cd2440d6SPetr Mladek 6656cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6657cd2440d6SPetr Mladek 6658cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6659cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6660cd2440d6SPetr Mladek /* 6661cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6662cd2440d6SPetr Mladek * drivers that queue work while holding locks 6663cd2440d6SPetr Mladek * also taken in their write paths. 6664cd2440d6SPetr Mladek */ 6665cd2440d6SPetr Mladek printk_deferred_enter(); 6666cd2440d6SPetr Mladek 6667cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6668cd2440d6SPetr Mladek sched_show_task(worker->task); 6669cd2440d6SPetr Mladek 6670cd2440d6SPetr Mladek printk_deferred_exit(); 6671cd2440d6SPetr Mladek } 6672cd2440d6SPetr Mladek } 6673cd2440d6SPetr Mladek 6674cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6675cd2440d6SPetr Mladek } 6676cd2440d6SPetr Mladek 6677cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6678cd2440d6SPetr Mladek { 6679cd2440d6SPetr Mladek struct worker_pool *pool; 6680cd2440d6SPetr Mladek int pi; 6681cd2440d6SPetr Mladek 6682cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6683cd2440d6SPetr Mladek 6684cd2440d6SPetr Mladek rcu_read_lock(); 6685cd2440d6SPetr Mladek 6686cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6687cd2440d6SPetr Mladek if (pool->cpu_stall) 6688cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6689cd2440d6SPetr Mladek 6690cd2440d6SPetr Mladek } 6691cd2440d6SPetr Mladek 6692cd2440d6SPetr Mladek rcu_read_unlock(); 6693cd2440d6SPetr Mladek } 6694cd2440d6SPetr Mladek 669582607adcSTejun Heo static void wq_watchdog_reset_touched(void) 669682607adcSTejun Heo { 669782607adcSTejun Heo int cpu; 669882607adcSTejun Heo 669982607adcSTejun Heo wq_watchdog_touched = jiffies; 670082607adcSTejun Heo for_each_possible_cpu(cpu) 670182607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 670282607adcSTejun Heo } 670382607adcSTejun Heo 67045cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 670582607adcSTejun Heo { 670682607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 670782607adcSTejun Heo bool lockup_detected = false; 6708cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6709940d71c6SSergey Senozhatsky unsigned long now = jiffies; 671082607adcSTejun Heo struct worker_pool *pool; 671182607adcSTejun Heo int pi; 671282607adcSTejun Heo 671382607adcSTejun Heo if (!thresh) 671482607adcSTejun Heo return; 671582607adcSTejun Heo 671682607adcSTejun Heo rcu_read_lock(); 671782607adcSTejun Heo 671882607adcSTejun Heo for_each_pool(pool, pi) { 671982607adcSTejun Heo unsigned long pool_ts, touched, ts; 672082607adcSTejun Heo 6721cd2440d6SPetr Mladek pool->cpu_stall = false; 672282607adcSTejun Heo if (list_empty(&pool->worklist)) 672382607adcSTejun Heo continue; 672482607adcSTejun Heo 6725940d71c6SSergey Senozhatsky /* 6726940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6727940d71c6SSergey Senozhatsky * the watchdog like a stall. 6728940d71c6SSergey Senozhatsky */ 6729940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6730940d71c6SSergey Senozhatsky 673182607adcSTejun Heo /* get the latest of pool and touched timestamps */ 673289e28ce6SWang Qing if (pool->cpu >= 0) 673389e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 673489e28ce6SWang Qing else 673582607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 673689e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 673782607adcSTejun Heo 673882607adcSTejun Heo if (time_after(pool_ts, touched)) 673982607adcSTejun Heo ts = pool_ts; 674082607adcSTejun Heo else 674182607adcSTejun Heo ts = touched; 674282607adcSTejun Heo 674382607adcSTejun Heo /* did we stall? */ 6744940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 674582607adcSTejun Heo lockup_detected = true; 6746cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6747cd2440d6SPetr Mladek pool->cpu_stall = true; 6748cd2440d6SPetr Mladek cpu_pool_stall = true; 6749cd2440d6SPetr Mladek } 675082607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 675182607adcSTejun Heo pr_cont_pool_info(pool); 675282607adcSTejun Heo pr_cont(" stuck for %us!\n", 6753940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 675482607adcSTejun Heo } 6755cd2440d6SPetr Mladek 6756cd2440d6SPetr Mladek 675782607adcSTejun Heo } 675882607adcSTejun Heo 675982607adcSTejun Heo rcu_read_unlock(); 676082607adcSTejun Heo 676182607adcSTejun Heo if (lockup_detected) 676255df0933SImran Khan show_all_workqueues(); 676382607adcSTejun Heo 6764cd2440d6SPetr Mladek if (cpu_pool_stall) 6765cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6766cd2440d6SPetr Mladek 676782607adcSTejun Heo wq_watchdog_reset_touched(); 676882607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 676982607adcSTejun Heo } 677082607adcSTejun Heo 6771cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 677282607adcSTejun Heo { 677382607adcSTejun Heo if (cpu >= 0) 677482607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 677589e28ce6SWang Qing 677682607adcSTejun Heo wq_watchdog_touched = jiffies; 677782607adcSTejun Heo } 677882607adcSTejun Heo 677982607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 678082607adcSTejun Heo { 678182607adcSTejun Heo wq_watchdog_thresh = 0; 678282607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 678382607adcSTejun Heo 678482607adcSTejun Heo if (thresh) { 678582607adcSTejun Heo wq_watchdog_thresh = thresh; 678682607adcSTejun Heo wq_watchdog_reset_touched(); 678782607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 678882607adcSTejun Heo } 678982607adcSTejun Heo } 679082607adcSTejun Heo 679182607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 679282607adcSTejun Heo const struct kernel_param *kp) 679382607adcSTejun Heo { 679482607adcSTejun Heo unsigned long thresh; 679582607adcSTejun Heo int ret; 679682607adcSTejun Heo 679782607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 679882607adcSTejun Heo if (ret) 679982607adcSTejun Heo return ret; 680082607adcSTejun Heo 680182607adcSTejun Heo if (system_wq) 680282607adcSTejun Heo wq_watchdog_set_thresh(thresh); 680382607adcSTejun Heo else 680482607adcSTejun Heo wq_watchdog_thresh = thresh; 680582607adcSTejun Heo 680682607adcSTejun Heo return 0; 680782607adcSTejun Heo } 680882607adcSTejun Heo 680982607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 681082607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 681182607adcSTejun Heo .get = param_get_ulong, 681282607adcSTejun Heo }; 681382607adcSTejun Heo 681482607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 681582607adcSTejun Heo 0644); 681682607adcSTejun Heo 681782607adcSTejun Heo static void wq_watchdog_init(void) 681882607adcSTejun Heo { 68195cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 682082607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 682182607adcSTejun Heo } 682282607adcSTejun Heo 682382607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 682482607adcSTejun Heo 682582607adcSTejun Heo static inline void wq_watchdog_init(void) { } 682682607adcSTejun Heo 682782607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 682882607adcSTejun Heo 68294a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 68304a6c5607STejun Heo { 68314a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 68324a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 68334a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 68344a6c5607STejun Heo return; 68354a6c5607STejun Heo } 68364a6c5607STejun Heo 68374a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 68384a6c5607STejun Heo } 68394a6c5607STejun Heo 68403347fa09STejun Heo /** 68413347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 68423347fa09STejun Heo * 68432930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 68442930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 68452930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 68462930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 68472930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 68482930155bSTejun Heo * before early initcalls. 68493347fa09STejun Heo */ 68502333e829SYu Chen void __init workqueue_init_early(void) 68511da177e4SLinus Torvalds { 685284193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 68537a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 68547a4e344cSTejun Heo int i, cpu; 6855c34056a3STejun Heo 685610cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6857e904e6c2STejun Heo 6858b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 6859fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 6860fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 6861b05a7928SFrederic Weisbecker 68624a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 68634a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 68644a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 6865ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 68664a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 6867ace3c549Stiozhang 6868fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 68698b03ae3cSTejun Heo 68708b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6871e22bee78STejun Heo 68722930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 68732930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 68742930155bSTejun Heo 68757bd20b6bSMarcelo Tosatti /* 68767bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 68777bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 68787bd20b6bSMarcelo Tosatti */ 68797bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 68807bd20b6bSMarcelo Tosatti wq_power_efficient = true; 68817bd20b6bSMarcelo Tosatti 688284193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 688384193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 688484193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 688584193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 688684193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 688784193c07STejun Heo 688884193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 688984193c07STejun Heo 689084193c07STejun Heo pt->nr_pods = 1; 689184193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 689284193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 689384193c07STejun Heo pt->cpu_pod[0] = 0; 689484193c07STejun Heo 6895f02ae73aSTejun Heo /* initialize CPU pools */ 689624647570STejun Heo for_each_possible_cpu(cpu) { 6897ebf44d16STejun Heo struct worker_pool *pool; 6898e22bee78STejun Heo 68994ce62e9eSTejun Heo i = 0; 6900e22bee78STejun Heo for_each_cpu_worker_pool(pool, cpu) { 690129c91e99STejun Heo BUG_ON(init_worker_pool(pool)); 690229c91e99STejun Heo pool->cpu = cpu; 690329c91e99STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 69049546b29eSTejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 69051da177e4SLinus Torvalds pool->attrs->nice = std_nice[i++]; 69068639ecebSTejun Heo pool->attrs->affn_strict = true; 69071da177e4SLinus Torvalds pool->node = cpu_to_node(cpu); 69081da177e4SLinus Torvalds 69091da177e4SLinus Torvalds /* alloc pool ID */ 69101da177e4SLinus Torvalds mutex_lock(&wq_pool_mutex); 69111da177e4SLinus Torvalds BUG_ON(worker_pool_assign_id(pool)); 69121da177e4SLinus Torvalds mutex_unlock(&wq_pool_mutex); 691329c91e99STejun Heo } 691429c91e99STejun Heo } 691529c91e99STejun Heo 69168a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 691729c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 691829c91e99STejun Heo struct workqueue_attrs *attrs; 691929c91e99STejun Heo 6920be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 692129c91e99STejun Heo attrs->nice = std_nice[i]; 692229c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 69238a2b7538STejun Heo 69248a2b7538STejun Heo /* 69258a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 69268a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 69278a2b7538STejun Heo */ 6928be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 69298a2b7538STejun Heo attrs->nice = std_nice[i]; 6930af73f5c9STejun Heo attrs->ordered = true; 69318a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 693229c91e99STejun Heo } 693329c91e99STejun Heo 69341da177e4SLinus Torvalds system_wq = alloc_workqueue("events", 0, 0); 69351aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 69361da177e4SLinus Torvalds system_long_wq = alloc_workqueue("events_long", 0, 0); 69371da177e4SLinus Torvalds system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6938636b927eSTejun Heo WQ_MAX_ACTIVE); 69391da177e4SLinus Torvalds system_freezable_wq = alloc_workqueue("events_freezable", 69401da177e4SLinus Torvalds WQ_FREEZABLE, 0); 69410668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 69420668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 69438318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 69440668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 69450668106cSViresh Kumar 0); 69461aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 69470668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 69480668106cSViresh Kumar !system_power_efficient_wq || 69490668106cSViresh Kumar !system_freezable_power_efficient_wq); 69503347fa09STejun Heo } 69513347fa09STejun Heo 6952aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 6953aa6fde93STejun Heo { 6954aa6fde93STejun Heo unsigned long thresh; 6955aa6fde93STejun Heo unsigned long bogo; 6956aa6fde93STejun Heo 6957dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 6958dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 6959dd64c873SZqiang 6960aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 6961aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 6962aa6fde93STejun Heo return; 6963aa6fde93STejun Heo 6964aa6fde93STejun Heo /* 6965aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 6966aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 6967aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 6968aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 6969aa6fde93STejun Heo * too low. 6970aa6fde93STejun Heo * 6971aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 6972aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 6973aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 6974aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 6975aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 6976aa6fde93STejun Heo * usefulness. 6977aa6fde93STejun Heo */ 6978aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 6979aa6fde93STejun Heo 6980aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 6981aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 6982aa6fde93STejun Heo if (bogo < 4000) 6983aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 6984aa6fde93STejun Heo 6985aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 6986aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 6987aa6fde93STejun Heo 6988aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 6989aa6fde93STejun Heo } 6990aa6fde93STejun Heo 69913347fa09STejun Heo /** 69923347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 69933347fa09STejun Heo * 69942930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 69952930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 69962930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 69972930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 69982930155bSTejun Heo * workers and enable future kworker creations. 69993347fa09STejun Heo */ 70002333e829SYu Chen void __init workqueue_init(void) 70013347fa09STejun Heo { 70022186d9f9STejun Heo struct workqueue_struct *wq; 70033347fa09STejun Heo struct worker_pool *pool; 70043347fa09STejun Heo int cpu, bkt; 70053347fa09STejun Heo 7006aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7007aa6fde93STejun Heo 70082186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 70092186d9f9STejun Heo 70102930155bSTejun Heo /* 70112930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 70122930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 70132930155bSTejun Heo */ 70142186d9f9STejun Heo for_each_possible_cpu(cpu) { 70152186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 70162186d9f9STejun Heo pool->node = cpu_to_node(cpu); 70172186d9f9STejun Heo } 70182186d9f9STejun Heo } 70192186d9f9STejun Heo 702040c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 702140c17f75STejun Heo WARN(init_rescuer(wq), 702240c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 702340c17f75STejun Heo wq->name); 702440c17f75STejun Heo } 70252186d9f9STejun Heo 70262186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 70272186d9f9STejun Heo 70283347fa09STejun Heo /* create the initial workers */ 70293347fa09STejun Heo for_each_online_cpu(cpu) { 70303347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 70313347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 70323347fa09STejun Heo BUG_ON(!create_worker(pool)); 70333347fa09STejun Heo } 70343347fa09STejun Heo } 70353347fa09STejun Heo 70363347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 70373347fa09STejun Heo BUG_ON(!create_worker(pool)); 70383347fa09STejun Heo 70393347fa09STejun Heo wq_online = true; 704082607adcSTejun Heo wq_watchdog_init(); 70411da177e4SLinus Torvalds } 7042c4f135d6STetsuo Handa 7043025e1684STejun Heo /* 7044025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7045025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7046025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7047025e1684STejun Heo */ 7048025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7049025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7050025e1684STejun Heo { 7051025e1684STejun Heo int cur, pre, cpu, pod; 7052025e1684STejun Heo 7053025e1684STejun Heo pt->nr_pods = 0; 7054025e1684STejun Heo 7055025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7056025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7057025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7058025e1684STejun Heo 7059025e1684STejun Heo for_each_possible_cpu(cur) { 7060025e1684STejun Heo for_each_possible_cpu(pre) { 7061025e1684STejun Heo if (pre >= cur) { 7062025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7063025e1684STejun Heo break; 7064025e1684STejun Heo } 7065025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7066025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7067025e1684STejun Heo break; 7068025e1684STejun Heo } 7069025e1684STejun Heo } 7070025e1684STejun Heo } 7071025e1684STejun Heo 7072025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7073025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7074025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7075025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7076025e1684STejun Heo 7077025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7078025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7079025e1684STejun Heo 7080025e1684STejun Heo for_each_possible_cpu(cpu) { 7081025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7082025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7083025e1684STejun Heo } 7084025e1684STejun Heo } 7085025e1684STejun Heo 708663c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 708763c5484eSTejun Heo { 708863c5484eSTejun Heo return false; 708963c5484eSTejun Heo } 709063c5484eSTejun Heo 709163c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 709263c5484eSTejun Heo { 709363c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 709463c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 709563c5484eSTejun Heo #else 709663c5484eSTejun Heo return false; 709763c5484eSTejun Heo #endif 709863c5484eSTejun Heo } 709963c5484eSTejun Heo 7100025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7101025e1684STejun Heo { 7102025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7103025e1684STejun Heo } 7104025e1684STejun Heo 71052930155bSTejun Heo /** 71062930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 71072930155bSTejun Heo * 71082930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 71092930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 71102930155bSTejun Heo * initializes the unbound CPU pods accordingly. 71112930155bSTejun Heo */ 71122930155bSTejun Heo void __init workqueue_init_topology(void) 7113a86feae6STejun Heo { 71142930155bSTejun Heo struct workqueue_struct *wq; 7115025e1684STejun Heo int cpu; 7116a86feae6STejun Heo 711763c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 711863c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 711963c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7120025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7121a86feae6STejun Heo 71222930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7123a86feae6STejun Heo 7124a86feae6STejun Heo /* 71252930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 71262930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 71272930155bSTejun Heo * combinations to apply per-pod sharing. 71282930155bSTejun Heo */ 71292930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 71302930155bSTejun Heo for_each_online_cpu(cpu) { 71312930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 71322930155bSTejun Heo } 71332930155bSTejun Heo } 71342930155bSTejun Heo 71352930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7136a86feae6STejun Heo } 7137a86feae6STejun Heo 713820bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 713920bdedafSTetsuo Handa { 714020bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 714120bdedafSTetsuo Handa dump_stack(); 714220bdedafSTetsuo Handa } 7143c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7144ace3c549Stiozhang 7145ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7146ace3c549Stiozhang { 7147ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7148ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7149ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7150ace3c549Stiozhang } 7151ace3c549Stiozhang 7152ace3c549Stiozhang return 1; 7153ace3c549Stiozhang } 7154ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7155