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> 55e22bee78STejun Heo 56ea138446STejun Heo #include "workqueue_internal.h" 571da177e4SLinus Torvalds 58c8e55f36STejun Heo enum { 59bc2ae0f5STejun Heo /* 6024647570STejun Heo * worker_pool flags 61bc2ae0f5STejun Heo * 6224647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 63bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 64bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 65bc2ae0f5STejun Heo * is in effect. 66bc2ae0f5STejun Heo * 67bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 68bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 6924647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 70bc2ae0f5STejun Heo * 71bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 721258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 734736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 74bc2ae0f5STejun Heo */ 75692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7624647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 77db7bccf4STejun Heo 78c8e55f36STejun Heo /* worker flags */ 79c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 80c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 81e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 82fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 83f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 84a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 85e22bee78STejun Heo 86a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 87a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 88db7bccf4STejun Heo 89e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 904ce62e9eSTejun Heo 9129c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 92c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 93db7bccf4STejun Heo 94e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 95e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 96e22bee78STejun Heo 973233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 983233cdbdSTejun Heo /* call for help after 10ms 993233cdbdSTejun Heo (min two ticks) */ 100e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 101e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 104e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1058698a745SDongsheng Yang * all cpus. Give MIN_NICE. 106e22bee78STejun Heo */ 1078698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1088698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 109ecf6881fSTejun Heo 110ecf6881fSTejun Heo WQ_NAME_LEN = 24, 111c8e55f36STejun Heo }; 112c8e55f36STejun Heo 1131da177e4SLinus Torvalds /* 1144690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1154690c4abSTejun Heo * 116e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 117e41e704bSTejun Heo * everyone else. 1184690c4abSTejun Heo * 119e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 120e22bee78STejun Heo * only be modified and accessed from the local cpu. 121e22bee78STejun Heo * 122d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1234690c4abSTejun Heo * 124d565ed63STejun Heo * X: During normal operation, modification requires pool->lock and should 125d565ed63STejun Heo * be done only from local cpu. Either disabling preemption on local 126d565ed63STejun Heo * cpu or grabbing pool->lock is enough for read access. If 127d565ed63STejun Heo * POOL_DISASSOCIATED is set, it's identical to L. 128e22bee78STejun Heo * 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 * 1502e109a28STejun Heo * MD: wq_mayday_lock protected. 151cd2440d6SPetr Mladek * 152cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1534690c4abSTejun Heo */ 1544690c4abSTejun Heo 1552eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 156c34056a3STejun Heo 157bd7bdd43STejun Heo struct worker_pool { 158a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 159d84ff051STejun Heo int cpu; /* I: the associated cpu */ 160f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1619daf9e67STejun Heo int id; /* I: pool ID */ 16211ebea50STejun Heo unsigned int flags; /* X: flags */ 163bd7bdd43STejun Heo 16482607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 165cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 16682607adcSTejun Heo 167bc35f7efSLai Jiangshan /* 168bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 169bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 170bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 171bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 172bc35f7efSLai Jiangshan */ 173bc35f7efSLai Jiangshan int nr_running; 17484f91c62SLai Jiangshan 175bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 176ea1abd61SLai Jiangshan 1775826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1785826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 179bd7bdd43STejun Heo 1802c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 181bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1823f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1833f959aa3SValentin Schneider 184bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 185bd7bdd43STejun Heo 186c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 187c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 188c9e7cf27STejun Heo /* L: hash of busy workers */ 189c9e7cf27STejun Heo 1902607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 19192f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 192e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 19360f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 194e19e397aSTejun Heo 1957cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 196e19e397aSTejun Heo 1977a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 19868e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 19968e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2007a4e344cSTejun Heo 201e19e397aSTejun Heo /* 20224acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 20329c91e99STejun Heo * from get_work_pool(). 20429c91e99STejun Heo */ 20529c91e99STejun Heo struct rcu_head rcu; 20684f91c62SLai Jiangshan }; 2078b03ae3cSTejun Heo 2088b03ae3cSTejun Heo /* 209725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 210725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 211725e8ec5STejun Heo */ 212725e8ec5STejun Heo enum pool_workqueue_stats { 213725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 214725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 215*616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 216725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 217725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 218725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 219725e8ec5STejun Heo 220725e8ec5STejun Heo PWQ_NR_STATS, 221725e8ec5STejun Heo }; 222725e8ec5STejun Heo 223725e8ec5STejun Heo /* 224112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 225112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 226112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 227112202d9STejun Heo * number of flag bits. 2281da177e4SLinus Torvalds */ 229112202d9STejun Heo struct pool_workqueue { 230bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2314690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 23273f53c4aSTejun Heo int work_color; /* L: current color */ 23373f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2348864b4e5STejun Heo int refcnt; /* L: reference count */ 23573f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 23673f53c4aSTejun Heo /* L: nr of in_flight works */ 237018f3a13SLai Jiangshan 238018f3a13SLai Jiangshan /* 239018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 240018f3a13SLai Jiangshan * 241018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 242018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 243018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 244018f3a13SLai Jiangshan * 245018f3a13SLai Jiangshan * All work items marked with WORK_STRUCT_INACTIVE do not participate 246018f3a13SLai Jiangshan * in pwq->nr_active and all work items in pwq->inactive_works are 247018f3a13SLai Jiangshan * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE 248018f3a13SLai Jiangshan * work items are in pwq->inactive_works. Some of them are ready to 249018f3a13SLai Jiangshan * run in pool->worklist or worker->scheduled. Those work itmes are 250018f3a13SLai Jiangshan * only struct wq_barrier which is used for flush_work() and should 251018f3a13SLai Jiangshan * not participate in pwq->nr_active. For non-barrier work item, it 252018f3a13SLai Jiangshan * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 253018f3a13SLai Jiangshan */ 2541e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 255a0a1a5fdSTejun Heo int max_active; /* L: max active works */ 256f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2573c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2582e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2598864b4e5STejun Heo 260725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 261725e8ec5STejun Heo 2628864b4e5STejun Heo /* 2638864b4e5STejun Heo * Release of unbound pwq is punted to system_wq. See put_pwq() 2648864b4e5STejun Heo * and pwq_unbound_release_workfn() for details. pool_workqueue 26524acfb71SThomas Gleixner * itself is also RCU protected so that the first pwq can be 266b09f4fd3SLai Jiangshan * determined without grabbing wq->mutex. 2678864b4e5STejun Heo */ 2688864b4e5STejun Heo struct work_struct unbound_release_work; 2698864b4e5STejun Heo struct rcu_head rcu; 270e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /* 27373f53c4aSTejun Heo * Structure used to wait for workqueue flush. 27473f53c4aSTejun Heo */ 27573f53c4aSTejun Heo struct wq_flusher { 2763c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2773c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 27873f53c4aSTejun Heo struct completion done; /* flush completion */ 27973f53c4aSTejun Heo }; 2801da177e4SLinus Torvalds 281226223abSTejun Heo struct wq_device; 282226223abSTejun Heo 28373f53c4aSTejun Heo /* 284c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 285c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2861da177e4SLinus Torvalds */ 2871da177e4SLinus Torvalds struct workqueue_struct { 2883c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 289e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 29073f53c4aSTejun Heo 2913c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 2923c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 2933c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 294112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 2953c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 2963c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 2973c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 29873f53c4aSTejun Heo 2992e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 30030ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 301e22bee78STejun Heo 30287fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 303a357fc03SLai Jiangshan int saved_max_active; /* WQ: saved pwq max_active */ 304226223abSTejun Heo 3055b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3065b95e1afSLai Jiangshan struct pool_workqueue *dfl_pwq; /* PW: only for unbound wqs */ 3076029a918STejun Heo 308226223abSTejun Heo #ifdef CONFIG_SYSFS 309226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 310226223abSTejun Heo #endif 3114e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 312669de8bdSBart Van Assche char *lock_name; 313669de8bdSBart Van Assche struct lock_class_key key; 3144e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3154e6045f1SJohannes Berg #endif 316ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3172728fd2fSTejun Heo 318e2dca7adSTejun Heo /* 31924acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 32024acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 321e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 322e2dca7adSTejun Heo */ 323e2dca7adSTejun Heo struct rcu_head rcu; 324e2dca7adSTejun Heo 3252728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3262728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 3272728fd2fSTejun Heo struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 3285b95e1afSLai Jiangshan struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 3291da177e4SLinus Torvalds }; 3301da177e4SLinus Torvalds 331e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 332e904e6c2STejun Heo 333bce90380STejun Heo static cpumask_var_t *wq_numa_possible_cpumask; 334bce90380STejun Heo /* possible CPUs of each node */ 335bce90380STejun Heo 336*616db877STejun Heo /* 337*616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 338*616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 339*616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 340*616db877STejun Heo */ 341*616db877STejun Heo static unsigned long wq_cpu_intensive_thresh_us = 10000; 342*616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 343*616db877STejun Heo 344d55262c4STejun Heo static bool wq_disable_numa; 345d55262c4STejun Heo module_param_named(disable_numa, wq_disable_numa, bool, 0444); 346d55262c4STejun Heo 347cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 348552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 349cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 350cee22a15SViresh Kumar 351863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3523347fa09STejun Heo 353bce90380STejun Heo static bool wq_numa_enabled; /* unbound NUMA affinity enabled */ 354bce90380STejun Heo 3554c16bd32STejun Heo /* buf for wq_update_unbound_numa_attrs(), protected by CPU hotplug exclusion */ 3564c16bd32STejun Heo static struct workqueue_attrs *wq_update_unbound_numa_attrs_buf; 3574c16bd32STejun Heo 35868e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3591258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 360a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 361d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 362d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3635bcab335STejun Heo 364e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 36568e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3667d19c5ceSTejun Heo 36799c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 368ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 369ef557180SMike Galbraith 370ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 371ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 372b05a7928SFrederic Weisbecker 373f303fccbSTejun Heo /* 374f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 375f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 376f303fccbSTejun Heo * to uncover usages which depend on it. 377f303fccbSTejun Heo */ 378f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 379f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 380f303fccbSTejun Heo #else 381f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 382f303fccbSTejun Heo #endif 383f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 384f303fccbSTejun Heo 3857d19c5ceSTejun Heo /* the per-cpu worker pools */ 38625528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 3877d19c5ceSTejun Heo 38868e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 3897d19c5ceSTejun Heo 39068e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 39129c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 39229c91e99STejun Heo 393c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 39429c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 39529c91e99STejun Heo 3968a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 3978a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 3988a2b7538STejun Heo 399d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 400ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 401044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 4021aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 403044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 404d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 405044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 406f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 407044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 40824d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 4090668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 4100668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 4110668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 4120668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 413d320c038STejun Heo 4147d19c5ceSTejun Heo static int worker_thread(void *__worker); 4156ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 416c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 41755df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4187d19c5ceSTejun Heo 41997bd2347STejun Heo #define CREATE_TRACE_POINTS 42097bd2347STejun Heo #include <trace/events/workqueue.h> 42197bd2347STejun Heo 42268e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 42324acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 424f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 42524acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4265bcab335STejun Heo 4275b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 42824acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 429f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 430f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 43124acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4325b95e1afSLai Jiangshan 433f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 434f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 435f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4367a62c2c8STejun Heo (pool)++) 4374ce62e9eSTejun Heo 43849e3cf44STejun Heo /** 43917116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 44017116969STejun Heo * @pool: iteration cursor 441611c92a0STejun Heo * @pi: integer used for iteration 442fa1b54e6STejun Heo * 44324acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 44468e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 44568e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 446fa1b54e6STejun Heo * 447fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 448fa1b54e6STejun Heo * ignored. 44917116969STejun Heo */ 450611c92a0STejun Heo #define for_each_pool(pool, pi) \ 451611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 45268e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 453fa1b54e6STejun Heo else 45417116969STejun Heo 45517116969STejun Heo /** 456822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 457822d8405STejun Heo * @worker: iteration cursor 458822d8405STejun Heo * @pool: worker_pool to iterate workers of 459822d8405STejun Heo * 4601258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 461822d8405STejun Heo * 462822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 463822d8405STejun Heo * ignored. 464822d8405STejun Heo */ 465da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 466da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 4671258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 468822d8405STejun Heo else 469822d8405STejun Heo 470822d8405STejun Heo /** 47149e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 47249e3cf44STejun Heo * @pwq: iteration cursor 47349e3cf44STejun Heo * @wq: the target workqueue 47476af4d93STejun Heo * 47524acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 476794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 477794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 47876af4d93STejun Heo * 47976af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 48076af4d93STejun Heo * ignored. 48149e3cf44STejun Heo */ 48249e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 48349e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 4845a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 485f3421797STejun Heo 486dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 487dc186ad7SThomas Gleixner 488f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 489dc186ad7SThomas Gleixner 49099777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 49199777288SStanislaw Gruszka { 49299777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 49399777288SStanislaw Gruszka } 49499777288SStanislaw Gruszka 495b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 496b9fdac7fSDu, Changbin { 497b9fdac7fSDu, Changbin struct work_struct *work = addr; 498b9fdac7fSDu, Changbin 499b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 500b9fdac7fSDu, Changbin } 501b9fdac7fSDu, Changbin 502dc186ad7SThomas Gleixner /* 503dc186ad7SThomas Gleixner * fixup_init is called when: 504dc186ad7SThomas Gleixner * - an active object is initialized 505dc186ad7SThomas Gleixner */ 50602a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 507dc186ad7SThomas Gleixner { 508dc186ad7SThomas Gleixner struct work_struct *work = addr; 509dc186ad7SThomas Gleixner 510dc186ad7SThomas Gleixner switch (state) { 511dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 512dc186ad7SThomas Gleixner cancel_work_sync(work); 513dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 51402a982a6SDu, Changbin return true; 515dc186ad7SThomas Gleixner default: 51602a982a6SDu, Changbin return false; 517dc186ad7SThomas Gleixner } 518dc186ad7SThomas Gleixner } 519dc186ad7SThomas Gleixner 520dc186ad7SThomas Gleixner /* 521dc186ad7SThomas Gleixner * fixup_free is called when: 522dc186ad7SThomas Gleixner * - an active object is freed 523dc186ad7SThomas Gleixner */ 52402a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 525dc186ad7SThomas Gleixner { 526dc186ad7SThomas Gleixner struct work_struct *work = addr; 527dc186ad7SThomas Gleixner 528dc186ad7SThomas Gleixner switch (state) { 529dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 530dc186ad7SThomas Gleixner cancel_work_sync(work); 531dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 53202a982a6SDu, Changbin return true; 533dc186ad7SThomas Gleixner default: 53402a982a6SDu, Changbin return false; 535dc186ad7SThomas Gleixner } 536dc186ad7SThomas Gleixner } 537dc186ad7SThomas Gleixner 538f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 539dc186ad7SThomas Gleixner .name = "work_struct", 54099777288SStanislaw Gruszka .debug_hint = work_debug_hint, 541b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 542dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 543dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 544dc186ad7SThomas Gleixner }; 545dc186ad7SThomas Gleixner 546dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 547dc186ad7SThomas Gleixner { 548dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 549dc186ad7SThomas Gleixner } 550dc186ad7SThomas Gleixner 551dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 552dc186ad7SThomas Gleixner { 553dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 554dc186ad7SThomas Gleixner } 555dc186ad7SThomas Gleixner 556dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 557dc186ad7SThomas Gleixner { 558dc186ad7SThomas Gleixner if (onstack) 559dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 560dc186ad7SThomas Gleixner else 561dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 562dc186ad7SThomas Gleixner } 563dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 564dc186ad7SThomas Gleixner 565dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 566dc186ad7SThomas Gleixner { 567dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 568dc186ad7SThomas Gleixner } 569dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 570dc186ad7SThomas Gleixner 571ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 572ea2e64f2SThomas Gleixner { 573ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 574ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 575ea2e64f2SThomas Gleixner } 576ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 577ea2e64f2SThomas Gleixner 578dc186ad7SThomas Gleixner #else 579dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 580dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 581dc186ad7SThomas Gleixner #endif 582dc186ad7SThomas Gleixner 5834e8b22bdSLi Bin /** 58467dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 5854e8b22bdSLi Bin * @pool: the pool pointer of interest 5864e8b22bdSLi Bin * 5874e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 5884e8b22bdSLi Bin * successfully, -errno on failure. 5894e8b22bdSLi Bin */ 5909daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 5919daf9e67STejun Heo { 5929daf9e67STejun Heo int ret; 5939daf9e67STejun Heo 59468e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5955bcab335STejun Heo 5964e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 5974e8b22bdSLi Bin GFP_KERNEL); 598229641a6STejun Heo if (ret >= 0) { 599e68035fbSTejun Heo pool->id = ret; 600229641a6STejun Heo return 0; 601229641a6STejun Heo } 6029daf9e67STejun Heo return ret; 6039daf9e67STejun Heo } 6049daf9e67STejun Heo 60576af4d93STejun Heo /** 606df2d5ae4STejun Heo * unbound_pwq_by_node - return the unbound pool_workqueue for the given node 607df2d5ae4STejun Heo * @wq: the target workqueue 608df2d5ae4STejun Heo * @node: the node ID 609df2d5ae4STejun Heo * 61024acfb71SThomas Gleixner * This must be called with any of wq_pool_mutex, wq->mutex or RCU 6115b95e1afSLai Jiangshan * read locked. 612df2d5ae4STejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 613df2d5ae4STejun Heo * responsible for guaranteeing that the pwq stays online. 614d185af30SYacine Belkadi * 615d185af30SYacine Belkadi * Return: The unbound pool_workqueue for @node. 616df2d5ae4STejun Heo */ 617df2d5ae4STejun Heo static struct pool_workqueue *unbound_pwq_by_node(struct workqueue_struct *wq, 618df2d5ae4STejun Heo int node) 619df2d5ae4STejun Heo { 6205b95e1afSLai Jiangshan assert_rcu_or_wq_mutex_or_pool_mutex(wq); 621d6e022f1STejun Heo 622d6e022f1STejun Heo /* 623d6e022f1STejun Heo * XXX: @node can be NUMA_NO_NODE if CPU goes offline while a 624d6e022f1STejun Heo * delayed item is pending. The plan is to keep CPU -> NODE 625d6e022f1STejun Heo * mapping valid and stable across CPU on/offlines. Once that 626d6e022f1STejun Heo * happens, this workaround can be removed. 627d6e022f1STejun Heo */ 628d6e022f1STejun Heo if (unlikely(node == NUMA_NO_NODE)) 629d6e022f1STejun Heo return wq->dfl_pwq; 630d6e022f1STejun Heo 631df2d5ae4STejun Heo return rcu_dereference_raw(wq->numa_pwq_tbl[node]); 632df2d5ae4STejun Heo } 633df2d5ae4STejun Heo 63473f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 63573f53c4aSTejun Heo { 63673f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 63773f53c4aSTejun Heo } 63873f53c4aSTejun Heo 639c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 64073f53c4aSTejun Heo { 641c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 64273f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 64373f53c4aSTejun Heo } 64473f53c4aSTejun Heo 64573f53c4aSTejun Heo static int work_next_color(int color) 64673f53c4aSTejun Heo { 64773f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 648a848e3b6SOleg Nesterov } 649a848e3b6SOleg Nesterov 6504594bf15SDavid Howells /* 651112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 652112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6537c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6547a22ad75STejun Heo * 655112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 656112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 657bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 658bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6597a22ad75STejun Heo * 660112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6617c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 662112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6637c3eed5cSTejun Heo * available only while the work item is queued. 664bbb68dfaSTejun Heo * 665bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 666bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 667bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 668bbb68dfaSTejun Heo * try to steal the PENDING bit. 6694594bf15SDavid Howells */ 6707a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6717a22ad75STejun Heo unsigned long flags) 6727a22ad75STejun Heo { 6736183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 6747a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 6757a22ad75STejun Heo } 6767a22ad75STejun Heo 677112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 6784690c4abSTejun Heo unsigned long extra_flags) 679365970a1SDavid Howells { 680112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 681112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 682365970a1SDavid Howells } 683365970a1SDavid Howells 6844468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 6854468a00fSLai Jiangshan int pool_id) 6864468a00fSLai Jiangshan { 6874468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 6884468a00fSLai Jiangshan WORK_STRUCT_PENDING); 6894468a00fSLai Jiangshan } 6904468a00fSLai Jiangshan 6917c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 6927c3eed5cSTejun Heo int pool_id) 6934d707b9fSOleg Nesterov { 69423657bb1STejun Heo /* 69523657bb1STejun Heo * The following wmb is paired with the implied mb in 69623657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 69723657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 69823657bb1STejun Heo * owner. 69923657bb1STejun Heo */ 70023657bb1STejun Heo smp_wmb(); 7017c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 702346c09f8SRoman Pen /* 703346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 704346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 705346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7068bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 707346c09f8SRoman Pen * the same @work. E.g. consider this case: 708346c09f8SRoman Pen * 709346c09f8SRoman Pen * CPU#0 CPU#1 710346c09f8SRoman Pen * ---------------------------- -------------------------------- 711346c09f8SRoman Pen * 712346c09f8SRoman Pen * 1 STORE event_indicated 713346c09f8SRoman Pen * 2 queue_work_on() { 714346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 715346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 716346c09f8SRoman Pen * 5 set_work_data() # clear bit 717346c09f8SRoman Pen * 6 smp_mb() 718346c09f8SRoman Pen * 7 work->current_func() { 719346c09f8SRoman Pen * 8 LOAD event_indicated 720346c09f8SRoman Pen * } 721346c09f8SRoman Pen * 722346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 723346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 724346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 725346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 726346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 727346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 728346c09f8SRoman Pen * before actual STORE. 729346c09f8SRoman Pen */ 730346c09f8SRoman Pen smp_mb(); 7314d707b9fSOleg Nesterov } 7324d707b9fSOleg Nesterov 7337a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 734365970a1SDavid Howells { 7357c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7367c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7377a22ad75STejun Heo } 7387a22ad75STejun Heo 739112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7407a22ad75STejun Heo { 741e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7427a22ad75STejun Heo 743112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 744e120153dSTejun Heo return (void *)(data & WORK_STRUCT_WQ_DATA_MASK); 745e120153dSTejun Heo else 746e120153dSTejun Heo return NULL; 7477a22ad75STejun Heo } 7487a22ad75STejun Heo 7497c3eed5cSTejun Heo /** 7507c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7517c3eed5cSTejun Heo * @work: the work item of interest 7527c3eed5cSTejun Heo * 75368e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 75424acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 75524acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 756fa1b54e6STejun Heo * 757fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 758fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 759fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 760fa1b54e6STejun Heo * returned pool is and stays online. 761d185af30SYacine Belkadi * 762d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7637c3eed5cSTejun Heo */ 7647c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7657a22ad75STejun Heo { 766e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7677c3eed5cSTejun Heo int pool_id; 7687a22ad75STejun Heo 76968e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 770fa1b54e6STejun Heo 771112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 772112202d9STejun Heo return ((struct pool_workqueue *) 7737c3eed5cSTejun Heo (data & WORK_STRUCT_WQ_DATA_MASK))->pool; 7747a22ad75STejun Heo 7757c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 7767c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 7777a22ad75STejun Heo return NULL; 7787a22ad75STejun Heo 779fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 7807c3eed5cSTejun Heo } 7817c3eed5cSTejun Heo 7827c3eed5cSTejun Heo /** 7837c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 7847c3eed5cSTejun Heo * @work: the work item of interest 7857c3eed5cSTejun Heo * 786d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 7877c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 7887c3eed5cSTejun Heo */ 7897c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 7907c3eed5cSTejun Heo { 79154d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 7927c3eed5cSTejun Heo 793112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 794112202d9STejun Heo return ((struct pool_workqueue *) 79554d5b7d0SLai Jiangshan (data & WORK_STRUCT_WQ_DATA_MASK))->pool->id; 79654d5b7d0SLai Jiangshan 79754d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 7987c3eed5cSTejun Heo } 7997c3eed5cSTejun Heo 800bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 801bbb68dfaSTejun Heo { 8027c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 803bbb68dfaSTejun Heo 8047c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8057c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 806bbb68dfaSTejun Heo } 807bbb68dfaSTejun Heo 808bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 809bbb68dfaSTejun Heo { 810bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 811bbb68dfaSTejun Heo 812112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 813bbb68dfaSTejun Heo } 814bbb68dfaSTejun Heo 815e22bee78STejun Heo /* 8163270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8173270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 818d565ed63STejun Heo * they're being called with pool->lock held. 819e22bee78STejun Heo */ 820e22bee78STejun Heo 82163d95a91STejun Heo static bool __need_more_worker(struct worker_pool *pool) 822649027d7STejun Heo { 823bc35f7efSLai Jiangshan return !pool->nr_running; 824649027d7STejun Heo } 825649027d7STejun Heo 826e22bee78STejun Heo /* 827e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 828e22bee78STejun Heo * running workers. 829974271c4STejun Heo * 830974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 831706026c2STejun Heo * function will always return %true for unbound pools as long as the 832974271c4STejun Heo * worklist isn't empty. 833e22bee78STejun Heo */ 83463d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 835e22bee78STejun Heo { 83663d95a91STejun Heo return !list_empty(&pool->worklist) && __need_more_worker(pool); 837e22bee78STejun Heo } 838e22bee78STejun Heo 839e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 84063d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 841e22bee78STejun Heo { 84263d95a91STejun Heo return pool->nr_idle; 843e22bee78STejun Heo } 844e22bee78STejun Heo 845e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 84663d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 847e22bee78STejun Heo { 848bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 849e22bee78STejun Heo } 850e22bee78STejun Heo 851e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 85263d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 853e22bee78STejun Heo { 85463d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 855e22bee78STejun Heo } 856e22bee78STejun Heo 857e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 85863d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 859e22bee78STejun Heo { 860692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 86163d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 86263d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 863e22bee78STejun Heo 864e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 865e22bee78STejun Heo } 866e22bee78STejun Heo 867e22bee78STejun Heo /* 868e22bee78STejun Heo * Wake up functions. 869e22bee78STejun Heo */ 870e22bee78STejun Heo 8712c1f1a91SLai Jiangshan /* Return the first idle worker. Called with pool->lock held. */ 8721037de36SLai Jiangshan static struct worker *first_idle_worker(struct worker_pool *pool) 8737e11629dSTejun Heo { 87463d95a91STejun Heo if (unlikely(list_empty(&pool->idle_list))) 8757e11629dSTejun Heo return NULL; 8767e11629dSTejun Heo 87763d95a91STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 8787e11629dSTejun Heo } 8797e11629dSTejun Heo 8807e11629dSTejun Heo /** 8817e11629dSTejun Heo * wake_up_worker - wake up an idle worker 88263d95a91STejun Heo * @pool: worker pool to wake worker from 8837e11629dSTejun Heo * 88463d95a91STejun Heo * Wake up the first idle worker of @pool. 8857e11629dSTejun Heo * 8867e11629dSTejun Heo * CONTEXT: 887a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 8887e11629dSTejun Heo */ 88963d95a91STejun Heo static void wake_up_worker(struct worker_pool *pool) 8907e11629dSTejun Heo { 8911037de36SLai Jiangshan struct worker *worker = first_idle_worker(pool); 8927e11629dSTejun Heo 8937e11629dSTejun Heo if (likely(worker)) 8947e11629dSTejun Heo wake_up_process(worker->task); 8957e11629dSTejun Heo } 8967e11629dSTejun Heo 8974690c4abSTejun Heo /** 898c54d5046STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 899c54d5046STejun Heo * @worker: self 900c54d5046STejun Heo * @flags: flags to set 901c54d5046STejun Heo * 902c54d5046STejun Heo * Set @flags in @worker->flags and adjust nr_running accordingly. 903c54d5046STejun Heo * 904c54d5046STejun Heo * CONTEXT: 905c54d5046STejun Heo * raw_spin_lock_irq(pool->lock) 906c54d5046STejun Heo */ 907c54d5046STejun Heo static inline void worker_set_flags(struct worker *worker, unsigned int flags) 908c54d5046STejun Heo { 909c54d5046STejun Heo struct worker_pool *pool = worker->pool; 910c54d5046STejun Heo 911c54d5046STejun Heo WARN_ON_ONCE(worker->task != current); 912c54d5046STejun Heo 913c54d5046STejun Heo /* If transitioning into NOT_RUNNING, adjust nr_running. */ 914c54d5046STejun Heo if ((flags & WORKER_NOT_RUNNING) && 915c54d5046STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 916c54d5046STejun Heo pool->nr_running--; 917c54d5046STejun Heo } 918c54d5046STejun Heo 919c54d5046STejun Heo worker->flags |= flags; 920c54d5046STejun Heo } 921c54d5046STejun Heo 922c54d5046STejun Heo /** 923c54d5046STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 924c54d5046STejun Heo * @worker: self 925c54d5046STejun Heo * @flags: flags to clear 926c54d5046STejun Heo * 927c54d5046STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 928c54d5046STejun Heo * 929c54d5046STejun Heo * CONTEXT: 930c54d5046STejun Heo * raw_spin_lock_irq(pool->lock) 931c54d5046STejun Heo */ 932c54d5046STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 933c54d5046STejun Heo { 934c54d5046STejun Heo struct worker_pool *pool = worker->pool; 935c54d5046STejun Heo unsigned int oflags = worker->flags; 936c54d5046STejun Heo 937c54d5046STejun Heo WARN_ON_ONCE(worker->task != current); 938c54d5046STejun Heo 939c54d5046STejun Heo worker->flags &= ~flags; 940c54d5046STejun Heo 941c54d5046STejun Heo /* 942c54d5046STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 943c54d5046STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 944c54d5046STejun Heo * of multiple flags, not a single flag. 945c54d5046STejun Heo */ 946c54d5046STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 947c54d5046STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 948c54d5046STejun Heo pool->nr_running++; 949c54d5046STejun Heo } 950c54d5046STejun Heo 951c54d5046STejun Heo /** 9526d25be57SThomas Gleixner * wq_worker_running - a worker is running again 953e22bee78STejun Heo * @task: task waking up 954e22bee78STejun Heo * 9556d25be57SThomas Gleixner * This function is called when a worker returns from schedule() 956e22bee78STejun Heo */ 9576d25be57SThomas Gleixner void wq_worker_running(struct task_struct *task) 958e22bee78STejun Heo { 959e22bee78STejun Heo struct worker *worker = kthread_data(task); 960e22bee78STejun Heo 9616d25be57SThomas Gleixner if (!worker->sleeping) 9626d25be57SThomas Gleixner return; 96307edfeceSFrederic Weisbecker 96407edfeceSFrederic Weisbecker /* 96507edfeceSFrederic Weisbecker * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 96607edfeceSFrederic Weisbecker * and the nr_running increment below, we may ruin the nr_running reset 96707edfeceSFrederic Weisbecker * and leave with an unexpected pool->nr_running == 1 on the newly unbound 96807edfeceSFrederic Weisbecker * pool. Protect against such race. 96907edfeceSFrederic Weisbecker */ 97007edfeceSFrederic Weisbecker preempt_disable(); 9716d25be57SThomas Gleixner if (!(worker->flags & WORKER_NOT_RUNNING)) 972bc35f7efSLai Jiangshan worker->pool->nr_running++; 97307edfeceSFrederic Weisbecker preempt_enable(); 974*616db877STejun Heo 975*616db877STejun Heo /* 976*616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 977*616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 978*616db877STejun Heo */ 979*616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 980*616db877STejun Heo 9816d25be57SThomas Gleixner worker->sleeping = 0; 98236576000SJoonsoo Kim } 983e22bee78STejun Heo 984e22bee78STejun Heo /** 985e22bee78STejun Heo * wq_worker_sleeping - a worker is going to sleep 986e22bee78STejun Heo * @task: task going to sleep 987e22bee78STejun Heo * 9886d25be57SThomas Gleixner * This function is called from schedule() when a busy worker is 989ccf45156SLai Jiangshan * going to sleep. 990e22bee78STejun Heo */ 9916d25be57SThomas Gleixner void wq_worker_sleeping(struct task_struct *task) 992e22bee78STejun Heo { 993cc5bff38SLai Jiangshan struct worker *worker = kthread_data(task); 994111c225aSTejun Heo struct worker_pool *pool; 995e22bee78STejun Heo 996111c225aSTejun Heo /* 997111c225aSTejun Heo * Rescuers, which may not have all the fields set up like normal 998111c225aSTejun Heo * workers, also reach here, let's not access anything before 999111c225aSTejun Heo * checking NOT_RUNNING. 1000111c225aSTejun Heo */ 10012d64672eSSteven Rostedt if (worker->flags & WORKER_NOT_RUNNING) 10026d25be57SThomas Gleixner return; 1003e22bee78STejun Heo 1004111c225aSTejun Heo pool = worker->pool; 1005111c225aSTejun Heo 100662849a96SSebastian Andrzej Siewior /* Return if preempted before wq_worker_running() was reached */ 100762849a96SSebastian Andrzej Siewior if (worker->sleeping) 10086d25be57SThomas Gleixner return; 10096d25be57SThomas Gleixner 10106d25be57SThomas Gleixner worker->sleeping = 1; 1011a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 1012e22bee78STejun Heo 1013e22bee78STejun Heo /* 101445c753f5SFrederic Weisbecker * Recheck in case unbind_workers() preempted us. We don't 101545c753f5SFrederic Weisbecker * want to decrement nr_running after the worker is unbound 101645c753f5SFrederic Weisbecker * and nr_running has been reset. 101745c753f5SFrederic Weisbecker */ 101845c753f5SFrederic Weisbecker if (worker->flags & WORKER_NOT_RUNNING) { 101945c753f5SFrederic Weisbecker raw_spin_unlock_irq(&pool->lock); 102045c753f5SFrederic Weisbecker return; 102145c753f5SFrederic Weisbecker } 102245c753f5SFrederic Weisbecker 1023bc35f7efSLai Jiangshan pool->nr_running--; 1024725e8ec5STejun Heo if (need_more_worker(pool)) { 1025725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1026cc5bff38SLai Jiangshan wake_up_worker(pool); 1027725e8ec5STejun Heo } 1028a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 1029e22bee78STejun Heo } 1030e22bee78STejun Heo 1031e22bee78STejun Heo /** 1032*616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1033*616db877STejun Heo * @task: task currently running 1034*616db877STejun Heo * 1035*616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1036*616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1037*616db877STejun Heo */ 1038*616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1039*616db877STejun Heo { 1040*616db877STejun Heo struct worker *worker = kthread_data(task); 1041*616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1042*616db877STejun Heo struct worker_pool *pool = worker->pool; 1043*616db877STejun Heo 1044*616db877STejun Heo if (!pwq) 1045*616db877STejun Heo return; 1046*616db877STejun Heo 1047*616db877STejun Heo /* 1048*616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1049*616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1050*616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1051*616db877STejun Heo */ 1052*616db877STejun Heo if ((worker->flags & WORKER_NOT_RUNNING) || 1053*616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1054*616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1055*616db877STejun Heo return; 1056*616db877STejun Heo 1057*616db877STejun Heo raw_spin_lock(&pool->lock); 1058*616db877STejun Heo 1059*616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 1060*616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1061*616db877STejun Heo 1062*616db877STejun Heo if (need_more_worker(pool)) { 1063*616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1064*616db877STejun Heo wake_up_worker(pool); 1065*616db877STejun Heo } 1066*616db877STejun Heo 1067*616db877STejun Heo raw_spin_unlock(&pool->lock); 1068*616db877STejun Heo } 1069*616db877STejun Heo 1070*616db877STejun Heo /** 10711b69ac6bSJohannes Weiner * wq_worker_last_func - retrieve worker's last work function 10728194fe94SBart Van Assche * @task: Task to retrieve last work function of. 10731b69ac6bSJohannes Weiner * 10741b69ac6bSJohannes Weiner * Determine the last function a worker executed. This is called from 10751b69ac6bSJohannes Weiner * the scheduler to get a worker's last known identity. 10761b69ac6bSJohannes Weiner * 10771b69ac6bSJohannes Weiner * CONTEXT: 1078a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(rq->lock) 10791b69ac6bSJohannes Weiner * 10804b047002SJohannes Weiner * This function is called during schedule() when a kworker is going 10814b047002SJohannes Weiner * to sleep. It's used by psi to identify aggregation workers during 10824b047002SJohannes Weiner * dequeuing, to allow periodic aggregation to shut-off when that 10834b047002SJohannes Weiner * worker is the last task in the system or cgroup to go to sleep. 10844b047002SJohannes Weiner * 10854b047002SJohannes Weiner * As this function doesn't involve any workqueue-related locking, it 10864b047002SJohannes Weiner * only returns stable values when called from inside the scheduler's 10874b047002SJohannes Weiner * queuing and dequeuing paths, when @task, which must be a kworker, 10884b047002SJohannes Weiner * is guaranteed to not be processing any works. 10894b047002SJohannes Weiner * 10901b69ac6bSJohannes Weiner * Return: 10911b69ac6bSJohannes Weiner * The last work function %current executed as a worker, NULL if it 10921b69ac6bSJohannes Weiner * hasn't executed any work yet. 10931b69ac6bSJohannes Weiner */ 10941b69ac6bSJohannes Weiner work_func_t wq_worker_last_func(struct task_struct *task) 10951b69ac6bSJohannes Weiner { 10961b69ac6bSJohannes Weiner struct worker *worker = kthread_data(task); 10971b69ac6bSJohannes Weiner 10981b69ac6bSJohannes Weiner return worker->last_func; 10991b69ac6bSJohannes Weiner } 11001b69ac6bSJohannes Weiner 11011b69ac6bSJohannes Weiner /** 11028cca0eeaSTejun Heo * find_worker_executing_work - find worker which is executing a work 1103c9e7cf27STejun Heo * @pool: pool of interest 11048cca0eeaSTejun Heo * @work: work to find worker for 11058cca0eeaSTejun Heo * 1106c9e7cf27STejun Heo * Find a worker which is executing @work on @pool by searching 1107c9e7cf27STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1108a2c1c57bSTejun Heo * to match, its current execution should match the address of @work and 1109a2c1c57bSTejun Heo * its work function. This is to avoid unwanted dependency between 1110a2c1c57bSTejun Heo * unrelated work executions through a work item being recycled while still 1111a2c1c57bSTejun Heo * being executed. 1112a2c1c57bSTejun Heo * 1113a2c1c57bSTejun Heo * This is a bit tricky. A work item may be freed once its execution 1114a2c1c57bSTejun Heo * starts and nothing prevents the freed area from being recycled for 1115a2c1c57bSTejun Heo * another work item. If the same work item address ends up being reused 1116a2c1c57bSTejun Heo * before the original execution finishes, workqueue will identify the 1117a2c1c57bSTejun Heo * recycled work item as currently executing and make it wait until the 1118a2c1c57bSTejun Heo * current execution finishes, introducing an unwanted dependency. 1119a2c1c57bSTejun Heo * 1120c5aa87bbSTejun Heo * This function checks the work item address and work function to avoid 1121c5aa87bbSTejun Heo * false positives. Note that this isn't complete as one may construct a 1122c5aa87bbSTejun Heo * work function which can introduce dependency onto itself through a 1123c5aa87bbSTejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1124c5aa87bbSTejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1125c5aa87bbSTejun Heo * actually occurs, it should be easy to locate the culprit work function. 11268cca0eeaSTejun Heo * 11278cca0eeaSTejun Heo * CONTEXT: 1128a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 11298cca0eeaSTejun Heo * 1130d185af30SYacine Belkadi * Return: 1131d185af30SYacine Belkadi * Pointer to worker which is executing @work if found, %NULL 11328cca0eeaSTejun Heo * otherwise. 11338cca0eeaSTejun Heo */ 1134c9e7cf27STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 11358cca0eeaSTejun Heo struct work_struct *work) 11368cca0eeaSTejun Heo { 113742f8570fSSasha Levin struct worker *worker; 113842f8570fSSasha Levin 1139b67bfe0dSSasha Levin hash_for_each_possible(pool->busy_hash, worker, hentry, 1140a2c1c57bSTejun Heo (unsigned long)work) 1141a2c1c57bSTejun Heo if (worker->current_work == work && 1142a2c1c57bSTejun Heo worker->current_func == work->func) 114342f8570fSSasha Levin return worker; 114442f8570fSSasha Levin 114542f8570fSSasha Levin return NULL; 11468cca0eeaSTejun Heo } 11478cca0eeaSTejun Heo 11488cca0eeaSTejun Heo /** 1149bf4ede01STejun Heo * move_linked_works - move linked works to a list 1150bf4ede01STejun Heo * @work: start of series of works to be scheduled 1151bf4ede01STejun Heo * @head: target list to append @work to 1152402dd89dSShailendra Verma * @nextp: out parameter for nested worklist walking 1153bf4ede01STejun Heo * 1154bf4ede01STejun Heo * Schedule linked works starting from @work to @head. Work series to 1155bf4ede01STejun Heo * be scheduled starts at @work and includes any consecutive work with 1156bf4ede01STejun Heo * WORK_STRUCT_LINKED set in its predecessor. 1157bf4ede01STejun Heo * 1158bf4ede01STejun Heo * If @nextp is not NULL, it's updated to point to the next work of 1159bf4ede01STejun Heo * the last scheduled work. This allows move_linked_works() to be 1160bf4ede01STejun Heo * nested inside outer list_for_each_entry_safe(). 1161bf4ede01STejun Heo * 1162bf4ede01STejun Heo * CONTEXT: 1163a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1164bf4ede01STejun Heo */ 1165bf4ede01STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1166bf4ede01STejun Heo struct work_struct **nextp) 1167bf4ede01STejun Heo { 1168bf4ede01STejun Heo struct work_struct *n; 1169bf4ede01STejun Heo 1170bf4ede01STejun Heo /* 1171bf4ede01STejun Heo * Linked worklist will always end before the end of the list, 1172bf4ede01STejun Heo * use NULL for list head. 1173bf4ede01STejun Heo */ 1174bf4ede01STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1175bf4ede01STejun Heo list_move_tail(&work->entry, head); 1176bf4ede01STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1177bf4ede01STejun Heo break; 1178bf4ede01STejun Heo } 1179bf4ede01STejun Heo 1180bf4ede01STejun Heo /* 1181bf4ede01STejun Heo * If we're already inside safe list traversal and have moved 1182bf4ede01STejun Heo * multiple works to the scheduled queue, the next position 1183bf4ede01STejun Heo * needs to be updated. 1184bf4ede01STejun Heo */ 1185bf4ede01STejun Heo if (nextp) 1186bf4ede01STejun Heo *nextp = n; 1187bf4ede01STejun Heo } 1188bf4ede01STejun Heo 11898864b4e5STejun Heo /** 11908864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 11918864b4e5STejun Heo * @pwq: pool_workqueue to get 11928864b4e5STejun Heo * 11938864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 11948864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 11958864b4e5STejun Heo */ 11968864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 11978864b4e5STejun Heo { 11988864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 11998864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 12008864b4e5STejun Heo pwq->refcnt++; 12018864b4e5STejun Heo } 12028864b4e5STejun Heo 12038864b4e5STejun Heo /** 12048864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 12058864b4e5STejun Heo * @pwq: pool_workqueue to put 12068864b4e5STejun Heo * 12078864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 12088864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 12098864b4e5STejun Heo */ 12108864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 12118864b4e5STejun Heo { 12128864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 12138864b4e5STejun Heo if (likely(--pwq->refcnt)) 12148864b4e5STejun Heo return; 12158864b4e5STejun Heo if (WARN_ON_ONCE(!(pwq->wq->flags & WQ_UNBOUND))) 12168864b4e5STejun Heo return; 12178864b4e5STejun Heo /* 12188864b4e5STejun Heo * @pwq can't be released under pool->lock, bounce to 12198864b4e5STejun Heo * pwq_unbound_release_workfn(). This never recurses on the same 12208864b4e5STejun Heo * pool->lock as this path is taken only for unbound workqueues and 12218864b4e5STejun Heo * the release work item is scheduled on a per-cpu workqueue. To 12228864b4e5STejun Heo * avoid lockdep warning, unbound pool->locks are given lockdep 12238864b4e5STejun Heo * subclass of 1 in get_unbound_pool(). 12248864b4e5STejun Heo */ 12258864b4e5STejun Heo schedule_work(&pwq->unbound_release_work); 12268864b4e5STejun Heo } 12278864b4e5STejun Heo 1228dce90d47STejun Heo /** 1229dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1230dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1231dce90d47STejun Heo * 1232dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1233dce90d47STejun Heo */ 1234dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1235dce90d47STejun Heo { 1236dce90d47STejun Heo if (pwq) { 1237dce90d47STejun Heo /* 123824acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1239dce90d47STejun Heo * following lock operations are safe. 1240dce90d47STejun Heo */ 1241a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1242dce90d47STejun Heo put_pwq(pwq); 1243a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1244dce90d47STejun Heo } 1245dce90d47STejun Heo } 1246dce90d47STejun Heo 1247f97a4a1aSLai Jiangshan static void pwq_activate_inactive_work(struct work_struct *work) 1248bf4ede01STejun Heo { 1249112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 1250bf4ede01STejun Heo 1251bf4ede01STejun Heo trace_workqueue_activate_work(work); 125282607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 125382607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1254112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 1255f97a4a1aSLai Jiangshan __clear_bit(WORK_STRUCT_INACTIVE_BIT, work_data_bits(work)); 1256112202d9STejun Heo pwq->nr_active++; 1257bf4ede01STejun Heo } 1258bf4ede01STejun Heo 1259f97a4a1aSLai Jiangshan static void pwq_activate_first_inactive(struct pool_workqueue *pwq) 12603aa62497SLai Jiangshan { 1261f97a4a1aSLai Jiangshan struct work_struct *work = list_first_entry(&pwq->inactive_works, 12623aa62497SLai Jiangshan struct work_struct, entry); 12633aa62497SLai Jiangshan 1264f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 12653aa62497SLai Jiangshan } 12663aa62497SLai Jiangshan 1267bf4ede01STejun Heo /** 1268112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1269112202d9STejun Heo * @pwq: pwq of interest 1270c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1271bf4ede01STejun Heo * 1272bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1273112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1274bf4ede01STejun Heo * 1275bf4ede01STejun Heo * CONTEXT: 1276a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1277bf4ede01STejun Heo */ 1278c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1279bf4ede01STejun Heo { 1280c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1281c4560c2cSLai Jiangshan 1282018f3a13SLai Jiangshan if (!(work_data & WORK_STRUCT_INACTIVE)) { 1283112202d9STejun Heo pwq->nr_active--; 1284f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 1285f97a4a1aSLai Jiangshan /* one down, submit an inactive one */ 1286112202d9STejun Heo if (pwq->nr_active < pwq->max_active) 1287f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 1288bf4ede01STejun Heo } 1289018f3a13SLai Jiangshan } 1290018f3a13SLai Jiangshan 1291018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1292bf4ede01STejun Heo 1293bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1294112202d9STejun Heo if (likely(pwq->flush_color != color)) 12958864b4e5STejun Heo goto out_put; 1296bf4ede01STejun Heo 1297bf4ede01STejun Heo /* are there still in-flight works? */ 1298112202d9STejun Heo if (pwq->nr_in_flight[color]) 12998864b4e5STejun Heo goto out_put; 1300bf4ede01STejun Heo 1301112202d9STejun Heo /* this pwq is done, clear flush_color */ 1302112202d9STejun Heo pwq->flush_color = -1; 1303bf4ede01STejun Heo 1304bf4ede01STejun Heo /* 1305112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1306bf4ede01STejun Heo * will handle the rest. 1307bf4ede01STejun Heo */ 1308112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1309112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 13108864b4e5STejun Heo out_put: 13118864b4e5STejun Heo put_pwq(pwq); 1312bf4ede01STejun Heo } 1313bf4ede01STejun Heo 131436e227d2STejun Heo /** 1315bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 131636e227d2STejun Heo * @work: work item to steal 131736e227d2STejun Heo * @is_dwork: @work is a delayed_work 1318bbb68dfaSTejun Heo * @flags: place to store irq state 131936e227d2STejun Heo * 132036e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1321d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 132236e227d2STejun Heo * 1323d185af30SYacine Belkadi * Return: 13243eb6b31bSMauro Carvalho Chehab * 13253eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 132636e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 132736e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 132836e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1329bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1330bbb68dfaSTejun Heo * for arbitrarily long 13313eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 133236e227d2STejun Heo * 1333d185af30SYacine Belkadi * Note: 1334bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1335e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1336e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1337e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1338bbb68dfaSTejun Heo * 1339bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1340bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1341bbb68dfaSTejun Heo * 1342e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1343bf4ede01STejun Heo */ 1344bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1345bbb68dfaSTejun Heo unsigned long *flags) 1346bf4ede01STejun Heo { 1347d565ed63STejun Heo struct worker_pool *pool; 1348112202d9STejun Heo struct pool_workqueue *pwq; 1349bf4ede01STejun Heo 1350bbb68dfaSTejun Heo local_irq_save(*flags); 1351bbb68dfaSTejun Heo 135236e227d2STejun Heo /* try to steal the timer if it exists */ 135336e227d2STejun Heo if (is_dwork) { 135436e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 135536e227d2STejun Heo 1356e0aecdd8STejun Heo /* 1357e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1358e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1359e0aecdd8STejun Heo * running on the local CPU. 1360e0aecdd8STejun Heo */ 136136e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 136236e227d2STejun Heo return 1; 136336e227d2STejun Heo } 136436e227d2STejun Heo 136536e227d2STejun Heo /* try to claim PENDING the normal way */ 1366bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1367bf4ede01STejun Heo return 0; 1368bf4ede01STejun Heo 136924acfb71SThomas Gleixner rcu_read_lock(); 1370bf4ede01STejun Heo /* 1371bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1372bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1373bf4ede01STejun Heo */ 1374d565ed63STejun Heo pool = get_work_pool(work); 1375d565ed63STejun Heo if (!pool) 1376bbb68dfaSTejun Heo goto fail; 1377bf4ede01STejun Heo 1378a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1379bf4ede01STejun Heo /* 1380112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1381112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1382112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1383112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1384112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 13850b3dae68SLai Jiangshan * item is currently queued on that pool. 1386bf4ede01STejun Heo */ 1387112202d9STejun Heo pwq = get_work_pwq(work); 1388112202d9STejun Heo if (pwq && pwq->pool == pool) { 1389bf4ede01STejun Heo debug_work_deactivate(work); 13903aa62497SLai Jiangshan 13913aa62497SLai Jiangshan /* 1392018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1393018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1394018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1395018f3a13SLai Jiangshan * 1396f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1397d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1398f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 139916062836STejun Heo * management later on and cause stall. Make sure the work 140016062836STejun Heo * item is activated before grabbing. 14013aa62497SLai Jiangshan */ 1402f97a4a1aSLai Jiangshan if (*work_data_bits(work) & WORK_STRUCT_INACTIVE) 1403f97a4a1aSLai Jiangshan pwq_activate_inactive_work(work); 14043aa62497SLai Jiangshan 1405bf4ede01STejun Heo list_del_init(&work->entry); 1406c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 140736e227d2STejun Heo 1408112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 14094468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 14104468a00fSLai Jiangshan 1411a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 141224acfb71SThomas Gleixner rcu_read_unlock(); 141336e227d2STejun Heo return 1; 1414bf4ede01STejun Heo } 1415a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1416bbb68dfaSTejun Heo fail: 141724acfb71SThomas Gleixner rcu_read_unlock(); 1418bbb68dfaSTejun Heo local_irq_restore(*flags); 1419bbb68dfaSTejun Heo if (work_is_canceling(work)) 1420bbb68dfaSTejun Heo return -ENOENT; 1421bbb68dfaSTejun Heo cpu_relax(); 142236e227d2STejun Heo return -EAGAIN; 1423bf4ede01STejun Heo } 1424bf4ede01STejun Heo 1425bf4ede01STejun Heo /** 1426706026c2STejun Heo * insert_work - insert a work into a pool 1427112202d9STejun Heo * @pwq: pwq @work belongs to 14284690c4abSTejun Heo * @work: work to insert 14294690c4abSTejun Heo * @head: insertion point 14304690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 14314690c4abSTejun Heo * 1432112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1433706026c2STejun Heo * work_struct flags. 14344690c4abSTejun Heo * 14354690c4abSTejun Heo * CONTEXT: 1436a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1437365970a1SDavid Howells */ 1438112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1439112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1440b89deed3SOleg Nesterov { 1441112202d9STejun Heo struct worker_pool *pool = pwq->pool; 1442e1d8aa9fSFrederic Weisbecker 1443e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1444f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1445e89a85d6SWalter Wu 14464690c4abSTejun Heo /* we own @work, set data and link */ 1447112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 14481a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 14498864b4e5STejun Heo get_pwq(pwq); 1450e22bee78STejun Heo 145163d95a91STejun Heo if (__need_more_worker(pool)) 145263d95a91STejun Heo wake_up_worker(pool); 1453b89deed3SOleg Nesterov } 1454b89deed3SOleg Nesterov 1455c8efcc25STejun Heo /* 1456c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 14578d03ecfeSTejun Heo * same workqueue. 1458c8efcc25STejun Heo */ 1459c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1460c8efcc25STejun Heo { 1461c8efcc25STejun Heo struct worker *worker; 1462c8efcc25STejun Heo 14638d03ecfeSTejun Heo worker = current_wq_worker(); 1464c8efcc25STejun Heo /* 1465bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 14668d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1467c8efcc25STejun Heo */ 1468112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1469c8efcc25STejun Heo } 1470c8efcc25STejun Heo 1471ef557180SMike Galbraith /* 1472ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1473ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1474ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1475ef557180SMike Galbraith */ 1476ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1477ef557180SMike Galbraith { 1478ef557180SMike Galbraith int new_cpu; 1479ef557180SMike Galbraith 1480f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1481ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1482ef557180SMike Galbraith return cpu; 1483a8ec5880SAmmar Faizi } else { 1484a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1485f303fccbSTejun Heo } 1486f303fccbSTejun Heo 1487ef557180SMike Galbraith if (cpumask_empty(wq_unbound_cpumask)) 1488ef557180SMike Galbraith return cpu; 1489ef557180SMike Galbraith 1490ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1491ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1492ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1493ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1494ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1495ef557180SMike Galbraith return cpu; 1496ef557180SMike Galbraith } 1497ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1498ef557180SMike Galbraith 1499ef557180SMike Galbraith return new_cpu; 1500ef557180SMike Galbraith } 1501ef557180SMike Galbraith 1502d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 15031da177e4SLinus Torvalds struct work_struct *work) 15041da177e4SLinus Torvalds { 1505112202d9STejun Heo struct pool_workqueue *pwq; 1506c9178087STejun Heo struct worker_pool *last_pool; 15071e19ffc6STejun Heo struct list_head *worklist; 15088a2e8e5dSTejun Heo unsigned int work_flags; 1509b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 15108930cabaSTejun Heo 15118930cabaSTejun Heo /* 15128930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 15138930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 15148930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 15158930cabaSTejun Heo * happen with IRQ disabled. 15168930cabaSTejun Heo */ 15178e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 15181da177e4SLinus Torvalds 15191e19ffc6STejun Heo 152033e3f0a3SRichard Clark /* 152133e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 152233e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 152333e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 152433e3f0a3SRichard Clark */ 152533e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 152633e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1527e41e704bSTejun Heo return; 152824acfb71SThomas Gleixner rcu_read_lock(); 15299e8cd2f5STejun Heo retry: 1530aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1531aa202f1fSHillf Danton if (wq->flags & WQ_UNBOUND) { 1532df2d5ae4STejun Heo if (req_cpu == WORK_CPU_UNBOUND) 1533ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1534df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 1535aa202f1fSHillf Danton } else { 1536aa202f1fSHillf Danton if (req_cpu == WORK_CPU_UNBOUND) 1537aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1538aa202f1fSHillf Danton pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 1539aa202f1fSHillf Danton } 1540f3421797STejun Heo 154118aa9effSTejun Heo /* 1542c9178087STejun Heo * If @work was previously on a different pool, it might still be 1543c9178087STejun Heo * running there, in which case the work needs to be queued on that 1544c9178087STejun Heo * pool to guarantee non-reentrancy. 154518aa9effSTejun Heo */ 1546c9e7cf27STejun Heo last_pool = get_work_pool(work); 1547112202d9STejun Heo if (last_pool && last_pool != pwq->pool) { 154818aa9effSTejun Heo struct worker *worker; 154918aa9effSTejun Heo 1550a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 155118aa9effSTejun Heo 1552c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 155318aa9effSTejun Heo 1554112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1555c9178087STejun Heo pwq = worker->current_pwq; 15568594fadeSLai Jiangshan } else { 155718aa9effSTejun Heo /* meh... not running there, queue here */ 1558a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1559a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 156018aa9effSTejun Heo } 15618930cabaSTejun Heo } else { 1562a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pwq->pool->lock); 15638930cabaSTejun Heo } 1564502ca9d8STejun Heo 15659e8cd2f5STejun Heo /* 15669e8cd2f5STejun Heo * pwq is determined and locked. For unbound pools, we could have 15679e8cd2f5STejun Heo * raced with pwq release and it could already be dead. If its 15689e8cd2f5STejun Heo * refcnt is zero, repeat pwq selection. Note that pwqs never die 1569df2d5ae4STejun Heo * without another pwq replacing it in the numa_pwq_tbl or while 1570df2d5ae4STejun Heo * work items are executing on it, so the retrying is guaranteed to 15719e8cd2f5STejun Heo * make forward-progress. 15729e8cd2f5STejun Heo */ 15739e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 15749e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1575a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 15769e8cd2f5STejun Heo cpu_relax(); 15779e8cd2f5STejun Heo goto retry; 15789e8cd2f5STejun Heo } 15799e8cd2f5STejun Heo /* oops */ 15809e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 15819e8cd2f5STejun Heo wq->name, cpu); 15829e8cd2f5STejun Heo } 15839e8cd2f5STejun Heo 1584112202d9STejun Heo /* pwq determined, queue */ 1585112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1586502ca9d8STejun Heo 158724acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 158824acfb71SThomas Gleixner goto out; 15891e19ffc6STejun Heo 1590112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1591112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 15921e19ffc6STejun Heo 1593112202d9STejun Heo if (likely(pwq->nr_active < pwq->max_active)) { 1594cdadf009STejun Heo trace_workqueue_activate_work(work); 1595112202d9STejun Heo pwq->nr_active++; 1596112202d9STejun Heo worklist = &pwq->pool->worklist; 159782607adcSTejun Heo if (list_empty(worklist)) 159882607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 15998a2e8e5dSTejun Heo } else { 1600f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1601f97a4a1aSLai Jiangshan worklist = &pwq->inactive_works; 16028a2e8e5dSTejun Heo } 16031e19ffc6STejun Heo 16040687c66bSZqiang debug_work_activate(work); 1605112202d9STejun Heo insert_work(pwq, work, worklist, work_flags); 16061e19ffc6STejun Heo 160724acfb71SThomas Gleixner out: 1608a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pwq->pool->lock); 160924acfb71SThomas Gleixner rcu_read_unlock(); 16101da177e4SLinus Torvalds } 16111da177e4SLinus Torvalds 16120fcb78c2SRolf Eike Beer /** 1613c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1614c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1615c1a220e7SZhang Rui * @wq: workqueue to use 1616c1a220e7SZhang Rui * @work: work to queue 1617c1a220e7SZhang Rui * 1618c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1619443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1620443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1621854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1622854f5cc5SPaul E. McKenney * online will get a splat. 1623d185af30SYacine Belkadi * 1624d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1625c1a220e7SZhang Rui */ 1626d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1627d4283e93STejun Heo struct work_struct *work) 1628c1a220e7SZhang Rui { 1629d4283e93STejun Heo bool ret = false; 16308930cabaSTejun Heo unsigned long flags; 16318930cabaSTejun Heo 16328930cabaSTejun Heo local_irq_save(flags); 1633c1a220e7SZhang Rui 163422df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 16354690c4abSTejun Heo __queue_work(cpu, wq, work); 1636d4283e93STejun Heo ret = true; 1637c1a220e7SZhang Rui } 16388930cabaSTejun Heo 16398930cabaSTejun Heo local_irq_restore(flags); 1640c1a220e7SZhang Rui return ret; 1641c1a220e7SZhang Rui } 1642ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1643c1a220e7SZhang Rui 16448204e0c1SAlexander Duyck /** 16458204e0c1SAlexander Duyck * workqueue_select_cpu_near - Select a CPU based on NUMA node 16468204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 16478204e0c1SAlexander Duyck * 16488204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 16498204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 16508204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 16518204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 16528204e0c1SAlexander Duyck */ 16538204e0c1SAlexander Duyck static int workqueue_select_cpu_near(int node) 16548204e0c1SAlexander Duyck { 16558204e0c1SAlexander Duyck int cpu; 16568204e0c1SAlexander Duyck 16578204e0c1SAlexander Duyck /* No point in doing this if NUMA isn't enabled for workqueues */ 16588204e0c1SAlexander Duyck if (!wq_numa_enabled) 16598204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 16608204e0c1SAlexander Duyck 16618204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 16628204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 16638204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 16648204e0c1SAlexander Duyck 16658204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 16668204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 16678204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 16688204e0c1SAlexander Duyck return cpu; 16698204e0c1SAlexander Duyck 16708204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 16718204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 16728204e0c1SAlexander Duyck 16738204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 16748204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 16758204e0c1SAlexander Duyck } 16768204e0c1SAlexander Duyck 16778204e0c1SAlexander Duyck /** 16788204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 16798204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 16808204e0c1SAlexander Duyck * @wq: workqueue to use 16818204e0c1SAlexander Duyck * @work: work to queue 16828204e0c1SAlexander Duyck * 16838204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 16848204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 16858204e0c1SAlexander Duyck * NUMA node. 16868204e0c1SAlexander Duyck * 16878204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 16888204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 16898204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 16908204e0c1SAlexander Duyck * 16918204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 16928204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 16938204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 16948204e0c1SAlexander Duyck * 16958204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 16968204e0c1SAlexander Duyck */ 16978204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 16988204e0c1SAlexander Duyck struct work_struct *work) 16998204e0c1SAlexander Duyck { 17008204e0c1SAlexander Duyck unsigned long flags; 17018204e0c1SAlexander Duyck bool ret = false; 17028204e0c1SAlexander Duyck 17038204e0c1SAlexander Duyck /* 17048204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 17058204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 17068204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 17078204e0c1SAlexander Duyck * 17088204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 17098204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 17108204e0c1SAlexander Duyck * some round robin type logic. 17118204e0c1SAlexander Duyck */ 17128204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 17138204e0c1SAlexander Duyck 17148204e0c1SAlexander Duyck local_irq_save(flags); 17158204e0c1SAlexander Duyck 17168204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 17178204e0c1SAlexander Duyck int cpu = workqueue_select_cpu_near(node); 17188204e0c1SAlexander Duyck 17198204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 17208204e0c1SAlexander Duyck ret = true; 17218204e0c1SAlexander Duyck } 17228204e0c1SAlexander Duyck 17238204e0c1SAlexander Duyck local_irq_restore(flags); 17248204e0c1SAlexander Duyck return ret; 17258204e0c1SAlexander Duyck } 17268204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 17278204e0c1SAlexander Duyck 17288c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 17291da177e4SLinus Torvalds { 17308c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 17311da177e4SLinus Torvalds 1732e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 173360c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 17341da177e4SLinus Torvalds } 17351438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 17361da177e4SLinus Torvalds 17377beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 173852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 17391da177e4SLinus Torvalds { 17407beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 17417beb2edfSTejun Heo struct work_struct *work = &dwork->work; 17421da177e4SLinus Torvalds 1743637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 17444b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 1745fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 1746fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 17477beb2edfSTejun Heo 17488852aac2STejun Heo /* 17498852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 17508852aac2STejun Heo * both optimization and correctness. The earliest @timer can 17518852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 17528852aac2STejun Heo * on that there's no such delay when @delay is 0. 17538852aac2STejun Heo */ 17548852aac2STejun Heo if (!delay) { 17558852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 17568852aac2STejun Heo return; 17578852aac2STejun Heo } 17588852aac2STejun Heo 175960c057bcSLai Jiangshan dwork->wq = wq; 17601265057fSTejun Heo dwork->cpu = cpu; 17617beb2edfSTejun Heo timer->expires = jiffies + delay; 17627beb2edfSTejun Heo 1763041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 17647beb2edfSTejun Heo add_timer_on(timer, cpu); 1765041bd12eSTejun Heo else 1766041bd12eSTejun Heo add_timer(timer); 17677beb2edfSTejun Heo } 17681da177e4SLinus Torvalds 17690fcb78c2SRolf Eike Beer /** 17700fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 17710fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 17720fcb78c2SRolf Eike Beer * @wq: workqueue to use 1773af9997e4SRandy Dunlap * @dwork: work to queue 17740fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 17750fcb78c2SRolf Eike Beer * 1776d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 1777715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 1778715f1300STejun Heo * execution. 17790fcb78c2SRolf Eike Beer */ 1780d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 178152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 17827a6bc1cdSVenkatesh Pallipadi { 178352bad64dSDavid Howells struct work_struct *work = &dwork->work; 1784d4283e93STejun Heo bool ret = false; 17858930cabaSTejun Heo unsigned long flags; 17868930cabaSTejun Heo 17878930cabaSTejun Heo /* read the comment in __queue_work() */ 17888930cabaSTejun Heo local_irq_save(flags); 17897a6bc1cdSVenkatesh Pallipadi 179022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 17917beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 1792d4283e93STejun Heo ret = true; 17937a6bc1cdSVenkatesh Pallipadi } 17948930cabaSTejun Heo 17958930cabaSTejun Heo local_irq_restore(flags); 17967a6bc1cdSVenkatesh Pallipadi return ret; 17977a6bc1cdSVenkatesh Pallipadi } 1798ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 17991da177e4SLinus Torvalds 1800c8e55f36STejun Heo /** 18018376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 18028376fe22STejun Heo * @cpu: CPU number to execute work on 18038376fe22STejun Heo * @wq: workqueue to use 18048376fe22STejun Heo * @dwork: work to queue 18058376fe22STejun Heo * @delay: number of jiffies to wait before queueing 18068376fe22STejun Heo * 18078376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 18088376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 18098376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 18108376fe22STejun Heo * current state. 18118376fe22STejun Heo * 1812d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 18138376fe22STejun Heo * pending and its timer was modified. 18148376fe22STejun Heo * 1815e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 18168376fe22STejun Heo * See try_to_grab_pending() for details. 18178376fe22STejun Heo */ 18188376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 18198376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 18208376fe22STejun Heo { 18218376fe22STejun Heo unsigned long flags; 18228376fe22STejun Heo int ret; 18238376fe22STejun Heo 18248376fe22STejun Heo do { 18258376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 18268376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 18278376fe22STejun Heo 18288376fe22STejun Heo if (likely(ret >= 0)) { 18298376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 18308376fe22STejun Heo local_irq_restore(flags); 18318376fe22STejun Heo } 18328376fe22STejun Heo 18338376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 18348376fe22STejun Heo return ret; 18358376fe22STejun Heo } 18368376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 18378376fe22STejun Heo 183805f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 183905f0fe6bSTejun Heo { 184005f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 184105f0fe6bSTejun Heo 184205f0fe6bSTejun Heo /* read the comment in __queue_work() */ 184305f0fe6bSTejun Heo local_irq_disable(); 184405f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 184505f0fe6bSTejun Heo local_irq_enable(); 184605f0fe6bSTejun Heo } 184705f0fe6bSTejun Heo 184805f0fe6bSTejun Heo /** 184905f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 185005f0fe6bSTejun Heo * @wq: workqueue to use 185105f0fe6bSTejun Heo * @rwork: work to queue 185205f0fe6bSTejun Heo * 185305f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 185405f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 1855bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 185605f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 185705f0fe6bSTejun Heo */ 185805f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 185905f0fe6bSTejun Heo { 186005f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 186105f0fe6bSTejun Heo 186205f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 186305f0fe6bSTejun Heo rwork->wq = wq; 1864a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 186505f0fe6bSTejun Heo return true; 186605f0fe6bSTejun Heo } 186705f0fe6bSTejun Heo 186805f0fe6bSTejun Heo return false; 186905f0fe6bSTejun Heo } 187005f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 187105f0fe6bSTejun Heo 18728376fe22STejun Heo /** 1873c8e55f36STejun Heo * worker_enter_idle - enter idle state 1874c8e55f36STejun Heo * @worker: worker which is entering idle state 1875c8e55f36STejun Heo * 1876c8e55f36STejun Heo * @worker is entering idle state. Update stats and idle timer if 1877c8e55f36STejun Heo * necessary. 1878c8e55f36STejun Heo * 1879c8e55f36STejun Heo * LOCKING: 1880a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1881c8e55f36STejun Heo */ 1882c8e55f36STejun Heo static void worker_enter_idle(struct worker *worker) 18831da177e4SLinus Torvalds { 1884bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1885c8e55f36STejun Heo 18866183c009STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 18876183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 18886183c009STejun Heo (worker->hentry.next || worker->hentry.pprev))) 18896183c009STejun Heo return; 1890c8e55f36STejun Heo 1891051e1850SLai Jiangshan /* can't use worker_set_flags(), also called from create_worker() */ 1892cb444766STejun Heo worker->flags |= WORKER_IDLE; 1893bd7bdd43STejun Heo pool->nr_idle++; 1894e22bee78STejun Heo worker->last_active = jiffies; 1895c8e55f36STejun Heo 1896c8e55f36STejun Heo /* idle_list is LIFO */ 1897bd7bdd43STejun Heo list_add(&worker->entry, &pool->idle_list); 1898db7bccf4STejun Heo 189963d95a91STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1900628c78e7STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1901cb444766STejun Heo 1902989442d7SLai Jiangshan /* Sanity check nr_running. */ 1903bc35f7efSLai Jiangshan WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1904c8e55f36STejun Heo } 1905c8e55f36STejun Heo 1906c8e55f36STejun Heo /** 1907c8e55f36STejun Heo * worker_leave_idle - leave idle state 1908c8e55f36STejun Heo * @worker: worker which is leaving idle state 1909c8e55f36STejun Heo * 1910c8e55f36STejun Heo * @worker is leaving idle state. Update stats. 1911c8e55f36STejun Heo * 1912c8e55f36STejun Heo * LOCKING: 1913a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1914c8e55f36STejun Heo */ 1915c8e55f36STejun Heo static void worker_leave_idle(struct worker *worker) 1916c8e55f36STejun Heo { 1917bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 1918c8e55f36STejun Heo 19196183c009STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 19206183c009STejun Heo return; 1921d302f017STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1922bd7bdd43STejun Heo pool->nr_idle--; 1923c8e55f36STejun Heo list_del_init(&worker->entry); 1924c8e55f36STejun Heo } 1925c8e55f36STejun Heo 1926f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 1927c34056a3STejun Heo { 1928c34056a3STejun Heo struct worker *worker; 1929c34056a3STejun Heo 1930f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 1931c8e55f36STejun Heo if (worker) { 1932c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 1933affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 1934da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 1935e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 1936e22bee78STejun Heo worker->flags = WORKER_PREP; 1937c8e55f36STejun Heo } 1938c34056a3STejun Heo return worker; 1939c34056a3STejun Heo } 1940c34056a3STejun Heo 1941c34056a3STejun Heo /** 19424736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 19434736cbf7SLai Jiangshan * @worker: worker to be attached 19444736cbf7SLai Jiangshan * @pool: the target pool 19454736cbf7SLai Jiangshan * 19464736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 19474736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 19484736cbf7SLai Jiangshan * cpu-[un]hotplugs. 19494736cbf7SLai Jiangshan */ 19504736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 19514736cbf7SLai Jiangshan struct worker_pool *pool) 19524736cbf7SLai Jiangshan { 19531258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 19544736cbf7SLai Jiangshan 19554736cbf7SLai Jiangshan /* 19561258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 19571258fae7STejun Heo * stable across this function. See the comments above the flag 19581258fae7STejun Heo * definition for details. 19594736cbf7SLai Jiangshan */ 19604736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 19614736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 19625c25b5ffSPeter Zijlstra else 19635c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 19644736cbf7SLai Jiangshan 1965640f17c8SPeter Zijlstra if (worker->rescue_wq) 1966640f17c8SPeter Zijlstra set_cpus_allowed_ptr(worker->task, pool->attrs->cpumask); 1967640f17c8SPeter Zijlstra 19684736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 1969a2d812a2STejun Heo worker->pool = pool; 19704736cbf7SLai Jiangshan 19711258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 19724736cbf7SLai Jiangshan } 19734736cbf7SLai Jiangshan 19744736cbf7SLai Jiangshan /** 197560f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 197660f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 197760f5a4bcSLai Jiangshan * 19784736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 19794736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 19804736cbf7SLai Jiangshan * other reference to the pool. 198160f5a4bcSLai Jiangshan */ 1982a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 198360f5a4bcSLai Jiangshan { 1984a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 198560f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 198660f5a4bcSLai Jiangshan 19871258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 1988a2d812a2STejun Heo 19895c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 1990da028469SLai Jiangshan list_del(&worker->node); 1991a2d812a2STejun Heo worker->pool = NULL; 1992a2d812a2STejun Heo 1993e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 199460f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 19951258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 199660f5a4bcSLai Jiangshan 1997b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 1998b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 1999b62c0751SLai Jiangshan 200060f5a4bcSLai Jiangshan if (detach_completion) 200160f5a4bcSLai Jiangshan complete(detach_completion); 200260f5a4bcSLai Jiangshan } 200360f5a4bcSLai Jiangshan 200460f5a4bcSLai Jiangshan /** 2005c34056a3STejun Heo * create_worker - create a new workqueue worker 200663d95a91STejun Heo * @pool: pool the new worker will belong to 2007c34056a3STejun Heo * 2008051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2009c34056a3STejun Heo * 2010c34056a3STejun Heo * CONTEXT: 2011c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2012c34056a3STejun Heo * 2013d185af30SYacine Belkadi * Return: 2014c34056a3STejun Heo * Pointer to the newly created worker. 2015c34056a3STejun Heo */ 2016bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2017c34056a3STejun Heo { 2018e441b56fSZhen Lei struct worker *worker; 2019e441b56fSZhen Lei int id; 2020e3c916a4STejun Heo char id_buf[16]; 2021c34056a3STejun Heo 20227cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2023e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 20243f0ea0b8SPetr Mladek if (id < 0) { 20253f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 20263f0ea0b8SPetr Mladek ERR_PTR(id)); 2027e441b56fSZhen Lei return NULL; 20283f0ea0b8SPetr Mladek } 2029c34056a3STejun Heo 2030f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 20313f0ea0b8SPetr Mladek if (!worker) { 20323f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2033c34056a3STejun Heo goto fail; 20343f0ea0b8SPetr Mladek } 2035c34056a3STejun Heo 2036c34056a3STejun Heo worker->id = id; 2037c34056a3STejun Heo 203829c91e99STejun Heo if (pool->cpu >= 0) 2039e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2040e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2041f3421797STejun Heo else 2042e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2043e3c916a4STejun Heo 2044f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2045e3c916a4STejun Heo "kworker/%s", id_buf); 20463f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 204760f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 204860f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 204960f54038SPetr Mladek id_buf); 205060f54038SPetr Mladek } else { 20513f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 20523f0ea0b8SPetr Mladek worker->task); 205360f54038SPetr Mladek } 2054c34056a3STejun Heo goto fail; 20553f0ea0b8SPetr Mladek } 2056c34056a3STejun Heo 205791151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 205825834c73SPeter Zijlstra kthread_bind_mask(worker->task, pool->attrs->cpumask); 205991151228SOleg Nesterov 2060da028469SLai Jiangshan /* successful, attach the worker to the pool */ 20614736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2062822d8405STejun Heo 2063051e1850SLai Jiangshan /* start the newly created worker */ 2064a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2065051e1850SLai Jiangshan worker->pool->nr_workers++; 2066051e1850SLai Jiangshan worker_enter_idle(worker); 2067051e1850SLai Jiangshan wake_up_process(worker->task); 2068a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2069051e1850SLai Jiangshan 2070c34056a3STejun Heo return worker; 2071822d8405STejun Heo 2072c34056a3STejun Heo fail: 2073e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2074c34056a3STejun Heo kfree(worker); 2075c34056a3STejun Heo return NULL; 2076c34056a3STejun Heo } 2077c34056a3STejun Heo 2078793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2079793777bcSValentin Schneider { 2080793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2081793777bcSValentin Schneider 2082793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2083793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2084793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2085793777bcSValentin Schneider else 2086793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2087793777bcSValentin Schneider } 2088793777bcSValentin Schneider 2089e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2090e02b9312SValentin Schneider { 2091e02b9312SValentin Schneider struct worker *worker, *tmp; 2092e02b9312SValentin Schneider 2093e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2094e02b9312SValentin Schneider list_del_init(&worker->entry); 2095e02b9312SValentin Schneider unbind_worker(worker); 2096e02b9312SValentin Schneider /* 2097e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2098e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2099e02b9312SValentin Schneider * wouldn't have gotten here. 2100c34056a3STejun Heo * 2101e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2102e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2103e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2104e02b9312SValentin Schneider * outside of pool->lock. 2105e02b9312SValentin Schneider */ 2106e02b9312SValentin Schneider wake_up_process(worker->task); 2107e02b9312SValentin Schneider } 2108e02b9312SValentin Schneider } 2109e02b9312SValentin Schneider 2110e02b9312SValentin Schneider /** 2111e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2112e02b9312SValentin Schneider * @worker: worker to be destroyed 2113e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2114e02b9312SValentin Schneider * 2115e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2116e02b9312SValentin Schneider * should be idle. 2117c8e55f36STejun Heo * 2118c8e55f36STejun Heo * CONTEXT: 2119a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2120c34056a3STejun Heo */ 2121e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2122c34056a3STejun Heo { 2123bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2124c34056a3STejun Heo 2125cd549687STejun Heo lockdep_assert_held(&pool->lock); 2126e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2127cd549687STejun Heo 2128c34056a3STejun Heo /* sanity check frenzy */ 21296183c009STejun Heo if (WARN_ON(worker->current_work) || 213073eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 213173eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 21326183c009STejun Heo return; 2133c34056a3STejun Heo 2134bd7bdd43STejun Heo pool->nr_workers--; 2135bd7bdd43STejun Heo pool->nr_idle--; 2136c8e55f36STejun Heo 2137cb444766STejun Heo worker->flags |= WORKER_DIE; 2138e02b9312SValentin Schneider 2139e02b9312SValentin Schneider list_move(&worker->entry, list); 2140e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2141c34056a3STejun Heo } 2142c34056a3STejun Heo 21433f959aa3SValentin Schneider /** 21443f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 21453f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 21463f959aa3SValentin Schneider * 21473f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 21483f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 21493f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 21503f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 21513f959aa3SValentin Schneider * it expire and re-evaluate things from there. 21523f959aa3SValentin Schneider */ 215332a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2154e22bee78STejun Heo { 215532a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 21563f959aa3SValentin Schneider bool do_cull = false; 21573f959aa3SValentin Schneider 21583f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 21593f959aa3SValentin Schneider return; 21603f959aa3SValentin Schneider 21613f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 21623f959aa3SValentin Schneider 21633f959aa3SValentin Schneider if (too_many_workers(pool)) { 21643f959aa3SValentin Schneider struct worker *worker; 21653f959aa3SValentin Schneider unsigned long expires; 21663f959aa3SValentin Schneider 21673f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 21683f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 21693f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 21703f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 21713f959aa3SValentin Schneider 21723f959aa3SValentin Schneider if (!do_cull) 21733f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 21743f959aa3SValentin Schneider } 21753f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 21763f959aa3SValentin Schneider 21773f959aa3SValentin Schneider if (do_cull) 21783f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 21793f959aa3SValentin Schneider } 21803f959aa3SValentin Schneider 21813f959aa3SValentin Schneider /** 21823f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 21833f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 21843f959aa3SValentin Schneider * 21853f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 21863f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2187e02b9312SValentin Schneider * 2188e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2189e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2190e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 21913f959aa3SValentin Schneider */ 21923f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 21933f959aa3SValentin Schneider { 21943f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 2195e02b9312SValentin Schneider struct list_head cull_list; 2196e22bee78STejun Heo 2197e02b9312SValentin Schneider INIT_LIST_HEAD(&cull_list); 2198e02b9312SValentin Schneider /* 2199e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2200e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2201e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2202e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2203e02b9312SValentin Schneider */ 2204e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2205a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2206e22bee78STejun Heo 22073347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2208e22bee78STejun Heo struct worker *worker; 2209e22bee78STejun Heo unsigned long expires; 2210e22bee78STejun Heo 221163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2212e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2213e22bee78STejun Heo 22143347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 221563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 22163347fc9fSLai Jiangshan break; 2217e22bee78STejun Heo } 22183347fc9fSLai Jiangshan 2219e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2220e22bee78STejun Heo } 2221e22bee78STejun Heo 2222a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2223e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2224e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2225e22bee78STejun Heo } 2226e22bee78STejun Heo 2227493a1724STejun Heo static void send_mayday(struct work_struct *work) 2228e22bee78STejun Heo { 2229112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2230112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2231493a1724STejun Heo 22322e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2233e22bee78STejun Heo 2234493008a8STejun Heo if (!wq->rescuer) 2235493a1724STejun Heo return; 2236e22bee78STejun Heo 2237e22bee78STejun Heo /* mayday mayday mayday */ 2238493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 223977668c8bSLai Jiangshan /* 224077668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 224177668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 224277668c8bSLai Jiangshan * rescuer is done with it. 224377668c8bSLai Jiangshan */ 224477668c8bSLai Jiangshan get_pwq(pwq); 2245493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2246e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2247725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2248493a1724STejun Heo } 2249e22bee78STejun Heo } 2250e22bee78STejun Heo 225132a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2252e22bee78STejun Heo { 225332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2254e22bee78STejun Heo struct work_struct *work; 2255e22bee78STejun Heo 2256a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2257a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2258e22bee78STejun Heo 225963d95a91STejun Heo if (need_to_create_worker(pool)) { 2260e22bee78STejun Heo /* 2261e22bee78STejun Heo * We've been trying to create a new worker but 2262e22bee78STejun Heo * haven't been successful. We might be hitting an 2263e22bee78STejun Heo * allocation deadlock. Send distress signals to 2264e22bee78STejun Heo * rescuers. 2265e22bee78STejun Heo */ 226663d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2267e22bee78STejun Heo send_mayday(work); 2268e22bee78STejun Heo } 2269e22bee78STejun Heo 2270a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2271a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2272e22bee78STejun Heo 227363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2274e22bee78STejun Heo } 2275e22bee78STejun Heo 2276e22bee78STejun Heo /** 2277e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 227863d95a91STejun Heo * @pool: pool to create a new worker for 2279e22bee78STejun Heo * 228063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2281e22bee78STejun Heo * have at least one idle worker on return from this function. If 2282e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 228363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2284e22bee78STejun Heo * possible allocation deadlock. 2285e22bee78STejun Heo * 2286c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2287c5aa87bbSTejun Heo * may_start_working() %true. 2288e22bee78STejun Heo * 2289e22bee78STejun Heo * LOCKING: 2290a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2291e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2292e22bee78STejun Heo * manager. 2293e22bee78STejun Heo */ 229429187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2295d565ed63STejun Heo __releases(&pool->lock) 2296d565ed63STejun Heo __acquires(&pool->lock) 2297e22bee78STejun Heo { 2298e22bee78STejun Heo restart: 2299a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 23009f9c2364STejun Heo 2301e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 230263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2303e22bee78STejun Heo 2304e22bee78STejun Heo while (true) { 2305051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2306e22bee78STejun Heo break; 2307e22bee78STejun Heo 2308e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 23099f9c2364STejun Heo 231063d95a91STejun Heo if (!need_to_create_worker(pool)) 2311e22bee78STejun Heo break; 2312e22bee78STejun Heo } 2313e22bee78STejun Heo 231463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2315a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2316051e1850SLai Jiangshan /* 2317051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2318051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2319051e1850SLai Jiangshan * already become busy. 2320051e1850SLai Jiangshan */ 232163d95a91STejun Heo if (need_to_create_worker(pool)) 2322e22bee78STejun Heo goto restart; 2323e22bee78STejun Heo } 2324e22bee78STejun Heo 2325e22bee78STejun Heo /** 2326e22bee78STejun Heo * manage_workers - manage worker pool 2327e22bee78STejun Heo * @worker: self 2328e22bee78STejun Heo * 2329706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2330e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2331706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2332e22bee78STejun Heo * 2333e22bee78STejun Heo * The caller can safely start processing works on false return. On 2334e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2335e22bee78STejun Heo * and may_start_working() is true. 2336e22bee78STejun Heo * 2337e22bee78STejun Heo * CONTEXT: 2338a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2339e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2340e22bee78STejun Heo * 2341d185af30SYacine Belkadi * Return: 234229187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 234329187a9eSTejun Heo * start processing works, %true if management function was performed and 234429187a9eSTejun Heo * the conditions that the caller verified before calling the function may 234529187a9eSTejun Heo * no longer be true. 2346e22bee78STejun Heo */ 2347e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2348e22bee78STejun Heo { 234963d95a91STejun Heo struct worker_pool *pool = worker->pool; 2350e22bee78STejun Heo 2351692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 235229187a9eSTejun Heo return false; 2353692b4825STejun Heo 2354692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 23552607d7a6STejun Heo pool->manager = worker; 2356e22bee78STejun Heo 235729187a9eSTejun Heo maybe_create_worker(pool); 2358e22bee78STejun Heo 23592607d7a6STejun Heo pool->manager = NULL; 2360692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2361d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 236229187a9eSTejun Heo return true; 2363e22bee78STejun Heo } 2364e22bee78STejun Heo 2365a62428c0STejun Heo /** 2366a62428c0STejun Heo * process_one_work - process single work 2367c34056a3STejun Heo * @worker: self 2368a62428c0STejun Heo * @work: work to process 2369a62428c0STejun Heo * 2370a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2371a62428c0STejun Heo * process a single work including synchronization against and 2372a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2373a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2374a62428c0STejun Heo * call this function to process a work. 2375a62428c0STejun Heo * 2376a62428c0STejun Heo * CONTEXT: 2377a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2378a62428c0STejun Heo */ 2379c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2380d565ed63STejun Heo __releases(&pool->lock) 2381d565ed63STejun Heo __acquires(&pool->lock) 23821da177e4SLinus Torvalds { 2383112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2384bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2385c4560c2cSLai Jiangshan unsigned long work_data; 23867e11629dSTejun Heo struct worker *collision; 23874e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 23884e6045f1SJohannes Berg /* 2389a62428c0STejun Heo * It is permissible to free the struct work_struct from 2390a62428c0STejun Heo * inside the function that is called from it, this we need to 2391a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2392a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2393a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 23944e6045f1SJohannes Berg */ 23954d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 23964d82a1deSPeter Zijlstra 23974d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 23984e6045f1SJohannes Berg #endif 2399807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 240085327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2401ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 240225511a47STejun Heo 24037e11629dSTejun Heo /* 24047e11629dSTejun Heo * A single work shouldn't be executed concurrently by 24057e11629dSTejun Heo * multiple workers on a single cpu. Check whether anyone is 24067e11629dSTejun Heo * already processing the work. If so, defer the work to the 24077e11629dSTejun Heo * currently executing one. 24087e11629dSTejun Heo */ 2409c9e7cf27STejun Heo collision = find_worker_executing_work(pool, work); 24107e11629dSTejun Heo if (unlikely(collision)) { 24117e11629dSTejun Heo move_linked_works(work, &collision->scheduled, NULL); 24127e11629dSTejun Heo return; 24137e11629dSTejun Heo } 24141da177e4SLinus Torvalds 24158930cabaSTejun Heo /* claim and dequeue */ 24161da177e4SLinus Torvalds debug_work_deactivate(work); 2417c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2418c34056a3STejun Heo worker->current_work = work; 2419a2c1c57bSTejun Heo worker->current_func = work->func; 2420112202d9STejun Heo worker->current_pwq = pwq; 2421*616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2422c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2423d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 24247a22ad75STejun Heo 24258bf89593STejun Heo /* 24268bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 24278bf89593STejun Heo * overridden through set_worker_desc(). 24288bf89593STejun Heo */ 24298bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 24308bf89593STejun Heo 2431a62428c0STejun Heo list_del_init(&work->entry); 2432a62428c0STejun Heo 2433649027d7STejun Heo /* 2434228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2435228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2436228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2437228f1d00SLai Jiangshan * execution of the pending work items. 2438fb0e7bebSTejun Heo */ 2439*616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2440228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2441fb0e7bebSTejun Heo 2442974271c4STejun Heo /* 2443a489a03eSLai Jiangshan * Wake up another worker if necessary. The condition is always 2444a489a03eSLai Jiangshan * false for normal per-cpu workers since nr_running would always 2445a489a03eSLai Jiangshan * be >= 1 at this point. This is used to chain execution of the 2446a489a03eSLai Jiangshan * pending work items for WORKER_NOT_RUNNING workers such as the 2447228f1d00SLai Jiangshan * UNBOUND and CPU_INTENSIVE ones. 2448974271c4STejun Heo */ 2449a489a03eSLai Jiangshan if (need_more_worker(pool)) 245063d95a91STejun Heo wake_up_worker(pool); 2451974271c4STejun Heo 24528930cabaSTejun Heo /* 24537c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2454d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 245523657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 245623657bb1STejun Heo * disabled. 24578930cabaSTejun Heo */ 24587c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 24591da177e4SLinus Torvalds 2460a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2461365970a1SDavid Howells 2462a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 24633295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2464e6f3faa7SPeter Zijlstra /* 2465f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2466f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2467e6f3faa7SPeter Zijlstra * 2468e6f3faa7SPeter Zijlstra * However, that would result in: 2469e6f3faa7SPeter Zijlstra * 2470e6f3faa7SPeter Zijlstra * A(W1) 2471e6f3faa7SPeter Zijlstra * WFC(C) 2472e6f3faa7SPeter Zijlstra * A(W1) 2473e6f3faa7SPeter Zijlstra * C(C) 2474e6f3faa7SPeter Zijlstra * 2475e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2476e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2477e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2478f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2479e6f3faa7SPeter Zijlstra * these locks. 2480e6f3faa7SPeter Zijlstra * 2481e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2482e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2483e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2484e6f3faa7SPeter Zijlstra */ 2485f52be570SPeter Zijlstra lockdep_invariant_state(true); 2486725e8ec5STejun Heo pwq->stats[PWQ_STAT_STARTED]++; 2487e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2488a2c1c57bSTejun Heo worker->current_func(work); 2489e36c886aSArjan van de Ven /* 2490e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2491e36c886aSArjan van de Ven * point will only record its address. 2492e36c886aSArjan van de Ven */ 24931c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2494725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 24953295f0efSIngo Molnar lock_map_release(&lockdep_map); 2496112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 24971da177e4SLinus Torvalds 2498d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2499044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2500d75f773cSSakari Ailus " last function: %ps\n", 2501a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2502a2c1c57bSTejun Heo worker->current_func); 2503d5abe669SPeter Zijlstra debug_show_held_locks(current); 2504d5abe669SPeter Zijlstra dump_stack(); 2505d5abe669SPeter Zijlstra } 2506d5abe669SPeter Zijlstra 2507b22ce278STejun Heo /* 2508025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2509b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2510b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2511b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2512789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2513789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2514b22ce278STejun Heo */ 2515a7e6425eSPaul E. McKenney cond_resched(); 2516b22ce278STejun Heo 2517a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2518a62428c0STejun Heo 2519*616db877STejun Heo /* 2520*616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 2521*616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 2522*616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 2523*616db877STejun Heo */ 2524fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2525fb0e7bebSTejun Heo 25261b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 25271b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 25281b69ac6bSJohannes Weiner 2529a62428c0STejun Heo /* we're done with it, release */ 253042f8570fSSasha Levin hash_del(&worker->hentry); 2531c34056a3STejun Heo worker->current_work = NULL; 2532a2c1c57bSTejun Heo worker->current_func = NULL; 2533112202d9STejun Heo worker->current_pwq = NULL; 2534d812796eSLai Jiangshan worker->current_color = INT_MAX; 2535c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 25361da177e4SLinus Torvalds } 25371da177e4SLinus Torvalds 2538affee4b2STejun Heo /** 2539affee4b2STejun Heo * process_scheduled_works - process scheduled works 2540affee4b2STejun Heo * @worker: self 2541affee4b2STejun Heo * 2542affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2543affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2544affee4b2STejun Heo * fetches a work from the top and executes it. 2545affee4b2STejun Heo * 2546affee4b2STejun Heo * CONTEXT: 2547a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2548affee4b2STejun Heo * multiple times. 2549affee4b2STejun Heo */ 2550affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 25511da177e4SLinus Torvalds { 2552affee4b2STejun Heo while (!list_empty(&worker->scheduled)) { 2553affee4b2STejun Heo struct work_struct *work = list_first_entry(&worker->scheduled, 2554a62428c0STejun Heo struct work_struct, entry); 2555c34056a3STejun Heo process_one_work(worker, work); 2556a62428c0STejun Heo } 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 2559197f6accSTejun Heo static void set_pf_worker(bool val) 2560197f6accSTejun Heo { 2561197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2562197f6accSTejun Heo if (val) 2563197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2564197f6accSTejun Heo else 2565197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2566197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2567197f6accSTejun Heo } 2568197f6accSTejun Heo 25694690c4abSTejun Heo /** 25704690c4abSTejun Heo * worker_thread - the worker thread function 2571c34056a3STejun Heo * @__worker: self 25724690c4abSTejun Heo * 2573c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2574c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2575c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2576c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2577c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2578d185af30SYacine Belkadi * 2579d185af30SYacine Belkadi * Return: 0 25804690c4abSTejun Heo */ 2581c34056a3STejun Heo static int worker_thread(void *__worker) 25821da177e4SLinus Torvalds { 2583c34056a3STejun Heo struct worker *worker = __worker; 2584bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 25851da177e4SLinus Torvalds 2586e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2587197f6accSTejun Heo set_pf_worker(true); 2588c8e55f36STejun Heo woke_up: 2589a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2590affee4b2STejun Heo 2591a9ab775bSTejun Heo /* am I supposed to die? */ 2592a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2593a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2594197f6accSTejun Heo set_pf_worker(false); 259560f5a4bcSLai Jiangshan 259660f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2597e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2598a2d812a2STejun Heo worker_detach_from_pool(worker); 2599e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 260060f5a4bcSLai Jiangshan kfree(worker); 2601c8e55f36STejun Heo return 0; 2602c8e55f36STejun Heo } 2603c8e55f36STejun Heo 2604c8e55f36STejun Heo worker_leave_idle(worker); 2605db7bccf4STejun Heo recheck: 2606e22bee78STejun Heo /* no more worker necessary? */ 260763d95a91STejun Heo if (!need_more_worker(pool)) 2608e22bee78STejun Heo goto sleep; 2609e22bee78STejun Heo 2610e22bee78STejun Heo /* do we need to manage? */ 261163d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2612e22bee78STejun Heo goto recheck; 2613e22bee78STejun Heo 2614c8e55f36STejun Heo /* 2615c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2616c8e55f36STejun Heo * preparing to process a work or actually processing it. 2617c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2618c8e55f36STejun Heo */ 26196183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2620c8e55f36STejun Heo 2621e22bee78STejun Heo /* 2622a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2623a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2624a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2625a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2626a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2627e22bee78STejun Heo */ 2628a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2629e22bee78STejun Heo 2630e22bee78STejun Heo do { 2631affee4b2STejun Heo struct work_struct *work = 2632bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2633affee4b2STejun Heo struct work_struct, entry); 2634affee4b2STejun Heo 263582607adcSTejun Heo pool->watchdog_ts = jiffies; 263682607adcSTejun Heo 2637c8e55f36STejun Heo if (likely(!(*work_data_bits(work) & WORK_STRUCT_LINKED))) { 2638affee4b2STejun Heo /* optimization path, not strictly necessary */ 2639affee4b2STejun Heo process_one_work(worker, work); 2640affee4b2STejun Heo if (unlikely(!list_empty(&worker->scheduled))) 2641affee4b2STejun Heo process_scheduled_works(worker); 2642affee4b2STejun Heo } else { 2643c8e55f36STejun Heo move_linked_works(work, &worker->scheduled, NULL); 2644affee4b2STejun Heo process_scheduled_works(worker); 2645affee4b2STejun Heo } 264663d95a91STejun Heo } while (keep_working(pool)); 2647affee4b2STejun Heo 2648228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2649d313dd85STejun Heo sleep: 2650c8e55f36STejun Heo /* 2651d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2652d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2653d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2654d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2655d565ed63STejun Heo * event. 2656c8e55f36STejun Heo */ 2657c8e55f36STejun Heo worker_enter_idle(worker); 2658c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2659a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 26601da177e4SLinus Torvalds schedule(); 2661c8e55f36STejun Heo goto woke_up; 26621da177e4SLinus Torvalds } 26631da177e4SLinus Torvalds 2664e22bee78STejun Heo /** 2665e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2666111c225aSTejun Heo * @__rescuer: self 2667e22bee78STejun Heo * 2668e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2669493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2670e22bee78STejun Heo * 2671706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2672e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2673e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2674e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2675e22bee78STejun Heo * the problem rescuer solves. 2676e22bee78STejun Heo * 2677706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2678706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2679e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2680e22bee78STejun Heo * 2681e22bee78STejun Heo * This should happen rarely. 2682d185af30SYacine Belkadi * 2683d185af30SYacine Belkadi * Return: 0 2684e22bee78STejun Heo */ 2685111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2686e22bee78STejun Heo { 2687111c225aSTejun Heo struct worker *rescuer = __rescuer; 2688111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 2689e22bee78STejun Heo struct list_head *scheduled = &rescuer->scheduled; 26904d595b86SLai Jiangshan bool should_stop; 2691e22bee78STejun Heo 2692e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2693111c225aSTejun Heo 2694111c225aSTejun Heo /* 2695111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2696111c225aSTejun Heo * doesn't participate in concurrency management. 2697111c225aSTejun Heo */ 2698197f6accSTejun Heo set_pf_worker(true); 2699e22bee78STejun Heo repeat: 2700c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 27011da177e4SLinus Torvalds 27024d595b86SLai Jiangshan /* 27034d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 27044d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 27054d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 27064d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 27074d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 27084d595b86SLai Jiangshan * list is always empty on exit. 27094d595b86SLai Jiangshan */ 27104d595b86SLai Jiangshan should_stop = kthread_should_stop(); 27111da177e4SLinus Torvalds 2712493a1724STejun Heo /* see whether any pwq is asking for help */ 2713a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 2714493a1724STejun Heo 2715493a1724STejun Heo while (!list_empty(&wq->maydays)) { 2716493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 2717493a1724STejun Heo struct pool_workqueue, mayday_node); 2718112202d9STejun Heo struct worker_pool *pool = pwq->pool; 2719e22bee78STejun Heo struct work_struct *work, *n; 272082607adcSTejun Heo bool first = true; 2721e22bee78STejun Heo 2722e22bee78STejun Heo __set_current_state(TASK_RUNNING); 2723493a1724STejun Heo list_del_init(&pwq->mayday_node); 2724493a1724STejun Heo 2725a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2726e22bee78STejun Heo 272751697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 272851697d39SLai Jiangshan 2729a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2730e22bee78STejun Heo 2731e22bee78STejun Heo /* 2732e22bee78STejun Heo * Slurp in all works issued via this workqueue and 2733e22bee78STejun Heo * process'em. 2734e22bee78STejun Heo */ 27350479c8c5STejun Heo WARN_ON_ONCE(!list_empty(scheduled)); 273682607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 273782607adcSTejun Heo if (get_work_pwq(work) == pwq) { 273882607adcSTejun Heo if (first) 273982607adcSTejun Heo pool->watchdog_ts = jiffies; 2740e22bee78STejun Heo move_linked_works(work, scheduled, &n); 2741725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 274282607adcSTejun Heo } 274382607adcSTejun Heo first = false; 274482607adcSTejun Heo } 2745e22bee78STejun Heo 2746008847f6SNeilBrown if (!list_empty(scheduled)) { 2747e22bee78STejun Heo process_scheduled_works(rescuer); 27487576958aSTejun Heo 27497576958aSTejun Heo /* 2750008847f6SNeilBrown * The above execution of rescued work items could 2751008847f6SNeilBrown * have created more to rescue through 2752f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 2753008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 2754008847f6SNeilBrown * that such back-to-back work items, which may be 2755008847f6SNeilBrown * being used to relieve memory pressure, don't 2756008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 2757008847f6SNeilBrown */ 27584f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 2759a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 2760e66b39afSTejun Heo /* 2761e66b39afSTejun Heo * Queue iff we aren't racing destruction 2762e66b39afSTejun Heo * and somebody else hasn't queued it already. 2763e66b39afSTejun Heo */ 2764e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 2765008847f6SNeilBrown get_pwq(pwq); 2766e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2767e66b39afSTejun Heo } 2768a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2769008847f6SNeilBrown } 2770008847f6SNeilBrown } 2771008847f6SNeilBrown 2772008847f6SNeilBrown /* 277377668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 277413b1d625SLai Jiangshan * go away while we're still attached to it. 277577668c8bSLai Jiangshan */ 277677668c8bSLai Jiangshan put_pwq(pwq); 277777668c8bSLai Jiangshan 277877668c8bSLai Jiangshan /* 2779d8ca83e6SLai Jiangshan * Leave this pool. If need_more_worker() is %true, notify a 27807576958aSTejun Heo * regular worker; otherwise, we end up with 0 concurrency 27817576958aSTejun Heo * and stalling the execution. 27827576958aSTejun Heo */ 2783d8ca83e6SLai Jiangshan if (need_more_worker(pool)) 278463d95a91STejun Heo wake_up_worker(pool); 27857576958aSTejun Heo 2786a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 278713b1d625SLai Jiangshan 2788a2d812a2STejun Heo worker_detach_from_pool(rescuer); 278913b1d625SLai Jiangshan 2790a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 2793a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 2794493a1724STejun Heo 27954d595b86SLai Jiangshan if (should_stop) { 27964d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 2797197f6accSTejun Heo set_pf_worker(false); 27984d595b86SLai Jiangshan return 0; 27994d595b86SLai Jiangshan } 28004d595b86SLai Jiangshan 2801111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 2802111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 2803e22bee78STejun Heo schedule(); 2804e22bee78STejun Heo goto repeat; 28051da177e4SLinus Torvalds } 28061da177e4SLinus Torvalds 2807fca839c0STejun Heo /** 2808fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 2809fca839c0STejun Heo * @target_wq: workqueue being flushed 2810fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 2811fca839c0STejun Heo * 2812fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 2813fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 2814fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 2815fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 2816fca839c0STejun Heo * a deadlock. 2817fca839c0STejun Heo */ 2818fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 2819fca839c0STejun Heo struct work_struct *target_work) 2820fca839c0STejun Heo { 2821fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 2822fca839c0STejun Heo struct worker *worker; 2823fca839c0STejun Heo 2824fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 2825fca839c0STejun Heo return; 2826fca839c0STejun Heo 2827fca839c0STejun Heo worker = current_wq_worker(); 2828fca839c0STejun Heo 2829fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 2830d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 2831fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 283223d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 283323d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 2834d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 2835fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 2836fca839c0STejun Heo target_wq->name, target_func); 2837fca839c0STejun Heo } 2838fca839c0STejun Heo 2839fc2e4d70SOleg Nesterov struct wq_barrier { 2840fc2e4d70SOleg Nesterov struct work_struct work; 2841fc2e4d70SOleg Nesterov struct completion done; 28422607d7a6STejun Heo struct task_struct *task; /* purely informational */ 2843fc2e4d70SOleg Nesterov }; 2844fc2e4d70SOleg Nesterov 2845fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 2846fc2e4d70SOleg Nesterov { 2847fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 2848fc2e4d70SOleg Nesterov complete(&barr->done); 2849fc2e4d70SOleg Nesterov } 2850fc2e4d70SOleg Nesterov 28514690c4abSTejun Heo /** 28524690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 2853112202d9STejun Heo * @pwq: pwq to insert barrier into 28544690c4abSTejun Heo * @barr: wq_barrier to insert 2855affee4b2STejun Heo * @target: target work to attach @barr to 2856affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 28574690c4abSTejun Heo * 2858affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 2859affee4b2STejun Heo * @target finishes execution. Please note that the ordering 2860affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 2861affee4b2STejun Heo * cpu. 2862affee4b2STejun Heo * 2863affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 2864affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 2865affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 2866affee4b2STejun Heo * flag of the previous work while there must be a valid next work 2867affee4b2STejun Heo * after a work with LINKED flag set. 2868affee4b2STejun Heo * 2869affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 2870112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 28714690c4abSTejun Heo * 28724690c4abSTejun Heo * CONTEXT: 2873a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 28744690c4abSTejun Heo */ 2875112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 2876affee4b2STejun Heo struct wq_barrier *barr, 2877affee4b2STejun Heo struct work_struct *target, struct worker *worker) 2878fc2e4d70SOleg Nesterov { 2879d812796eSLai Jiangshan unsigned int work_flags = 0; 2880d812796eSLai Jiangshan unsigned int work_color; 2881affee4b2STejun Heo struct list_head *head; 2882affee4b2STejun Heo 2883dc186ad7SThomas Gleixner /* 2884d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 2885dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 2886dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 2887dc186ad7SThomas Gleixner * might deadlock. 2888dc186ad7SThomas Gleixner */ 2889ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 289022df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 289152fa5bc5SBoqun Feng 2892fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 2893fd1a5b04SByungchul Park 28942607d7a6STejun Heo barr->task = current; 289583c22520SOleg Nesterov 2896018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 2897018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2898018f3a13SLai Jiangshan 2899affee4b2STejun Heo /* 2900affee4b2STejun Heo * If @target is currently being executed, schedule the 2901affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 2902affee4b2STejun Heo */ 2903d812796eSLai Jiangshan if (worker) { 2904affee4b2STejun Heo head = worker->scheduled.next; 2905d812796eSLai Jiangshan work_color = worker->current_color; 2906d812796eSLai Jiangshan } else { 2907affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 2908affee4b2STejun Heo 2909affee4b2STejun Heo head = target->entry.next; 2910affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 2911d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 2912d812796eSLai Jiangshan work_color = get_work_color(*bits); 2913affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 2914affee4b2STejun Heo } 2915affee4b2STejun Heo 2916d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 2917d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 2918d812796eSLai Jiangshan 2919dc186ad7SThomas Gleixner debug_work_activate(&barr->work); 2920d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 2921fc2e4d70SOleg Nesterov } 2922fc2e4d70SOleg Nesterov 292373f53c4aSTejun Heo /** 2924112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 292573f53c4aSTejun Heo * @wq: workqueue being flushed 292673f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 292773f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 292873f53c4aSTejun Heo * 2929112202d9STejun Heo * Prepare pwqs for workqueue flushing. 293073f53c4aSTejun Heo * 2931112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 2932112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 2933112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 2934112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 2935112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 293673f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 293773f53c4aSTejun Heo * 293873f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 293973f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 294073f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 294173f53c4aSTejun Heo * is returned. 294273f53c4aSTejun Heo * 2943112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 294473f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 294573f53c4aSTejun Heo * advanced to @work_color. 294673f53c4aSTejun Heo * 294773f53c4aSTejun Heo * CONTEXT: 29483c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 294973f53c4aSTejun Heo * 2950d185af30SYacine Belkadi * Return: 295173f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 295273f53c4aSTejun Heo * otherwise. 295373f53c4aSTejun Heo */ 2954112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 295573f53c4aSTejun Heo int flush_color, int work_color) 29561da177e4SLinus Torvalds { 295773f53c4aSTejun Heo bool wait = false; 295849e3cf44STejun Heo struct pool_workqueue *pwq; 29591da177e4SLinus Torvalds 296073f53c4aSTejun Heo if (flush_color >= 0) { 29616183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 2962112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 2963dc186ad7SThomas Gleixner } 296414441960SOleg Nesterov 296549e3cf44STejun Heo for_each_pwq(pwq, wq) { 2966112202d9STejun Heo struct worker_pool *pool = pwq->pool; 29671da177e4SLinus Torvalds 2968a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 296973f53c4aSTejun Heo 297073f53c4aSTejun Heo if (flush_color >= 0) { 29716183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 297273f53c4aSTejun Heo 2973112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 2974112202d9STejun Heo pwq->flush_color = flush_color; 2975112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 297673f53c4aSTejun Heo wait = true; 29771da177e4SLinus Torvalds } 297873f53c4aSTejun Heo } 297973f53c4aSTejun Heo 298073f53c4aSTejun Heo if (work_color >= 0) { 29816183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 2982112202d9STejun Heo pwq->work_color = work_color; 298373f53c4aSTejun Heo } 298473f53c4aSTejun Heo 2985a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 29861da177e4SLinus Torvalds } 29871da177e4SLinus Torvalds 2988112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 298973f53c4aSTejun Heo complete(&wq->first_flusher->done); 299073f53c4aSTejun Heo 299173f53c4aSTejun Heo return wait; 299283c22520SOleg Nesterov } 29931da177e4SLinus Torvalds 29940fcb78c2SRolf Eike Beer /** 2995c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 29960fcb78c2SRolf Eike Beer * @wq: workqueue to flush 29971da177e4SLinus Torvalds * 2998c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 2999c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 30001da177e4SLinus Torvalds */ 3001c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 30021da177e4SLinus Torvalds { 300373f53c4aSTejun Heo struct wq_flusher this_flusher = { 300473f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 300573f53c4aSTejun Heo .flush_color = -1, 3006fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 300773f53c4aSTejun Heo }; 300873f53c4aSTejun Heo int next_color; 3009b1f4ec17SOleg Nesterov 30103347fa09STejun Heo if (WARN_ON(!wq_online)) 30113347fa09STejun Heo return; 30123347fa09STejun Heo 301387915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 301487915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 301587915adcSJohannes Berg 30163c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 301773f53c4aSTejun Heo 301873f53c4aSTejun Heo /* 301973f53c4aSTejun Heo * Start-to-wait phase 302073f53c4aSTejun Heo */ 302173f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 302273f53c4aSTejun Heo 302373f53c4aSTejun Heo if (next_color != wq->flush_color) { 302473f53c4aSTejun Heo /* 302573f53c4aSTejun Heo * Color space is not full. The current work_color 302673f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 302773f53c4aSTejun Heo * by one. 302873f53c4aSTejun Heo */ 30296183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 303073f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 303173f53c4aSTejun Heo wq->work_color = next_color; 303273f53c4aSTejun Heo 303373f53c4aSTejun Heo if (!wq->first_flusher) { 303473f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 30356183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 303673f53c4aSTejun Heo 303773f53c4aSTejun Heo wq->first_flusher = &this_flusher; 303873f53c4aSTejun Heo 3039112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 304073f53c4aSTejun Heo wq->work_color)) { 304173f53c4aSTejun Heo /* nothing to flush, done */ 304273f53c4aSTejun Heo wq->flush_color = next_color; 304373f53c4aSTejun Heo wq->first_flusher = NULL; 304473f53c4aSTejun Heo goto out_unlock; 304573f53c4aSTejun Heo } 304673f53c4aSTejun Heo } else { 304773f53c4aSTejun Heo /* wait in queue */ 30486183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 304973f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3050112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 305173f53c4aSTejun Heo } 305273f53c4aSTejun Heo } else { 305373f53c4aSTejun Heo /* 305473f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 305573f53c4aSTejun Heo * The next flush completion will assign us 305673f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 305773f53c4aSTejun Heo */ 305873f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 305973f53c4aSTejun Heo } 306073f53c4aSTejun Heo 3061fca839c0STejun Heo check_flush_dependency(wq, NULL); 3062fca839c0STejun Heo 30633c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 306473f53c4aSTejun Heo 306573f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 306673f53c4aSTejun Heo 306773f53c4aSTejun Heo /* 306873f53c4aSTejun Heo * Wake-up-and-cascade phase 306973f53c4aSTejun Heo * 307073f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 307173f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 307273f53c4aSTejun Heo */ 307300d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 307473f53c4aSTejun Heo return; 307573f53c4aSTejun Heo 30763c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 307773f53c4aSTejun Heo 30784ce48b37STejun Heo /* we might have raced, check again with mutex held */ 30794ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 30804ce48b37STejun Heo goto out_unlock; 30814ce48b37STejun Heo 308200d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 308373f53c4aSTejun Heo 30846183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 30856183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 308673f53c4aSTejun Heo 308773f53c4aSTejun Heo while (true) { 308873f53c4aSTejun Heo struct wq_flusher *next, *tmp; 308973f53c4aSTejun Heo 309073f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 309173f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 309273f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 309373f53c4aSTejun Heo break; 309473f53c4aSTejun Heo list_del_init(&next->list); 309573f53c4aSTejun Heo complete(&next->done); 309673f53c4aSTejun Heo } 309773f53c4aSTejun Heo 30986183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 309973f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 310073f53c4aSTejun Heo 310173f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 310273f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 310373f53c4aSTejun Heo 310473f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 310573f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 310673f53c4aSTejun Heo /* 310773f53c4aSTejun Heo * Assign the same color to all overflowed 310873f53c4aSTejun Heo * flushers, advance work_color and append to 310973f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 311073f53c4aSTejun Heo * phase for these overflowed flushers. 311173f53c4aSTejun Heo */ 311273f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 311373f53c4aSTejun Heo tmp->flush_color = wq->work_color; 311473f53c4aSTejun Heo 311573f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 311673f53c4aSTejun Heo 311773f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 311873f53c4aSTejun Heo &wq->flusher_queue); 3119112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 312073f53c4aSTejun Heo } 312173f53c4aSTejun Heo 312273f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 31236183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 312473f53c4aSTejun Heo break; 312573f53c4aSTejun Heo } 312673f53c4aSTejun Heo 312773f53c4aSTejun Heo /* 312873f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3129112202d9STejun Heo * the new first flusher and arm pwqs. 313073f53c4aSTejun Heo */ 31316183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 31326183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 313373f53c4aSTejun Heo 313473f53c4aSTejun Heo list_del_init(&next->list); 313573f53c4aSTejun Heo wq->first_flusher = next; 313673f53c4aSTejun Heo 3137112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 313873f53c4aSTejun Heo break; 313973f53c4aSTejun Heo 314073f53c4aSTejun Heo /* 314173f53c4aSTejun Heo * Meh... this color is already done, clear first 314273f53c4aSTejun Heo * flusher and repeat cascading. 314373f53c4aSTejun Heo */ 314473f53c4aSTejun Heo wq->first_flusher = NULL; 314573f53c4aSTejun Heo } 314673f53c4aSTejun Heo 314773f53c4aSTejun Heo out_unlock: 31483c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 31491da177e4SLinus Torvalds } 3150c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 31511da177e4SLinus Torvalds 31529c5a2ba7STejun Heo /** 31539c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 31549c5a2ba7STejun Heo * @wq: workqueue to drain 31559c5a2ba7STejun Heo * 31569c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 31579c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 31589c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3159b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 31609c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 31619c5a2ba7STejun Heo * takes too long. 31629c5a2ba7STejun Heo */ 31639c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 31649c5a2ba7STejun Heo { 31659c5a2ba7STejun Heo unsigned int flush_cnt = 0; 316649e3cf44STejun Heo struct pool_workqueue *pwq; 31679c5a2ba7STejun Heo 31689c5a2ba7STejun Heo /* 31699c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 31709c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3171618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 31729c5a2ba7STejun Heo */ 317387fc741eSLai Jiangshan mutex_lock(&wq->mutex); 31749c5a2ba7STejun Heo if (!wq->nr_drainers++) 3175618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 317687fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 31779c5a2ba7STejun Heo reflush: 3178c4f135d6STetsuo Handa __flush_workqueue(wq); 31799c5a2ba7STejun Heo 3180b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 318176af4d93STejun Heo 318249e3cf44STejun Heo for_each_pwq(pwq, wq) { 3183fa2563e4SThomas Tuttle bool drained; 31849c5a2ba7STejun Heo 3185a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3186f97a4a1aSLai Jiangshan drained = !pwq->nr_active && list_empty(&pwq->inactive_works); 3187a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3188fa2563e4SThomas Tuttle 3189fa2563e4SThomas Tuttle if (drained) 31909c5a2ba7STejun Heo continue; 31919c5a2ba7STejun Heo 31929c5a2ba7STejun Heo if (++flush_cnt == 10 || 31939c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3194e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3195e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 319676af4d93STejun Heo 3197b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 31989c5a2ba7STejun Heo goto reflush; 31999c5a2ba7STejun Heo } 32009c5a2ba7STejun Heo 32019c5a2ba7STejun Heo if (!--wq->nr_drainers) 3202618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 320387fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 32049c5a2ba7STejun Heo } 32059c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 32069c5a2ba7STejun Heo 3207d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3208d6e89786SJohannes Berg bool from_cancel) 3209baf59022STejun Heo { 3210baf59022STejun Heo struct worker *worker = NULL; 3211c9e7cf27STejun Heo struct worker_pool *pool; 3212112202d9STejun Heo struct pool_workqueue *pwq; 3213baf59022STejun Heo 3214baf59022STejun Heo might_sleep(); 3215baf59022STejun Heo 321624acfb71SThomas Gleixner rcu_read_lock(); 3217fa1b54e6STejun Heo pool = get_work_pool(work); 3218fa1b54e6STejun Heo if (!pool) { 321924acfb71SThomas Gleixner rcu_read_unlock(); 3220fa1b54e6STejun Heo return false; 3221fa1b54e6STejun Heo } 3222fa1b54e6STejun Heo 3223a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 32240b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3225112202d9STejun Heo pwq = get_work_pwq(work); 3226112202d9STejun Heo if (pwq) { 3227112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3228baf59022STejun Heo goto already_gone; 3229606a5020STejun Heo } else { 3230c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3231baf59022STejun Heo if (!worker) 3232baf59022STejun Heo goto already_gone; 3233112202d9STejun Heo pwq = worker->current_pwq; 3234606a5020STejun Heo } 3235baf59022STejun Heo 3236fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3237fca839c0STejun Heo 3238112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3239a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3240baf59022STejun Heo 3241e159489bSTejun Heo /* 3242a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3243a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3244a1d14934SPeter Zijlstra * 3245a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3246a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3247a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3248a1d14934SPeter Zijlstra * forward progress. 3249e159489bSTejun Heo */ 3250d6e89786SJohannes Berg if (!from_cancel && 3251d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3252112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3253112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3254a1d14934SPeter Zijlstra } 325524acfb71SThomas Gleixner rcu_read_unlock(); 3256baf59022STejun Heo return true; 3257baf59022STejun Heo already_gone: 3258a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 325924acfb71SThomas Gleixner rcu_read_unlock(); 3260baf59022STejun Heo return false; 3261baf59022STejun Heo } 3262baf59022STejun Heo 3263d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3264d6e89786SJohannes Berg { 3265d6e89786SJohannes Berg struct wq_barrier barr; 3266d6e89786SJohannes Berg 3267d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3268d6e89786SJohannes Berg return false; 3269d6e89786SJohannes Berg 32704d43d395STetsuo Handa if (WARN_ON(!work->func)) 32714d43d395STetsuo Handa return false; 32724d43d395STetsuo Handa 327387915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 327487915adcSJohannes Berg lock_map_release(&work->lockdep_map); 327587915adcSJohannes Berg 3276d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3277d6e89786SJohannes Berg wait_for_completion(&barr.done); 3278d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3279d6e89786SJohannes Berg return true; 3280d6e89786SJohannes Berg } else { 3281d6e89786SJohannes Berg return false; 3282d6e89786SJohannes Berg } 3283d6e89786SJohannes Berg } 3284d6e89786SJohannes Berg 3285db700897SOleg Nesterov /** 3286401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3287401a8d04STejun Heo * @work: the work to flush 3288db700897SOleg Nesterov * 3289606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3290606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3291401a8d04STejun Heo * 3292d185af30SYacine Belkadi * Return: 3293401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3294401a8d04STejun Heo * %false if it was already idle. 3295db700897SOleg Nesterov */ 3296401a8d04STejun Heo bool flush_work(struct work_struct *work) 3297db700897SOleg Nesterov { 3298d6e89786SJohannes Berg return __flush_work(work, false); 3299606a5020STejun Heo } 3300db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3301db700897SOleg Nesterov 33028603e1b3STejun Heo struct cwt_wait { 3303ac6424b9SIngo Molnar wait_queue_entry_t wait; 33048603e1b3STejun Heo struct work_struct *work; 33058603e1b3STejun Heo }; 33068603e1b3STejun Heo 3307ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 33088603e1b3STejun Heo { 33098603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 33108603e1b3STejun Heo 33118603e1b3STejun Heo if (cwait->work != key) 33128603e1b3STejun Heo return 0; 33138603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 33148603e1b3STejun Heo } 33158603e1b3STejun Heo 331636e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3317401a8d04STejun Heo { 33188603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3319bbb68dfaSTejun Heo unsigned long flags; 33201f1f642eSOleg Nesterov int ret; 33211f1f642eSOleg Nesterov 33221f1f642eSOleg Nesterov do { 3323bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3324bbb68dfaSTejun Heo /* 33258603e1b3STejun Heo * If someone else is already canceling, wait for it to 33268603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 33278603e1b3STejun Heo * because we may get scheduled between @work's completion 33288603e1b3STejun Heo * and the other canceling task resuming and clearing 33298603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 33308603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 33318603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 33328603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 33338603e1b3STejun Heo * we're hogging the CPU. 33348603e1b3STejun Heo * 33358603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 33368603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 33378603e1b3STejun Heo * wake function which matches @work along with exclusive 33388603e1b3STejun Heo * wait and wakeup. 3339bbb68dfaSTejun Heo */ 33408603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 33418603e1b3STejun Heo struct cwt_wait cwait; 33428603e1b3STejun Heo 33438603e1b3STejun Heo init_wait(&cwait.wait); 33448603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 33458603e1b3STejun Heo cwait.work = work; 33468603e1b3STejun Heo 33478603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 33488603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 33498603e1b3STejun Heo if (work_is_canceling(work)) 33508603e1b3STejun Heo schedule(); 33518603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 33528603e1b3STejun Heo } 33531f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 33541f1f642eSOleg Nesterov 3355bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3356bbb68dfaSTejun Heo mark_work_canceling(work); 3357bbb68dfaSTejun Heo local_irq_restore(flags); 3358bbb68dfaSTejun Heo 33593347fa09STejun Heo /* 33603347fa09STejun Heo * This allows canceling during early boot. We know that @work 33613347fa09STejun Heo * isn't executing. 33623347fa09STejun Heo */ 33633347fa09STejun Heo if (wq_online) 3364d6e89786SJohannes Berg __flush_work(work, true); 33653347fa09STejun Heo 33667a22ad75STejun Heo clear_work_data(work); 33678603e1b3STejun Heo 33688603e1b3STejun Heo /* 33698603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 33708603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 33718603e1b3STejun Heo * visible there. 33728603e1b3STejun Heo */ 33738603e1b3STejun Heo smp_mb(); 33748603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 33758603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 33768603e1b3STejun Heo 33771f1f642eSOleg Nesterov return ret; 33781f1f642eSOleg Nesterov } 33791f1f642eSOleg Nesterov 33806e84d644SOleg Nesterov /** 3381401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3382401a8d04STejun Heo * @work: the work to cancel 33836e84d644SOleg Nesterov * 3384401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3385401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3386401a8d04STejun Heo * another workqueue. On return from this function, @work is 3387401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 33881f1f642eSOleg Nesterov * 3389401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3390401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 33916e84d644SOleg Nesterov * 3392401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 33936e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3394401a8d04STejun Heo * 3395d185af30SYacine Belkadi * Return: 3396401a8d04STejun Heo * %true if @work was pending, %false otherwise. 33976e84d644SOleg Nesterov */ 3398401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 33996e84d644SOleg Nesterov { 340036e227d2STejun Heo return __cancel_work_timer(work, false); 3401b89deed3SOleg Nesterov } 340228e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3403b89deed3SOleg Nesterov 34046e84d644SOleg Nesterov /** 3405401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3406401a8d04STejun Heo * @dwork: the delayed work to flush 34076e84d644SOleg Nesterov * 3408401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3409401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3410401a8d04STejun Heo * considers the last queueing instance of @dwork. 34111f1f642eSOleg Nesterov * 3412d185af30SYacine Belkadi * Return: 3413401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3414401a8d04STejun Heo * %false if it was already idle. 34156e84d644SOleg Nesterov */ 3416401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3417401a8d04STejun Heo { 34188930cabaSTejun Heo local_irq_disable(); 3419401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 342060c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 34218930cabaSTejun Heo local_irq_enable(); 3422401a8d04STejun Heo return flush_work(&dwork->work); 3423401a8d04STejun Heo } 3424401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3425401a8d04STejun Heo 342605f0fe6bSTejun Heo /** 342705f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 342805f0fe6bSTejun Heo * @rwork: the rcu work to flush 342905f0fe6bSTejun Heo * 343005f0fe6bSTejun Heo * Return: 343105f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 343205f0fe6bSTejun Heo * %false if it was already idle. 343305f0fe6bSTejun Heo */ 343405f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 343505f0fe6bSTejun Heo { 343605f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 343705f0fe6bSTejun Heo rcu_barrier(); 343805f0fe6bSTejun Heo flush_work(&rwork->work); 343905f0fe6bSTejun Heo return true; 344005f0fe6bSTejun Heo } else { 344105f0fe6bSTejun Heo return flush_work(&rwork->work); 344205f0fe6bSTejun Heo } 344305f0fe6bSTejun Heo } 344405f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 344505f0fe6bSTejun Heo 3446f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3447f72b8792SJens Axboe { 3448f72b8792SJens Axboe unsigned long flags; 3449f72b8792SJens Axboe int ret; 3450f72b8792SJens Axboe 3451f72b8792SJens Axboe do { 3452f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3453f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3454f72b8792SJens Axboe 3455f72b8792SJens Axboe if (unlikely(ret < 0)) 3456f72b8792SJens Axboe return false; 3457f72b8792SJens Axboe 3458f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3459f72b8792SJens Axboe local_irq_restore(flags); 3460f72b8792SJens Axboe return ret; 3461f72b8792SJens Axboe } 3462f72b8792SJens Axboe 346373b4b532SAndrey Grodzovsky /* 346473b4b532SAndrey Grodzovsky * See cancel_delayed_work() 346573b4b532SAndrey Grodzovsky */ 346673b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 346773b4b532SAndrey Grodzovsky { 346873b4b532SAndrey Grodzovsky return __cancel_work(work, false); 346973b4b532SAndrey Grodzovsky } 347073b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 347173b4b532SAndrey Grodzovsky 3472401a8d04STejun Heo /** 347357b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 347457b30ae7STejun Heo * @dwork: delayed_work to cancel 347509383498STejun Heo * 3476d185af30SYacine Belkadi * Kill off a pending delayed_work. 3477d185af30SYacine Belkadi * 3478d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3479d185af30SYacine Belkadi * pending. 3480d185af30SYacine Belkadi * 3481d185af30SYacine Belkadi * Note: 3482d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3483d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3484d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 348509383498STejun Heo * 348657b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 348709383498STejun Heo */ 348857b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 348909383498STejun Heo { 3490f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 349109383498STejun Heo } 349257b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 349309383498STejun Heo 349409383498STejun Heo /** 3495401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3496401a8d04STejun Heo * @dwork: the delayed work cancel 3497401a8d04STejun Heo * 3498401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3499401a8d04STejun Heo * 3500d185af30SYacine Belkadi * Return: 3501401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3502401a8d04STejun Heo */ 3503401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 35046e84d644SOleg Nesterov { 350536e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 35066e84d644SOleg Nesterov } 3507f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 35081da177e4SLinus Torvalds 35090fcb78c2SRolf Eike Beer /** 351031ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3511b6136773SAndrew Morton * @func: the function to call 3512b6136773SAndrew Morton * 351331ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 351431ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3515b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 351631ddd871STejun Heo * 3517d185af30SYacine Belkadi * Return: 351831ddd871STejun Heo * 0 on success, -errno on failure. 3519b6136773SAndrew Morton */ 352065f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 352115316ba8SChristoph Lameter { 352215316ba8SChristoph Lameter int cpu; 352338f51568SNamhyung Kim struct work_struct __percpu *works; 352415316ba8SChristoph Lameter 3525b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3526b6136773SAndrew Morton if (!works) 352715316ba8SChristoph Lameter return -ENOMEM; 3528b6136773SAndrew Morton 3529ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 353093981800STejun Heo 353115316ba8SChristoph Lameter for_each_online_cpu(cpu) { 35329bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 35339bfb1839SIngo Molnar 35349bfb1839SIngo Molnar INIT_WORK(work, func); 35358de6d308SOleg Nesterov schedule_work_on(cpu, work); 353615316ba8SChristoph Lameter } 353793981800STejun Heo 353893981800STejun Heo for_each_online_cpu(cpu) 35398616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 354093981800STejun Heo 3541ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3542b6136773SAndrew Morton free_percpu(works); 354315316ba8SChristoph Lameter return 0; 354415316ba8SChristoph Lameter } 354515316ba8SChristoph Lameter 3546eef6a7d5SAlan Stern /** 35471fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 35481fa44ecaSJames Bottomley * @fn: the function to execute 35491fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 35501fa44ecaSJames Bottomley * be available when the work executes) 35511fa44ecaSJames Bottomley * 35521fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 35531fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 35541fa44ecaSJames Bottomley * 3555d185af30SYacine Belkadi * Return: 0 - function was executed 35561fa44ecaSJames Bottomley * 1 - function was scheduled for execution 35571fa44ecaSJames Bottomley */ 355865f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 35591fa44ecaSJames Bottomley { 35601fa44ecaSJames Bottomley if (!in_interrupt()) { 356165f27f38SDavid Howells fn(&ew->work); 35621fa44ecaSJames Bottomley return 0; 35631fa44ecaSJames Bottomley } 35641fa44ecaSJames Bottomley 356565f27f38SDavid Howells INIT_WORK(&ew->work, fn); 35661fa44ecaSJames Bottomley schedule_work(&ew->work); 35671fa44ecaSJames Bottomley 35681fa44ecaSJames Bottomley return 1; 35691fa44ecaSJames Bottomley } 35701fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 35711fa44ecaSJames Bottomley 35727a4e344cSTejun Heo /** 35737a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 35747a4e344cSTejun Heo * @attrs: workqueue_attrs to free 35757a4e344cSTejun Heo * 35767a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 35777a4e344cSTejun Heo */ 3578513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 35797a4e344cSTejun Heo { 35807a4e344cSTejun Heo if (attrs) { 35817a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 35827a4e344cSTejun Heo kfree(attrs); 35837a4e344cSTejun Heo } 35847a4e344cSTejun Heo } 35857a4e344cSTejun Heo 35867a4e344cSTejun Heo /** 35877a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 35887a4e344cSTejun Heo * 35897a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3590d185af30SYacine Belkadi * return it. 3591d185af30SYacine Belkadi * 3592d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 35937a4e344cSTejun Heo */ 3594513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 35957a4e344cSTejun Heo { 35967a4e344cSTejun Heo struct workqueue_attrs *attrs; 35977a4e344cSTejun Heo 3598be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 35997a4e344cSTejun Heo if (!attrs) 36007a4e344cSTejun Heo goto fail; 3601be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 36027a4e344cSTejun Heo goto fail; 36037a4e344cSTejun Heo 360413e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 36057a4e344cSTejun Heo return attrs; 36067a4e344cSTejun Heo fail: 36077a4e344cSTejun Heo free_workqueue_attrs(attrs); 36087a4e344cSTejun Heo return NULL; 36097a4e344cSTejun Heo } 36107a4e344cSTejun Heo 361129c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 361229c91e99STejun Heo const struct workqueue_attrs *from) 361329c91e99STejun Heo { 361429c91e99STejun Heo to->nice = from->nice; 361529c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 36162865a8fbSShaohua Li /* 36172865a8fbSShaohua Li * Unlike hash and equality test, this function doesn't ignore 36182865a8fbSShaohua Li * ->no_numa as it is used for both pool and wq attrs. Instead, 36192865a8fbSShaohua Li * get_unbound_pool() explicitly clears ->no_numa after copying. 36202865a8fbSShaohua Li */ 36212865a8fbSShaohua Li to->no_numa = from->no_numa; 362229c91e99STejun Heo } 362329c91e99STejun Heo 362429c91e99STejun Heo /* hash value of the content of @attr */ 362529c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 362629c91e99STejun Heo { 362729c91e99STejun Heo u32 hash = 0; 362829c91e99STejun Heo 362929c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 363013e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 363113e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 363229c91e99STejun Heo return hash; 363329c91e99STejun Heo } 363429c91e99STejun Heo 363529c91e99STejun Heo /* content equality test */ 363629c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 363729c91e99STejun Heo const struct workqueue_attrs *b) 363829c91e99STejun Heo { 363929c91e99STejun Heo if (a->nice != b->nice) 364029c91e99STejun Heo return false; 364129c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 364229c91e99STejun Heo return false; 364329c91e99STejun Heo return true; 364429c91e99STejun Heo } 364529c91e99STejun Heo 36467a4e344cSTejun Heo /** 36477a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 36487a4e344cSTejun Heo * @pool: worker_pool to initialize 36497a4e344cSTejun Heo * 3650402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 3651d185af30SYacine Belkadi * 3652d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 365329c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 365429c91e99STejun Heo * on @pool safely to release it. 36557a4e344cSTejun Heo */ 36567a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 36574e1a1f9aSTejun Heo { 3658a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 365929c91e99STejun Heo pool->id = -1; 366029c91e99STejun Heo pool->cpu = -1; 3661f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 36624e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 366382607adcSTejun Heo pool->watchdog_ts = jiffies; 36644e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 36654e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 36664e1a1f9aSTejun Heo hash_init(pool->busy_hash); 36674e1a1f9aSTejun Heo 366832a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 36693f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 36704e1a1f9aSTejun Heo 367132a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 36724e1a1f9aSTejun Heo 3673da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 3674e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 36757a4e344cSTejun Heo 36767cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 367729c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 367829c91e99STejun Heo pool->refcnt = 1; 367929c91e99STejun Heo 368029c91e99STejun Heo /* shouldn't fail above this point */ 3681be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 36827a4e344cSTejun Heo if (!pool->attrs) 36837a4e344cSTejun Heo return -ENOMEM; 36847a4e344cSTejun Heo return 0; 36854e1a1f9aSTejun Heo } 36864e1a1f9aSTejun Heo 3687669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 3688669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3689669de8bdSBart Van Assche { 3690669de8bdSBart Van Assche char *lock_name; 3691669de8bdSBart Van Assche 3692669de8bdSBart Van Assche lockdep_register_key(&wq->key); 3693669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 3694669de8bdSBart Van Assche if (!lock_name) 3695669de8bdSBart Van Assche lock_name = wq->name; 369669a106c0SQian Cai 369769a106c0SQian Cai wq->lock_name = lock_name; 3698669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 3699669de8bdSBart Van Assche } 3700669de8bdSBart Van Assche 3701669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3702669de8bdSBart Van Assche { 3703669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 3704669de8bdSBart Van Assche } 3705669de8bdSBart Van Assche 3706669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3707669de8bdSBart Van Assche { 3708669de8bdSBart Van Assche if (wq->lock_name != wq->name) 3709669de8bdSBart Van Assche kfree(wq->lock_name); 3710669de8bdSBart Van Assche } 3711669de8bdSBart Van Assche #else 3712669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 3713669de8bdSBart Van Assche { 3714669de8bdSBart Van Assche } 3715669de8bdSBart Van Assche 3716669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 3717669de8bdSBart Van Assche { 3718669de8bdSBart Van Assche } 3719669de8bdSBart Van Assche 3720669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 3721669de8bdSBart Van Assche { 3722669de8bdSBart Van Assche } 3723669de8bdSBart Van Assche #endif 3724669de8bdSBart Van Assche 3725e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 3726e2dca7adSTejun Heo { 3727e2dca7adSTejun Heo struct workqueue_struct *wq = 3728e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 3729e2dca7adSTejun Heo 3730669de8bdSBart Van Assche wq_free_lockdep(wq); 3731669de8bdSBart Van Assche 3732e2dca7adSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 3733e2dca7adSTejun Heo free_percpu(wq->cpu_pwqs); 3734e2dca7adSTejun Heo else 3735e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 3736e2dca7adSTejun Heo 3737e2dca7adSTejun Heo kfree(wq); 3738e2dca7adSTejun Heo } 3739e2dca7adSTejun Heo 374029c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 374129c91e99STejun Heo { 374229c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 374329c91e99STejun Heo 37447cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 374529c91e99STejun Heo free_workqueue_attrs(pool->attrs); 374629c91e99STejun Heo kfree(pool); 374729c91e99STejun Heo } 374829c91e99STejun Heo 374929c91e99STejun Heo /** 375029c91e99STejun Heo * put_unbound_pool - put a worker_pool 375129c91e99STejun Heo * @pool: worker_pool to put 375229c91e99STejun Heo * 375324acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 3754c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 3755c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 3756c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 3757a892caccSTejun Heo * 3758a892caccSTejun Heo * Should be called with wq_pool_mutex held. 375929c91e99STejun Heo */ 376029c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 376129c91e99STejun Heo { 376260f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 3763e02b9312SValentin Schneider struct list_head cull_list; 376429c91e99STejun Heo struct worker *worker; 376529c91e99STejun Heo 3766e02b9312SValentin Schneider INIT_LIST_HEAD(&cull_list); 3767e02b9312SValentin Schneider 3768a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3769a892caccSTejun Heo 3770a892caccSTejun Heo if (--pool->refcnt) 377129c91e99STejun Heo return; 377229c91e99STejun Heo 377329c91e99STejun Heo /* sanity checks */ 377461d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 3775a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 377629c91e99STejun Heo return; 377729c91e99STejun Heo 377829c91e99STejun Heo /* release id and unhash */ 377929c91e99STejun Heo if (pool->id >= 0) 378029c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 378129c91e99STejun Heo hash_del(&pool->hash_node); 378229c91e99STejun Heo 3783c5aa87bbSTejun Heo /* 3784692b4825STejun Heo * Become the manager and destroy all workers. This prevents 3785692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 3786692b4825STejun Heo * manager and @pool gets freed with the flag set. 37879ab03be4SValentin Schneider * 37889ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 37899ab03be4SValentin Schneider * only get here with 37909ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 37919ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 37929ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 37939ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 37949ab03be4SValentin Schneider * drops pool->lock 3795c5aa87bbSTejun Heo */ 37969ab03be4SValentin Schneider while (true) { 37979ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 37989ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 3799d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 3800e02b9312SValentin Schneider 3801e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 38029ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 38039ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 3804692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 38059ab03be4SValentin Schneider break; 38069ab03be4SValentin Schneider } 38079ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 3808e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 38099ab03be4SValentin Schneider } 3810692b4825STejun Heo 38111037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 3812e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 381329c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 3814a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 381560f5a4bcSLai Jiangshan 3816e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3817e02b9312SValentin Schneider 3818e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 381960f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 38201258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 382160f5a4bcSLai Jiangshan 382260f5a4bcSLai Jiangshan if (pool->detach_completion) 382360f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 382460f5a4bcSLai Jiangshan 382529c91e99STejun Heo /* shut down the timers */ 382629c91e99STejun Heo del_timer_sync(&pool->idle_timer); 38273f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 382829c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 382929c91e99STejun Heo 383024acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 383125b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 383229c91e99STejun Heo } 383329c91e99STejun Heo 383429c91e99STejun Heo /** 383529c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 383629c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 383729c91e99STejun Heo * 383829c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 383929c91e99STejun Heo * reference count and return it. If there already is a matching 384029c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 3841d185af30SYacine Belkadi * create a new one. 3842a892caccSTejun Heo * 3843a892caccSTejun Heo * Should be called with wq_pool_mutex held. 3844d185af30SYacine Belkadi * 3845d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 3846d185af30SYacine Belkadi * On failure, %NULL. 384729c91e99STejun Heo */ 384829c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 384929c91e99STejun Heo { 385029c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 385129c91e99STejun Heo struct worker_pool *pool; 3852f3f90ad4STejun Heo int node; 3853e2273584SXunlei Pang int target_node = NUMA_NO_NODE; 385429c91e99STejun Heo 3855a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 385629c91e99STejun Heo 385729c91e99STejun Heo /* do we already have a matching pool? */ 385829c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 385929c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 386029c91e99STejun Heo pool->refcnt++; 38613fb1823cSLai Jiangshan return pool; 386229c91e99STejun Heo } 386329c91e99STejun Heo } 386429c91e99STejun Heo 3865e2273584SXunlei Pang /* if cpumask is contained inside a NUMA node, we belong to that node */ 3866e2273584SXunlei Pang if (wq_numa_enabled) { 3867e2273584SXunlei Pang for_each_node(node) { 3868e2273584SXunlei Pang if (cpumask_subset(attrs->cpumask, 3869e2273584SXunlei Pang wq_numa_possible_cpumask[node])) { 3870e2273584SXunlei Pang target_node = node; 3871e2273584SXunlei Pang break; 3872e2273584SXunlei Pang } 3873e2273584SXunlei Pang } 3874e2273584SXunlei Pang } 3875e2273584SXunlei Pang 387629c91e99STejun Heo /* nope, create a new one */ 3877e2273584SXunlei Pang pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, target_node); 387829c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 387929c91e99STejun Heo goto fail; 388029c91e99STejun Heo 38818864b4e5STejun Heo lockdep_set_subclass(&pool->lock, 1); /* see put_pwq() */ 388229c91e99STejun Heo copy_workqueue_attrs(pool->attrs, attrs); 3883e2273584SXunlei Pang pool->node = target_node; 388429c91e99STejun Heo 38852865a8fbSShaohua Li /* 38862865a8fbSShaohua Li * no_numa isn't a worker_pool attribute, always clear it. See 38872865a8fbSShaohua Li * 'struct workqueue_attrs' comments for detail. 38882865a8fbSShaohua Li */ 38892865a8fbSShaohua Li pool->attrs->no_numa = false; 38902865a8fbSShaohua Li 389129c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 389229c91e99STejun Heo goto fail; 389329c91e99STejun Heo 389429c91e99STejun Heo /* create and start the initial worker */ 38953347fa09STejun Heo if (wq_online && !create_worker(pool)) 389629c91e99STejun Heo goto fail; 389729c91e99STejun Heo 389829c91e99STejun Heo /* install */ 389929c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 39003fb1823cSLai Jiangshan 390129c91e99STejun Heo return pool; 390229c91e99STejun Heo fail: 390329c91e99STejun Heo if (pool) 390429c91e99STejun Heo put_unbound_pool(pool); 390529c91e99STejun Heo return NULL; 390629c91e99STejun Heo } 390729c91e99STejun Heo 39088864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 39098864b4e5STejun Heo { 39108864b4e5STejun Heo kmem_cache_free(pwq_cache, 39118864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 39128864b4e5STejun Heo } 39138864b4e5STejun Heo 39148864b4e5STejun Heo /* 39158864b4e5STejun Heo * Scheduled on system_wq by put_pwq() when an unbound pwq hits zero refcnt 39168864b4e5STejun Heo * and needs to be destroyed. 39178864b4e5STejun Heo */ 39188864b4e5STejun Heo static void pwq_unbound_release_workfn(struct work_struct *work) 39198864b4e5STejun Heo { 39208864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 39218864b4e5STejun Heo unbound_release_work); 39228864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 39238864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 3924b42b0bddSYang Yingliang bool is_last = false; 39258864b4e5STejun Heo 3926b42b0bddSYang Yingliang /* 3927b42b0bddSYang Yingliang * when @pwq is not linked, it doesn't hold any reference to the 3928b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 3929b42b0bddSYang Yingliang */ 3930b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 39318864b4e5STejun Heo if (WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND))) 39328864b4e5STejun Heo return; 39338864b4e5STejun Heo 39343c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 39358864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 3936bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 39373c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 3938b42b0bddSYang Yingliang } 39398864b4e5STejun Heo 3940a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 39418864b4e5STejun Heo put_unbound_pool(pool); 3942a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 3943a892caccSTejun Heo 394425b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 39458864b4e5STejun Heo 39468864b4e5STejun Heo /* 39478864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 3948e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 39498864b4e5STejun Heo */ 3950669de8bdSBart Van Assche if (is_last) { 3951669de8bdSBart Van Assche wq_unregister_lockdep(wq); 395225b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 39536029a918STejun Heo } 3954669de8bdSBart Van Assche } 39558864b4e5STejun Heo 39560fbd95aaSTejun Heo /** 3957699ce097STejun Heo * pwq_adjust_max_active - update a pwq's max_active to the current setting 39580fbd95aaSTejun Heo * @pwq: target pool_workqueue 39590fbd95aaSTejun Heo * 3960699ce097STejun Heo * If @pwq isn't freezing, set @pwq->max_active to the associated 3961f97a4a1aSLai Jiangshan * workqueue's saved_max_active and activate inactive work items 3962699ce097STejun Heo * accordingly. If @pwq is freezing, clear @pwq->max_active to zero. 39630fbd95aaSTejun Heo */ 3964699ce097STejun Heo static void pwq_adjust_max_active(struct pool_workqueue *pwq) 39650fbd95aaSTejun Heo { 3966699ce097STejun Heo struct workqueue_struct *wq = pwq->wq; 3967699ce097STejun Heo bool freezable = wq->flags & WQ_FREEZABLE; 39683347fa09STejun Heo unsigned long flags; 3969699ce097STejun Heo 3970699ce097STejun Heo /* for @wq->saved_max_active */ 3971a357fc03SLai Jiangshan lockdep_assert_held(&wq->mutex); 3972699ce097STejun Heo 3973699ce097STejun Heo /* fast exit for non-freezable wqs */ 3974699ce097STejun Heo if (!freezable && pwq->max_active == wq->saved_max_active) 3975699ce097STejun Heo return; 3976699ce097STejun Heo 39773347fa09STejun Heo /* this function can be called during early boot w/ irq disabled */ 3978a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 3979699ce097STejun Heo 398074b414eaSLai Jiangshan /* 398174b414eaSLai Jiangshan * During [un]freezing, the caller is responsible for ensuring that 398274b414eaSLai Jiangshan * this function is called at least once after @workqueue_freezing 398374b414eaSLai Jiangshan * is updated and visible. 398474b414eaSLai Jiangshan */ 398574b414eaSLai Jiangshan if (!freezable || !workqueue_freezing) { 398601341fbdSYunfeng Ye bool kick = false; 398701341fbdSYunfeng Ye 3988699ce097STejun Heo pwq->max_active = wq->saved_max_active; 39890fbd95aaSTejun Heo 3990f97a4a1aSLai Jiangshan while (!list_empty(&pwq->inactive_works) && 399101341fbdSYunfeng Ye pwq->nr_active < pwq->max_active) { 3992f97a4a1aSLai Jiangshan pwq_activate_first_inactive(pwq); 399301341fbdSYunfeng Ye kick = true; 399401341fbdSYunfeng Ye } 3995951a078aSLai Jiangshan 3996951a078aSLai Jiangshan /* 3997951a078aSLai Jiangshan * Need to kick a worker after thawed or an unbound wq's 399801341fbdSYunfeng Ye * max_active is bumped. In realtime scenarios, always kicking a 399901341fbdSYunfeng Ye * worker will cause interference on the isolated cpu cores, so 400001341fbdSYunfeng Ye * let's kick iff work items were activated. 4001951a078aSLai Jiangshan */ 400201341fbdSYunfeng Ye if (kick) 4003951a078aSLai Jiangshan wake_up_worker(pwq->pool); 4004699ce097STejun Heo } else { 4005699ce097STejun Heo pwq->max_active = 0; 4006699ce097STejun Heo } 4007699ce097STejun Heo 4008a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 40090fbd95aaSTejun Heo } 40100fbd95aaSTejun Heo 401167dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4012f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4013f147f29eSTejun Heo struct worker_pool *pool) 4014d2c1d404STejun Heo { 4015d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4016d2c1d404STejun Heo 4017e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4018e50aba9aSTejun Heo 4019d2c1d404STejun Heo pwq->pool = pool; 4020d2c1d404STejun Heo pwq->wq = wq; 4021d2c1d404STejun Heo pwq->flush_color = -1; 40228864b4e5STejun Heo pwq->refcnt = 1; 4023f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 40241befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4025d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 40268864b4e5STejun Heo INIT_WORK(&pwq->unbound_release_work, pwq_unbound_release_workfn); 4027f147f29eSTejun Heo } 4028d2c1d404STejun Heo 4029f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 40301befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4031f147f29eSTejun Heo { 4032f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4033f147f29eSTejun Heo 4034f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 403575ccf595STejun Heo 40361befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 40371befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 40381befcf30STejun Heo return; 40391befcf30STejun Heo 404029b1cb41SLai Jiangshan /* set the matching work_color */ 404175ccf595STejun Heo pwq->work_color = wq->work_color; 4042983ca25eSTejun Heo 4043983ca25eSTejun Heo /* sync max_active to the current setting */ 4044983ca25eSTejun Heo pwq_adjust_max_active(pwq); 4045983ca25eSTejun Heo 4046983ca25eSTejun Heo /* link in @pwq */ 40479e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4048df2d5ae4STejun Heo } 40496029a918STejun Heo 4050f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4051f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4052f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4053f147f29eSTejun Heo { 4054f147f29eSTejun Heo struct worker_pool *pool; 4055f147f29eSTejun Heo struct pool_workqueue *pwq; 4056f147f29eSTejun Heo 4057f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4058f147f29eSTejun Heo 4059f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4060f147f29eSTejun Heo if (!pool) 4061f147f29eSTejun Heo return NULL; 4062f147f29eSTejun Heo 4063e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4064f147f29eSTejun Heo if (!pwq) { 4065f147f29eSTejun Heo put_unbound_pool(pool); 4066f147f29eSTejun Heo return NULL; 4067f147f29eSTejun Heo } 4068f147f29eSTejun Heo 4069f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4070f147f29eSTejun Heo return pwq; 4071d2c1d404STejun Heo } 4072d2c1d404STejun Heo 40734c16bd32STejun Heo /** 407430186c6fSGong Zhaogang * wq_calc_node_cpumask - calculate a wq_attrs' cpumask for the specified node 4075042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 40764c16bd32STejun Heo * @node: the target NUMA node 40774c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 40784c16bd32STejun Heo * @cpumask: outarg, the resulting cpumask 40794c16bd32STejun Heo * 40804c16bd32STejun Heo * Calculate the cpumask a workqueue with @attrs should use on @node. If 40814c16bd32STejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during 4082d185af30SYacine Belkadi * calculation. The result is stored in @cpumask. 40834c16bd32STejun Heo * 40844c16bd32STejun Heo * If NUMA affinity is not enabled, @attrs->cpumask is always used. If 40854c16bd32STejun Heo * enabled and @node has online CPUs requested by @attrs, the returned 40864c16bd32STejun Heo * cpumask is the intersection of the possible CPUs of @node and 40874c16bd32STejun Heo * @attrs->cpumask. 40884c16bd32STejun Heo * 40894c16bd32STejun Heo * The caller is responsible for ensuring that the cpumask of @node stays 40904c16bd32STejun Heo * stable. 4091d185af30SYacine Belkadi * 4092d185af30SYacine Belkadi * Return: %true if the resulting @cpumask is different from @attrs->cpumask, 4093d185af30SYacine Belkadi * %false if equal. 40944c16bd32STejun Heo */ 40954c16bd32STejun Heo static bool wq_calc_node_cpumask(const struct workqueue_attrs *attrs, int node, 40964c16bd32STejun Heo int cpu_going_down, cpumask_t *cpumask) 40974c16bd32STejun Heo { 4098d55262c4STejun Heo if (!wq_numa_enabled || attrs->no_numa) 40994c16bd32STejun Heo goto use_dfl; 41004c16bd32STejun Heo 41014c16bd32STejun Heo /* does @node have any online CPUs @attrs wants? */ 41024c16bd32STejun Heo cpumask_and(cpumask, cpumask_of_node(node), attrs->cpumask); 41034c16bd32STejun Heo if (cpu_going_down >= 0) 41044c16bd32STejun Heo cpumask_clear_cpu(cpu_going_down, cpumask); 41054c16bd32STejun Heo 41064c16bd32STejun Heo if (cpumask_empty(cpumask)) 41074c16bd32STejun Heo goto use_dfl; 41084c16bd32STejun Heo 41094c16bd32STejun Heo /* yeap, return possible CPUs in @node that @attrs wants */ 41104c16bd32STejun Heo cpumask_and(cpumask, attrs->cpumask, wq_numa_possible_cpumask[node]); 41111ad0f0a7SMichael Bringmann 41121ad0f0a7SMichael Bringmann if (cpumask_empty(cpumask)) { 41131ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 41141ad0f0a7SMichael Bringmann "possible intersect\n"); 41151ad0f0a7SMichael Bringmann return false; 41161ad0f0a7SMichael Bringmann } 41171ad0f0a7SMichael Bringmann 41184c16bd32STejun Heo return !cpumask_equal(cpumask, attrs->cpumask); 41194c16bd32STejun Heo 41204c16bd32STejun Heo use_dfl: 41214c16bd32STejun Heo cpumask_copy(cpumask, attrs->cpumask); 41224c16bd32STejun Heo return false; 41234c16bd32STejun Heo } 41244c16bd32STejun Heo 41251befcf30STejun Heo /* install @pwq into @wq's numa_pwq_tbl[] for @node and return the old pwq */ 41261befcf30STejun Heo static struct pool_workqueue *numa_pwq_tbl_install(struct workqueue_struct *wq, 41271befcf30STejun Heo int node, 41281befcf30STejun Heo struct pool_workqueue *pwq) 41291befcf30STejun Heo { 41301befcf30STejun Heo struct pool_workqueue *old_pwq; 41311befcf30STejun Heo 41325b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 41331befcf30STejun Heo lockdep_assert_held(&wq->mutex); 41341befcf30STejun Heo 41351befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 41361befcf30STejun Heo link_pwq(pwq); 41371befcf30STejun Heo 41381befcf30STejun Heo old_pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 41391befcf30STejun Heo rcu_assign_pointer(wq->numa_pwq_tbl[node], pwq); 41401befcf30STejun Heo return old_pwq; 41411befcf30STejun Heo } 41421befcf30STejun Heo 41432d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 41442d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 41452d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 41462d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4147042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 41482d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 41492d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 41502d5f0764SLai Jiangshan }; 41512d5f0764SLai Jiangshan 41522d5f0764SLai Jiangshan /* free the resources after success or abort */ 41532d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 41542d5f0764SLai Jiangshan { 41552d5f0764SLai Jiangshan if (ctx) { 41562d5f0764SLai Jiangshan int node; 41572d5f0764SLai Jiangshan 41582d5f0764SLai Jiangshan for_each_node(node) 41592d5f0764SLai Jiangshan put_pwq_unlocked(ctx->pwq_tbl[node]); 41602d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 41612d5f0764SLai Jiangshan 41622d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 41632d5f0764SLai Jiangshan 41642d5f0764SLai Jiangshan kfree(ctx); 41652d5f0764SLai Jiangshan } 41662d5f0764SLai Jiangshan } 41672d5f0764SLai Jiangshan 41682d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 41692d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 41702d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 417199c621efSLai Jiangshan const struct workqueue_attrs *attrs, 417299c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 41732d5f0764SLai Jiangshan { 41742d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 41752d5f0764SLai Jiangshan struct workqueue_attrs *new_attrs, *tmp_attrs; 41762d5f0764SLai Jiangshan int node; 41772d5f0764SLai Jiangshan 41782d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 41792d5f0764SLai Jiangshan 4180acafe7e3SKees Cook ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_node_ids), GFP_KERNEL); 41812d5f0764SLai Jiangshan 4182be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 4183be69d00dSThomas Gleixner tmp_attrs = alloc_workqueue_attrs(); 41842d5f0764SLai Jiangshan if (!ctx || !new_attrs || !tmp_attrs) 41852d5f0764SLai Jiangshan goto out_free; 41862d5f0764SLai Jiangshan 4187042f7df1SLai Jiangshan /* 418899c621efSLai Jiangshan * Calculate the attrs of the default pwq with unbound_cpumask 418999c621efSLai Jiangshan * which is wq_unbound_cpumask or to set to wq_unbound_cpumask. 4190042f7df1SLai Jiangshan * If the user configured cpumask doesn't overlap with the 4191042f7df1SLai Jiangshan * wq_unbound_cpumask, we fallback to the wq_unbound_cpumask. 4192042f7df1SLai Jiangshan */ 41932d5f0764SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 419499c621efSLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, unbound_cpumask); 4195042f7df1SLai Jiangshan if (unlikely(cpumask_empty(new_attrs->cpumask))) 419699c621efSLai Jiangshan cpumask_copy(new_attrs->cpumask, unbound_cpumask); 41972d5f0764SLai Jiangshan 41982d5f0764SLai Jiangshan /* 41992d5f0764SLai Jiangshan * We may create multiple pwqs with differing cpumasks. Make a 42002d5f0764SLai Jiangshan * copy of @new_attrs which will be modified and used to obtain 42012d5f0764SLai Jiangshan * pools. 42022d5f0764SLai Jiangshan */ 42032d5f0764SLai Jiangshan copy_workqueue_attrs(tmp_attrs, new_attrs); 42042d5f0764SLai Jiangshan 42052d5f0764SLai Jiangshan /* 42062d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 42072d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 42082d5f0764SLai Jiangshan * it even if we don't use it immediately. 42092d5f0764SLai Jiangshan */ 42102d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 42112d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 42122d5f0764SLai Jiangshan goto out_free; 42132d5f0764SLai Jiangshan 42142d5f0764SLai Jiangshan for_each_node(node) { 4215042f7df1SLai Jiangshan if (wq_calc_node_cpumask(new_attrs, node, -1, tmp_attrs->cpumask)) { 42162d5f0764SLai Jiangshan ctx->pwq_tbl[node] = alloc_unbound_pwq(wq, tmp_attrs); 42172d5f0764SLai Jiangshan if (!ctx->pwq_tbl[node]) 42182d5f0764SLai Jiangshan goto out_free; 42192d5f0764SLai Jiangshan } else { 42202d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 42212d5f0764SLai Jiangshan ctx->pwq_tbl[node] = ctx->dfl_pwq; 42222d5f0764SLai Jiangshan } 42232d5f0764SLai Jiangshan } 42242d5f0764SLai Jiangshan 4225042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4226042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4227042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 42282d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4229042f7df1SLai Jiangshan 42302d5f0764SLai Jiangshan ctx->wq = wq; 42312d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 42322d5f0764SLai Jiangshan return ctx; 42332d5f0764SLai Jiangshan 42342d5f0764SLai Jiangshan out_free: 42352d5f0764SLai Jiangshan free_workqueue_attrs(tmp_attrs); 42362d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 42372d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 42382d5f0764SLai Jiangshan return NULL; 42392d5f0764SLai Jiangshan } 42402d5f0764SLai Jiangshan 42412d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 42422d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 42432d5f0764SLai Jiangshan { 42442d5f0764SLai Jiangshan int node; 42452d5f0764SLai Jiangshan 42462d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 42472d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 42482d5f0764SLai Jiangshan 42492d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 42502d5f0764SLai Jiangshan 42512d5f0764SLai Jiangshan /* save the previous pwq and install the new one */ 42522d5f0764SLai Jiangshan for_each_node(node) 42532d5f0764SLai Jiangshan ctx->pwq_tbl[node] = numa_pwq_tbl_install(ctx->wq, node, 42542d5f0764SLai Jiangshan ctx->pwq_tbl[node]); 42552d5f0764SLai Jiangshan 42562d5f0764SLai Jiangshan /* @dfl_pwq might not have been used, ensure it's linked */ 42572d5f0764SLai Jiangshan link_pwq(ctx->dfl_pwq); 42582d5f0764SLai Jiangshan swap(ctx->wq->dfl_pwq, ctx->dfl_pwq); 42592d5f0764SLai Jiangshan 42602d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 42612d5f0764SLai Jiangshan } 42622d5f0764SLai Jiangshan 4263a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4264a0111cf6SLai Jiangshan { 4265a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4266ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4267a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4268a0111cf6SLai Jiangshan } 4269a0111cf6SLai Jiangshan 4270a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4271a0111cf6SLai Jiangshan { 4272a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4273ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4274a0111cf6SLai Jiangshan } 4275a0111cf6SLai Jiangshan 4276a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4277a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4278a0111cf6SLai Jiangshan { 4279a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4280a0111cf6SLai Jiangshan 4281a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4282a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4283a0111cf6SLai Jiangshan return -EINVAL; 4284a0111cf6SLai Jiangshan 4285a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 42860a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 42870a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4288a0111cf6SLai Jiangshan return -EINVAL; 4289a0111cf6SLai Jiangshan 42900a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 42910a94efb5STejun Heo } 42920a94efb5STejun Heo 429399c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 42946201171eSwanghaibin if (!ctx) 42956201171eSwanghaibin return -ENOMEM; 4296a0111cf6SLai Jiangshan 4297a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4298a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4299a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4300a0111cf6SLai Jiangshan 43016201171eSwanghaibin return 0; 4302a0111cf6SLai Jiangshan } 4303a0111cf6SLai Jiangshan 43049e8cd2f5STejun Heo /** 43059e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 43069e8cd2f5STejun Heo * @wq: the target workqueue 43079e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 43089e8cd2f5STejun Heo * 43094c16bd32STejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, on NUMA 43104c16bd32STejun Heo * machines, this function maps a separate pwq to each NUMA node with 43114c16bd32STejun Heo * possibles CPUs in @attrs->cpumask so that work items are affine to the 43124c16bd32STejun Heo * NUMA node it was issued on. Older pwqs are released as in-flight work 43134c16bd32STejun Heo * items finish. Note that a work item which repeatedly requeues itself 43144c16bd32STejun Heo * back-to-back will stay on its current pwq. 43159e8cd2f5STejun Heo * 4316d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4317d185af30SYacine Belkadi * 4318ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4319509b3204SDaniel Jordan * 4320d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 43219e8cd2f5STejun Heo */ 4322513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 43239e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 43249e8cd2f5STejun Heo { 4325a0111cf6SLai Jiangshan int ret; 43269e8cd2f5STejun Heo 4327509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4328509b3204SDaniel Jordan 4329509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4330a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4331509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 43322d5f0764SLai Jiangshan 43332d5f0764SLai Jiangshan return ret; 43349e8cd2f5STejun Heo } 43359e8cd2f5STejun Heo 43364c16bd32STejun Heo /** 43374c16bd32STejun Heo * wq_update_unbound_numa - update NUMA affinity of a wq for CPU hot[un]plug 43384c16bd32STejun Heo * @wq: the target workqueue 43394c16bd32STejun Heo * @cpu: the CPU coming up or going down 43404c16bd32STejun Heo * @online: whether @cpu is coming up or going down 43414c16bd32STejun Heo * 43424c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 43434c16bd32STejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update NUMA affinity of 43444c16bd32STejun Heo * @wq accordingly. 43454c16bd32STejun Heo * 43464c16bd32STejun Heo * If NUMA affinity can't be adjusted due to memory allocation failure, it 43474c16bd32STejun Heo * falls back to @wq->dfl_pwq which may not be optimal but is always 43484c16bd32STejun Heo * correct. 43494c16bd32STejun Heo * 43504c16bd32STejun Heo * Note that when the last allowed CPU of a NUMA node goes offline for a 43514c16bd32STejun Heo * workqueue with a cpumask spanning multiple nodes, the workers which were 43524c16bd32STejun Heo * already executing the work items for the workqueue will lose their CPU 43534c16bd32STejun Heo * affinity and may execute on any CPU. This is similar to how per-cpu 43544c16bd32STejun Heo * workqueues behave on CPU_DOWN. If a workqueue user wants strict 43554c16bd32STejun Heo * affinity, it's the user's responsibility to flush the work item from 43564c16bd32STejun Heo * CPU_DOWN_PREPARE. 43574c16bd32STejun Heo */ 43584c16bd32STejun Heo static void wq_update_unbound_numa(struct workqueue_struct *wq, int cpu, 43594c16bd32STejun Heo bool online) 43604c16bd32STejun Heo { 43614c16bd32STejun Heo int node = cpu_to_node(cpu); 43624c16bd32STejun Heo int cpu_off = online ? -1 : cpu; 43634c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 43644c16bd32STejun Heo struct workqueue_attrs *target_attrs; 43654c16bd32STejun Heo cpumask_t *cpumask; 43664c16bd32STejun Heo 43674c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 43684c16bd32STejun Heo 4369f7142ed4SLai Jiangshan if (!wq_numa_enabled || !(wq->flags & WQ_UNBOUND) || 4370f7142ed4SLai Jiangshan wq->unbound_attrs->no_numa) 43714c16bd32STejun Heo return; 43724c16bd32STejun Heo 43734c16bd32STejun Heo /* 43744c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 43754c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 43764c16bd32STejun Heo * CPU hotplug exclusion. 43774c16bd32STejun Heo */ 43784c16bd32STejun Heo target_attrs = wq_update_unbound_numa_attrs_buf; 43794c16bd32STejun Heo cpumask = target_attrs->cpumask; 43804c16bd32STejun Heo 43814c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 43824c16bd32STejun Heo pwq = unbound_pwq_by_node(wq, node); 43834c16bd32STejun Heo 43844c16bd32STejun Heo /* 43854c16bd32STejun Heo * Let's determine what needs to be done. If the target cpumask is 4386042f7df1SLai Jiangshan * different from the default pwq's, we need to compare it to @pwq's 4387042f7df1SLai Jiangshan * and create a new one if they don't match. If the target cpumask 4388042f7df1SLai Jiangshan * equals the default pwq's, the default pwq should be used. 43894c16bd32STejun Heo */ 4390042f7df1SLai Jiangshan if (wq_calc_node_cpumask(wq->dfl_pwq->pool->attrs, node, cpu_off, cpumask)) { 43914c16bd32STejun Heo if (cpumask_equal(cpumask, pwq->pool->attrs->cpumask)) 4392f7142ed4SLai Jiangshan return; 43934c16bd32STejun Heo } else { 43944c16bd32STejun Heo goto use_dfl_pwq; 43954c16bd32STejun Heo } 43964c16bd32STejun Heo 43974c16bd32STejun Heo /* create a new pwq */ 43984c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 43994c16bd32STejun Heo if (!pwq) { 44002d916033SFabian Frederick pr_warn("workqueue: allocation failed while updating NUMA affinity of \"%s\"\n", 44014c16bd32STejun Heo wq->name); 440277f300b1SDaeseok Youn goto use_dfl_pwq; 44034c16bd32STejun Heo } 44044c16bd32STejun Heo 4405f7142ed4SLai Jiangshan /* Install the new pwq. */ 44064c16bd32STejun Heo mutex_lock(&wq->mutex); 44074c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, pwq); 44084c16bd32STejun Heo goto out_unlock; 44094c16bd32STejun Heo 44104c16bd32STejun Heo use_dfl_pwq: 4411f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4412a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq->dfl_pwq->pool->lock); 44134c16bd32STejun Heo get_pwq(wq->dfl_pwq); 4414a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq->dfl_pwq->pool->lock); 44154c16bd32STejun Heo old_pwq = numa_pwq_tbl_install(wq, node, wq->dfl_pwq); 44164c16bd32STejun Heo out_unlock: 44174c16bd32STejun Heo mutex_unlock(&wq->mutex); 44184c16bd32STejun Heo put_pwq_unlocked(old_pwq); 44194c16bd32STejun Heo } 44204c16bd32STejun Heo 442130cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 44221da177e4SLinus Torvalds { 442349e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 44248a2b7538STejun Heo int cpu, ret; 4425e1d8aa9fSFrederic Weisbecker 442630cdf249STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4427420c0ddbSTejun Heo wq->cpu_pwqs = alloc_percpu(struct pool_workqueue); 4428420c0ddbSTejun Heo if (!wq->cpu_pwqs) 442930cdf249STejun Heo return -ENOMEM; 443030cdf249STejun Heo 443130cdf249STejun Heo for_each_possible_cpu(cpu) { 44327fb98ea7STejun Heo struct pool_workqueue *pwq = 44337fb98ea7STejun Heo per_cpu_ptr(wq->cpu_pwqs, cpu); 44347a62c2c8STejun Heo struct worker_pool *cpu_pools = 4435f02ae73aSTejun Heo per_cpu(cpu_worker_pools, cpu); 443630cdf249STejun Heo 4437f147f29eSTejun Heo init_pwq(pwq, wq, &cpu_pools[highpri]); 4438f147f29eSTejun Heo 4439f147f29eSTejun Heo mutex_lock(&wq->mutex); 44401befcf30STejun Heo link_pwq(pwq); 4441f147f29eSTejun Heo mutex_unlock(&wq->mutex); 444230cdf249STejun Heo } 444330cdf249STejun Heo return 0; 4444509b3204SDaniel Jordan } 4445509b3204SDaniel Jordan 4446ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4447509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 44488a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 44498a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 44508a2b7538STejun Heo WARN(!ret && (wq->pwqs.next != &wq->dfl_pwq->pwqs_node || 44518a2b7538STejun Heo wq->pwqs.prev != &wq->dfl_pwq->pwqs_node), 44528a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 44539e8cd2f5STejun Heo } else { 4454509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 44559e8cd2f5STejun Heo } 4456ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4457509b3204SDaniel Jordan 4458509b3204SDaniel Jordan return ret; 44590f900049STejun Heo } 44600f900049STejun Heo 4461f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4462f3421797STejun Heo const char *name) 4463b71ab8c2STejun Heo { 4464f3421797STejun Heo int lim = flags & WQ_UNBOUND ? WQ_UNBOUND_MAX_ACTIVE : WQ_MAX_ACTIVE; 4465f3421797STejun Heo 4466f3421797STejun Heo if (max_active < 1 || max_active > lim) 4467044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4468f3421797STejun Heo max_active, name, 1, lim); 4469b71ab8c2STejun Heo 4470f3421797STejun Heo return clamp_val(max_active, 1, lim); 4471b71ab8c2STejun Heo } 4472b71ab8c2STejun Heo 4473983c7515STejun Heo /* 4474983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4475983c7515STejun Heo * to guarantee forward progress. 4476983c7515STejun Heo */ 4477983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4478983c7515STejun Heo { 4479983c7515STejun Heo struct worker *rescuer; 4480b92b36eaSDan Carpenter int ret; 4481983c7515STejun Heo 4482983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4483983c7515STejun Heo return 0; 4484983c7515STejun Heo 4485983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 44864c0736a7SPetr Mladek if (!rescuer) { 44874c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 44884c0736a7SPetr Mladek wq->name); 4489983c7515STejun Heo return -ENOMEM; 44904c0736a7SPetr Mladek } 4491983c7515STejun Heo 4492983c7515STejun Heo rescuer->rescue_wq = wq; 4493983c7515STejun Heo rescuer->task = kthread_create(rescuer_thread, rescuer, "%s", wq->name); 4494f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4495b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 44964c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 44974c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4498983c7515STejun Heo kfree(rescuer); 4499b92b36eaSDan Carpenter return ret; 4500983c7515STejun Heo } 4501983c7515STejun Heo 4502983c7515STejun Heo wq->rescuer = rescuer; 4503983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4504983c7515STejun Heo wake_up_process(rescuer->task); 4505983c7515STejun Heo 4506983c7515STejun Heo return 0; 4507983c7515STejun Heo } 4508983c7515STejun Heo 4509a2775bbcSMathieu Malaterre __printf(1, 4) 4510669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 451197e37d7bSTejun Heo unsigned int flags, 4512669de8bdSBart Van Assche int max_active, ...) 45133af24433SOleg Nesterov { 4514df2d5ae4STejun Heo size_t tbl_size = 0; 4515ecf6881fSTejun Heo va_list args; 45163af24433SOleg Nesterov struct workqueue_struct *wq; 451749e3cf44STejun Heo struct pool_workqueue *pwq; 4518b196be89STejun Heo 45195c0338c6STejun Heo /* 45205c0338c6STejun Heo * Unbound && max_active == 1 used to imply ordered, which is no 45215c0338c6STejun Heo * longer the case on NUMA machines due to per-node pools. While 45225c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 45235c0338c6STejun Heo * workqueue, keep the previous behavior to avoid subtle breakages 45245c0338c6STejun Heo * on NUMA. 45255c0338c6STejun Heo */ 45265c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 45275c0338c6STejun Heo flags |= __WQ_ORDERED; 45285c0338c6STejun Heo 4529cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4530cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4531cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4532cee22a15SViresh Kumar 4533ecf6881fSTejun Heo /* allocate wq and format name */ 4534df2d5ae4STejun Heo if (flags & WQ_UNBOUND) 4535ddcb57e2SLai Jiangshan tbl_size = nr_node_ids * sizeof(wq->numa_pwq_tbl[0]); 4536df2d5ae4STejun Heo 4537df2d5ae4STejun Heo wq = kzalloc(sizeof(*wq) + tbl_size, GFP_KERNEL); 4538b196be89STejun Heo if (!wq) 4539d2c1d404STejun Heo return NULL; 4540b196be89STejun Heo 45416029a918STejun Heo if (flags & WQ_UNBOUND) { 4542be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 45436029a918STejun Heo if (!wq->unbound_attrs) 45446029a918STejun Heo goto err_free_wq; 45456029a918STejun Heo } 45466029a918STejun Heo 4547669de8bdSBart Van Assche va_start(args, max_active); 4548ecf6881fSTejun Heo vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4549b196be89STejun Heo va_end(args); 45503af24433SOleg Nesterov 4551d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4552b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 45533af24433SOleg Nesterov 4554b196be89STejun Heo /* init wq */ 455597e37d7bSTejun Heo wq->flags = flags; 4556a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 45573c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4558112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 455930cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 456073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 456173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4562493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 45633af24433SOleg Nesterov 4564669de8bdSBart Van Assche wq_init_lockdep(wq); 4565cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 45663af24433SOleg Nesterov 456730cdf249STejun Heo if (alloc_and_link_pwqs(wq) < 0) 456882efcab3SBart Van Assche goto err_unreg_lockdep; 45691537663fSTejun Heo 457040c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4571d2c1d404STejun Heo goto err_destroy; 4572e22bee78STejun Heo 4573226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4574226223abSTejun Heo goto err_destroy; 4575226223abSTejun Heo 45766af8bf3dSOleg Nesterov /* 457768e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 457868e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 457968e13a67SLai Jiangshan * list. 45806af8bf3dSOleg Nesterov */ 458168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4582a0a1a5fdSTejun Heo 4583a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 458449e3cf44STejun Heo for_each_pwq(pwq, wq) 4585699ce097STejun Heo pwq_adjust_max_active(pwq); 4586a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4587a0a1a5fdSTejun Heo 4588e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4589a0a1a5fdSTejun Heo 459068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 45913af24433SOleg Nesterov 45923af24433SOleg Nesterov return wq; 4593d2c1d404STejun Heo 459482efcab3SBart Van Assche err_unreg_lockdep: 4595009bb421SBart Van Assche wq_unregister_lockdep(wq); 4596009bb421SBart Van Assche wq_free_lockdep(wq); 459782efcab3SBart Van Assche err_free_wq: 45986029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 45994690c4abSTejun Heo kfree(wq); 4600d2c1d404STejun Heo return NULL; 4601d2c1d404STejun Heo err_destroy: 4602d2c1d404STejun Heo destroy_workqueue(wq); 46034690c4abSTejun Heo return NULL; 46041da177e4SLinus Torvalds } 4605669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 46061da177e4SLinus Torvalds 4607c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4608c29eb853STejun Heo { 4609c29eb853STejun Heo int i; 4610c29eb853STejun Heo 4611c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 4612c29eb853STejun Heo if (pwq->nr_in_flight[i]) 4613c29eb853STejun Heo return true; 4614c29eb853STejun Heo 4615c29eb853STejun Heo if ((pwq != pwq->wq->dfl_pwq) && (pwq->refcnt > 1)) 4616c29eb853STejun Heo return true; 4617f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) 4618c29eb853STejun Heo return true; 4619c29eb853STejun Heo 4620c29eb853STejun Heo return false; 4621c29eb853STejun Heo } 4622c29eb853STejun Heo 46233af24433SOleg Nesterov /** 46243af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 46253af24433SOleg Nesterov * @wq: target workqueue 46263af24433SOleg Nesterov * 46273af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 46283af24433SOleg Nesterov */ 46293af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 46303af24433SOleg Nesterov { 463149e3cf44STejun Heo struct pool_workqueue *pwq; 46324c16bd32STejun Heo int node; 46333af24433SOleg Nesterov 4634def98c84STejun Heo /* 4635def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 4636def98c84STejun Heo * lead to sysfs name conflicts. 4637def98c84STejun Heo */ 4638def98c84STejun Heo workqueue_sysfs_unregister(wq); 4639def98c84STejun Heo 464033e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 464133e3f0a3SRichard Clark mutex_lock(&wq->mutex); 464233e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 464333e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 464433e3f0a3SRichard Clark 46459c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 46469c5a2ba7STejun Heo drain_workqueue(wq); 4647c8efcc25STejun Heo 4648def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 4649def98c84STejun Heo if (wq->rescuer) { 4650def98c84STejun Heo struct worker *rescuer = wq->rescuer; 4651def98c84STejun Heo 4652def98c84STejun Heo /* this prevents new queueing */ 4653a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 4654def98c84STejun Heo wq->rescuer = NULL; 4655a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 4656def98c84STejun Heo 4657def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 4658def98c84STejun Heo kthread_stop(rescuer->task); 46598efe1223STejun Heo kfree(rescuer); 4660def98c84STejun Heo } 4661def98c84STejun Heo 4662c29eb853STejun Heo /* 4663c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 4664c29eb853STejun Heo * in-flight operations which may do put_pwq(). 4665c29eb853STejun Heo */ 4666c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 4667b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 466849e3cf44STejun Heo for_each_pwq(pwq, wq) { 4669a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4670c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 46711d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 4672e66b39afSTejun Heo __func__, wq->name); 4673c29eb853STejun Heo show_pwq(pwq); 4674a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4675b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 4676c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 467755df0933SImran Khan show_one_workqueue(wq); 46786183c009STejun Heo return; 467976af4d93STejun Heo } 4680a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 468176af4d93STejun Heo } 4682b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 46836183c009STejun Heo 4684a0a1a5fdSTejun Heo /* 4685a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 4686a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 4687a0a1a5fdSTejun Heo */ 4688e2dca7adSTejun Heo list_del_rcu(&wq->list); 468968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 46903af24433SOleg Nesterov 46918864b4e5STejun Heo if (!(wq->flags & WQ_UNBOUND)) { 4692669de8bdSBart Van Assche wq_unregister_lockdep(wq); 469329c91e99STejun Heo /* 46948864b4e5STejun Heo * The base ref is never dropped on per-cpu pwqs. Directly 4695e2dca7adSTejun Heo * schedule RCU free. 469629c91e99STejun Heo */ 469725b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 46988864b4e5STejun Heo } else { 46998864b4e5STejun Heo /* 47008864b4e5STejun Heo * We're the sole accessor of @wq at this point. Directly 47014c16bd32STejun Heo * access numa_pwq_tbl[] and dfl_pwq to put the base refs. 47024c16bd32STejun Heo * @wq will be freed when the last pwq is released. 47038864b4e5STejun Heo */ 47044c16bd32STejun Heo for_each_node(node) { 47054c16bd32STejun Heo pwq = rcu_access_pointer(wq->numa_pwq_tbl[node]); 47064c16bd32STejun Heo RCU_INIT_POINTER(wq->numa_pwq_tbl[node], NULL); 47074c16bd32STejun Heo put_pwq_unlocked(pwq); 47084c16bd32STejun Heo } 47094c16bd32STejun Heo 47104c16bd32STejun Heo /* 47114c16bd32STejun Heo * Put dfl_pwq. @wq may be freed any time after dfl_pwq is 47124c16bd32STejun Heo * put. Don't access it afterwards. 47134c16bd32STejun Heo */ 47144c16bd32STejun Heo pwq = wq->dfl_pwq; 47154c16bd32STejun Heo wq->dfl_pwq = NULL; 4716dce90d47STejun Heo put_pwq_unlocked(pwq); 471729c91e99STejun Heo } 47183af24433SOleg Nesterov } 47193af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 47203af24433SOleg Nesterov 4721dcd989cbSTejun Heo /** 4722dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 4723dcd989cbSTejun Heo * @wq: target workqueue 4724dcd989cbSTejun Heo * @max_active: new max_active value. 4725dcd989cbSTejun Heo * 4726dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 4727dcd989cbSTejun Heo * 4728dcd989cbSTejun Heo * CONTEXT: 4729dcd989cbSTejun Heo * Don't call from IRQ context. 4730dcd989cbSTejun Heo */ 4731dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 4732dcd989cbSTejun Heo { 473349e3cf44STejun Heo struct pool_workqueue *pwq; 4734dcd989cbSTejun Heo 47358719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 47360a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 47378719dceaSTejun Heo return; 47388719dceaSTejun Heo 4739f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 4740dcd989cbSTejun Heo 4741a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 4742dcd989cbSTejun Heo 47430a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 4744dcd989cbSTejun Heo wq->saved_max_active = max_active; 4745dcd989cbSTejun Heo 4746699ce097STejun Heo for_each_pwq(pwq, wq) 4747699ce097STejun Heo pwq_adjust_max_active(pwq); 4748dcd989cbSTejun Heo 4749a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4750dcd989cbSTejun Heo } 4751dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 4752dcd989cbSTejun Heo 4753dcd989cbSTejun Heo /** 475427d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 475527d4ee03SLukas Wunner * 475627d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 475727d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 475827d4ee03SLukas Wunner * 475927d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 476027d4ee03SLukas Wunner */ 476127d4ee03SLukas Wunner struct work_struct *current_work(void) 476227d4ee03SLukas Wunner { 476327d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 476427d4ee03SLukas Wunner 476527d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 476627d4ee03SLukas Wunner } 476727d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 476827d4ee03SLukas Wunner 476927d4ee03SLukas Wunner /** 4770e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 4771e6267616STejun Heo * 4772e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 4773e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 4774d185af30SYacine Belkadi * 4775d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 4776e6267616STejun Heo */ 4777e6267616STejun Heo bool current_is_workqueue_rescuer(void) 4778e6267616STejun Heo { 4779e6267616STejun Heo struct worker *worker = current_wq_worker(); 4780e6267616STejun Heo 47816a092dfdSLai Jiangshan return worker && worker->rescue_wq; 4782e6267616STejun Heo } 4783e6267616STejun Heo 4784e6267616STejun Heo /** 4785dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 4786dcd989cbSTejun Heo * @cpu: CPU in question 4787dcd989cbSTejun Heo * @wq: target workqueue 4788dcd989cbSTejun Heo * 4789dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 4790dcd989cbSTejun Heo * no synchronization around this function and the test result is 4791dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4792dcd989cbSTejun Heo * 4793d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 4794d3251859STejun Heo * Note that both per-cpu and unbound workqueues may be associated with 4795d3251859STejun Heo * multiple pool_workqueues which have separate congested states. A 4796d3251859STejun Heo * workqueue being congested on one CPU doesn't mean the workqueue is also 4797d3251859STejun Heo * contested on other CPUs / NUMA nodes. 4798d3251859STejun Heo * 4799d185af30SYacine Belkadi * Return: 4800dcd989cbSTejun Heo * %true if congested, %false otherwise. 4801dcd989cbSTejun Heo */ 4802d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 4803dcd989cbSTejun Heo { 48047fb98ea7STejun Heo struct pool_workqueue *pwq; 480576af4d93STejun Heo bool ret; 480676af4d93STejun Heo 480724acfb71SThomas Gleixner rcu_read_lock(); 480824acfb71SThomas Gleixner preempt_disable(); 48097fb98ea7STejun Heo 4810d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 4811d3251859STejun Heo cpu = smp_processor_id(); 4812d3251859STejun Heo 48137fb98ea7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 48147fb98ea7STejun Heo pwq = per_cpu_ptr(wq->cpu_pwqs, cpu); 48157fb98ea7STejun Heo else 4816df2d5ae4STejun Heo pwq = unbound_pwq_by_node(wq, cpu_to_node(cpu)); 4817dcd989cbSTejun Heo 4818f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 481924acfb71SThomas Gleixner preempt_enable(); 482024acfb71SThomas Gleixner rcu_read_unlock(); 482176af4d93STejun Heo 482276af4d93STejun Heo return ret; 4823dcd989cbSTejun Heo } 4824dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 4825dcd989cbSTejun Heo 4826dcd989cbSTejun Heo /** 4827dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 4828dcd989cbSTejun Heo * @work: the work to be tested 4829dcd989cbSTejun Heo * 4830dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 4831dcd989cbSTejun Heo * synchronization around this function and the test result is 4832dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 4833dcd989cbSTejun Heo * 4834d185af30SYacine Belkadi * Return: 4835dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 4836dcd989cbSTejun Heo */ 4837dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 4838dcd989cbSTejun Heo { 4839fa1b54e6STejun Heo struct worker_pool *pool; 4840dcd989cbSTejun Heo unsigned long flags; 4841dcd989cbSTejun Heo unsigned int ret = 0; 4842dcd989cbSTejun Heo 4843dcd989cbSTejun Heo if (work_pending(work)) 4844dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 4845038366c5SLai Jiangshan 484624acfb71SThomas Gleixner rcu_read_lock(); 4847fa1b54e6STejun Heo pool = get_work_pool(work); 4848038366c5SLai Jiangshan if (pool) { 4849a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 4850c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 4851dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 4852a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 4853038366c5SLai Jiangshan } 485424acfb71SThomas Gleixner rcu_read_unlock(); 4855dcd989cbSTejun Heo 4856dcd989cbSTejun Heo return ret; 4857dcd989cbSTejun Heo } 4858dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 4859dcd989cbSTejun Heo 48603d1cb205STejun Heo /** 48613d1cb205STejun Heo * set_worker_desc - set description for the current work item 48623d1cb205STejun Heo * @fmt: printf-style format string 48633d1cb205STejun Heo * @...: arguments for the format string 48643d1cb205STejun Heo * 48653d1cb205STejun Heo * This function can be called by a running work function to describe what 48663d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 48673d1cb205STejun Heo * information will be printed out together to help debugging. The 48683d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 48693d1cb205STejun Heo */ 48703d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 48713d1cb205STejun Heo { 48723d1cb205STejun Heo struct worker *worker = current_wq_worker(); 48733d1cb205STejun Heo va_list args; 48743d1cb205STejun Heo 48753d1cb205STejun Heo if (worker) { 48763d1cb205STejun Heo va_start(args, fmt); 48773d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 48783d1cb205STejun Heo va_end(args); 48793d1cb205STejun Heo } 48803d1cb205STejun Heo } 48815c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 48823d1cb205STejun Heo 48833d1cb205STejun Heo /** 48843d1cb205STejun Heo * print_worker_info - print out worker information and description 48853d1cb205STejun Heo * @log_lvl: the log level to use when printing 48863d1cb205STejun Heo * @task: target task 48873d1cb205STejun Heo * 48883d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 48893d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 48903d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 48913d1cb205STejun Heo * 48923d1cb205STejun Heo * This function can be safely called on any task as long as the 48933d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 48943d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 48953d1cb205STejun Heo */ 48963d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 48973d1cb205STejun Heo { 48983d1cb205STejun Heo work_func_t *fn = NULL; 48993d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 49003d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 49013d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 49023d1cb205STejun Heo struct workqueue_struct *wq = NULL; 49033d1cb205STejun Heo struct worker *worker; 49043d1cb205STejun Heo 49053d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 49063d1cb205STejun Heo return; 49073d1cb205STejun Heo 49083d1cb205STejun Heo /* 49093d1cb205STejun Heo * This function is called without any synchronization and @task 49103d1cb205STejun Heo * could be in any state. Be careful with dereferences. 49113d1cb205STejun Heo */ 4912e700591aSPetr Mladek worker = kthread_probe_data(task); 49133d1cb205STejun Heo 49143d1cb205STejun Heo /* 49158bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 49168bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 49173d1cb205STejun Heo */ 4918fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 4919fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 4920fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 4921fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 4922fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 49233d1cb205STejun Heo 49243d1cb205STejun Heo if (fn || name[0] || desc[0]) { 4925d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 49268bf89593STejun Heo if (strcmp(name, desc)) 49273d1cb205STejun Heo pr_cont(" (%s)", desc); 49283d1cb205STejun Heo pr_cont("\n"); 49293d1cb205STejun Heo } 49303d1cb205STejun Heo } 49313d1cb205STejun Heo 49323494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 49333494fc30STejun Heo { 49343494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 49353494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 49363494fc30STejun Heo pr_cont(" node=%d", pool->node); 49373494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 49383494fc30STejun Heo } 49393494fc30STejun Heo 4940c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 4941c76feb0dSPaul E. McKenney bool comma; 4942c76feb0dSPaul E. McKenney work_func_t func; 4943c76feb0dSPaul E. McKenney long ctr; 4944c76feb0dSPaul E. McKenney }; 4945c76feb0dSPaul E. McKenney 4946c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 4947c76feb0dSPaul E. McKenney { 4948c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 4949c76feb0dSPaul E. McKenney goto out_record; 4950c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 4951c76feb0dSPaul E. McKenney pcwsp->ctr++; 4952c76feb0dSPaul E. McKenney return; 4953c76feb0dSPaul E. McKenney } 4954c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 4955c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 4956c76feb0dSPaul E. McKenney else 4957c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 4958c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 4959c76feb0dSPaul E. McKenney out_record: 4960c76feb0dSPaul E. McKenney if ((long)func == -1L) 4961c76feb0dSPaul E. McKenney return; 4962c76feb0dSPaul E. McKenney pcwsp->comma = comma; 4963c76feb0dSPaul E. McKenney pcwsp->func = func; 4964c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 4965c76feb0dSPaul E. McKenney } 4966c76feb0dSPaul E. McKenney 4967c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 49683494fc30STejun Heo { 49693494fc30STejun Heo if (work->func == wq_barrier_func) { 49703494fc30STejun Heo struct wq_barrier *barr; 49713494fc30STejun Heo 49723494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 49733494fc30STejun Heo 4974c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 49753494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 49763494fc30STejun Heo task_pid_nr(barr->task)); 49773494fc30STejun Heo } else { 4978c76feb0dSPaul E. McKenney if (!comma) 4979c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 4980c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 49813494fc30STejun Heo } 49823494fc30STejun Heo } 49833494fc30STejun Heo 49843494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 49853494fc30STejun Heo { 4986c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 49873494fc30STejun Heo struct worker_pool *pool = pwq->pool; 49883494fc30STejun Heo struct work_struct *work; 49893494fc30STejun Heo struct worker *worker; 49903494fc30STejun Heo bool has_in_flight = false, has_pending = false; 49913494fc30STejun Heo int bkt; 49923494fc30STejun Heo 49933494fc30STejun Heo pr_info(" pwq %d:", pool->id); 49943494fc30STejun Heo pr_cont_pool_info(pool); 49953494fc30STejun Heo 4996e66b39afSTejun Heo pr_cont(" active=%d/%d refcnt=%d%s\n", 4997e66b39afSTejun Heo pwq->nr_active, pwq->max_active, pwq->refcnt, 49983494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 49993494fc30STejun Heo 50003494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 50013494fc30STejun Heo if (worker->current_pwq == pwq) { 50023494fc30STejun Heo has_in_flight = true; 50033494fc30STejun Heo break; 50043494fc30STejun Heo } 50053494fc30STejun Heo } 50063494fc30STejun Heo if (has_in_flight) { 50073494fc30STejun Heo bool comma = false; 50083494fc30STejun Heo 50093494fc30STejun Heo pr_info(" in-flight:"); 50103494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 50113494fc30STejun Heo if (worker->current_pwq != pwq) 50123494fc30STejun Heo continue; 50133494fc30STejun Heo 5014d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 50153494fc30STejun Heo task_pid_nr(worker->task), 501630ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 50173494fc30STejun Heo worker->current_func); 50183494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5019c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5020c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 50213494fc30STejun Heo comma = true; 50223494fc30STejun Heo } 50233494fc30STejun Heo pr_cont("\n"); 50243494fc30STejun Heo } 50253494fc30STejun Heo 50263494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 50273494fc30STejun Heo if (get_work_pwq(work) == pwq) { 50283494fc30STejun Heo has_pending = true; 50293494fc30STejun Heo break; 50303494fc30STejun Heo } 50313494fc30STejun Heo } 50323494fc30STejun Heo if (has_pending) { 50333494fc30STejun Heo bool comma = false; 50343494fc30STejun Heo 50353494fc30STejun Heo pr_info(" pending:"); 50363494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 50373494fc30STejun Heo if (get_work_pwq(work) != pwq) 50383494fc30STejun Heo continue; 50393494fc30STejun Heo 5040c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 50413494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 50423494fc30STejun Heo } 5043c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 50443494fc30STejun Heo pr_cont("\n"); 50453494fc30STejun Heo } 50463494fc30STejun Heo 5047f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 50483494fc30STejun Heo bool comma = false; 50493494fc30STejun Heo 5050f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5051f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5052c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 50533494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 50543494fc30STejun Heo } 5055c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 50563494fc30STejun Heo pr_cont("\n"); 50573494fc30STejun Heo } 50583494fc30STejun Heo } 50593494fc30STejun Heo 50603494fc30STejun Heo /** 506155df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 506255df0933SImran Khan * @wq: workqueue whose state will be printed 50633494fc30STejun Heo */ 506455df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 50653494fc30STejun Heo { 50663494fc30STejun Heo struct pool_workqueue *pwq; 50673494fc30STejun Heo bool idle = true; 506855df0933SImran Khan unsigned long flags; 50693494fc30STejun Heo 50703494fc30STejun Heo for_each_pwq(pwq, wq) { 5071f97a4a1aSLai Jiangshan if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 50723494fc30STejun Heo idle = false; 50733494fc30STejun Heo break; 50743494fc30STejun Heo } 50753494fc30STejun Heo } 507655df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 507755df0933SImran Khan return; 50783494fc30STejun Heo 50793494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 50803494fc30STejun Heo 50813494fc30STejun Heo for_each_pwq(pwq, wq) { 5082a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 508357116ce1SJohan Hovold if (pwq->nr_active || !list_empty(&pwq->inactive_works)) { 508457116ce1SJohan Hovold /* 508557116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 508657116ce1SJohan Hovold * drivers that queue work while holding locks 508757116ce1SJohan Hovold * also taken in their write paths. 508857116ce1SJohan Hovold */ 508957116ce1SJohan Hovold printk_deferred_enter(); 50903494fc30STejun Heo show_pwq(pwq); 509157116ce1SJohan Hovold printk_deferred_exit(); 509257116ce1SJohan Hovold } 5093a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 509462635ea8SSergey Senozhatsky /* 509562635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 509655df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 509762635ea8SSergey Senozhatsky * hard lockup. 509862635ea8SSergey Senozhatsky */ 509962635ea8SSergey Senozhatsky touch_nmi_watchdog(); 51003494fc30STejun Heo } 510155df0933SImran Khan 51023494fc30STejun Heo } 51033494fc30STejun Heo 510455df0933SImran Khan /** 510555df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 510655df0933SImran Khan * @pool: worker pool whose state will be printed 510755df0933SImran Khan */ 510855df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 510955df0933SImran Khan { 51103494fc30STejun Heo struct worker *worker; 51113494fc30STejun Heo bool first = true; 511255df0933SImran Khan unsigned long flags; 5113335a42ebSPetr Mladek unsigned long hung = 0; 51143494fc30STejun Heo 5115a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 51163494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 51173494fc30STejun Heo goto next_pool; 5118335a42ebSPetr Mladek 5119335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5120335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5121335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5122335a42ebSPetr Mladek 512357116ce1SJohan Hovold /* 512457116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 512557116ce1SJohan Hovold * queue work while holding locks also taken in their write 512657116ce1SJohan Hovold * paths. 512757116ce1SJohan Hovold */ 512857116ce1SJohan Hovold printk_deferred_enter(); 51293494fc30STejun Heo pr_info("pool %d:", pool->id); 51303494fc30STejun Heo pr_cont_pool_info(pool); 5131335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 51323494fc30STejun Heo if (pool->manager) 51333494fc30STejun Heo pr_cont(" manager: %d", 51343494fc30STejun Heo task_pid_nr(pool->manager->task)); 51353494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 51363494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 51373494fc30STejun Heo task_pid_nr(worker->task)); 51383494fc30STejun Heo first = false; 51393494fc30STejun Heo } 51403494fc30STejun Heo pr_cont("\n"); 514157116ce1SJohan Hovold printk_deferred_exit(); 51423494fc30STejun Heo next_pool: 5143a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 514462635ea8SSergey Senozhatsky /* 514562635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 514655df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 514762635ea8SSergey Senozhatsky * hard lockup. 514862635ea8SSergey Senozhatsky */ 514962635ea8SSergey Senozhatsky touch_nmi_watchdog(); 515055df0933SImran Khan 51513494fc30STejun Heo } 51523494fc30STejun Heo 515355df0933SImran Khan /** 515455df0933SImran Khan * show_all_workqueues - dump workqueue state 515555df0933SImran Khan * 5156704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 515755df0933SImran Khan */ 515855df0933SImran Khan void show_all_workqueues(void) 515955df0933SImran Khan { 516055df0933SImran Khan struct workqueue_struct *wq; 516155df0933SImran Khan struct worker_pool *pool; 516255df0933SImran Khan int pi; 516355df0933SImran Khan 516455df0933SImran Khan rcu_read_lock(); 516555df0933SImran Khan 516655df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 516755df0933SImran Khan 516855df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 516955df0933SImran Khan show_one_workqueue(wq); 517055df0933SImran Khan 517155df0933SImran Khan for_each_pool(pool, pi) 517255df0933SImran Khan show_one_worker_pool(pool); 517355df0933SImran Khan 517424acfb71SThomas Gleixner rcu_read_unlock(); 51753494fc30STejun Heo } 51763494fc30STejun Heo 5177704bc669SJungseung Lee /** 5178704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5179704bc669SJungseung Lee * 5180704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5181704bc669SJungseung Lee * still busy. 5182704bc669SJungseung Lee */ 5183704bc669SJungseung Lee void show_freezable_workqueues(void) 5184704bc669SJungseung Lee { 5185704bc669SJungseung Lee struct workqueue_struct *wq; 5186704bc669SJungseung Lee 5187704bc669SJungseung Lee rcu_read_lock(); 5188704bc669SJungseung Lee 5189704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5190704bc669SJungseung Lee 5191704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5192704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5193704bc669SJungseung Lee continue; 5194704bc669SJungseung Lee show_one_workqueue(wq); 5195704bc669SJungseung Lee } 5196704bc669SJungseung Lee 5197704bc669SJungseung Lee rcu_read_unlock(); 5198704bc669SJungseung Lee } 5199704bc669SJungseung Lee 52006b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 52016b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 52026b59808bSTejun Heo { 52036b59808bSTejun Heo int off; 52046b59808bSTejun Heo 52056b59808bSTejun Heo /* always show the actual comm */ 52066b59808bSTejun Heo off = strscpy(buf, task->comm, size); 52076b59808bSTejun Heo if (off < 0) 52086b59808bSTejun Heo return; 52096b59808bSTejun Heo 5210197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 52116b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 52126b59808bSTejun Heo 5213197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5214197f6accSTejun Heo struct worker *worker = kthread_data(task); 5215197f6accSTejun Heo struct worker_pool *pool = worker->pool; 52166b59808bSTejun Heo 52176b59808bSTejun Heo if (pool) { 5218a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 52196b59808bSTejun Heo /* 5220197f6accSTejun Heo * ->desc tracks information (wq name or 5221197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5222197f6accSTejun Heo * current, prepend '+', otherwise '-'. 52236b59808bSTejun Heo */ 52246b59808bSTejun Heo if (worker->desc[0] != '\0') { 52256b59808bSTejun Heo if (worker->current_work) 52266b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 52276b59808bSTejun Heo worker->desc); 52286b59808bSTejun Heo else 52296b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 52306b59808bSTejun Heo worker->desc); 52316b59808bSTejun Heo } 5232a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 52336b59808bSTejun Heo } 5234197f6accSTejun Heo } 52356b59808bSTejun Heo 52366b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 52376b59808bSTejun Heo } 52386b59808bSTejun Heo 523966448bc2SMathieu Malaterre #ifdef CONFIG_SMP 524066448bc2SMathieu Malaterre 5241db7bccf4STejun Heo /* 5242db7bccf4STejun Heo * CPU hotplug. 5243db7bccf4STejun Heo * 5244e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5245112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5246706026c2STejun Heo * pool which make migrating pending and scheduled works very 5247e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 524894cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5249e22bee78STejun Heo * blocked draining impractical. 5250e22bee78STejun Heo * 525124647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5252628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5253628c78e7STejun Heo * cpu comes back online. 5254db7bccf4STejun Heo */ 5255db7bccf4STejun Heo 5256e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5257db7bccf4STejun Heo { 52584ce62e9eSTejun Heo struct worker_pool *pool; 5259db7bccf4STejun Heo struct worker *worker; 5260db7bccf4STejun Heo 5261f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 52621258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5263a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5264e22bee78STejun Heo 5265f2d5a0eeSTejun Heo /* 526692f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 526794cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 526811b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5269b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5270b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5271b4ac9384SLai Jiangshan * is on the same cpu. 5272f2d5a0eeSTejun Heo */ 5273da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5274403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5275db7bccf4STejun Heo 527624647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5277f2d5a0eeSTejun Heo 5278e22bee78STejun Heo /* 5279989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5280989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5281989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5282989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5283989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5284eb283428SLai Jiangshan * are served by workers tied to the pool. 5285e22bee78STejun Heo */ 5286bc35f7efSLai Jiangshan pool->nr_running = 0; 5287eb283428SLai Jiangshan 5288eb283428SLai Jiangshan /* 5289eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5290eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5291eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5292eb283428SLai Jiangshan */ 5293eb283428SLai Jiangshan wake_up_worker(pool); 5294989442d7SLai Jiangshan 5295a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5296989442d7SLai Jiangshan 5297793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5298793777bcSValentin Schneider unbind_worker(worker); 5299989442d7SLai Jiangshan 5300989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5301eb283428SLai Jiangshan } 5302db7bccf4STejun Heo } 5303db7bccf4STejun Heo 5304bd7c089eSTejun Heo /** 5305bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5306bd7c089eSTejun Heo * @pool: pool of interest 5307bd7c089eSTejun Heo * 5308a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5309bd7c089eSTejun Heo */ 5310bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5311bd7c089eSTejun Heo { 5312a9ab775bSTejun Heo struct worker *worker; 5313bd7c089eSTejun Heo 53141258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5315bd7c089eSTejun Heo 5316bd7c089eSTejun Heo /* 5317a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5318a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5319402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5320a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5321a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5322bd7c089eSTejun Heo */ 5323c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5324c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5325c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 5326c63a2e52SValentin Schneider pool->attrs->cpumask) < 0); 5327c63a2e52SValentin Schneider } 5328a9ab775bSTejun Heo 5329a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5330f7c17d26SWanpeng Li 53313de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5332a9ab775bSTejun Heo 5333da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5334a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5335a9ab775bSTejun Heo 5336a9ab775bSTejun Heo /* 5337a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5338a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5339a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5340a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5341a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5342a9ab775bSTejun Heo * concurrency management. Note that when or whether 5343a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5344a9ab775bSTejun Heo * 5345c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5346a9ab775bSTejun Heo * tested without holding any lock in 53476d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5348a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5349a9ab775bSTejun Heo * management operations. 5350bd7c089eSTejun Heo */ 5351a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5352a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5353a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5354c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5355bd7c089eSTejun Heo } 5356a9ab775bSTejun Heo 5357a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5358bd7c089eSTejun Heo } 5359bd7c089eSTejun Heo 53607dbc725eSTejun Heo /** 53617dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 53627dbc725eSTejun Heo * @pool: unbound pool of interest 53637dbc725eSTejun Heo * @cpu: the CPU which is coming up 53647dbc725eSTejun Heo * 53657dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 53667dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 53677dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 53687dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 53697dbc725eSTejun Heo */ 53707dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 53717dbc725eSTejun Heo { 53727dbc725eSTejun Heo static cpumask_t cpumask; 53737dbc725eSTejun Heo struct worker *worker; 53747dbc725eSTejun Heo 53751258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 53767dbc725eSTejun Heo 53777dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 53787dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 53797dbc725eSTejun Heo return; 53807dbc725eSTejun Heo 53817dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 53827dbc725eSTejun Heo 53837dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5384da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5385d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 53867dbc725eSTejun Heo } 53877dbc725eSTejun Heo 53887ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 53891da177e4SLinus Torvalds { 53904ce62e9eSTejun Heo struct worker_pool *pool; 53911da177e4SLinus Torvalds 5392f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 53933ce63377STejun Heo if (pool->nr_workers) 53943ce63377STejun Heo continue; 5395051e1850SLai Jiangshan if (!create_worker(pool)) 53967ee681b2SThomas Gleixner return -ENOMEM; 53973af24433SOleg Nesterov } 53987ee681b2SThomas Gleixner return 0; 53997ee681b2SThomas Gleixner } 54001da177e4SLinus Torvalds 54017ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 54027ee681b2SThomas Gleixner { 54037ee681b2SThomas Gleixner struct worker_pool *pool; 54047ee681b2SThomas Gleixner struct workqueue_struct *wq; 54057ee681b2SThomas Gleixner int pi; 54067ee681b2SThomas Gleixner 540768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 54087dbc725eSTejun Heo 54097dbc725eSTejun Heo for_each_pool(pool, pi) { 54101258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 541194cf58bbSTejun Heo 5412f05b558dSLai Jiangshan if (pool->cpu == cpu) 541394cf58bbSTejun Heo rebind_workers(pool); 5414f05b558dSLai Jiangshan else if (pool->cpu < 0) 54157dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 541694cf58bbSTejun Heo 54171258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 541894cf58bbSTejun Heo } 54197dbc725eSTejun Heo 54204c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 54214c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 54224c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, true); 54234c16bd32STejun Heo 542468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 54257ee681b2SThomas Gleixner return 0; 542665758202STejun Heo } 542765758202STejun Heo 54287ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 542965758202STejun Heo { 54304c16bd32STejun Heo struct workqueue_struct *wq; 54318db25e78STejun Heo 54324c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5433e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5434e8b3f8dbSLai Jiangshan return -1; 5435e8b3f8dbSLai Jiangshan 5436e8b3f8dbSLai Jiangshan unbind_workers(cpu); 54374c16bd32STejun Heo 54384c16bd32STejun Heo /* update NUMA affinity of unbound workqueues */ 54394c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 54404c16bd32STejun Heo list_for_each_entry(wq, &workqueues, list) 54414c16bd32STejun Heo wq_update_unbound_numa(wq, cpu, false); 54424c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 54434c16bd32STejun Heo 54447ee681b2SThomas Gleixner return 0; 544565758202STejun Heo } 544665758202STejun Heo 54472d3854a3SRusty Russell struct work_for_cpu { 5448ed48ece2STejun Heo struct work_struct work; 54492d3854a3SRusty Russell long (*fn)(void *); 54502d3854a3SRusty Russell void *arg; 54512d3854a3SRusty Russell long ret; 54522d3854a3SRusty Russell }; 54532d3854a3SRusty Russell 5454ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 54552d3854a3SRusty Russell { 5456ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5457ed48ece2STejun Heo 54582d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 54592d3854a3SRusty Russell } 54602d3854a3SRusty Russell 54612d3854a3SRusty Russell /** 546222aceb31SAnna-Maria Gleixner * work_on_cpu - run a function in thread context on a particular cpu 54632d3854a3SRusty Russell * @cpu: the cpu to run on 54642d3854a3SRusty Russell * @fn: the function to run 54652d3854a3SRusty Russell * @arg: the function arg 54662d3854a3SRusty Russell * 546731ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 54686b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5469d185af30SYacine Belkadi * 5470d185af30SYacine Belkadi * Return: The value @fn returns. 54712d3854a3SRusty Russell */ 5472d84ff051STejun Heo long work_on_cpu(int cpu, long (*fn)(void *), void *arg) 54732d3854a3SRusty Russell { 5474ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 54752d3854a3SRusty Russell 5476ed48ece2STejun Heo INIT_WORK_ONSTACK(&wfc.work, work_for_cpu_fn); 5477ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 547812997d1aSBjorn Helgaas flush_work(&wfc.work); 5479440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 54802d3854a3SRusty Russell return wfc.ret; 54812d3854a3SRusty Russell } 54822d3854a3SRusty Russell EXPORT_SYMBOL_GPL(work_on_cpu); 54830e8d6a93SThomas Gleixner 54840e8d6a93SThomas Gleixner /** 54850e8d6a93SThomas Gleixner * work_on_cpu_safe - run a function in thread context on a particular cpu 54860e8d6a93SThomas Gleixner * @cpu: the cpu to run on 54870e8d6a93SThomas Gleixner * @fn: the function to run 54880e8d6a93SThomas Gleixner * @arg: the function argument 54890e8d6a93SThomas Gleixner * 54900e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 54910e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 54920e8d6a93SThomas Gleixner * 54930e8d6a93SThomas Gleixner * Return: The value @fn returns. 54940e8d6a93SThomas Gleixner */ 54950e8d6a93SThomas Gleixner long work_on_cpu_safe(int cpu, long (*fn)(void *), void *arg) 54960e8d6a93SThomas Gleixner { 54970e8d6a93SThomas Gleixner long ret = -ENODEV; 54980e8d6a93SThomas Gleixner 5499ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 55000e8d6a93SThomas Gleixner if (cpu_online(cpu)) 55010e8d6a93SThomas Gleixner ret = work_on_cpu(cpu, fn, arg); 5502ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 55030e8d6a93SThomas Gleixner return ret; 55040e8d6a93SThomas Gleixner } 55050e8d6a93SThomas Gleixner EXPORT_SYMBOL_GPL(work_on_cpu_safe); 55062d3854a3SRusty Russell #endif /* CONFIG_SMP */ 55072d3854a3SRusty Russell 5508a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5509e7577c50SRusty Russell 5510a0a1a5fdSTejun Heo /** 5511a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5512a0a1a5fdSTejun Heo * 551358a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5514f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5515706026c2STejun Heo * pool->worklist. 5516a0a1a5fdSTejun Heo * 5517a0a1a5fdSTejun Heo * CONTEXT: 5518a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5519a0a1a5fdSTejun Heo */ 5520a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5521a0a1a5fdSTejun Heo { 552224b8a847STejun Heo struct workqueue_struct *wq; 552324b8a847STejun Heo struct pool_workqueue *pwq; 5524a0a1a5fdSTejun Heo 552568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5526a0a1a5fdSTejun Heo 55276183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5528a0a1a5fdSTejun Heo workqueue_freezing = true; 5529a0a1a5fdSTejun Heo 553024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5531a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5532699ce097STejun Heo for_each_pwq(pwq, wq) 5533699ce097STejun Heo pwq_adjust_max_active(pwq); 5534a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5535a1056305STejun Heo } 55365bcab335STejun Heo 553768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5538a0a1a5fdSTejun Heo } 5539a0a1a5fdSTejun Heo 5540a0a1a5fdSTejun Heo /** 554158a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5542a0a1a5fdSTejun Heo * 5543a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5544a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5545a0a1a5fdSTejun Heo * 5546a0a1a5fdSTejun Heo * CONTEXT: 554768e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5548a0a1a5fdSTejun Heo * 5549d185af30SYacine Belkadi * Return: 555058a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 555158a69cb4STejun Heo * is complete. 5552a0a1a5fdSTejun Heo */ 5553a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5554a0a1a5fdSTejun Heo { 5555a0a1a5fdSTejun Heo bool busy = false; 555624b8a847STejun Heo struct workqueue_struct *wq; 555724b8a847STejun Heo struct pool_workqueue *pwq; 5558a0a1a5fdSTejun Heo 555968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5560a0a1a5fdSTejun Heo 55616183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5562a0a1a5fdSTejun Heo 556324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 556424b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 556524b8a847STejun Heo continue; 5566a0a1a5fdSTejun Heo /* 5567a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5568a0a1a5fdSTejun Heo * to peek without lock. 5569a0a1a5fdSTejun Heo */ 557024acfb71SThomas Gleixner rcu_read_lock(); 557124b8a847STejun Heo for_each_pwq(pwq, wq) { 55726183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5573112202d9STejun Heo if (pwq->nr_active) { 5574a0a1a5fdSTejun Heo busy = true; 557524acfb71SThomas Gleixner rcu_read_unlock(); 5576a0a1a5fdSTejun Heo goto out_unlock; 5577a0a1a5fdSTejun Heo } 5578a0a1a5fdSTejun Heo } 557924acfb71SThomas Gleixner rcu_read_unlock(); 5580a0a1a5fdSTejun Heo } 5581a0a1a5fdSTejun Heo out_unlock: 558268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5583a0a1a5fdSTejun Heo return busy; 5584a0a1a5fdSTejun Heo } 5585a0a1a5fdSTejun Heo 5586a0a1a5fdSTejun Heo /** 5587a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5588a0a1a5fdSTejun Heo * 5589a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5590706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5591a0a1a5fdSTejun Heo * 5592a0a1a5fdSTejun Heo * CONTEXT: 5593a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5594a0a1a5fdSTejun Heo */ 5595a0a1a5fdSTejun Heo void thaw_workqueues(void) 5596a0a1a5fdSTejun Heo { 559724b8a847STejun Heo struct workqueue_struct *wq; 559824b8a847STejun Heo struct pool_workqueue *pwq; 5599a0a1a5fdSTejun Heo 560068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5601a0a1a5fdSTejun Heo 5602a0a1a5fdSTejun Heo if (!workqueue_freezing) 5603a0a1a5fdSTejun Heo goto out_unlock; 5604a0a1a5fdSTejun Heo 560574b414eaSLai Jiangshan workqueue_freezing = false; 560624b8a847STejun Heo 560724b8a847STejun Heo /* restore max_active and repopulate worklist */ 560824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5609a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5610699ce097STejun Heo for_each_pwq(pwq, wq) 5611699ce097STejun Heo pwq_adjust_max_active(pwq); 5612a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 561324b8a847STejun Heo } 561424b8a847STejun Heo 5615a0a1a5fdSTejun Heo out_unlock: 561668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5617a0a1a5fdSTejun Heo } 5618a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 5619a0a1a5fdSTejun Heo 562099c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 5621042f7df1SLai Jiangshan { 5622042f7df1SLai Jiangshan LIST_HEAD(ctxs); 5623042f7df1SLai Jiangshan int ret = 0; 5624042f7df1SLai Jiangshan struct workqueue_struct *wq; 5625042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 5626042f7df1SLai Jiangshan 5627042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5628042f7df1SLai Jiangshan 5629042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 5630042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 5631042f7df1SLai Jiangshan continue; 5632042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 5633042f7df1SLai Jiangshan if (wq->flags & __WQ_ORDERED) 5634042f7df1SLai Jiangshan continue; 5635042f7df1SLai Jiangshan 563699c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 5637042f7df1SLai Jiangshan if (!ctx) { 5638042f7df1SLai Jiangshan ret = -ENOMEM; 5639042f7df1SLai Jiangshan break; 5640042f7df1SLai Jiangshan } 5641042f7df1SLai Jiangshan 5642042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 5643042f7df1SLai Jiangshan } 5644042f7df1SLai Jiangshan 5645042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 5646042f7df1SLai Jiangshan if (!ret) 5647042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 5648042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 5649042f7df1SLai Jiangshan } 5650042f7df1SLai Jiangshan 565199c621efSLai Jiangshan if (!ret) { 565299c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 565399c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 565499c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 565599c621efSLai Jiangshan } 5656042f7df1SLai Jiangshan return ret; 5657042f7df1SLai Jiangshan } 5658042f7df1SLai Jiangshan 5659042f7df1SLai Jiangshan /** 5660042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 5661042f7df1SLai Jiangshan * @cpumask: the cpumask to set 5662042f7df1SLai Jiangshan * 5663042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 5664042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 5665042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 5666042f7df1SLai Jiangshan * 566767dc8325SCai Huoqing * Return: 0 - Success 5668042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 5669042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 5670042f7df1SLai Jiangshan */ 5671042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 5672042f7df1SLai Jiangshan { 5673042f7df1SLai Jiangshan int ret = -EINVAL; 5674042f7df1SLai Jiangshan 5675c98a9805STal Shorer /* 5676c98a9805STal Shorer * Not excluding isolated cpus on purpose. 5677c98a9805STal Shorer * If the user wishes to include them, we allow that. 5678c98a9805STal Shorer */ 5679042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 5680042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 5681a0111cf6SLai Jiangshan apply_wqattrs_lock(); 5682d25302e4SMenglong Dong if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 5683d25302e4SMenglong Dong ret = 0; 5684d25302e4SMenglong Dong goto out_unlock; 5685d25302e4SMenglong Dong } 5686d25302e4SMenglong Dong 568799c621efSLai Jiangshan ret = workqueue_apply_unbound_cpumask(cpumask); 5688042f7df1SLai Jiangshan 5689d25302e4SMenglong Dong out_unlock: 5690a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 5691042f7df1SLai Jiangshan } 5692042f7df1SLai Jiangshan 5693042f7df1SLai Jiangshan return ret; 5694042f7df1SLai Jiangshan } 5695042f7df1SLai Jiangshan 56966ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 56976ba94429SFrederic Weisbecker /* 56986ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 56996ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 57006ba94429SFrederic Weisbecker * following attributes. 57016ba94429SFrederic Weisbecker * 57026ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 57036ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 57046ba94429SFrederic Weisbecker * 57056ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 57066ba94429SFrederic Weisbecker * 57079a19b463SWang Long * pool_ids RO int : the associated pool IDs for each node 57086ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 57096ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 57109a19b463SWang Long * numa RW bool : whether enable NUMA affinity 57116ba94429SFrederic Weisbecker */ 57126ba94429SFrederic Weisbecker struct wq_device { 57136ba94429SFrederic Weisbecker struct workqueue_struct *wq; 57146ba94429SFrederic Weisbecker struct device dev; 57156ba94429SFrederic Weisbecker }; 57166ba94429SFrederic Weisbecker 57176ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 57186ba94429SFrederic Weisbecker { 57196ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 57206ba94429SFrederic Weisbecker 57216ba94429SFrederic Weisbecker return wq_dev->wq; 57226ba94429SFrederic Weisbecker } 57236ba94429SFrederic Weisbecker 57246ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 57256ba94429SFrederic Weisbecker char *buf) 57266ba94429SFrederic Weisbecker { 57276ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57286ba94429SFrederic Weisbecker 57296ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 57306ba94429SFrederic Weisbecker } 57316ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 57326ba94429SFrederic Weisbecker 57336ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 57346ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 57356ba94429SFrederic Weisbecker { 57366ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57376ba94429SFrederic Weisbecker 57386ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 57396ba94429SFrederic Weisbecker } 57406ba94429SFrederic Weisbecker 57416ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 57426ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 57436ba94429SFrederic Weisbecker size_t count) 57446ba94429SFrederic Weisbecker { 57456ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57466ba94429SFrederic Weisbecker int val; 57476ba94429SFrederic Weisbecker 57486ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 57496ba94429SFrederic Weisbecker return -EINVAL; 57506ba94429SFrederic Weisbecker 57516ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 57526ba94429SFrederic Weisbecker return count; 57536ba94429SFrederic Weisbecker } 57546ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 57556ba94429SFrederic Weisbecker 57566ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 57576ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 57586ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 57596ba94429SFrederic Weisbecker NULL, 57606ba94429SFrederic Weisbecker }; 57616ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 57626ba94429SFrederic Weisbecker 57636ba94429SFrederic Weisbecker static ssize_t wq_pool_ids_show(struct device *dev, 57646ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 57656ba94429SFrederic Weisbecker { 57666ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57676ba94429SFrederic Weisbecker const char *delim = ""; 57686ba94429SFrederic Weisbecker int node, written = 0; 57696ba94429SFrederic Weisbecker 5770ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 577124acfb71SThomas Gleixner rcu_read_lock(); 57726ba94429SFrederic Weisbecker for_each_node(node) { 57736ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, 57746ba94429SFrederic Weisbecker "%s%d:%d", delim, node, 57756ba94429SFrederic Weisbecker unbound_pwq_by_node(wq, node)->pool->id); 57766ba94429SFrederic Weisbecker delim = " "; 57776ba94429SFrederic Weisbecker } 57786ba94429SFrederic Weisbecker written += scnprintf(buf + written, PAGE_SIZE - written, "\n"); 577924acfb71SThomas Gleixner rcu_read_unlock(); 5780ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 57816ba94429SFrederic Weisbecker 57826ba94429SFrederic Weisbecker return written; 57836ba94429SFrederic Weisbecker } 57846ba94429SFrederic Weisbecker 57856ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 57866ba94429SFrederic Weisbecker char *buf) 57876ba94429SFrederic Weisbecker { 57886ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 57896ba94429SFrederic Weisbecker int written; 57906ba94429SFrederic Weisbecker 57916ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 57926ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 57936ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 57946ba94429SFrederic Weisbecker 57956ba94429SFrederic Weisbecker return written; 57966ba94429SFrederic Weisbecker } 57976ba94429SFrederic Weisbecker 57986ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 57996ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 58006ba94429SFrederic Weisbecker { 58016ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 58026ba94429SFrederic Weisbecker 5803899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 5804899a94feSLai Jiangshan 5805be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 58066ba94429SFrederic Weisbecker if (!attrs) 58076ba94429SFrederic Weisbecker return NULL; 58086ba94429SFrederic Weisbecker 58096ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 58106ba94429SFrederic Weisbecker return attrs; 58116ba94429SFrederic Weisbecker } 58126ba94429SFrederic Weisbecker 58136ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 58146ba94429SFrederic Weisbecker const char *buf, size_t count) 58156ba94429SFrederic Weisbecker { 58166ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58176ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5818d4d3e257SLai Jiangshan int ret = -ENOMEM; 5819d4d3e257SLai Jiangshan 5820d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58216ba94429SFrederic Weisbecker 58226ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58236ba94429SFrederic Weisbecker if (!attrs) 5824d4d3e257SLai Jiangshan goto out_unlock; 58256ba94429SFrederic Weisbecker 58266ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 58276ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 5828d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 58296ba94429SFrederic Weisbecker else 58306ba94429SFrederic Weisbecker ret = -EINVAL; 58316ba94429SFrederic Weisbecker 5832d4d3e257SLai Jiangshan out_unlock: 5833d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 58346ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 58356ba94429SFrederic Weisbecker return ret ?: count; 58366ba94429SFrederic Weisbecker } 58376ba94429SFrederic Weisbecker 58386ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 58396ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 58406ba94429SFrederic Weisbecker { 58416ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58426ba94429SFrederic Weisbecker int written; 58436ba94429SFrederic Weisbecker 58446ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 58456ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 58466ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 58476ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 58486ba94429SFrederic Weisbecker return written; 58496ba94429SFrederic Weisbecker } 58506ba94429SFrederic Weisbecker 58516ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 58526ba94429SFrederic Weisbecker struct device_attribute *attr, 58536ba94429SFrederic Weisbecker const char *buf, size_t count) 58546ba94429SFrederic Weisbecker { 58556ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58566ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5857d4d3e257SLai Jiangshan int ret = -ENOMEM; 5858d4d3e257SLai Jiangshan 5859d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58606ba94429SFrederic Weisbecker 58616ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58626ba94429SFrederic Weisbecker if (!attrs) 5863d4d3e257SLai Jiangshan goto out_unlock; 58646ba94429SFrederic Weisbecker 58656ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 58666ba94429SFrederic Weisbecker if (!ret) 5867d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 58686ba94429SFrederic Weisbecker 5869d4d3e257SLai Jiangshan out_unlock: 5870d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 58716ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 58726ba94429SFrederic Weisbecker return ret ?: count; 58736ba94429SFrederic Weisbecker } 58746ba94429SFrederic Weisbecker 58756ba94429SFrederic Weisbecker static ssize_t wq_numa_show(struct device *dev, struct device_attribute *attr, 58766ba94429SFrederic Weisbecker char *buf) 58776ba94429SFrederic Weisbecker { 58786ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58796ba94429SFrederic Weisbecker int written; 58806ba94429SFrederic Weisbecker 58816ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 58826ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", 58836ba94429SFrederic Weisbecker !wq->unbound_attrs->no_numa); 58846ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 58856ba94429SFrederic Weisbecker 58866ba94429SFrederic Weisbecker return written; 58876ba94429SFrederic Weisbecker } 58886ba94429SFrederic Weisbecker 58896ba94429SFrederic Weisbecker static ssize_t wq_numa_store(struct device *dev, struct device_attribute *attr, 58906ba94429SFrederic Weisbecker const char *buf, size_t count) 58916ba94429SFrederic Weisbecker { 58926ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 58936ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 5894d4d3e257SLai Jiangshan int v, ret = -ENOMEM; 5895d4d3e257SLai Jiangshan 5896d4d3e257SLai Jiangshan apply_wqattrs_lock(); 58976ba94429SFrederic Weisbecker 58986ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 58996ba94429SFrederic Weisbecker if (!attrs) 5900d4d3e257SLai Jiangshan goto out_unlock; 59016ba94429SFrederic Weisbecker 59026ba94429SFrederic Weisbecker ret = -EINVAL; 59036ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &v) == 1) { 59046ba94429SFrederic Weisbecker attrs->no_numa = !v; 5905d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 59066ba94429SFrederic Weisbecker } 59076ba94429SFrederic Weisbecker 5908d4d3e257SLai Jiangshan out_unlock: 5909d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 59106ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 59116ba94429SFrederic Weisbecker return ret ?: count; 59126ba94429SFrederic Weisbecker } 59136ba94429SFrederic Weisbecker 59146ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 59156ba94429SFrederic Weisbecker __ATTR(pool_ids, 0444, wq_pool_ids_show, NULL), 59166ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 59176ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 59186ba94429SFrederic Weisbecker __ATTR(numa, 0644, wq_numa_show, wq_numa_store), 59196ba94429SFrederic Weisbecker __ATTR_NULL, 59206ba94429SFrederic Weisbecker }; 59216ba94429SFrederic Weisbecker 59226ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 59236ba94429SFrederic Weisbecker .name = "workqueue", 59246ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 59256ba94429SFrederic Weisbecker }; 59266ba94429SFrederic Weisbecker 5927b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 5928b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 5929b05a7928SFrederic Weisbecker { 5930b05a7928SFrederic Weisbecker int written; 5931b05a7928SFrederic Weisbecker 5932042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 5933b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 5934b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 5935042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5936b05a7928SFrederic Weisbecker 5937b05a7928SFrederic Weisbecker return written; 5938b05a7928SFrederic Weisbecker } 5939b05a7928SFrederic Weisbecker 5940042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 5941042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 5942042f7df1SLai Jiangshan { 5943042f7df1SLai Jiangshan cpumask_var_t cpumask; 5944042f7df1SLai Jiangshan int ret; 5945042f7df1SLai Jiangshan 5946042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 5947042f7df1SLai Jiangshan return -ENOMEM; 5948042f7df1SLai Jiangshan 5949042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 5950042f7df1SLai Jiangshan if (!ret) 5951042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 5952042f7df1SLai Jiangshan 5953042f7df1SLai Jiangshan free_cpumask_var(cpumask); 5954042f7df1SLai Jiangshan return ret ? ret : count; 5955042f7df1SLai Jiangshan } 5956042f7df1SLai Jiangshan 5957b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 5958042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 5959042f7df1SLai Jiangshan wq_unbound_cpumask_store); 5960b05a7928SFrederic Weisbecker 59616ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 59626ba94429SFrederic Weisbecker { 5963686f6697SGreg Kroah-Hartman struct device *dev_root; 5964b05a7928SFrederic Weisbecker int err; 5965b05a7928SFrederic Weisbecker 5966b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 5967b05a7928SFrederic Weisbecker if (err) 5968b05a7928SFrederic Weisbecker return err; 5969b05a7928SFrederic Weisbecker 5970686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 5971686f6697SGreg Kroah-Hartman if (dev_root) { 5972686f6697SGreg Kroah-Hartman err = device_create_file(dev_root, &wq_sysfs_cpumask_attr); 5973686f6697SGreg Kroah-Hartman put_device(dev_root); 5974686f6697SGreg Kroah-Hartman } 5975686f6697SGreg Kroah-Hartman return err; 59766ba94429SFrederic Weisbecker } 59776ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 59786ba94429SFrederic Weisbecker 59796ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 59806ba94429SFrederic Weisbecker { 59816ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 59826ba94429SFrederic Weisbecker 59836ba94429SFrederic Weisbecker kfree(wq_dev); 59846ba94429SFrederic Weisbecker } 59856ba94429SFrederic Weisbecker 59866ba94429SFrederic Weisbecker /** 59876ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 59886ba94429SFrederic Weisbecker * @wq: the workqueue to register 59896ba94429SFrederic Weisbecker * 59906ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 59916ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 59926ba94429SFrederic Weisbecker * which is the preferred method. 59936ba94429SFrederic Weisbecker * 59946ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 59956ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 59966ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 59976ba94429SFrederic Weisbecker * attributes. 59986ba94429SFrederic Weisbecker * 59996ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 60006ba94429SFrederic Weisbecker */ 60016ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 60026ba94429SFrederic Weisbecker { 60036ba94429SFrederic Weisbecker struct wq_device *wq_dev; 60046ba94429SFrederic Weisbecker int ret; 60056ba94429SFrederic Weisbecker 60066ba94429SFrederic Weisbecker /* 6007402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 60086ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 60096ba94429SFrederic Weisbecker * workqueues. 60106ba94429SFrederic Weisbecker */ 60110a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 60126ba94429SFrederic Weisbecker return -EINVAL; 60136ba94429SFrederic Weisbecker 60146ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 60156ba94429SFrederic Weisbecker if (!wq_dev) 60166ba94429SFrederic Weisbecker return -ENOMEM; 60176ba94429SFrederic Weisbecker 60186ba94429SFrederic Weisbecker wq_dev->wq = wq; 60196ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 60206ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 602123217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 60226ba94429SFrederic Weisbecker 60236ba94429SFrederic Weisbecker /* 60246ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 60256ba94429SFrederic Weisbecker * everything is ready. 60266ba94429SFrederic Weisbecker */ 60276ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 60286ba94429SFrederic Weisbecker 60296ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 60306ba94429SFrederic Weisbecker if (ret) { 6031537f4146SArvind Yadav put_device(&wq_dev->dev); 60326ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60336ba94429SFrederic Weisbecker return ret; 60346ba94429SFrederic Weisbecker } 60356ba94429SFrederic Weisbecker 60366ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 60376ba94429SFrederic Weisbecker struct device_attribute *attr; 60386ba94429SFrederic Weisbecker 60396ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 60406ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 60416ba94429SFrederic Weisbecker if (ret) { 60426ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 60436ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60446ba94429SFrederic Weisbecker return ret; 60456ba94429SFrederic Weisbecker } 60466ba94429SFrederic Weisbecker } 60476ba94429SFrederic Weisbecker } 60486ba94429SFrederic Weisbecker 60496ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 60506ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 60516ba94429SFrederic Weisbecker return 0; 60526ba94429SFrederic Weisbecker } 60536ba94429SFrederic Weisbecker 60546ba94429SFrederic Weisbecker /** 60556ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 60566ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 60576ba94429SFrederic Weisbecker * 60586ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 60596ba94429SFrederic Weisbecker */ 60606ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 60616ba94429SFrederic Weisbecker { 60626ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 60636ba94429SFrederic Weisbecker 60646ba94429SFrederic Weisbecker if (!wq->wq_dev) 60656ba94429SFrederic Weisbecker return; 60666ba94429SFrederic Weisbecker 60676ba94429SFrederic Weisbecker wq->wq_dev = NULL; 60686ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 60696ba94429SFrederic Weisbecker } 60706ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 60716ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 60726ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 60736ba94429SFrederic Weisbecker 607482607adcSTejun Heo /* 607582607adcSTejun Heo * Workqueue watchdog. 607682607adcSTejun Heo * 607782607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 607882607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 607982607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 608082607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 608182607adcSTejun Heo * largely opaque. 608282607adcSTejun Heo * 608382607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 608482607adcSTejun Heo * state if some pools failed to make forward progress for a while where 608582607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 608682607adcSTejun Heo * 608782607adcSTejun Heo * This mechanism is controlled through the kernel parameter 608882607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 608982607adcSTejun Heo * corresponding sysfs parameter file. 609082607adcSTejun Heo */ 609182607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 609282607adcSTejun Heo 609382607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 60945cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 609582607adcSTejun Heo 609682607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 609782607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 609882607adcSTejun Heo 6099cd2440d6SPetr Mladek /* 6100cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6101cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6102cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6103cd2440d6SPetr Mladek * in all other situations. 6104cd2440d6SPetr Mladek */ 6105cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6106cd2440d6SPetr Mladek { 6107cd2440d6SPetr Mladek struct worker *worker; 6108cd2440d6SPetr Mladek unsigned long flags; 6109cd2440d6SPetr Mladek int bkt; 6110cd2440d6SPetr Mladek 6111cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6112cd2440d6SPetr Mladek 6113cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6114cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6115cd2440d6SPetr Mladek /* 6116cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6117cd2440d6SPetr Mladek * drivers that queue work while holding locks 6118cd2440d6SPetr Mladek * also taken in their write paths. 6119cd2440d6SPetr Mladek */ 6120cd2440d6SPetr Mladek printk_deferred_enter(); 6121cd2440d6SPetr Mladek 6122cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6123cd2440d6SPetr Mladek sched_show_task(worker->task); 6124cd2440d6SPetr Mladek 6125cd2440d6SPetr Mladek printk_deferred_exit(); 6126cd2440d6SPetr Mladek } 6127cd2440d6SPetr Mladek } 6128cd2440d6SPetr Mladek 6129cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6130cd2440d6SPetr Mladek } 6131cd2440d6SPetr Mladek 6132cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6133cd2440d6SPetr Mladek { 6134cd2440d6SPetr Mladek struct worker_pool *pool; 6135cd2440d6SPetr Mladek int pi; 6136cd2440d6SPetr Mladek 6137cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6138cd2440d6SPetr Mladek 6139cd2440d6SPetr Mladek rcu_read_lock(); 6140cd2440d6SPetr Mladek 6141cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6142cd2440d6SPetr Mladek if (pool->cpu_stall) 6143cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6144cd2440d6SPetr Mladek 6145cd2440d6SPetr Mladek } 6146cd2440d6SPetr Mladek 6147cd2440d6SPetr Mladek rcu_read_unlock(); 6148cd2440d6SPetr Mladek } 6149cd2440d6SPetr Mladek 615082607adcSTejun Heo static void wq_watchdog_reset_touched(void) 615182607adcSTejun Heo { 615282607adcSTejun Heo int cpu; 615382607adcSTejun Heo 615482607adcSTejun Heo wq_watchdog_touched = jiffies; 615582607adcSTejun Heo for_each_possible_cpu(cpu) 615682607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 615782607adcSTejun Heo } 615882607adcSTejun Heo 61595cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 616082607adcSTejun Heo { 616182607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 616282607adcSTejun Heo bool lockup_detected = false; 6163cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6164940d71c6SSergey Senozhatsky unsigned long now = jiffies; 616582607adcSTejun Heo struct worker_pool *pool; 616682607adcSTejun Heo int pi; 616782607adcSTejun Heo 616882607adcSTejun Heo if (!thresh) 616982607adcSTejun Heo return; 617082607adcSTejun Heo 617182607adcSTejun Heo rcu_read_lock(); 617282607adcSTejun Heo 617382607adcSTejun Heo for_each_pool(pool, pi) { 617482607adcSTejun Heo unsigned long pool_ts, touched, ts; 617582607adcSTejun Heo 6176cd2440d6SPetr Mladek pool->cpu_stall = false; 617782607adcSTejun Heo if (list_empty(&pool->worklist)) 617882607adcSTejun Heo continue; 617982607adcSTejun Heo 6180940d71c6SSergey Senozhatsky /* 6181940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6182940d71c6SSergey Senozhatsky * the watchdog like a stall. 6183940d71c6SSergey Senozhatsky */ 6184940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6185940d71c6SSergey Senozhatsky 618682607adcSTejun Heo /* get the latest of pool and touched timestamps */ 618789e28ce6SWang Qing if (pool->cpu >= 0) 618889e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 618989e28ce6SWang Qing else 619082607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 619189e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 619282607adcSTejun Heo 619382607adcSTejun Heo if (time_after(pool_ts, touched)) 619482607adcSTejun Heo ts = pool_ts; 619582607adcSTejun Heo else 619682607adcSTejun Heo ts = touched; 619782607adcSTejun Heo 619882607adcSTejun Heo /* did we stall? */ 6199940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 620082607adcSTejun Heo lockup_detected = true; 6201cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6202cd2440d6SPetr Mladek pool->cpu_stall = true; 6203cd2440d6SPetr Mladek cpu_pool_stall = true; 6204cd2440d6SPetr Mladek } 620582607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 620682607adcSTejun Heo pr_cont_pool_info(pool); 620782607adcSTejun Heo pr_cont(" stuck for %us!\n", 6208940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 620982607adcSTejun Heo } 6210cd2440d6SPetr Mladek 6211cd2440d6SPetr Mladek 621282607adcSTejun Heo } 621382607adcSTejun Heo 621482607adcSTejun Heo rcu_read_unlock(); 621582607adcSTejun Heo 621682607adcSTejun Heo if (lockup_detected) 621755df0933SImran Khan show_all_workqueues(); 621882607adcSTejun Heo 6219cd2440d6SPetr Mladek if (cpu_pool_stall) 6220cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6221cd2440d6SPetr Mladek 622282607adcSTejun Heo wq_watchdog_reset_touched(); 622382607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 622482607adcSTejun Heo } 622582607adcSTejun Heo 6226cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 622782607adcSTejun Heo { 622882607adcSTejun Heo if (cpu >= 0) 622982607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 623089e28ce6SWang Qing 623182607adcSTejun Heo wq_watchdog_touched = jiffies; 623282607adcSTejun Heo } 623382607adcSTejun Heo 623482607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 623582607adcSTejun Heo { 623682607adcSTejun Heo wq_watchdog_thresh = 0; 623782607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 623882607adcSTejun Heo 623982607adcSTejun Heo if (thresh) { 624082607adcSTejun Heo wq_watchdog_thresh = thresh; 624182607adcSTejun Heo wq_watchdog_reset_touched(); 624282607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 624382607adcSTejun Heo } 624482607adcSTejun Heo } 624582607adcSTejun Heo 624682607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 624782607adcSTejun Heo const struct kernel_param *kp) 624882607adcSTejun Heo { 624982607adcSTejun Heo unsigned long thresh; 625082607adcSTejun Heo int ret; 625182607adcSTejun Heo 625282607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 625382607adcSTejun Heo if (ret) 625482607adcSTejun Heo return ret; 625582607adcSTejun Heo 625682607adcSTejun Heo if (system_wq) 625782607adcSTejun Heo wq_watchdog_set_thresh(thresh); 625882607adcSTejun Heo else 625982607adcSTejun Heo wq_watchdog_thresh = thresh; 626082607adcSTejun Heo 626182607adcSTejun Heo return 0; 626282607adcSTejun Heo } 626382607adcSTejun Heo 626482607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 626582607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 626682607adcSTejun Heo .get = param_get_ulong, 626782607adcSTejun Heo }; 626882607adcSTejun Heo 626982607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 627082607adcSTejun Heo 0644); 627182607adcSTejun Heo 627282607adcSTejun Heo static void wq_watchdog_init(void) 627382607adcSTejun Heo { 62745cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 627582607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 627682607adcSTejun Heo } 627782607adcSTejun Heo 627882607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 627982607adcSTejun Heo 628082607adcSTejun Heo static inline void wq_watchdog_init(void) { } 628182607adcSTejun Heo 628282607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 628382607adcSTejun Heo 6284bce90380STejun Heo static void __init wq_numa_init(void) 6285bce90380STejun Heo { 6286bce90380STejun Heo cpumask_var_t *tbl; 6287bce90380STejun Heo int node, cpu; 6288bce90380STejun Heo 6289bce90380STejun Heo if (num_possible_nodes() <= 1) 6290bce90380STejun Heo return; 6291bce90380STejun Heo 6292d55262c4STejun Heo if (wq_disable_numa) { 6293d55262c4STejun Heo pr_info("workqueue: NUMA affinity support disabled\n"); 6294d55262c4STejun Heo return; 6295d55262c4STejun Heo } 6296d55262c4STejun Heo 6297f728c4a9SZhen Lei for_each_possible_cpu(cpu) { 6298f728c4a9SZhen Lei if (WARN_ON(cpu_to_node(cpu) == NUMA_NO_NODE)) { 6299f728c4a9SZhen Lei pr_warn("workqueue: NUMA node mapping not available for cpu%d, disabling NUMA support\n", cpu); 6300f728c4a9SZhen Lei return; 6301f728c4a9SZhen Lei } 6302f728c4a9SZhen Lei } 6303f728c4a9SZhen Lei 6304be69d00dSThomas Gleixner wq_update_unbound_numa_attrs_buf = alloc_workqueue_attrs(); 63054c16bd32STejun Heo BUG_ON(!wq_update_unbound_numa_attrs_buf); 63064c16bd32STejun Heo 6307bce90380STejun Heo /* 6308bce90380STejun Heo * We want masks of possible CPUs of each node which isn't readily 6309bce90380STejun Heo * available. Build one from cpu_to_node() which should have been 6310bce90380STejun Heo * fully initialized by now. 6311bce90380STejun Heo */ 63126396bb22SKees Cook tbl = kcalloc(nr_node_ids, sizeof(tbl[0]), GFP_KERNEL); 6313bce90380STejun Heo BUG_ON(!tbl); 6314bce90380STejun Heo 6315bce90380STejun Heo for_each_node(node) 63165a6024f1SYasuaki Ishimatsu BUG_ON(!zalloc_cpumask_var_node(&tbl[node], GFP_KERNEL, 63171be0c25dSTejun Heo node_online(node) ? node : NUMA_NO_NODE)); 6318bce90380STejun Heo 6319bce90380STejun Heo for_each_possible_cpu(cpu) { 6320bce90380STejun Heo node = cpu_to_node(cpu); 6321bce90380STejun Heo cpumask_set_cpu(cpu, tbl[node]); 6322bce90380STejun Heo } 6323bce90380STejun Heo 6324bce90380STejun Heo wq_numa_possible_cpumask = tbl; 6325bce90380STejun Heo wq_numa_enabled = true; 6326bce90380STejun Heo } 6327bce90380STejun Heo 63283347fa09STejun Heo /** 63293347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 63303347fa09STejun Heo * 63313347fa09STejun Heo * This is the first half of two-staged workqueue subsystem initialization 63323347fa09STejun Heo * and invoked as soon as the bare basics - memory allocation, cpumasks and 63333347fa09STejun Heo * idr are up. It sets up all the data structures and system workqueues 63343347fa09STejun Heo * and allows early boot code to create workqueues and queue/cancel work 63353347fa09STejun Heo * items. Actual work item execution starts only after kthreads can be 63363347fa09STejun Heo * created and scheduled right before early initcalls. 63373347fa09STejun Heo */ 63382333e829SYu Chen void __init workqueue_init_early(void) 63391da177e4SLinus Torvalds { 63407a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 63417a4e344cSTejun Heo int i, cpu; 6342c34056a3STejun Heo 634310cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6344e904e6c2STejun Heo 6345b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 634604d4e665SFrederic Weisbecker cpumask_copy(wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_WQ)); 634704d4e665SFrederic Weisbecker cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, housekeeping_cpumask(HK_TYPE_DOMAIN)); 6348b05a7928SFrederic Weisbecker 6349e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6350e904e6c2STejun Heo 6351706026c2STejun Heo /* initialize CPU pools */ 635229c91e99STejun Heo for_each_possible_cpu(cpu) { 63534ce62e9eSTejun Heo struct worker_pool *pool; 63548b03ae3cSTejun Heo 63557a4e344cSTejun Heo i = 0; 6356f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63577a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 6358ec22ca5eSTejun Heo pool->cpu = cpu; 63597a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 63607a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 6361f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 63627a4e344cSTejun Heo 63639daf9e67STejun Heo /* alloc pool ID */ 636468e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63659daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 636668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 63674ce62e9eSTejun Heo } 63688b03ae3cSTejun Heo } 63698b03ae3cSTejun Heo 63708a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 637129c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 637229c91e99STejun Heo struct workqueue_attrs *attrs; 637329c91e99STejun Heo 6374be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 637529c91e99STejun Heo attrs->nice = std_nice[i]; 637629c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 63778a2b7538STejun Heo 63788a2b7538STejun Heo /* 63798a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 63808a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 63818a2b7538STejun Heo * Turn off NUMA so that dfl_pwq is used for all nodes. 63828a2b7538STejun Heo */ 6383be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 63848a2b7538STejun Heo attrs->nice = std_nice[i]; 63858a2b7538STejun Heo attrs->no_numa = true; 63868a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 638729c91e99STejun Heo } 638829c91e99STejun Heo 6389d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 63901aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 6391d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 6392f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6393f3421797STejun Heo WQ_UNBOUND_MAX_ACTIVE); 639424d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 639524d51addSTejun Heo WQ_FREEZABLE, 0); 63960668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 63970668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 63980668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 63990668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 64000668106cSViresh Kumar 0); 64011aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 64020668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 64030668106cSViresh Kumar !system_power_efficient_wq || 64040668106cSViresh Kumar !system_freezable_power_efficient_wq); 64053347fa09STejun Heo } 64063347fa09STejun Heo 64073347fa09STejun Heo /** 64083347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 64093347fa09STejun Heo * 64103347fa09STejun Heo * This is the latter half of two-staged workqueue subsystem initialization 64113347fa09STejun Heo * and invoked as soon as kthreads can be created and scheduled. 64123347fa09STejun Heo * Workqueues have been created and work items queued on them, but there 64133347fa09STejun Heo * are no kworkers executing the work items yet. Populate the worker pools 64143347fa09STejun Heo * with the initial workers and enable future kworker creations. 64153347fa09STejun Heo */ 64162333e829SYu Chen void __init workqueue_init(void) 64173347fa09STejun Heo { 64182186d9f9STejun Heo struct workqueue_struct *wq; 64193347fa09STejun Heo struct worker_pool *pool; 64203347fa09STejun Heo int cpu, bkt; 64213347fa09STejun Heo 64222186d9f9STejun Heo /* 64232186d9f9STejun Heo * It'd be simpler to initialize NUMA in workqueue_init_early() but 64242186d9f9STejun Heo * CPU to node mapping may not be available that early on some 64252186d9f9STejun Heo * archs such as power and arm64. As per-cpu pools created 64262186d9f9STejun Heo * previously could be missing node hint and unbound pools NUMA 64272186d9f9STejun Heo * affinity, fix them up. 642840c17f75STejun Heo * 642940c17f75STejun Heo * Also, while iterating workqueues, create rescuers if requested. 64302186d9f9STejun Heo */ 64312186d9f9STejun Heo wq_numa_init(); 64322186d9f9STejun Heo 64332186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 64342186d9f9STejun Heo 64352186d9f9STejun Heo for_each_possible_cpu(cpu) { 64362186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 64372186d9f9STejun Heo pool->node = cpu_to_node(cpu); 64382186d9f9STejun Heo } 64392186d9f9STejun Heo } 64402186d9f9STejun Heo 644140c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 64422186d9f9STejun Heo wq_update_unbound_numa(wq, smp_processor_id(), true); 644340c17f75STejun Heo WARN(init_rescuer(wq), 644440c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 644540c17f75STejun Heo wq->name); 644640c17f75STejun Heo } 64472186d9f9STejun Heo 64482186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 64492186d9f9STejun Heo 64503347fa09STejun Heo /* create the initial workers */ 64513347fa09STejun Heo for_each_online_cpu(cpu) { 64523347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 64533347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 64543347fa09STejun Heo BUG_ON(!create_worker(pool)); 64553347fa09STejun Heo } 64563347fa09STejun Heo } 64573347fa09STejun Heo 64583347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 64593347fa09STejun Heo BUG_ON(!create_worker(pool)); 64603347fa09STejun Heo 64613347fa09STejun Heo wq_online = true; 646282607adcSTejun Heo wq_watchdog_init(); 64631da177e4SLinus Torvalds } 6464c4f135d6STetsuo Handa 6465c4f135d6STetsuo Handa /* 6466c4f135d6STetsuo Handa * Despite the naming, this is a no-op function which is here only for avoiding 6467c4f135d6STetsuo Handa * link error. Since compile-time warning may fail to catch, we will need to 6468c4f135d6STetsuo Handa * emit run-time warning from __flush_workqueue(). 6469c4f135d6STetsuo Handa */ 6470c4f135d6STetsuo Handa void __warn_flushing_systemwide_wq(void) { } 6471c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 6472