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 /* 288c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 289c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2901da177e4SLinus Torvalds */ 2911da177e4SLinus Torvalds struct workqueue_struct { 2923c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 293e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 29473f53c4aSTejun Heo 2953c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2963c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2973c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 298112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2993c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3003c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3013c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 30273f53c4aSTejun Heo 3032e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 30430ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 305e22bee78STejun Heo 30687fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 307a045a272STejun Heo int max_active; /* WO: max active works */ 308a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 309226223abSTejun Heo 3105b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3115b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 3126029a918STejun Heo 313226223abSTejun Heo #ifdef CONFIG_SYSFS 314226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 315226223abSTejun Heo #endif 3164e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 317669de8bdSBart Van Assche char *lock_name; 318669de8bdSBart Van Assche struct lock_class_key key; 3194e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3204e6045f1SJohannes Berg #endif 321ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3222728fd2fSTejun Heo 323e2dca7adSTejun Heo /* 32424acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 32524acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 326e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 327e2dca7adSTejun Heo */ 328e2dca7adSTejun Heo struct rcu_head rcu; 329e2dca7adSTejun Heo 3302728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3312728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 332636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 3331da177e4SLinus Torvalds }; 3341da177e4SLinus Torvalds 335e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 336e904e6c2STejun Heo 33784193c07STejun Heo /* 33884193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 33984193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 34084193c07STejun Heo */ 34184193c07STejun Heo struct wq_pod_type { 34284193c07STejun Heo int nr_pods; /* number of pods */ 34384193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 34484193c07STejun Heo int *pod_node; /* pod -> node */ 34584193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 34684193c07STejun Heo }; 34784193c07STejun Heo 34884193c07STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 349523a301eSTejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 35063c5484eSTejun Heo 35163c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 352523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 35363c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 35463c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 35563c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 35663c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 35763c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 35863c5484eSTejun Heo }; 359bce90380STejun Heo 360616db877STejun Heo /* 361616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 362616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 363616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 364aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 365aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 366616db877STejun Heo */ 367aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 368616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 369616db877STejun Heo 370cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 371552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 372cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 373cee22a15SViresh Kumar 374863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3753347fa09STejun Heo 376fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 377fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 3784c16bd32STejun Heo 37968e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3801258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 381a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 382d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 383d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3845bcab335STejun Heo 385e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 38668e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3877d19c5ceSTejun Heo 38899c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 389ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 390ef557180SMike Galbraith 391fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 392fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 393fe28f631SWaiman Long 394fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 395fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 396fe28f631SWaiman Long 397ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 398ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 399ace3c549Stiozhang 400ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 401ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 402b05a7928SFrederic Weisbecker 403f303fccbSTejun Heo /* 404f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 405f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 406f303fccbSTejun Heo * to uncover usages which depend on it. 407f303fccbSTejun Heo */ 408f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 409f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 410f303fccbSTejun Heo #else 411f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 412f303fccbSTejun Heo #endif 413f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 414f303fccbSTejun Heo 4157d19c5ceSTejun Heo /* the per-cpu worker pools */ 41625528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 4177d19c5ceSTejun Heo 41868e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4197d19c5ceSTejun Heo 42068e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 42129c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 42229c91e99STejun Heo 423c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 42429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 42529c91e99STejun Heo 4268a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4278a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4288a2b7538STejun Heo 429967b494eSTejun Heo /* 430967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 431967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 432967b494eSTejun Heo * worker to avoid A-A deadlocks. 433967b494eSTejun Heo */ 43468279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 435967b494eSTejun Heo 43668279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 437ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 43868279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 4391aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 44068279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 441d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 44268279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 443f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 44468279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 44524d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 44668279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 4470668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 44868279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 4490668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 450d320c038STejun Heo 4517d19c5ceSTejun Heo static int worker_thread(void *__worker); 4526ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 453c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 45455df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4557d19c5ceSTejun Heo 45697bd2347STejun Heo #define CREATE_TRACE_POINTS 45797bd2347STejun Heo #include <trace/events/workqueue.h> 45897bd2347STejun Heo 45968e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 46024acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 461f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 46224acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4635bcab335STejun Heo 4645b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 46524acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 466f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 467f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 46824acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4695b95e1afSLai Jiangshan 470f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 471f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 472f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4737a62c2c8STejun Heo (pool)++) 4744ce62e9eSTejun Heo 47549e3cf44STejun Heo /** 47617116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 47717116969STejun Heo * @pool: iteration cursor 478611c92a0STejun Heo * @pi: integer used for iteration 479fa1b54e6STejun Heo * 48024acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 48168e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 48268e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 483fa1b54e6STejun Heo * 484fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 485fa1b54e6STejun Heo * ignored. 48617116969STejun Heo */ 487611c92a0STejun Heo #define for_each_pool(pool, pi) \ 488611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 48968e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 490fa1b54e6STejun Heo else 49117116969STejun Heo 49217116969STejun Heo /** 493822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 494822d8405STejun Heo * @worker: iteration cursor 495822d8405STejun Heo * @pool: worker_pool to iterate workers of 496822d8405STejun Heo * 4971258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 498822d8405STejun Heo * 499822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 500822d8405STejun Heo * ignored. 501822d8405STejun Heo */ 502da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 503da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5041258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 505822d8405STejun Heo else 506822d8405STejun Heo 507822d8405STejun Heo /** 50849e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 50949e3cf44STejun Heo * @pwq: iteration cursor 51049e3cf44STejun Heo * @wq: the target workqueue 51176af4d93STejun Heo * 51224acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 513794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 514794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 51576af4d93STejun Heo * 51676af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 51776af4d93STejun Heo * ignored. 51849e3cf44STejun Heo */ 51949e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 52049e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5215a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 522f3421797STejun Heo 523dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 524dc186ad7SThomas Gleixner 525f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 526dc186ad7SThomas Gleixner 52799777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 52899777288SStanislaw Gruszka { 52999777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 53099777288SStanislaw Gruszka } 53199777288SStanislaw Gruszka 532b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 533b9fdac7fSDu, Changbin { 534b9fdac7fSDu, Changbin struct work_struct *work = addr; 535b9fdac7fSDu, Changbin 536b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 537b9fdac7fSDu, Changbin } 538b9fdac7fSDu, Changbin 539dc186ad7SThomas Gleixner /* 540dc186ad7SThomas Gleixner * fixup_init is called when: 541dc186ad7SThomas Gleixner * - an active object is initialized 542dc186ad7SThomas Gleixner */ 54302a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 544dc186ad7SThomas Gleixner { 545dc186ad7SThomas Gleixner struct work_struct *work = addr; 546dc186ad7SThomas Gleixner 547dc186ad7SThomas Gleixner switch (state) { 548dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 549dc186ad7SThomas Gleixner cancel_work_sync(work); 550dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 55102a982a6SDu, Changbin return true; 552dc186ad7SThomas Gleixner default: 55302a982a6SDu, Changbin return false; 554dc186ad7SThomas Gleixner } 555dc186ad7SThomas Gleixner } 556dc186ad7SThomas Gleixner 557dc186ad7SThomas Gleixner /* 558dc186ad7SThomas Gleixner * fixup_free is called when: 559dc186ad7SThomas Gleixner * - an active object is freed 560dc186ad7SThomas Gleixner */ 56102a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 562dc186ad7SThomas Gleixner { 563dc186ad7SThomas Gleixner struct work_struct *work = addr; 564dc186ad7SThomas Gleixner 565dc186ad7SThomas Gleixner switch (state) { 566dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 567dc186ad7SThomas Gleixner cancel_work_sync(work); 568dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 56902a982a6SDu, Changbin return true; 570dc186ad7SThomas Gleixner default: 57102a982a6SDu, Changbin return false; 572dc186ad7SThomas Gleixner } 573dc186ad7SThomas Gleixner } 574dc186ad7SThomas Gleixner 575f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 576dc186ad7SThomas Gleixner .name = "work_struct", 57799777288SStanislaw Gruszka .debug_hint = work_debug_hint, 578b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 579dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 580dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 581dc186ad7SThomas Gleixner }; 582dc186ad7SThomas Gleixner 583dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 584dc186ad7SThomas Gleixner { 585dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 586dc186ad7SThomas Gleixner } 587dc186ad7SThomas Gleixner 588dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 589dc186ad7SThomas Gleixner { 590dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 591dc186ad7SThomas Gleixner } 592dc186ad7SThomas Gleixner 593dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 594dc186ad7SThomas Gleixner { 595dc186ad7SThomas Gleixner if (onstack) 596dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 597dc186ad7SThomas Gleixner else 598dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 599dc186ad7SThomas Gleixner } 600dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 601dc186ad7SThomas Gleixner 602dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 603dc186ad7SThomas Gleixner { 604dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 605dc186ad7SThomas Gleixner } 606dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 607dc186ad7SThomas Gleixner 608ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 609ea2e64f2SThomas Gleixner { 610ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 611ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 612ea2e64f2SThomas Gleixner } 613ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 614ea2e64f2SThomas Gleixner 615dc186ad7SThomas Gleixner #else 616dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 617dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 618dc186ad7SThomas Gleixner #endif 619dc186ad7SThomas Gleixner 6204e8b22bdSLi Bin /** 62167dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6224e8b22bdSLi Bin * @pool: the pool pointer of interest 6234e8b22bdSLi Bin * 6244e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6254e8b22bdSLi Bin * successfully, -errno on failure. 6264e8b22bdSLi Bin */ 6279daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6289daf9e67STejun Heo { 6299daf9e67STejun Heo int ret; 6309daf9e67STejun Heo 63168e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6325bcab335STejun Heo 6334e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6344e8b22bdSLi Bin GFP_KERNEL); 635229641a6STejun Heo if (ret >= 0) { 636e68035fbSTejun Heo pool->id = ret; 637229641a6STejun Heo return 0; 638229641a6STejun Heo } 6399daf9e67STejun Heo return ret; 6409daf9e67STejun Heo } 6419daf9e67STejun Heo 64273f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 64373f53c4aSTejun Heo { 64473f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 64573f53c4aSTejun Heo } 64673f53c4aSTejun Heo 647c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 64873f53c4aSTejun Heo { 649c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 65073f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 65173f53c4aSTejun Heo } 65273f53c4aSTejun Heo 65373f53c4aSTejun Heo static int work_next_color(int color) 65473f53c4aSTejun Heo { 65573f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 656a848e3b6SOleg Nesterov } 657a848e3b6SOleg Nesterov 6584594bf15SDavid Howells /* 659112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 660112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6617c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6627a22ad75STejun Heo * 663112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 664112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 665bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 666bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6677a22ad75STejun Heo * 668112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6697c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 670112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6717c3eed5cSTejun Heo * available only while the work item is queued. 672bbb68dfaSTejun Heo * 673bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 674bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 675bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 676bbb68dfaSTejun Heo * try to steal the PENDING bit. 6774594bf15SDavid Howells */ 6787a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6797a22ad75STejun Heo unsigned long flags) 6807a22ad75STejun Heo { 6816183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6827a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6837a22ad75STejun Heo } 6847a22ad75STejun Heo 685112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6864690c4abSTejun Heo unsigned long extra_flags) 687365970a1SDavid Howells { 688112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 689112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 690365970a1SDavid Howells } 691365970a1SDavid Howells 6924468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6934468a00fSLai Jiangshan int pool_id) 6944468a00fSLai Jiangshan { 6954468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6964468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6974468a00fSLai Jiangshan } 6984468a00fSLai Jiangshan 6997c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7007c3eed5cSTejun Heo int pool_id) 7014d707b9fSOleg Nesterov { 70223657bb1STejun Heo /* 70323657bb1STejun Heo * The following wmb is paired with the implied mb in 70423657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 70523657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 70623657bb1STejun Heo * owner. 70723657bb1STejun Heo */ 70823657bb1STejun Heo smp_wmb(); 7097c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 710346c09f8SRoman Pen /* 711346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 712346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 713346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7148bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 715346c09f8SRoman Pen * the same @work. E.g. consider this case: 716346c09f8SRoman Pen * 717346c09f8SRoman Pen * CPU#0 CPU#1 718346c09f8SRoman Pen * ---------------------------- -------------------------------- 719346c09f8SRoman Pen * 720346c09f8SRoman Pen * 1 STORE event_indicated 721346c09f8SRoman Pen * 2 queue_work_on() { 722346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 723346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 724346c09f8SRoman Pen * 5 set_work_data() # clear bit 725346c09f8SRoman Pen * 6 smp_mb() 726346c09f8SRoman Pen * 7 work->current_func() { 727346c09f8SRoman Pen * 8 LOAD event_indicated 728346c09f8SRoman Pen * } 729346c09f8SRoman Pen * 730346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 731346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 732346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 733346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 734346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 735346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 736346c09f8SRoman Pen * before actual STORE. 737346c09f8SRoman Pen */ 738346c09f8SRoman Pen smp_mb(); 7394d707b9fSOleg Nesterov } 7404d707b9fSOleg Nesterov 7417a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 742365970a1SDavid Howells { 7437c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7447c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7457a22ad75STejun Heo } 7467a22ad75STejun Heo 747afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 748afa4bb77SLinus Torvalds { 749afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 750afa4bb77SLinus Torvalds } 751afa4bb77SLinus Torvalds 752112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7537a22ad75STejun Heo { 754e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7557a22ad75STejun Heo 756112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 757afa4bb77SLinus Torvalds return work_struct_pwq(data); 758e120153dSTejun Heo else 759e120153dSTejun Heo return NULL; 7607a22ad75STejun Heo } 7617a22ad75STejun Heo 7627c3eed5cSTejun Heo /** 7637c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7647c3eed5cSTejun Heo * @work: the work item of interest 7657c3eed5cSTejun Heo * 76668e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 76724acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 76824acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 769fa1b54e6STejun Heo * 770fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 771fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 772fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 773fa1b54e6STejun Heo * returned pool is and stays online. 774d185af30SYacine Belkadi * 775d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7767c3eed5cSTejun Heo */ 7777c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7787a22ad75STejun Heo { 779e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7807c3eed5cSTejun Heo int pool_id; 7817a22ad75STejun Heo 78268e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 783fa1b54e6STejun Heo 784112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 785afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 7867a22ad75STejun Heo 7877c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7887c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7897a22ad75STejun Heo return NULL; 7907a22ad75STejun Heo 791fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7927c3eed5cSTejun Heo } 7937c3eed5cSTejun Heo 7947c3eed5cSTejun Heo /** 7957c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7967c3eed5cSTejun Heo * @work: the work item of interest 7977c3eed5cSTejun Heo * 798d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7997c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8007c3eed5cSTejun Heo */ 8017c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8027c3eed5cSTejun Heo { 80354d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 8047c3eed5cSTejun Heo 805112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 806afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 80754d5b7d0SLai Jiangshan 80854d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 8097c3eed5cSTejun Heo } 8107c3eed5cSTejun Heo 811bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 812bbb68dfaSTejun Heo { 8137c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 814bbb68dfaSTejun Heo 8157c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8167c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 817bbb68dfaSTejun Heo } 818bbb68dfaSTejun Heo 819bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 820bbb68dfaSTejun Heo { 821bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 822bbb68dfaSTejun Heo 823112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 824bbb68dfaSTejun Heo } 825bbb68dfaSTejun Heo 826e22bee78STejun Heo /* 8273270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8283270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 829d565ed63STejun Heo * they're being called with pool->lock held. 830e22bee78STejun Heo */ 831e22bee78STejun Heo 832e22bee78STejun Heo /* 833e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 834e22bee78STejun Heo * running workers. 835974271c4STejun Heo * 836974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 837706026c2STejun Heo * function will always return %true for unbound pools as long as the 838974271c4STejun Heo * worklist isn't empty. 839e22bee78STejun Heo */ 84063d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 841e22bee78STejun Heo { 8420219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 843e22bee78STejun Heo } 844e22bee78STejun Heo 845e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 84663d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 847e22bee78STejun Heo { 84863d95a91STejun Heo return pool->nr_idle; 849e22bee78STejun Heo } 850e22bee78STejun Heo 851e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 85263d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 853e22bee78STejun Heo { 854bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 855e22bee78STejun Heo } 856e22bee78STejun Heo 857e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 85863d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 859e22bee78STejun Heo { 86063d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 861e22bee78STejun Heo } 862e22bee78STejun Heo 863e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 86463d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 865e22bee78STejun Heo { 866692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 86763d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 86863d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 869e22bee78STejun Heo 870e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 871e22bee78STejun Heo } 872e22bee78STejun Heo 8734690c4abSTejun Heo /** 874e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 875cb444766STejun Heo * @worker: self 876d302f017STejun Heo * @flags: flags to set 877d302f017STejun Heo * 878228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 879d302f017STejun Heo */ 880228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 881d302f017STejun Heo { 882bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 883e22bee78STejun Heo 884bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 885cb444766STejun Heo 886228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 887e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 888e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 889bc35f7efSLai Jiangshan pool->nr_running--; 890e22bee78STejun Heo } 891e22bee78STejun Heo 892d302f017STejun Heo worker->flags |= flags; 893d302f017STejun Heo } 894d302f017STejun Heo 895d302f017STejun Heo /** 896e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 897cb444766STejun Heo * @worker: self 898d302f017STejun Heo * @flags: flags to clear 899d302f017STejun Heo * 900e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 901d302f017STejun Heo */ 902d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 903d302f017STejun Heo { 90463d95a91STejun Heo struct worker_pool *pool = worker->pool; 905e22bee78STejun Heo unsigned int oflags = worker->flags; 906e22bee78STejun Heo 907bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 908cb444766STejun Heo 909d302f017STejun Heo worker->flags &= ~flags; 910e22bee78STejun Heo 91142c025f3STejun Heo /* 91242c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 91342c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 91442c025f3STejun Heo * of multiple flags, not a single flag. 91542c025f3STejun Heo */ 916e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 917e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 918bc35f7efSLai Jiangshan pool->nr_running++; 919d302f017STejun Heo } 920d302f017STejun Heo 921797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 922797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 923797e8345STejun Heo { 924797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 925797e8345STejun Heo return NULL; 926797e8345STejun Heo 927797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 928797e8345STejun Heo } 929797e8345STejun Heo 930797e8345STejun Heo /** 931797e8345STejun Heo * worker_enter_idle - enter idle state 932797e8345STejun Heo * @worker: worker which is entering idle state 933797e8345STejun Heo * 934797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 935797e8345STejun Heo * necessary. 936797e8345STejun Heo * 937797e8345STejun Heo * LOCKING: 938797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 939797e8345STejun Heo */ 940797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 941797e8345STejun Heo { 942797e8345STejun Heo struct worker_pool *pool = worker->pool; 943797e8345STejun Heo 944797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 945797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 946797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 947797e8345STejun Heo return; 948797e8345STejun Heo 949797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 950797e8345STejun Heo worker->flags |= WORKER_IDLE; 951797e8345STejun Heo pool->nr_idle++; 952797e8345STejun Heo worker->last_active = jiffies; 953797e8345STejun Heo 954797e8345STejun Heo /* idle_list is LIFO */ 955797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 956797e8345STejun Heo 957797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 958797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 959797e8345STejun Heo 960797e8345STejun Heo /* Sanity check nr_running. */ 961797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 962797e8345STejun Heo } 963797e8345STejun Heo 964797e8345STejun Heo /** 965797e8345STejun Heo * worker_leave_idle - leave idle state 966797e8345STejun Heo * @worker: worker which is leaving idle state 967797e8345STejun Heo * 968797e8345STejun Heo * @worker is leaving idle state. Update stats. 969797e8345STejun Heo * 970797e8345STejun Heo * LOCKING: 971797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 972797e8345STejun Heo */ 973797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 974797e8345STejun Heo { 975797e8345STejun Heo struct worker_pool *pool = worker->pool; 976797e8345STejun Heo 977797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 978797e8345STejun Heo return; 979797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 980797e8345STejun Heo pool->nr_idle--; 981797e8345STejun Heo list_del_init(&worker->entry); 982797e8345STejun Heo } 983797e8345STejun Heo 984797e8345STejun Heo /** 985797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 986797e8345STejun Heo * @pool: pool of interest 987797e8345STejun Heo * @work: work to find worker for 988797e8345STejun Heo * 989797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 990797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 991797e8345STejun Heo * to match, its current execution should match the address of @work and 992797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 993797e8345STejun Heo * unrelated work executions through a work item being recycled while still 994797e8345STejun Heo * being executed. 995797e8345STejun Heo * 996797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 997797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 998797e8345STejun Heo * another work item. If the same work item address ends up being reused 999797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1000797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1001797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1002797e8345STejun Heo * 1003797e8345STejun Heo * This function checks the work item address and work function to avoid 1004797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1005797e8345STejun Heo * work function which can introduce dependency onto itself through a 1006797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1007797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1008797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1009797e8345STejun Heo * 1010797e8345STejun Heo * CONTEXT: 1011797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1012797e8345STejun Heo * 1013797e8345STejun Heo * Return: 1014797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1015797e8345STejun Heo * otherwise. 1016797e8345STejun Heo */ 1017797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1018797e8345STejun Heo struct work_struct *work) 1019797e8345STejun Heo { 1020797e8345STejun Heo struct worker *worker; 1021797e8345STejun Heo 1022797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1023797e8345STejun Heo (unsigned long)work) 1024797e8345STejun Heo if (worker->current_work == work && 1025797e8345STejun Heo worker->current_func == work->func) 1026797e8345STejun Heo return worker; 1027797e8345STejun Heo 1028797e8345STejun Heo return NULL; 1029797e8345STejun Heo } 1030797e8345STejun Heo 1031797e8345STejun Heo /** 1032797e8345STejun Heo * move_linked_works - move linked works to a list 1033797e8345STejun Heo * @work: start of series of works to be scheduled 1034797e8345STejun Heo * @head: target list to append @work to 1035797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1036797e8345STejun Heo * 1037873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1038873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1039873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1040873eaca6STejun Heo * @nextp. 1041797e8345STejun Heo * 1042797e8345STejun Heo * CONTEXT: 1043797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1044797e8345STejun Heo */ 1045797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1046797e8345STejun Heo struct work_struct **nextp) 1047797e8345STejun Heo { 1048797e8345STejun Heo struct work_struct *n; 1049797e8345STejun Heo 1050797e8345STejun Heo /* 1051797e8345STejun Heo * Linked worklist will always end before the end of the list, 1052797e8345STejun Heo * use NULL for list head. 1053797e8345STejun Heo */ 1054797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1055797e8345STejun Heo list_move_tail(&work->entry, head); 1056797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1057797e8345STejun Heo break; 1058797e8345STejun Heo } 1059797e8345STejun Heo 1060797e8345STejun Heo /* 1061797e8345STejun Heo * If we're already inside safe list traversal and have moved 1062797e8345STejun Heo * multiple works to the scheduled queue, the next position 1063797e8345STejun Heo * needs to be updated. 1064797e8345STejun Heo */ 1065797e8345STejun Heo if (nextp) 1066797e8345STejun Heo *nextp = n; 1067797e8345STejun Heo } 1068797e8345STejun Heo 1069797e8345STejun Heo /** 1070873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1071873eaca6STejun Heo * @work: work to assign 1072873eaca6STejun Heo * @worker: worker to assign to 1073873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1074873eaca6STejun Heo * 1075873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1076873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1077873eaca6STejun Heo * 1078873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1079873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1080873eaca6STejun Heo * list_for_each_entry_safe(). 1081873eaca6STejun Heo * 1082873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1083873eaca6STejun Heo * was punted to another worker already executing it. 1084873eaca6STejun Heo */ 1085873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1086873eaca6STejun Heo struct work_struct **nextp) 1087873eaca6STejun Heo { 1088873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1089873eaca6STejun Heo struct worker *collision; 1090873eaca6STejun Heo 1091873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1092873eaca6STejun Heo 1093873eaca6STejun Heo /* 1094873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1095873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1096873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1097873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1098873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1099873eaca6STejun Heo * defer the work to the currently executing one. 1100873eaca6STejun Heo */ 1101873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1102873eaca6STejun Heo if (unlikely(collision)) { 1103873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1104873eaca6STejun Heo return false; 1105873eaca6STejun Heo } 1106873eaca6STejun Heo 1107873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1108873eaca6STejun Heo return true; 1109873eaca6STejun Heo } 1110873eaca6STejun Heo 1111873eaca6STejun Heo /** 11120219a352STejun Heo * kick_pool - wake up an idle worker if necessary 11130219a352STejun Heo * @pool: pool to kick 1114797e8345STejun Heo * 11150219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 11160219a352STejun Heo * whether a worker was woken up. 1117797e8345STejun Heo */ 11180219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1119797e8345STejun Heo { 1120797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 11218639ecebSTejun Heo struct task_struct *p; 1122797e8345STejun Heo 11230219a352STejun Heo lockdep_assert_held(&pool->lock); 11240219a352STejun Heo 11250219a352STejun Heo if (!need_more_worker(pool) || !worker) 11260219a352STejun Heo return false; 11270219a352STejun Heo 11288639ecebSTejun Heo p = worker->task; 11298639ecebSTejun Heo 11308639ecebSTejun Heo #ifdef CONFIG_SMP 11318639ecebSTejun Heo /* 11328639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 11338639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 11348639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 11358639ecebSTejun Heo * execution locality. 11368639ecebSTejun Heo * 11378639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 11388639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 11398639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 11408639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 11418639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 11428639ecebSTejun Heo * still on cpu when picking an idle worker. 11438639ecebSTejun Heo * 11448639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 11458639ecebSTejun Heo * its affinity scope. Repatriate. 11468639ecebSTejun Heo */ 11478639ecebSTejun Heo if (!pool->attrs->affn_strict && 11488639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 11498639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 11508639ecebSTejun Heo struct work_struct, entry); 11518639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 11528639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 11538639ecebSTejun Heo } 11548639ecebSTejun Heo #endif 11558639ecebSTejun Heo wake_up_process(p); 11560219a352STejun Heo return true; 1157797e8345STejun Heo } 1158797e8345STejun Heo 115963638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 116063638450STejun Heo 116163638450STejun Heo /* 116263638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 116363638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 116463638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 116563638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 116663638450STejun Heo * should be using an unbound workqueue instead. 116763638450STejun Heo * 116863638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 116963638450STejun Heo * and report them so that they can be examined and converted to use unbound 117063638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 117163638450STejun Heo * function is tracked and reported with exponential backoff. 117263638450STejun Heo */ 117363638450STejun Heo #define WCI_MAX_ENTS 128 117463638450STejun Heo 117563638450STejun Heo struct wci_ent { 117663638450STejun Heo work_func_t func; 117763638450STejun Heo atomic64_t cnt; 117863638450STejun Heo struct hlist_node hash_node; 117963638450STejun Heo }; 118063638450STejun Heo 118163638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 118263638450STejun Heo static int wci_nr_ents; 118363638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 118463638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 118563638450STejun Heo 118663638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 118763638450STejun Heo { 118863638450STejun Heo struct wci_ent *ent; 118963638450STejun Heo 119063638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 119163638450STejun Heo (unsigned long)func) { 119263638450STejun Heo if (ent->func == func) 119363638450STejun Heo return ent; 119463638450STejun Heo } 119563638450STejun Heo return NULL; 119663638450STejun Heo } 119763638450STejun Heo 119863638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 119963638450STejun Heo { 120063638450STejun Heo struct wci_ent *ent; 120163638450STejun Heo 120263638450STejun Heo restart: 120363638450STejun Heo ent = wci_find_ent(func); 120463638450STejun Heo if (ent) { 120563638450STejun Heo u64 cnt; 120663638450STejun Heo 120763638450STejun Heo /* 120863638450STejun Heo * Start reporting from the fourth time and back off 120963638450STejun Heo * exponentially. 121063638450STejun Heo */ 121163638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 121263638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 121363638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 121463638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 121563638450STejun Heo atomic64_read(&ent->cnt)); 121663638450STejun Heo return; 121763638450STejun Heo } 121863638450STejun Heo 121963638450STejun Heo /* 122063638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 122163638450STejun Heo * is exhausted, something went really wrong and we probably made enough 122263638450STejun Heo * noise already. 122363638450STejun Heo */ 122463638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 122563638450STejun Heo return; 122663638450STejun Heo 122763638450STejun Heo raw_spin_lock(&wci_lock); 122863638450STejun Heo 122963638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 123063638450STejun Heo raw_spin_unlock(&wci_lock); 123163638450STejun Heo return; 123263638450STejun Heo } 123363638450STejun Heo 123463638450STejun Heo if (wci_find_ent(func)) { 123563638450STejun Heo raw_spin_unlock(&wci_lock); 123663638450STejun Heo goto restart; 123763638450STejun Heo } 123863638450STejun Heo 123963638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 124063638450STejun Heo ent->func = func; 124163638450STejun Heo atomic64_set(&ent->cnt, 1); 124263638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 124363638450STejun Heo 124463638450STejun Heo raw_spin_unlock(&wci_lock); 124563638450STejun Heo } 124663638450STejun Heo 124763638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 124863638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 124963638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 125063638450STejun Heo 1251c54d5046STejun Heo /** 12521da177e4SLinus Torvalds * wq_worker_running - a worker is running again 12531da177e4SLinus Torvalds * @task: task waking up 12541da177e4SLinus Torvalds * 12551da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 12561da177e4SLinus Torvalds */ 12571da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 12581da177e4SLinus Torvalds { 12591da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12601da177e4SLinus Torvalds 1261c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 12621da177e4SLinus Torvalds return; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds /* 12651da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 12661da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 12671da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 12681da177e4SLinus Torvalds * pool. Protect against such race. 12691da177e4SLinus Torvalds */ 12701da177e4SLinus Torvalds preempt_disable(); 12711da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 12721da177e4SLinus Torvalds worker->pool->nr_running++; 12731da177e4SLinus Torvalds preempt_enable(); 1274616db877STejun Heo 1275616db877STejun Heo /* 1276616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1277616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1278616db877STejun Heo */ 1279616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1280616db877STejun Heo 1281c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds /** 12851da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 12861da177e4SLinus Torvalds * @task: task going to sleep 12871da177e4SLinus Torvalds * 12881da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 12891da177e4SLinus Torvalds * going to sleep. 12901da177e4SLinus Torvalds */ 12911da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 12921da177e4SLinus Torvalds { 12931da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12941da177e4SLinus Torvalds struct worker_pool *pool; 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds /* 12971da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 12981da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 12991da177e4SLinus Torvalds * checking NOT_RUNNING. 13001da177e4SLinus Torvalds */ 13011da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 13021da177e4SLinus Torvalds return; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds pool = worker->pool; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1307c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 13081da177e4SLinus Torvalds return; 13091da177e4SLinus Torvalds 1310c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 13111da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 13151da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 13161da177e4SLinus Torvalds * and nr_running has been reset. 13171da177e4SLinus Torvalds */ 13181da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 13191da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13201da177e4SLinus Torvalds return; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds pool->nr_running--; 13240219a352STejun Heo if (kick_pool(pool)) 1325725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 13260219a352STejun Heo 13271da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13281da177e4SLinus Torvalds } 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds /** 1331616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1332616db877STejun Heo * @task: task currently running 1333616db877STejun Heo * 1334616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1335616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1336616db877STejun Heo */ 1337616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1338616db877STejun Heo { 1339616db877STejun Heo struct worker *worker = kthread_data(task); 1340616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1341616db877STejun Heo struct worker_pool *pool = worker->pool; 1342616db877STejun Heo 1343616db877STejun Heo if (!pwq) 1344616db877STejun Heo return; 1345616db877STejun Heo 13468a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 13478a1dd1e5STejun Heo 134818c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 134918c8ae81SZqiang return; 135018c8ae81SZqiang 1351616db877STejun Heo /* 1352616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1353616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1354616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1355c8f6219bSZqiang * 1356c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1357c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1358c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1359c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1360c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1361c8f6219bSZqiang * We probably want to make this prettier in the future. 1362616db877STejun Heo */ 1363c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1364616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1365616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1366616db877STejun Heo return; 1367616db877STejun Heo 1368616db877STejun Heo raw_spin_lock(&pool->lock); 1369616db877STejun Heo 1370616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 137163638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1372616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1373616db877STejun Heo 13740219a352STejun Heo if (kick_pool(pool)) 1375616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1376616db877STejun Heo 1377616db877STejun Heo raw_spin_unlock(&pool->lock); 1378616db877STejun Heo } 1379616db877STejun Heo 1380616db877STejun Heo /** 13811da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 13821da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 13831da177e4SLinus Torvalds * 13841da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 13851da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 13861da177e4SLinus Torvalds * 13871da177e4SLinus Torvalds * CONTEXT: 13889b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 13891da177e4SLinus Torvalds * 13901da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1391f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1392f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 13931da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 13941da177e4SLinus Torvalds * 13951da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 13961da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 13971da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 13981da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1399365970a1SDavid Howells * 1400365970a1SDavid Howells * Return: 1401365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1402365970a1SDavid Howells * hasn't executed any work yet. 1403365970a1SDavid Howells */ 1404365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1405365970a1SDavid Howells { 1406365970a1SDavid Howells struct worker *worker = kthread_data(task); 1407365970a1SDavid Howells 1408365970a1SDavid Howells return worker->last_func; 1409365970a1SDavid Howells } 1410365970a1SDavid Howells 1411d302f017STejun Heo /** 14128864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 14138864b4e5STejun Heo * @pwq: pool_workqueue to get 14148864b4e5STejun Heo * 14158864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 14168864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 14178864b4e5STejun Heo */ 14188864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 14198864b4e5STejun Heo { 14208864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 14218864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 14228864b4e5STejun Heo pwq->refcnt++; 14238864b4e5STejun Heo } 14248864b4e5STejun Heo 14258864b4e5STejun Heo /** 14268864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 14278864b4e5STejun Heo * @pwq: pool_workqueue to put 14288864b4e5STejun Heo * 14298864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 14308864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 14318864b4e5STejun Heo */ 14328864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 14338864b4e5STejun Heo { 14348864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 14358864b4e5STejun Heo if (likely(--pwq->refcnt)) 14368864b4e5STejun Heo return; 14378864b4e5STejun Heo /* 1438967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1439967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 14408864b4e5STejun Heo */ 1441687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 14428864b4e5STejun Heo } 14438864b4e5STejun Heo 1444dce90d47STejun Heo /** 1445dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1446dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1447dce90d47STejun Heo * 1448dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1449dce90d47STejun Heo */ 1450dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1451dce90d47STejun Heo { 1452dce90d47STejun Heo if (pwq) { 1453dce90d47STejun Heo /* 145424acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1455dce90d47STejun Heo * following lock operations are safe. 1456dce90d47STejun Heo */ 1457a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1458dce90d47STejun Heo put_pwq(pwq); 1459a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1460dce90d47STejun Heo } 1461dce90d47STejun Heo } 1462dce90d47STejun Heo 1463*afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1464*afa87ce8STejun Heo { 1465*afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1466*afa87ce8STejun Heo } 1467*afa87ce8STejun Heo 1468f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1469bf4ede01STejun Heo { 1470112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1471bf4ede01STejun Heo 1472bf4ede01STejun Heo trace_workqueue_activate_work(work); 147382607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 147482607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1475112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1476f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1477112202d9STejun Heo pwq->nr_active++; 1478bf4ede01STejun Heo } 1479bf4ede01STejun Heo 1480f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 14813aa62497SLai Jiangshan { 1482f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 14833aa62497SLai Jiangshan struct work_struct, entry); 14843aa62497SLai Jiangshan 1485f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 14863aa62497SLai Jiangshan } 14873aa62497SLai Jiangshan 1488bf4ede01STejun Heo /** 1489112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1490112202d9STejun Heo * @pwq: pwq of interest 1491c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1492bf4ede01STejun Heo * 1493bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1494112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1495bf4ede01STejun Heo * 1496bf4ede01STejun Heo * CONTEXT: 1497a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1498bf4ede01STejun Heo */ 1499c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1500bf4ede01STejun Heo { 1501c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1502c4560c2cSLai Jiangshan 1503018f3a13SLai Jiangshan if (!(work_data & WORK_STRUCT_INACTIVE)) { 1504112202d9STejun Heo pwq->nr_active--; 1505f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1506f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1507a045a272STejun Heo if (pwq->nr_active < READ_ONCE(pwq->wq->max_active)) 1508f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1509bf4ede01STejun Heo } 1510018f3a13SLai Jiangshan } 1511018f3a13SLai Jiangshan 1512018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1513bf4ede01STejun Heo 1514bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1515112202d9STejun Heo if (likely(pwq->flush_color != color)) 15168864b4e5STejun Heo goto out_put; 1517bf4ede01STejun Heo 1518bf4ede01STejun Heo /* are there still in-flight works? */ 1519112202d9STejun Heo if (pwq->nr_in_flight[color]) 15208864b4e5STejun Heo goto out_put; 1521bf4ede01STejun Heo 1522112202d9STejun Heo /* this pwq is done, clear flush_color */ 1523112202d9STejun Heo pwq->flush_color = -1; 1524bf4ede01STejun Heo 1525bf4ede01STejun Heo /* 1526112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1527bf4ede01STejun Heo * will handle the rest. 1528bf4ede01STejun Heo */ 1529112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1530112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 15318864b4e5STejun Heo out_put: 15328864b4e5STejun Heo put_pwq(pwq); 1533bf4ede01STejun Heo } 1534bf4ede01STejun Heo 153536e227d2STejun Heo /** 1536bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 153736e227d2STejun Heo * @work: work item to steal 153836e227d2STejun Heo * @is_dwork: @work is a delayed_work 1539bbb68dfaSTejun Heo * @flags: place to store irq state 154036e227d2STejun Heo * 154136e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1542d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 154336e227d2STejun Heo * 1544d185af30SYacine Belkadi * Return: 15453eb6b31bSMauro Carvalho Chehab * 15463eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 154736e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 154836e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 154936e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1550bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1551bbb68dfaSTejun Heo * for arbitrarily long 15523eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 155336e227d2STejun Heo * 1554d185af30SYacine Belkadi * Note: 1555bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1556e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1557e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1558e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1559bbb68dfaSTejun Heo * 1560bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1561bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1562bbb68dfaSTejun Heo * 1563e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1564bf4ede01STejun Heo */ 1565bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1566bbb68dfaSTejun Heo unsigned long *flags) 1567bf4ede01STejun Heo { 1568d565ed63STejun Heo struct worker_pool *pool; 1569112202d9STejun Heo struct pool_workqueue *pwq; 1570bf4ede01STejun Heo 1571bbb68dfaSTejun Heo local_irq_save(*flags); 1572bbb68dfaSTejun Heo 157336e227d2STejun Heo /* try to steal the timer if it exists */ 157436e227d2STejun Heo if (is_dwork) { 157536e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 157636e227d2STejun Heo 1577e0aecdd8STejun Heo /* 1578e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1579e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1580e0aecdd8STejun Heo * running on the local CPU. 1581e0aecdd8STejun Heo */ 158236e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 158336e227d2STejun Heo return 1; 158436e227d2STejun Heo } 158536e227d2STejun Heo 158636e227d2STejun Heo /* try to claim PENDING the normal way */ 1587bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1588bf4ede01STejun Heo return 0; 1589bf4ede01STejun Heo 159024acfb71SThomas Gleixner rcu_read_lock(); 1591bf4ede01STejun Heo /* 1592bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1593bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1594bf4ede01STejun Heo */ 1595d565ed63STejun Heo pool = get_work_pool(work); 1596d565ed63STejun Heo if (!pool) 1597bbb68dfaSTejun Heo goto fail; 1598bf4ede01STejun Heo 1599a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1600bf4ede01STejun Heo /* 1601112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1602112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1603112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1604112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1605112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 16060b3dae68SLai Jiangshan * item is currently queued on that pool. 1607bf4ede01STejun Heo */ 1608112202d9STejun Heo pwq = get_work_pwq(work); 1609112202d9STejun Heo if (pwq && pwq->pool == pool) { 1610bf4ede01STejun Heo debug_work_deactivate(work); 16113aa62497SLai Jiangshan 16123aa62497SLai Jiangshan /* 1613018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1614018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1615018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1616018f3a13SLai Jiangshan * 1617f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1618d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1619f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 162016062836STejun Heo * management later on and cause stall. Make sure the work 162116062836STejun Heo * item is activated before grabbing. 16223aa62497SLai Jiangshan */ 1623f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1624f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 16253aa62497SLai Jiangshan 1626bf4ede01STejun Heo list_del_init(&work->entry); 1627c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 162836e227d2STejun Heo 1629112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 16304468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 16314468a00fSLai Jiangshan 1632a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 163324acfb71SThomas Gleixner rcu_read_unlock(); 163436e227d2STejun Heo return 1; 1635bf4ede01STejun Heo } 1636a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1637bbb68dfaSTejun Heo fail: 163824acfb71SThomas Gleixner rcu_read_unlock(); 1639bbb68dfaSTejun Heo local_irq_restore(*flags); 1640bbb68dfaSTejun Heo if (work_is_canceling(work)) 1641bbb68dfaSTejun Heo return -ENOENT; 1642bbb68dfaSTejun Heo cpu_relax(); 164336e227d2STejun Heo return -EAGAIN; 1644bf4ede01STejun Heo } 1645bf4ede01STejun Heo 1646bf4ede01STejun Heo /** 1647706026c2STejun Heo * insert_work - insert a work into a pool 1648112202d9STejun Heo * @pwq: pwq @work belongs to 16494690c4abSTejun Heo * @work: work to insert 16504690c4abSTejun Heo * @head: insertion point 16514690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 16524690c4abSTejun Heo * 1653112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1654706026c2STejun Heo * work_struct flags. 16554690c4abSTejun Heo * 16564690c4abSTejun Heo * CONTEXT: 1657a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1658365970a1SDavid Howells */ 1659112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1660112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1661b89deed3SOleg Nesterov { 1662fe089f87STejun Heo debug_work_activate(work); 1663e1d8aa9fSFrederic Weisbecker 1664e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1665f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1666e89a85d6SWalter Wu 16674690c4abSTejun Heo /* we own @work, set data and link */ 1668112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 16691a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 16708864b4e5STejun Heo get_pwq(pwq); 1671b89deed3SOleg Nesterov } 1672b89deed3SOleg Nesterov 1673c8efcc25STejun Heo /* 1674c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 16758d03ecfeSTejun Heo * same workqueue. 1676c8efcc25STejun Heo */ 1677c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1678c8efcc25STejun Heo { 1679c8efcc25STejun Heo struct worker *worker; 1680c8efcc25STejun Heo 16818d03ecfeSTejun Heo worker = current_wq_worker(); 1682c8efcc25STejun Heo /* 1683bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 16848d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1685c8efcc25STejun Heo */ 1686112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1687c8efcc25STejun Heo } 1688c8efcc25STejun Heo 1689ef557180SMike Galbraith /* 1690ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1691ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1692ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1693ef557180SMike Galbraith */ 1694ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1695ef557180SMike Galbraith { 1696ef557180SMike Galbraith int new_cpu; 1697ef557180SMike Galbraith 1698f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1699ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1700ef557180SMike Galbraith return cpu; 1701a8ec5880SAmmar Faizi } else { 1702a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1703f303fccbSTejun Heo } 1704f303fccbSTejun Heo 1705ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1706ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1707ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1708ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1709ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1710ef557180SMike Galbraith return cpu; 1711ef557180SMike Galbraith } 1712ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1713ef557180SMike Galbraith 1714ef557180SMike Galbraith return new_cpu; 1715ef557180SMike Galbraith } 1716ef557180SMike Galbraith 1717d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 17181da177e4SLinus Torvalds struct work_struct *work) 17191da177e4SLinus Torvalds { 1720112202d9STejun Heo struct pool_workqueue *pwq; 1721fe089f87STejun Heo struct worker_pool *last_pool, *pool; 17228a2e8e5dSTejun Heo unsigned int work_flags; 1723b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 17248930cabaSTejun Heo 17258930cabaSTejun Heo /* 17268930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 17278930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 17288930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 17298930cabaSTejun Heo * happen with IRQ disabled. 17308930cabaSTejun Heo */ 17318e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 17321da177e4SLinus Torvalds 17331e19ffc6STejun Heo 173433e3f0a3SRichard Clark /* 173533e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 173633e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 173733e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 173833e3f0a3SRichard Clark */ 173933e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 174033e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1741e41e704bSTejun Heo return; 174224acfb71SThomas Gleixner rcu_read_lock(); 17439e8cd2f5STejun Heo retry: 1744aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1745636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 1746636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 1747ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1748636b927eSTejun Heo else 1749aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1750aa202f1fSHillf Danton } 1751f3421797STejun Heo 1752636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 1753fe089f87STejun Heo pool = pwq->pool; 1754fe089f87STejun Heo 175518aa9effSTejun Heo /* 1756c9178087STejun Heo * If @work was previously on a different pool, it might still be 1757c9178087STejun Heo * running there, in which case the work needs to be queued on that 1758c9178087STejun Heo * pool to guarantee non-reentrancy. 175918aa9effSTejun Heo */ 1760c9e7cf27STejun Heo last_pool = get_work_pool(work); 1761fe089f87STejun Heo if (last_pool && last_pool != pool) { 176218aa9effSTejun Heo struct worker *worker; 176318aa9effSTejun Heo 1764a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 176518aa9effSTejun Heo 1766c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 176718aa9effSTejun Heo 1768112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1769c9178087STejun Heo pwq = worker->current_pwq; 1770fe089f87STejun Heo pool = pwq->pool; 1771fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 17728594fadeSLai Jiangshan } else { 177318aa9effSTejun Heo /* meh... not running there, queue here */ 1774a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1775fe089f87STejun Heo raw_spin_lock(&pool->lock); 177618aa9effSTejun Heo } 17778930cabaSTejun Heo } else { 1778fe089f87STejun Heo raw_spin_lock(&pool->lock); 17798930cabaSTejun Heo } 1780502ca9d8STejun Heo 17819e8cd2f5STejun Heo /* 1782636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 1783636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 1784636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 1785636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 1786636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 17879e8cd2f5STejun Heo */ 17889e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 17899e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1790fe089f87STejun Heo raw_spin_unlock(&pool->lock); 17919e8cd2f5STejun Heo cpu_relax(); 17929e8cd2f5STejun Heo goto retry; 17939e8cd2f5STejun Heo } 17949e8cd2f5STejun Heo /* oops */ 17959e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 17969e8cd2f5STejun Heo wq->name, cpu); 17979e8cd2f5STejun Heo } 17989e8cd2f5STejun Heo 1799112202d9STejun Heo /* pwq determined, queue */ 1800112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1801502ca9d8STejun Heo 180224acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 180324acfb71SThomas Gleixner goto out; 18041e19ffc6STejun Heo 1805112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1806112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 18071e19ffc6STejun Heo 1808a045a272STejun Heo /* 1809a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 1810a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 1811a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 1812a045a272STejun Heo */ 1813a045a272STejun Heo if (list_empty(&pwq->inactive_works) && 1814a045a272STejun Heo pwq->nr_active < READ_ONCE(pwq->wq->max_active)) { 1815fe089f87STejun Heo if (list_empty(&pool->worklist)) 1816fe089f87STejun Heo pool->watchdog_ts = jiffies; 1817fe089f87STejun Heo 1818cdadf009STejun Heo trace_workqueue_activate_work(work); 1819112202d9STejun Heo pwq->nr_active++; 1820fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 18210219a352STejun Heo kick_pool(pool); 18228a2e8e5dSTejun Heo } else { 1823f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1824fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 18258a2e8e5dSTejun Heo } 18261e19ffc6STejun Heo 182724acfb71SThomas Gleixner out: 1828fe089f87STejun Heo raw_spin_unlock(&pool->lock); 182924acfb71SThomas Gleixner rcu_read_unlock(); 18301da177e4SLinus Torvalds } 18311da177e4SLinus Torvalds 18320fcb78c2SRolf Eike Beer /** 1833c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1834c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1835c1a220e7SZhang Rui * @wq: workqueue to use 1836c1a220e7SZhang Rui * @work: work to queue 1837c1a220e7SZhang Rui * 1838c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1839443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1840443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1841854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1842854f5cc5SPaul E. McKenney * online will get a splat. 1843d185af30SYacine Belkadi * 1844d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1845c1a220e7SZhang Rui */ 1846d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1847d4283e93STejun Heo struct work_struct *work) 1848c1a220e7SZhang Rui { 1849d4283e93STejun Heo bool ret = false; 18508930cabaSTejun Heo unsigned long flags; 18518930cabaSTejun Heo 18528930cabaSTejun Heo local_irq_save(flags); 1853c1a220e7SZhang Rui 185422df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 18554690c4abSTejun Heo __queue_work(cpu, wq, work); 1856d4283e93STejun Heo ret = true; 1857c1a220e7SZhang Rui } 18588930cabaSTejun Heo 18598930cabaSTejun Heo local_irq_restore(flags); 1860c1a220e7SZhang Rui return ret; 1861c1a220e7SZhang Rui } 1862ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1863c1a220e7SZhang Rui 18648204e0c1SAlexander Duyck /** 1865fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 18668204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 18678204e0c1SAlexander Duyck * 18688204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 18698204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 18708204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 18718204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 18728204e0c1SAlexander Duyck */ 1873fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 18748204e0c1SAlexander Duyck { 18758204e0c1SAlexander Duyck int cpu; 18768204e0c1SAlexander Duyck 18778204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 18788204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 18798204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 18808204e0c1SAlexander Duyck 18818204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 18828204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 18838204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 18848204e0c1SAlexander Duyck return cpu; 18858204e0c1SAlexander Duyck 18868204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 18878204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 18888204e0c1SAlexander Duyck 18898204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 18908204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 18918204e0c1SAlexander Duyck } 18928204e0c1SAlexander Duyck 18938204e0c1SAlexander Duyck /** 18948204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 18958204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 18968204e0c1SAlexander Duyck * @wq: workqueue to use 18978204e0c1SAlexander Duyck * @work: work to queue 18988204e0c1SAlexander Duyck * 18998204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 19008204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 19018204e0c1SAlexander Duyck * NUMA node. 19028204e0c1SAlexander Duyck * 19038204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 19048204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 19058204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 19068204e0c1SAlexander Duyck * 19078204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 19088204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 19098204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 19108204e0c1SAlexander Duyck * 19118204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 19128204e0c1SAlexander Duyck */ 19138204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 19148204e0c1SAlexander Duyck struct work_struct *work) 19158204e0c1SAlexander Duyck { 19168204e0c1SAlexander Duyck unsigned long flags; 19178204e0c1SAlexander Duyck bool ret = false; 19188204e0c1SAlexander Duyck 19198204e0c1SAlexander Duyck /* 19208204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 19218204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 19228204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 19238204e0c1SAlexander Duyck * 19248204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 19258204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 19268204e0c1SAlexander Duyck * some round robin type logic. 19278204e0c1SAlexander Duyck */ 19288204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 19298204e0c1SAlexander Duyck 19308204e0c1SAlexander Duyck local_irq_save(flags); 19318204e0c1SAlexander Duyck 19328204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 1933fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 19348204e0c1SAlexander Duyck 19358204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 19368204e0c1SAlexander Duyck ret = true; 19378204e0c1SAlexander Duyck } 19388204e0c1SAlexander Duyck 19398204e0c1SAlexander Duyck local_irq_restore(flags); 19408204e0c1SAlexander Duyck return ret; 19418204e0c1SAlexander Duyck } 19428204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 19438204e0c1SAlexander Duyck 19448c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 19451da177e4SLinus Torvalds { 19468c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 19471da177e4SLinus Torvalds 1948e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 194960c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 19501da177e4SLinus Torvalds } 19511438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 19521da177e4SLinus Torvalds 19537beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 195452bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 19551da177e4SLinus Torvalds { 19567beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 19577beb2edfSTejun Heo struct work_struct *work = &dwork->work; 19581da177e4SLinus Torvalds 1959637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 19604b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1961fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1962fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 19637beb2edfSTejun Heo 19648852aac2STejun Heo /* 19658852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 19668852aac2STejun Heo * both optimization and correctness. The earliest @timer can 19678852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 19688852aac2STejun Heo * on that there's no such delay when @delay is 0. 19698852aac2STejun Heo */ 19708852aac2STejun Heo if (!delay) { 19718852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 19728852aac2STejun Heo return; 19738852aac2STejun Heo } 19748852aac2STejun Heo 197560c057bcSLai Jiangshan dwork->wq = wq; 19761265057fSTejun Heo dwork->cpu = cpu; 19777beb2edfSTejun Heo timer->expires = jiffies + delay; 19787beb2edfSTejun Heo 1979041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 19807beb2edfSTejun Heo add_timer_on(timer, cpu); 1981041bd12eSTejun Heo else 1982041bd12eSTejun Heo add_timer(timer); 19837beb2edfSTejun Heo } 19841da177e4SLinus Torvalds 19850fcb78c2SRolf Eike Beer /** 19860fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 19870fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 19880fcb78c2SRolf Eike Beer * @wq: workqueue to use 1989af9997e4SRandy Dunlap * @dwork: work to queue 19900fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 19910fcb78c2SRolf Eike Beer * 1992d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1993715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1994715f1300STejun Heo * execution. 19950fcb78c2SRolf Eike Beer */ 1996d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 199752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 19987a6bc1cdSVenkatesh Pallipadi { 199952bad64dSDavid Howells struct work_struct *work = &dwork->work; 2000d4283e93STejun Heo bool ret = false; 20018930cabaSTejun Heo unsigned long flags; 20028930cabaSTejun Heo 20038930cabaSTejun Heo /* read the comment in __queue_work() */ 20048930cabaSTejun Heo local_irq_save(flags); 20057a6bc1cdSVenkatesh Pallipadi 200622df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 20077beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2008d4283e93STejun Heo ret = true; 20097a6bc1cdSVenkatesh Pallipadi } 20108930cabaSTejun Heo 20118930cabaSTejun Heo local_irq_restore(flags); 20127a6bc1cdSVenkatesh Pallipadi return ret; 20137a6bc1cdSVenkatesh Pallipadi } 2014ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 20151da177e4SLinus Torvalds 2016c8e55f36STejun Heo /** 20178376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 20188376fe22STejun Heo * @cpu: CPU number to execute work on 20198376fe22STejun Heo * @wq: workqueue to use 20208376fe22STejun Heo * @dwork: work to queue 20218376fe22STejun Heo * @delay: number of jiffies to wait before queueing 20228376fe22STejun Heo * 20238376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 20248376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 20258376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 20268376fe22STejun Heo * current state. 20278376fe22STejun Heo * 2028d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 20298376fe22STejun Heo * pending and its timer was modified. 20308376fe22STejun Heo * 2031e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 20328376fe22STejun Heo * See try_to_grab_pending() for details. 20338376fe22STejun Heo */ 20348376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 20358376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 20368376fe22STejun Heo { 20378376fe22STejun Heo unsigned long flags; 20388376fe22STejun Heo int ret; 20398376fe22STejun Heo 20408376fe22STejun Heo do { 20418376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 20428376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 20438376fe22STejun Heo 20448376fe22STejun Heo if (likely(ret >= 0)) { 20458376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 20468376fe22STejun Heo local_irq_restore(flags); 20478376fe22STejun Heo } 20488376fe22STejun Heo 20498376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 20508376fe22STejun Heo return ret; 20518376fe22STejun Heo } 20528376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 20538376fe22STejun Heo 205405f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 205505f0fe6bSTejun Heo { 205605f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 205705f0fe6bSTejun Heo 205805f0fe6bSTejun Heo /* read the comment in __queue_work() */ 205905f0fe6bSTejun Heo local_irq_disable(); 206005f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 206105f0fe6bSTejun Heo local_irq_enable(); 206205f0fe6bSTejun Heo } 206305f0fe6bSTejun Heo 206405f0fe6bSTejun Heo /** 206505f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 206605f0fe6bSTejun Heo * @wq: workqueue to use 206705f0fe6bSTejun Heo * @rwork: work to queue 206805f0fe6bSTejun Heo * 206905f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 207005f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2071bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 207205f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 207305f0fe6bSTejun Heo */ 207405f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 207505f0fe6bSTejun Heo { 207605f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 207705f0fe6bSTejun Heo 207805f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 207905f0fe6bSTejun Heo rwork->wq = wq; 2080a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 208105f0fe6bSTejun Heo return true; 208205f0fe6bSTejun Heo } 208305f0fe6bSTejun Heo 208405f0fe6bSTejun Heo return false; 208505f0fe6bSTejun Heo } 208605f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 208705f0fe6bSTejun Heo 2088f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2089c34056a3STejun Heo { 2090c34056a3STejun Heo struct worker *worker; 2091c34056a3STejun Heo 2092f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2093c8e55f36STejun Heo if (worker) { 2094c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2095affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2096da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2097e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2098e22bee78STejun Heo worker->flags = WORKER_PREP; 2099c8e55f36STejun Heo } 2100c34056a3STejun Heo return worker; 2101c34056a3STejun Heo } 2102c34056a3STejun Heo 21039546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 21049546b29eSTejun Heo { 21058639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 21069546b29eSTejun Heo return pool->attrs->__pod_cpumask; 21078639ecebSTejun Heo else 21088639ecebSTejun Heo return pool->attrs->cpumask; 21099546b29eSTejun Heo } 21109546b29eSTejun Heo 2111c34056a3STejun Heo /** 21124736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 21134736cbf7SLai Jiangshan * @worker: worker to be attached 21144736cbf7SLai Jiangshan * @pool: the target pool 21154736cbf7SLai Jiangshan * 21164736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 21174736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 21184736cbf7SLai Jiangshan * cpu-[un]hotplugs. 21194736cbf7SLai Jiangshan */ 21204736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 21214736cbf7SLai Jiangshan struct worker_pool *pool) 21224736cbf7SLai Jiangshan { 21231258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 21244736cbf7SLai Jiangshan 21254736cbf7SLai Jiangshan /* 21261258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 21271258fae7STejun Heo * stable across this function. See the comments above the flag 21281258fae7STejun Heo * definition for details. 21294736cbf7SLai Jiangshan */ 21304736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 21314736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 21325c25b5ffSPeter Zijlstra else 21335c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 21344736cbf7SLai Jiangshan 2135640f17c8SPeter Zijlstra if (worker->rescue_wq) 21369546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2137640f17c8SPeter Zijlstra 21384736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2139a2d812a2STejun Heo worker->pool = pool; 21404736cbf7SLai Jiangshan 21411258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 21424736cbf7SLai Jiangshan } 21434736cbf7SLai Jiangshan 21444736cbf7SLai Jiangshan /** 214560f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 214660f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 214760f5a4bcSLai Jiangshan * 21484736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 21494736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 21504736cbf7SLai Jiangshan * other reference to the pool. 215160f5a4bcSLai Jiangshan */ 2152a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 215360f5a4bcSLai Jiangshan { 2154a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 215560f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 215660f5a4bcSLai Jiangshan 21571258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2158a2d812a2STejun Heo 21595c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2160da028469SLai Jiangshan list_del(&worker->node); 2161a2d812a2STejun Heo worker->pool = NULL; 2162a2d812a2STejun Heo 2163e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 216460f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 21651258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 216660f5a4bcSLai Jiangshan 2167b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2168b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2169b62c0751SLai Jiangshan 217060f5a4bcSLai Jiangshan if (detach_completion) 217160f5a4bcSLai Jiangshan complete(detach_completion); 217260f5a4bcSLai Jiangshan } 217360f5a4bcSLai Jiangshan 217460f5a4bcSLai Jiangshan /** 2175c34056a3STejun Heo * create_worker - create a new workqueue worker 217663d95a91STejun Heo * @pool: pool the new worker will belong to 2177c34056a3STejun Heo * 2178051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2179c34056a3STejun Heo * 2180c34056a3STejun Heo * CONTEXT: 2181c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2182c34056a3STejun Heo * 2183d185af30SYacine Belkadi * Return: 2184c34056a3STejun Heo * Pointer to the newly created worker. 2185c34056a3STejun Heo */ 2186bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2187c34056a3STejun Heo { 2188e441b56fSZhen Lei struct worker *worker; 2189e441b56fSZhen Lei int id; 21905d9c7a1eSLucy Mielke char id_buf[23]; 2191c34056a3STejun Heo 21927cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2193e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 21943f0ea0b8SPetr Mladek if (id < 0) { 21953f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 21963f0ea0b8SPetr Mladek ERR_PTR(id)); 2197e441b56fSZhen Lei return NULL; 21983f0ea0b8SPetr Mladek } 2199c34056a3STejun Heo 2200f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 22013f0ea0b8SPetr Mladek if (!worker) { 22023f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2203c34056a3STejun Heo goto fail; 22043f0ea0b8SPetr Mladek } 2205c34056a3STejun Heo 2206c34056a3STejun Heo worker->id = id; 2207c34056a3STejun Heo 220829c91e99STejun Heo if (pool->cpu >= 0) 2209e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2210e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2211f3421797STejun Heo else 2212e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2213e3c916a4STejun Heo 2214f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2215e3c916a4STejun Heo "kworker/%s", id_buf); 22163f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 221760f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 221860f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 221960f54038SPetr Mladek id_buf); 222060f54038SPetr Mladek } else { 22213f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 22223f0ea0b8SPetr Mladek worker->task); 222360f54038SPetr Mladek } 2224c34056a3STejun Heo goto fail; 22253f0ea0b8SPetr Mladek } 2226c34056a3STejun Heo 222791151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 22289546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 222991151228SOleg Nesterov 2230da028469SLai Jiangshan /* successful, attach the worker to the pool */ 22314736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2232822d8405STejun Heo 2233051e1850SLai Jiangshan /* start the newly created worker */ 2234a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 22350219a352STejun Heo 2236051e1850SLai Jiangshan worker->pool->nr_workers++; 2237051e1850SLai Jiangshan worker_enter_idle(worker); 22380219a352STejun Heo 22390219a352STejun Heo /* 22400219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 22416a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 22426a229b0eSTejun Heo * wake it up explicitly. 22430219a352STejun Heo */ 2244051e1850SLai Jiangshan wake_up_process(worker->task); 22450219a352STejun Heo 2246a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2247051e1850SLai Jiangshan 2248c34056a3STejun Heo return worker; 2249822d8405STejun Heo 2250c34056a3STejun Heo fail: 2251e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2252c34056a3STejun Heo kfree(worker); 2253c34056a3STejun Heo return NULL; 2254c34056a3STejun Heo } 2255c34056a3STejun Heo 2256793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2257793777bcSValentin Schneider { 2258793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2259793777bcSValentin Schneider 2260793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2261793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2262793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2263793777bcSValentin Schneider else 2264793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2265793777bcSValentin Schneider } 2266793777bcSValentin Schneider 2267e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2268e02b9312SValentin Schneider { 2269e02b9312SValentin Schneider struct worker *worker, *tmp; 2270e02b9312SValentin Schneider 2271e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2272e02b9312SValentin Schneider list_del_init(&worker->entry); 2273e02b9312SValentin Schneider unbind_worker(worker); 2274e02b9312SValentin Schneider /* 2275e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2276e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2277e02b9312SValentin Schneider * wouldn't have gotten here. 2278c34056a3STejun Heo * 2279e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2280e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2281e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2282e02b9312SValentin Schneider * outside of pool->lock. 2283e02b9312SValentin Schneider */ 2284e02b9312SValentin Schneider wake_up_process(worker->task); 2285e02b9312SValentin Schneider } 2286e02b9312SValentin Schneider } 2287e02b9312SValentin Schneider 2288e02b9312SValentin Schneider /** 2289e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2290e02b9312SValentin Schneider * @worker: worker to be destroyed 2291e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2292e02b9312SValentin Schneider * 2293e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2294e02b9312SValentin Schneider * should be idle. 2295c8e55f36STejun Heo * 2296c8e55f36STejun Heo * CONTEXT: 2297a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2298c34056a3STejun Heo */ 2299e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2300c34056a3STejun Heo { 2301bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2302c34056a3STejun Heo 2303cd549687STejun Heo lockdep_assert_held(&pool->lock); 2304e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2305cd549687STejun Heo 2306c34056a3STejun Heo /* sanity check frenzy */ 23076183c009STejun Heo if (WARN_ON(worker->current_work) || 230873eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 230973eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 23106183c009STejun Heo return; 2311c34056a3STejun Heo 2312bd7bdd43STejun Heo pool->nr_workers--; 2313bd7bdd43STejun Heo pool->nr_idle--; 2314c8e55f36STejun Heo 2315cb444766STejun Heo worker->flags |= WORKER_DIE; 2316e02b9312SValentin Schneider 2317e02b9312SValentin Schneider list_move(&worker->entry, list); 2318e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2319c34056a3STejun Heo } 2320c34056a3STejun Heo 23213f959aa3SValentin Schneider /** 23223f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 23233f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 23243f959aa3SValentin Schneider * 23253f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 23263f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 23273f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 23283f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 23293f959aa3SValentin Schneider * it expire and re-evaluate things from there. 23303f959aa3SValentin Schneider */ 233132a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2332e22bee78STejun Heo { 233332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 23343f959aa3SValentin Schneider bool do_cull = false; 23353f959aa3SValentin Schneider 23363f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 23373f959aa3SValentin Schneider return; 23383f959aa3SValentin Schneider 23393f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 23403f959aa3SValentin Schneider 23413f959aa3SValentin Schneider if (too_many_workers(pool)) { 23423f959aa3SValentin Schneider struct worker *worker; 23433f959aa3SValentin Schneider unsigned long expires; 23443f959aa3SValentin Schneider 23453f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 23463f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 23473f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 23483f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 23493f959aa3SValentin Schneider 23503f959aa3SValentin Schneider if (!do_cull) 23513f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 23523f959aa3SValentin Schneider } 23533f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 23543f959aa3SValentin Schneider 23553f959aa3SValentin Schneider if (do_cull) 23563f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 23573f959aa3SValentin Schneider } 23583f959aa3SValentin Schneider 23593f959aa3SValentin Schneider /** 23603f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 23613f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 23623f959aa3SValentin Schneider * 23633f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 23643f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2365e02b9312SValentin Schneider * 2366e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2367e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2368e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 23693f959aa3SValentin Schneider */ 23703f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 23713f959aa3SValentin Schneider { 23723f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 23739680540cSYang Yingliang LIST_HEAD(cull_list); 2374e22bee78STejun Heo 2375e02b9312SValentin Schneider /* 2376e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2377e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2378e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2379e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2380e02b9312SValentin Schneider */ 2381e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2382a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2383e22bee78STejun Heo 23843347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2385e22bee78STejun Heo struct worker *worker; 2386e22bee78STejun Heo unsigned long expires; 2387e22bee78STejun Heo 238863d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2389e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2390e22bee78STejun Heo 23913347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 239263d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 23933347fc9fSLai Jiangshan break; 2394e22bee78STejun Heo } 23953347fc9fSLai Jiangshan 2396e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2397e22bee78STejun Heo } 2398e22bee78STejun Heo 2399a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2400e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2401e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2402e22bee78STejun Heo } 2403e22bee78STejun Heo 2404493a1724STejun Heo static void send_mayday(struct work_struct *work) 2405e22bee78STejun Heo { 2406112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2407112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2408493a1724STejun Heo 24092e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2410e22bee78STejun Heo 2411493008a8STejun Heo if (!wq->rescuer) 2412493a1724STejun Heo return; 2413e22bee78STejun Heo 2414e22bee78STejun Heo /* mayday mayday mayday */ 2415493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 241677668c8bSLai Jiangshan /* 241777668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 241877668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 241977668c8bSLai Jiangshan * rescuer is done with it. 242077668c8bSLai Jiangshan */ 242177668c8bSLai Jiangshan get_pwq(pwq); 2422493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2423e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2424725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2425493a1724STejun Heo } 2426e22bee78STejun Heo } 2427e22bee78STejun Heo 242832a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2429e22bee78STejun Heo { 243032a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2431e22bee78STejun Heo struct work_struct *work; 2432e22bee78STejun Heo 2433a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2434a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2435e22bee78STejun Heo 243663d95a91STejun Heo if (need_to_create_worker(pool)) { 2437e22bee78STejun Heo /* 2438e22bee78STejun Heo * We've been trying to create a new worker but 2439e22bee78STejun Heo * haven't been successful. We might be hitting an 2440e22bee78STejun Heo * allocation deadlock. Send distress signals to 2441e22bee78STejun Heo * rescuers. 2442e22bee78STejun Heo */ 244363d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2444e22bee78STejun Heo send_mayday(work); 2445e22bee78STejun Heo } 2446e22bee78STejun Heo 2447a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2448a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2449e22bee78STejun Heo 245063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2451e22bee78STejun Heo } 2452e22bee78STejun Heo 2453e22bee78STejun Heo /** 2454e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 245563d95a91STejun Heo * @pool: pool to create a new worker for 2456e22bee78STejun Heo * 245763d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2458e22bee78STejun Heo * have at least one idle worker on return from this function. If 2459e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 246063d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2461e22bee78STejun Heo * possible allocation deadlock. 2462e22bee78STejun Heo * 2463c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2464c5aa87bbSTejun Heo * may_start_working() %true. 2465e22bee78STejun Heo * 2466e22bee78STejun Heo * LOCKING: 2467a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2468e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2469e22bee78STejun Heo * manager. 2470e22bee78STejun Heo */ 247129187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2472d565ed63STejun Heo __releases(&pool->lock) 2473d565ed63STejun Heo __acquires(&pool->lock) 2474e22bee78STejun Heo { 2475e22bee78STejun Heo restart: 2476a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 24779f9c2364STejun Heo 2478e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 247963d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2480e22bee78STejun Heo 2481e22bee78STejun Heo while (true) { 2482051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2483e22bee78STejun Heo break; 2484e22bee78STejun Heo 2485e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 24869f9c2364STejun Heo 248763d95a91STejun Heo if (!need_to_create_worker(pool)) 2488e22bee78STejun Heo break; 2489e22bee78STejun Heo } 2490e22bee78STejun Heo 249163d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2492a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2493051e1850SLai Jiangshan /* 2494051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2495051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2496051e1850SLai Jiangshan * already become busy. 2497051e1850SLai Jiangshan */ 249863d95a91STejun Heo if (need_to_create_worker(pool)) 2499e22bee78STejun Heo goto restart; 2500e22bee78STejun Heo } 2501e22bee78STejun Heo 2502e22bee78STejun Heo /** 2503e22bee78STejun Heo * manage_workers - manage worker pool 2504e22bee78STejun Heo * @worker: self 2505e22bee78STejun Heo * 2506706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2507e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2508706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2509e22bee78STejun Heo * 2510e22bee78STejun Heo * The caller can safely start processing works on false return. On 2511e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2512e22bee78STejun Heo * and may_start_working() is true. 2513e22bee78STejun Heo * 2514e22bee78STejun Heo * CONTEXT: 2515a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2516e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2517e22bee78STejun Heo * 2518d185af30SYacine Belkadi * Return: 251929187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 252029187a9eSTejun Heo * start processing works, %true if management function was performed and 252129187a9eSTejun Heo * the conditions that the caller verified before calling the function may 252229187a9eSTejun Heo * no longer be true. 2523e22bee78STejun Heo */ 2524e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2525e22bee78STejun Heo { 252663d95a91STejun Heo struct worker_pool *pool = worker->pool; 2527e22bee78STejun Heo 2528692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 252929187a9eSTejun Heo return false; 2530692b4825STejun Heo 2531692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 25322607d7a6STejun Heo pool->manager = worker; 2533e22bee78STejun Heo 253429187a9eSTejun Heo maybe_create_worker(pool); 2535e22bee78STejun Heo 25362607d7a6STejun Heo pool->manager = NULL; 2537692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2538d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 253929187a9eSTejun Heo return true; 2540e22bee78STejun Heo } 2541e22bee78STejun Heo 2542a62428c0STejun Heo /** 2543a62428c0STejun Heo * process_one_work - process single work 2544c34056a3STejun Heo * @worker: self 2545a62428c0STejun Heo * @work: work to process 2546a62428c0STejun Heo * 2547a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2548a62428c0STejun Heo * process a single work including synchronization against and 2549a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2550a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2551a62428c0STejun Heo * call this function to process a work. 2552a62428c0STejun Heo * 2553a62428c0STejun Heo * CONTEXT: 2554a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2555a62428c0STejun Heo */ 2556c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2557d565ed63STejun Heo __releases(&pool->lock) 2558d565ed63STejun Heo __acquires(&pool->lock) 25591da177e4SLinus Torvalds { 2560112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2561bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2562c4560c2cSLai Jiangshan unsigned long work_data; 25634e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 25644e6045f1SJohannes Berg /* 2565a62428c0STejun Heo * It is permissible to free the struct work_struct from 2566a62428c0STejun Heo * inside the function that is called from it, this we need to 2567a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2568a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2569a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 25704e6045f1SJohannes Berg */ 25714d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 25724d82a1deSPeter Zijlstra 25734d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 25744e6045f1SJohannes Berg #endif 2575807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 257685327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2577ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 257825511a47STejun Heo 25798930cabaSTejun Heo /* claim and dequeue */ 2580dc186ad7SThomas Gleixner debug_work_deactivate(work); 2581c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2582c34056a3STejun Heo worker->current_work = work; 2583a2c1c57bSTejun Heo worker->current_func = work->func; 2584112202d9STejun Heo worker->current_pwq = pwq; 2585616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2586c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2587d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 25887a22ad75STejun Heo 25898bf89593STejun Heo /* 25908bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 25918bf89593STejun Heo * overridden through set_worker_desc(). 25928bf89593STejun Heo */ 25938bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 25948bf89593STejun Heo 2595a62428c0STejun Heo list_del_init(&work->entry); 2596a62428c0STejun Heo 2597649027d7STejun Heo /* 2598228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2599228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2600228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2601228f1d00SLai Jiangshan * execution of the pending work items. 2602fb0e7bebSTejun Heo */ 2603616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2604228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2605fb0e7bebSTejun Heo 2606974271c4STejun Heo /* 26070219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 26080219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 26090219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 26100219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 2611974271c4STejun Heo */ 26120219a352STejun Heo kick_pool(pool); 2613974271c4STejun Heo 26148930cabaSTejun Heo /* 26157c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2616d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 261723657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 261823657bb1STejun Heo * disabled. 26198930cabaSTejun Heo */ 26207c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 26211da177e4SLinus Torvalds 2622fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 2623a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2624365970a1SDavid Howells 2625a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 26263295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2627e6f3faa7SPeter Zijlstra /* 2628f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2629f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2630e6f3faa7SPeter Zijlstra * 2631e6f3faa7SPeter Zijlstra * However, that would result in: 2632e6f3faa7SPeter Zijlstra * 2633e6f3faa7SPeter Zijlstra * A(W1) 2634e6f3faa7SPeter Zijlstra * WFC(C) 2635e6f3faa7SPeter Zijlstra * A(W1) 2636e6f3faa7SPeter Zijlstra * C(C) 2637e6f3faa7SPeter Zijlstra * 2638e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2639e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2640e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2641f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2642e6f3faa7SPeter Zijlstra * these locks. 2643e6f3faa7SPeter Zijlstra * 2644e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2645e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2646e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2647e6f3faa7SPeter Zijlstra */ 2648f52be570SPeter Zijlstra lockdep_invariant_state(true); 2649e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2650a2c1c57bSTejun Heo worker->current_func(work); 2651e36c886aSArjan van de Ven /* 2652e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2653e36c886aSArjan van de Ven * point will only record its address. 2654e36c886aSArjan van de Ven */ 26551c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2656725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 26573295f0efSIngo Molnar lock_map_release(&lockdep_map); 2658112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 26591da177e4SLinus Torvalds 26601a65a6d1SXuewen Yan if (unlikely(in_atomic() || lockdep_depth(current) > 0 || 26611a65a6d1SXuewen Yan rcu_preempt_depth() > 0)) { 26621a65a6d1SXuewen Yan pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d/%d\n" 2663d75f773cSSakari Ailus " last function: %ps\n", 26641a65a6d1SXuewen Yan current->comm, preempt_count(), rcu_preempt_depth(), 26651a65a6d1SXuewen Yan task_pid_nr(current), worker->current_func); 2666d5abe669SPeter Zijlstra debug_show_held_locks(current); 2667d5abe669SPeter Zijlstra dump_stack(); 2668d5abe669SPeter Zijlstra } 2669d5abe669SPeter Zijlstra 2670b22ce278STejun Heo /* 2671025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2672b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2673b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2674b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2675789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2676789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2677b22ce278STejun Heo */ 2678a7e6425eSPaul E. McKenney cond_resched(); 2679b22ce278STejun Heo 2680a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2681a62428c0STejun Heo 2682616db877STejun Heo /* 2683616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 2684616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 2685616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 2686616db877STejun Heo */ 2687fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2688fb0e7bebSTejun Heo 26891b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 26901b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 26911b69ac6bSJohannes Weiner 2692a62428c0STejun Heo /* we're done with it, release */ 269342f8570fSSasha Levin hash_del(&worker->hentry); 2694c34056a3STejun Heo worker->current_work = NULL; 2695a2c1c57bSTejun Heo worker->current_func = NULL; 2696112202d9STejun Heo worker->current_pwq = NULL; 2697d812796eSLai Jiangshan worker->current_color = INT_MAX; 2698c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 26991da177e4SLinus Torvalds } 27001da177e4SLinus Torvalds 2701affee4b2STejun Heo /** 2702affee4b2STejun Heo * process_scheduled_works - process scheduled works 2703affee4b2STejun Heo * @worker: self 2704affee4b2STejun Heo * 2705affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2706affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2707affee4b2STejun Heo * fetches a work from the top and executes it. 2708affee4b2STejun Heo * 2709affee4b2STejun Heo * CONTEXT: 2710a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2711affee4b2STejun Heo * multiple times. 2712affee4b2STejun Heo */ 2713affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 27141da177e4SLinus Torvalds { 2715c0ab017dSTejun Heo struct work_struct *work; 2716c0ab017dSTejun Heo bool first = true; 2717c0ab017dSTejun Heo 2718c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 2719c0ab017dSTejun Heo struct work_struct, entry))) { 2720c0ab017dSTejun Heo if (first) { 2721c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 2722c0ab017dSTejun Heo first = false; 2723c0ab017dSTejun Heo } 2724c34056a3STejun Heo process_one_work(worker, work); 2725a62428c0STejun Heo } 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 2728197f6accSTejun Heo static void set_pf_worker(bool val) 2729197f6accSTejun Heo { 2730197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2731197f6accSTejun Heo if (val) 2732197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2733197f6accSTejun Heo else 2734197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2735197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2736197f6accSTejun Heo } 2737197f6accSTejun Heo 27384690c4abSTejun Heo /** 27394690c4abSTejun Heo * worker_thread - the worker thread function 2740c34056a3STejun Heo * @__worker: self 27414690c4abSTejun Heo * 2742c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2743c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2744c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2745c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2746c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2747d185af30SYacine Belkadi * 2748d185af30SYacine Belkadi * Return: 0 27494690c4abSTejun Heo */ 2750c34056a3STejun Heo static int worker_thread(void *__worker) 27511da177e4SLinus Torvalds { 2752c34056a3STejun Heo struct worker *worker = __worker; 2753bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 27541da177e4SLinus Torvalds 2755e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2756197f6accSTejun Heo set_pf_worker(true); 2757c8e55f36STejun Heo woke_up: 2758a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2759affee4b2STejun Heo 2760a9ab775bSTejun Heo /* am I supposed to die? */ 2761a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2762a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2763197f6accSTejun Heo set_pf_worker(false); 276460f5a4bcSLai Jiangshan 276560f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2766e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2767a2d812a2STejun Heo worker_detach_from_pool(worker); 2768e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 276960f5a4bcSLai Jiangshan kfree(worker); 2770c8e55f36STejun Heo return 0; 2771c8e55f36STejun Heo } 2772c8e55f36STejun Heo 2773c8e55f36STejun Heo worker_leave_idle(worker); 2774db7bccf4STejun Heo recheck: 2775e22bee78STejun Heo /* no more worker necessary? */ 277663d95a91STejun Heo if (!need_more_worker(pool)) 2777e22bee78STejun Heo goto sleep; 2778e22bee78STejun Heo 2779e22bee78STejun Heo /* do we need to manage? */ 278063d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2781e22bee78STejun Heo goto recheck; 2782e22bee78STejun Heo 2783c8e55f36STejun Heo /* 2784c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2785c8e55f36STejun Heo * preparing to process a work or actually processing it. 2786c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2787c8e55f36STejun Heo */ 27886183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2789c8e55f36STejun Heo 2790e22bee78STejun Heo /* 2791a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2792a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2793a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2794a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2795a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2796e22bee78STejun Heo */ 2797a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2798e22bee78STejun Heo 2799e22bee78STejun Heo do { 2800affee4b2STejun Heo struct work_struct *work = 2801bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2802affee4b2STejun Heo struct work_struct, entry); 2803affee4b2STejun Heo 2804873eaca6STejun Heo if (assign_work(work, worker, NULL)) 2805affee4b2STejun Heo process_scheduled_works(worker); 280663d95a91STejun Heo } while (keep_working(pool)); 2807affee4b2STejun Heo 2808228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2809d313dd85STejun Heo sleep: 2810c8e55f36STejun Heo /* 2811d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2812d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2813d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2814d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2815d565ed63STejun Heo * event. 2816c8e55f36STejun Heo */ 2817c8e55f36STejun Heo worker_enter_idle(worker); 2818c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2819a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 28201da177e4SLinus Torvalds schedule(); 2821c8e55f36STejun Heo goto woke_up; 28221da177e4SLinus Torvalds } 28231da177e4SLinus Torvalds 2824e22bee78STejun Heo /** 2825e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2826111c225aSTejun Heo * @__rescuer: self 2827e22bee78STejun Heo * 2828e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2829493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2830e22bee78STejun Heo * 2831706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2832e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2833e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2834e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2835e22bee78STejun Heo * the problem rescuer solves. 2836e22bee78STejun Heo * 2837706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2838706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2839e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2840e22bee78STejun Heo * 2841e22bee78STejun Heo * This should happen rarely. 2842d185af30SYacine Belkadi * 2843d185af30SYacine Belkadi * Return: 0 2844e22bee78STejun Heo */ 2845111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2846e22bee78STejun Heo { 2847111c225aSTejun Heo struct worker *rescuer = __rescuer; 2848111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 28494d595b86SLai Jiangshan bool should_stop; 2850e22bee78STejun Heo 2851e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2852111c225aSTejun Heo 2853111c225aSTejun Heo /* 2854111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2855111c225aSTejun Heo * doesn't participate in concurrency management. 2856111c225aSTejun Heo */ 2857197f6accSTejun Heo set_pf_worker(true); 2858e22bee78STejun Heo repeat: 2859c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 28601da177e4SLinus Torvalds 28614d595b86SLai Jiangshan /* 28624d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 28634d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 28644d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 28654d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 28664d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 28674d595b86SLai Jiangshan * list is always empty on exit. 28684d595b86SLai Jiangshan */ 28694d595b86SLai Jiangshan should_stop = kthread_should_stop(); 28701da177e4SLinus Torvalds 2871493a1724STejun Heo /* see whether any pwq is asking for help */ 2872a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2873493a1724STejun Heo 2874493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2875493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2876493a1724STejun Heo struct pool_workqueue, mayday_node); 2877112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2878e22bee78STejun Heo struct work_struct *work, *n; 2879e22bee78STejun Heo 2880e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2881493a1724STejun Heo list_del_init(&pwq->mayday_node); 2882493a1724STejun Heo 2883a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2884e22bee78STejun Heo 288551697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 288651697d39SLai Jiangshan 2887a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2888e22bee78STejun Heo 2889e22bee78STejun Heo /* 2890e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2891e22bee78STejun Heo * process'em. 2892e22bee78STejun Heo */ 2893873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 289482607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 2895873eaca6STejun Heo if (get_work_pwq(work) == pwq && 2896873eaca6STejun Heo assign_work(work, rescuer, &n)) 2897725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 289882607adcSTejun Heo } 2899e22bee78STejun Heo 2900873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 2901e22bee78STejun Heo process_scheduled_works(rescuer); 29027576958aSTejun Heo 29037576958aSTejun Heo /* 2904008847f6SNeilBrown * The above execution of rescued work items could 2905008847f6SNeilBrown * have created more to rescue through 2906f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2907008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2908008847f6SNeilBrown * that such back-to-back work items, which may be 2909008847f6SNeilBrown * being used to relieve memory pressure, don't 2910008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2911008847f6SNeilBrown */ 29124f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2913a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2914e66b39afSTejun Heo /* 2915e66b39afSTejun Heo * Queue iff we aren't racing destruction 2916e66b39afSTejun Heo * and somebody else hasn't queued it already. 2917e66b39afSTejun Heo */ 2918e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2919008847f6SNeilBrown get_pwq(pwq); 2920e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2921e66b39afSTejun Heo } 2922a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2923008847f6SNeilBrown } 2924008847f6SNeilBrown } 2925008847f6SNeilBrown 2926008847f6SNeilBrown /* 292777668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 292813b1d625SLai Jiangshan * go away while we're still attached to it. 292977668c8bSLai Jiangshan */ 293077668c8bSLai Jiangshan put_pwq(pwq); 293177668c8bSLai Jiangshan 293277668c8bSLai Jiangshan /* 29330219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 29340219a352STejun Heo * with 0 concurrency and stalling the execution. 29357576958aSTejun Heo */ 29360219a352STejun Heo kick_pool(pool); 29377576958aSTejun Heo 2938a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 293913b1d625SLai Jiangshan 2940a2d812a2STejun Heo worker_detach_from_pool(rescuer); 294113b1d625SLai Jiangshan 2942a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 29431da177e4SLinus Torvalds } 29441da177e4SLinus Torvalds 2945a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2946493a1724STejun Heo 29474d595b86SLai Jiangshan if (should_stop) { 29484d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2949197f6accSTejun Heo set_pf_worker(false); 29504d595b86SLai Jiangshan return 0; 29514d595b86SLai Jiangshan } 29524d595b86SLai Jiangshan 2953111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2954111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2955e22bee78STejun Heo schedule(); 2956e22bee78STejun Heo goto repeat; 29571da177e4SLinus Torvalds } 29581da177e4SLinus Torvalds 2959fca839c0STejun Heo /** 2960fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2961fca839c0STejun Heo * @target_wq: workqueue being flushed 2962fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2963fca839c0STejun Heo * 2964fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2965fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2966fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2967fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2968fca839c0STejun Heo * a deadlock. 2969fca839c0STejun Heo */ 2970fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2971fca839c0STejun Heo struct work_struct *target_work) 2972fca839c0STejun Heo { 2973fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2974fca839c0STejun Heo struct worker *worker; 2975fca839c0STejun Heo 2976fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2977fca839c0STejun Heo return; 2978fca839c0STejun Heo 2979fca839c0STejun Heo worker = current_wq_worker(); 2980fca839c0STejun Heo 2981fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2982d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2983fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 298423d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 298523d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2986d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2987fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2988fca839c0STejun Heo target_wq->name, target_func); 2989fca839c0STejun Heo } 2990fca839c0STejun Heo 2991fc2e4d70SOleg Nesterov struct wq_barrier { 2992fc2e4d70SOleg Nesterov struct work_struct work; 2993fc2e4d70SOleg Nesterov struct completion done; 29942607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2995fc2e4d70SOleg Nesterov }; 2996fc2e4d70SOleg Nesterov 2997fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2998fc2e4d70SOleg Nesterov { 2999fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3000fc2e4d70SOleg Nesterov complete(&barr->done); 3001fc2e4d70SOleg Nesterov } 3002fc2e4d70SOleg Nesterov 30034690c4abSTejun Heo /** 30044690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3005112202d9STejun Heo * @pwq: pwq to insert barrier into 30064690c4abSTejun Heo * @barr: wq_barrier to insert 3007affee4b2STejun Heo * @target: target work to attach @barr to 3008affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 30094690c4abSTejun Heo * 3010affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3011affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3012affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3013affee4b2STejun Heo * cpu. 3014affee4b2STejun Heo * 3015affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3016affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3017affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3018affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3019affee4b2STejun Heo * after a work with LINKED flag set. 3020affee4b2STejun Heo * 3021affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3022112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 30234690c4abSTejun Heo * 30244690c4abSTejun Heo * CONTEXT: 3025a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 30264690c4abSTejun Heo */ 3027112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3028affee4b2STejun Heo struct wq_barrier *barr, 3029affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3030fc2e4d70SOleg Nesterov { 3031d812796eSLai Jiangshan unsigned int work_flags = 0; 3032d812796eSLai Jiangshan unsigned int work_color; 3033affee4b2STejun Heo struct list_head *head; 3034affee4b2STejun Heo 3035dc186ad7SThomas Gleixner /* 3036d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3037dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3038dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3039dc186ad7SThomas Gleixner * might deadlock. 3040dc186ad7SThomas Gleixner */ 3041ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 304222df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 304352fa5bc5SBoqun Feng 3044fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3045fd1a5b04SByungchul Park 30462607d7a6STejun Heo barr->task = current; 304783c22520SOleg Nesterov 3048018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 3049018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3050018f3a13SLai Jiangshan 3051affee4b2STejun Heo /* 3052affee4b2STejun Heo * If @target is currently being executed, schedule the 3053affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3054affee4b2STejun Heo */ 3055d812796eSLai Jiangshan if (worker) { 3056affee4b2STejun Heo head = worker->scheduled.next; 3057d812796eSLai Jiangshan work_color = worker->current_color; 3058d812796eSLai Jiangshan } else { 3059affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3060affee4b2STejun Heo 3061affee4b2STejun Heo head = target->entry.next; 3062affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3063d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3064d812796eSLai Jiangshan work_color = get_work_color(*bits); 3065affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3066affee4b2STejun Heo } 3067affee4b2STejun Heo 3068d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3069d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3070d812796eSLai Jiangshan 3071d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3072fc2e4d70SOleg Nesterov } 3073fc2e4d70SOleg Nesterov 307473f53c4aSTejun Heo /** 3075112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 307673f53c4aSTejun Heo * @wq: workqueue being flushed 307773f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 307873f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 307973f53c4aSTejun Heo * 3080112202d9STejun Heo * Prepare pwqs for workqueue flushing. 308173f53c4aSTejun Heo * 3082112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3083112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3084112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3085112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3086112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 308773f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 308873f53c4aSTejun Heo * 308973f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 309073f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 309173f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 309273f53c4aSTejun Heo * is returned. 309373f53c4aSTejun Heo * 3094112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 309573f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 309673f53c4aSTejun Heo * advanced to @work_color. 309773f53c4aSTejun Heo * 309873f53c4aSTejun Heo * CONTEXT: 30993c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 310073f53c4aSTejun Heo * 3101d185af30SYacine Belkadi * Return: 310273f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 310373f53c4aSTejun Heo * otherwise. 310473f53c4aSTejun Heo */ 3105112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 310673f53c4aSTejun Heo int flush_color, int work_color) 31071da177e4SLinus Torvalds { 310873f53c4aSTejun Heo bool wait = false; 310949e3cf44STejun Heo struct pool_workqueue *pwq; 31101da177e4SLinus Torvalds 311173f53c4aSTejun Heo if (flush_color >= 0) { 31126183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3113112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3114dc186ad7SThomas Gleixner } 311514441960SOleg Nesterov 311649e3cf44STejun Heo for_each_pwq(pwq, wq) { 3117112202d9STejun Heo struct worker_pool *pool = pwq->pool; 31181da177e4SLinus Torvalds 3119a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 312073f53c4aSTejun Heo 312173f53c4aSTejun Heo if (flush_color >= 0) { 31226183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 312373f53c4aSTejun Heo 3124112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3125112202d9STejun Heo pwq->flush_color = flush_color; 3126112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 312773f53c4aSTejun Heo wait = true; 31281da177e4SLinus Torvalds } 312973f53c4aSTejun Heo } 313073f53c4aSTejun Heo 313173f53c4aSTejun Heo if (work_color >= 0) { 31326183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3133112202d9STejun Heo pwq->work_color = work_color; 313473f53c4aSTejun Heo } 313573f53c4aSTejun Heo 3136a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 31371da177e4SLinus Torvalds } 31381da177e4SLinus Torvalds 3139112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 314073f53c4aSTejun Heo complete(&wq->first_flusher->done); 314173f53c4aSTejun Heo 314273f53c4aSTejun Heo return wait; 314383c22520SOleg Nesterov } 31441da177e4SLinus Torvalds 31450fcb78c2SRolf Eike Beer /** 3146c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 31470fcb78c2SRolf Eike Beer * @wq: workqueue to flush 31481da177e4SLinus Torvalds * 3149c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3150c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 31511da177e4SLinus Torvalds */ 3152c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 31531da177e4SLinus Torvalds { 315473f53c4aSTejun Heo struct wq_flusher this_flusher = { 315573f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 315673f53c4aSTejun Heo .flush_color = -1, 3157fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 315873f53c4aSTejun Heo }; 315973f53c4aSTejun Heo int next_color; 3160b1f4ec17SOleg Nesterov 31613347fa09STejun Heo if (WARN_ON(!wq_online)) 31623347fa09STejun Heo return; 31633347fa09STejun Heo 316487915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 316587915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 316687915adcSJohannes Berg 31673c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 316873f53c4aSTejun Heo 316973f53c4aSTejun Heo /* 317073f53c4aSTejun Heo * Start-to-wait phase 317173f53c4aSTejun Heo */ 317273f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 317373f53c4aSTejun Heo 317473f53c4aSTejun Heo if (next_color != wq->flush_color) { 317573f53c4aSTejun Heo /* 317673f53c4aSTejun Heo * Color space is not full. The current work_color 317773f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 317873f53c4aSTejun Heo * by one. 317973f53c4aSTejun Heo */ 31806183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 318173f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 318273f53c4aSTejun Heo wq->work_color = next_color; 318373f53c4aSTejun Heo 318473f53c4aSTejun Heo if (!wq->first_flusher) { 318573f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 31866183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 318773f53c4aSTejun Heo 318873f53c4aSTejun Heo wq->first_flusher = &this_flusher; 318973f53c4aSTejun Heo 3190112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 319173f53c4aSTejun Heo wq->work_color)) { 319273f53c4aSTejun Heo /* nothing to flush, done */ 319373f53c4aSTejun Heo wq->flush_color = next_color; 319473f53c4aSTejun Heo wq->first_flusher = NULL; 319573f53c4aSTejun Heo goto out_unlock; 319673f53c4aSTejun Heo } 319773f53c4aSTejun Heo } else { 319873f53c4aSTejun Heo /* wait in queue */ 31996183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 320073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3201112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 320273f53c4aSTejun Heo } 320373f53c4aSTejun Heo } else { 320473f53c4aSTejun Heo /* 320573f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 320673f53c4aSTejun Heo * The next flush completion will assign us 320773f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 320873f53c4aSTejun Heo */ 320973f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 321073f53c4aSTejun Heo } 321173f53c4aSTejun Heo 3212fca839c0STejun Heo check_flush_dependency(wq, NULL); 3213fca839c0STejun Heo 32143c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 321573f53c4aSTejun Heo 321673f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 321773f53c4aSTejun Heo 321873f53c4aSTejun Heo /* 321973f53c4aSTejun Heo * Wake-up-and-cascade phase 322073f53c4aSTejun Heo * 322173f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 322273f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 322373f53c4aSTejun Heo */ 322400d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 322573f53c4aSTejun Heo return; 322673f53c4aSTejun Heo 32273c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 322873f53c4aSTejun Heo 32294ce48b37STejun Heo /* we might have raced, check again with mutex held */ 32304ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 32314ce48b37STejun Heo goto out_unlock; 32324ce48b37STejun Heo 323300d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 323473f53c4aSTejun Heo 32356183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 32366183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 323773f53c4aSTejun Heo 323873f53c4aSTejun Heo while (true) { 323973f53c4aSTejun Heo struct wq_flusher *next, *tmp; 324073f53c4aSTejun Heo 324173f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 324273f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 324373f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 324473f53c4aSTejun Heo break; 324573f53c4aSTejun Heo list_del_init(&next->list); 324673f53c4aSTejun Heo complete(&next->done); 324773f53c4aSTejun Heo } 324873f53c4aSTejun Heo 32496183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 325073f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 325173f53c4aSTejun Heo 325273f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 325373f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 325473f53c4aSTejun Heo 325573f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 325673f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 325773f53c4aSTejun Heo /* 325873f53c4aSTejun Heo * Assign the same color to all overflowed 325973f53c4aSTejun Heo * flushers, advance work_color and append to 326073f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 326173f53c4aSTejun Heo * phase for these overflowed flushers. 326273f53c4aSTejun Heo */ 326373f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 326473f53c4aSTejun Heo tmp->flush_color = wq->work_color; 326573f53c4aSTejun Heo 326673f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 326773f53c4aSTejun Heo 326873f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 326973f53c4aSTejun Heo &wq->flusher_queue); 3270112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 327173f53c4aSTejun Heo } 327273f53c4aSTejun Heo 327373f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 32746183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 327573f53c4aSTejun Heo break; 327673f53c4aSTejun Heo } 327773f53c4aSTejun Heo 327873f53c4aSTejun Heo /* 327973f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3280112202d9STejun Heo * the new first flusher and arm pwqs. 328173f53c4aSTejun Heo */ 32826183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 32836183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 328473f53c4aSTejun Heo 328573f53c4aSTejun Heo list_del_init(&next->list); 328673f53c4aSTejun Heo wq->first_flusher = next; 328773f53c4aSTejun Heo 3288112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 328973f53c4aSTejun Heo break; 329073f53c4aSTejun Heo 329173f53c4aSTejun Heo /* 329273f53c4aSTejun Heo * Meh... this color is already done, clear first 329373f53c4aSTejun Heo * flusher and repeat cascading. 329473f53c4aSTejun Heo */ 329573f53c4aSTejun Heo wq->first_flusher = NULL; 329673f53c4aSTejun Heo } 329773f53c4aSTejun Heo 329873f53c4aSTejun Heo out_unlock: 32993c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 33001da177e4SLinus Torvalds } 3301c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 33021da177e4SLinus Torvalds 33039c5a2ba7STejun Heo /** 33049c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 33059c5a2ba7STejun Heo * @wq: workqueue to drain 33069c5a2ba7STejun Heo * 33079c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 33089c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 33099c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3310b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 33119c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 33129c5a2ba7STejun Heo * takes too long. 33139c5a2ba7STejun Heo */ 33149c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 33159c5a2ba7STejun Heo { 33169c5a2ba7STejun Heo unsigned int flush_cnt = 0; 331749e3cf44STejun Heo struct pool_workqueue *pwq; 33189c5a2ba7STejun Heo 33199c5a2ba7STejun Heo /* 33209c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 33219c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3322618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 33239c5a2ba7STejun Heo */ 332487fc741eSLai Jiangshan mutex_lock(&wq->mutex); 33259c5a2ba7STejun Heo if (!wq->nr_drainers++) 3326618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 332787fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 33289c5a2ba7STejun Heo reflush: 3329c4f135d6STetsuo Handa __flush_workqueue(wq); 33309c5a2ba7STejun Heo 3331b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 333276af4d93STejun Heo 333349e3cf44STejun Heo for_each_pwq(pwq, wq) { 3334fa2563e4SThomas Tuttle bool drained; 33359c5a2ba7STejun Heo 3336a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3337*afa87ce8STejun Heo drained = pwq_is_empty(pwq); 3338a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3339fa2563e4SThomas Tuttle 3340fa2563e4SThomas Tuttle if (drained) 33419c5a2ba7STejun Heo continue; 33429c5a2ba7STejun Heo 33439c5a2ba7STejun Heo if (++flush_cnt == 10 || 33449c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3345e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3346e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 334776af4d93STejun Heo 3348b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 33499c5a2ba7STejun Heo goto reflush; 33509c5a2ba7STejun Heo } 33519c5a2ba7STejun Heo 33529c5a2ba7STejun Heo if (!--wq->nr_drainers) 3353618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 335487fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 33559c5a2ba7STejun Heo } 33569c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 33579c5a2ba7STejun Heo 3358d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3359d6e89786SJohannes Berg bool from_cancel) 3360baf59022STejun Heo { 3361baf59022STejun Heo struct worker *worker = NULL; 3362c9e7cf27STejun Heo struct worker_pool *pool; 3363112202d9STejun Heo struct pool_workqueue *pwq; 3364baf59022STejun Heo 3365baf59022STejun Heo might_sleep(); 3366baf59022STejun Heo 336724acfb71SThomas Gleixner rcu_read_lock(); 3368fa1b54e6STejun Heo pool = get_work_pool(work); 3369fa1b54e6STejun Heo if (!pool) { 337024acfb71SThomas Gleixner rcu_read_unlock(); 3371fa1b54e6STejun Heo return false; 3372fa1b54e6STejun Heo } 3373fa1b54e6STejun Heo 3374a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 33750b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3376112202d9STejun Heo pwq = get_work_pwq(work); 3377112202d9STejun Heo if (pwq) { 3378112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3379baf59022STejun Heo goto already_gone; 3380606a5020STejun Heo } else { 3381c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3382baf59022STejun Heo if (!worker) 3383baf59022STejun Heo goto already_gone; 3384112202d9STejun Heo pwq = worker->current_pwq; 3385606a5020STejun Heo } 3386baf59022STejun Heo 3387fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3388fca839c0STejun Heo 3389112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3390a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3391baf59022STejun Heo 3392e159489bSTejun Heo /* 3393a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3394a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3395a1d14934SPeter Zijlstra * 3396a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3397a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3398a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3399a1d14934SPeter Zijlstra * forward progress. 3400e159489bSTejun Heo */ 3401d6e89786SJohannes Berg if (!from_cancel && 3402d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3403112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3404112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3405a1d14934SPeter Zijlstra } 340624acfb71SThomas Gleixner rcu_read_unlock(); 3407baf59022STejun Heo return true; 3408baf59022STejun Heo already_gone: 3409a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 341024acfb71SThomas Gleixner rcu_read_unlock(); 3411baf59022STejun Heo return false; 3412baf59022STejun Heo } 3413baf59022STejun Heo 3414d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3415d6e89786SJohannes Berg { 3416d6e89786SJohannes Berg struct wq_barrier barr; 3417d6e89786SJohannes Berg 3418d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3419d6e89786SJohannes Berg return false; 3420d6e89786SJohannes Berg 34214d43d395STetsuo Handa if (WARN_ON(!work->func)) 34224d43d395STetsuo Handa return false; 34234d43d395STetsuo Handa 342487915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 342587915adcSJohannes Berg lock_map_release(&work->lockdep_map); 342687915adcSJohannes Berg 3427d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3428d6e89786SJohannes Berg wait_for_completion(&barr.done); 3429d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3430d6e89786SJohannes Berg return true; 3431d6e89786SJohannes Berg } else { 3432d6e89786SJohannes Berg return false; 3433d6e89786SJohannes Berg } 3434d6e89786SJohannes Berg } 3435d6e89786SJohannes Berg 3436db700897SOleg Nesterov /** 3437401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3438401a8d04STejun Heo * @work: the work to flush 3439db700897SOleg Nesterov * 3440606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3441606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3442401a8d04STejun Heo * 3443d185af30SYacine Belkadi * Return: 3444401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3445401a8d04STejun Heo * %false if it was already idle. 3446db700897SOleg Nesterov */ 3447401a8d04STejun Heo bool flush_work(struct work_struct *work) 3448db700897SOleg Nesterov { 3449d6e89786SJohannes Berg return __flush_work(work, false); 3450606a5020STejun Heo } 3451db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3452db700897SOleg Nesterov 34538603e1b3STejun Heo struct cwt_wait { 3454ac6424b9SIngo Molnar wait_queue_entry_t wait; 34558603e1b3STejun Heo struct work_struct *work; 34568603e1b3STejun Heo }; 34578603e1b3STejun Heo 3458ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 34598603e1b3STejun Heo { 34608603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 34618603e1b3STejun Heo 34628603e1b3STejun Heo if (cwait->work != key) 34638603e1b3STejun Heo return 0; 34648603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 34658603e1b3STejun Heo } 34668603e1b3STejun Heo 346736e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3468401a8d04STejun Heo { 34698603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3470bbb68dfaSTejun Heo unsigned long flags; 34711f1f642eSOleg Nesterov int ret; 34721f1f642eSOleg Nesterov 34731f1f642eSOleg Nesterov do { 3474bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3475bbb68dfaSTejun Heo /* 34768603e1b3STejun Heo * If someone else is already canceling, wait for it to 34778603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 34788603e1b3STejun Heo * because we may get scheduled between @work's completion 34798603e1b3STejun Heo * and the other canceling task resuming and clearing 34808603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 34818603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 34828603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 34838603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 34848603e1b3STejun Heo * we're hogging the CPU. 34858603e1b3STejun Heo * 34868603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 34878603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 34888603e1b3STejun Heo * wake function which matches @work along with exclusive 34898603e1b3STejun Heo * wait and wakeup. 3490bbb68dfaSTejun Heo */ 34918603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 34928603e1b3STejun Heo struct cwt_wait cwait; 34938603e1b3STejun Heo 34948603e1b3STejun Heo init_wait(&cwait.wait); 34958603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 34968603e1b3STejun Heo cwait.work = work; 34978603e1b3STejun Heo 34988603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 34998603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 35008603e1b3STejun Heo if (work_is_canceling(work)) 35018603e1b3STejun Heo schedule(); 35028603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 35038603e1b3STejun Heo } 35041f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 35051f1f642eSOleg Nesterov 3506bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3507bbb68dfaSTejun Heo mark_work_canceling(work); 3508bbb68dfaSTejun Heo local_irq_restore(flags); 3509bbb68dfaSTejun Heo 35103347fa09STejun Heo /* 35113347fa09STejun Heo * This allows canceling during early boot. We know that @work 35123347fa09STejun Heo * isn't executing. 35133347fa09STejun Heo */ 35143347fa09STejun Heo if (wq_online) 3515d6e89786SJohannes Berg __flush_work(work, true); 35163347fa09STejun Heo 35177a22ad75STejun Heo clear_work_data(work); 35188603e1b3STejun Heo 35198603e1b3STejun Heo /* 35208603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 35218603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 35228603e1b3STejun Heo * visible there. 35238603e1b3STejun Heo */ 35248603e1b3STejun Heo smp_mb(); 35258603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 35268603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 35278603e1b3STejun Heo 35281f1f642eSOleg Nesterov return ret; 35291f1f642eSOleg Nesterov } 35301f1f642eSOleg Nesterov 35316e84d644SOleg Nesterov /** 3532401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3533401a8d04STejun Heo * @work: the work to cancel 35346e84d644SOleg Nesterov * 3535401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3536401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3537401a8d04STejun Heo * another workqueue. On return from this function, @work is 3538401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 35391f1f642eSOleg Nesterov * 3540401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3541401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 35426e84d644SOleg Nesterov * 3543401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 35446e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3545401a8d04STejun Heo * 3546d185af30SYacine Belkadi * Return: 3547401a8d04STejun Heo * %true if @work was pending, %false otherwise. 35486e84d644SOleg Nesterov */ 3549401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 35506e84d644SOleg Nesterov { 355136e227d2STejun Heo return __cancel_work_timer(work, false); 3552b89deed3SOleg Nesterov } 355328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3554b89deed3SOleg Nesterov 35556e84d644SOleg Nesterov /** 3556401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3557401a8d04STejun Heo * @dwork: the delayed work to flush 35586e84d644SOleg Nesterov * 3559401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3560401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3561401a8d04STejun Heo * considers the last queueing instance of @dwork. 35621f1f642eSOleg Nesterov * 3563d185af30SYacine Belkadi * Return: 3564401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3565401a8d04STejun Heo * %false if it was already idle. 35666e84d644SOleg Nesterov */ 3567401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3568401a8d04STejun Heo { 35698930cabaSTejun Heo local_irq_disable(); 3570401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 357160c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 35728930cabaSTejun Heo local_irq_enable(); 3573401a8d04STejun Heo return flush_work(&dwork->work); 3574401a8d04STejun Heo } 3575401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3576401a8d04STejun Heo 357705f0fe6bSTejun Heo /** 357805f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 357905f0fe6bSTejun Heo * @rwork: the rcu work to flush 358005f0fe6bSTejun Heo * 358105f0fe6bSTejun Heo * Return: 358205f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 358305f0fe6bSTejun Heo * %false if it was already idle. 358405f0fe6bSTejun Heo */ 358505f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 358605f0fe6bSTejun Heo { 358705f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 358805f0fe6bSTejun Heo rcu_barrier(); 358905f0fe6bSTejun Heo flush_work(&rwork->work); 359005f0fe6bSTejun Heo return true; 359105f0fe6bSTejun Heo } else { 359205f0fe6bSTejun Heo return flush_work(&rwork->work); 359305f0fe6bSTejun Heo } 359405f0fe6bSTejun Heo } 359505f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 359605f0fe6bSTejun Heo 3597f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3598f72b8792SJens Axboe { 3599f72b8792SJens Axboe unsigned long flags; 3600f72b8792SJens Axboe int ret; 3601f72b8792SJens Axboe 3602f72b8792SJens Axboe do { 3603f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3604f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3605f72b8792SJens Axboe 3606f72b8792SJens Axboe if (unlikely(ret < 0)) 3607f72b8792SJens Axboe return false; 3608f72b8792SJens Axboe 3609f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3610f72b8792SJens Axboe local_irq_restore(flags); 3611f72b8792SJens Axboe return ret; 3612f72b8792SJens Axboe } 3613f72b8792SJens Axboe 361473b4b532SAndrey Grodzovsky /* 361573b4b532SAndrey Grodzovsky * See cancel_delayed_work() 361673b4b532SAndrey Grodzovsky */ 361773b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 361873b4b532SAndrey Grodzovsky { 361973b4b532SAndrey Grodzovsky return __cancel_work(work, false); 362073b4b532SAndrey Grodzovsky } 362173b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 362273b4b532SAndrey Grodzovsky 3623401a8d04STejun Heo /** 362457b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 362557b30ae7STejun Heo * @dwork: delayed_work to cancel 362609383498STejun Heo * 3627d185af30SYacine Belkadi * Kill off a pending delayed_work. 3628d185af30SYacine Belkadi * 3629d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3630d185af30SYacine Belkadi * pending. 3631d185af30SYacine Belkadi * 3632d185af30SYacine Belkadi * Note: 3633d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3634d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3635d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 363609383498STejun Heo * 363757b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 363809383498STejun Heo */ 363957b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 364009383498STejun Heo { 3641f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 364209383498STejun Heo } 364357b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 364409383498STejun Heo 364509383498STejun Heo /** 3646401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3647401a8d04STejun Heo * @dwork: the delayed work cancel 3648401a8d04STejun Heo * 3649401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3650401a8d04STejun Heo * 3651d185af30SYacine Belkadi * Return: 3652401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3653401a8d04STejun Heo */ 3654401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 36556e84d644SOleg Nesterov { 365636e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 36576e84d644SOleg Nesterov } 3658f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 36591da177e4SLinus Torvalds 36600fcb78c2SRolf Eike Beer /** 366131ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3662b6136773SAndrew Morton * @func: the function to call 3663b6136773SAndrew Morton * 366431ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 366531ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3666b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 366731ddd871STejun Heo * 3668d185af30SYacine Belkadi * Return: 366931ddd871STejun Heo * 0 on success, -errno on failure. 3670b6136773SAndrew Morton */ 367165f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 367215316ba8SChristoph Lameter { 367315316ba8SChristoph Lameter int cpu; 367438f51568SNamhyung Kim struct work_struct __percpu *works; 367515316ba8SChristoph Lameter 3676b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3677b6136773SAndrew Morton if (!works) 367815316ba8SChristoph Lameter return -ENOMEM; 3679b6136773SAndrew Morton 3680ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 368193981800STejun Heo 368215316ba8SChristoph Lameter for_each_online_cpu(cpu) { 36839bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 36849bfb1839SIngo Molnar 36859bfb1839SIngo Molnar INIT_WORK(work, func); 36868de6d308SOleg Nesterov schedule_work_on(cpu, work); 368715316ba8SChristoph Lameter } 368893981800STejun Heo 368993981800STejun Heo for_each_online_cpu(cpu) 36908616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 369193981800STejun Heo 3692ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3693b6136773SAndrew Morton free_percpu(works); 369415316ba8SChristoph Lameter return 0; 369515316ba8SChristoph Lameter } 369615316ba8SChristoph Lameter 3697eef6a7d5SAlan Stern /** 36981fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 36991fa44ecaSJames Bottomley * @fn: the function to execute 37001fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 37011fa44ecaSJames Bottomley * be available when the work executes) 37021fa44ecaSJames Bottomley * 37031fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 37041fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 37051fa44ecaSJames Bottomley * 3706d185af30SYacine Belkadi * Return: 0 - function was executed 37071fa44ecaSJames Bottomley * 1 - function was scheduled for execution 37081fa44ecaSJames Bottomley */ 370965f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 37101fa44ecaSJames Bottomley { 37111fa44ecaSJames Bottomley if (!in_interrupt()) { 371265f27f38SDavid Howells fn(&ew->work); 37131fa44ecaSJames Bottomley return 0; 37141fa44ecaSJames Bottomley } 37151fa44ecaSJames Bottomley 371665f27f38SDavid Howells INIT_WORK(&ew->work, fn); 37171fa44ecaSJames Bottomley schedule_work(&ew->work); 37181fa44ecaSJames Bottomley 37191fa44ecaSJames Bottomley return 1; 37201fa44ecaSJames Bottomley } 37211fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 37221fa44ecaSJames Bottomley 37237a4e344cSTejun Heo /** 37247a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 37257a4e344cSTejun Heo * @attrs: workqueue_attrs to free 37267a4e344cSTejun Heo * 37277a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 37287a4e344cSTejun Heo */ 3729513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 37307a4e344cSTejun Heo { 37317a4e344cSTejun Heo if (attrs) { 37327a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 37339546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 37347a4e344cSTejun Heo kfree(attrs); 37357a4e344cSTejun Heo } 37367a4e344cSTejun Heo } 37377a4e344cSTejun Heo 37387a4e344cSTejun Heo /** 37397a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 37407a4e344cSTejun Heo * 37417a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3742d185af30SYacine Belkadi * return it. 3743d185af30SYacine Belkadi * 3744d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 37457a4e344cSTejun Heo */ 3746513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 37477a4e344cSTejun Heo { 37487a4e344cSTejun Heo struct workqueue_attrs *attrs; 37497a4e344cSTejun Heo 3750be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 37517a4e344cSTejun Heo if (!attrs) 37527a4e344cSTejun Heo goto fail; 3753be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 37547a4e344cSTejun Heo goto fail; 37559546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 37569546b29eSTejun Heo goto fail; 37577a4e344cSTejun Heo 375813e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 3759523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 37607a4e344cSTejun Heo return attrs; 37617a4e344cSTejun Heo fail: 37627a4e344cSTejun Heo free_workqueue_attrs(attrs); 37637a4e344cSTejun Heo return NULL; 37647a4e344cSTejun Heo } 37657a4e344cSTejun Heo 376629c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 376729c91e99STejun Heo const struct workqueue_attrs *from) 376829c91e99STejun Heo { 376929c91e99STejun Heo to->nice = from->nice; 377029c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 37719546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 37728639ecebSTejun Heo to->affn_strict = from->affn_strict; 377384193c07STejun Heo 37742865a8fbSShaohua Li /* 377584193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 377684193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 377784193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 37782865a8fbSShaohua Li */ 377984193c07STejun Heo to->affn_scope = from->affn_scope; 3780af73f5c9STejun Heo to->ordered = from->ordered; 378129c91e99STejun Heo } 378229c91e99STejun Heo 37835de7a03cSTejun Heo /* 37845de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 37855de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 37865de7a03cSTejun Heo */ 37875de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 37885de7a03cSTejun Heo { 378984193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 37905de7a03cSTejun Heo attrs->ordered = false; 37915de7a03cSTejun Heo } 37925de7a03cSTejun Heo 379329c91e99STejun Heo /* hash value of the content of @attr */ 379429c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 379529c91e99STejun Heo { 379629c91e99STejun Heo u32 hash = 0; 379729c91e99STejun Heo 379829c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 379913e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 380013e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 38019546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 38029546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 38038639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 380429c91e99STejun Heo return hash; 380529c91e99STejun Heo } 380629c91e99STejun Heo 380729c91e99STejun Heo /* content equality test */ 380829c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 380929c91e99STejun Heo const struct workqueue_attrs *b) 381029c91e99STejun Heo { 381129c91e99STejun Heo if (a->nice != b->nice) 381229c91e99STejun Heo return false; 381329c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 381429c91e99STejun Heo return false; 38159546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 38169546b29eSTejun Heo return false; 38178639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 38188639ecebSTejun Heo return false; 381929c91e99STejun Heo return true; 382029c91e99STejun Heo } 382129c91e99STejun Heo 38220f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 38230f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 38240f36ee24STejun Heo const cpumask_t *unbound_cpumask) 38250f36ee24STejun Heo { 38260f36ee24STejun Heo /* 38270f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 38280f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 38290f36ee24STejun Heo * @unbound_cpumask. 38300f36ee24STejun Heo */ 38310f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 38320f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 38330f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 38340f36ee24STejun Heo } 38350f36ee24STejun Heo 383684193c07STejun Heo /* find wq_pod_type to use for @attrs */ 383784193c07STejun Heo static const struct wq_pod_type * 383884193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 383984193c07STejun Heo { 3840523a301eSTejun Heo enum wq_affn_scope scope; 3841523a301eSTejun Heo struct wq_pod_type *pt; 3842523a301eSTejun Heo 3843523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 3844523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3845523a301eSTejun Heo 3846523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 3847523a301eSTejun Heo scope = wq_affn_dfl; 3848523a301eSTejun Heo else 3849523a301eSTejun Heo scope = attrs->affn_scope; 3850523a301eSTejun Heo 3851523a301eSTejun Heo pt = &wq_pod_types[scope]; 385284193c07STejun Heo 385384193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 385484193c07STejun Heo likely(pt->nr_pods)) 385584193c07STejun Heo return pt; 385684193c07STejun Heo 385784193c07STejun Heo /* 385884193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 385984193c07STejun Heo * initialized in workqueue_init_early(). 386084193c07STejun Heo */ 386184193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 386284193c07STejun Heo BUG_ON(!pt->nr_pods); 386384193c07STejun Heo return pt; 386484193c07STejun Heo } 386584193c07STejun Heo 38667a4e344cSTejun Heo /** 38677a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 38687a4e344cSTejun Heo * @pool: worker_pool to initialize 38697a4e344cSTejun Heo * 3870402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3871d185af30SYacine Belkadi * 3872d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 387329c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 387429c91e99STejun Heo * on @pool safely to release it. 38757a4e344cSTejun Heo */ 38767a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 38774e1a1f9aSTejun Heo { 3878a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 387929c91e99STejun Heo pool->id = -1; 388029c91e99STejun Heo pool->cpu = -1; 3881f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 38824e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 388382607adcSTejun Heo pool->watchdog_ts = jiffies; 38844e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 38854e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 38864e1a1f9aSTejun Heo hash_init(pool->busy_hash); 38874e1a1f9aSTejun Heo 388832a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 38893f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 38904e1a1f9aSTejun Heo 389132a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 38924e1a1f9aSTejun Heo 3893da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 3894e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 38957a4e344cSTejun Heo 38967cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 389729c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 389829c91e99STejun Heo pool->refcnt = 1; 389929c91e99STejun Heo 390029c91e99STejun Heo /* shouldn't fail above this point */ 3901be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 39027a4e344cSTejun Heo if (!pool->attrs) 39037a4e344cSTejun Heo return -ENOMEM; 39045de7a03cSTejun Heo 39055de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 39065de7a03cSTejun Heo 39077a4e344cSTejun Heo return 0; 39084e1a1f9aSTejun Heo } 39094e1a1f9aSTejun Heo 3910669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3911669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3912669de8bdSBart Van Assche { 3913669de8bdSBart Van Assche char *lock_name; 3914669de8bdSBart Van Assche 3915669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3916669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3917669de8bdSBart Van Assche if (!lock_name) 3918669de8bdSBart Van Assche lock_name = wq->name; 391969a106c0SQian Cai 392069a106c0SQian Cai wq->lock_name = lock_name; 3921669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3922669de8bdSBart Van Assche } 3923669de8bdSBart Van Assche 3924669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3925669de8bdSBart Van Assche { 3926669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3927669de8bdSBart Van Assche } 3928669de8bdSBart Van Assche 3929669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3930669de8bdSBart Van Assche { 3931669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3932669de8bdSBart Van Assche kfree(wq->lock_name); 3933669de8bdSBart Van Assche } 3934669de8bdSBart Van Assche #else 3935669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3936669de8bdSBart Van Assche { 3937669de8bdSBart Van Assche } 3938669de8bdSBart Van Assche 3939669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3940669de8bdSBart Van Assche { 3941669de8bdSBart Van Assche } 3942669de8bdSBart Van Assche 3943669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3944669de8bdSBart Van Assche { 3945669de8bdSBart Van Assche } 3946669de8bdSBart Van Assche #endif 3947669de8bdSBart Van Assche 3948e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3949e2dca7adSTejun Heo { 3950e2dca7adSTejun Heo struct workqueue_struct *wq = 3951e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3952e2dca7adSTejun Heo 3953669de8bdSBart Van Assche wq_free_lockdep(wq); 3954ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 3955e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3956e2dca7adSTejun Heo kfree(wq); 3957e2dca7adSTejun Heo } 3958e2dca7adSTejun Heo 395929c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 396029c91e99STejun Heo { 396129c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 396229c91e99STejun Heo 39637cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 396429c91e99STejun Heo free_workqueue_attrs(pool->attrs); 396529c91e99STejun Heo kfree(pool); 396629c91e99STejun Heo } 396729c91e99STejun Heo 396829c91e99STejun Heo /** 396929c91e99STejun Heo * put_unbound_pool - put a worker_pool 397029c91e99STejun Heo * @pool: worker_pool to put 397129c91e99STejun Heo * 397224acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3973c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3974c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3975c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3976a892caccSTejun Heo * 3977a892caccSTejun Heo * Should be called with wq_pool_mutex held. 397829c91e99STejun Heo */ 397929c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 398029c91e99STejun Heo { 398160f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 398229c91e99STejun Heo struct worker *worker; 39839680540cSYang Yingliang LIST_HEAD(cull_list); 3984e02b9312SValentin Schneider 3985a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3986a892caccSTejun Heo 3987a892caccSTejun Heo if (--pool->refcnt) 398829c91e99STejun Heo return; 398929c91e99STejun Heo 399029c91e99STejun Heo /* sanity checks */ 399161d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3992a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 399329c91e99STejun Heo return; 399429c91e99STejun Heo 399529c91e99STejun Heo /* release id and unhash */ 399629c91e99STejun Heo if (pool->id >= 0) 399729c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 399829c91e99STejun Heo hash_del(&pool->hash_node); 399929c91e99STejun Heo 4000c5aa87bbSTejun Heo /* 4001692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4002692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4003692b4825STejun Heo * manager and @pool gets freed with the flag set. 40049ab03be4SValentin Schneider * 40059ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 40069ab03be4SValentin Schneider * only get here with 40079ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 40089ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 40099ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 40109ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 40119ab03be4SValentin Schneider * drops pool->lock 4012c5aa87bbSTejun Heo */ 40139ab03be4SValentin Schneider while (true) { 40149ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 40159ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4016d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4017e02b9312SValentin Schneider 4018e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 40199ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 40209ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4021692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 40229ab03be4SValentin Schneider break; 40239ab03be4SValentin Schneider } 40249ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4025e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 40269ab03be4SValentin Schneider } 4027692b4825STejun Heo 40281037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4029e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 403029c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4031a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 403260f5a4bcSLai Jiangshan 4033e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4034e02b9312SValentin Schneider 4035e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 403660f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 40371258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 403860f5a4bcSLai Jiangshan 403960f5a4bcSLai Jiangshan if (pool->detach_completion) 404060f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 404160f5a4bcSLai Jiangshan 404229c91e99STejun Heo /* shut down the timers */ 404329c91e99STejun Heo del_timer_sync(&pool->idle_timer); 40443f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 404529c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 404629c91e99STejun Heo 404724acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 404825b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 404929c91e99STejun Heo } 405029c91e99STejun Heo 405129c91e99STejun Heo /** 405229c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 405329c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 405429c91e99STejun Heo * 405529c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 405629c91e99STejun Heo * reference count and return it. If there already is a matching 405729c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4058d185af30SYacine Belkadi * create a new one. 4059a892caccSTejun Heo * 4060a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4061d185af30SYacine Belkadi * 4062d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4063d185af30SYacine Belkadi * On failure, %NULL. 406429c91e99STejun Heo */ 406529c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 406629c91e99STejun Heo { 406784193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 406829c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 406929c91e99STejun Heo struct worker_pool *pool; 407084193c07STejun Heo int pod, node = NUMA_NO_NODE; 407129c91e99STejun Heo 4072a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 407329c91e99STejun Heo 407429c91e99STejun Heo /* do we already have a matching pool? */ 407529c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 407629c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 407729c91e99STejun Heo pool->refcnt++; 40783fb1823cSLai Jiangshan return pool; 407929c91e99STejun Heo } 408029c91e99STejun Heo } 408129c91e99STejun Heo 40829546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 408384193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 40849546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 408584193c07STejun Heo node = pt->pod_node[pod]; 4086e2273584SXunlei Pang break; 4087e2273584SXunlei Pang } 4088e2273584SXunlei Pang } 4089e2273584SXunlei Pang 409029c91e99STejun Heo /* nope, create a new one */ 409184193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 409229c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 409329c91e99STejun Heo goto fail; 409429c91e99STejun Heo 409584193c07STejun Heo pool->node = node; 40965de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 40975de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 40982865a8fbSShaohua Li 409929c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 410029c91e99STejun Heo goto fail; 410129c91e99STejun Heo 410229c91e99STejun Heo /* create and start the initial worker */ 41033347fa09STejun Heo if (wq_online && !create_worker(pool)) 410429c91e99STejun Heo goto fail; 410529c91e99STejun Heo 410629c91e99STejun Heo /* install */ 410729c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 41083fb1823cSLai Jiangshan 410929c91e99STejun Heo return pool; 411029c91e99STejun Heo fail: 411129c91e99STejun Heo if (pool) 411229c91e99STejun Heo put_unbound_pool(pool); 411329c91e99STejun Heo return NULL; 411429c91e99STejun Heo } 411529c91e99STejun Heo 41168864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 41178864b4e5STejun Heo { 41188864b4e5STejun Heo kmem_cache_free(pwq_cache, 41198864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 41208864b4e5STejun Heo } 41218864b4e5STejun Heo 41228864b4e5STejun Heo /* 4123967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4124967b494eSTejun Heo * refcnt and needs to be destroyed. 41258864b4e5STejun Heo */ 4126687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 41278864b4e5STejun Heo { 41288864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4129687a9aa5STejun Heo release_work); 41308864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 41318864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4132b42b0bddSYang Yingliang bool is_last = false; 41338864b4e5STejun Heo 4134b42b0bddSYang Yingliang /* 4135687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4136b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4137b42b0bddSYang Yingliang */ 4138b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 41393c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 41408864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4141bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 41423c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4143b42b0bddSYang Yingliang } 41448864b4e5STejun Heo 4145687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4146a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 41478864b4e5STejun Heo put_unbound_pool(pool); 4148a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4149687a9aa5STejun Heo } 4150a892caccSTejun Heo 415125b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 41528864b4e5STejun Heo 41538864b4e5STejun Heo /* 41548864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4155e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 41568864b4e5STejun Heo */ 4157669de8bdSBart Van Assche if (is_last) { 4158669de8bdSBart Van Assche wq_unregister_lockdep(wq); 415925b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 41606029a918STejun Heo } 4161669de8bdSBart Van Assche } 41628864b4e5STejun Heo 416367dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4164f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4165f147f29eSTejun Heo struct worker_pool *pool) 4166d2c1d404STejun Heo { 4167d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4168d2c1d404STejun Heo 4169e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4170e50aba9aSTejun Heo 4171d2c1d404STejun Heo pwq->pool = pool; 4172d2c1d404STejun Heo pwq->wq = wq; 4173d2c1d404STejun Heo pwq->flush_color = -1; 41748864b4e5STejun Heo pwq->refcnt = 1; 4175f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 41761befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4177d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4178687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4179f147f29eSTejun Heo } 4180d2c1d404STejun Heo 4181f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 41821befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4183f147f29eSTejun Heo { 4184f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4185f147f29eSTejun Heo 4186f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 418775ccf595STejun Heo 41881befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 41891befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 41901befcf30STejun Heo return; 41911befcf30STejun Heo 419229b1cb41SLai Jiangshan /* set the matching work_color */ 419375ccf595STejun Heo pwq->work_color = wq->work_color; 4194983ca25eSTejun Heo 4195983ca25eSTejun Heo /* link in @pwq */ 41969e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4197df2d5ae4STejun Heo } 41986029a918STejun Heo 4199f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4200f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4201f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4202f147f29eSTejun Heo { 4203f147f29eSTejun Heo struct worker_pool *pool; 4204f147f29eSTejun Heo struct pool_workqueue *pwq; 4205f147f29eSTejun Heo 4206f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4207f147f29eSTejun Heo 4208f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4209f147f29eSTejun Heo if (!pool) 4210f147f29eSTejun Heo return NULL; 4211f147f29eSTejun Heo 4212e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4213f147f29eSTejun Heo if (!pwq) { 4214f147f29eSTejun Heo put_unbound_pool(pool); 4215f147f29eSTejun Heo return NULL; 4216f147f29eSTejun Heo } 4217f147f29eSTejun Heo 4218f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4219f147f29eSTejun Heo return pwq; 4220d2c1d404STejun Heo } 4221d2c1d404STejun Heo 42224c16bd32STejun Heo /** 4223fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4224042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 422584193c07STejun Heo * @cpu: the target CPU 42264c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 42274c16bd32STejun Heo * 4228fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4229fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 42309546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 42314c16bd32STejun Heo * 4232fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4233fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4234fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 42354c16bd32STejun Heo * 4236fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 42374c16bd32STejun Heo */ 42389546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 42399546b29eSTejun Heo int cpu_going_down) 42404c16bd32STejun Heo { 424184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 424284193c07STejun Heo int pod = pt->cpu_pod[cpu]; 42434c16bd32STejun Heo 4244fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 42459546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 42469546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 42474c16bd32STejun Heo if (cpu_going_down >= 0) 42489546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 42494c16bd32STejun Heo 42509546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 42519546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 425284193c07STejun Heo return; 425384193c07STejun Heo } 42544c16bd32STejun Heo 4255fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 42569546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 42571ad0f0a7SMichael Bringmann 42589546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 42591ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 42601ad0f0a7SMichael Bringmann "possible intersect\n"); 42614c16bd32STejun Heo } 42624c16bd32STejun Heo 4263636b927eSTejun Heo /* install @pwq into @wq's cpu_pwq and return the old pwq */ 4264636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4265636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 42661befcf30STejun Heo { 42671befcf30STejun Heo struct pool_workqueue *old_pwq; 42681befcf30STejun Heo 42695b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 42701befcf30STejun Heo lockdep_assert_held(&wq->mutex); 42711befcf30STejun Heo 42721befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 42731befcf30STejun Heo link_pwq(pwq); 42741befcf30STejun Heo 4275636b927eSTejun Heo old_pwq = rcu_access_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4276636b927eSTejun Heo rcu_assign_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu), pwq); 42771befcf30STejun Heo return old_pwq; 42781befcf30STejun Heo } 42791befcf30STejun Heo 42802d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 42812d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 42822d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 42832d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4284042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 42852d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 42862d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 42872d5f0764SLai Jiangshan }; 42882d5f0764SLai Jiangshan 42892d5f0764SLai Jiangshan /* free the resources after success or abort */ 42902d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 42912d5f0764SLai Jiangshan { 42922d5f0764SLai Jiangshan if (ctx) { 4293636b927eSTejun Heo int cpu; 42942d5f0764SLai Jiangshan 4295636b927eSTejun Heo for_each_possible_cpu(cpu) 4296636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 42972d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 42982d5f0764SLai Jiangshan 42992d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 43002d5f0764SLai Jiangshan 43012d5f0764SLai Jiangshan kfree(ctx); 43022d5f0764SLai Jiangshan } 43032d5f0764SLai Jiangshan } 43042d5f0764SLai Jiangshan 43052d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 43062d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 43072d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 430899c621efSLai Jiangshan const struct workqueue_attrs *attrs, 430999c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 43102d5f0764SLai Jiangshan { 43112d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 43129546b29eSTejun Heo struct workqueue_attrs *new_attrs; 4313636b927eSTejun Heo int cpu; 43142d5f0764SLai Jiangshan 43152d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 43162d5f0764SLai Jiangshan 431784193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 431884193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 431984193c07STejun Heo return ERR_PTR(-EINVAL); 432084193c07STejun Heo 4321636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 43222d5f0764SLai Jiangshan 4323be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 43249546b29eSTejun Heo if (!ctx || !new_attrs) 43252d5f0764SLai Jiangshan goto out_free; 43262d5f0764SLai Jiangshan 4327042f7df1SLai Jiangshan /* 43282d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 43292d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 43302d5f0764SLai Jiangshan * it even if we don't use it immediately. 43312d5f0764SLai Jiangshan */ 43320f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 43330f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 43349546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 43352d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 43362d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 43372d5f0764SLai Jiangshan goto out_free; 43382d5f0764SLai Jiangshan 4339636b927eSTejun Heo for_each_possible_cpu(cpu) { 4340af73f5c9STejun Heo if (new_attrs->ordered) { 43412d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4342636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4343636b927eSTejun Heo } else { 43449546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 43459546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 4346636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4347636b927eSTejun Heo goto out_free; 43482d5f0764SLai Jiangshan } 43492d5f0764SLai Jiangshan } 43502d5f0764SLai Jiangshan 4351042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4352042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4353042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 43549546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 43552d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4356042f7df1SLai Jiangshan 43572d5f0764SLai Jiangshan ctx->wq = wq; 43582d5f0764SLai Jiangshan return ctx; 43592d5f0764SLai Jiangshan 43602d5f0764SLai Jiangshan out_free: 43612d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 43622d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 436384193c07STejun Heo return ERR_PTR(-ENOMEM); 43642d5f0764SLai Jiangshan } 43652d5f0764SLai Jiangshan 43662d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 43672d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 43682d5f0764SLai Jiangshan { 4369636b927eSTejun Heo int cpu; 43702d5f0764SLai Jiangshan 43712d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 43722d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 43732d5f0764SLai Jiangshan 43742d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 43752d5f0764SLai Jiangshan 43762d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 4377636b927eSTejun Heo for_each_possible_cpu(cpu) 4378636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4379636b927eSTejun Heo ctx->pwq_tbl[cpu]); 43802d5f0764SLai Jiangshan 43812d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 43822d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 43832d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 43842d5f0764SLai Jiangshan 43852d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 43862d5f0764SLai Jiangshan } 43872d5f0764SLai Jiangshan 4388a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4389a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4390a0111cf6SLai Jiangshan { 4391a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4392a0111cf6SLai Jiangshan 4393a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4394a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4395a0111cf6SLai Jiangshan return -EINVAL; 4396a0111cf6SLai Jiangshan 4397a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 43980a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 43990a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4400a0111cf6SLai Jiangshan return -EINVAL; 4401a0111cf6SLai Jiangshan 44020a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 44030a94efb5STejun Heo } 44040a94efb5STejun Heo 440599c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 440684193c07STejun Heo if (IS_ERR(ctx)) 440784193c07STejun Heo return PTR_ERR(ctx); 4408a0111cf6SLai Jiangshan 4409a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4410a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4411a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4412a0111cf6SLai Jiangshan 44136201171eSwanghaibin return 0; 4414a0111cf6SLai Jiangshan } 4415a0111cf6SLai Jiangshan 44169e8cd2f5STejun Heo /** 44179e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 44189e8cd2f5STejun Heo * @wq: the target workqueue 44199e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 44209e8cd2f5STejun Heo * 4421fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4422fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4423fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4424fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4425fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 44269e8cd2f5STejun Heo * 4427d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4428d185af30SYacine Belkadi * 4429ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4430509b3204SDaniel Jordan * 4431d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 44329e8cd2f5STejun Heo */ 4433513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 44349e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 44359e8cd2f5STejun Heo { 4436a0111cf6SLai Jiangshan int ret; 44379e8cd2f5STejun Heo 4438509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4439509b3204SDaniel Jordan 4440509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4441a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4442509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 44432d5f0764SLai Jiangshan 44442d5f0764SLai Jiangshan return ret; 44459e8cd2f5STejun Heo } 44469e8cd2f5STejun Heo 44474c16bd32STejun Heo /** 4448fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 44494c16bd32STejun Heo * @wq: the target workqueue 44504cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 44514cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 44524c16bd32STejun Heo * @online: whether @cpu is coming up or going down 44534c16bd32STejun Heo * 44544c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4455fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 44564c16bd32STejun Heo * @wq accordingly. 44574c16bd32STejun Heo * 44584c16bd32STejun Heo * 4459fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4460fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4461fef59c9cSTejun Heo * 4462fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4463fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4464fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4465fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4466fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4467fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 44684c16bd32STejun Heo */ 4469fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 44704cbfd3deSTejun Heo int hotplug_cpu, bool online) 44714c16bd32STejun Heo { 44724cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 44734c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 44744c16bd32STejun Heo struct workqueue_attrs *target_attrs; 44754c16bd32STejun Heo 44764c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 44774c16bd32STejun Heo 447884193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 44794c16bd32STejun Heo return; 44804c16bd32STejun Heo 44814c16bd32STejun Heo /* 44824c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 44834c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 44844c16bd32STejun Heo * CPU hotplug exclusion. 44854c16bd32STejun Heo */ 4486fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 44874c16bd32STejun Heo 44884c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 44890f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 44904c16bd32STejun Heo 4491636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 44929546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 4493636b927eSTejun Heo pwq = rcu_dereference_protected(*per_cpu_ptr(wq->cpu_pwq, cpu), 4494636b927eSTejun Heo lockdep_is_held(&wq_pool_mutex)); 44959546b29eSTejun Heo if (wqattrs_equal(target_attrs, pwq->pool->attrs)) 4496f7142ed4SLai Jiangshan return; 44974c16bd32STejun Heo 44984c16bd32STejun Heo /* create a new pwq */ 44994c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 45004c16bd32STejun Heo if (!pwq) { 4501fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 45024c16bd32STejun Heo wq->name); 450377f300b1SDaeseok Youn goto use_dfl_pwq; 45044c16bd32STejun Heo } 45054c16bd32STejun Heo 4506f7142ed4SLai Jiangshan /* Install the new pwq. */ 45074c16bd32STejun Heo mutex_lock(&wq->mutex); 4508636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 45094c16bd32STejun Heo goto out_unlock; 45104c16bd32STejun Heo 45114c16bd32STejun Heo use_dfl_pwq: 4512f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4513a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 45144c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4515a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 4516636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, wq->dfl_pwq); 45174c16bd32STejun Heo out_unlock: 45184c16bd32STejun Heo mutex_unlock(&wq->mutex); 45194c16bd32STejun Heo put_pwq_unlocked(old_pwq); 45204c16bd32STejun Heo } 45214c16bd32STejun Heo 452230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 45231da177e4SLinus Torvalds { 452449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 45258a2b7538STejun Heo int cpu, ret; 4526e1d8aa9fSFrederic Weisbecker 4527687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 4528ee1ceef7STejun Heo if (!wq->cpu_pwq) 4529687a9aa5STejun Heo goto enomem; 453030cdf249STejun Heo 4531636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 453230cdf249STejun Heo for_each_possible_cpu(cpu) { 4533687a9aa5STejun Heo struct pool_workqueue **pwq_p = 4534ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 4535687a9aa5STejun Heo struct worker_pool *pool = 4536687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 453730cdf249STejun Heo 4538687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 4539687a9aa5STejun Heo pool->node); 4540687a9aa5STejun Heo if (!*pwq_p) 4541687a9aa5STejun Heo goto enomem; 4542687a9aa5STejun Heo 4543687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 4544f147f29eSTejun Heo 4545f147f29eSTejun Heo mutex_lock(&wq->mutex); 4546687a9aa5STejun Heo link_pwq(*pwq_p); 4547f147f29eSTejun Heo mutex_unlock(&wq->mutex); 454830cdf249STejun Heo } 454930cdf249STejun Heo return 0; 4550509b3204SDaniel Jordan } 4551509b3204SDaniel Jordan 4552ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4553509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 45548a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 45558a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 45568a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 45578a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 45588a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 45599e8cd2f5STejun Heo } else { 4560509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 45619e8cd2f5STejun Heo } 4562ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4563509b3204SDaniel Jordan 456464344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 456564344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 456664344553SZqiang */ 456764344553SZqiang if (ret) 456864344553SZqiang kthread_flush_worker(pwq_release_worker); 456964344553SZqiang 4570509b3204SDaniel Jordan return ret; 4571687a9aa5STejun Heo 4572687a9aa5STejun Heo enomem: 4573687a9aa5STejun Heo if (wq->cpu_pwq) { 45747b42f401SZqiang for_each_possible_cpu(cpu) { 45757b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 45767b42f401SZqiang 45777b42f401SZqiang if (pwq) 45787b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 45797b42f401SZqiang } 4580687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 4581687a9aa5STejun Heo wq->cpu_pwq = NULL; 4582687a9aa5STejun Heo } 4583687a9aa5STejun Heo return -ENOMEM; 45840f900049STejun Heo } 45850f900049STejun Heo 4586f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4587f3421797STejun Heo const char *name) 4588b71ab8c2STejun Heo { 4589636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 4590044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4591636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 4592b71ab8c2STejun Heo 4593636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 4594b71ab8c2STejun Heo } 4595b71ab8c2STejun Heo 4596983c7515STejun Heo /* 4597983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4598983c7515STejun Heo * to guarantee forward progress. 4599983c7515STejun Heo */ 4600983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4601983c7515STejun Heo { 4602983c7515STejun Heo struct worker *rescuer; 4603b92b36eaSDan Carpenter int ret; 4604983c7515STejun Heo 4605983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4606983c7515STejun Heo return 0; 4607983c7515STejun Heo 4608983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 46094c0736a7SPetr Mladek if (!rescuer) { 46104c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 46114c0736a7SPetr Mladek wq->name); 4612983c7515STejun Heo return -ENOMEM; 46134c0736a7SPetr Mladek } 4614983c7515STejun Heo 4615983c7515STejun Heo rescuer->rescue_wq = wq; 4616b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 4617f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4618b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 46194c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 46204c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4621983c7515STejun Heo kfree(rescuer); 4622b92b36eaSDan Carpenter return ret; 4623983c7515STejun Heo } 4624983c7515STejun Heo 4625983c7515STejun Heo wq->rescuer = rescuer; 462685f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 462785f0ab43SJuri Lelli kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); 462885f0ab43SJuri Lelli else 4629983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4630983c7515STejun Heo wake_up_process(rescuer->task); 4631983c7515STejun Heo 4632983c7515STejun Heo return 0; 4633983c7515STejun Heo } 4634983c7515STejun Heo 4635a045a272STejun Heo /** 4636a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 4637a045a272STejun Heo * @wq: target workqueue 4638a045a272STejun Heo * 4639a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 4640a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 4641a045a272STejun Heo * @wq->max_active to zero. 4642a045a272STejun Heo */ 4643a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 4644a045a272STejun Heo { 4645a045a272STejun Heo struct pool_workqueue *pwq; 4646a045a272STejun Heo 4647a045a272STejun Heo lockdep_assert_held(&wq->mutex); 4648a045a272STejun Heo 4649a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 4650a045a272STejun Heo WRITE_ONCE(wq->max_active, 0); 4651a045a272STejun Heo return; 4652a045a272STejun Heo } 4653a045a272STejun Heo 4654a045a272STejun Heo if (wq->max_active == wq->saved_max_active) 4655a045a272STejun Heo return; 4656a045a272STejun Heo 4657a045a272STejun Heo /* 4658a045a272STejun Heo * Update @wq->max_active and then kick inactive work items if more 4659a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 4660a045a272STejun Heo * because new work items are always queued behind existing inactive 4661a045a272STejun Heo * work items if there are any. 4662a045a272STejun Heo */ 4663a045a272STejun Heo WRITE_ONCE(wq->max_active, wq->saved_max_active); 4664a045a272STejun Heo 4665a045a272STejun Heo for_each_pwq(pwq, wq) { 4666a045a272STejun Heo unsigned long flags; 4667a045a272STejun Heo 4668a045a272STejun Heo /* this function can be called during early boot w/ irq disabled */ 4669a045a272STejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, flags); 4670a045a272STejun Heo 4671a045a272STejun Heo while (!list_empty(&pwq->inactive_works) && 4672a045a272STejun Heo pwq->nr_active < wq->max_active) 4673a045a272STejun Heo pwq_activate_first_inactive(pwq); 4674a045a272STejun Heo 4675a045a272STejun Heo kick_pool(pwq->pool); 4676a045a272STejun Heo 4677a045a272STejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 4678a045a272STejun Heo } 4679a045a272STejun Heo } 4680a045a272STejun Heo 4681a2775bbcSMathieu Malaterre __printf(1, 4) 4682669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 468397e37d7bSTejun Heo unsigned int flags, 4684669de8bdSBart Van Assche int max_active, ...) 46853af24433SOleg Nesterov { 4686ecf6881fSTejun Heo va_list args; 46873af24433SOleg Nesterov struct workqueue_struct *wq; 468831c89007SAudra Mitchell int len; 4689b196be89STejun Heo 46905c0338c6STejun Heo /* 4691fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 4692fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 46935c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 4694fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 46955c0338c6STejun Heo */ 46965c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 46975c0338c6STejun Heo flags |= __WQ_ORDERED; 46985c0338c6STejun Heo 4699cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4700cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4701cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4702cee22a15SViresh Kumar 4703ecf6881fSTejun Heo /* allocate wq and format name */ 4704636b927eSTejun Heo wq = kzalloc(sizeof(*wq), GFP_KERNEL); 4705b196be89STejun Heo if (!wq) 4706d2c1d404STejun Heo return NULL; 4707b196be89STejun Heo 47086029a918STejun Heo if (flags & WQ_UNBOUND) { 4709be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 47106029a918STejun Heo if (!wq->unbound_attrs) 47116029a918STejun Heo goto err_free_wq; 47126029a918STejun Heo } 47136029a918STejun Heo 4714669de8bdSBart Van Assche va_start(args, max_active); 471531c89007SAudra Mitchell len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4716b196be89STejun Heo va_end(args); 47173af24433SOleg Nesterov 471831c89007SAudra Mitchell if (len >= WQ_NAME_LEN) 471931c89007SAudra Mitchell pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", wq->name); 472031c89007SAudra Mitchell 4721d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4722b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 47233af24433SOleg Nesterov 4724b196be89STejun Heo /* init wq */ 472597e37d7bSTejun Heo wq->flags = flags; 4726a045a272STejun Heo wq->max_active = max_active; 4727a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 47283c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4729112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 473030cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 473173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 473273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4733493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 47343af24433SOleg Nesterov 4735669de8bdSBart Van Assche wq_init_lockdep(wq); 4736cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 47373af24433SOleg Nesterov 473830cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 473982efcab3SBart Van Assche goto err_unreg_lockdep; 47401537663fSTejun Heo 474140c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4742d2c1d404STejun Heo goto err_destroy; 4743e22bee78STejun Heo 4744226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4745226223abSTejun Heo goto err_destroy; 4746226223abSTejun Heo 47476af8bf3dSOleg Nesterov /* 474868e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 474968e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 475068e13a67SLai Jiangshan * list. 47516af8bf3dSOleg Nesterov */ 475268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4753a0a1a5fdSTejun Heo 4754a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4755a045a272STejun Heo wq_adjust_max_active(wq); 4756a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4757a0a1a5fdSTejun Heo 4758e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4759a0a1a5fdSTejun Heo 476068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 47613af24433SOleg Nesterov 47623af24433SOleg Nesterov return wq; 4763d2c1d404STejun Heo 476482efcab3SBart Van Assche err_unreg_lockdep: 4765009bb421SBart Van Assche wq_unregister_lockdep(wq); 4766009bb421SBart Van Assche wq_free_lockdep(wq); 476782efcab3SBart Van Assche err_free_wq: 47686029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 47694690c4abSTejun Heo kfree(wq); 4770d2c1d404STejun Heo return NULL; 4771d2c1d404STejun Heo err_destroy: 4772d2c1d404STejun Heo destroy_workqueue(wq); 47734690c4abSTejun Heo return NULL; 47741da177e4SLinus Torvalds } 4775669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 47761da177e4SLinus Torvalds 4777c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4778c29eb853STejun Heo { 4779c29eb853STejun Heo int i; 4780c29eb853STejun Heo 4781c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4782c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4783c29eb853STejun Heo return true; 4784c29eb853STejun Heo 4785c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4786c29eb853STejun Heo return true; 4787*afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 4788c29eb853STejun Heo return true; 4789c29eb853STejun Heo 4790c29eb853STejun Heo return false; 4791c29eb853STejun Heo } 4792c29eb853STejun Heo 47933af24433SOleg Nesterov /** 47943af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 47953af24433SOleg Nesterov * @wq: target workqueue 47963af24433SOleg Nesterov * 47973af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 47983af24433SOleg Nesterov */ 47993af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 48003af24433SOleg Nesterov { 480149e3cf44STejun Heo struct pool_workqueue *pwq; 4802636b927eSTejun Heo int cpu; 48033af24433SOleg Nesterov 4804def98c84STejun Heo /* 4805def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4806def98c84STejun Heo * lead to sysfs name conflicts. 4807def98c84STejun Heo */ 4808def98c84STejun Heo workqueue_sysfs_unregister(wq); 4809def98c84STejun Heo 481033e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 481133e3f0a3SRichard Clark mutex_lock(&wq->mutex); 481233e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 481333e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 481433e3f0a3SRichard Clark 48159c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 48169c5a2ba7STejun Heo drain_workqueue(wq); 4817c8efcc25STejun Heo 4818def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4819def98c84STejun Heo if (wq->rescuer) { 4820def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4821def98c84STejun Heo 4822def98c84STejun Heo /* this prevents new queueing */ 4823a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4824def98c84STejun Heo wq->rescuer = NULL; 4825a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4826def98c84STejun Heo 4827def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4828def98c84STejun Heo kthread_stop(rescuer->task); 48298efe1223STejun Heo kfree(rescuer); 4830def98c84STejun Heo } 4831def98c84STejun Heo 4832c29eb853STejun Heo /* 4833c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4834c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4835c29eb853STejun Heo */ 4836c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4837b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 483849e3cf44STejun Heo for_each_pwq(pwq, wq) { 4839a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4840c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 48411d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4842e66b39afSTejun Heo __func__, wq->name); 4843c29eb853STejun Heo show_pwq(pwq); 4844a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4845b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4846c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 484755df0933SImran Khan show_one_workqueue(wq); 48486183c009STejun Heo return; 484976af4d93STejun Heo } 4850a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 485176af4d93STejun Heo } 4852b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 48536183c009STejun Heo 4854a0a1a5fdSTejun Heo /* 4855a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4856a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4857a0a1a5fdSTejun Heo */ 4858e2dca7adSTejun Heo list_del_rcu(&wq->list); 485968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 48603af24433SOleg Nesterov 48618864b4e5STejun Heo /* 4862636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 4863636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 4864636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 48658864b4e5STejun Heo */ 4866636b927eSTejun Heo rcu_read_lock(); 4867636b927eSTejun Heo 4868636b927eSTejun Heo for_each_possible_cpu(cpu) { 4869636b927eSTejun Heo pwq = rcu_access_pointer(*per_cpu_ptr(wq->cpu_pwq, cpu)); 4870636b927eSTejun Heo RCU_INIT_POINTER(*per_cpu_ptr(wq->cpu_pwq, cpu), NULL); 48714c16bd32STejun Heo put_pwq_unlocked(pwq); 48724c16bd32STejun Heo } 48734c16bd32STejun Heo 4874636b927eSTejun Heo put_pwq_unlocked(wq->dfl_pwq); 48754c16bd32STejun Heo wq->dfl_pwq = NULL; 4876636b927eSTejun Heo 4877636b927eSTejun Heo rcu_read_unlock(); 48783af24433SOleg Nesterov } 48793af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 48803af24433SOleg Nesterov 4881dcd989cbSTejun Heo /** 4882dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4883dcd989cbSTejun Heo * @wq: target workqueue 4884dcd989cbSTejun Heo * @max_active: new max_active value. 4885dcd989cbSTejun Heo * 4886dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4887dcd989cbSTejun Heo * 4888dcd989cbSTejun Heo * CONTEXT: 4889dcd989cbSTejun Heo * Don't call from IRQ context. 4890dcd989cbSTejun Heo */ 4891dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4892dcd989cbSTejun Heo { 48938719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 48940a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 48958719dceaSTejun Heo return; 48968719dceaSTejun Heo 4897f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4898dcd989cbSTejun Heo 4899a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4900dcd989cbSTejun Heo 49010a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4902dcd989cbSTejun Heo wq->saved_max_active = max_active; 4903a045a272STejun Heo wq_adjust_max_active(wq); 4904dcd989cbSTejun Heo 4905a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4906dcd989cbSTejun Heo } 4907dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4908dcd989cbSTejun Heo 4909dcd989cbSTejun Heo /** 491027d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 491127d4ee03SLukas Wunner * 491227d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 491327d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 491427d4ee03SLukas Wunner * 491527d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 491627d4ee03SLukas Wunner */ 491727d4ee03SLukas Wunner struct work_struct *current_work(void) 491827d4ee03SLukas Wunner { 491927d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 492027d4ee03SLukas Wunner 492127d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 492227d4ee03SLukas Wunner } 492327d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 492427d4ee03SLukas Wunner 492527d4ee03SLukas Wunner /** 4926e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4927e6267616STejun Heo * 4928e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4929e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4930d185af30SYacine Belkadi * 4931d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4932e6267616STejun Heo */ 4933e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4934e6267616STejun Heo { 4935e6267616STejun Heo struct worker *worker = current_wq_worker(); 4936e6267616STejun Heo 49376a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4938e6267616STejun Heo } 4939e6267616STejun Heo 4940e6267616STejun Heo /** 4941dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4942dcd989cbSTejun Heo * @cpu: CPU in question 4943dcd989cbSTejun Heo * @wq: target workqueue 4944dcd989cbSTejun Heo * 4945dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4946dcd989cbSTejun Heo * no synchronization around this function and the test result is 4947dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4948dcd989cbSTejun Heo * 4949d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4950636b927eSTejun Heo * 4951636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 4952636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 4953636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 4954636b927eSTejun Heo * other CPUs. 4955d3251859STejun Heo * 4956d185af30SYacine Belkadi * Return: 4957dcd989cbSTejun Heo * %true if congested, %false otherwise. 4958dcd989cbSTejun Heo */ 4959d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4960dcd989cbSTejun Heo { 49617fb98ea7STejun Heo struct pool_workqueue *pwq; 496276af4d93STejun Heo bool ret; 496376af4d93STejun Heo 496424acfb71SThomas Gleixner rcu_read_lock(); 496524acfb71SThomas Gleixner preempt_disable(); 49667fb98ea7STejun Heo 4967d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4968d3251859STejun Heo cpu = smp_processor_id(); 4969d3251859STejun Heo 4970687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 4971f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 4972636b927eSTejun Heo 497324acfb71SThomas Gleixner preempt_enable(); 497424acfb71SThomas Gleixner rcu_read_unlock(); 497576af4d93STejun Heo 497676af4d93STejun Heo return ret; 4977dcd989cbSTejun Heo } 4978dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4979dcd989cbSTejun Heo 4980dcd989cbSTejun Heo /** 4981dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4982dcd989cbSTejun Heo * @work: the work to be tested 4983dcd989cbSTejun Heo * 4984dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4985dcd989cbSTejun Heo * synchronization around this function and the test result is 4986dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4987dcd989cbSTejun Heo * 4988d185af30SYacine Belkadi * Return: 4989dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4990dcd989cbSTejun Heo */ 4991dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4992dcd989cbSTejun Heo { 4993fa1b54e6STejun Heo struct worker_pool *pool; 4994dcd989cbSTejun Heo unsigned long flags; 4995dcd989cbSTejun Heo unsigned int ret = 0; 4996dcd989cbSTejun Heo 4997dcd989cbSTejun Heo if (work_pending(work)) 4998dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4999038366c5SLai Jiangshan 500024acfb71SThomas Gleixner rcu_read_lock(); 5001fa1b54e6STejun Heo pool = get_work_pool(work); 5002038366c5SLai Jiangshan if (pool) { 5003a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 5004c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5005dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5006a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 5007038366c5SLai Jiangshan } 500824acfb71SThomas Gleixner rcu_read_unlock(); 5009dcd989cbSTejun Heo 5010dcd989cbSTejun Heo return ret; 5011dcd989cbSTejun Heo } 5012dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5013dcd989cbSTejun Heo 50143d1cb205STejun Heo /** 50153d1cb205STejun Heo * set_worker_desc - set description for the current work item 50163d1cb205STejun Heo * @fmt: printf-style format string 50173d1cb205STejun Heo * @...: arguments for the format string 50183d1cb205STejun Heo * 50193d1cb205STejun Heo * This function can be called by a running work function to describe what 50203d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 50213d1cb205STejun Heo * information will be printed out together to help debugging. The 50223d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 50233d1cb205STejun Heo */ 50243d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 50253d1cb205STejun Heo { 50263d1cb205STejun Heo struct worker *worker = current_wq_worker(); 50273d1cb205STejun Heo va_list args; 50283d1cb205STejun Heo 50293d1cb205STejun Heo if (worker) { 50303d1cb205STejun Heo va_start(args, fmt); 50313d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 50323d1cb205STejun Heo va_end(args); 50333d1cb205STejun Heo } 50343d1cb205STejun Heo } 50355c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 50363d1cb205STejun Heo 50373d1cb205STejun Heo /** 50383d1cb205STejun Heo * print_worker_info - print out worker information and description 50393d1cb205STejun Heo * @log_lvl: the log level to use when printing 50403d1cb205STejun Heo * @task: target task 50413d1cb205STejun Heo * 50423d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 50433d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 50443d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 50453d1cb205STejun Heo * 50463d1cb205STejun Heo * This function can be safely called on any task as long as the 50473d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 50483d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 50493d1cb205STejun Heo */ 50503d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 50513d1cb205STejun Heo { 50523d1cb205STejun Heo work_func_t *fn = NULL; 50533d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 50543d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 50553d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 50563d1cb205STejun Heo struct workqueue_struct *wq = NULL; 50573d1cb205STejun Heo struct worker *worker; 50583d1cb205STejun Heo 50593d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 50603d1cb205STejun Heo return; 50613d1cb205STejun Heo 50623d1cb205STejun Heo /* 50633d1cb205STejun Heo * This function is called without any synchronization and @task 50643d1cb205STejun Heo * could be in any state. Be careful with dereferences. 50653d1cb205STejun Heo */ 5066e700591aSPetr Mladek worker = kthread_probe_data(task); 50673d1cb205STejun Heo 50683d1cb205STejun Heo /* 50698bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 50708bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 50713d1cb205STejun Heo */ 5072fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5073fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5074fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5075fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5076fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 50773d1cb205STejun Heo 50783d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5079d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 50808bf89593STejun Heo if (strcmp(name, desc)) 50813d1cb205STejun Heo pr_cont(" (%s)", desc); 50823d1cb205STejun Heo pr_cont("\n"); 50833d1cb205STejun Heo } 50843d1cb205STejun Heo } 50853d1cb205STejun Heo 50863494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 50873494fc30STejun Heo { 50883494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 50893494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 50903494fc30STejun Heo pr_cont(" node=%d", pool->node); 50913494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 50923494fc30STejun Heo } 50933494fc30STejun Heo 5094c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5095c76feb0dSPaul E. McKenney bool comma; 5096c76feb0dSPaul E. McKenney work_func_t func; 5097c76feb0dSPaul E. McKenney long ctr; 5098c76feb0dSPaul E. McKenney }; 5099c76feb0dSPaul E. McKenney 5100c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5101c76feb0dSPaul E. McKenney { 5102c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5103c76feb0dSPaul E. McKenney goto out_record; 5104c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5105c76feb0dSPaul E. McKenney pcwsp->ctr++; 5106c76feb0dSPaul E. McKenney return; 5107c76feb0dSPaul E. McKenney } 5108c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5109c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5110c76feb0dSPaul E. McKenney else 5111c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5112c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5113c76feb0dSPaul E. McKenney out_record: 5114c76feb0dSPaul E. McKenney if ((long)func == -1L) 5115c76feb0dSPaul E. McKenney return; 5116c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5117c76feb0dSPaul E. McKenney pcwsp->func = func; 5118c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5119c76feb0dSPaul E. McKenney } 5120c76feb0dSPaul E. McKenney 5121c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 51223494fc30STejun Heo { 51233494fc30STejun Heo if (work->func == wq_barrier_func) { 51243494fc30STejun Heo struct wq_barrier *barr; 51253494fc30STejun Heo 51263494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 51273494fc30STejun Heo 5128c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 51293494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 51303494fc30STejun Heo task_pid_nr(barr->task)); 51313494fc30STejun Heo } else { 5132c76feb0dSPaul E. McKenney if (!comma) 5133c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5134c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 51353494fc30STejun Heo } 51363494fc30STejun Heo } 51373494fc30STejun Heo 51383494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 51393494fc30STejun Heo { 5140c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 51413494fc30STejun Heo struct worker_pool *pool = pwq->pool; 51423494fc30STejun Heo struct work_struct *work; 51433494fc30STejun Heo struct worker *worker; 51443494fc30STejun Heo bool has_in_flight = false, has_pending = false; 51453494fc30STejun Heo int bkt; 51463494fc30STejun Heo 51473494fc30STejun Heo pr_info(" pwq %d:", pool->id); 51483494fc30STejun Heo pr_cont_pool_info(pool); 51493494fc30STejun Heo 5150a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5151a045a272STejun Heo pwq->nr_active, pwq->refcnt, 51523494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 51533494fc30STejun Heo 51543494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 51553494fc30STejun Heo if (worker->current_pwq == pwq) { 51563494fc30STejun Heo has_in_flight = true; 51573494fc30STejun Heo break; 51583494fc30STejun Heo } 51593494fc30STejun Heo } 51603494fc30STejun Heo if (has_in_flight) { 51613494fc30STejun Heo bool comma = false; 51623494fc30STejun Heo 51633494fc30STejun Heo pr_info(" in-flight:"); 51643494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 51653494fc30STejun Heo if (worker->current_pwq != pwq) 51663494fc30STejun Heo continue; 51673494fc30STejun Heo 5168d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 51693494fc30STejun Heo task_pid_nr(worker->task), 517030ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 51713494fc30STejun Heo worker->current_func); 51723494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5173c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5174c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51753494fc30STejun Heo comma = true; 51763494fc30STejun Heo } 51773494fc30STejun Heo pr_cont("\n"); 51783494fc30STejun Heo } 51793494fc30STejun Heo 51803494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 51813494fc30STejun Heo if (get_work_pwq(work) == pwq) { 51823494fc30STejun Heo has_pending = true; 51833494fc30STejun Heo break; 51843494fc30STejun Heo } 51853494fc30STejun Heo } 51863494fc30STejun Heo if (has_pending) { 51873494fc30STejun Heo bool comma = false; 51883494fc30STejun Heo 51893494fc30STejun Heo pr_info(" pending:"); 51903494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 51913494fc30STejun Heo if (get_work_pwq(work) != pwq) 51923494fc30STejun Heo continue; 51933494fc30STejun Heo 5194c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 51953494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 51963494fc30STejun Heo } 5197c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 51983494fc30STejun Heo pr_cont("\n"); 51993494fc30STejun Heo } 52003494fc30STejun Heo 5201f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 52023494fc30STejun Heo bool comma = false; 52033494fc30STejun Heo 5204f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5205f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5206c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 52073494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 52083494fc30STejun Heo } 5209c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 52103494fc30STejun Heo pr_cont("\n"); 52113494fc30STejun Heo } 52123494fc30STejun Heo } 52133494fc30STejun Heo 52143494fc30STejun Heo /** 521555df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 521655df0933SImran Khan * @wq: workqueue whose state will be printed 52173494fc30STejun Heo */ 521855df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 52193494fc30STejun Heo { 52203494fc30STejun Heo struct pool_workqueue *pwq; 52213494fc30STejun Heo bool idle = true; 522255df0933SImran Khan unsigned long flags; 52233494fc30STejun Heo 52243494fc30STejun Heo for_each_pwq(pwq, wq) { 5225*afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 52263494fc30STejun Heo idle = false; 52273494fc30STejun Heo break; 52283494fc30STejun Heo } 52293494fc30STejun Heo } 523055df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 523155df0933SImran Khan return; 52323494fc30STejun Heo 52333494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 52343494fc30STejun Heo 52353494fc30STejun Heo for_each_pwq(pwq, wq) { 5236a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 5237*afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 523857116ce1SJohan Hovold /* 523957116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 524057116ce1SJohan Hovold * drivers that queue work while holding locks 524157116ce1SJohan Hovold * also taken in their write paths. 524257116ce1SJohan Hovold */ 524357116ce1SJohan Hovold printk_deferred_enter(); 52443494fc30STejun Heo show_pwq(pwq); 524557116ce1SJohan Hovold printk_deferred_exit(); 524657116ce1SJohan Hovold } 5247a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 524862635ea8SSergey Senozhatsky /* 524962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 525055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 525162635ea8SSergey Senozhatsky * hard lockup. 525262635ea8SSergey Senozhatsky */ 525362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 52543494fc30STejun Heo } 525555df0933SImran Khan 52563494fc30STejun Heo } 52573494fc30STejun Heo 525855df0933SImran Khan /** 525955df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 526055df0933SImran Khan * @pool: worker pool whose state will be printed 526155df0933SImran Khan */ 526255df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 526355df0933SImran Khan { 52643494fc30STejun Heo struct worker *worker; 52653494fc30STejun Heo bool first = true; 526655df0933SImran Khan unsigned long flags; 5267335a42ebSPetr Mladek unsigned long hung = 0; 52683494fc30STejun Heo 5269a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 52703494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 52713494fc30STejun Heo goto next_pool; 5272335a42ebSPetr Mladek 5273335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5274335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5275335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5276335a42ebSPetr Mladek 527757116ce1SJohan Hovold /* 527857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 527957116ce1SJohan Hovold * queue work while holding locks also taken in their write 528057116ce1SJohan Hovold * paths. 528157116ce1SJohan Hovold */ 528257116ce1SJohan Hovold printk_deferred_enter(); 52833494fc30STejun Heo pr_info("pool %d:", pool->id); 52843494fc30STejun Heo pr_cont_pool_info(pool); 5285335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 52863494fc30STejun Heo if (pool->manager) 52873494fc30STejun Heo pr_cont(" manager: %d", 52883494fc30STejun Heo task_pid_nr(pool->manager->task)); 52893494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 52903494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 52913494fc30STejun Heo task_pid_nr(worker->task)); 52923494fc30STejun Heo first = false; 52933494fc30STejun Heo } 52943494fc30STejun Heo pr_cont("\n"); 529557116ce1SJohan Hovold printk_deferred_exit(); 52963494fc30STejun Heo next_pool: 5297a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 529862635ea8SSergey Senozhatsky /* 529962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 530055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 530162635ea8SSergey Senozhatsky * hard lockup. 530262635ea8SSergey Senozhatsky */ 530362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 530455df0933SImran Khan 53053494fc30STejun Heo } 53063494fc30STejun Heo 530755df0933SImran Khan /** 530855df0933SImran Khan * show_all_workqueues - dump workqueue state 530955df0933SImran Khan * 5310704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 531155df0933SImran Khan */ 531255df0933SImran Khan void show_all_workqueues(void) 531355df0933SImran Khan { 531455df0933SImran Khan struct workqueue_struct *wq; 531555df0933SImran Khan struct worker_pool *pool; 531655df0933SImran Khan int pi; 531755df0933SImran Khan 531855df0933SImran Khan rcu_read_lock(); 531955df0933SImran Khan 532055df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 532155df0933SImran Khan 532255df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 532355df0933SImran Khan show_one_workqueue(wq); 532455df0933SImran Khan 532555df0933SImran Khan for_each_pool(pool, pi) 532655df0933SImran Khan show_one_worker_pool(pool); 532755df0933SImran Khan 532824acfb71SThomas Gleixner rcu_read_unlock(); 53293494fc30STejun Heo } 53303494fc30STejun Heo 5331704bc669SJungseung Lee /** 5332704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5333704bc669SJungseung Lee * 5334704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5335704bc669SJungseung Lee * still busy. 5336704bc669SJungseung Lee */ 5337704bc669SJungseung Lee void show_freezable_workqueues(void) 5338704bc669SJungseung Lee { 5339704bc669SJungseung Lee struct workqueue_struct *wq; 5340704bc669SJungseung Lee 5341704bc669SJungseung Lee rcu_read_lock(); 5342704bc669SJungseung Lee 5343704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5344704bc669SJungseung Lee 5345704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5346704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5347704bc669SJungseung Lee continue; 5348704bc669SJungseung Lee show_one_workqueue(wq); 5349704bc669SJungseung Lee } 5350704bc669SJungseung Lee 5351704bc669SJungseung Lee rcu_read_unlock(); 5352704bc669SJungseung Lee } 5353704bc669SJungseung Lee 53546b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 53556b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 53566b59808bSTejun Heo { 53576b59808bSTejun Heo int off; 53586b59808bSTejun Heo 53596b59808bSTejun Heo /* always show the actual comm */ 53606b59808bSTejun Heo off = strscpy(buf, task->comm, size); 53616b59808bSTejun Heo if (off < 0) 53626b59808bSTejun Heo return; 53636b59808bSTejun Heo 5364197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 53656b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 53666b59808bSTejun Heo 5367197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5368197f6accSTejun Heo struct worker *worker = kthread_data(task); 5369197f6accSTejun Heo struct worker_pool *pool = worker->pool; 53706b59808bSTejun Heo 53716b59808bSTejun Heo if (pool) { 5372a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 53736b59808bSTejun Heo /* 5374197f6accSTejun Heo * ->desc tracks information (wq name or 5375197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5376197f6accSTejun Heo * current, prepend '+', otherwise '-'. 53776b59808bSTejun Heo */ 53786b59808bSTejun Heo if (worker->desc[0] != '\0') { 53796b59808bSTejun Heo if (worker->current_work) 53806b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 53816b59808bSTejun Heo worker->desc); 53826b59808bSTejun Heo else 53836b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 53846b59808bSTejun Heo worker->desc); 53856b59808bSTejun Heo } 5386a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 53876b59808bSTejun Heo } 5388197f6accSTejun Heo } 53896b59808bSTejun Heo 53906b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 53916b59808bSTejun Heo } 53926b59808bSTejun Heo 539366448bc2SMathieu Malaterre #ifdef CONFIG_SMP 539466448bc2SMathieu Malaterre 5395db7bccf4STejun Heo /* 5396db7bccf4STejun Heo * CPU hotplug. 5397db7bccf4STejun Heo * 5398e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5399112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5400706026c2STejun Heo * pool which make migrating pending and scheduled works very 5401e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 540294cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5403e22bee78STejun Heo * blocked draining impractical. 5404e22bee78STejun Heo * 540524647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5406628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5407628c78e7STejun Heo * cpu comes back online. 5408db7bccf4STejun Heo */ 5409db7bccf4STejun Heo 5410e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5411db7bccf4STejun Heo { 54124ce62e9eSTejun Heo struct worker_pool *pool; 5413db7bccf4STejun Heo struct worker *worker; 5414db7bccf4STejun Heo 5415f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 54161258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5417a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5418e22bee78STejun Heo 5419f2d5a0eeSTejun Heo /* 542092f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 542194cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 542211b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5423b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5424b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5425b4ac9384SLai Jiangshan * is on the same cpu. 5426f2d5a0eeSTejun Heo */ 5427da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5428403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5429db7bccf4STejun Heo 543024647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5431f2d5a0eeSTejun Heo 5432e22bee78STejun Heo /* 5433989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5434989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5435989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5436989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5437989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5438eb283428SLai Jiangshan * are served by workers tied to the pool. 5439e22bee78STejun Heo */ 5440bc35f7efSLai Jiangshan pool->nr_running = 0; 5441eb283428SLai Jiangshan 5442eb283428SLai Jiangshan /* 5443eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5444eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5445eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5446eb283428SLai Jiangshan */ 54470219a352STejun Heo kick_pool(pool); 5448989442d7SLai Jiangshan 5449a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5450989442d7SLai Jiangshan 5451793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5452793777bcSValentin Schneider unbind_worker(worker); 5453989442d7SLai Jiangshan 5454989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5455eb283428SLai Jiangshan } 5456db7bccf4STejun Heo } 5457db7bccf4STejun Heo 5458bd7c089eSTejun Heo /** 5459bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5460bd7c089eSTejun Heo * @pool: pool of interest 5461bd7c089eSTejun Heo * 5462a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5463bd7c089eSTejun Heo */ 5464bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5465bd7c089eSTejun Heo { 5466a9ab775bSTejun Heo struct worker *worker; 5467bd7c089eSTejun Heo 54681258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5469bd7c089eSTejun Heo 5470bd7c089eSTejun Heo /* 5471a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5472a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5473402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5474a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5475a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5476bd7c089eSTejun Heo */ 5477c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5478c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5479c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 54809546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 5481c63a2e52SValentin Schneider } 5482a9ab775bSTejun Heo 5483a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5484f7c17d26SWanpeng Li 54853de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5486a9ab775bSTejun Heo 5487da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5488a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5489a9ab775bSTejun Heo 5490a9ab775bSTejun Heo /* 5491a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5492a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5493a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5494a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5495a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5496a9ab775bSTejun Heo * concurrency management. Note that when or whether 5497a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5498a9ab775bSTejun Heo * 5499c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5500a9ab775bSTejun Heo * tested without holding any lock in 55016d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5502a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5503a9ab775bSTejun Heo * management operations. 5504bd7c089eSTejun Heo */ 5505a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5506a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5507a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5508c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5509bd7c089eSTejun Heo } 5510a9ab775bSTejun Heo 5511a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5512bd7c089eSTejun Heo } 5513bd7c089eSTejun Heo 55147dbc725eSTejun Heo /** 55157dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 55167dbc725eSTejun Heo * @pool: unbound pool of interest 55177dbc725eSTejun Heo * @cpu: the CPU which is coming up 55187dbc725eSTejun Heo * 55197dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 55207dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 55217dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 55227dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 55237dbc725eSTejun Heo */ 55247dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 55257dbc725eSTejun Heo { 55267dbc725eSTejun Heo static cpumask_t cpumask; 55277dbc725eSTejun Heo struct worker *worker; 55287dbc725eSTejun Heo 55291258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 55307dbc725eSTejun Heo 55317dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 55327dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 55337dbc725eSTejun Heo return; 55347dbc725eSTejun Heo 55357dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 55367dbc725eSTejun Heo 55377dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5538da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5539d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 55407dbc725eSTejun Heo } 55417dbc725eSTejun Heo 55427ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 55431da177e4SLinus Torvalds { 55444ce62e9eSTejun Heo struct worker_pool *pool; 55451da177e4SLinus Torvalds 5546f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 55473ce63377STejun Heo if (pool->nr_workers) 55483ce63377STejun Heo continue; 5549051e1850SLai Jiangshan if (!create_worker(pool)) 55507ee681b2SThomas Gleixner return -ENOMEM; 55513af24433SOleg Nesterov } 55527ee681b2SThomas Gleixner return 0; 55537ee681b2SThomas Gleixner } 55541da177e4SLinus Torvalds 55557ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 55567ee681b2SThomas Gleixner { 55577ee681b2SThomas Gleixner struct worker_pool *pool; 55587ee681b2SThomas Gleixner struct workqueue_struct *wq; 55597ee681b2SThomas Gleixner int pi; 55607ee681b2SThomas Gleixner 556168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 55627dbc725eSTejun Heo 55637dbc725eSTejun Heo for_each_pool(pool, pi) { 55641258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 556594cf58bbSTejun Heo 5566f05b558dSLai Jiangshan if (pool->cpu == cpu) 556794cf58bbSTejun Heo rebind_workers(pool); 5568f05b558dSLai Jiangshan else if (pool->cpu < 0) 55697dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 557094cf58bbSTejun Heo 55711258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 557294cf58bbSTejun Heo } 55737dbc725eSTejun Heo 5574fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 55754cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 557684193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 557784193c07STejun Heo 557884193c07STejun Heo if (attrs) { 557984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 55804cbfd3deSTejun Heo int tcpu; 55814cbfd3deSTejun Heo 558284193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5583fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 55844cbfd3deSTejun Heo } 55854cbfd3deSTejun Heo } 55864c16bd32STejun Heo 558768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55887ee681b2SThomas Gleixner return 0; 558965758202STejun Heo } 559065758202STejun Heo 55917ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 559265758202STejun Heo { 55934c16bd32STejun Heo struct workqueue_struct *wq; 55948db25e78STejun Heo 55954c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5596e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5597e8b3f8dbSLai Jiangshan return -1; 5598e8b3f8dbSLai Jiangshan 5599e8b3f8dbSLai Jiangshan unbind_workers(cpu); 56004c16bd32STejun Heo 5601fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 56024c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 56034cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 560484193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 560584193c07STejun Heo 560684193c07STejun Heo if (attrs) { 560784193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 56084cbfd3deSTejun Heo int tcpu; 56094cbfd3deSTejun Heo 561084193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5611fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 56124cbfd3deSTejun Heo } 56134cbfd3deSTejun Heo } 56144c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 56154c16bd32STejun Heo 56167ee681b2SThomas Gleixner return 0; 561765758202STejun Heo } 561865758202STejun Heo 56192d3854a3SRusty Russell struct work_for_cpu { 5620ed48ece2STejun Heo struct work_struct work; 56212d3854a3SRusty Russell long (*fn)(void *); 56222d3854a3SRusty Russell void *arg; 56232d3854a3SRusty Russell long ret; 56242d3854a3SRusty Russell }; 56252d3854a3SRusty Russell 5626ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 56272d3854a3SRusty Russell { 5628ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5629ed48ece2STejun Heo 56302d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 56312d3854a3SRusty Russell } 56322d3854a3SRusty Russell 56332d3854a3SRusty Russell /** 5634265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 56352d3854a3SRusty Russell * @cpu: the cpu to run on 56362d3854a3SRusty Russell * @fn: the function to run 56372d3854a3SRusty Russell * @arg: the function arg 5638265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 56392d3854a3SRusty Russell * 564031ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 56416b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5642d185af30SYacine Belkadi * 5643d185af30SYacine Belkadi * Return: The value @fn returns. 56442d3854a3SRusty Russell */ 5645265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 5646265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 56472d3854a3SRusty Russell { 5648ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 56492d3854a3SRusty Russell 5650265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 5651ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 565212997d1aSBjorn Helgaas flush_work(&wfc.work); 5653440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 56542d3854a3SRusty Russell return wfc.ret; 56552d3854a3SRusty Russell } 5656265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 56570e8d6a93SThomas Gleixner 56580e8d6a93SThomas Gleixner /** 5659265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 56600e8d6a93SThomas Gleixner * @cpu: the cpu to run on 56610e8d6a93SThomas Gleixner * @fn: the function to run 56620e8d6a93SThomas Gleixner * @arg: the function argument 5663265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 56640e8d6a93SThomas Gleixner * 56650e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 56660e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 56670e8d6a93SThomas Gleixner * 56680e8d6a93SThomas Gleixner * Return: The value @fn returns. 56690e8d6a93SThomas Gleixner */ 5670265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 5671265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 56720e8d6a93SThomas Gleixner { 56730e8d6a93SThomas Gleixner long ret = -ENODEV; 56740e8d6a93SThomas Gleixner 5675ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 56760e8d6a93SThomas Gleixner if (cpu_online(cpu)) 5677265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 5678ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 56790e8d6a93SThomas Gleixner return ret; 56800e8d6a93SThomas Gleixner } 5681265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 56822d3854a3SRusty Russell #endif /* CONFIG_SMP */ 56832d3854a3SRusty Russell 5684a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5685e7577c50SRusty Russell 5686a0a1a5fdSTejun Heo /** 5687a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5688a0a1a5fdSTejun Heo * 568958a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5690f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5691706026c2STejun Heo * pool->worklist. 5692a0a1a5fdSTejun Heo * 5693a0a1a5fdSTejun Heo * CONTEXT: 5694a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5695a0a1a5fdSTejun Heo */ 5696a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5697a0a1a5fdSTejun Heo { 569824b8a847STejun Heo struct workqueue_struct *wq; 5699a0a1a5fdSTejun Heo 570068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5701a0a1a5fdSTejun Heo 57026183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5703a0a1a5fdSTejun Heo workqueue_freezing = true; 5704a0a1a5fdSTejun Heo 570524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5706a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5707a045a272STejun Heo wq_adjust_max_active(wq); 5708a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5709a1056305STejun Heo } 57105bcab335STejun Heo 571168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5712a0a1a5fdSTejun Heo } 5713a0a1a5fdSTejun Heo 5714a0a1a5fdSTejun Heo /** 571558a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5716a0a1a5fdSTejun Heo * 5717a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5718a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5719a0a1a5fdSTejun Heo * 5720a0a1a5fdSTejun Heo * CONTEXT: 572168e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5722a0a1a5fdSTejun Heo * 5723d185af30SYacine Belkadi * Return: 572458a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 572558a69cb4STejun Heo * is complete. 5726a0a1a5fdSTejun Heo */ 5727a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5728a0a1a5fdSTejun Heo { 5729a0a1a5fdSTejun Heo bool busy = false; 573024b8a847STejun Heo struct workqueue_struct *wq; 573124b8a847STejun Heo struct pool_workqueue *pwq; 5732a0a1a5fdSTejun Heo 573368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5734a0a1a5fdSTejun Heo 57356183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5736a0a1a5fdSTejun Heo 573724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 573824b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 573924b8a847STejun Heo continue; 5740a0a1a5fdSTejun Heo /* 5741a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5742a0a1a5fdSTejun Heo * to peek without lock. 5743a0a1a5fdSTejun Heo */ 574424acfb71SThomas Gleixner rcu_read_lock(); 574524b8a847STejun Heo for_each_pwq(pwq, wq) { 57466183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5747112202d9STejun Heo if (pwq->nr_active) { 5748a0a1a5fdSTejun Heo busy = true; 574924acfb71SThomas Gleixner rcu_read_unlock(); 5750a0a1a5fdSTejun Heo goto out_unlock; 5751a0a1a5fdSTejun Heo } 5752a0a1a5fdSTejun Heo } 575324acfb71SThomas Gleixner rcu_read_unlock(); 5754a0a1a5fdSTejun Heo } 5755a0a1a5fdSTejun Heo out_unlock: 575668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5757a0a1a5fdSTejun Heo return busy; 5758a0a1a5fdSTejun Heo } 5759a0a1a5fdSTejun Heo 5760a0a1a5fdSTejun Heo /** 5761a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5762a0a1a5fdSTejun Heo * 5763a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5764706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5765a0a1a5fdSTejun Heo * 5766a0a1a5fdSTejun Heo * CONTEXT: 5767a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5768a0a1a5fdSTejun Heo */ 5769a0a1a5fdSTejun Heo void thaw_workqueues(void) 5770a0a1a5fdSTejun Heo { 577124b8a847STejun Heo struct workqueue_struct *wq; 5772a0a1a5fdSTejun Heo 577368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5774a0a1a5fdSTejun Heo 5775a0a1a5fdSTejun Heo if (!workqueue_freezing) 5776a0a1a5fdSTejun Heo goto out_unlock; 5777a0a1a5fdSTejun Heo 577874b414eaSLai Jiangshan workqueue_freezing = false; 577924b8a847STejun Heo 578024b8a847STejun Heo /* restore max_active and repopulate worklist */ 578124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5782a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5783a045a272STejun Heo wq_adjust_max_active(wq); 5784a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 578524b8a847STejun Heo } 578624b8a847STejun Heo 5787a0a1a5fdSTejun Heo out_unlock: 578868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5789a0a1a5fdSTejun Heo } 5790a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5791a0a1a5fdSTejun Heo 579299c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 5793042f7df1SLai Jiangshan { 5794042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5795042f7df1SLai Jiangshan int ret = 0; 5796042f7df1SLai Jiangshan struct workqueue_struct *wq; 5797042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5798042f7df1SLai Jiangshan 5799042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5800042f7df1SLai Jiangshan 5801042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5802042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5803042f7df1SLai Jiangshan continue; 5804ca10d851SWaiman Long 5805042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5806ca10d851SWaiman Long if (!list_empty(&wq->pwqs)) { 5807ca10d851SWaiman Long if (wq->flags & __WQ_ORDERED_EXPLICIT) 5808042f7df1SLai Jiangshan continue; 5809ca10d851SWaiman Long wq->flags &= ~__WQ_ORDERED; 5810ca10d851SWaiman Long } 5811042f7df1SLai Jiangshan 581299c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 581384193c07STejun Heo if (IS_ERR(ctx)) { 581484193c07STejun Heo ret = PTR_ERR(ctx); 5815042f7df1SLai Jiangshan break; 5816042f7df1SLai Jiangshan } 5817042f7df1SLai Jiangshan 5818042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5819042f7df1SLai Jiangshan } 5820042f7df1SLai Jiangshan 5821042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5822042f7df1SLai Jiangshan if (!ret) 5823042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5824042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5825042f7df1SLai Jiangshan } 5826042f7df1SLai Jiangshan 582799c621efSLai Jiangshan if (!ret) { 582899c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 582999c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 583099c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 583199c621efSLai Jiangshan } 5832042f7df1SLai Jiangshan return ret; 5833042f7df1SLai Jiangshan } 5834042f7df1SLai Jiangshan 5835042f7df1SLai Jiangshan /** 5836fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 5837fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 5838fe28f631SWaiman Long * 5839fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 5840fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 5841fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 5842fe28f631SWaiman Long */ 5843fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 5844fe28f631SWaiman Long { 5845fe28f631SWaiman Long cpumask_var_t cpumask; 5846fe28f631SWaiman Long int ret = 0; 5847fe28f631SWaiman Long 5848fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5849fe28f631SWaiman Long return -ENOMEM; 5850fe28f631SWaiman Long 5851fe28f631SWaiman Long lockdep_assert_cpus_held(); 5852fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 5853fe28f631SWaiman Long 5854fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 5855fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 5856fe28f631SWaiman Long 5857fe28f631SWaiman Long /* 5858fe28f631SWaiman Long * If the operation fails, it will fall back to 5859fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 5860fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 5861fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 5862fe28f631SWaiman Long */ 5863fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 5864fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 5865fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 5866fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 5867fe28f631SWaiman Long 5868fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 5869fe28f631SWaiman Long free_cpumask_var(cpumask); 5870fe28f631SWaiman Long return ret; 5871fe28f631SWaiman Long } 5872fe28f631SWaiman Long 587363c5484eSTejun Heo static int parse_affn_scope(const char *val) 587463c5484eSTejun Heo { 587563c5484eSTejun Heo int i; 587663c5484eSTejun Heo 587763c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 587863c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 587963c5484eSTejun Heo return i; 588063c5484eSTejun Heo } 588163c5484eSTejun Heo return -EINVAL; 588263c5484eSTejun Heo } 588363c5484eSTejun Heo 588463c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 588563c5484eSTejun Heo { 5886523a301eSTejun Heo struct workqueue_struct *wq; 5887523a301eSTejun Heo int affn, cpu; 588863c5484eSTejun Heo 588963c5484eSTejun Heo affn = parse_affn_scope(val); 589063c5484eSTejun Heo if (affn < 0) 589163c5484eSTejun Heo return affn; 5892523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 5893523a301eSTejun Heo return -EINVAL; 5894523a301eSTejun Heo 5895523a301eSTejun Heo cpus_read_lock(); 5896523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 589763c5484eSTejun Heo 589863c5484eSTejun Heo wq_affn_dfl = affn; 5899523a301eSTejun Heo 5900523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 5901523a301eSTejun Heo for_each_online_cpu(cpu) { 5902523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 5903523a301eSTejun Heo } 5904523a301eSTejun Heo } 5905523a301eSTejun Heo 5906523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 5907523a301eSTejun Heo cpus_read_unlock(); 5908523a301eSTejun Heo 590963c5484eSTejun Heo return 0; 591063c5484eSTejun Heo } 591163c5484eSTejun Heo 591263c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 591363c5484eSTejun Heo { 591463c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 591563c5484eSTejun Heo } 591663c5484eSTejun Heo 591763c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 591863c5484eSTejun Heo .set = wq_affn_dfl_set, 591963c5484eSTejun Heo .get = wq_affn_dfl_get, 592063c5484eSTejun Heo }; 592163c5484eSTejun Heo 592263c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 592363c5484eSTejun Heo 59246ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 59256ba94429SFrederic Weisbecker /* 59266ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 59276ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 59286ba94429SFrederic Weisbecker * following attributes. 59296ba94429SFrederic Weisbecker * 59306ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 59316ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 59326ba94429SFrederic Weisbecker * 59336ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 59346ba94429SFrederic Weisbecker * 59356ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 59366ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 593763c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 59388639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 59396ba94429SFrederic Weisbecker */ 59406ba94429SFrederic Weisbecker struct wq_device { 59416ba94429SFrederic Weisbecker struct workqueue_struct *wq; 59426ba94429SFrederic Weisbecker struct device dev; 59436ba94429SFrederic Weisbecker }; 59446ba94429SFrederic Weisbecker 59456ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 59466ba94429SFrederic Weisbecker { 59476ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 59486ba94429SFrederic Weisbecker 59496ba94429SFrederic Weisbecker return wq_dev->wq; 59506ba94429SFrederic Weisbecker } 59516ba94429SFrederic Weisbecker 59526ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 59536ba94429SFrederic Weisbecker char *buf) 59546ba94429SFrederic Weisbecker { 59556ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59566ba94429SFrederic Weisbecker 59576ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 59586ba94429SFrederic Weisbecker } 59596ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 59606ba94429SFrederic Weisbecker 59616ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 59626ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 59636ba94429SFrederic Weisbecker { 59646ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59656ba94429SFrederic Weisbecker 59666ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 59676ba94429SFrederic Weisbecker } 59686ba94429SFrederic Weisbecker 59696ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 59706ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 59716ba94429SFrederic Weisbecker size_t count) 59726ba94429SFrederic Weisbecker { 59736ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 59746ba94429SFrederic Weisbecker int val; 59756ba94429SFrederic Weisbecker 59766ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 59776ba94429SFrederic Weisbecker return -EINVAL; 59786ba94429SFrederic Weisbecker 59796ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 59806ba94429SFrederic Weisbecker return count; 59816ba94429SFrederic Weisbecker } 59826ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 59836ba94429SFrederic Weisbecker 59846ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 59856ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 59866ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 59876ba94429SFrederic Weisbecker NULL, 59886ba94429SFrederic Weisbecker }; 59896ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 59906ba94429SFrederic Weisbecker 599149277a5bSWaiman Long static void apply_wqattrs_lock(void) 599249277a5bSWaiman Long { 599349277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 599449277a5bSWaiman Long cpus_read_lock(); 599549277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 599649277a5bSWaiman Long } 599749277a5bSWaiman Long 599849277a5bSWaiman Long static void apply_wqattrs_unlock(void) 599949277a5bSWaiman Long { 600049277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 600149277a5bSWaiman Long cpus_read_unlock(); 600249277a5bSWaiman Long } 600349277a5bSWaiman Long 60046ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 60056ba94429SFrederic Weisbecker char *buf) 60066ba94429SFrederic Weisbecker { 60076ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 60086ba94429SFrederic Weisbecker int written; 60096ba94429SFrederic Weisbecker 60106ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 60116ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 60126ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 60136ba94429SFrederic Weisbecker 60146ba94429SFrederic Weisbecker return written; 60156ba94429SFrederic Weisbecker } 60166ba94429SFrederic Weisbecker 60176ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 60186ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 60196ba94429SFrederic Weisbecker { 60206ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 60216ba94429SFrederic Weisbecker 6022899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6023899a94feSLai Jiangshan 6024be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 60256ba94429SFrederic Weisbecker if (!attrs) 60266ba94429SFrederic Weisbecker return NULL; 60276ba94429SFrederic Weisbecker 60286ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 60296ba94429SFrederic Weisbecker return attrs; 60306ba94429SFrederic Weisbecker } 60316ba94429SFrederic Weisbecker 60326ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 60336ba94429SFrederic Weisbecker const char *buf, size_t count) 60346ba94429SFrederic Weisbecker { 60356ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 60366ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6037d4d3e257SLai Jiangshan int ret = -ENOMEM; 6038d4d3e257SLai Jiangshan 6039d4d3e257SLai Jiangshan apply_wqattrs_lock(); 60406ba94429SFrederic Weisbecker 60416ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 60426ba94429SFrederic Weisbecker if (!attrs) 6043d4d3e257SLai Jiangshan goto out_unlock; 60446ba94429SFrederic Weisbecker 60456ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 60466ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6047d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 60486ba94429SFrederic Weisbecker else 60496ba94429SFrederic Weisbecker ret = -EINVAL; 60506ba94429SFrederic Weisbecker 6051d4d3e257SLai Jiangshan out_unlock: 6052d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 60536ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 60546ba94429SFrederic Weisbecker return ret ?: count; 60556ba94429SFrederic Weisbecker } 60566ba94429SFrederic Weisbecker 60576ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 60586ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 60596ba94429SFrederic Weisbecker { 60606ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 60616ba94429SFrederic Weisbecker int written; 60626ba94429SFrederic Weisbecker 60636ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 60646ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 60656ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 60666ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 60676ba94429SFrederic Weisbecker return written; 60686ba94429SFrederic Weisbecker } 60696ba94429SFrederic Weisbecker 60706ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 60716ba94429SFrederic Weisbecker struct device_attribute *attr, 60726ba94429SFrederic Weisbecker const char *buf, size_t count) 60736ba94429SFrederic Weisbecker { 60746ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 60756ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6076d4d3e257SLai Jiangshan int ret = -ENOMEM; 6077d4d3e257SLai Jiangshan 6078d4d3e257SLai Jiangshan apply_wqattrs_lock(); 60796ba94429SFrederic Weisbecker 60806ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 60816ba94429SFrederic Weisbecker if (!attrs) 6082d4d3e257SLai Jiangshan goto out_unlock; 60836ba94429SFrederic Weisbecker 60846ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 60856ba94429SFrederic Weisbecker if (!ret) 6086d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 60876ba94429SFrederic Weisbecker 6088d4d3e257SLai Jiangshan out_unlock: 6089d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 60906ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 60916ba94429SFrederic Weisbecker return ret ?: count; 60926ba94429SFrederic Weisbecker } 60936ba94429SFrederic Weisbecker 609463c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 609563c5484eSTejun Heo struct device_attribute *attr, char *buf) 609663c5484eSTejun Heo { 609763c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 609863c5484eSTejun Heo int written; 609963c5484eSTejun Heo 610063c5484eSTejun Heo mutex_lock(&wq->mutex); 6101523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6102523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6103523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6104523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6105523a301eSTejun Heo else 610663c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 610763c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 610863c5484eSTejun Heo mutex_unlock(&wq->mutex); 610963c5484eSTejun Heo 611063c5484eSTejun Heo return written; 611163c5484eSTejun Heo } 611263c5484eSTejun Heo 611363c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 611463c5484eSTejun Heo struct device_attribute *attr, 611563c5484eSTejun Heo const char *buf, size_t count) 611663c5484eSTejun Heo { 611763c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 611863c5484eSTejun Heo struct workqueue_attrs *attrs; 611963c5484eSTejun Heo int affn, ret = -ENOMEM; 612063c5484eSTejun Heo 612163c5484eSTejun Heo affn = parse_affn_scope(buf); 612263c5484eSTejun Heo if (affn < 0) 612363c5484eSTejun Heo return affn; 612463c5484eSTejun Heo 612563c5484eSTejun Heo apply_wqattrs_lock(); 612663c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 612763c5484eSTejun Heo if (attrs) { 612863c5484eSTejun Heo attrs->affn_scope = affn; 612963c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 613063c5484eSTejun Heo } 613163c5484eSTejun Heo apply_wqattrs_unlock(); 613263c5484eSTejun Heo free_workqueue_attrs(attrs); 613363c5484eSTejun Heo return ret ?: count; 613463c5484eSTejun Heo } 613563c5484eSTejun Heo 61368639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 61378639ecebSTejun Heo struct device_attribute *attr, char *buf) 61388639ecebSTejun Heo { 61398639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 61408639ecebSTejun Heo 61418639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 61428639ecebSTejun Heo wq->unbound_attrs->affn_strict); 61438639ecebSTejun Heo } 61448639ecebSTejun Heo 61458639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 61468639ecebSTejun Heo struct device_attribute *attr, 61478639ecebSTejun Heo const char *buf, size_t count) 61488639ecebSTejun Heo { 61498639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 61508639ecebSTejun Heo struct workqueue_attrs *attrs; 61518639ecebSTejun Heo int v, ret = -ENOMEM; 61528639ecebSTejun Heo 61538639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 61548639ecebSTejun Heo return -EINVAL; 61558639ecebSTejun Heo 61568639ecebSTejun Heo apply_wqattrs_lock(); 61578639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 61588639ecebSTejun Heo if (attrs) { 61598639ecebSTejun Heo attrs->affn_strict = (bool)v; 61608639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 61618639ecebSTejun Heo } 61628639ecebSTejun Heo apply_wqattrs_unlock(); 61638639ecebSTejun Heo free_workqueue_attrs(attrs); 61648639ecebSTejun Heo return ret ?: count; 61658639ecebSTejun Heo } 61668639ecebSTejun Heo 61676ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 61686ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 61696ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 617063c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 61718639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 61726ba94429SFrederic Weisbecker __ATTR_NULL, 61736ba94429SFrederic Weisbecker }; 61746ba94429SFrederic Weisbecker 61756ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 61766ba94429SFrederic Weisbecker .name = "workqueue", 61776ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 61786ba94429SFrederic Weisbecker }; 61796ba94429SFrederic Weisbecker 618049277a5bSWaiman Long /** 618149277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 618249277a5bSWaiman Long * @cpumask: the cpumask to set 618349277a5bSWaiman Long * 618449277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 618549277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 618649277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 618749277a5bSWaiman Long * 618849277a5bSWaiman Long * Return: 0 - Success 618949277a5bSWaiman Long * -EINVAL - Invalid @cpumask 619049277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 619149277a5bSWaiman Long */ 619249277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 619349277a5bSWaiman Long { 619449277a5bSWaiman Long int ret = -EINVAL; 619549277a5bSWaiman Long 619649277a5bSWaiman Long /* 619749277a5bSWaiman Long * Not excluding isolated cpus on purpose. 619849277a5bSWaiman Long * If the user wishes to include them, we allow that. 619949277a5bSWaiman Long */ 620049277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 620149277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 620249277a5bSWaiman Long apply_wqattrs_lock(); 620349277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 620449277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 620549277a5bSWaiman Long ret = 0; 620649277a5bSWaiman Long goto out_unlock; 620749277a5bSWaiman Long } 620849277a5bSWaiman Long 620949277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 621049277a5bSWaiman Long 621149277a5bSWaiman Long out_unlock: 621249277a5bSWaiman Long apply_wqattrs_unlock(); 621349277a5bSWaiman Long } 621449277a5bSWaiman Long 621549277a5bSWaiman Long return ret; 621649277a5bSWaiman Long } 621749277a5bSWaiman Long 6218fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 6219fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 6220b05a7928SFrederic Weisbecker { 6221b05a7928SFrederic Weisbecker int written; 6222b05a7928SFrederic Weisbecker 6223042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6224fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 6225042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6226b05a7928SFrederic Weisbecker 6227b05a7928SFrederic Weisbecker return written; 6228b05a7928SFrederic Weisbecker } 6229b05a7928SFrederic Weisbecker 6230fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 6231fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6232fe28f631SWaiman Long { 6233fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 6234fe28f631SWaiman Long } 6235fe28f631SWaiman Long 6236fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 6237fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6238fe28f631SWaiman Long { 6239fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 6240fe28f631SWaiman Long } 6241fe28f631SWaiman Long 6242fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 6243fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6244fe28f631SWaiman Long { 6245fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 6246fe28f631SWaiman Long } 6247fe28f631SWaiman Long 6248042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6249042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6250042f7df1SLai Jiangshan { 6251042f7df1SLai Jiangshan cpumask_var_t cpumask; 6252042f7df1SLai Jiangshan int ret; 6253042f7df1SLai Jiangshan 6254042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6255042f7df1SLai Jiangshan return -ENOMEM; 6256042f7df1SLai Jiangshan 6257042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 6258042f7df1SLai Jiangshan if (!ret) 6259042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 6260042f7df1SLai Jiangshan 6261042f7df1SLai Jiangshan free_cpumask_var(cpumask); 6262042f7df1SLai Jiangshan return ret ? ret : count; 6263042f7df1SLai Jiangshan } 6264042f7df1SLai Jiangshan 6265fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 6266042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 6267fe28f631SWaiman Long wq_unbound_cpumask_store), 6268fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 6269fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 6270fe28f631SWaiman Long __ATTR_NULL, 6271fe28f631SWaiman Long }; 6272b05a7928SFrederic Weisbecker 62736ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 62746ba94429SFrederic Weisbecker { 6275686f6697SGreg Kroah-Hartman struct device *dev_root; 6276b05a7928SFrederic Weisbecker int err; 6277b05a7928SFrederic Weisbecker 6278b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 6279b05a7928SFrederic Weisbecker if (err) 6280b05a7928SFrederic Weisbecker return err; 6281b05a7928SFrederic Weisbecker 6282686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 6283686f6697SGreg Kroah-Hartman if (dev_root) { 6284fe28f631SWaiman Long struct device_attribute *attr; 6285fe28f631SWaiman Long 6286fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 6287fe28f631SWaiman Long err = device_create_file(dev_root, attr); 6288fe28f631SWaiman Long if (err) 6289fe28f631SWaiman Long break; 6290fe28f631SWaiman Long } 6291686f6697SGreg Kroah-Hartman put_device(dev_root); 6292686f6697SGreg Kroah-Hartman } 6293686f6697SGreg Kroah-Hartman return err; 62946ba94429SFrederic Weisbecker } 62956ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 62966ba94429SFrederic Weisbecker 62976ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 62986ba94429SFrederic Weisbecker { 62996ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 63006ba94429SFrederic Weisbecker 63016ba94429SFrederic Weisbecker kfree(wq_dev); 63026ba94429SFrederic Weisbecker } 63036ba94429SFrederic Weisbecker 63046ba94429SFrederic Weisbecker /** 63056ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 63066ba94429SFrederic Weisbecker * @wq: the workqueue to register 63076ba94429SFrederic Weisbecker * 63086ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 63096ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 63106ba94429SFrederic Weisbecker * which is the preferred method. 63116ba94429SFrederic Weisbecker * 63126ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 63136ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 63146ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 63156ba94429SFrederic Weisbecker * attributes. 63166ba94429SFrederic Weisbecker * 63176ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 63186ba94429SFrederic Weisbecker */ 63196ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 63206ba94429SFrederic Weisbecker { 63216ba94429SFrederic Weisbecker struct wq_device *wq_dev; 63226ba94429SFrederic Weisbecker int ret; 63236ba94429SFrederic Weisbecker 63246ba94429SFrederic Weisbecker /* 6325402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 63266ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 63276ba94429SFrederic Weisbecker * workqueues. 63286ba94429SFrederic Weisbecker */ 63290a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 63306ba94429SFrederic Weisbecker return -EINVAL; 63316ba94429SFrederic Weisbecker 63326ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 63336ba94429SFrederic Weisbecker if (!wq_dev) 63346ba94429SFrederic Weisbecker return -ENOMEM; 63356ba94429SFrederic Weisbecker 63366ba94429SFrederic Weisbecker wq_dev->wq = wq; 63376ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 63386ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 633923217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 63406ba94429SFrederic Weisbecker 63416ba94429SFrederic Weisbecker /* 63426ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 63436ba94429SFrederic Weisbecker * everything is ready. 63446ba94429SFrederic Weisbecker */ 63456ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 63466ba94429SFrederic Weisbecker 63476ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 63486ba94429SFrederic Weisbecker if (ret) { 6349537f4146SArvind Yadav put_device(&wq_dev->dev); 63506ba94429SFrederic Weisbecker wq->wq_dev = NULL; 63516ba94429SFrederic Weisbecker return ret; 63526ba94429SFrederic Weisbecker } 63536ba94429SFrederic Weisbecker 63546ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 63556ba94429SFrederic Weisbecker struct device_attribute *attr; 63566ba94429SFrederic Weisbecker 63576ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 63586ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 63596ba94429SFrederic Weisbecker if (ret) { 63606ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 63616ba94429SFrederic Weisbecker wq->wq_dev = NULL; 63626ba94429SFrederic Weisbecker return ret; 63636ba94429SFrederic Weisbecker } 63646ba94429SFrederic Weisbecker } 63656ba94429SFrederic Weisbecker } 63666ba94429SFrederic Weisbecker 63676ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 63686ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 63696ba94429SFrederic Weisbecker return 0; 63706ba94429SFrederic Weisbecker } 63716ba94429SFrederic Weisbecker 63726ba94429SFrederic Weisbecker /** 63736ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 63746ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 63756ba94429SFrederic Weisbecker * 63766ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 63776ba94429SFrederic Weisbecker */ 63786ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 63796ba94429SFrederic Weisbecker { 63806ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 63816ba94429SFrederic Weisbecker 63826ba94429SFrederic Weisbecker if (!wq->wq_dev) 63836ba94429SFrederic Weisbecker return; 63846ba94429SFrederic Weisbecker 63856ba94429SFrederic Weisbecker wq->wq_dev = NULL; 63866ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 63876ba94429SFrederic Weisbecker } 63886ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 63896ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 63906ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 63916ba94429SFrederic Weisbecker 639282607adcSTejun Heo /* 639382607adcSTejun Heo * Workqueue watchdog. 639482607adcSTejun Heo * 639582607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 639682607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 639782607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 639882607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 639982607adcSTejun Heo * largely opaque. 640082607adcSTejun Heo * 640182607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 640282607adcSTejun Heo * state if some pools failed to make forward progress for a while where 640382607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 640482607adcSTejun Heo * 640582607adcSTejun Heo * This mechanism is controlled through the kernel parameter 640682607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 640782607adcSTejun Heo * corresponding sysfs parameter file. 640882607adcSTejun Heo */ 640982607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 641082607adcSTejun Heo 641182607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 64125cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 641382607adcSTejun Heo 641482607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 641582607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 641682607adcSTejun Heo 6417cd2440d6SPetr Mladek /* 6418cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6419cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6420cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6421cd2440d6SPetr Mladek * in all other situations. 6422cd2440d6SPetr Mladek */ 6423cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6424cd2440d6SPetr Mladek { 6425cd2440d6SPetr Mladek struct worker *worker; 6426cd2440d6SPetr Mladek unsigned long flags; 6427cd2440d6SPetr Mladek int bkt; 6428cd2440d6SPetr Mladek 6429cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6430cd2440d6SPetr Mladek 6431cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6432cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6433cd2440d6SPetr Mladek /* 6434cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6435cd2440d6SPetr Mladek * drivers that queue work while holding locks 6436cd2440d6SPetr Mladek * also taken in their write paths. 6437cd2440d6SPetr Mladek */ 6438cd2440d6SPetr Mladek printk_deferred_enter(); 6439cd2440d6SPetr Mladek 6440cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6441cd2440d6SPetr Mladek sched_show_task(worker->task); 6442cd2440d6SPetr Mladek 6443cd2440d6SPetr Mladek printk_deferred_exit(); 6444cd2440d6SPetr Mladek } 6445cd2440d6SPetr Mladek } 6446cd2440d6SPetr Mladek 6447cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6448cd2440d6SPetr Mladek } 6449cd2440d6SPetr Mladek 6450cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6451cd2440d6SPetr Mladek { 6452cd2440d6SPetr Mladek struct worker_pool *pool; 6453cd2440d6SPetr Mladek int pi; 6454cd2440d6SPetr Mladek 6455cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6456cd2440d6SPetr Mladek 6457cd2440d6SPetr Mladek rcu_read_lock(); 6458cd2440d6SPetr Mladek 6459cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6460cd2440d6SPetr Mladek if (pool->cpu_stall) 6461cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6462cd2440d6SPetr Mladek 6463cd2440d6SPetr Mladek } 6464cd2440d6SPetr Mladek 6465cd2440d6SPetr Mladek rcu_read_unlock(); 6466cd2440d6SPetr Mladek } 6467cd2440d6SPetr Mladek 646882607adcSTejun Heo static void wq_watchdog_reset_touched(void) 646982607adcSTejun Heo { 647082607adcSTejun Heo int cpu; 647182607adcSTejun Heo 647282607adcSTejun Heo wq_watchdog_touched = jiffies; 647382607adcSTejun Heo for_each_possible_cpu(cpu) 647482607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 647582607adcSTejun Heo } 647682607adcSTejun Heo 64775cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 647882607adcSTejun Heo { 647982607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 648082607adcSTejun Heo bool lockup_detected = false; 6481cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6482940d71c6SSergey Senozhatsky unsigned long now = jiffies; 648382607adcSTejun Heo struct worker_pool *pool; 648482607adcSTejun Heo int pi; 648582607adcSTejun Heo 648682607adcSTejun Heo if (!thresh) 648782607adcSTejun Heo return; 648882607adcSTejun Heo 648982607adcSTejun Heo rcu_read_lock(); 649082607adcSTejun Heo 649182607adcSTejun Heo for_each_pool(pool, pi) { 649282607adcSTejun Heo unsigned long pool_ts, touched, ts; 649382607adcSTejun Heo 6494cd2440d6SPetr Mladek pool->cpu_stall = false; 649582607adcSTejun Heo if (list_empty(&pool->worklist)) 649682607adcSTejun Heo continue; 649782607adcSTejun Heo 6498940d71c6SSergey Senozhatsky /* 6499940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6500940d71c6SSergey Senozhatsky * the watchdog like a stall. 6501940d71c6SSergey Senozhatsky */ 6502940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6503940d71c6SSergey Senozhatsky 650482607adcSTejun Heo /* get the latest of pool and touched timestamps */ 650589e28ce6SWang Qing if (pool->cpu >= 0) 650689e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 650789e28ce6SWang Qing else 650882607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 650989e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 651082607adcSTejun Heo 651182607adcSTejun Heo if (time_after(pool_ts, touched)) 651282607adcSTejun Heo ts = pool_ts; 651382607adcSTejun Heo else 651482607adcSTejun Heo ts = touched; 651582607adcSTejun Heo 651682607adcSTejun Heo /* did we stall? */ 6517940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 651882607adcSTejun Heo lockup_detected = true; 6519cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6520cd2440d6SPetr Mladek pool->cpu_stall = true; 6521cd2440d6SPetr Mladek cpu_pool_stall = true; 6522cd2440d6SPetr Mladek } 652382607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 652482607adcSTejun Heo pr_cont_pool_info(pool); 652582607adcSTejun Heo pr_cont(" stuck for %us!\n", 6526940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 652782607adcSTejun Heo } 6528cd2440d6SPetr Mladek 6529cd2440d6SPetr Mladek 653082607adcSTejun Heo } 653182607adcSTejun Heo 653282607adcSTejun Heo rcu_read_unlock(); 653382607adcSTejun Heo 653482607adcSTejun Heo if (lockup_detected) 653555df0933SImran Khan show_all_workqueues(); 653682607adcSTejun Heo 6537cd2440d6SPetr Mladek if (cpu_pool_stall) 6538cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6539cd2440d6SPetr Mladek 654082607adcSTejun Heo wq_watchdog_reset_touched(); 654182607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 654282607adcSTejun Heo } 654382607adcSTejun Heo 6544cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 654582607adcSTejun Heo { 654682607adcSTejun Heo if (cpu >= 0) 654782607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 654889e28ce6SWang Qing 654982607adcSTejun Heo wq_watchdog_touched = jiffies; 655082607adcSTejun Heo } 655182607adcSTejun Heo 655282607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 655382607adcSTejun Heo { 655482607adcSTejun Heo wq_watchdog_thresh = 0; 655582607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 655682607adcSTejun Heo 655782607adcSTejun Heo if (thresh) { 655882607adcSTejun Heo wq_watchdog_thresh = thresh; 655982607adcSTejun Heo wq_watchdog_reset_touched(); 656082607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 656182607adcSTejun Heo } 656282607adcSTejun Heo } 656382607adcSTejun Heo 656482607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 656582607adcSTejun Heo const struct kernel_param *kp) 656682607adcSTejun Heo { 656782607adcSTejun Heo unsigned long thresh; 656882607adcSTejun Heo int ret; 656982607adcSTejun Heo 657082607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 657182607adcSTejun Heo if (ret) 657282607adcSTejun Heo return ret; 657382607adcSTejun Heo 657482607adcSTejun Heo if (system_wq) 657582607adcSTejun Heo wq_watchdog_set_thresh(thresh); 657682607adcSTejun Heo else 657782607adcSTejun Heo wq_watchdog_thresh = thresh; 657882607adcSTejun Heo 657982607adcSTejun Heo return 0; 658082607adcSTejun Heo } 658182607adcSTejun Heo 658282607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 658382607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 658482607adcSTejun Heo .get = param_get_ulong, 658582607adcSTejun Heo }; 658682607adcSTejun Heo 658782607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 658882607adcSTejun Heo 0644); 658982607adcSTejun Heo 659082607adcSTejun Heo static void wq_watchdog_init(void) 659182607adcSTejun Heo { 65925cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 659382607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 659482607adcSTejun Heo } 659582607adcSTejun Heo 659682607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 659782607adcSTejun Heo 659882607adcSTejun Heo static inline void wq_watchdog_init(void) { } 659982607adcSTejun Heo 660082607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 660182607adcSTejun Heo 66024a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 66034a6c5607STejun Heo { 66044a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 66054a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 66064a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 66074a6c5607STejun Heo return; 66084a6c5607STejun Heo } 66094a6c5607STejun Heo 66104a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 66114a6c5607STejun Heo } 66124a6c5607STejun Heo 66133347fa09STejun Heo /** 66143347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 66153347fa09STejun Heo * 66162930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 66172930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 66182930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 66192930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 66202930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 66212930155bSTejun Heo * before early initcalls. 66223347fa09STejun Heo */ 66232333e829SYu Chen void __init workqueue_init_early(void) 66241da177e4SLinus Torvalds { 662584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 66267a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 66277a4e344cSTejun Heo int i, cpu; 6628c34056a3STejun Heo 662910cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6630e904e6c2STejun Heo 6631b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 6632fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 6633fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 6634b05a7928SFrederic Weisbecker 66354a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 66364a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 66374a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 6638ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 66394a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 6640ace3c549Stiozhang 6641fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 66428b03ae3cSTejun Heo 66438b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6644e22bee78STejun Heo 66452930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 66462930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 66472930155bSTejun Heo 66487bd20b6bSMarcelo Tosatti /* 66497bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 66507bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 66517bd20b6bSMarcelo Tosatti */ 66527bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 66537bd20b6bSMarcelo Tosatti wq_power_efficient = true; 66547bd20b6bSMarcelo Tosatti 665584193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 665684193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 665784193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 665884193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 665984193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 666084193c07STejun Heo 666184193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 666284193c07STejun Heo 666384193c07STejun Heo pt->nr_pods = 1; 666484193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 666584193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 666684193c07STejun Heo pt->cpu_pod[0] = 0; 666784193c07STejun Heo 6668f02ae73aSTejun Heo /* initialize CPU pools */ 666924647570STejun Heo for_each_possible_cpu(cpu) { 6670ebf44d16STejun Heo struct worker_pool *pool; 6671e22bee78STejun Heo 66724ce62e9eSTejun Heo i = 0; 6673e22bee78STejun Heo for_each_cpu_worker_pool(pool, cpu) { 667429c91e99STejun Heo BUG_ON(init_worker_pool(pool)); 667529c91e99STejun Heo pool->cpu = cpu; 667629c91e99STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 66779546b29eSTejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 66781da177e4SLinus Torvalds pool->attrs->nice = std_nice[i++]; 66798639ecebSTejun Heo pool->attrs->affn_strict = true; 66801da177e4SLinus Torvalds pool->node = cpu_to_node(cpu); 66811da177e4SLinus Torvalds 66821da177e4SLinus Torvalds /* alloc pool ID */ 66831da177e4SLinus Torvalds mutex_lock(&wq_pool_mutex); 66841da177e4SLinus Torvalds BUG_ON(worker_pool_assign_id(pool)); 66851da177e4SLinus Torvalds mutex_unlock(&wq_pool_mutex); 668629c91e99STejun Heo } 668729c91e99STejun Heo } 668829c91e99STejun Heo 66898a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 669029c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 669129c91e99STejun Heo struct workqueue_attrs *attrs; 669229c91e99STejun Heo 6693be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 669429c91e99STejun Heo attrs->nice = std_nice[i]; 669529c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 66968a2b7538STejun Heo 66978a2b7538STejun Heo /* 66988a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 66998a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 67008a2b7538STejun Heo */ 6701be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 67028a2b7538STejun Heo attrs->nice = std_nice[i]; 6703af73f5c9STejun Heo attrs->ordered = true; 67048a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 670529c91e99STejun Heo } 670629c91e99STejun Heo 67071da177e4SLinus Torvalds system_wq = alloc_workqueue("events", 0, 0); 67081aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 67091da177e4SLinus Torvalds system_long_wq = alloc_workqueue("events_long", 0, 0); 67101da177e4SLinus Torvalds system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6711636b927eSTejun Heo WQ_MAX_ACTIVE); 67121da177e4SLinus Torvalds system_freezable_wq = alloc_workqueue("events_freezable", 67131da177e4SLinus Torvalds WQ_FREEZABLE, 0); 67140668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 67150668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 67168318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 67170668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 67180668106cSViresh Kumar 0); 67191aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 67200668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 67210668106cSViresh Kumar !system_power_efficient_wq || 67220668106cSViresh Kumar !system_freezable_power_efficient_wq); 67233347fa09STejun Heo } 67243347fa09STejun Heo 6725aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 6726aa6fde93STejun Heo { 6727aa6fde93STejun Heo unsigned long thresh; 6728aa6fde93STejun Heo unsigned long bogo; 6729aa6fde93STejun Heo 6730dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 6731dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 6732dd64c873SZqiang 6733aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 6734aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 6735aa6fde93STejun Heo return; 6736aa6fde93STejun Heo 6737aa6fde93STejun Heo /* 6738aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 6739aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 6740aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 6741aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 6742aa6fde93STejun Heo * too low. 6743aa6fde93STejun Heo * 6744aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 6745aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 6746aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 6747aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 6748aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 6749aa6fde93STejun Heo * usefulness. 6750aa6fde93STejun Heo */ 6751aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 6752aa6fde93STejun Heo 6753aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 6754aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 6755aa6fde93STejun Heo if (bogo < 4000) 6756aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 6757aa6fde93STejun Heo 6758aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 6759aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 6760aa6fde93STejun Heo 6761aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 6762aa6fde93STejun Heo } 6763aa6fde93STejun Heo 67643347fa09STejun Heo /** 67653347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 67663347fa09STejun Heo * 67672930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 67682930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 67692930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 67702930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 67712930155bSTejun Heo * workers and enable future kworker creations. 67723347fa09STejun Heo */ 67732333e829SYu Chen void __init workqueue_init(void) 67743347fa09STejun Heo { 67752186d9f9STejun Heo struct workqueue_struct *wq; 67763347fa09STejun Heo struct worker_pool *pool; 67773347fa09STejun Heo int cpu, bkt; 67783347fa09STejun Heo 6779aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 6780aa6fde93STejun Heo 67812186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 67822186d9f9STejun Heo 67832930155bSTejun Heo /* 67842930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 67852930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 67862930155bSTejun Heo */ 67872186d9f9STejun Heo for_each_possible_cpu(cpu) { 67882186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 67892186d9f9STejun Heo pool->node = cpu_to_node(cpu); 67902186d9f9STejun Heo } 67912186d9f9STejun Heo } 67922186d9f9STejun Heo 679340c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 679440c17f75STejun Heo WARN(init_rescuer(wq), 679540c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 679640c17f75STejun Heo wq->name); 679740c17f75STejun Heo } 67982186d9f9STejun Heo 67992186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 68002186d9f9STejun Heo 68013347fa09STejun Heo /* create the initial workers */ 68023347fa09STejun Heo for_each_online_cpu(cpu) { 68033347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 68043347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 68053347fa09STejun Heo BUG_ON(!create_worker(pool)); 68063347fa09STejun Heo } 68073347fa09STejun Heo } 68083347fa09STejun Heo 68093347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 68103347fa09STejun Heo BUG_ON(!create_worker(pool)); 68113347fa09STejun Heo 68123347fa09STejun Heo wq_online = true; 681382607adcSTejun Heo wq_watchdog_init(); 68141da177e4SLinus Torvalds } 6815c4f135d6STetsuo Handa 6816025e1684STejun Heo /* 6817025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 6818025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 6819025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 6820025e1684STejun Heo */ 6821025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 6822025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 6823025e1684STejun Heo { 6824025e1684STejun Heo int cur, pre, cpu, pod; 6825025e1684STejun Heo 6826025e1684STejun Heo pt->nr_pods = 0; 6827025e1684STejun Heo 6828025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 6829025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 6830025e1684STejun Heo BUG_ON(!pt->cpu_pod); 6831025e1684STejun Heo 6832025e1684STejun Heo for_each_possible_cpu(cur) { 6833025e1684STejun Heo for_each_possible_cpu(pre) { 6834025e1684STejun Heo if (pre >= cur) { 6835025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 6836025e1684STejun Heo break; 6837025e1684STejun Heo } 6838025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 6839025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 6840025e1684STejun Heo break; 6841025e1684STejun Heo } 6842025e1684STejun Heo } 6843025e1684STejun Heo } 6844025e1684STejun Heo 6845025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 6846025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 6847025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 6848025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 6849025e1684STejun Heo 6850025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 6851025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 6852025e1684STejun Heo 6853025e1684STejun Heo for_each_possible_cpu(cpu) { 6854025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 6855025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 6856025e1684STejun Heo } 6857025e1684STejun Heo } 6858025e1684STejun Heo 685963c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 686063c5484eSTejun Heo { 686163c5484eSTejun Heo return false; 686263c5484eSTejun Heo } 686363c5484eSTejun Heo 686463c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 686563c5484eSTejun Heo { 686663c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 686763c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 686863c5484eSTejun Heo #else 686963c5484eSTejun Heo return false; 687063c5484eSTejun Heo #endif 687163c5484eSTejun Heo } 687263c5484eSTejun Heo 6873025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 6874025e1684STejun Heo { 6875025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 6876025e1684STejun Heo } 6877025e1684STejun Heo 68782930155bSTejun Heo /** 68792930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 68802930155bSTejun Heo * 68812930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 68822930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 68832930155bSTejun Heo * initializes the unbound CPU pods accordingly. 68842930155bSTejun Heo */ 68852930155bSTejun Heo void __init workqueue_init_topology(void) 6886a86feae6STejun Heo { 68872930155bSTejun Heo struct workqueue_struct *wq; 6888025e1684STejun Heo int cpu; 6889a86feae6STejun Heo 689063c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 689163c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 689263c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 6893025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 6894a86feae6STejun Heo 68952930155bSTejun Heo mutex_lock(&wq_pool_mutex); 6896a86feae6STejun Heo 6897a86feae6STejun Heo /* 68982930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 68992930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 69002930155bSTejun Heo * combinations to apply per-pod sharing. 69012930155bSTejun Heo */ 69022930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 69032930155bSTejun Heo for_each_online_cpu(cpu) { 69042930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 69052930155bSTejun Heo } 69062930155bSTejun Heo } 69072930155bSTejun Heo 69082930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 6909a86feae6STejun Heo } 6910a86feae6STejun Heo 691120bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 691220bdedafSTetsuo Handa { 691320bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 691420bdedafSTetsuo Handa dump_stack(); 691520bdedafSTetsuo Handa } 6916c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 6917ace3c549Stiozhang 6918ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 6919ace3c549Stiozhang { 6920ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 6921ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 6922ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 6923ace3c549Stiozhang } 6924ace3c549Stiozhang 6925ace3c549Stiozhang return 1; 6926ace3c549Stiozhang } 6927ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 6928