1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 41da177e4SLinus Torvalds * 5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 81da177e4SLinus Torvalds * David Woodhouse <dwmw2@infradead.org> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <wpp@marie.physik.tu-berlin.de> 111da177e4SLinus Torvalds * Theodore Ts'o <tytso@mit.edu> 1289ada679SChristoph Lameter * 13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 14c54fce6eSTejun Heo * 15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <tj@kernel.org> 17c54fce6eSTejun Heo * 18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 21b11895c4SLibin * normal work items and the other for high priority ones) and some extra 22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 23b11895c4SLibin * number of these backing pools is dynamic. 24c54fce6eSTejun Heo * 259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 289984de1aSPaul Gortmaker #include <linux/export.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/signal.h> 331da177e4SLinus Torvalds #include <linux/completion.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/notifier.h> 381da177e4SLinus Torvalds #include <linux/kthread.h> 391fa44ecaSJames Bottomley #include <linux/hardirq.h> 4046934023SChristoph Lameter #include <linux/mempolicy.h> 41341a5958SRafael J. Wysocki #include <linux/freezer.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 52cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5362635ea8SSergey Senozhatsky #include <linux/nmi.h> 54940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 55aa6fde93STejun Heo #include <linux/delay.h> 56e22bee78STejun Heo 57ea138446STejun Heo #include "workqueue_internal.h" 581da177e4SLinus Torvalds 59c8e55f36STejun Heo enum { 60bc2ae0f5STejun Heo /* 6124647570STejun Heo * worker_pool flags 62bc2ae0f5STejun Heo * 6324647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 64bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 65bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 66bc2ae0f5STejun Heo * is in effect. 67bc2ae0f5STejun Heo * 68bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 69bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7024647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 71bc2ae0f5STejun Heo * 72bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 731258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 744736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 75bc2ae0f5STejun Heo */ 76692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7724647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 78db7bccf4STejun Heo 79c8e55f36STejun Heo /* worker flags */ 80c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 81c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 82e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 83fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 84f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 85a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 86e22bee78STejun Heo 87a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 88a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 89db7bccf4STejun Heo 90e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 914ce62e9eSTejun Heo 9229c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 93c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 94db7bccf4STejun Heo 95e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 96e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 97e22bee78STejun Heo 983233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 993233cdbdSTejun Heo /* call for help after 10ms 1003233cdbdSTejun Heo (min two ticks) */ 101e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 102e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* 105e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1068698a745SDongsheng Yang * all cpus. Give MIN_NICE. 107e22bee78STejun Heo */ 1088698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1098698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 110ecf6881fSTejun Heo 11143a181f8SAudra Mitchell WQ_NAME_LEN = 32, 112c8e55f36STejun Heo }; 113c8e55f36STejun Heo 1141da177e4SLinus Torvalds /* 1154690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1164690c4abSTejun Heo * 117e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 118e41e704bSTejun Heo * everyone else. 1194690c4abSTejun Heo * 120e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 121e22bee78STejun Heo * only be modified and accessed from the local cpu. 122e22bee78STejun Heo * 123d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1244690c4abSTejun Heo * 125bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 126bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 127bdf8b9bfSTejun Heo * kworker. 128bdf8b9bfSTejun Heo * 129bdf8b9bfSTejun Heo * S: Only modified by worker self. 130bdf8b9bfSTejun Heo * 1311258fae7STejun Heo * A: wq_pool_attach_mutex protected. 132822d8405STejun Heo * 13368e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 13476af4d93STejun Heo * 13524acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1365bcab335STejun Heo * 1375b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1385b95e1afSLai Jiangshan * 1395b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 14024acfb71SThomas Gleixner * RCU for reads. 1415b95e1afSLai Jiangshan * 1423c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1433c25a55dSLai Jiangshan * 14424acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1452e109a28STejun Heo * 14682e098f5STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 14782e098f5STejun Heo * with READ_ONCE() without locking. 14882e098f5STejun Heo * 1492e109a28STejun Heo * MD: wq_mayday_lock protected. 150cd2440d6SPetr Mladek * 151cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1524690c4abSTejun Heo */ 1534690c4abSTejun Heo 1542eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 155c34056a3STejun Heo 156bd7bdd43STejun Heo struct worker_pool { 157a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 158d84ff051STejun Heo int cpu; /* I: the associated cpu */ 159f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1609daf9e67STejun Heo int id; /* I: pool ID */ 161bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 162bd7bdd43STejun Heo 16382607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 164cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 16582607adcSTejun Heo 166bc35f7efSLai Jiangshan /* 167bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 168bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 169bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 170bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 171bc35f7efSLai Jiangshan */ 172bc35f7efSLai Jiangshan int nr_running; 17384f91c62SLai Jiangshan 174bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 175ea1abd61SLai Jiangshan 1765826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1775826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 178bd7bdd43STejun Heo 1792c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 180bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1813f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1823f959aa3SValentin Schneider 183bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 184bd7bdd43STejun Heo 185c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 186c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 187c9e7cf27STejun Heo /* L: hash of busy workers */ 188c9e7cf27STejun Heo 1892607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 19092f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 191e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 19260f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 193e19e397aSTejun Heo 1947cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 195e19e397aSTejun Heo 1967a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 19768e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 19868e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 1997a4e344cSTejun Heo 200e19e397aSTejun Heo /* 20124acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 20229c91e99STejun Heo * from get_work_pool(). 20329c91e99STejun Heo */ 20429c91e99STejun Heo struct rcu_head rcu; 20584f91c62SLai Jiangshan }; 2068b03ae3cSTejun Heo 2078b03ae3cSTejun Heo /* 208725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 209725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 210725e8ec5STejun Heo */ 211725e8ec5STejun Heo enum pool_workqueue_stats { 212725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 213725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2148a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 215616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 216725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2178639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 218725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 219725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 220725e8ec5STejun Heo 221725e8ec5STejun Heo PWQ_NR_STATS, 222725e8ec5STejun Heo }; 223725e8ec5STejun Heo 224725e8ec5STejun Heo /* 225112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 226112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 227112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 228112202d9STejun Heo * number of flag bits. 2291da177e4SLinus Torvalds */ 230112202d9STejun Heo struct pool_workqueue { 231bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2324690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 23373f53c4aSTejun Heo int work_color; /* L: current color */ 23473f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2358864b4e5STejun Heo int refcnt; /* L: reference count */ 23673f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 23773f53c4aSTejun Heo /* L: nr of in_flight works */ 238018f3a13SLai Jiangshan 239018f3a13SLai Jiangshan /* 240018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 241018f3a13SLai Jiangshan * 242018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 243018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 244018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 245018f3a13SLai Jiangshan * 246018f3a13SLai Jiangshan * All work items marked with WORK_STRUCT_INACTIVE do not participate 247018f3a13SLai Jiangshan * in pwq->nr_active and all work items in pwq->inactive_works are 248018f3a13SLai Jiangshan * marked with WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE 249018f3a13SLai Jiangshan * work items are in pwq->inactive_works. Some of them are ready to 250018f3a13SLai Jiangshan * run in pool->worklist or worker->scheduled. Those work itmes are 251018f3a13SLai Jiangshan * only struct wq_barrier which is used for flush_work() and should 252018f3a13SLai Jiangshan * not participate in pwq->nr_active. For non-barrier work item, it 253018f3a13SLai Jiangshan * is marked with WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 254018f3a13SLai Jiangshan */ 2551e19ffc6STejun Heo int nr_active; /* L: nr of 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 /* 263967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 264687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 265687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 266967b494eSTejun Heo * grabbing wq->mutex. 2678864b4e5STejun Heo */ 268687a9aa5STejun Heo struct kthread_work 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 /* 284*b522229aSTejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 285*b522229aSTejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 286*b522229aSTejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 287*b522229aSTejun Heo * the counting and enforcement is per NUMA node. 288*b522229aSTejun Heo */ 289*b522229aSTejun Heo struct wq_node_nr_active { 290*b522229aSTejun Heo atomic_t nr; /* per-node nr_active count */ 291*b522229aSTejun Heo }; 292*b522229aSTejun Heo 293*b522229aSTejun Heo /* 294c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 295c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 2961da177e4SLinus Torvalds */ 2971da177e4SLinus Torvalds struct workqueue_struct { 2983c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 299e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 30073f53c4aSTejun Heo 3013c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3023c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3033c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 304112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3053c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3063c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3073c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 30873f53c4aSTejun Heo 3092e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 31030ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 311e22bee78STejun Heo 31287fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 31382e098f5STejun Heo int max_active; /* WO: max active works */ 31482e098f5STejun Heo int saved_max_active; /* WQ: saved max_active */ 315226223abSTejun Heo 3165b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 317bd31fb92STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3186029a918STejun Heo 319226223abSTejun Heo #ifdef CONFIG_SYSFS 320226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 321226223abSTejun Heo #endif 3224e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 323669de8bdSBart Van Assche char *lock_name; 324669de8bdSBart Van Assche struct lock_class_key key; 3254e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3264e6045f1SJohannes Berg #endif 327ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3282728fd2fSTejun Heo 329e2dca7adSTejun Heo /* 33024acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 33124acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 332e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 333e2dca7adSTejun Heo */ 334e2dca7adSTejun Heo struct rcu_head rcu; 335e2dca7adSTejun Heo 3362728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3372728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 338636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 339*b522229aSTejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3401da177e4SLinus Torvalds }; 3411da177e4SLinus Torvalds 342e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 343e904e6c2STejun Heo 34484193c07STejun Heo /* 34584193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 34684193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 34784193c07STejun Heo */ 34884193c07STejun Heo struct wq_pod_type { 34984193c07STejun Heo int nr_pods; /* number of pods */ 35084193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 35184193c07STejun Heo int *pod_node; /* pod -> node */ 35284193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 35384193c07STejun Heo }; 35484193c07STejun Heo 35584193c07STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 356523a301eSTejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 35763c5484eSTejun Heo 35863c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 359523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 36063c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 36163c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 36263c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 36363c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 36463c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 36563c5484eSTejun Heo }; 366bce90380STejun Heo 367616db877STejun Heo /* 368616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 369616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 370616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 371aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 372aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 373616db877STejun Heo */ 374aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 375616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 376616db877STejun Heo 377cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 378552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 379cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 380cee22a15SViresh Kumar 381863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 3823347fa09STejun Heo 383fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 384fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 3854c16bd32STejun Heo 38668e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 3871258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 388a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 389d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 390d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 3915bcab335STejun Heo 392e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 39368e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 3947d19c5ceSTejun Heo 39599c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 396ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 397ef557180SMike Galbraith 398ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 399ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 400ace3c549Stiozhang 401ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 402ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 403b05a7928SFrederic Weisbecker 404f303fccbSTejun Heo /* 405f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 406f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 407f303fccbSTejun Heo * to uncover usages which depend on it. 408f303fccbSTejun Heo */ 409f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 410f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 411f303fccbSTejun Heo #else 412f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 413f303fccbSTejun Heo #endif 414f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 415f303fccbSTejun Heo 4167d19c5ceSTejun Heo /* the per-cpu worker pools */ 41725528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 4187d19c5ceSTejun Heo 41968e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4207d19c5ceSTejun Heo 42168e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 42229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 42329c91e99STejun Heo 424c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 42529c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 42629c91e99STejun Heo 4278a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4288a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4298a2b7538STejun Heo 430967b494eSTejun Heo /* 431967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 432967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 433967b494eSTejun Heo * worker to avoid A-A deadlocks. 434967b494eSTejun Heo */ 435967b494eSTejun Heo static struct kthread_worker *pwq_release_worker; 436967b494eSTejun Heo 437d320c038STejun Heo struct workqueue_struct *system_wq __read_mostly; 438ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 439044c782cSValentin Ilie struct workqueue_struct *system_highpri_wq __read_mostly; 4401aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 441044c782cSValentin Ilie struct workqueue_struct *system_long_wq __read_mostly; 442d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 443044c782cSValentin Ilie struct workqueue_struct *system_unbound_wq __read_mostly; 444f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 445044c782cSValentin Ilie struct workqueue_struct *system_freezable_wq __read_mostly; 44624d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 4470668106cSViresh Kumar struct workqueue_struct *system_power_efficient_wq __read_mostly; 4480668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 4490668106cSViresh Kumar struct workqueue_struct *system_freezable_power_efficient_wq __read_mostly; 4500668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 451d320c038STejun Heo 4527d19c5ceSTejun Heo static int worker_thread(void *__worker); 4536ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 454c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 45555df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4567d19c5ceSTejun Heo 45797bd2347STejun Heo #define CREATE_TRACE_POINTS 45897bd2347STejun Heo #include <trace/events/workqueue.h> 45997bd2347STejun Heo 46068e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 46124acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 462f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 46324acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4645bcab335STejun Heo 4655b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 46624acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 467f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 468f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 46924acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4705b95e1afSLai Jiangshan 471f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 472f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 473f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 4747a62c2c8STejun Heo (pool)++) 4754ce62e9eSTejun Heo 47649e3cf44STejun Heo /** 47717116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 47817116969STejun Heo * @pool: iteration cursor 479611c92a0STejun Heo * @pi: integer used for iteration 480fa1b54e6STejun Heo * 48124acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 48268e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 48368e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 484fa1b54e6STejun Heo * 485fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 486fa1b54e6STejun Heo * ignored. 48717116969STejun Heo */ 488611c92a0STejun Heo #define for_each_pool(pool, pi) \ 489611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 49068e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 491fa1b54e6STejun Heo else 49217116969STejun Heo 49317116969STejun Heo /** 494822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 495822d8405STejun Heo * @worker: iteration cursor 496822d8405STejun Heo * @pool: worker_pool to iterate workers of 497822d8405STejun Heo * 4981258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 499822d8405STejun Heo * 500822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 501822d8405STejun Heo * ignored. 502822d8405STejun Heo */ 503da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 504da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5051258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 506822d8405STejun Heo else 507822d8405STejun Heo 508822d8405STejun Heo /** 50949e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 51049e3cf44STejun Heo * @pwq: iteration cursor 51149e3cf44STejun Heo * @wq: the target workqueue 51276af4d93STejun Heo * 51324acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 514794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 515794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 51676af4d93STejun Heo * 51776af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 51876af4d93STejun Heo * ignored. 51949e3cf44STejun Heo */ 52049e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 52149e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5225a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 523f3421797STejun Heo 524dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 525dc186ad7SThomas Gleixner 526f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 527dc186ad7SThomas Gleixner 52899777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 52999777288SStanislaw Gruszka { 53099777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 53199777288SStanislaw Gruszka } 53299777288SStanislaw Gruszka 533b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 534b9fdac7fSDu, Changbin { 535b9fdac7fSDu, Changbin struct work_struct *work = addr; 536b9fdac7fSDu, Changbin 537b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 538b9fdac7fSDu, Changbin } 539b9fdac7fSDu, Changbin 540dc186ad7SThomas Gleixner /* 541dc186ad7SThomas Gleixner * fixup_init is called when: 542dc186ad7SThomas Gleixner * - an active object is initialized 543dc186ad7SThomas Gleixner */ 54402a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 545dc186ad7SThomas Gleixner { 546dc186ad7SThomas Gleixner struct work_struct *work = addr; 547dc186ad7SThomas Gleixner 548dc186ad7SThomas Gleixner switch (state) { 549dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 550dc186ad7SThomas Gleixner cancel_work_sync(work); 551dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 55202a982a6SDu, Changbin return true; 553dc186ad7SThomas Gleixner default: 55402a982a6SDu, Changbin return false; 555dc186ad7SThomas Gleixner } 556dc186ad7SThomas Gleixner } 557dc186ad7SThomas Gleixner 558dc186ad7SThomas Gleixner /* 559dc186ad7SThomas Gleixner * fixup_free is called when: 560dc186ad7SThomas Gleixner * - an active object is freed 561dc186ad7SThomas Gleixner */ 56202a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 563dc186ad7SThomas Gleixner { 564dc186ad7SThomas Gleixner struct work_struct *work = addr; 565dc186ad7SThomas Gleixner 566dc186ad7SThomas Gleixner switch (state) { 567dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 568dc186ad7SThomas Gleixner cancel_work_sync(work); 569dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 57002a982a6SDu, Changbin return true; 571dc186ad7SThomas Gleixner default: 57202a982a6SDu, Changbin return false; 573dc186ad7SThomas Gleixner } 574dc186ad7SThomas Gleixner } 575dc186ad7SThomas Gleixner 576f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 577dc186ad7SThomas Gleixner .name = "work_struct", 57899777288SStanislaw Gruszka .debug_hint = work_debug_hint, 579b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 580dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 581dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 582dc186ad7SThomas Gleixner }; 583dc186ad7SThomas Gleixner 584dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 585dc186ad7SThomas Gleixner { 586dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 587dc186ad7SThomas Gleixner } 588dc186ad7SThomas Gleixner 589dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 590dc186ad7SThomas Gleixner { 591dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 592dc186ad7SThomas Gleixner } 593dc186ad7SThomas Gleixner 594dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 595dc186ad7SThomas Gleixner { 596dc186ad7SThomas Gleixner if (onstack) 597dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 598dc186ad7SThomas Gleixner else 599dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 600dc186ad7SThomas Gleixner } 601dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 602dc186ad7SThomas Gleixner 603dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 604dc186ad7SThomas Gleixner { 605dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 606dc186ad7SThomas Gleixner } 607dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 608dc186ad7SThomas Gleixner 609ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 610ea2e64f2SThomas Gleixner { 611ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 612ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 613ea2e64f2SThomas Gleixner } 614ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 615ea2e64f2SThomas Gleixner 616dc186ad7SThomas Gleixner #else 617dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 618dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 619dc186ad7SThomas Gleixner #endif 620dc186ad7SThomas Gleixner 6214e8b22bdSLi Bin /** 62267dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6234e8b22bdSLi Bin * @pool: the pool pointer of interest 6244e8b22bdSLi Bin * 6254e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6264e8b22bdSLi Bin * successfully, -errno on failure. 6274e8b22bdSLi Bin */ 6289daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6299daf9e67STejun Heo { 6309daf9e67STejun Heo int ret; 6319daf9e67STejun Heo 63268e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6335bcab335STejun Heo 6344e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6354e8b22bdSLi Bin GFP_KERNEL); 636229641a6STejun Heo if (ret >= 0) { 637e68035fbSTejun Heo pool->id = ret; 638229641a6STejun Heo return 0; 639229641a6STejun Heo } 6409daf9e67STejun Heo return ret; 6419daf9e67STejun Heo } 6429daf9e67STejun Heo 643bd31fb92STejun Heo static struct pool_workqueue __rcu ** 644bd31fb92STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 645bd31fb92STejun Heo { 646bd31fb92STejun Heo if (cpu >= 0) 647bd31fb92STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 648bd31fb92STejun Heo else 649bd31fb92STejun Heo return &wq->dfl_pwq; 650bd31fb92STejun Heo } 651bd31fb92STejun Heo 652bd31fb92STejun Heo /* @cpu < 0 for dfl_pwq */ 653bd31fb92STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 654bd31fb92STejun Heo { 655bd31fb92STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 656bd31fb92STejun Heo lockdep_is_held(&wq_pool_mutex) || 657bd31fb92STejun Heo lockdep_is_held(&wq->mutex)); 658bd31fb92STejun Heo } 659bd31fb92STejun Heo 66073f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 66173f53c4aSTejun Heo { 66273f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 66373f53c4aSTejun Heo } 66473f53c4aSTejun Heo 665c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 66673f53c4aSTejun Heo { 667c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 66873f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 66973f53c4aSTejun Heo } 67073f53c4aSTejun Heo 67173f53c4aSTejun Heo static int work_next_color(int color) 67273f53c4aSTejun Heo { 67373f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 674a848e3b6SOleg Nesterov } 675a848e3b6SOleg Nesterov 6764594bf15SDavid Howells /* 677112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 678112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 6797c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 6807a22ad75STejun Heo * 681112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 682112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 683bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 684bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 6857a22ad75STejun Heo * 686112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 6877c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 688112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 6897c3eed5cSTejun Heo * available only while the work item is queued. 690bbb68dfaSTejun Heo * 691bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 692bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 693bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 694bbb68dfaSTejun Heo * try to steal the PENDING bit. 6954594bf15SDavid Howells */ 6967a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 6977a22ad75STejun Heo unsigned long flags) 6987a22ad75STejun Heo { 6996183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7007a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7017a22ad75STejun Heo } 7027a22ad75STejun Heo 703112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7044690c4abSTejun Heo unsigned long extra_flags) 705365970a1SDavid Howells { 706112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 707112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 708365970a1SDavid Howells } 709365970a1SDavid Howells 7104468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7114468a00fSLai Jiangshan int pool_id) 7124468a00fSLai Jiangshan { 7134468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7144468a00fSLai Jiangshan WORK_STRUCT_PENDING); 7154468a00fSLai Jiangshan } 7164468a00fSLai Jiangshan 7177c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7187c3eed5cSTejun Heo int pool_id) 7194d707b9fSOleg Nesterov { 72023657bb1STejun Heo /* 72123657bb1STejun Heo * The following wmb is paired with the implied mb in 72223657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 72323657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 72423657bb1STejun Heo * owner. 72523657bb1STejun Heo */ 72623657bb1STejun Heo smp_wmb(); 7277c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 728346c09f8SRoman Pen /* 729346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 730346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 731346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7328bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 733346c09f8SRoman Pen * the same @work. E.g. consider this case: 734346c09f8SRoman Pen * 735346c09f8SRoman Pen * CPU#0 CPU#1 736346c09f8SRoman Pen * ---------------------------- -------------------------------- 737346c09f8SRoman Pen * 738346c09f8SRoman Pen * 1 STORE event_indicated 739346c09f8SRoman Pen * 2 queue_work_on() { 740346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 741346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 742346c09f8SRoman Pen * 5 set_work_data() # clear bit 743346c09f8SRoman Pen * 6 smp_mb() 744346c09f8SRoman Pen * 7 work->current_func() { 745346c09f8SRoman Pen * 8 LOAD event_indicated 746346c09f8SRoman Pen * } 747346c09f8SRoman Pen * 748346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 749346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 750346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 751346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 752346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 753346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 754346c09f8SRoman Pen * before actual STORE. 755346c09f8SRoman Pen */ 756346c09f8SRoman Pen smp_mb(); 7574d707b9fSOleg Nesterov } 7584d707b9fSOleg Nesterov 7597a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 760365970a1SDavid Howells { 7617c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 7627c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 7637a22ad75STejun Heo } 7647a22ad75STejun Heo 765afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 766afa4bb77SLinus Torvalds { 767afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 768afa4bb77SLinus Torvalds } 769afa4bb77SLinus Torvalds 770112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 7717a22ad75STejun Heo { 772e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7737a22ad75STejun Heo 774112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 775afa4bb77SLinus Torvalds return work_struct_pwq(data); 776e120153dSTejun Heo else 777e120153dSTejun Heo return NULL; 7787a22ad75STejun Heo } 7797a22ad75STejun Heo 7807c3eed5cSTejun Heo /** 7817c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 7827c3eed5cSTejun Heo * @work: the work item of interest 7837c3eed5cSTejun Heo * 78468e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 78524acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 78624acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 787fa1b54e6STejun Heo * 788fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 789fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 790fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 791fa1b54e6STejun Heo * returned pool is and stays online. 792d185af30SYacine Belkadi * 793d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 7947c3eed5cSTejun Heo */ 7957c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 7967a22ad75STejun Heo { 797e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 7987c3eed5cSTejun Heo int pool_id; 7997a22ad75STejun Heo 80068e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 801fa1b54e6STejun Heo 802112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 803afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8047a22ad75STejun Heo 8057c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8067c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8077a22ad75STejun Heo return NULL; 8087a22ad75STejun Heo 809fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8107c3eed5cSTejun Heo } 8117c3eed5cSTejun Heo 8127c3eed5cSTejun Heo /** 8137c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8147c3eed5cSTejun Heo * @work: the work item of interest 8157c3eed5cSTejun Heo * 816d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8177c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8187c3eed5cSTejun Heo */ 8197c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8207c3eed5cSTejun Heo { 82154d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 8227c3eed5cSTejun Heo 823112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 824afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 82554d5b7d0SLai Jiangshan 82654d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 8277c3eed5cSTejun Heo } 8287c3eed5cSTejun Heo 829bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 830bbb68dfaSTejun Heo { 8317c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 832bbb68dfaSTejun Heo 8337c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8347c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 835bbb68dfaSTejun Heo } 836bbb68dfaSTejun Heo 837bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 838bbb68dfaSTejun Heo { 839bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 840bbb68dfaSTejun Heo 841112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 842bbb68dfaSTejun Heo } 843bbb68dfaSTejun Heo 844e22bee78STejun Heo /* 8453270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8463270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 847d565ed63STejun Heo * they're being called with pool->lock held. 848e22bee78STejun Heo */ 849e22bee78STejun Heo 850e22bee78STejun Heo /* 851e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 852e22bee78STejun Heo * running workers. 853974271c4STejun Heo * 854974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 855706026c2STejun Heo * function will always return %true for unbound pools as long as the 856974271c4STejun Heo * worklist isn't empty. 857e22bee78STejun Heo */ 85863d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 859e22bee78STejun Heo { 8600219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 861e22bee78STejun Heo } 862e22bee78STejun Heo 863e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 86463d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 865e22bee78STejun Heo { 86663d95a91STejun Heo return pool->nr_idle; 867e22bee78STejun Heo } 868e22bee78STejun Heo 869e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 87063d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 871e22bee78STejun Heo { 872bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 873e22bee78STejun Heo } 874e22bee78STejun Heo 875e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 87663d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 877e22bee78STejun Heo { 87863d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 879e22bee78STejun Heo } 880e22bee78STejun Heo 881e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 88263d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 883e22bee78STejun Heo { 884692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 88563d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 88663d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 887e22bee78STejun Heo 888e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 889e22bee78STejun Heo } 890e22bee78STejun Heo 8914690c4abSTejun Heo /** 892e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 893cb444766STejun Heo * @worker: self 894d302f017STejun Heo * @flags: flags to set 895d302f017STejun Heo * 896228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 897d302f017STejun Heo */ 898228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 899d302f017STejun Heo { 900bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 901e22bee78STejun Heo 902bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 903cb444766STejun Heo 904228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 905e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 906e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 907bc35f7efSLai Jiangshan pool->nr_running--; 908e22bee78STejun Heo } 909e22bee78STejun Heo 910d302f017STejun Heo worker->flags |= flags; 911d302f017STejun Heo } 912d302f017STejun Heo 913d302f017STejun Heo /** 914e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 915cb444766STejun Heo * @worker: self 916d302f017STejun Heo * @flags: flags to clear 917d302f017STejun Heo * 918e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 919d302f017STejun Heo */ 920d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 921d302f017STejun Heo { 92263d95a91STejun Heo struct worker_pool *pool = worker->pool; 923e22bee78STejun Heo unsigned int oflags = worker->flags; 924e22bee78STejun Heo 925bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 926cb444766STejun Heo 927d302f017STejun Heo worker->flags &= ~flags; 928e22bee78STejun Heo 92942c025f3STejun Heo /* 93042c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 93142c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 93242c025f3STejun Heo * of multiple flags, not a single flag. 93342c025f3STejun Heo */ 934e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 935e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 936bc35f7efSLai Jiangshan pool->nr_running++; 937d302f017STejun Heo } 938d302f017STejun Heo 939797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 940797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 941797e8345STejun Heo { 942797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 943797e8345STejun Heo return NULL; 944797e8345STejun Heo 945797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 946797e8345STejun Heo } 947797e8345STejun Heo 948797e8345STejun Heo /** 949797e8345STejun Heo * worker_enter_idle - enter idle state 950797e8345STejun Heo * @worker: worker which is entering idle state 951797e8345STejun Heo * 952797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 953797e8345STejun Heo * necessary. 954797e8345STejun Heo * 955797e8345STejun Heo * LOCKING: 956797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 957797e8345STejun Heo */ 958797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 959797e8345STejun Heo { 960797e8345STejun Heo struct worker_pool *pool = worker->pool; 961797e8345STejun Heo 962797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 963797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 964797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 965797e8345STejun Heo return; 966797e8345STejun Heo 967797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 968797e8345STejun Heo worker->flags |= WORKER_IDLE; 969797e8345STejun Heo pool->nr_idle++; 970797e8345STejun Heo worker->last_active = jiffies; 971797e8345STejun Heo 972797e8345STejun Heo /* idle_list is LIFO */ 973797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 974797e8345STejun Heo 975797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 976797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 977797e8345STejun Heo 978797e8345STejun Heo /* Sanity check nr_running. */ 979797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 980797e8345STejun Heo } 981797e8345STejun Heo 982797e8345STejun Heo /** 983797e8345STejun Heo * worker_leave_idle - leave idle state 984797e8345STejun Heo * @worker: worker which is leaving idle state 985797e8345STejun Heo * 986797e8345STejun Heo * @worker is leaving idle state. Update stats. 987797e8345STejun Heo * 988797e8345STejun Heo * LOCKING: 989797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 990797e8345STejun Heo */ 991797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 992797e8345STejun Heo { 993797e8345STejun Heo struct worker_pool *pool = worker->pool; 994797e8345STejun Heo 995797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 996797e8345STejun Heo return; 997797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 998797e8345STejun Heo pool->nr_idle--; 999797e8345STejun Heo list_del_init(&worker->entry); 1000797e8345STejun Heo } 1001797e8345STejun Heo 1002797e8345STejun Heo /** 1003797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1004797e8345STejun Heo * @pool: pool of interest 1005797e8345STejun Heo * @work: work to find worker for 1006797e8345STejun Heo * 1007797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1008797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1009797e8345STejun Heo * to match, its current execution should match the address of @work and 1010797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1011797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1012797e8345STejun Heo * being executed. 1013797e8345STejun Heo * 1014797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1015797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1016797e8345STejun Heo * another work item. If the same work item address ends up being reused 1017797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1018797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1019797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1020797e8345STejun Heo * 1021797e8345STejun Heo * This function checks the work item address and work function to avoid 1022797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1023797e8345STejun Heo * work function which can introduce dependency onto itself through a 1024797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1025797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1026797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1027797e8345STejun Heo * 1028797e8345STejun Heo * CONTEXT: 1029797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1030797e8345STejun Heo * 1031797e8345STejun Heo * Return: 1032797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1033797e8345STejun Heo * otherwise. 1034797e8345STejun Heo */ 1035797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1036797e8345STejun Heo struct work_struct *work) 1037797e8345STejun Heo { 1038797e8345STejun Heo struct worker *worker; 1039797e8345STejun Heo 1040797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1041797e8345STejun Heo (unsigned long)work) 1042797e8345STejun Heo if (worker->current_work == work && 1043797e8345STejun Heo worker->current_func == work->func) 1044797e8345STejun Heo return worker; 1045797e8345STejun Heo 1046797e8345STejun Heo return NULL; 1047797e8345STejun Heo } 1048797e8345STejun Heo 1049797e8345STejun Heo /** 1050797e8345STejun Heo * move_linked_works - move linked works to a list 1051797e8345STejun Heo * @work: start of series of works to be scheduled 1052797e8345STejun Heo * @head: target list to append @work to 1053797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1054797e8345STejun Heo * 1055873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1056873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1057873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1058873eaca6STejun Heo * @nextp. 1059797e8345STejun Heo * 1060797e8345STejun Heo * CONTEXT: 1061797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1062797e8345STejun Heo */ 1063797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1064797e8345STejun Heo struct work_struct **nextp) 1065797e8345STejun Heo { 1066797e8345STejun Heo struct work_struct *n; 1067797e8345STejun Heo 1068797e8345STejun Heo /* 1069797e8345STejun Heo * Linked worklist will always end before the end of the list, 1070797e8345STejun Heo * use NULL for list head. 1071797e8345STejun Heo */ 1072797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1073797e8345STejun Heo list_move_tail(&work->entry, head); 1074797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1075797e8345STejun Heo break; 1076797e8345STejun Heo } 1077797e8345STejun Heo 1078797e8345STejun Heo /* 1079797e8345STejun Heo * If we're already inside safe list traversal and have moved 1080797e8345STejun Heo * multiple works to the scheduled queue, the next position 1081797e8345STejun Heo * needs to be updated. 1082797e8345STejun Heo */ 1083797e8345STejun Heo if (nextp) 1084797e8345STejun Heo *nextp = n; 1085797e8345STejun Heo } 1086797e8345STejun Heo 1087797e8345STejun Heo /** 1088873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1089873eaca6STejun Heo * @work: work to assign 1090873eaca6STejun Heo * @worker: worker to assign to 1091873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1092873eaca6STejun Heo * 1093873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1094873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1095873eaca6STejun Heo * 1096873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1097873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1098873eaca6STejun Heo * list_for_each_entry_safe(). 1099873eaca6STejun Heo * 1100873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1101873eaca6STejun Heo * was punted to another worker already executing it. 1102873eaca6STejun Heo */ 1103873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1104873eaca6STejun Heo struct work_struct **nextp) 1105873eaca6STejun Heo { 1106873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1107873eaca6STejun Heo struct worker *collision; 1108873eaca6STejun Heo 1109873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1110873eaca6STejun Heo 1111873eaca6STejun Heo /* 1112873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1113873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1114873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1115873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1116873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1117873eaca6STejun Heo * defer the work to the currently executing one. 1118873eaca6STejun Heo */ 1119873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1120873eaca6STejun Heo if (unlikely(collision)) { 1121873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1122873eaca6STejun Heo return false; 1123873eaca6STejun Heo } 1124873eaca6STejun Heo 1125873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1126873eaca6STejun Heo return true; 1127873eaca6STejun Heo } 1128873eaca6STejun Heo 1129873eaca6STejun Heo /** 11300219a352STejun Heo * kick_pool - wake up an idle worker if necessary 11310219a352STejun Heo * @pool: pool to kick 1132797e8345STejun Heo * 11330219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 11340219a352STejun Heo * whether a worker was woken up. 1135797e8345STejun Heo */ 11360219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1137797e8345STejun Heo { 1138797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 11398639ecebSTejun Heo struct task_struct *p; 1140797e8345STejun Heo 11410219a352STejun Heo lockdep_assert_held(&pool->lock); 11420219a352STejun Heo 11430219a352STejun Heo if (!need_more_worker(pool) || !worker) 11440219a352STejun Heo return false; 11450219a352STejun Heo 11468639ecebSTejun Heo p = worker->task; 11478639ecebSTejun Heo 11488639ecebSTejun Heo #ifdef CONFIG_SMP 11498639ecebSTejun Heo /* 11508639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 11518639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 11528639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 11538639ecebSTejun Heo * execution locality. 11548639ecebSTejun Heo * 11558639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 11568639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 11578639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 11588639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 11598639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 11608639ecebSTejun Heo * still on cpu when picking an idle worker. 11618639ecebSTejun Heo * 11628639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 11638639ecebSTejun Heo * its affinity scope. Repatriate. 11648639ecebSTejun Heo */ 11658639ecebSTejun Heo if (!pool->attrs->affn_strict && 11668639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 11678639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 11688639ecebSTejun Heo struct work_struct, entry); 11698639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 11708639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 11718639ecebSTejun Heo } 11728639ecebSTejun Heo #endif 11738639ecebSTejun Heo wake_up_process(p); 11740219a352STejun Heo return true; 1175797e8345STejun Heo } 1176797e8345STejun Heo 117763638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 117863638450STejun Heo 117963638450STejun Heo /* 118063638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 118163638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 118263638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 118363638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 118463638450STejun Heo * should be using an unbound workqueue instead. 118563638450STejun Heo * 118663638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 118763638450STejun Heo * and report them so that they can be examined and converted to use unbound 118863638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 118963638450STejun Heo * function is tracked and reported with exponential backoff. 119063638450STejun Heo */ 119163638450STejun Heo #define WCI_MAX_ENTS 128 119263638450STejun Heo 119363638450STejun Heo struct wci_ent { 119463638450STejun Heo work_func_t func; 119563638450STejun Heo atomic64_t cnt; 119663638450STejun Heo struct hlist_node hash_node; 119763638450STejun Heo }; 119863638450STejun Heo 119963638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 120063638450STejun Heo static int wci_nr_ents; 120163638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 120263638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 120363638450STejun Heo 120463638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 120563638450STejun Heo { 120663638450STejun Heo struct wci_ent *ent; 120763638450STejun Heo 120863638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 120963638450STejun Heo (unsigned long)func) { 121063638450STejun Heo if (ent->func == func) 121163638450STejun Heo return ent; 121263638450STejun Heo } 121363638450STejun Heo return NULL; 121463638450STejun Heo } 121563638450STejun Heo 121663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 121763638450STejun Heo { 121863638450STejun Heo struct wci_ent *ent; 121963638450STejun Heo 122063638450STejun Heo restart: 122163638450STejun Heo ent = wci_find_ent(func); 122263638450STejun Heo if (ent) { 122363638450STejun Heo u64 cnt; 122463638450STejun Heo 122563638450STejun Heo /* 122663638450STejun Heo * Start reporting from the fourth time and back off 122763638450STejun Heo * exponentially. 122863638450STejun Heo */ 122963638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 123063638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 123163638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 123263638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 123363638450STejun Heo atomic64_read(&ent->cnt)); 123463638450STejun Heo return; 123563638450STejun Heo } 123663638450STejun Heo 123763638450STejun Heo /* 123863638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 123963638450STejun Heo * is exhausted, something went really wrong and we probably made enough 124063638450STejun Heo * noise already. 124163638450STejun Heo */ 124263638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 124363638450STejun Heo return; 124463638450STejun Heo 124563638450STejun Heo raw_spin_lock(&wci_lock); 124663638450STejun Heo 124763638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 124863638450STejun Heo raw_spin_unlock(&wci_lock); 124963638450STejun Heo return; 125063638450STejun Heo } 125163638450STejun Heo 125263638450STejun Heo if (wci_find_ent(func)) { 125363638450STejun Heo raw_spin_unlock(&wci_lock); 125463638450STejun Heo goto restart; 125563638450STejun Heo } 125663638450STejun Heo 125763638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 125863638450STejun Heo ent->func = func; 125963638450STejun Heo atomic64_set(&ent->cnt, 1); 126063638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 126163638450STejun Heo 126263638450STejun Heo raw_spin_unlock(&wci_lock); 126363638450STejun Heo } 126463638450STejun Heo 126563638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 126663638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 126763638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 126863638450STejun Heo 1269c54d5046STejun Heo /** 12701da177e4SLinus Torvalds * wq_worker_running - a worker is running again 12711da177e4SLinus Torvalds * @task: task waking up 12721da177e4SLinus Torvalds * 12731da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 12741da177e4SLinus Torvalds */ 12751da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 12761da177e4SLinus Torvalds { 12771da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 12781da177e4SLinus Torvalds 1279c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 12801da177e4SLinus Torvalds return; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds /* 12831da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 12841da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 12851da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 12861da177e4SLinus Torvalds * pool. Protect against such race. 12871da177e4SLinus Torvalds */ 12881da177e4SLinus Torvalds preempt_disable(); 12891da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 12901da177e4SLinus Torvalds worker->pool->nr_running++; 12911da177e4SLinus Torvalds preempt_enable(); 1292616db877STejun Heo 1293616db877STejun Heo /* 1294616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1295616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1296616db877STejun Heo */ 1297616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1298616db877STejun Heo 1299c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds /** 13031da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 13041da177e4SLinus Torvalds * @task: task going to sleep 13051da177e4SLinus Torvalds * 13061da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 13071da177e4SLinus Torvalds * going to sleep. 13081da177e4SLinus Torvalds */ 13091da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 13101da177e4SLinus Torvalds { 13111da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13121da177e4SLinus Torvalds struct worker_pool *pool; 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds /* 13151da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 13161da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 13171da177e4SLinus Torvalds * checking NOT_RUNNING. 13181da177e4SLinus Torvalds */ 13191da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 13201da177e4SLinus Torvalds return; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds pool = worker->pool; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1325c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 13261da177e4SLinus Torvalds return; 13271da177e4SLinus Torvalds 1328c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 13291da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* 13321da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 13331da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 13341da177e4SLinus Torvalds * and nr_running has been reset. 13351da177e4SLinus Torvalds */ 13361da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 13371da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13381da177e4SLinus Torvalds return; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds 13411da177e4SLinus Torvalds pool->nr_running--; 13420219a352STejun Heo if (kick_pool(pool)) 1343725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 13440219a352STejun Heo 13451da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13461da177e4SLinus Torvalds } 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /** 1349616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1350616db877STejun Heo * @task: task currently running 1351616db877STejun Heo * 1352616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1353616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1354616db877STejun Heo */ 1355616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1356616db877STejun Heo { 1357616db877STejun Heo struct worker *worker = kthread_data(task); 1358616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1359616db877STejun Heo struct worker_pool *pool = worker->pool; 1360616db877STejun Heo 1361616db877STejun Heo if (!pwq) 1362616db877STejun Heo return; 1363616db877STejun Heo 13648a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 13658a1dd1e5STejun Heo 136618c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 136718c8ae81SZqiang return; 136818c8ae81SZqiang 1369616db877STejun Heo /* 1370616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1371616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1372616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1373c8f6219bSZqiang * 1374c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1375c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1376c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1377c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1378c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1379c8f6219bSZqiang * We probably want to make this prettier in the future. 1380616db877STejun Heo */ 1381c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1382616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1383616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1384616db877STejun Heo return; 1385616db877STejun Heo 1386616db877STejun Heo raw_spin_lock(&pool->lock); 1387616db877STejun Heo 1388616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 138963638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1390616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1391616db877STejun Heo 13920219a352STejun Heo if (kick_pool(pool)) 1393616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1394616db877STejun Heo 1395616db877STejun Heo raw_spin_unlock(&pool->lock); 1396616db877STejun Heo } 1397616db877STejun Heo 1398616db877STejun Heo /** 13991da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 14001da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 14011da177e4SLinus Torvalds * 14021da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 14031da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 14041da177e4SLinus Torvalds * 14051da177e4SLinus Torvalds * CONTEXT: 14069b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 14071da177e4SLinus Torvalds * 14081da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1409f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1410f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 14111da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 14121da177e4SLinus Torvalds * 14131da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 14141da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 14151da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 14161da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1417365970a1SDavid Howells * 1418365970a1SDavid Howells * Return: 1419365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1420365970a1SDavid Howells * hasn't executed any work yet. 1421365970a1SDavid Howells */ 1422365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1423365970a1SDavid Howells { 1424365970a1SDavid Howells struct worker *worker = kthread_data(task); 1425365970a1SDavid Howells 1426365970a1SDavid Howells return worker->last_func; 1427365970a1SDavid Howells } 1428365970a1SDavid Howells 1429d302f017STejun Heo /** 1430*b522229aSTejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 1431*b522229aSTejun Heo * @wq: workqueue of interest 1432*b522229aSTejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 1433*b522229aSTejun Heo * 1434*b522229aSTejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 1435*b522229aSTejun Heo * 1436*b522229aSTejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 1437*b522229aSTejun Heo * 1438*b522229aSTejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 1439*b522229aSTejun Heo * 1440*b522229aSTejun Heo * - Otherwise, node_nr_active[@node]. 1441*b522229aSTejun Heo */ 1442*b522229aSTejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 1443*b522229aSTejun Heo int node) 1444*b522229aSTejun Heo { 1445*b522229aSTejun Heo if (!(wq->flags & WQ_UNBOUND)) 1446*b522229aSTejun Heo return NULL; 1447*b522229aSTejun Heo 1448*b522229aSTejun Heo if (node == NUMA_NO_NODE) 1449*b522229aSTejun Heo node = nr_node_ids; 1450*b522229aSTejun Heo 1451*b522229aSTejun Heo return wq->node_nr_active[node]; 1452*b522229aSTejun Heo } 1453*b522229aSTejun Heo 1454*b522229aSTejun Heo /** 14558864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 14568864b4e5STejun Heo * @pwq: pool_workqueue to get 14578864b4e5STejun Heo * 14588864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 14598864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 14608864b4e5STejun Heo */ 14618864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 14628864b4e5STejun Heo { 14638864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 14648864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 14658864b4e5STejun Heo pwq->refcnt++; 14668864b4e5STejun Heo } 14678864b4e5STejun Heo 14688864b4e5STejun Heo /** 14698864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 14708864b4e5STejun Heo * @pwq: pool_workqueue to put 14718864b4e5STejun Heo * 14728864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 14738864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 14748864b4e5STejun Heo */ 14758864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 14768864b4e5STejun Heo { 14778864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 14788864b4e5STejun Heo if (likely(--pwq->refcnt)) 14798864b4e5STejun Heo return; 14808864b4e5STejun Heo /* 1481967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1482967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 14838864b4e5STejun Heo */ 1484687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 14858864b4e5STejun Heo } 14868864b4e5STejun Heo 1487dce90d47STejun Heo /** 1488dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1489dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1490dce90d47STejun Heo * 1491dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1492dce90d47STejun Heo */ 1493dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1494dce90d47STejun Heo { 1495dce90d47STejun Heo if (pwq) { 1496dce90d47STejun Heo /* 149724acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1498dce90d47STejun Heo * following lock operations are safe. 1499dce90d47STejun Heo */ 1500a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1501dce90d47STejun Heo put_pwq(pwq); 1502a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1503dce90d47STejun Heo } 1504dce90d47STejun Heo } 1505dce90d47STejun Heo 1506bad184d2STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1507bad184d2STejun Heo { 1508bad184d2STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1509bad184d2STejun Heo } 1510bad184d2STejun Heo 15116c592f0bSTejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 15126c592f0bSTejun Heo struct work_struct *work) 1513bf4ede01STejun Heo { 15144023a2d9STejun Heo unsigned long *wdb = work_data_bits(work); 15154023a2d9STejun Heo 15164023a2d9STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1517bf4ede01STejun Heo trace_workqueue_activate_work(work); 151882607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 151982607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1520112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 15214023a2d9STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 15226c592f0bSTejun Heo } 15236c592f0bSTejun Heo 15246c592f0bSTejun Heo /** 15256c592f0bSTejun Heo * pwq_activate_work - Activate a work item if inactive 15266c592f0bSTejun Heo * @pwq: pool_workqueue @work belongs to 15276c592f0bSTejun Heo * @work: work item to activate 15286c592f0bSTejun Heo * 15296c592f0bSTejun Heo * Returns %true if activated. %false if already active. 15306c592f0bSTejun Heo */ 15316c592f0bSTejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 15326c592f0bSTejun Heo struct work_struct *work) 15336c592f0bSTejun Heo { 15346c592f0bSTejun Heo struct worker_pool *pool = pwq->pool; 1535*b522229aSTejun Heo struct wq_node_nr_active *nna; 15366c592f0bSTejun Heo 15376c592f0bSTejun Heo lockdep_assert_held(&pool->lock); 15386c592f0bSTejun Heo 15396c592f0bSTejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 15406c592f0bSTejun Heo return false; 15416c592f0bSTejun Heo 1542*b522229aSTejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 1543*b522229aSTejun Heo if (nna) 1544*b522229aSTejun Heo atomic_inc(&nna->nr); 1545*b522229aSTejun Heo 1546112202d9STejun Heo pwq->nr_active++; 15476c592f0bSTejun Heo __pwq_activate_work(pwq, work); 15486c592f0bSTejun Heo return true; 1549bf4ede01STejun Heo } 1550bf4ede01STejun Heo 15514023a2d9STejun Heo /** 15524023a2d9STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 15534023a2d9STejun Heo * @pwq: pool_workqueue of interest 15544023a2d9STejun Heo * 15554023a2d9STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 15564023a2d9STejun Heo * successfully obtained. %false otherwise. 15574023a2d9STejun Heo */ 15584023a2d9STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq) 15593aa62497SLai Jiangshan { 15604023a2d9STejun Heo struct workqueue_struct *wq = pwq->wq; 15614023a2d9STejun Heo struct worker_pool *pool = pwq->pool; 1562*b522229aSTejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 15634023a2d9STejun Heo bool obtained; 15644023a2d9STejun Heo 15654023a2d9STejun Heo lockdep_assert_held(&pool->lock); 15664023a2d9STejun Heo 15674023a2d9STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 15684023a2d9STejun Heo 1569*b522229aSTejun Heo if (obtained) { 15704023a2d9STejun Heo pwq->nr_active++; 1571*b522229aSTejun Heo if (nna) 1572*b522229aSTejun Heo atomic_inc(&nna->nr); 1573*b522229aSTejun Heo } 15744023a2d9STejun Heo return obtained; 15754023a2d9STejun Heo } 15764023a2d9STejun Heo 15774023a2d9STejun Heo /** 15784023a2d9STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 15794023a2d9STejun Heo * @pwq: pool_workqueue of interest 15804023a2d9STejun Heo * 15814023a2d9STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 15824023a2d9STejun Heo * max_active limit. 15834023a2d9STejun Heo * 15844023a2d9STejun Heo * Returns %true if an inactive work item has been activated. %false if no 15854023a2d9STejun Heo * inactive work item is found or max_active limit is reached. 15864023a2d9STejun Heo */ 15874023a2d9STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq) 15884023a2d9STejun Heo { 15894023a2d9STejun Heo struct work_struct *work = 15904023a2d9STejun Heo list_first_entry_or_null(&pwq->inactive_works, 15913aa62497SLai Jiangshan struct work_struct, entry); 15923aa62497SLai Jiangshan 15934023a2d9STejun Heo if (work && pwq_tryinc_nr_active(pwq)) { 15944023a2d9STejun Heo __pwq_activate_work(pwq, work); 15954023a2d9STejun Heo return true; 15964023a2d9STejun Heo } else { 15974023a2d9STejun Heo return false; 15984023a2d9STejun Heo } 15994023a2d9STejun Heo } 16004023a2d9STejun Heo 16014023a2d9STejun Heo /** 16024023a2d9STejun Heo * pwq_dec_nr_active - Retire an active count 16034023a2d9STejun Heo * @pwq: pool_workqueue of interest 16044023a2d9STejun Heo * 16054023a2d9STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 16064023a2d9STejun Heo */ 16074023a2d9STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 16084023a2d9STejun Heo { 16094023a2d9STejun Heo struct worker_pool *pool = pwq->pool; 1610*b522229aSTejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 16114023a2d9STejun Heo 16124023a2d9STejun Heo lockdep_assert_held(&pool->lock); 16134023a2d9STejun Heo 1614*b522229aSTejun Heo /* 1615*b522229aSTejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 1616*b522229aSTejun Heo * workqueues. 1617*b522229aSTejun Heo */ 16184023a2d9STejun Heo pwq->nr_active--; 1619*b522229aSTejun Heo 1620*b522229aSTejun Heo /* 1621*b522229aSTejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 1622*b522229aSTejun Heo * inactive work item on @pwq itself. 1623*b522229aSTejun Heo */ 1624*b522229aSTejun Heo if (!nna) { 1625*b522229aSTejun Heo pwq_activate_first_inactive(pwq); 1626*b522229aSTejun Heo return; 1627*b522229aSTejun Heo } 1628*b522229aSTejun Heo 1629*b522229aSTejun Heo atomic_dec(&nna->nr); 16304023a2d9STejun Heo pwq_activate_first_inactive(pwq); 16313aa62497SLai Jiangshan } 16323aa62497SLai Jiangshan 1633bf4ede01STejun Heo /** 1634112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1635112202d9STejun Heo * @pwq: pwq of interest 1636c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1637bf4ede01STejun Heo * 1638bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1639112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1640bf4ede01STejun Heo * 1641bf4ede01STejun Heo * CONTEXT: 1642a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1643bf4ede01STejun Heo */ 1644c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1645bf4ede01STejun Heo { 1646c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1647c4560c2cSLai Jiangshan 16484023a2d9STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 16494023a2d9STejun Heo pwq_dec_nr_active(pwq); 1650018f3a13SLai Jiangshan 1651018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1652bf4ede01STejun Heo 1653bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1654112202d9STejun Heo if (likely(pwq->flush_color != color)) 16558864b4e5STejun Heo goto out_put; 1656bf4ede01STejun Heo 1657bf4ede01STejun Heo /* are there still in-flight works? */ 1658112202d9STejun Heo if (pwq->nr_in_flight[color]) 16598864b4e5STejun Heo goto out_put; 1660bf4ede01STejun Heo 1661112202d9STejun Heo /* this pwq is done, clear flush_color */ 1662112202d9STejun Heo pwq->flush_color = -1; 1663bf4ede01STejun Heo 1664bf4ede01STejun Heo /* 1665112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1666bf4ede01STejun Heo * will handle the rest. 1667bf4ede01STejun Heo */ 1668112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1669112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 16708864b4e5STejun Heo out_put: 16718864b4e5STejun Heo put_pwq(pwq); 1672bf4ede01STejun Heo } 1673bf4ede01STejun Heo 167436e227d2STejun Heo /** 1675bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 167636e227d2STejun Heo * @work: work item to steal 167736e227d2STejun Heo * @is_dwork: @work is a delayed_work 1678bbb68dfaSTejun Heo * @flags: place to store irq state 167936e227d2STejun Heo * 168036e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1681d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 168236e227d2STejun Heo * 1683d185af30SYacine Belkadi * Return: 16843eb6b31bSMauro Carvalho Chehab * 16853eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 168636e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 168736e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 168836e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1689bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1690bbb68dfaSTejun Heo * for arbitrarily long 16913eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 169236e227d2STejun Heo * 1693d185af30SYacine Belkadi * Note: 1694bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1695e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1696e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1697e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1698bbb68dfaSTejun Heo * 1699bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1700bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1701bbb68dfaSTejun Heo * 1702e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1703bf4ede01STejun Heo */ 1704bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1705bbb68dfaSTejun Heo unsigned long *flags) 1706bf4ede01STejun Heo { 1707d565ed63STejun Heo struct worker_pool *pool; 1708112202d9STejun Heo struct pool_workqueue *pwq; 1709bf4ede01STejun Heo 1710bbb68dfaSTejun Heo local_irq_save(*flags); 1711bbb68dfaSTejun Heo 171236e227d2STejun Heo /* try to steal the timer if it exists */ 171336e227d2STejun Heo if (is_dwork) { 171436e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 171536e227d2STejun Heo 1716e0aecdd8STejun Heo /* 1717e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1718e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1719e0aecdd8STejun Heo * running on the local CPU. 1720e0aecdd8STejun Heo */ 172136e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 172236e227d2STejun Heo return 1; 172336e227d2STejun Heo } 172436e227d2STejun Heo 172536e227d2STejun Heo /* try to claim PENDING the normal way */ 1726bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1727bf4ede01STejun Heo return 0; 1728bf4ede01STejun Heo 172924acfb71SThomas Gleixner rcu_read_lock(); 1730bf4ede01STejun Heo /* 1731bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1732bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1733bf4ede01STejun Heo */ 1734d565ed63STejun Heo pool = get_work_pool(work); 1735d565ed63STejun Heo if (!pool) 1736bbb68dfaSTejun Heo goto fail; 1737bf4ede01STejun Heo 1738a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1739bf4ede01STejun Heo /* 1740112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1741112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1742112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1743112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1744112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 17450b3dae68SLai Jiangshan * item is currently queued on that pool. 1746bf4ede01STejun Heo */ 1747112202d9STejun Heo pwq = get_work_pwq(work); 1748112202d9STejun Heo if (pwq && pwq->pool == pool) { 1749bf4ede01STejun Heo debug_work_deactivate(work); 17503aa62497SLai Jiangshan 17513aa62497SLai Jiangshan /* 1752018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 1753018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 1754018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 1755018f3a13SLai Jiangshan * 1756f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 1757d812796eSLai Jiangshan * it might have linked barrier work items which, if left 1758f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 175916062836STejun Heo * management later on and cause stall. Make sure the work 176016062836STejun Heo * item is activated before grabbing. 17613aa62497SLai Jiangshan */ 17626c592f0bSTejun Heo pwq_activate_work(pwq, work); 17633aa62497SLai Jiangshan 1764bf4ede01STejun Heo list_del_init(&work->entry); 1765c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 176636e227d2STejun Heo 1767112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 17684468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 17694468a00fSLai Jiangshan 1770a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 177124acfb71SThomas Gleixner rcu_read_unlock(); 177236e227d2STejun Heo return 1; 1773bf4ede01STejun Heo } 1774a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 1775bbb68dfaSTejun Heo fail: 177624acfb71SThomas Gleixner rcu_read_unlock(); 1777bbb68dfaSTejun Heo local_irq_restore(*flags); 1778bbb68dfaSTejun Heo if (work_is_canceling(work)) 1779bbb68dfaSTejun Heo return -ENOENT; 1780bbb68dfaSTejun Heo cpu_relax(); 178136e227d2STejun Heo return -EAGAIN; 1782bf4ede01STejun Heo } 1783bf4ede01STejun Heo 1784bf4ede01STejun Heo /** 1785706026c2STejun Heo * insert_work - insert a work into a pool 1786112202d9STejun Heo * @pwq: pwq @work belongs to 17874690c4abSTejun Heo * @work: work to insert 17884690c4abSTejun Heo * @head: insertion point 17894690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 17904690c4abSTejun Heo * 1791112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 1792706026c2STejun Heo * work_struct flags. 17934690c4abSTejun Heo * 17944690c4abSTejun Heo * CONTEXT: 1795a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1796365970a1SDavid Howells */ 1797112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 1798112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 1799b89deed3SOleg Nesterov { 1800fe089f87STejun Heo debug_work_activate(work); 1801e1d8aa9fSFrederic Weisbecker 1802e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 1803f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 1804e89a85d6SWalter Wu 18054690c4abSTejun Heo /* we own @work, set data and link */ 1806112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 18071a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 18088864b4e5STejun Heo get_pwq(pwq); 1809b89deed3SOleg Nesterov } 1810b89deed3SOleg Nesterov 1811c8efcc25STejun Heo /* 1812c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 18138d03ecfeSTejun Heo * same workqueue. 1814c8efcc25STejun Heo */ 1815c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 1816c8efcc25STejun Heo { 1817c8efcc25STejun Heo struct worker *worker; 1818c8efcc25STejun Heo 18198d03ecfeSTejun Heo worker = current_wq_worker(); 1820c8efcc25STejun Heo /* 1821bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 18228d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 1823c8efcc25STejun Heo */ 1824112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 1825c8efcc25STejun Heo } 1826c8efcc25STejun Heo 1827ef557180SMike Galbraith /* 1828ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 1829ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 1830ef557180SMike Galbraith * avoid perturbing sensitive tasks. 1831ef557180SMike Galbraith */ 1832ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 1833ef557180SMike Galbraith { 1834ef557180SMike Galbraith int new_cpu; 1835ef557180SMike Galbraith 1836f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 1837ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 1838ef557180SMike Galbraith return cpu; 1839a8ec5880SAmmar Faizi } else { 1840a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 1841f303fccbSTejun Heo } 1842f303fccbSTejun Heo 1843ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 1844ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 1845ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 1846ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 1847ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 1848ef557180SMike Galbraith return cpu; 1849ef557180SMike Galbraith } 1850ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 1851ef557180SMike Galbraith 1852ef557180SMike Galbraith return new_cpu; 1853ef557180SMike Galbraith } 1854ef557180SMike Galbraith 1855d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 18561da177e4SLinus Torvalds struct work_struct *work) 18571da177e4SLinus Torvalds { 1858112202d9STejun Heo struct pool_workqueue *pwq; 1859fe089f87STejun Heo struct worker_pool *last_pool, *pool; 18608a2e8e5dSTejun Heo unsigned int work_flags; 1861b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 18628930cabaSTejun Heo 18638930cabaSTejun Heo /* 18648930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 18658930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 18668930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 18678930cabaSTejun Heo * happen with IRQ disabled. 18688930cabaSTejun Heo */ 18698e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 18701da177e4SLinus Torvalds 18711e19ffc6STejun Heo 187233e3f0a3SRichard Clark /* 187333e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 187433e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 187533e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 187633e3f0a3SRichard Clark */ 187733e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 187833e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 1879e41e704bSTejun Heo return; 188024acfb71SThomas Gleixner rcu_read_lock(); 18819e8cd2f5STejun Heo retry: 1882aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 1883636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 1884636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 1885ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 1886636b927eSTejun Heo else 1887aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 1888aa202f1fSHillf Danton } 1889f3421797STejun Heo 1890636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 1891fe089f87STejun Heo pool = pwq->pool; 1892fe089f87STejun Heo 189318aa9effSTejun Heo /* 1894c9178087STejun Heo * If @work was previously on a different pool, it might still be 1895c9178087STejun Heo * running there, in which case the work needs to be queued on that 1896c9178087STejun Heo * pool to guarantee non-reentrancy. 189718aa9effSTejun Heo */ 1898c9e7cf27STejun Heo last_pool = get_work_pool(work); 1899fe089f87STejun Heo if (last_pool && last_pool != pool) { 190018aa9effSTejun Heo struct worker *worker; 190118aa9effSTejun Heo 1902a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 190318aa9effSTejun Heo 1904c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 190518aa9effSTejun Heo 1906112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 1907c9178087STejun Heo pwq = worker->current_pwq; 1908fe089f87STejun Heo pool = pwq->pool; 1909fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 19108594fadeSLai Jiangshan } else { 191118aa9effSTejun Heo /* meh... not running there, queue here */ 1912a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 1913fe089f87STejun Heo raw_spin_lock(&pool->lock); 191418aa9effSTejun Heo } 19158930cabaSTejun Heo } else { 1916fe089f87STejun Heo raw_spin_lock(&pool->lock); 19178930cabaSTejun Heo } 1918502ca9d8STejun Heo 19199e8cd2f5STejun Heo /* 1920636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 1921636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 1922636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 1923636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 1924636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 19259e8cd2f5STejun Heo */ 19269e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 19279e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 1928fe089f87STejun Heo raw_spin_unlock(&pool->lock); 19299e8cd2f5STejun Heo cpu_relax(); 19309e8cd2f5STejun Heo goto retry; 19319e8cd2f5STejun Heo } 19329e8cd2f5STejun Heo /* oops */ 19339e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 19349e8cd2f5STejun Heo wq->name, cpu); 19359e8cd2f5STejun Heo } 19369e8cd2f5STejun Heo 1937112202d9STejun Heo /* pwq determined, queue */ 1938112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 1939502ca9d8STejun Heo 194024acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 194124acfb71SThomas Gleixner goto out; 19421e19ffc6STejun Heo 1943112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 1944112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 19451e19ffc6STejun Heo 194682e098f5STejun Heo /* 194782e098f5STejun Heo * Limit the number of concurrently active work items to max_active. 194882e098f5STejun Heo * @work must also queue behind existing inactive work items to maintain 194982e098f5STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 195082e098f5STejun Heo */ 19514023a2d9STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq)) { 1952fe089f87STejun Heo if (list_empty(&pool->worklist)) 1953fe089f87STejun Heo pool->watchdog_ts = jiffies; 1954fe089f87STejun Heo 1955cdadf009STejun Heo trace_workqueue_activate_work(work); 1956fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 19570219a352STejun Heo kick_pool(pool); 19588a2e8e5dSTejun Heo } else { 1959f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 1960fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 19618a2e8e5dSTejun Heo } 19621e19ffc6STejun Heo 196324acfb71SThomas Gleixner out: 1964fe089f87STejun Heo raw_spin_unlock(&pool->lock); 196524acfb71SThomas Gleixner rcu_read_unlock(); 19661da177e4SLinus Torvalds } 19671da177e4SLinus Torvalds 19680fcb78c2SRolf Eike Beer /** 1969c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 1970c1a220e7SZhang Rui * @cpu: CPU number to execute work on 1971c1a220e7SZhang Rui * @wq: workqueue to use 1972c1a220e7SZhang Rui * @work: work to queue 1973c1a220e7SZhang Rui * 1974c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 1975443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 1976443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 1977854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 1978854f5cc5SPaul E. McKenney * online will get a splat. 1979d185af30SYacine Belkadi * 1980d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 1981c1a220e7SZhang Rui */ 1982d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 1983d4283e93STejun Heo struct work_struct *work) 1984c1a220e7SZhang Rui { 1985d4283e93STejun Heo bool ret = false; 19868930cabaSTejun Heo unsigned long flags; 19878930cabaSTejun Heo 19888930cabaSTejun Heo local_irq_save(flags); 1989c1a220e7SZhang Rui 199022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 19914690c4abSTejun Heo __queue_work(cpu, wq, work); 1992d4283e93STejun Heo ret = true; 1993c1a220e7SZhang Rui } 19948930cabaSTejun Heo 19958930cabaSTejun Heo local_irq_restore(flags); 1996c1a220e7SZhang Rui return ret; 1997c1a220e7SZhang Rui } 1998ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 1999c1a220e7SZhang Rui 20008204e0c1SAlexander Duyck /** 2001fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 20028204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 20038204e0c1SAlexander Duyck * 20048204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 20058204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 20068204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 20078204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 20088204e0c1SAlexander Duyck */ 2009fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 20108204e0c1SAlexander Duyck { 20118204e0c1SAlexander Duyck int cpu; 20128204e0c1SAlexander Duyck 20138204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 20148204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 20158204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 20168204e0c1SAlexander Duyck 20178204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 20188204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 20198204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 20208204e0c1SAlexander Duyck return cpu; 20218204e0c1SAlexander Duyck 20228204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 20238204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 20248204e0c1SAlexander Duyck 20258204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 20268204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 20278204e0c1SAlexander Duyck } 20288204e0c1SAlexander Duyck 20298204e0c1SAlexander Duyck /** 20308204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 20318204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 20328204e0c1SAlexander Duyck * @wq: workqueue to use 20338204e0c1SAlexander Duyck * @work: work to queue 20348204e0c1SAlexander Duyck * 20358204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 20368204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 20378204e0c1SAlexander Duyck * NUMA node. 20388204e0c1SAlexander Duyck * 20398204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 20408204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 20418204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 20428204e0c1SAlexander Duyck * 20438204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 20448204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 20458204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 20468204e0c1SAlexander Duyck * 20478204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 20488204e0c1SAlexander Duyck */ 20498204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 20508204e0c1SAlexander Duyck struct work_struct *work) 20518204e0c1SAlexander Duyck { 20528204e0c1SAlexander Duyck unsigned long flags; 20538204e0c1SAlexander Duyck bool ret = false; 20548204e0c1SAlexander Duyck 20558204e0c1SAlexander Duyck /* 20568204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 20578204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 20588204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 20598204e0c1SAlexander Duyck * 20608204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 20618204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 20628204e0c1SAlexander Duyck * some round robin type logic. 20638204e0c1SAlexander Duyck */ 20648204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 20658204e0c1SAlexander Duyck 20668204e0c1SAlexander Duyck local_irq_save(flags); 20678204e0c1SAlexander Duyck 20688204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2069fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 20708204e0c1SAlexander Duyck 20718204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 20728204e0c1SAlexander Duyck ret = true; 20738204e0c1SAlexander Duyck } 20748204e0c1SAlexander Duyck 20758204e0c1SAlexander Duyck local_irq_restore(flags); 20768204e0c1SAlexander Duyck return ret; 20778204e0c1SAlexander Duyck } 20788204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 20798204e0c1SAlexander Duyck 20808c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 20811da177e4SLinus Torvalds { 20828c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 20831da177e4SLinus Torvalds 2084e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 208560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 20861da177e4SLinus Torvalds } 20871438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 20881da177e4SLinus Torvalds 20897beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 209052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 20911da177e4SLinus Torvalds { 20927beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 20937beb2edfSTejun Heo struct work_struct *work = &dwork->work; 20941da177e4SLinus Torvalds 2095637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 20964b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2097fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2098fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 20997beb2edfSTejun Heo 21008852aac2STejun Heo /* 21018852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 21028852aac2STejun Heo * both optimization and correctness. The earliest @timer can 21038852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 21048852aac2STejun Heo * on that there's no such delay when @delay is 0. 21058852aac2STejun Heo */ 21068852aac2STejun Heo if (!delay) { 21078852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 21088852aac2STejun Heo return; 21098852aac2STejun Heo } 21108852aac2STejun Heo 211160c057bcSLai Jiangshan dwork->wq = wq; 21121265057fSTejun Heo dwork->cpu = cpu; 21137beb2edfSTejun Heo timer->expires = jiffies + delay; 21147beb2edfSTejun Heo 2115041bd12eSTejun Heo if (unlikely(cpu != WORK_CPU_UNBOUND)) 21167beb2edfSTejun Heo add_timer_on(timer, cpu); 2117041bd12eSTejun Heo else 2118041bd12eSTejun Heo add_timer(timer); 21197beb2edfSTejun Heo } 21201da177e4SLinus Torvalds 21210fcb78c2SRolf Eike Beer /** 21220fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 21230fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 21240fcb78c2SRolf Eike Beer * @wq: workqueue to use 2125af9997e4SRandy Dunlap * @dwork: work to queue 21260fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 21270fcb78c2SRolf Eike Beer * 2128d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2129715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2130715f1300STejun Heo * execution. 21310fcb78c2SRolf Eike Beer */ 2132d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 213352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 21347a6bc1cdSVenkatesh Pallipadi { 213552bad64dSDavid Howells struct work_struct *work = &dwork->work; 2136d4283e93STejun Heo bool ret = false; 21378930cabaSTejun Heo unsigned long flags; 21388930cabaSTejun Heo 21398930cabaSTejun Heo /* read the comment in __queue_work() */ 21408930cabaSTejun Heo local_irq_save(flags); 21417a6bc1cdSVenkatesh Pallipadi 214222df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 21437beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2144d4283e93STejun Heo ret = true; 21457a6bc1cdSVenkatesh Pallipadi } 21468930cabaSTejun Heo 21478930cabaSTejun Heo local_irq_restore(flags); 21487a6bc1cdSVenkatesh Pallipadi return ret; 21497a6bc1cdSVenkatesh Pallipadi } 2150ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 21511da177e4SLinus Torvalds 2152c8e55f36STejun Heo /** 21538376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 21548376fe22STejun Heo * @cpu: CPU number to execute work on 21558376fe22STejun Heo * @wq: workqueue to use 21568376fe22STejun Heo * @dwork: work to queue 21578376fe22STejun Heo * @delay: number of jiffies to wait before queueing 21588376fe22STejun Heo * 21598376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 21608376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 21618376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 21628376fe22STejun Heo * current state. 21638376fe22STejun Heo * 2164d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 21658376fe22STejun Heo * pending and its timer was modified. 21668376fe22STejun Heo * 2167e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 21688376fe22STejun Heo * See try_to_grab_pending() for details. 21698376fe22STejun Heo */ 21708376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 21718376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 21728376fe22STejun Heo { 21738376fe22STejun Heo unsigned long flags; 21748376fe22STejun Heo int ret; 21758376fe22STejun Heo 21768376fe22STejun Heo do { 21778376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 21788376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 21798376fe22STejun Heo 21808376fe22STejun Heo if (likely(ret >= 0)) { 21818376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 21828376fe22STejun Heo local_irq_restore(flags); 21838376fe22STejun Heo } 21848376fe22STejun Heo 21858376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 21868376fe22STejun Heo return ret; 21878376fe22STejun Heo } 21888376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 21898376fe22STejun Heo 219005f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 219105f0fe6bSTejun Heo { 219205f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 219305f0fe6bSTejun Heo 219405f0fe6bSTejun Heo /* read the comment in __queue_work() */ 219505f0fe6bSTejun Heo local_irq_disable(); 219605f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 219705f0fe6bSTejun Heo local_irq_enable(); 219805f0fe6bSTejun Heo } 219905f0fe6bSTejun Heo 220005f0fe6bSTejun Heo /** 220105f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 220205f0fe6bSTejun Heo * @wq: workqueue to use 220305f0fe6bSTejun Heo * @rwork: work to queue 220405f0fe6bSTejun Heo * 220505f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 220605f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2207bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 220805f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 220905f0fe6bSTejun Heo */ 221005f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 221105f0fe6bSTejun Heo { 221205f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 221305f0fe6bSTejun Heo 221405f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 221505f0fe6bSTejun Heo rwork->wq = wq; 2216a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 221705f0fe6bSTejun Heo return true; 221805f0fe6bSTejun Heo } 221905f0fe6bSTejun Heo 222005f0fe6bSTejun Heo return false; 222105f0fe6bSTejun Heo } 222205f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 222305f0fe6bSTejun Heo 2224f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2225c34056a3STejun Heo { 2226c34056a3STejun Heo struct worker *worker; 2227c34056a3STejun Heo 2228f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2229c8e55f36STejun Heo if (worker) { 2230c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2231affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2232da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2233e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2234e22bee78STejun Heo worker->flags = WORKER_PREP; 2235c8e55f36STejun Heo } 2236c34056a3STejun Heo return worker; 2237c34056a3STejun Heo } 2238c34056a3STejun Heo 22399546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 22409546b29eSTejun Heo { 22418639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 22429546b29eSTejun Heo return pool->attrs->__pod_cpumask; 22438639ecebSTejun Heo else 22448639ecebSTejun Heo return pool->attrs->cpumask; 22459546b29eSTejun Heo } 22469546b29eSTejun Heo 2247c34056a3STejun Heo /** 22484736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 22494736cbf7SLai Jiangshan * @worker: worker to be attached 22504736cbf7SLai Jiangshan * @pool: the target pool 22514736cbf7SLai Jiangshan * 22524736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 22534736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 22544736cbf7SLai Jiangshan * cpu-[un]hotplugs. 22554736cbf7SLai Jiangshan */ 22564736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 22574736cbf7SLai Jiangshan struct worker_pool *pool) 22584736cbf7SLai Jiangshan { 22591258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 22604736cbf7SLai Jiangshan 22614736cbf7SLai Jiangshan /* 22621258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 22631258fae7STejun Heo * stable across this function. See the comments above the flag 22641258fae7STejun Heo * definition for details. 22654736cbf7SLai Jiangshan */ 22664736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 22674736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 22685c25b5ffSPeter Zijlstra else 22695c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 22704736cbf7SLai Jiangshan 2271640f17c8SPeter Zijlstra if (worker->rescue_wq) 22729546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2273640f17c8SPeter Zijlstra 22744736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2275a2d812a2STejun Heo worker->pool = pool; 22764736cbf7SLai Jiangshan 22771258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 22784736cbf7SLai Jiangshan } 22794736cbf7SLai Jiangshan 22804736cbf7SLai Jiangshan /** 228160f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 228260f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 228360f5a4bcSLai Jiangshan * 22844736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 22854736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 22864736cbf7SLai Jiangshan * other reference to the pool. 228760f5a4bcSLai Jiangshan */ 2288a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 228960f5a4bcSLai Jiangshan { 2290a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 229160f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 229260f5a4bcSLai Jiangshan 22931258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2294a2d812a2STejun Heo 22955c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2296da028469SLai Jiangshan list_del(&worker->node); 2297a2d812a2STejun Heo worker->pool = NULL; 2298a2d812a2STejun Heo 2299e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 230060f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 23011258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 230260f5a4bcSLai Jiangshan 2303b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2304b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2305b62c0751SLai Jiangshan 230660f5a4bcSLai Jiangshan if (detach_completion) 230760f5a4bcSLai Jiangshan complete(detach_completion); 230860f5a4bcSLai Jiangshan } 230960f5a4bcSLai Jiangshan 231060f5a4bcSLai Jiangshan /** 2311c34056a3STejun Heo * create_worker - create a new workqueue worker 231263d95a91STejun Heo * @pool: pool the new worker will belong to 2313c34056a3STejun Heo * 2314051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2315c34056a3STejun Heo * 2316c34056a3STejun Heo * CONTEXT: 2317c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2318c34056a3STejun Heo * 2319d185af30SYacine Belkadi * Return: 2320c34056a3STejun Heo * Pointer to the newly created worker. 2321c34056a3STejun Heo */ 2322bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2323c34056a3STejun Heo { 2324e441b56fSZhen Lei struct worker *worker; 2325e441b56fSZhen Lei int id; 23265d9c7a1eSLucy Mielke char id_buf[23]; 2327c34056a3STejun Heo 23287cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2329e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 23303f0ea0b8SPetr Mladek if (id < 0) { 23313f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 23323f0ea0b8SPetr Mladek ERR_PTR(id)); 2333e441b56fSZhen Lei return NULL; 23343f0ea0b8SPetr Mladek } 2335c34056a3STejun Heo 2336f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 23373f0ea0b8SPetr Mladek if (!worker) { 23383f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2339c34056a3STejun Heo goto fail; 23403f0ea0b8SPetr Mladek } 2341c34056a3STejun Heo 2342c34056a3STejun Heo worker->id = id; 2343c34056a3STejun Heo 234429c91e99STejun Heo if (pool->cpu >= 0) 2345e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2346e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2347f3421797STejun Heo else 2348e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2349e3c916a4STejun Heo 2350f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2351e3c916a4STejun Heo "kworker/%s", id_buf); 23523f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 235360f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 235460f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 235560f54038SPetr Mladek id_buf); 235660f54038SPetr Mladek } else { 23573f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 23583f0ea0b8SPetr Mladek worker->task); 235960f54038SPetr Mladek } 2360c34056a3STejun Heo goto fail; 23613f0ea0b8SPetr Mladek } 2362c34056a3STejun Heo 236391151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 23649546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 236591151228SOleg Nesterov 2366da028469SLai Jiangshan /* successful, attach the worker to the pool */ 23674736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2368822d8405STejun Heo 2369051e1850SLai Jiangshan /* start the newly created worker */ 2370a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 23710219a352STejun Heo 2372051e1850SLai Jiangshan worker->pool->nr_workers++; 2373051e1850SLai Jiangshan worker_enter_idle(worker); 23740219a352STejun Heo kick_pool(pool); 23750219a352STejun Heo 23760219a352STejun Heo /* 23770219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 23780219a352STejun Heo * check if not woken up soon. As kick_pool() might not have waken it 23790219a352STejun Heo * up, wake it up explicitly once more. 23800219a352STejun Heo */ 2381051e1850SLai Jiangshan wake_up_process(worker->task); 23820219a352STejun Heo 2383a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2384051e1850SLai Jiangshan 2385c34056a3STejun Heo return worker; 2386822d8405STejun Heo 2387c34056a3STejun Heo fail: 2388e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2389c34056a3STejun Heo kfree(worker); 2390c34056a3STejun Heo return NULL; 2391c34056a3STejun Heo } 2392c34056a3STejun Heo 2393793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2394793777bcSValentin Schneider { 2395793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2396793777bcSValentin Schneider 2397793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2398793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2399793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2400793777bcSValentin Schneider else 2401793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2402793777bcSValentin Schneider } 2403793777bcSValentin Schneider 2404e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2405e02b9312SValentin Schneider { 2406e02b9312SValentin Schneider struct worker *worker, *tmp; 2407e02b9312SValentin Schneider 2408e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2409e02b9312SValentin Schneider list_del_init(&worker->entry); 2410e02b9312SValentin Schneider unbind_worker(worker); 2411e02b9312SValentin Schneider /* 2412e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2413e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2414e02b9312SValentin Schneider * wouldn't have gotten here. 2415c34056a3STejun Heo * 2416e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2417e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2418e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2419e02b9312SValentin Schneider * outside of pool->lock. 2420e02b9312SValentin Schneider */ 2421e02b9312SValentin Schneider wake_up_process(worker->task); 2422e02b9312SValentin Schneider } 2423e02b9312SValentin Schneider } 2424e02b9312SValentin Schneider 2425e02b9312SValentin Schneider /** 2426e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2427e02b9312SValentin Schneider * @worker: worker to be destroyed 2428e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2429e02b9312SValentin Schneider * 2430e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2431e02b9312SValentin Schneider * should be idle. 2432c8e55f36STejun Heo * 2433c8e55f36STejun Heo * CONTEXT: 2434a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2435c34056a3STejun Heo */ 2436e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2437c34056a3STejun Heo { 2438bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2439c34056a3STejun Heo 2440cd549687STejun Heo lockdep_assert_held(&pool->lock); 2441e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2442cd549687STejun Heo 2443c34056a3STejun Heo /* sanity check frenzy */ 24446183c009STejun Heo if (WARN_ON(worker->current_work) || 244573eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 244673eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 24476183c009STejun Heo return; 2448c34056a3STejun Heo 2449bd7bdd43STejun Heo pool->nr_workers--; 2450bd7bdd43STejun Heo pool->nr_idle--; 2451c8e55f36STejun Heo 2452cb444766STejun Heo worker->flags |= WORKER_DIE; 2453e02b9312SValentin Schneider 2454e02b9312SValentin Schneider list_move(&worker->entry, list); 2455e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2456c34056a3STejun Heo } 2457c34056a3STejun Heo 24583f959aa3SValentin Schneider /** 24593f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 24603f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 24613f959aa3SValentin Schneider * 24623f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 24633f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 24643f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 24653f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 24663f959aa3SValentin Schneider * it expire and re-evaluate things from there. 24673f959aa3SValentin Schneider */ 246832a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2469e22bee78STejun Heo { 247032a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 24713f959aa3SValentin Schneider bool do_cull = false; 24723f959aa3SValentin Schneider 24733f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 24743f959aa3SValentin Schneider return; 24753f959aa3SValentin Schneider 24763f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 24773f959aa3SValentin Schneider 24783f959aa3SValentin Schneider if (too_many_workers(pool)) { 24793f959aa3SValentin Schneider struct worker *worker; 24803f959aa3SValentin Schneider unsigned long expires; 24813f959aa3SValentin Schneider 24823f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 24833f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 24843f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 24853f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 24863f959aa3SValentin Schneider 24873f959aa3SValentin Schneider if (!do_cull) 24883f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 24893f959aa3SValentin Schneider } 24903f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 24913f959aa3SValentin Schneider 24923f959aa3SValentin Schneider if (do_cull) 24933f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 24943f959aa3SValentin Schneider } 24953f959aa3SValentin Schneider 24963f959aa3SValentin Schneider /** 24973f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 24983f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 24993f959aa3SValentin Schneider * 25003f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 25013f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2502e02b9312SValentin Schneider * 2503e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2504e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2505e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 25063f959aa3SValentin Schneider */ 25073f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 25083f959aa3SValentin Schneider { 25093f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 25109680540cSYang Yingliang LIST_HEAD(cull_list); 2511e22bee78STejun Heo 2512e02b9312SValentin Schneider /* 2513e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2514e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2515e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2516e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2517e02b9312SValentin Schneider */ 2518e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2519a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2520e22bee78STejun Heo 25213347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2522e22bee78STejun Heo struct worker *worker; 2523e22bee78STejun Heo unsigned long expires; 2524e22bee78STejun Heo 252563d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2526e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2527e22bee78STejun Heo 25283347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 252963d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 25303347fc9fSLai Jiangshan break; 2531e22bee78STejun Heo } 25323347fc9fSLai Jiangshan 2533e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2534e22bee78STejun Heo } 2535e22bee78STejun Heo 2536a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2537e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2538e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2539e22bee78STejun Heo } 2540e22bee78STejun Heo 2541493a1724STejun Heo static void send_mayday(struct work_struct *work) 2542e22bee78STejun Heo { 2543112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2544112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2545493a1724STejun Heo 25462e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2547e22bee78STejun Heo 2548493008a8STejun Heo if (!wq->rescuer) 2549493a1724STejun Heo return; 2550e22bee78STejun Heo 2551e22bee78STejun Heo /* mayday mayday mayday */ 2552493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 255377668c8bSLai Jiangshan /* 255477668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 255577668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 255677668c8bSLai Jiangshan * rescuer is done with it. 255777668c8bSLai Jiangshan */ 255877668c8bSLai Jiangshan get_pwq(pwq); 2559493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2560e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2561725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2562493a1724STejun Heo } 2563e22bee78STejun Heo } 2564e22bee78STejun Heo 256532a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2566e22bee78STejun Heo { 256732a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2568e22bee78STejun Heo struct work_struct *work; 2569e22bee78STejun Heo 2570a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2571a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2572e22bee78STejun Heo 257363d95a91STejun Heo if (need_to_create_worker(pool)) { 2574e22bee78STejun Heo /* 2575e22bee78STejun Heo * We've been trying to create a new worker but 2576e22bee78STejun Heo * haven't been successful. We might be hitting an 2577e22bee78STejun Heo * allocation deadlock. Send distress signals to 2578e22bee78STejun Heo * rescuers. 2579e22bee78STejun Heo */ 258063d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2581e22bee78STejun Heo send_mayday(work); 2582e22bee78STejun Heo } 2583e22bee78STejun Heo 2584a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2585a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2586e22bee78STejun Heo 258763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2588e22bee78STejun Heo } 2589e22bee78STejun Heo 2590e22bee78STejun Heo /** 2591e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 259263d95a91STejun Heo * @pool: pool to create a new worker for 2593e22bee78STejun Heo * 259463d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2595e22bee78STejun Heo * have at least one idle worker on return from this function. If 2596e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 259763d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2598e22bee78STejun Heo * possible allocation deadlock. 2599e22bee78STejun Heo * 2600c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2601c5aa87bbSTejun Heo * may_start_working() %true. 2602e22bee78STejun Heo * 2603e22bee78STejun Heo * LOCKING: 2604a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2605e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2606e22bee78STejun Heo * manager. 2607e22bee78STejun Heo */ 260829187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2609d565ed63STejun Heo __releases(&pool->lock) 2610d565ed63STejun Heo __acquires(&pool->lock) 2611e22bee78STejun Heo { 2612e22bee78STejun Heo restart: 2613a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 26149f9c2364STejun Heo 2615e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 261663d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2617e22bee78STejun Heo 2618e22bee78STejun Heo while (true) { 2619051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2620e22bee78STejun Heo break; 2621e22bee78STejun Heo 2622e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 26239f9c2364STejun Heo 262463d95a91STejun Heo if (!need_to_create_worker(pool)) 2625e22bee78STejun Heo break; 2626e22bee78STejun Heo } 2627e22bee78STejun Heo 262863d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2629a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2630051e1850SLai Jiangshan /* 2631051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2632051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2633051e1850SLai Jiangshan * already become busy. 2634051e1850SLai Jiangshan */ 263563d95a91STejun Heo if (need_to_create_worker(pool)) 2636e22bee78STejun Heo goto restart; 2637e22bee78STejun Heo } 2638e22bee78STejun Heo 2639e22bee78STejun Heo /** 2640e22bee78STejun Heo * manage_workers - manage worker pool 2641e22bee78STejun Heo * @worker: self 2642e22bee78STejun Heo * 2643706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2644e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2645706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2646e22bee78STejun Heo * 2647e22bee78STejun Heo * The caller can safely start processing works on false return. On 2648e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2649e22bee78STejun Heo * and may_start_working() is true. 2650e22bee78STejun Heo * 2651e22bee78STejun Heo * CONTEXT: 2652a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2653e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2654e22bee78STejun Heo * 2655d185af30SYacine Belkadi * Return: 265629187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 265729187a9eSTejun Heo * start processing works, %true if management function was performed and 265829187a9eSTejun Heo * the conditions that the caller verified before calling the function may 265929187a9eSTejun Heo * no longer be true. 2660e22bee78STejun Heo */ 2661e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2662e22bee78STejun Heo { 266363d95a91STejun Heo struct worker_pool *pool = worker->pool; 2664e22bee78STejun Heo 2665692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 266629187a9eSTejun Heo return false; 2667692b4825STejun Heo 2668692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 26692607d7a6STejun Heo pool->manager = worker; 2670e22bee78STejun Heo 267129187a9eSTejun Heo maybe_create_worker(pool); 2672e22bee78STejun Heo 26732607d7a6STejun Heo pool->manager = NULL; 2674692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2675d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 267629187a9eSTejun Heo return true; 2677e22bee78STejun Heo } 2678e22bee78STejun Heo 2679a62428c0STejun Heo /** 2680a62428c0STejun Heo * process_one_work - process single work 2681c34056a3STejun Heo * @worker: self 2682a62428c0STejun Heo * @work: work to process 2683a62428c0STejun Heo * 2684a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2685a62428c0STejun Heo * process a single work including synchronization against and 2686a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2687a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2688a62428c0STejun Heo * call this function to process a work. 2689a62428c0STejun Heo * 2690a62428c0STejun Heo * CONTEXT: 2691a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2692a62428c0STejun Heo */ 2693c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2694d565ed63STejun Heo __releases(&pool->lock) 2695d565ed63STejun Heo __acquires(&pool->lock) 26961da177e4SLinus Torvalds { 2697112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2698bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2699c4560c2cSLai Jiangshan unsigned long work_data; 27004e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 27014e6045f1SJohannes Berg /* 2702a62428c0STejun Heo * It is permissible to free the struct work_struct from 2703a62428c0STejun Heo * inside the function that is called from it, this we need to 2704a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2705a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2706a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 27074e6045f1SJohannes Berg */ 27084d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 27094d82a1deSPeter Zijlstra 27104d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 27114e6045f1SJohannes Berg #endif 2712807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 271385327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2714ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 271525511a47STejun Heo 27168930cabaSTejun Heo /* claim and dequeue */ 2717dc186ad7SThomas Gleixner debug_work_deactivate(work); 2718c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2719c34056a3STejun Heo worker->current_work = work; 2720a2c1c57bSTejun Heo worker->current_func = work->func; 2721112202d9STejun Heo worker->current_pwq = pwq; 2722616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2723c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2724d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 27257a22ad75STejun Heo 27268bf89593STejun Heo /* 27278bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 27288bf89593STejun Heo * overridden through set_worker_desc(). 27298bf89593STejun Heo */ 27308bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 27318bf89593STejun Heo 2732a62428c0STejun Heo list_del_init(&work->entry); 2733a62428c0STejun Heo 2734649027d7STejun Heo /* 2735228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2736228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2737228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2738228f1d00SLai Jiangshan * execution of the pending work items. 2739fb0e7bebSTejun Heo */ 2740616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2741228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2742fb0e7bebSTejun Heo 2743974271c4STejun Heo /* 27440219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 27450219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 27460219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 27470219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 2748974271c4STejun Heo */ 27490219a352STejun Heo kick_pool(pool); 2750974271c4STejun Heo 27518930cabaSTejun Heo /* 27527c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 2753d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 275423657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 275523657bb1STejun Heo * disabled. 27568930cabaSTejun Heo */ 27577c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 27581da177e4SLinus Torvalds 2759fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 2760a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2761365970a1SDavid Howells 2762a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 27633295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 2764e6f3faa7SPeter Zijlstra /* 2765f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 2766f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 2767e6f3faa7SPeter Zijlstra * 2768e6f3faa7SPeter Zijlstra * However, that would result in: 2769e6f3faa7SPeter Zijlstra * 2770e6f3faa7SPeter Zijlstra * A(W1) 2771e6f3faa7SPeter Zijlstra * WFC(C) 2772e6f3faa7SPeter Zijlstra * A(W1) 2773e6f3faa7SPeter Zijlstra * C(C) 2774e6f3faa7SPeter Zijlstra * 2775e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 2776e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 2777e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 2778f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 2779e6f3faa7SPeter Zijlstra * these locks. 2780e6f3faa7SPeter Zijlstra * 2781e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 2782e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 2783e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 2784e6f3faa7SPeter Zijlstra */ 2785f52be570SPeter Zijlstra lockdep_invariant_state(true); 2786e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 2787a2c1c57bSTejun Heo worker->current_func(work); 2788e36c886aSArjan van de Ven /* 2789e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 2790e36c886aSArjan van de Ven * point will only record its address. 2791e36c886aSArjan van de Ven */ 27921c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 2793725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 27943295f0efSIngo Molnar lock_map_release(&lockdep_map); 2795112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 27961da177e4SLinus Torvalds 2797d5abe669SPeter Zijlstra if (unlikely(in_atomic() || lockdep_depth(current) > 0)) { 2798044c782cSValentin Ilie pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d\n" 2799d75f773cSSakari Ailus " last function: %ps\n", 2800a2c1c57bSTejun Heo current->comm, preempt_count(), task_pid_nr(current), 2801a2c1c57bSTejun Heo worker->current_func); 2802d5abe669SPeter Zijlstra debug_show_held_locks(current); 2803d5abe669SPeter Zijlstra dump_stack(); 2804d5abe669SPeter Zijlstra } 2805d5abe669SPeter Zijlstra 2806b22ce278STejun Heo /* 2807025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 2808b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 2809b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 2810b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 2811789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 2812789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 2813b22ce278STejun Heo */ 2814a7e6425eSPaul E. McKenney cond_resched(); 2815b22ce278STejun Heo 2816a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2817a62428c0STejun Heo 2818616db877STejun Heo /* 2819616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 2820616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 2821616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 2822616db877STejun Heo */ 2823fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 2824fb0e7bebSTejun Heo 28251b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 28261b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 28271b69ac6bSJohannes Weiner 2828a62428c0STejun Heo /* we're done with it, release */ 282942f8570fSSasha Levin hash_del(&worker->hentry); 2830c34056a3STejun Heo worker->current_work = NULL; 2831a2c1c57bSTejun Heo worker->current_func = NULL; 2832112202d9STejun Heo worker->current_pwq = NULL; 2833d812796eSLai Jiangshan worker->current_color = INT_MAX; 2834c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 2837affee4b2STejun Heo /** 2838affee4b2STejun Heo * process_scheduled_works - process scheduled works 2839affee4b2STejun Heo * @worker: self 2840affee4b2STejun Heo * 2841affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 2842affee4b2STejun Heo * may change while processing a work, so this function repeatedly 2843affee4b2STejun Heo * fetches a work from the top and executes it. 2844affee4b2STejun Heo * 2845affee4b2STejun Heo * CONTEXT: 2846a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2847affee4b2STejun Heo * multiple times. 2848affee4b2STejun Heo */ 2849affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 28501da177e4SLinus Torvalds { 2851c0ab017dSTejun Heo struct work_struct *work; 2852c0ab017dSTejun Heo bool first = true; 2853c0ab017dSTejun Heo 2854c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 2855c0ab017dSTejun Heo struct work_struct, entry))) { 2856c0ab017dSTejun Heo if (first) { 2857c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 2858c0ab017dSTejun Heo first = false; 2859c0ab017dSTejun Heo } 2860c34056a3STejun Heo process_one_work(worker, work); 2861a62428c0STejun Heo } 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds 2864197f6accSTejun Heo static void set_pf_worker(bool val) 2865197f6accSTejun Heo { 2866197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 2867197f6accSTejun Heo if (val) 2868197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 2869197f6accSTejun Heo else 2870197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 2871197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 2872197f6accSTejun Heo } 2873197f6accSTejun Heo 28744690c4abSTejun Heo /** 28754690c4abSTejun Heo * worker_thread - the worker thread function 2876c34056a3STejun Heo * @__worker: self 28774690c4abSTejun Heo * 2878c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 2879c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 2880c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 2881c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 2882c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 2883d185af30SYacine Belkadi * 2884d185af30SYacine Belkadi * Return: 0 28854690c4abSTejun Heo */ 2886c34056a3STejun Heo static int worker_thread(void *__worker) 28871da177e4SLinus Torvalds { 2888c34056a3STejun Heo struct worker *worker = __worker; 2889bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 28901da177e4SLinus Torvalds 2891e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 2892197f6accSTejun Heo set_pf_worker(true); 2893c8e55f36STejun Heo woke_up: 2894a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2895affee4b2STejun Heo 2896a9ab775bSTejun Heo /* am I supposed to die? */ 2897a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 2898a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2899197f6accSTejun Heo set_pf_worker(false); 290060f5a4bcSLai Jiangshan 290160f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 2902e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 2903a2d812a2STejun Heo worker_detach_from_pool(worker); 2904e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 290560f5a4bcSLai Jiangshan kfree(worker); 2906c8e55f36STejun Heo return 0; 2907c8e55f36STejun Heo } 2908c8e55f36STejun Heo 2909c8e55f36STejun Heo worker_leave_idle(worker); 2910db7bccf4STejun Heo recheck: 2911e22bee78STejun Heo /* no more worker necessary? */ 291263d95a91STejun Heo if (!need_more_worker(pool)) 2913e22bee78STejun Heo goto sleep; 2914e22bee78STejun Heo 2915e22bee78STejun Heo /* do we need to manage? */ 291663d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 2917e22bee78STejun Heo goto recheck; 2918e22bee78STejun Heo 2919c8e55f36STejun Heo /* 2920c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 2921c8e55f36STejun Heo * preparing to process a work or actually processing it. 2922c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 2923c8e55f36STejun Heo */ 29246183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 2925c8e55f36STejun Heo 2926e22bee78STejun Heo /* 2927a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 2928a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 2929a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 2930a9ab775bSTejun Heo * management if applicable and concurrency management is restored 2931a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 2932e22bee78STejun Heo */ 2933a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 2934e22bee78STejun Heo 2935e22bee78STejun Heo do { 2936affee4b2STejun Heo struct work_struct *work = 2937bd7bdd43STejun Heo list_first_entry(&pool->worklist, 2938affee4b2STejun Heo struct work_struct, entry); 2939affee4b2STejun Heo 2940873eaca6STejun Heo if (assign_work(work, worker, NULL)) 2941affee4b2STejun Heo process_scheduled_works(worker); 294263d95a91STejun Heo } while (keep_working(pool)); 2943affee4b2STejun Heo 2944228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 2945d313dd85STejun Heo sleep: 2946c8e55f36STejun Heo /* 2947d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 2948d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 2949d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 2950d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 2951d565ed63STejun Heo * event. 2952c8e55f36STejun Heo */ 2953c8e55f36STejun Heo worker_enter_idle(worker); 2954c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 2955a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 29561da177e4SLinus Torvalds schedule(); 2957c8e55f36STejun Heo goto woke_up; 29581da177e4SLinus Torvalds } 29591da177e4SLinus Torvalds 2960e22bee78STejun Heo /** 2961e22bee78STejun Heo * rescuer_thread - the rescuer thread function 2962111c225aSTejun Heo * @__rescuer: self 2963e22bee78STejun Heo * 2964e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 2965493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 2966e22bee78STejun Heo * 2967706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 2968e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 2969e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 2970e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 2971e22bee78STejun Heo * the problem rescuer solves. 2972e22bee78STejun Heo * 2973706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 2974706026c2STejun Heo * workqueues which have works queued on the pool and let them process 2975e22bee78STejun Heo * those works so that forward progress can be guaranteed. 2976e22bee78STejun Heo * 2977e22bee78STejun Heo * This should happen rarely. 2978d185af30SYacine Belkadi * 2979d185af30SYacine Belkadi * Return: 0 2980e22bee78STejun Heo */ 2981111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 2982e22bee78STejun Heo { 2983111c225aSTejun Heo struct worker *rescuer = __rescuer; 2984111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 29854d595b86SLai Jiangshan bool should_stop; 2986e22bee78STejun Heo 2987e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 2988111c225aSTejun Heo 2989111c225aSTejun Heo /* 2990111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 2991111c225aSTejun Heo * doesn't participate in concurrency management. 2992111c225aSTejun Heo */ 2993197f6accSTejun Heo set_pf_worker(true); 2994e22bee78STejun Heo repeat: 2995c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 29961da177e4SLinus Torvalds 29974d595b86SLai Jiangshan /* 29984d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 29994d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 30004d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 30014d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 30024d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 30034d595b86SLai Jiangshan * list is always empty on exit. 30044d595b86SLai Jiangshan */ 30054d595b86SLai Jiangshan should_stop = kthread_should_stop(); 30061da177e4SLinus Torvalds 3007493a1724STejun Heo /* see whether any pwq is asking for help */ 3008a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3009493a1724STejun Heo 3010493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3011493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3012493a1724STejun Heo struct pool_workqueue, mayday_node); 3013112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3014e22bee78STejun Heo struct work_struct *work, *n; 3015e22bee78STejun Heo 3016e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3017493a1724STejun Heo list_del_init(&pwq->mayday_node); 3018493a1724STejun Heo 3019a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3020e22bee78STejun Heo 302151697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 302251697d39SLai Jiangshan 3023a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3024e22bee78STejun Heo 3025e22bee78STejun Heo /* 3026e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3027e22bee78STejun Heo * process'em. 3028e22bee78STejun Heo */ 3029873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 303082607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3031873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3032873eaca6STejun Heo assign_work(work, rescuer, &n)) 3033725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 303482607adcSTejun Heo } 3035e22bee78STejun Heo 3036873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3037e22bee78STejun Heo process_scheduled_works(rescuer); 30387576958aSTejun Heo 30397576958aSTejun Heo /* 3040008847f6SNeilBrown * The above execution of rescued work items could 3041008847f6SNeilBrown * have created more to rescue through 3042f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3043008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3044008847f6SNeilBrown * that such back-to-back work items, which may be 3045008847f6SNeilBrown * being used to relieve memory pressure, don't 3046008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3047008847f6SNeilBrown */ 30484f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3049a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3050e66b39afSTejun Heo /* 3051e66b39afSTejun Heo * Queue iff we aren't racing destruction 3052e66b39afSTejun Heo * and somebody else hasn't queued it already. 3053e66b39afSTejun Heo */ 3054e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3055008847f6SNeilBrown get_pwq(pwq); 3056e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3057e66b39afSTejun Heo } 3058a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3059008847f6SNeilBrown } 3060008847f6SNeilBrown } 3061008847f6SNeilBrown 3062008847f6SNeilBrown /* 306377668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 306413b1d625SLai Jiangshan * go away while we're still attached to it. 306577668c8bSLai Jiangshan */ 306677668c8bSLai Jiangshan put_pwq(pwq); 306777668c8bSLai Jiangshan 306877668c8bSLai Jiangshan /* 30690219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 30700219a352STejun Heo * with 0 concurrency and stalling the execution. 30717576958aSTejun Heo */ 30720219a352STejun Heo kick_pool(pool); 30737576958aSTejun Heo 3074a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 307513b1d625SLai Jiangshan 3076a2d812a2STejun Heo worker_detach_from_pool(rescuer); 307713b1d625SLai Jiangshan 3078a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 30791da177e4SLinus Torvalds } 30801da177e4SLinus Torvalds 3081a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3082493a1724STejun Heo 30834d595b86SLai Jiangshan if (should_stop) { 30844d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3085197f6accSTejun Heo set_pf_worker(false); 30864d595b86SLai Jiangshan return 0; 30874d595b86SLai Jiangshan } 30884d595b86SLai Jiangshan 3089111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3090111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3091e22bee78STejun Heo schedule(); 3092e22bee78STejun Heo goto repeat; 30931da177e4SLinus Torvalds } 30941da177e4SLinus Torvalds 3095fca839c0STejun Heo /** 3096fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3097fca839c0STejun Heo * @target_wq: workqueue being flushed 3098fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3099fca839c0STejun Heo * 3100fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3101fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3102fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3103fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3104fca839c0STejun Heo * a deadlock. 3105fca839c0STejun Heo */ 3106fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3107fca839c0STejun Heo struct work_struct *target_work) 3108fca839c0STejun Heo { 3109fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3110fca839c0STejun Heo struct worker *worker; 3111fca839c0STejun Heo 3112fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3113fca839c0STejun Heo return; 3114fca839c0STejun Heo 3115fca839c0STejun Heo worker = current_wq_worker(); 3116fca839c0STejun Heo 3117fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3118d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3119fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 312023d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 312123d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3122d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3123fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3124fca839c0STejun Heo target_wq->name, target_func); 3125fca839c0STejun Heo } 3126fca839c0STejun Heo 3127fc2e4d70SOleg Nesterov struct wq_barrier { 3128fc2e4d70SOleg Nesterov struct work_struct work; 3129fc2e4d70SOleg Nesterov struct completion done; 31302607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3131fc2e4d70SOleg Nesterov }; 3132fc2e4d70SOleg Nesterov 3133fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3134fc2e4d70SOleg Nesterov { 3135fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3136fc2e4d70SOleg Nesterov complete(&barr->done); 3137fc2e4d70SOleg Nesterov } 3138fc2e4d70SOleg Nesterov 31394690c4abSTejun Heo /** 31404690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3141112202d9STejun Heo * @pwq: pwq to insert barrier into 31424690c4abSTejun Heo * @barr: wq_barrier to insert 3143affee4b2STejun Heo * @target: target work to attach @barr to 3144affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 31454690c4abSTejun Heo * 3146affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3147affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3148affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3149affee4b2STejun Heo * cpu. 3150affee4b2STejun Heo * 3151affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3152affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3153affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3154affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3155affee4b2STejun Heo * after a work with LINKED flag set. 3156affee4b2STejun Heo * 3157affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3158112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 31594690c4abSTejun Heo * 31604690c4abSTejun Heo * CONTEXT: 3161a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 31624690c4abSTejun Heo */ 3163112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3164affee4b2STejun Heo struct wq_barrier *barr, 3165affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3166fc2e4d70SOleg Nesterov { 3167d812796eSLai Jiangshan unsigned int work_flags = 0; 3168d812796eSLai Jiangshan unsigned int work_color; 3169affee4b2STejun Heo struct list_head *head; 3170affee4b2STejun Heo 3171dc186ad7SThomas Gleixner /* 3172d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3173dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3174dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3175dc186ad7SThomas Gleixner * might deadlock. 3176dc186ad7SThomas Gleixner */ 3177ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 317822df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 317952fa5bc5SBoqun Feng 3180fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3181fd1a5b04SByungchul Park 31822607d7a6STejun Heo barr->task = current; 318383c22520SOleg Nesterov 3184018f3a13SLai Jiangshan /* The barrier work item does not participate in pwq->nr_active. */ 3185018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3186018f3a13SLai Jiangshan 3187affee4b2STejun Heo /* 3188affee4b2STejun Heo * If @target is currently being executed, schedule the 3189affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3190affee4b2STejun Heo */ 3191d812796eSLai Jiangshan if (worker) { 3192affee4b2STejun Heo head = worker->scheduled.next; 3193d812796eSLai Jiangshan work_color = worker->current_color; 3194d812796eSLai Jiangshan } else { 3195affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3196affee4b2STejun Heo 3197affee4b2STejun Heo head = target->entry.next; 3198affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3199d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3200d812796eSLai Jiangshan work_color = get_work_color(*bits); 3201affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3202affee4b2STejun Heo } 3203affee4b2STejun Heo 3204d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3205d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3206d812796eSLai Jiangshan 3207d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3208fc2e4d70SOleg Nesterov } 3209fc2e4d70SOleg Nesterov 321073f53c4aSTejun Heo /** 3211112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 321273f53c4aSTejun Heo * @wq: workqueue being flushed 321373f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 321473f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 321573f53c4aSTejun Heo * 3216112202d9STejun Heo * Prepare pwqs for workqueue flushing. 321773f53c4aSTejun Heo * 3218112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3219112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3220112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3221112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3222112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 322373f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 322473f53c4aSTejun Heo * 322573f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 322673f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 322773f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 322873f53c4aSTejun Heo * is returned. 322973f53c4aSTejun Heo * 3230112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 323173f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 323273f53c4aSTejun Heo * advanced to @work_color. 323373f53c4aSTejun Heo * 323473f53c4aSTejun Heo * CONTEXT: 32353c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 323673f53c4aSTejun Heo * 3237d185af30SYacine Belkadi * Return: 323873f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 323973f53c4aSTejun Heo * otherwise. 324073f53c4aSTejun Heo */ 3241112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 324273f53c4aSTejun Heo int flush_color, int work_color) 32431da177e4SLinus Torvalds { 324473f53c4aSTejun Heo bool wait = false; 324549e3cf44STejun Heo struct pool_workqueue *pwq; 32461da177e4SLinus Torvalds 324773f53c4aSTejun Heo if (flush_color >= 0) { 32486183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3249112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3250dc186ad7SThomas Gleixner } 325114441960SOleg Nesterov 325249e3cf44STejun Heo for_each_pwq(pwq, wq) { 3253112202d9STejun Heo struct worker_pool *pool = pwq->pool; 32541da177e4SLinus Torvalds 3255a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 325673f53c4aSTejun Heo 325773f53c4aSTejun Heo if (flush_color >= 0) { 32586183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 325973f53c4aSTejun Heo 3260112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3261112202d9STejun Heo pwq->flush_color = flush_color; 3262112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 326373f53c4aSTejun Heo wait = true; 32641da177e4SLinus Torvalds } 326573f53c4aSTejun Heo } 326673f53c4aSTejun Heo 326773f53c4aSTejun Heo if (work_color >= 0) { 32686183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3269112202d9STejun Heo pwq->work_color = work_color; 327073f53c4aSTejun Heo } 327173f53c4aSTejun Heo 3272a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 32731da177e4SLinus Torvalds } 32741da177e4SLinus Torvalds 3275112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 327673f53c4aSTejun Heo complete(&wq->first_flusher->done); 327773f53c4aSTejun Heo 327873f53c4aSTejun Heo return wait; 327983c22520SOleg Nesterov } 32801da177e4SLinus Torvalds 32810fcb78c2SRolf Eike Beer /** 3282c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 32830fcb78c2SRolf Eike Beer * @wq: workqueue to flush 32841da177e4SLinus Torvalds * 3285c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3286c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 32871da177e4SLinus Torvalds */ 3288c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 32891da177e4SLinus Torvalds { 329073f53c4aSTejun Heo struct wq_flusher this_flusher = { 329173f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 329273f53c4aSTejun Heo .flush_color = -1, 3293fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 329473f53c4aSTejun Heo }; 329573f53c4aSTejun Heo int next_color; 3296b1f4ec17SOleg Nesterov 32973347fa09STejun Heo if (WARN_ON(!wq_online)) 32983347fa09STejun Heo return; 32993347fa09STejun Heo 330087915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 330187915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 330287915adcSJohannes Berg 33033c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 330473f53c4aSTejun Heo 330573f53c4aSTejun Heo /* 330673f53c4aSTejun Heo * Start-to-wait phase 330773f53c4aSTejun Heo */ 330873f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 330973f53c4aSTejun Heo 331073f53c4aSTejun Heo if (next_color != wq->flush_color) { 331173f53c4aSTejun Heo /* 331273f53c4aSTejun Heo * Color space is not full. The current work_color 331373f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 331473f53c4aSTejun Heo * by one. 331573f53c4aSTejun Heo */ 33166183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 331773f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 331873f53c4aSTejun Heo wq->work_color = next_color; 331973f53c4aSTejun Heo 332073f53c4aSTejun Heo if (!wq->first_flusher) { 332173f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 33226183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 332373f53c4aSTejun Heo 332473f53c4aSTejun Heo wq->first_flusher = &this_flusher; 332573f53c4aSTejun Heo 3326112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 332773f53c4aSTejun Heo wq->work_color)) { 332873f53c4aSTejun Heo /* nothing to flush, done */ 332973f53c4aSTejun Heo wq->flush_color = next_color; 333073f53c4aSTejun Heo wq->first_flusher = NULL; 333173f53c4aSTejun Heo goto out_unlock; 333273f53c4aSTejun Heo } 333373f53c4aSTejun Heo } else { 333473f53c4aSTejun Heo /* wait in queue */ 33356183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 333673f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3337112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 333873f53c4aSTejun Heo } 333973f53c4aSTejun Heo } else { 334073f53c4aSTejun Heo /* 334173f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 334273f53c4aSTejun Heo * The next flush completion will assign us 334373f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 334473f53c4aSTejun Heo */ 334573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 334673f53c4aSTejun Heo } 334773f53c4aSTejun Heo 3348fca839c0STejun Heo check_flush_dependency(wq, NULL); 3349fca839c0STejun Heo 33503c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 335173f53c4aSTejun Heo 335273f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 335373f53c4aSTejun Heo 335473f53c4aSTejun Heo /* 335573f53c4aSTejun Heo * Wake-up-and-cascade phase 335673f53c4aSTejun Heo * 335773f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 335873f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 335973f53c4aSTejun Heo */ 336000d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 336173f53c4aSTejun Heo return; 336273f53c4aSTejun Heo 33633c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 336473f53c4aSTejun Heo 33654ce48b37STejun Heo /* we might have raced, check again with mutex held */ 33664ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 33674ce48b37STejun Heo goto out_unlock; 33684ce48b37STejun Heo 336900d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 337073f53c4aSTejun Heo 33716183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 33726183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 337373f53c4aSTejun Heo 337473f53c4aSTejun Heo while (true) { 337573f53c4aSTejun Heo struct wq_flusher *next, *tmp; 337673f53c4aSTejun Heo 337773f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 337873f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 337973f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 338073f53c4aSTejun Heo break; 338173f53c4aSTejun Heo list_del_init(&next->list); 338273f53c4aSTejun Heo complete(&next->done); 338373f53c4aSTejun Heo } 338473f53c4aSTejun Heo 33856183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 338673f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 338773f53c4aSTejun Heo 338873f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 338973f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 339073f53c4aSTejun Heo 339173f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 339273f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 339373f53c4aSTejun Heo /* 339473f53c4aSTejun Heo * Assign the same color to all overflowed 339573f53c4aSTejun Heo * flushers, advance work_color and append to 339673f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 339773f53c4aSTejun Heo * phase for these overflowed flushers. 339873f53c4aSTejun Heo */ 339973f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 340073f53c4aSTejun Heo tmp->flush_color = wq->work_color; 340173f53c4aSTejun Heo 340273f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 340373f53c4aSTejun Heo 340473f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 340573f53c4aSTejun Heo &wq->flusher_queue); 3406112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 340773f53c4aSTejun Heo } 340873f53c4aSTejun Heo 340973f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 34106183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 341173f53c4aSTejun Heo break; 341273f53c4aSTejun Heo } 341373f53c4aSTejun Heo 341473f53c4aSTejun Heo /* 341573f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3416112202d9STejun Heo * the new first flusher and arm pwqs. 341773f53c4aSTejun Heo */ 34186183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 34196183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 342073f53c4aSTejun Heo 342173f53c4aSTejun Heo list_del_init(&next->list); 342273f53c4aSTejun Heo wq->first_flusher = next; 342373f53c4aSTejun Heo 3424112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 342573f53c4aSTejun Heo break; 342673f53c4aSTejun Heo 342773f53c4aSTejun Heo /* 342873f53c4aSTejun Heo * Meh... this color is already done, clear first 342973f53c4aSTejun Heo * flusher and repeat cascading. 343073f53c4aSTejun Heo */ 343173f53c4aSTejun Heo wq->first_flusher = NULL; 343273f53c4aSTejun Heo } 343373f53c4aSTejun Heo 343473f53c4aSTejun Heo out_unlock: 34353c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 34361da177e4SLinus Torvalds } 3437c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 34381da177e4SLinus Torvalds 34399c5a2ba7STejun Heo /** 34409c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 34419c5a2ba7STejun Heo * @wq: workqueue to drain 34429c5a2ba7STejun Heo * 34439c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 34449c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 34459c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3446b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 34479c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 34489c5a2ba7STejun Heo * takes too long. 34499c5a2ba7STejun Heo */ 34509c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 34519c5a2ba7STejun Heo { 34529c5a2ba7STejun Heo unsigned int flush_cnt = 0; 345349e3cf44STejun Heo struct pool_workqueue *pwq; 34549c5a2ba7STejun Heo 34559c5a2ba7STejun Heo /* 34569c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 34579c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3458618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 34599c5a2ba7STejun Heo */ 346087fc741eSLai Jiangshan mutex_lock(&wq->mutex); 34619c5a2ba7STejun Heo if (!wq->nr_drainers++) 3462618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 346387fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 34649c5a2ba7STejun Heo reflush: 3465c4f135d6STetsuo Handa __flush_workqueue(wq); 34669c5a2ba7STejun Heo 3467b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 346876af4d93STejun Heo 346949e3cf44STejun Heo for_each_pwq(pwq, wq) { 3470fa2563e4SThomas Tuttle bool drained; 34719c5a2ba7STejun Heo 3472a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3473bad184d2STejun Heo drained = pwq_is_empty(pwq); 3474a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3475fa2563e4SThomas Tuttle 3476fa2563e4SThomas Tuttle if (drained) 34779c5a2ba7STejun Heo continue; 34789c5a2ba7STejun Heo 34799c5a2ba7STejun Heo if (++flush_cnt == 10 || 34809c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3481e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3482e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 348376af4d93STejun Heo 3484b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 34859c5a2ba7STejun Heo goto reflush; 34869c5a2ba7STejun Heo } 34879c5a2ba7STejun Heo 34889c5a2ba7STejun Heo if (!--wq->nr_drainers) 3489618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 349087fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 34919c5a2ba7STejun Heo } 34929c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 34939c5a2ba7STejun Heo 3494d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3495d6e89786SJohannes Berg bool from_cancel) 3496baf59022STejun Heo { 3497baf59022STejun Heo struct worker *worker = NULL; 3498c9e7cf27STejun Heo struct worker_pool *pool; 3499112202d9STejun Heo struct pool_workqueue *pwq; 3500baf59022STejun Heo 3501baf59022STejun Heo might_sleep(); 3502baf59022STejun Heo 350324acfb71SThomas Gleixner rcu_read_lock(); 3504fa1b54e6STejun Heo pool = get_work_pool(work); 3505fa1b54e6STejun Heo if (!pool) { 350624acfb71SThomas Gleixner rcu_read_unlock(); 3507fa1b54e6STejun Heo return false; 3508fa1b54e6STejun Heo } 3509fa1b54e6STejun Heo 3510a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 35110b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3512112202d9STejun Heo pwq = get_work_pwq(work); 3513112202d9STejun Heo if (pwq) { 3514112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3515baf59022STejun Heo goto already_gone; 3516606a5020STejun Heo } else { 3517c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3518baf59022STejun Heo if (!worker) 3519baf59022STejun Heo goto already_gone; 3520112202d9STejun Heo pwq = worker->current_pwq; 3521606a5020STejun Heo } 3522baf59022STejun Heo 3523fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3524fca839c0STejun Heo 3525112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3526a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3527baf59022STejun Heo 3528e159489bSTejun Heo /* 3529a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3530a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3531a1d14934SPeter Zijlstra * 3532a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3533a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3534a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3535a1d14934SPeter Zijlstra * forward progress. 3536e159489bSTejun Heo */ 3537d6e89786SJohannes Berg if (!from_cancel && 3538d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3539112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3540112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3541a1d14934SPeter Zijlstra } 354224acfb71SThomas Gleixner rcu_read_unlock(); 3543baf59022STejun Heo return true; 3544baf59022STejun Heo already_gone: 3545a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 354624acfb71SThomas Gleixner rcu_read_unlock(); 3547baf59022STejun Heo return false; 3548baf59022STejun Heo } 3549baf59022STejun Heo 3550d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3551d6e89786SJohannes Berg { 3552d6e89786SJohannes Berg struct wq_barrier barr; 3553d6e89786SJohannes Berg 3554d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3555d6e89786SJohannes Berg return false; 3556d6e89786SJohannes Berg 35574d43d395STetsuo Handa if (WARN_ON(!work->func)) 35584d43d395STetsuo Handa return false; 35594d43d395STetsuo Handa 356087915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 356187915adcSJohannes Berg lock_map_release(&work->lockdep_map); 356287915adcSJohannes Berg 3563d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3564d6e89786SJohannes Berg wait_for_completion(&barr.done); 3565d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3566d6e89786SJohannes Berg return true; 3567d6e89786SJohannes Berg } else { 3568d6e89786SJohannes Berg return false; 3569d6e89786SJohannes Berg } 3570d6e89786SJohannes Berg } 3571d6e89786SJohannes Berg 3572db700897SOleg Nesterov /** 3573401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3574401a8d04STejun Heo * @work: the work to flush 3575db700897SOleg Nesterov * 3576606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3577606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3578401a8d04STejun Heo * 3579d185af30SYacine Belkadi * Return: 3580401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3581401a8d04STejun Heo * %false if it was already idle. 3582db700897SOleg Nesterov */ 3583401a8d04STejun Heo bool flush_work(struct work_struct *work) 3584db700897SOleg Nesterov { 3585d6e89786SJohannes Berg return __flush_work(work, false); 3586606a5020STejun Heo } 3587db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3588db700897SOleg Nesterov 35898603e1b3STejun Heo struct cwt_wait { 3590ac6424b9SIngo Molnar wait_queue_entry_t wait; 35918603e1b3STejun Heo struct work_struct *work; 35928603e1b3STejun Heo }; 35938603e1b3STejun Heo 3594ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 35958603e1b3STejun Heo { 35968603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 35978603e1b3STejun Heo 35988603e1b3STejun Heo if (cwait->work != key) 35998603e1b3STejun Heo return 0; 36008603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 36018603e1b3STejun Heo } 36028603e1b3STejun Heo 360336e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3604401a8d04STejun Heo { 36058603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3606bbb68dfaSTejun Heo unsigned long flags; 36071f1f642eSOleg Nesterov int ret; 36081f1f642eSOleg Nesterov 36091f1f642eSOleg Nesterov do { 3610bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3611bbb68dfaSTejun Heo /* 36128603e1b3STejun Heo * If someone else is already canceling, wait for it to 36138603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 36148603e1b3STejun Heo * because we may get scheduled between @work's completion 36158603e1b3STejun Heo * and the other canceling task resuming and clearing 36168603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 36178603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 36188603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 36198603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 36208603e1b3STejun Heo * we're hogging the CPU. 36218603e1b3STejun Heo * 36228603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 36238603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 36248603e1b3STejun Heo * wake function which matches @work along with exclusive 36258603e1b3STejun Heo * wait and wakeup. 3626bbb68dfaSTejun Heo */ 36278603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 36288603e1b3STejun Heo struct cwt_wait cwait; 36298603e1b3STejun Heo 36308603e1b3STejun Heo init_wait(&cwait.wait); 36318603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 36328603e1b3STejun Heo cwait.work = work; 36338603e1b3STejun Heo 36348603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 36358603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 36368603e1b3STejun Heo if (work_is_canceling(work)) 36378603e1b3STejun Heo schedule(); 36388603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 36398603e1b3STejun Heo } 36401f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 36411f1f642eSOleg Nesterov 3642bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3643bbb68dfaSTejun Heo mark_work_canceling(work); 3644bbb68dfaSTejun Heo local_irq_restore(flags); 3645bbb68dfaSTejun Heo 36463347fa09STejun Heo /* 36473347fa09STejun Heo * This allows canceling during early boot. We know that @work 36483347fa09STejun Heo * isn't executing. 36493347fa09STejun Heo */ 36503347fa09STejun Heo if (wq_online) 3651d6e89786SJohannes Berg __flush_work(work, true); 36523347fa09STejun Heo 36537a22ad75STejun Heo clear_work_data(work); 36548603e1b3STejun Heo 36558603e1b3STejun Heo /* 36568603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 36578603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 36588603e1b3STejun Heo * visible there. 36598603e1b3STejun Heo */ 36608603e1b3STejun Heo smp_mb(); 36618603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 36628603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 36638603e1b3STejun Heo 36641f1f642eSOleg Nesterov return ret; 36651f1f642eSOleg Nesterov } 36661f1f642eSOleg Nesterov 36676e84d644SOleg Nesterov /** 3668401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3669401a8d04STejun Heo * @work: the work to cancel 36706e84d644SOleg Nesterov * 3671401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3672401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3673401a8d04STejun Heo * another workqueue. On return from this function, @work is 3674401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 36751f1f642eSOleg Nesterov * 3676401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3677401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 36786e84d644SOleg Nesterov * 3679401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 36806e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3681401a8d04STejun Heo * 3682d185af30SYacine Belkadi * Return: 3683401a8d04STejun Heo * %true if @work was pending, %false otherwise. 36846e84d644SOleg Nesterov */ 3685401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 36866e84d644SOleg Nesterov { 368736e227d2STejun Heo return __cancel_work_timer(work, false); 3688b89deed3SOleg Nesterov } 368928e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3690b89deed3SOleg Nesterov 36916e84d644SOleg Nesterov /** 3692401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3693401a8d04STejun Heo * @dwork: the delayed work to flush 36946e84d644SOleg Nesterov * 3695401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3696401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3697401a8d04STejun Heo * considers the last queueing instance of @dwork. 36981f1f642eSOleg Nesterov * 3699d185af30SYacine Belkadi * Return: 3700401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3701401a8d04STejun Heo * %false if it was already idle. 37026e84d644SOleg Nesterov */ 3703401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3704401a8d04STejun Heo { 37058930cabaSTejun Heo local_irq_disable(); 3706401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 370760c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 37088930cabaSTejun Heo local_irq_enable(); 3709401a8d04STejun Heo return flush_work(&dwork->work); 3710401a8d04STejun Heo } 3711401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3712401a8d04STejun Heo 371305f0fe6bSTejun Heo /** 371405f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 371505f0fe6bSTejun Heo * @rwork: the rcu work to flush 371605f0fe6bSTejun Heo * 371705f0fe6bSTejun Heo * Return: 371805f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 371905f0fe6bSTejun Heo * %false if it was already idle. 372005f0fe6bSTejun Heo */ 372105f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 372205f0fe6bSTejun Heo { 372305f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 372405f0fe6bSTejun Heo rcu_barrier(); 372505f0fe6bSTejun Heo flush_work(&rwork->work); 372605f0fe6bSTejun Heo return true; 372705f0fe6bSTejun Heo } else { 372805f0fe6bSTejun Heo return flush_work(&rwork->work); 372905f0fe6bSTejun Heo } 373005f0fe6bSTejun Heo } 373105f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 373205f0fe6bSTejun Heo 3733f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3734f72b8792SJens Axboe { 3735f72b8792SJens Axboe unsigned long flags; 3736f72b8792SJens Axboe int ret; 3737f72b8792SJens Axboe 3738f72b8792SJens Axboe do { 3739f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 3740f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 3741f72b8792SJens Axboe 3742f72b8792SJens Axboe if (unlikely(ret < 0)) 3743f72b8792SJens Axboe return false; 3744f72b8792SJens Axboe 3745f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 3746f72b8792SJens Axboe local_irq_restore(flags); 3747f72b8792SJens Axboe return ret; 3748f72b8792SJens Axboe } 3749f72b8792SJens Axboe 375073b4b532SAndrey Grodzovsky /* 375173b4b532SAndrey Grodzovsky * See cancel_delayed_work() 375273b4b532SAndrey Grodzovsky */ 375373b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 375473b4b532SAndrey Grodzovsky { 375573b4b532SAndrey Grodzovsky return __cancel_work(work, false); 375673b4b532SAndrey Grodzovsky } 375773b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 375873b4b532SAndrey Grodzovsky 3759401a8d04STejun Heo /** 376057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 376157b30ae7STejun Heo * @dwork: delayed_work to cancel 376209383498STejun Heo * 3763d185af30SYacine Belkadi * Kill off a pending delayed_work. 3764d185af30SYacine Belkadi * 3765d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 3766d185af30SYacine Belkadi * pending. 3767d185af30SYacine Belkadi * 3768d185af30SYacine Belkadi * Note: 3769d185af30SYacine Belkadi * The work callback function may still be running on return, unless 3770d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 3771d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 377209383498STejun Heo * 377357b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 377409383498STejun Heo */ 377557b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 377609383498STejun Heo { 3777f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 377809383498STejun Heo } 377957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 378009383498STejun Heo 378109383498STejun Heo /** 3782401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 3783401a8d04STejun Heo * @dwork: the delayed work cancel 3784401a8d04STejun Heo * 3785401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 3786401a8d04STejun Heo * 3787d185af30SYacine Belkadi * Return: 3788401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 3789401a8d04STejun Heo */ 3790401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 37916e84d644SOleg Nesterov { 379236e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 37936e84d644SOleg Nesterov } 3794f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 37951da177e4SLinus Torvalds 37960fcb78c2SRolf Eike Beer /** 379731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 3798b6136773SAndrew Morton * @func: the function to call 3799b6136773SAndrew Morton * 380031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 380131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 3802b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 380331ddd871STejun Heo * 3804d185af30SYacine Belkadi * Return: 380531ddd871STejun Heo * 0 on success, -errno on failure. 3806b6136773SAndrew Morton */ 380765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 380815316ba8SChristoph Lameter { 380915316ba8SChristoph Lameter int cpu; 381038f51568SNamhyung Kim struct work_struct __percpu *works; 381115316ba8SChristoph Lameter 3812b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 3813b6136773SAndrew Morton if (!works) 381415316ba8SChristoph Lameter return -ENOMEM; 3815b6136773SAndrew Morton 3816ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 381793981800STejun Heo 381815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 38199bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 38209bfb1839SIngo Molnar 38219bfb1839SIngo Molnar INIT_WORK(work, func); 38228de6d308SOleg Nesterov schedule_work_on(cpu, work); 382315316ba8SChristoph Lameter } 382493981800STejun Heo 382593981800STejun Heo for_each_online_cpu(cpu) 38268616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 382793981800STejun Heo 3828ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 3829b6136773SAndrew Morton free_percpu(works); 383015316ba8SChristoph Lameter return 0; 383115316ba8SChristoph Lameter } 383215316ba8SChristoph Lameter 3833eef6a7d5SAlan Stern /** 38341fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 38351fa44ecaSJames Bottomley * @fn: the function to execute 38361fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 38371fa44ecaSJames Bottomley * be available when the work executes) 38381fa44ecaSJames Bottomley * 38391fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 38401fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 38411fa44ecaSJames Bottomley * 3842d185af30SYacine Belkadi * Return: 0 - function was executed 38431fa44ecaSJames Bottomley * 1 - function was scheduled for execution 38441fa44ecaSJames Bottomley */ 384565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 38461fa44ecaSJames Bottomley { 38471fa44ecaSJames Bottomley if (!in_interrupt()) { 384865f27f38SDavid Howells fn(&ew->work); 38491fa44ecaSJames Bottomley return 0; 38501fa44ecaSJames Bottomley } 38511fa44ecaSJames Bottomley 385265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 38531fa44ecaSJames Bottomley schedule_work(&ew->work); 38541fa44ecaSJames Bottomley 38551fa44ecaSJames Bottomley return 1; 38561fa44ecaSJames Bottomley } 38571fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 38581fa44ecaSJames Bottomley 38597a4e344cSTejun Heo /** 38607a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 38617a4e344cSTejun Heo * @attrs: workqueue_attrs to free 38627a4e344cSTejun Heo * 38637a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 38647a4e344cSTejun Heo */ 3865513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 38667a4e344cSTejun Heo { 38677a4e344cSTejun Heo if (attrs) { 38687a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 38699546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 38707a4e344cSTejun Heo kfree(attrs); 38717a4e344cSTejun Heo } 38727a4e344cSTejun Heo } 38737a4e344cSTejun Heo 38747a4e344cSTejun Heo /** 38757a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 38767a4e344cSTejun Heo * 38777a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 3878d185af30SYacine Belkadi * return it. 3879d185af30SYacine Belkadi * 3880d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 38817a4e344cSTejun Heo */ 3882513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 38837a4e344cSTejun Heo { 38847a4e344cSTejun Heo struct workqueue_attrs *attrs; 38857a4e344cSTejun Heo 3886be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 38877a4e344cSTejun Heo if (!attrs) 38887a4e344cSTejun Heo goto fail; 3889be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 38907a4e344cSTejun Heo goto fail; 38919546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 38929546b29eSTejun Heo goto fail; 38937a4e344cSTejun Heo 389413e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 3895523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 38967a4e344cSTejun Heo return attrs; 38977a4e344cSTejun Heo fail: 38987a4e344cSTejun Heo free_workqueue_attrs(attrs); 38997a4e344cSTejun Heo return NULL; 39007a4e344cSTejun Heo } 39017a4e344cSTejun Heo 390229c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 390329c91e99STejun Heo const struct workqueue_attrs *from) 390429c91e99STejun Heo { 390529c91e99STejun Heo to->nice = from->nice; 390629c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 39079546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 39088639ecebSTejun Heo to->affn_strict = from->affn_strict; 390984193c07STejun Heo 39102865a8fbSShaohua Li /* 391184193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 391284193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 391384193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 39142865a8fbSShaohua Li */ 391584193c07STejun Heo to->affn_scope = from->affn_scope; 3916af73f5c9STejun Heo to->ordered = from->ordered; 391729c91e99STejun Heo } 391829c91e99STejun Heo 39195de7a03cSTejun Heo /* 39205de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 39215de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 39225de7a03cSTejun Heo */ 39235de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 39245de7a03cSTejun Heo { 392584193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 39265de7a03cSTejun Heo attrs->ordered = false; 39275de7a03cSTejun Heo } 39285de7a03cSTejun Heo 392929c91e99STejun Heo /* hash value of the content of @attr */ 393029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 393129c91e99STejun Heo { 393229c91e99STejun Heo u32 hash = 0; 393329c91e99STejun Heo 393429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 393513e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 393613e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 39379546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 39389546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 39398639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 394029c91e99STejun Heo return hash; 394129c91e99STejun Heo } 394229c91e99STejun Heo 394329c91e99STejun Heo /* content equality test */ 394429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 394529c91e99STejun Heo const struct workqueue_attrs *b) 394629c91e99STejun Heo { 394729c91e99STejun Heo if (a->nice != b->nice) 394829c91e99STejun Heo return false; 394929c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 395029c91e99STejun Heo return false; 39519546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 39529546b29eSTejun Heo return false; 39538639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 39548639ecebSTejun Heo return false; 395529c91e99STejun Heo return true; 395629c91e99STejun Heo } 395729c91e99STejun Heo 39580f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 39590f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 39600f36ee24STejun Heo const cpumask_t *unbound_cpumask) 39610f36ee24STejun Heo { 39620f36ee24STejun Heo /* 39630f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 39640f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 39650f36ee24STejun Heo * @unbound_cpumask. 39660f36ee24STejun Heo */ 39670f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 39680f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 39690f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 39700f36ee24STejun Heo } 39710f36ee24STejun Heo 397284193c07STejun Heo /* find wq_pod_type to use for @attrs */ 397384193c07STejun Heo static const struct wq_pod_type * 397484193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 397584193c07STejun Heo { 3976523a301eSTejun Heo enum wq_affn_scope scope; 3977523a301eSTejun Heo struct wq_pod_type *pt; 3978523a301eSTejun Heo 3979523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 3980523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 3981523a301eSTejun Heo 3982523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 3983523a301eSTejun Heo scope = wq_affn_dfl; 3984523a301eSTejun Heo else 3985523a301eSTejun Heo scope = attrs->affn_scope; 3986523a301eSTejun Heo 3987523a301eSTejun Heo pt = &wq_pod_types[scope]; 398884193c07STejun Heo 398984193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 399084193c07STejun Heo likely(pt->nr_pods)) 399184193c07STejun Heo return pt; 399284193c07STejun Heo 399384193c07STejun Heo /* 399484193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 399584193c07STejun Heo * initialized in workqueue_init_early(). 399684193c07STejun Heo */ 399784193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 399884193c07STejun Heo BUG_ON(!pt->nr_pods); 399984193c07STejun Heo return pt; 400084193c07STejun Heo } 400184193c07STejun Heo 40027a4e344cSTejun Heo /** 40037a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 40047a4e344cSTejun Heo * @pool: worker_pool to initialize 40057a4e344cSTejun Heo * 4006402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4007d185af30SYacine Belkadi * 4008d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 400929c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 401029c91e99STejun Heo * on @pool safely to release it. 40117a4e344cSTejun Heo */ 40127a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 40134e1a1f9aSTejun Heo { 4014a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 401529c91e99STejun Heo pool->id = -1; 401629c91e99STejun Heo pool->cpu = -1; 4017f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 40184e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 401982607adcSTejun Heo pool->watchdog_ts = jiffies; 40204e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 40214e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 40224e1a1f9aSTejun Heo hash_init(pool->busy_hash); 40234e1a1f9aSTejun Heo 402432a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 40253f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 40264e1a1f9aSTejun Heo 402732a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 40284e1a1f9aSTejun Heo 4029da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4030e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 40317a4e344cSTejun Heo 40327cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 403329c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 403429c91e99STejun Heo pool->refcnt = 1; 403529c91e99STejun Heo 403629c91e99STejun Heo /* shouldn't fail above this point */ 4037be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 40387a4e344cSTejun Heo if (!pool->attrs) 40397a4e344cSTejun Heo return -ENOMEM; 40405de7a03cSTejun Heo 40415de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 40425de7a03cSTejun Heo 40437a4e344cSTejun Heo return 0; 40444e1a1f9aSTejun Heo } 40454e1a1f9aSTejun Heo 4046669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4047669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4048669de8bdSBart Van Assche { 4049669de8bdSBart Van Assche char *lock_name; 4050669de8bdSBart Van Assche 4051669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4052669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4053669de8bdSBart Van Assche if (!lock_name) 4054669de8bdSBart Van Assche lock_name = wq->name; 405569a106c0SQian Cai 405669a106c0SQian Cai wq->lock_name = lock_name; 4057669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4058669de8bdSBart Van Assche } 4059669de8bdSBart Van Assche 4060669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4061669de8bdSBart Van Assche { 4062669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4063669de8bdSBart Van Assche } 4064669de8bdSBart Van Assche 4065669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4066669de8bdSBart Van Assche { 4067669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4068669de8bdSBart Van Assche kfree(wq->lock_name); 4069669de8bdSBart Van Assche } 4070669de8bdSBart Van Assche #else 4071669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4072669de8bdSBart Van Assche { 4073669de8bdSBart Van Assche } 4074669de8bdSBart Van Assche 4075669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4076669de8bdSBart Van Assche { 4077669de8bdSBart Van Assche } 4078669de8bdSBart Van Assche 4079669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4080669de8bdSBart Van Assche { 4081669de8bdSBart Van Assche } 4082669de8bdSBart Van Assche #endif 4083669de8bdSBart Van Assche 4084*b522229aSTejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 4085*b522229aSTejun Heo { 4086*b522229aSTejun Heo int node; 4087*b522229aSTejun Heo 4088*b522229aSTejun Heo for_each_node(node) { 4089*b522229aSTejun Heo kfree(nna_ar[node]); 4090*b522229aSTejun Heo nna_ar[node] = NULL; 4091*b522229aSTejun Heo } 4092*b522229aSTejun Heo 4093*b522229aSTejun Heo kfree(nna_ar[nr_node_ids]); 4094*b522229aSTejun Heo nna_ar[nr_node_ids] = NULL; 4095*b522229aSTejun Heo } 4096*b522229aSTejun Heo 4097*b522229aSTejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 4098*b522229aSTejun Heo { 4099*b522229aSTejun Heo atomic_set(&nna->nr, 0); 4100*b522229aSTejun Heo } 4101*b522229aSTejun Heo 4102*b522229aSTejun Heo /* 4103*b522229aSTejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 4104*b522229aSTejun Heo * should be allocated in the node. 4105*b522229aSTejun Heo */ 4106*b522229aSTejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 4107*b522229aSTejun Heo { 4108*b522229aSTejun Heo struct wq_node_nr_active *nna; 4109*b522229aSTejun Heo int node; 4110*b522229aSTejun Heo 4111*b522229aSTejun Heo for_each_node(node) { 4112*b522229aSTejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 4113*b522229aSTejun Heo if (!nna) 4114*b522229aSTejun Heo goto err_free; 4115*b522229aSTejun Heo init_node_nr_active(nna); 4116*b522229aSTejun Heo nna_ar[node] = nna; 4117*b522229aSTejun Heo } 4118*b522229aSTejun Heo 4119*b522229aSTejun Heo /* [nr_node_ids] is used as the fallback */ 4120*b522229aSTejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 4121*b522229aSTejun Heo if (!nna) 4122*b522229aSTejun Heo goto err_free; 4123*b522229aSTejun Heo init_node_nr_active(nna); 4124*b522229aSTejun Heo nna_ar[nr_node_ids] = nna; 4125*b522229aSTejun Heo 4126*b522229aSTejun Heo return 0; 4127*b522229aSTejun Heo 4128*b522229aSTejun Heo err_free: 4129*b522229aSTejun Heo free_node_nr_active(nna_ar); 4130*b522229aSTejun Heo return -ENOMEM; 4131*b522229aSTejun Heo } 4132*b522229aSTejun Heo 4133e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4134e2dca7adSTejun Heo { 4135e2dca7adSTejun Heo struct workqueue_struct *wq = 4136e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4137e2dca7adSTejun Heo 4138*b522229aSTejun Heo if (wq->flags & WQ_UNBOUND) 4139*b522229aSTejun Heo free_node_nr_active(wq->node_nr_active); 4140*b522229aSTejun Heo 4141669de8bdSBart Van Assche wq_free_lockdep(wq); 4142ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4143e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4144e2dca7adSTejun Heo kfree(wq); 4145e2dca7adSTejun Heo } 4146e2dca7adSTejun Heo 414729c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 414829c91e99STejun Heo { 414929c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 415029c91e99STejun Heo 41517cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 415229c91e99STejun Heo free_workqueue_attrs(pool->attrs); 415329c91e99STejun Heo kfree(pool); 415429c91e99STejun Heo } 415529c91e99STejun Heo 415629c91e99STejun Heo /** 415729c91e99STejun Heo * put_unbound_pool - put a worker_pool 415829c91e99STejun Heo * @pool: worker_pool to put 415929c91e99STejun Heo * 416024acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4161c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4162c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4163c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4164a892caccSTejun Heo * 4165a892caccSTejun Heo * Should be called with wq_pool_mutex held. 416629c91e99STejun Heo */ 416729c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 416829c91e99STejun Heo { 416960f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 417029c91e99STejun Heo struct worker *worker; 41719680540cSYang Yingliang LIST_HEAD(cull_list); 4172e02b9312SValentin Schneider 4173a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4174a892caccSTejun Heo 4175a892caccSTejun Heo if (--pool->refcnt) 417629c91e99STejun Heo return; 417729c91e99STejun Heo 417829c91e99STejun Heo /* sanity checks */ 417961d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4180a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 418129c91e99STejun Heo return; 418229c91e99STejun Heo 418329c91e99STejun Heo /* release id and unhash */ 418429c91e99STejun Heo if (pool->id >= 0) 418529c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 418629c91e99STejun Heo hash_del(&pool->hash_node); 418729c91e99STejun Heo 4188c5aa87bbSTejun Heo /* 4189692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4190692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4191692b4825STejun Heo * manager and @pool gets freed with the flag set. 41929ab03be4SValentin Schneider * 41939ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 41949ab03be4SValentin Schneider * only get here with 41959ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 41969ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 41979ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 41989ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 41999ab03be4SValentin Schneider * drops pool->lock 4200c5aa87bbSTejun Heo */ 42019ab03be4SValentin Schneider while (true) { 42029ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 42039ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4204d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4205e02b9312SValentin Schneider 4206e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 42079ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 42089ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4209692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 42109ab03be4SValentin Schneider break; 42119ab03be4SValentin Schneider } 42129ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4213e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 42149ab03be4SValentin Schneider } 4215692b4825STejun Heo 42161037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4217e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 421829c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4219a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 422060f5a4bcSLai Jiangshan 4221e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4222e02b9312SValentin Schneider 4223e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 422460f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 42251258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 422660f5a4bcSLai Jiangshan 422760f5a4bcSLai Jiangshan if (pool->detach_completion) 422860f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 422960f5a4bcSLai Jiangshan 423029c91e99STejun Heo /* shut down the timers */ 423129c91e99STejun Heo del_timer_sync(&pool->idle_timer); 42323f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 423329c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 423429c91e99STejun Heo 423524acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 423625b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 423729c91e99STejun Heo } 423829c91e99STejun Heo 423929c91e99STejun Heo /** 424029c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 424129c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 424229c91e99STejun Heo * 424329c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 424429c91e99STejun Heo * reference count and return it. If there already is a matching 424529c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4246d185af30SYacine Belkadi * create a new one. 4247a892caccSTejun Heo * 4248a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4249d185af30SYacine Belkadi * 4250d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4251d185af30SYacine Belkadi * On failure, %NULL. 425229c91e99STejun Heo */ 425329c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 425429c91e99STejun Heo { 425584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 425629c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 425729c91e99STejun Heo struct worker_pool *pool; 425884193c07STejun Heo int pod, node = NUMA_NO_NODE; 425929c91e99STejun Heo 4260a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 426129c91e99STejun Heo 426229c91e99STejun Heo /* do we already have a matching pool? */ 426329c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 426429c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 426529c91e99STejun Heo pool->refcnt++; 42663fb1823cSLai Jiangshan return pool; 426729c91e99STejun Heo } 426829c91e99STejun Heo } 426929c91e99STejun Heo 42709546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 427184193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 42729546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 427384193c07STejun Heo node = pt->pod_node[pod]; 4274e2273584SXunlei Pang break; 4275e2273584SXunlei Pang } 4276e2273584SXunlei Pang } 4277e2273584SXunlei Pang 427829c91e99STejun Heo /* nope, create a new one */ 427984193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 428029c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 428129c91e99STejun Heo goto fail; 428229c91e99STejun Heo 428384193c07STejun Heo pool->node = node; 42845de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 42855de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 42862865a8fbSShaohua Li 428729c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 428829c91e99STejun Heo goto fail; 428929c91e99STejun Heo 429029c91e99STejun Heo /* create and start the initial worker */ 42913347fa09STejun Heo if (wq_online && !create_worker(pool)) 429229c91e99STejun Heo goto fail; 429329c91e99STejun Heo 429429c91e99STejun Heo /* install */ 429529c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 42963fb1823cSLai Jiangshan 429729c91e99STejun Heo return pool; 429829c91e99STejun Heo fail: 429929c91e99STejun Heo if (pool) 430029c91e99STejun Heo put_unbound_pool(pool); 430129c91e99STejun Heo return NULL; 430229c91e99STejun Heo } 430329c91e99STejun Heo 43048864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 43058864b4e5STejun Heo { 43068864b4e5STejun Heo kmem_cache_free(pwq_cache, 43078864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 43088864b4e5STejun Heo } 43098864b4e5STejun Heo 43108864b4e5STejun Heo /* 4311967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4312967b494eSTejun Heo * refcnt and needs to be destroyed. 43138864b4e5STejun Heo */ 4314687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 43158864b4e5STejun Heo { 43168864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4317687a9aa5STejun Heo release_work); 43188864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 43198864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4320b42b0bddSYang Yingliang bool is_last = false; 43218864b4e5STejun Heo 4322b42b0bddSYang Yingliang /* 4323687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4324b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4325b42b0bddSYang Yingliang */ 4326b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 43273c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 43288864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4329bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 43303c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4331b42b0bddSYang Yingliang } 43328864b4e5STejun Heo 4333687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4334a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 43358864b4e5STejun Heo put_unbound_pool(pool); 4336a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4337687a9aa5STejun Heo } 4338a892caccSTejun Heo 433925b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 43408864b4e5STejun Heo 43418864b4e5STejun Heo /* 43428864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4343e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 43448864b4e5STejun Heo */ 4345669de8bdSBart Van Assche if (is_last) { 4346669de8bdSBart Van Assche wq_unregister_lockdep(wq); 434725b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 43486029a918STejun Heo } 4349669de8bdSBart Van Assche } 43508864b4e5STejun Heo 435167dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4352f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4353f147f29eSTejun Heo struct worker_pool *pool) 4354d2c1d404STejun Heo { 4355d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4356d2c1d404STejun Heo 4357e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4358e50aba9aSTejun Heo 4359d2c1d404STejun Heo pwq->pool = pool; 4360d2c1d404STejun Heo pwq->wq = wq; 4361d2c1d404STejun Heo pwq->flush_color = -1; 43628864b4e5STejun Heo pwq->refcnt = 1; 4363f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 43641befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4365d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4366687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4367f147f29eSTejun Heo } 4368d2c1d404STejun Heo 4369f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 43701befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4371f147f29eSTejun Heo { 4372f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4373f147f29eSTejun Heo 4374f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 437575ccf595STejun Heo 43761befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 43771befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 43781befcf30STejun Heo return; 43791befcf30STejun Heo 438029b1cb41SLai Jiangshan /* set the matching work_color */ 438175ccf595STejun Heo pwq->work_color = wq->work_color; 4382983ca25eSTejun Heo 4383983ca25eSTejun Heo /* link in @pwq */ 43849e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4385df2d5ae4STejun Heo } 43866029a918STejun Heo 4387f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4388f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4389f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4390f147f29eSTejun Heo { 4391f147f29eSTejun Heo struct worker_pool *pool; 4392f147f29eSTejun Heo struct pool_workqueue *pwq; 4393f147f29eSTejun Heo 4394f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4395f147f29eSTejun Heo 4396f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4397f147f29eSTejun Heo if (!pool) 4398f147f29eSTejun Heo return NULL; 4399f147f29eSTejun Heo 4400e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4401f147f29eSTejun Heo if (!pwq) { 4402f147f29eSTejun Heo put_unbound_pool(pool); 4403f147f29eSTejun Heo return NULL; 4404f147f29eSTejun Heo } 4405f147f29eSTejun Heo 4406f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4407f147f29eSTejun Heo return pwq; 4408d2c1d404STejun Heo } 4409d2c1d404STejun Heo 44104c16bd32STejun Heo /** 4411fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4412042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 441384193c07STejun Heo * @cpu: the target CPU 44144c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 44154c16bd32STejun Heo * 4416fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4417fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 44189546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 44194c16bd32STejun Heo * 4420fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4421fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4422fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 44234c16bd32STejun Heo * 4424fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 44254c16bd32STejun Heo */ 44269546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 44279546b29eSTejun Heo int cpu_going_down) 44284c16bd32STejun Heo { 442984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 443084193c07STejun Heo int pod = pt->cpu_pod[cpu]; 44314c16bd32STejun Heo 4432fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 44339546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 44349546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 44354c16bd32STejun Heo if (cpu_going_down >= 0) 44369546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 44374c16bd32STejun Heo 44389546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 44399546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 444084193c07STejun Heo return; 444184193c07STejun Heo } 44424c16bd32STejun Heo 4443fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 44449546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 44451ad0f0a7SMichael Bringmann 44469546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 44471ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 44481ad0f0a7SMichael Bringmann "possible intersect\n"); 44494c16bd32STejun Heo } 44504c16bd32STejun Heo 4451bd31fb92STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4452636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4453636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 44541befcf30STejun Heo { 4455bd31fb92STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 44561befcf30STejun Heo struct pool_workqueue *old_pwq; 44571befcf30STejun Heo 44585b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 44591befcf30STejun Heo lockdep_assert_held(&wq->mutex); 44601befcf30STejun Heo 44611befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 44621befcf30STejun Heo link_pwq(pwq); 44631befcf30STejun Heo 4464bd31fb92STejun Heo old_pwq = rcu_access_pointer(*slot); 4465bd31fb92STejun Heo rcu_assign_pointer(*slot, pwq); 44661befcf30STejun Heo return old_pwq; 44671befcf30STejun Heo } 44681befcf30STejun Heo 44692d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 44702d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 44712d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 44722d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4473042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 44742d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 44752d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 44762d5f0764SLai Jiangshan }; 44772d5f0764SLai Jiangshan 44782d5f0764SLai Jiangshan /* free the resources after success or abort */ 44792d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 44802d5f0764SLai Jiangshan { 44812d5f0764SLai Jiangshan if (ctx) { 4482636b927eSTejun Heo int cpu; 44832d5f0764SLai Jiangshan 4484636b927eSTejun Heo for_each_possible_cpu(cpu) 4485636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 44862d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 44872d5f0764SLai Jiangshan 44882d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 44892d5f0764SLai Jiangshan 44902d5f0764SLai Jiangshan kfree(ctx); 44912d5f0764SLai Jiangshan } 44922d5f0764SLai Jiangshan } 44932d5f0764SLai Jiangshan 44942d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 44952d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 44962d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 449799c621efSLai Jiangshan const struct workqueue_attrs *attrs, 449899c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 44992d5f0764SLai Jiangshan { 45002d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 45019546b29eSTejun Heo struct workqueue_attrs *new_attrs; 4502636b927eSTejun Heo int cpu; 45032d5f0764SLai Jiangshan 45042d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 45052d5f0764SLai Jiangshan 450684193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 450784193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 450884193c07STejun Heo return ERR_PTR(-EINVAL); 450984193c07STejun Heo 4510636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 45112d5f0764SLai Jiangshan 4512be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 45139546b29eSTejun Heo if (!ctx || !new_attrs) 45142d5f0764SLai Jiangshan goto out_free; 45152d5f0764SLai Jiangshan 4516042f7df1SLai Jiangshan /* 45172d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 45182d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 45192d5f0764SLai Jiangshan * it even if we don't use it immediately. 45202d5f0764SLai Jiangshan */ 45210f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 45220f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 45239546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 45242d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 45252d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 45262d5f0764SLai Jiangshan goto out_free; 45272d5f0764SLai Jiangshan 4528636b927eSTejun Heo for_each_possible_cpu(cpu) { 4529af73f5c9STejun Heo if (new_attrs->ordered) { 45302d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4531636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4532636b927eSTejun Heo } else { 45339546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 45349546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 4535636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4536636b927eSTejun Heo goto out_free; 45372d5f0764SLai Jiangshan } 45382d5f0764SLai Jiangshan } 45392d5f0764SLai Jiangshan 4540042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4541042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4542042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 45439546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 45442d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4545042f7df1SLai Jiangshan 45462d5f0764SLai Jiangshan ctx->wq = wq; 45472d5f0764SLai Jiangshan return ctx; 45482d5f0764SLai Jiangshan 45492d5f0764SLai Jiangshan out_free: 45502d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 45512d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 455284193c07STejun Heo return ERR_PTR(-ENOMEM); 45532d5f0764SLai Jiangshan } 45542d5f0764SLai Jiangshan 45552d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 45562d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 45572d5f0764SLai Jiangshan { 4558636b927eSTejun Heo int cpu; 45592d5f0764SLai Jiangshan 45602d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 45612d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 45622d5f0764SLai Jiangshan 45632d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 45642d5f0764SLai Jiangshan 4565bd31fb92STejun Heo /* save the previous pwqs and install the new ones */ 4566636b927eSTejun Heo for_each_possible_cpu(cpu) 4567636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4568636b927eSTejun Heo ctx->pwq_tbl[cpu]); 4569bd31fb92STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 45702d5f0764SLai Jiangshan 45712d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 45722d5f0764SLai Jiangshan } 45732d5f0764SLai Jiangshan 4574a0111cf6SLai Jiangshan static void apply_wqattrs_lock(void) 4575a0111cf6SLai Jiangshan { 4576a0111cf6SLai Jiangshan /* CPUs should stay stable across pwq creations and installations */ 4577ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4578a0111cf6SLai Jiangshan mutex_lock(&wq_pool_mutex); 4579a0111cf6SLai Jiangshan } 4580a0111cf6SLai Jiangshan 4581a0111cf6SLai Jiangshan static void apply_wqattrs_unlock(void) 4582a0111cf6SLai Jiangshan { 4583a0111cf6SLai Jiangshan mutex_unlock(&wq_pool_mutex); 4584ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4585a0111cf6SLai Jiangshan } 4586a0111cf6SLai Jiangshan 4587a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4588a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4589a0111cf6SLai Jiangshan { 4590a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4591a0111cf6SLai Jiangshan 4592a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4593a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4594a0111cf6SLai Jiangshan return -EINVAL; 4595a0111cf6SLai Jiangshan 4596a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 45970a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 45980a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4599a0111cf6SLai Jiangshan return -EINVAL; 4600a0111cf6SLai Jiangshan 46010a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 46020a94efb5STejun Heo } 46030a94efb5STejun Heo 460499c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 460584193c07STejun Heo if (IS_ERR(ctx)) 460684193c07STejun Heo return PTR_ERR(ctx); 4607a0111cf6SLai Jiangshan 4608a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4609a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4610a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4611a0111cf6SLai Jiangshan 46126201171eSwanghaibin return 0; 4613a0111cf6SLai Jiangshan } 4614a0111cf6SLai Jiangshan 46159e8cd2f5STejun Heo /** 46169e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 46179e8cd2f5STejun Heo * @wq: the target workqueue 46189e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 46199e8cd2f5STejun Heo * 4620fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4621fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4622fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4623fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4624fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 46259e8cd2f5STejun Heo * 4626d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4627d185af30SYacine Belkadi * 4628ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4629509b3204SDaniel Jordan * 4630d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 46319e8cd2f5STejun Heo */ 4632513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 46339e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 46349e8cd2f5STejun Heo { 4635a0111cf6SLai Jiangshan int ret; 46369e8cd2f5STejun Heo 4637509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4638509b3204SDaniel Jordan 4639509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4640a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4641509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 46422d5f0764SLai Jiangshan 46432d5f0764SLai Jiangshan return ret; 46449e8cd2f5STejun Heo } 46459e8cd2f5STejun Heo 46464c16bd32STejun Heo /** 4647fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 46484c16bd32STejun Heo * @wq: the target workqueue 46494cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 46504cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 46514c16bd32STejun Heo * @online: whether @cpu is coming up or going down 46524c16bd32STejun Heo * 46534c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4654fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 46554c16bd32STejun Heo * @wq accordingly. 46564c16bd32STejun Heo * 46574c16bd32STejun Heo * 4658fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4659fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4660fef59c9cSTejun Heo * 4661fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4662fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4663fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4664fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4665fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4666fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 46674c16bd32STejun Heo */ 4668fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 46694cbfd3deSTejun Heo int hotplug_cpu, bool online) 46704c16bd32STejun Heo { 46714cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 46724c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 46734c16bd32STejun Heo struct workqueue_attrs *target_attrs; 46744c16bd32STejun Heo 46754c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 46764c16bd32STejun Heo 467784193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 46784c16bd32STejun Heo return; 46794c16bd32STejun Heo 46804c16bd32STejun Heo /* 46814c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 46824c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 46834c16bd32STejun Heo * CPU hotplug exclusion. 46844c16bd32STejun Heo */ 4685fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 46864c16bd32STejun Heo 46874c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 46880f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 46894c16bd32STejun Heo 4690636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 46919546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 4692bd31fb92STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 4693f7142ed4SLai Jiangshan return; 46944c16bd32STejun Heo 46954c16bd32STejun Heo /* create a new pwq */ 46964c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 46974c16bd32STejun Heo if (!pwq) { 4698fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 46994c16bd32STejun Heo wq->name); 470077f300b1SDaeseok Youn goto use_dfl_pwq; 47014c16bd32STejun Heo } 47024c16bd32STejun Heo 4703f7142ed4SLai Jiangshan /* Install the new pwq. */ 47044c16bd32STejun Heo mutex_lock(&wq->mutex); 4705636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 47064c16bd32STejun Heo goto out_unlock; 47074c16bd32STejun Heo 47084c16bd32STejun Heo use_dfl_pwq: 4709f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 4710bd31fb92STejun Heo pwq = unbound_pwq(wq, -1); 4711bd31fb92STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 4712bd31fb92STejun Heo get_pwq(pwq); 4713bd31fb92STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 4714bd31fb92STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 47154c16bd32STejun Heo out_unlock: 47164c16bd32STejun Heo mutex_unlock(&wq->mutex); 47174c16bd32STejun Heo put_pwq_unlocked(old_pwq); 47184c16bd32STejun Heo } 47194c16bd32STejun Heo 472030cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 47211da177e4SLinus Torvalds { 472249e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 47238a2b7538STejun Heo int cpu, ret; 4724e1d8aa9fSFrederic Weisbecker 4725687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 4726ee1ceef7STejun Heo if (!wq->cpu_pwq) 4727687a9aa5STejun Heo goto enomem; 472830cdf249STejun Heo 4729636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 473030cdf249STejun Heo for_each_possible_cpu(cpu) { 4731687a9aa5STejun Heo struct pool_workqueue **pwq_p = 4732ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 4733687a9aa5STejun Heo struct worker_pool *pool = 4734687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 473530cdf249STejun Heo 4736687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 4737687a9aa5STejun Heo pool->node); 4738687a9aa5STejun Heo if (!*pwq_p) 4739687a9aa5STejun Heo goto enomem; 4740687a9aa5STejun Heo 4741687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 4742f147f29eSTejun Heo 4743f147f29eSTejun Heo mutex_lock(&wq->mutex); 4744687a9aa5STejun Heo link_pwq(*pwq_p); 4745f147f29eSTejun Heo mutex_unlock(&wq->mutex); 474630cdf249STejun Heo } 474730cdf249STejun Heo return 0; 4748509b3204SDaniel Jordan } 4749509b3204SDaniel Jordan 4750ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 4751509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 4752bd31fb92STejun Heo struct pool_workqueue *dfl_pwq; 4753bd31fb92STejun Heo 47548a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 47558a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 4756bd31fb92STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 4757bd31fb92STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 4758bd31fb92STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 47598a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 47609e8cd2f5STejun Heo } else { 4761509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 47629e8cd2f5STejun Heo } 4763ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4764509b3204SDaniel Jordan 476564344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 476664344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 476764344553SZqiang */ 476864344553SZqiang if (ret) 476964344553SZqiang kthread_flush_worker(pwq_release_worker); 477064344553SZqiang 4771509b3204SDaniel Jordan return ret; 4772687a9aa5STejun Heo 4773687a9aa5STejun Heo enomem: 4774687a9aa5STejun Heo if (wq->cpu_pwq) { 47757b42f401SZqiang for_each_possible_cpu(cpu) { 47767b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 47777b42f401SZqiang 47787b42f401SZqiang if (pwq) 47797b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 47807b42f401SZqiang } 4781687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 4782687a9aa5STejun Heo wq->cpu_pwq = NULL; 4783687a9aa5STejun Heo } 4784687a9aa5STejun Heo return -ENOMEM; 47850f900049STejun Heo } 47860f900049STejun Heo 4787f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 4788f3421797STejun Heo const char *name) 4789b71ab8c2STejun Heo { 4790636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 4791044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 4792636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 4793b71ab8c2STejun Heo 4794636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 4795b71ab8c2STejun Heo } 4796b71ab8c2STejun Heo 4797983c7515STejun Heo /* 4798983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 4799983c7515STejun Heo * to guarantee forward progress. 4800983c7515STejun Heo */ 4801983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 4802983c7515STejun Heo { 4803983c7515STejun Heo struct worker *rescuer; 4804b92b36eaSDan Carpenter int ret; 4805983c7515STejun Heo 4806983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 4807983c7515STejun Heo return 0; 4808983c7515STejun Heo 4809983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 48104c0736a7SPetr Mladek if (!rescuer) { 48114c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 48124c0736a7SPetr Mladek wq->name); 4813983c7515STejun Heo return -ENOMEM; 48144c0736a7SPetr Mladek } 4815983c7515STejun Heo 4816983c7515STejun Heo rescuer->rescue_wq = wq; 4817b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 4818f187b697SSean Fu if (IS_ERR(rescuer->task)) { 4819b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 48204c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 48214c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 4822983c7515STejun Heo kfree(rescuer); 4823b92b36eaSDan Carpenter return ret; 4824983c7515STejun Heo } 4825983c7515STejun Heo 4826983c7515STejun Heo wq->rescuer = rescuer; 4827983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 4828983c7515STejun Heo wake_up_process(rescuer->task); 4829983c7515STejun Heo 4830983c7515STejun Heo return 0; 4831983c7515STejun Heo } 4832983c7515STejun Heo 483382e098f5STejun Heo /** 483482e098f5STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 483582e098f5STejun Heo * @wq: target workqueue 483682e098f5STejun Heo * 483782e098f5STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 483882e098f5STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 483982e098f5STejun Heo * @wq->max_active to zero. 484082e098f5STejun Heo */ 484182e098f5STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 484282e098f5STejun Heo { 48435f99fee6STejun Heo bool activated; 484482e098f5STejun Heo 484582e098f5STejun Heo lockdep_assert_held(&wq->mutex); 484682e098f5STejun Heo 484782e098f5STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 484882e098f5STejun Heo WRITE_ONCE(wq->max_active, 0); 484982e098f5STejun Heo return; 485082e098f5STejun Heo } 485182e098f5STejun Heo 485282e098f5STejun Heo if (wq->max_active == wq->saved_max_active) 485382e098f5STejun Heo return; 485482e098f5STejun Heo 485582e098f5STejun Heo /* 485682e098f5STejun Heo * Update @wq->max_active and then kick inactive work items if more 485782e098f5STejun Heo * active work items are allowed. This doesn't break work item ordering 485882e098f5STejun Heo * because new work items are always queued behind existing inactive 485982e098f5STejun Heo * work items if there are any. 486082e098f5STejun Heo */ 486182e098f5STejun Heo WRITE_ONCE(wq->max_active, wq->saved_max_active); 486282e098f5STejun Heo 48635f99fee6STejun Heo /* 48645f99fee6STejun Heo * Round-robin through pwq's activating the first inactive work item 48655f99fee6STejun Heo * until max_active is filled. 48665f99fee6STejun Heo */ 48675f99fee6STejun Heo do { 48685f99fee6STejun Heo struct pool_workqueue *pwq; 48695f99fee6STejun Heo 48705f99fee6STejun Heo activated = false; 487182e098f5STejun Heo for_each_pwq(pwq, wq) { 487282e098f5STejun Heo unsigned long flags; 487382e098f5STejun Heo 48745f99fee6STejun Heo /* can be called during early boot w/ irq disabled */ 487582e098f5STejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, flags); 48765f99fee6STejun Heo if (pwq_activate_first_inactive(pwq)) { 48775f99fee6STejun Heo activated = true; 487882e098f5STejun Heo kick_pool(pwq->pool); 48795f99fee6STejun Heo } 488082e098f5STejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 488182e098f5STejun Heo } 48825f99fee6STejun Heo } while (activated); 488382e098f5STejun Heo } 488482e098f5STejun Heo 4885a2775bbcSMathieu Malaterre __printf(1, 4) 4886669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 488797e37d7bSTejun Heo unsigned int flags, 4888669de8bdSBart Van Assche int max_active, ...) 48893af24433SOleg Nesterov { 4890ecf6881fSTejun Heo va_list args; 48913af24433SOleg Nesterov struct workqueue_struct *wq; 4892*b522229aSTejun Heo size_t wq_size; 4893*b522229aSTejun Heo int name_len; 4894b196be89STejun Heo 48955c0338c6STejun Heo /* 4896fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 4897fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 48985c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 4899fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 49005c0338c6STejun Heo */ 49015c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 49025c0338c6STejun Heo flags |= __WQ_ORDERED; 49035c0338c6STejun Heo 4904cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 4905cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 4906cee22a15SViresh Kumar flags |= WQ_UNBOUND; 4907cee22a15SViresh Kumar 4908ecf6881fSTejun Heo /* allocate wq and format name */ 4909*b522229aSTejun Heo if (flags & WQ_UNBOUND) 4910*b522229aSTejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 4911*b522229aSTejun Heo else 4912*b522229aSTejun Heo wq_size = sizeof(*wq); 4913*b522229aSTejun Heo 4914*b522229aSTejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 4915b196be89STejun Heo if (!wq) 4916d2c1d404STejun Heo return NULL; 4917b196be89STejun Heo 49186029a918STejun Heo if (flags & WQ_UNBOUND) { 4919be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 49206029a918STejun Heo if (!wq->unbound_attrs) 49216029a918STejun Heo goto err_free_wq; 49226029a918STejun Heo } 49236029a918STejun Heo 4924669de8bdSBart Van Assche va_start(args, max_active); 4925*b522229aSTejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 4926b196be89STejun Heo va_end(args); 49273af24433SOleg Nesterov 4928*b522229aSTejun Heo if (name_len >= WQ_NAME_LEN) 4929*b522229aSTejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 4930*b522229aSTejun Heo wq->name); 493143a181f8SAudra Mitchell 4932d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 4933b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 49343af24433SOleg Nesterov 4935b196be89STejun Heo /* init wq */ 493697e37d7bSTejun Heo wq->flags = flags; 493782e098f5STejun Heo wq->max_active = max_active; 4938a0a1a5fdSTejun Heo wq->saved_max_active = max_active; 49393c25a55dSLai Jiangshan mutex_init(&wq->mutex); 4940112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 494130cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 494273f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 494373f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 4944493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 49453af24433SOleg Nesterov 4946669de8bdSBart Van Assche wq_init_lockdep(wq); 4947cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 49483af24433SOleg Nesterov 4949*b522229aSTejun Heo if (flags & WQ_UNBOUND) { 4950*b522229aSTejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 495182efcab3SBart Van Assche goto err_unreg_lockdep; 4952*b522229aSTejun Heo } 4953*b522229aSTejun Heo 4954*b522229aSTejun Heo if (alloc_and_link_pwqs(wq) < 0) 4955*b522229aSTejun Heo goto err_free_node_nr_active; 49561537663fSTejun Heo 495740c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 4958d2c1d404STejun Heo goto err_destroy; 4959e22bee78STejun Heo 4960226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 4961226223abSTejun Heo goto err_destroy; 4962226223abSTejun Heo 49636af8bf3dSOleg Nesterov /* 496468e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 496568e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 496668e13a67SLai Jiangshan * list. 49676af8bf3dSOleg Nesterov */ 496868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 4969a0a1a5fdSTejun Heo 4970a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 497182e098f5STejun Heo wq_adjust_max_active(wq); 4972a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 4973a0a1a5fdSTejun Heo 4974e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 4975a0a1a5fdSTejun Heo 497668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 49773af24433SOleg Nesterov 49783af24433SOleg Nesterov return wq; 4979d2c1d404STejun Heo 4980*b522229aSTejun Heo err_free_node_nr_active: 4981*b522229aSTejun Heo if (wq->flags & WQ_UNBOUND) 4982*b522229aSTejun Heo free_node_nr_active(wq->node_nr_active); 498382efcab3SBart Van Assche err_unreg_lockdep: 4984009bb421SBart Van Assche wq_unregister_lockdep(wq); 4985009bb421SBart Van Assche wq_free_lockdep(wq); 498682efcab3SBart Van Assche err_free_wq: 49876029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 49884690c4abSTejun Heo kfree(wq); 4989d2c1d404STejun Heo return NULL; 4990d2c1d404STejun Heo err_destroy: 4991d2c1d404STejun Heo destroy_workqueue(wq); 49924690c4abSTejun Heo return NULL; 49931da177e4SLinus Torvalds } 4994669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 49951da177e4SLinus Torvalds 4996c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 4997c29eb853STejun Heo { 4998c29eb853STejun Heo int i; 4999c29eb853STejun Heo 5000c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5001c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5002c29eb853STejun Heo return true; 5003c29eb853STejun Heo 5004bd31fb92STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5005c29eb853STejun Heo return true; 5006bad184d2STejun Heo if (!pwq_is_empty(pwq)) 5007c29eb853STejun Heo return true; 5008c29eb853STejun Heo 5009c29eb853STejun Heo return false; 5010c29eb853STejun Heo } 5011c29eb853STejun Heo 50123af24433SOleg Nesterov /** 50133af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 50143af24433SOleg Nesterov * @wq: target workqueue 50153af24433SOleg Nesterov * 50163af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 50173af24433SOleg Nesterov */ 50183af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 50193af24433SOleg Nesterov { 502049e3cf44STejun Heo struct pool_workqueue *pwq; 5021636b927eSTejun Heo int cpu; 50223af24433SOleg Nesterov 5023def98c84STejun Heo /* 5024def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5025def98c84STejun Heo * lead to sysfs name conflicts. 5026def98c84STejun Heo */ 5027def98c84STejun Heo workqueue_sysfs_unregister(wq); 5028def98c84STejun Heo 502933e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 503033e3f0a3SRichard Clark mutex_lock(&wq->mutex); 503133e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 503233e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 503333e3f0a3SRichard Clark 50349c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 50359c5a2ba7STejun Heo drain_workqueue(wq); 5036c8efcc25STejun Heo 5037def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5038def98c84STejun Heo if (wq->rescuer) { 5039def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5040def98c84STejun Heo 5041def98c84STejun Heo /* this prevents new queueing */ 5042a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5043def98c84STejun Heo wq->rescuer = NULL; 5044a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5045def98c84STejun Heo 5046def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5047def98c84STejun Heo kthread_stop(rescuer->task); 50488efe1223STejun Heo kfree(rescuer); 5049def98c84STejun Heo } 5050def98c84STejun Heo 5051c29eb853STejun Heo /* 5052c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5053c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5054c29eb853STejun Heo */ 5055c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5056b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 505749e3cf44STejun Heo for_each_pwq(pwq, wq) { 5058a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5059c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 50601d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5061e66b39afSTejun Heo __func__, wq->name); 5062c29eb853STejun Heo show_pwq(pwq); 5063a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5064b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5065c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 506655df0933SImran Khan show_one_workqueue(wq); 50676183c009STejun Heo return; 506876af4d93STejun Heo } 5069a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 507076af4d93STejun Heo } 5071b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 50726183c009STejun Heo 5073a0a1a5fdSTejun Heo /* 5074a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5075a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5076a0a1a5fdSTejun Heo */ 5077e2dca7adSTejun Heo list_del_rcu(&wq->list); 507868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 50793af24433SOleg Nesterov 50808864b4e5STejun Heo /* 5081636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5082636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5083636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 50848864b4e5STejun Heo */ 5085636b927eSTejun Heo rcu_read_lock(); 5086636b927eSTejun Heo 5087636b927eSTejun Heo for_each_possible_cpu(cpu) { 5088bd31fb92STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 5089bd31fb92STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 50904c16bd32STejun Heo } 50914c16bd32STejun Heo 5092bd31fb92STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 5093bd31fb92STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5094636b927eSTejun Heo 5095636b927eSTejun Heo rcu_read_unlock(); 50963af24433SOleg Nesterov } 50973af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 50983af24433SOleg Nesterov 5099dcd989cbSTejun Heo /** 5100dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5101dcd989cbSTejun Heo * @wq: target workqueue 5102dcd989cbSTejun Heo * @max_active: new max_active value. 5103dcd989cbSTejun Heo * 5104dcd989cbSTejun Heo * Set max_active of @wq to @max_active. 5105dcd989cbSTejun Heo * 5106dcd989cbSTejun Heo * CONTEXT: 5107dcd989cbSTejun Heo * Don't call from IRQ context. 5108dcd989cbSTejun Heo */ 5109dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5110dcd989cbSTejun Heo { 51118719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 51120a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 51138719dceaSTejun Heo return; 51148719dceaSTejun Heo 5115f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5116dcd989cbSTejun Heo 5117a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5118dcd989cbSTejun Heo 51190a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 5120dcd989cbSTejun Heo wq->saved_max_active = max_active; 512182e098f5STejun Heo wq_adjust_max_active(wq); 5122dcd989cbSTejun Heo 5123a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5124dcd989cbSTejun Heo } 5125dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5126dcd989cbSTejun Heo 5127dcd989cbSTejun Heo /** 512827d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 512927d4ee03SLukas Wunner * 513027d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 513127d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 513227d4ee03SLukas Wunner * 513327d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 513427d4ee03SLukas Wunner */ 513527d4ee03SLukas Wunner struct work_struct *current_work(void) 513627d4ee03SLukas Wunner { 513727d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 513827d4ee03SLukas Wunner 513927d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 514027d4ee03SLukas Wunner } 514127d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 514227d4ee03SLukas Wunner 514327d4ee03SLukas Wunner /** 5144e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5145e6267616STejun Heo * 5146e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5147e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5148d185af30SYacine Belkadi * 5149d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5150e6267616STejun Heo */ 5151e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5152e6267616STejun Heo { 5153e6267616STejun Heo struct worker *worker = current_wq_worker(); 5154e6267616STejun Heo 51556a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5156e6267616STejun Heo } 5157e6267616STejun Heo 5158e6267616STejun Heo /** 5159dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5160dcd989cbSTejun Heo * @cpu: CPU in question 5161dcd989cbSTejun Heo * @wq: target workqueue 5162dcd989cbSTejun Heo * 5163dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5164dcd989cbSTejun Heo * no synchronization around this function and the test result is 5165dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5166dcd989cbSTejun Heo * 5167d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5168636b927eSTejun Heo * 5169636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5170636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5171636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5172636b927eSTejun Heo * other CPUs. 5173d3251859STejun Heo * 5174d185af30SYacine Belkadi * Return: 5175dcd989cbSTejun Heo * %true if congested, %false otherwise. 5176dcd989cbSTejun Heo */ 5177d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5178dcd989cbSTejun Heo { 51797fb98ea7STejun Heo struct pool_workqueue *pwq; 518076af4d93STejun Heo bool ret; 518176af4d93STejun Heo 518224acfb71SThomas Gleixner rcu_read_lock(); 518324acfb71SThomas Gleixner preempt_disable(); 51847fb98ea7STejun Heo 5185d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5186d3251859STejun Heo cpu = smp_processor_id(); 5187d3251859STejun Heo 5188687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5189f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5190636b927eSTejun Heo 519124acfb71SThomas Gleixner preempt_enable(); 519224acfb71SThomas Gleixner rcu_read_unlock(); 519376af4d93STejun Heo 519476af4d93STejun Heo return ret; 5195dcd989cbSTejun Heo } 5196dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5197dcd989cbSTejun Heo 5198dcd989cbSTejun Heo /** 5199dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5200dcd989cbSTejun Heo * @work: the work to be tested 5201dcd989cbSTejun Heo * 5202dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5203dcd989cbSTejun Heo * synchronization around this function and the test result is 5204dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5205dcd989cbSTejun Heo * 5206d185af30SYacine Belkadi * Return: 5207dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5208dcd989cbSTejun Heo */ 5209dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5210dcd989cbSTejun Heo { 5211fa1b54e6STejun Heo struct worker_pool *pool; 5212dcd989cbSTejun Heo unsigned long flags; 5213dcd989cbSTejun Heo unsigned int ret = 0; 5214dcd989cbSTejun Heo 5215dcd989cbSTejun Heo if (work_pending(work)) 5216dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5217038366c5SLai Jiangshan 521824acfb71SThomas Gleixner rcu_read_lock(); 5219fa1b54e6STejun Heo pool = get_work_pool(work); 5220038366c5SLai Jiangshan if (pool) { 5221a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 5222c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5223dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5224a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 5225038366c5SLai Jiangshan } 522624acfb71SThomas Gleixner rcu_read_unlock(); 5227dcd989cbSTejun Heo 5228dcd989cbSTejun Heo return ret; 5229dcd989cbSTejun Heo } 5230dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5231dcd989cbSTejun Heo 52323d1cb205STejun Heo /** 52333d1cb205STejun Heo * set_worker_desc - set description for the current work item 52343d1cb205STejun Heo * @fmt: printf-style format string 52353d1cb205STejun Heo * @...: arguments for the format string 52363d1cb205STejun Heo * 52373d1cb205STejun Heo * This function can be called by a running work function to describe what 52383d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 52393d1cb205STejun Heo * information will be printed out together to help debugging. The 52403d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 52413d1cb205STejun Heo */ 52423d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 52433d1cb205STejun Heo { 52443d1cb205STejun Heo struct worker *worker = current_wq_worker(); 52453d1cb205STejun Heo va_list args; 52463d1cb205STejun Heo 52473d1cb205STejun Heo if (worker) { 52483d1cb205STejun Heo va_start(args, fmt); 52493d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 52503d1cb205STejun Heo va_end(args); 52513d1cb205STejun Heo } 52523d1cb205STejun Heo } 52535c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 52543d1cb205STejun Heo 52553d1cb205STejun Heo /** 52563d1cb205STejun Heo * print_worker_info - print out worker information and description 52573d1cb205STejun Heo * @log_lvl: the log level to use when printing 52583d1cb205STejun Heo * @task: target task 52593d1cb205STejun Heo * 52603d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 52613d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 52623d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 52633d1cb205STejun Heo * 52643d1cb205STejun Heo * This function can be safely called on any task as long as the 52653d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 52663d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 52673d1cb205STejun Heo */ 52683d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 52693d1cb205STejun Heo { 52703d1cb205STejun Heo work_func_t *fn = NULL; 52713d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 52723d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 52733d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 52743d1cb205STejun Heo struct workqueue_struct *wq = NULL; 52753d1cb205STejun Heo struct worker *worker; 52763d1cb205STejun Heo 52773d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 52783d1cb205STejun Heo return; 52793d1cb205STejun Heo 52803d1cb205STejun Heo /* 52813d1cb205STejun Heo * This function is called without any synchronization and @task 52823d1cb205STejun Heo * could be in any state. Be careful with dereferences. 52833d1cb205STejun Heo */ 5284e700591aSPetr Mladek worker = kthread_probe_data(task); 52853d1cb205STejun Heo 52863d1cb205STejun Heo /* 52878bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 52888bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 52893d1cb205STejun Heo */ 5290fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5291fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5292fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5293fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5294fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 52953d1cb205STejun Heo 52963d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5297d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 52988bf89593STejun Heo if (strcmp(name, desc)) 52993d1cb205STejun Heo pr_cont(" (%s)", desc); 53003d1cb205STejun Heo pr_cont("\n"); 53013d1cb205STejun Heo } 53023d1cb205STejun Heo } 53033d1cb205STejun Heo 53043494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 53053494fc30STejun Heo { 53063494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 53073494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 53083494fc30STejun Heo pr_cont(" node=%d", pool->node); 53093494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 53103494fc30STejun Heo } 53113494fc30STejun Heo 5312c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5313c76feb0dSPaul E. McKenney bool comma; 5314c76feb0dSPaul E. McKenney work_func_t func; 5315c76feb0dSPaul E. McKenney long ctr; 5316c76feb0dSPaul E. McKenney }; 5317c76feb0dSPaul E. McKenney 5318c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5319c76feb0dSPaul E. McKenney { 5320c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5321c76feb0dSPaul E. McKenney goto out_record; 5322c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5323c76feb0dSPaul E. McKenney pcwsp->ctr++; 5324c76feb0dSPaul E. McKenney return; 5325c76feb0dSPaul E. McKenney } 5326c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5327c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5328c76feb0dSPaul E. McKenney else 5329c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5330c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5331c76feb0dSPaul E. McKenney out_record: 5332c76feb0dSPaul E. McKenney if ((long)func == -1L) 5333c76feb0dSPaul E. McKenney return; 5334c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5335c76feb0dSPaul E. McKenney pcwsp->func = func; 5336c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5337c76feb0dSPaul E. McKenney } 5338c76feb0dSPaul E. McKenney 5339c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 53403494fc30STejun Heo { 53413494fc30STejun Heo if (work->func == wq_barrier_func) { 53423494fc30STejun Heo struct wq_barrier *barr; 53433494fc30STejun Heo 53443494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 53453494fc30STejun Heo 5346c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 53473494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 53483494fc30STejun Heo task_pid_nr(barr->task)); 53493494fc30STejun Heo } else { 5350c76feb0dSPaul E. McKenney if (!comma) 5351c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5352c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 53533494fc30STejun Heo } 53543494fc30STejun Heo } 53553494fc30STejun Heo 53563494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 53573494fc30STejun Heo { 5358c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 53593494fc30STejun Heo struct worker_pool *pool = pwq->pool; 53603494fc30STejun Heo struct work_struct *work; 53613494fc30STejun Heo struct worker *worker; 53623494fc30STejun Heo bool has_in_flight = false, has_pending = false; 53633494fc30STejun Heo int bkt; 53643494fc30STejun Heo 53653494fc30STejun Heo pr_info(" pwq %d:", pool->id); 53663494fc30STejun Heo pr_cont_pool_info(pool); 53673494fc30STejun Heo 536882e098f5STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 536982e098f5STejun Heo pwq->nr_active, pwq->refcnt, 53703494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 53713494fc30STejun Heo 53723494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 53733494fc30STejun Heo if (worker->current_pwq == pwq) { 53743494fc30STejun Heo has_in_flight = true; 53753494fc30STejun Heo break; 53763494fc30STejun Heo } 53773494fc30STejun Heo } 53783494fc30STejun Heo if (has_in_flight) { 53793494fc30STejun Heo bool comma = false; 53803494fc30STejun Heo 53813494fc30STejun Heo pr_info(" in-flight:"); 53823494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 53833494fc30STejun Heo if (worker->current_pwq != pwq) 53843494fc30STejun Heo continue; 53853494fc30STejun Heo 5386d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 53873494fc30STejun Heo task_pid_nr(worker->task), 538830ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 53893494fc30STejun Heo worker->current_func); 53903494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5391c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5392c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 53933494fc30STejun Heo comma = true; 53943494fc30STejun Heo } 53953494fc30STejun Heo pr_cont("\n"); 53963494fc30STejun Heo } 53973494fc30STejun Heo 53983494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 53993494fc30STejun Heo if (get_work_pwq(work) == pwq) { 54003494fc30STejun Heo has_pending = true; 54013494fc30STejun Heo break; 54023494fc30STejun Heo } 54033494fc30STejun Heo } 54043494fc30STejun Heo if (has_pending) { 54053494fc30STejun Heo bool comma = false; 54063494fc30STejun Heo 54073494fc30STejun Heo pr_info(" pending:"); 54083494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 54093494fc30STejun Heo if (get_work_pwq(work) != pwq) 54103494fc30STejun Heo continue; 54113494fc30STejun Heo 5412c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 54133494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 54143494fc30STejun Heo } 5415c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 54163494fc30STejun Heo pr_cont("\n"); 54173494fc30STejun Heo } 54183494fc30STejun Heo 5419f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 54203494fc30STejun Heo bool comma = false; 54213494fc30STejun Heo 5422f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5423f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5424c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 54253494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 54263494fc30STejun Heo } 5427c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 54283494fc30STejun Heo pr_cont("\n"); 54293494fc30STejun Heo } 54303494fc30STejun Heo } 54313494fc30STejun Heo 54323494fc30STejun Heo /** 543355df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 543455df0933SImran Khan * @wq: workqueue whose state will be printed 54353494fc30STejun Heo */ 543655df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 54373494fc30STejun Heo { 54383494fc30STejun Heo struct pool_workqueue *pwq; 54393494fc30STejun Heo bool idle = true; 544055df0933SImran Khan unsigned long flags; 54413494fc30STejun Heo 54423494fc30STejun Heo for_each_pwq(pwq, wq) { 5443bad184d2STejun Heo if (!pwq_is_empty(pwq)) { 54443494fc30STejun Heo idle = false; 54453494fc30STejun Heo break; 54463494fc30STejun Heo } 54473494fc30STejun Heo } 544855df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 544955df0933SImran Khan return; 54503494fc30STejun Heo 54513494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 54523494fc30STejun Heo 54533494fc30STejun Heo for_each_pwq(pwq, wq) { 5454a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 5455bad184d2STejun Heo if (!pwq_is_empty(pwq)) { 545657116ce1SJohan Hovold /* 545757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 545857116ce1SJohan Hovold * drivers that queue work while holding locks 545957116ce1SJohan Hovold * also taken in their write paths. 546057116ce1SJohan Hovold */ 546157116ce1SJohan Hovold printk_deferred_enter(); 54623494fc30STejun Heo show_pwq(pwq); 546357116ce1SJohan Hovold printk_deferred_exit(); 546457116ce1SJohan Hovold } 5465a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 546662635ea8SSergey Senozhatsky /* 546762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 546855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 546962635ea8SSergey Senozhatsky * hard lockup. 547062635ea8SSergey Senozhatsky */ 547162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 54723494fc30STejun Heo } 547355df0933SImran Khan 54743494fc30STejun Heo } 54753494fc30STejun Heo 547655df0933SImran Khan /** 547755df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 547855df0933SImran Khan * @pool: worker pool whose state will be printed 547955df0933SImran Khan */ 548055df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 548155df0933SImran Khan { 54823494fc30STejun Heo struct worker *worker; 54833494fc30STejun Heo bool first = true; 548455df0933SImran Khan unsigned long flags; 5485335a42ebSPetr Mladek unsigned long hung = 0; 54863494fc30STejun Heo 5487a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 54883494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 54893494fc30STejun Heo goto next_pool; 5490335a42ebSPetr Mladek 5491335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5492335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5493335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5494335a42ebSPetr Mladek 549557116ce1SJohan Hovold /* 549657116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 549757116ce1SJohan Hovold * queue work while holding locks also taken in their write 549857116ce1SJohan Hovold * paths. 549957116ce1SJohan Hovold */ 550057116ce1SJohan Hovold printk_deferred_enter(); 55013494fc30STejun Heo pr_info("pool %d:", pool->id); 55023494fc30STejun Heo pr_cont_pool_info(pool); 5503335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 55043494fc30STejun Heo if (pool->manager) 55053494fc30STejun Heo pr_cont(" manager: %d", 55063494fc30STejun Heo task_pid_nr(pool->manager->task)); 55073494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 55083494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 55093494fc30STejun Heo task_pid_nr(worker->task)); 55103494fc30STejun Heo first = false; 55113494fc30STejun Heo } 55123494fc30STejun Heo pr_cont("\n"); 551357116ce1SJohan Hovold printk_deferred_exit(); 55143494fc30STejun Heo next_pool: 5515a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 551662635ea8SSergey Senozhatsky /* 551762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 551855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 551962635ea8SSergey Senozhatsky * hard lockup. 552062635ea8SSergey Senozhatsky */ 552162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 552255df0933SImran Khan 55233494fc30STejun Heo } 55243494fc30STejun Heo 552555df0933SImran Khan /** 552655df0933SImran Khan * show_all_workqueues - dump workqueue state 552755df0933SImran Khan * 5528704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 552955df0933SImran Khan */ 553055df0933SImran Khan void show_all_workqueues(void) 553155df0933SImran Khan { 553255df0933SImran Khan struct workqueue_struct *wq; 553355df0933SImran Khan struct worker_pool *pool; 553455df0933SImran Khan int pi; 553555df0933SImran Khan 553655df0933SImran Khan rcu_read_lock(); 553755df0933SImran Khan 553855df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 553955df0933SImran Khan 554055df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 554155df0933SImran Khan show_one_workqueue(wq); 554255df0933SImran Khan 554355df0933SImran Khan for_each_pool(pool, pi) 554455df0933SImran Khan show_one_worker_pool(pool); 554555df0933SImran Khan 554624acfb71SThomas Gleixner rcu_read_unlock(); 55473494fc30STejun Heo } 55483494fc30STejun Heo 5549704bc669SJungseung Lee /** 5550704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5551704bc669SJungseung Lee * 5552704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5553704bc669SJungseung Lee * still busy. 5554704bc669SJungseung Lee */ 5555704bc669SJungseung Lee void show_freezable_workqueues(void) 5556704bc669SJungseung Lee { 5557704bc669SJungseung Lee struct workqueue_struct *wq; 5558704bc669SJungseung Lee 5559704bc669SJungseung Lee rcu_read_lock(); 5560704bc669SJungseung Lee 5561704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5562704bc669SJungseung Lee 5563704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5564704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5565704bc669SJungseung Lee continue; 5566704bc669SJungseung Lee show_one_workqueue(wq); 5567704bc669SJungseung Lee } 5568704bc669SJungseung Lee 5569704bc669SJungseung Lee rcu_read_unlock(); 5570704bc669SJungseung Lee } 5571704bc669SJungseung Lee 55726b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 55736b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 55746b59808bSTejun Heo { 55756b59808bSTejun Heo int off; 55766b59808bSTejun Heo 55776b59808bSTejun Heo /* always show the actual comm */ 55786b59808bSTejun Heo off = strscpy(buf, task->comm, size); 55796b59808bSTejun Heo if (off < 0) 55806b59808bSTejun Heo return; 55816b59808bSTejun Heo 5582197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 55836b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 55846b59808bSTejun Heo 5585197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5586197f6accSTejun Heo struct worker *worker = kthread_data(task); 5587197f6accSTejun Heo struct worker_pool *pool = worker->pool; 55886b59808bSTejun Heo 55896b59808bSTejun Heo if (pool) { 5590a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 55916b59808bSTejun Heo /* 5592197f6accSTejun Heo * ->desc tracks information (wq name or 5593197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5594197f6accSTejun Heo * current, prepend '+', otherwise '-'. 55956b59808bSTejun Heo */ 55966b59808bSTejun Heo if (worker->desc[0] != '\0') { 55976b59808bSTejun Heo if (worker->current_work) 55986b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 55996b59808bSTejun Heo worker->desc); 56006b59808bSTejun Heo else 56016b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 56026b59808bSTejun Heo worker->desc); 56036b59808bSTejun Heo } 5604a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 56056b59808bSTejun Heo } 5606197f6accSTejun Heo } 56076b59808bSTejun Heo 56086b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 56096b59808bSTejun Heo } 56106b59808bSTejun Heo 561166448bc2SMathieu Malaterre #ifdef CONFIG_SMP 561266448bc2SMathieu Malaterre 5613db7bccf4STejun Heo /* 5614db7bccf4STejun Heo * CPU hotplug. 5615db7bccf4STejun Heo * 5616e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5617112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5618706026c2STejun Heo * pool which make migrating pending and scheduled works very 5619e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 562094cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5621e22bee78STejun Heo * blocked draining impractical. 5622e22bee78STejun Heo * 562324647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5624628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5625628c78e7STejun Heo * cpu comes back online. 5626db7bccf4STejun Heo */ 5627db7bccf4STejun Heo 5628e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5629db7bccf4STejun Heo { 56304ce62e9eSTejun Heo struct worker_pool *pool; 5631db7bccf4STejun Heo struct worker *worker; 5632db7bccf4STejun Heo 5633f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 56341258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5635a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5636e22bee78STejun Heo 5637f2d5a0eeSTejun Heo /* 563892f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 563994cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 564011b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5641b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5642b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5643b4ac9384SLai Jiangshan * is on the same cpu. 5644f2d5a0eeSTejun Heo */ 5645da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5646403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5647db7bccf4STejun Heo 564824647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5649f2d5a0eeSTejun Heo 5650e22bee78STejun Heo /* 5651989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5652989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5653989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5654989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5655989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5656eb283428SLai Jiangshan * are served by workers tied to the pool. 5657e22bee78STejun Heo */ 5658bc35f7efSLai Jiangshan pool->nr_running = 0; 5659eb283428SLai Jiangshan 5660eb283428SLai Jiangshan /* 5661eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5662eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5663eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5664eb283428SLai Jiangshan */ 56650219a352STejun Heo kick_pool(pool); 5666989442d7SLai Jiangshan 5667a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5668989442d7SLai Jiangshan 5669793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5670793777bcSValentin Schneider unbind_worker(worker); 5671989442d7SLai Jiangshan 5672989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5673eb283428SLai Jiangshan } 5674db7bccf4STejun Heo } 5675db7bccf4STejun Heo 5676bd7c089eSTejun Heo /** 5677bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5678bd7c089eSTejun Heo * @pool: pool of interest 5679bd7c089eSTejun Heo * 5680a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5681bd7c089eSTejun Heo */ 5682bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5683bd7c089eSTejun Heo { 5684a9ab775bSTejun Heo struct worker *worker; 5685bd7c089eSTejun Heo 56861258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5687bd7c089eSTejun Heo 5688bd7c089eSTejun Heo /* 5689a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5690a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5691402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5692a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5693a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5694bd7c089eSTejun Heo */ 5695c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5696c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5697c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 56989546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 5699c63a2e52SValentin Schneider } 5700a9ab775bSTejun Heo 5701a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5702f7c17d26SWanpeng Li 57033de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5704a9ab775bSTejun Heo 5705da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5706a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5707a9ab775bSTejun Heo 5708a9ab775bSTejun Heo /* 5709a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5710a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5711a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5712a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5713a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5714a9ab775bSTejun Heo * concurrency management. Note that when or whether 5715a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5716a9ab775bSTejun Heo * 5717c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 5718a9ab775bSTejun Heo * tested without holding any lock in 57196d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 5720a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 5721a9ab775bSTejun Heo * management operations. 5722bd7c089eSTejun Heo */ 5723a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 5724a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 5725a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 5726c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 5727bd7c089eSTejun Heo } 5728a9ab775bSTejun Heo 5729a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5730bd7c089eSTejun Heo } 5731bd7c089eSTejun Heo 57327dbc725eSTejun Heo /** 57337dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 57347dbc725eSTejun Heo * @pool: unbound pool of interest 57357dbc725eSTejun Heo * @cpu: the CPU which is coming up 57367dbc725eSTejun Heo * 57377dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 57387dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 57397dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 57407dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 57417dbc725eSTejun Heo */ 57427dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 57437dbc725eSTejun Heo { 57447dbc725eSTejun Heo static cpumask_t cpumask; 57457dbc725eSTejun Heo struct worker *worker; 57467dbc725eSTejun Heo 57471258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 57487dbc725eSTejun Heo 57497dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 57507dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 57517dbc725eSTejun Heo return; 57527dbc725eSTejun Heo 57537dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 57547dbc725eSTejun Heo 57557dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 5756da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5757d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 57587dbc725eSTejun Heo } 57597dbc725eSTejun Heo 57607ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 57611da177e4SLinus Torvalds { 57624ce62e9eSTejun Heo struct worker_pool *pool; 57631da177e4SLinus Torvalds 5764f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 57653ce63377STejun Heo if (pool->nr_workers) 57663ce63377STejun Heo continue; 5767051e1850SLai Jiangshan if (!create_worker(pool)) 57687ee681b2SThomas Gleixner return -ENOMEM; 57693af24433SOleg Nesterov } 57707ee681b2SThomas Gleixner return 0; 57717ee681b2SThomas Gleixner } 57721da177e4SLinus Torvalds 57737ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 57747ee681b2SThomas Gleixner { 57757ee681b2SThomas Gleixner struct worker_pool *pool; 57767ee681b2SThomas Gleixner struct workqueue_struct *wq; 57777ee681b2SThomas Gleixner int pi; 57787ee681b2SThomas Gleixner 577968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 57807dbc725eSTejun Heo 57817dbc725eSTejun Heo for_each_pool(pool, pi) { 57821258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 578394cf58bbSTejun Heo 5784f05b558dSLai Jiangshan if (pool->cpu == cpu) 578594cf58bbSTejun Heo rebind_workers(pool); 5786f05b558dSLai Jiangshan else if (pool->cpu < 0) 57877dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 578894cf58bbSTejun Heo 57891258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 579094cf58bbSTejun Heo } 57917dbc725eSTejun Heo 5792fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 57934cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 579484193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 579584193c07STejun Heo 579684193c07STejun Heo if (attrs) { 579784193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 57984cbfd3deSTejun Heo int tcpu; 57994cbfd3deSTejun Heo 580084193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5801fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 58024cbfd3deSTejun Heo } 58034cbfd3deSTejun Heo } 58044c16bd32STejun Heo 580568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 58067ee681b2SThomas Gleixner return 0; 580765758202STejun Heo } 580865758202STejun Heo 58097ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 581065758202STejun Heo { 58114c16bd32STejun Heo struct workqueue_struct *wq; 58128db25e78STejun Heo 58134c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 5814e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 5815e8b3f8dbSLai Jiangshan return -1; 5816e8b3f8dbSLai Jiangshan 5817e8b3f8dbSLai Jiangshan unbind_workers(cpu); 58184c16bd32STejun Heo 5819fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 58204c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 58214cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 582284193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 582384193c07STejun Heo 582484193c07STejun Heo if (attrs) { 582584193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 58264cbfd3deSTejun Heo int tcpu; 58274cbfd3deSTejun Heo 582884193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 5829fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 58304cbfd3deSTejun Heo } 58314cbfd3deSTejun Heo } 58324c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 58334c16bd32STejun Heo 58347ee681b2SThomas Gleixner return 0; 583565758202STejun Heo } 583665758202STejun Heo 58372d3854a3SRusty Russell struct work_for_cpu { 5838ed48ece2STejun Heo struct work_struct work; 58392d3854a3SRusty Russell long (*fn)(void *); 58402d3854a3SRusty Russell void *arg; 58412d3854a3SRusty Russell long ret; 58422d3854a3SRusty Russell }; 58432d3854a3SRusty Russell 5844ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 58452d3854a3SRusty Russell { 5846ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 5847ed48ece2STejun Heo 58482d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 58492d3854a3SRusty Russell } 58502d3854a3SRusty Russell 58512d3854a3SRusty Russell /** 5852be2355b7SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 58532d3854a3SRusty Russell * @cpu: the cpu to run on 58542d3854a3SRusty Russell * @fn: the function to run 58552d3854a3SRusty Russell * @arg: the function arg 5856be2355b7SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 58572d3854a3SRusty Russell * 585831ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 58596b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 5860d185af30SYacine Belkadi * 5861d185af30SYacine Belkadi * Return: The value @fn returns. 58622d3854a3SRusty Russell */ 5863be2355b7SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 5864be2355b7SFrederic Weisbecker void *arg, struct lock_class_key *key) 58652d3854a3SRusty Russell { 5866ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 58672d3854a3SRusty Russell 5868be2355b7SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 5869ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 587012997d1aSBjorn Helgaas flush_work(&wfc.work); 5871440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 58722d3854a3SRusty Russell return wfc.ret; 58732d3854a3SRusty Russell } 5874be2355b7SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 58750e8d6a93SThomas Gleixner 58760e8d6a93SThomas Gleixner /** 5877be2355b7SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 58780e8d6a93SThomas Gleixner * @cpu: the cpu to run on 58790e8d6a93SThomas Gleixner * @fn: the function to run 58800e8d6a93SThomas Gleixner * @arg: the function argument 5881be2355b7SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 58820e8d6a93SThomas Gleixner * 58830e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 58840e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 58850e8d6a93SThomas Gleixner * 58860e8d6a93SThomas Gleixner * Return: The value @fn returns. 58870e8d6a93SThomas Gleixner */ 5888be2355b7SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 5889be2355b7SFrederic Weisbecker void *arg, struct lock_class_key *key) 58900e8d6a93SThomas Gleixner { 58910e8d6a93SThomas Gleixner long ret = -ENODEV; 58920e8d6a93SThomas Gleixner 5893ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 58940e8d6a93SThomas Gleixner if (cpu_online(cpu)) 5895be2355b7SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 5896ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 58970e8d6a93SThomas Gleixner return ret; 58980e8d6a93SThomas Gleixner } 5899be2355b7SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 59002d3854a3SRusty Russell #endif /* CONFIG_SMP */ 59012d3854a3SRusty Russell 5902a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 5903e7577c50SRusty Russell 5904a0a1a5fdSTejun Heo /** 5905a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 5906a0a1a5fdSTejun Heo * 590758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 5908f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 5909706026c2STejun Heo * pool->worklist. 5910a0a1a5fdSTejun Heo * 5911a0a1a5fdSTejun Heo * CONTEXT: 5912a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5913a0a1a5fdSTejun Heo */ 5914a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 5915a0a1a5fdSTejun Heo { 591624b8a847STejun Heo struct workqueue_struct *wq; 5917a0a1a5fdSTejun Heo 591868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5919a0a1a5fdSTejun Heo 59206183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 5921a0a1a5fdSTejun Heo workqueue_freezing = true; 5922a0a1a5fdSTejun Heo 592324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 5924a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 592582e098f5STejun Heo wq_adjust_max_active(wq); 5926a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5927a1056305STejun Heo } 59285bcab335STejun Heo 592968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5930a0a1a5fdSTejun Heo } 5931a0a1a5fdSTejun Heo 5932a0a1a5fdSTejun Heo /** 593358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 5934a0a1a5fdSTejun Heo * 5935a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 5936a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 5937a0a1a5fdSTejun Heo * 5938a0a1a5fdSTejun Heo * CONTEXT: 593968e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 5940a0a1a5fdSTejun Heo * 5941d185af30SYacine Belkadi * Return: 594258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 594358a69cb4STejun Heo * is complete. 5944a0a1a5fdSTejun Heo */ 5945a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 5946a0a1a5fdSTejun Heo { 5947a0a1a5fdSTejun Heo bool busy = false; 594824b8a847STejun Heo struct workqueue_struct *wq; 594924b8a847STejun Heo struct pool_workqueue *pwq; 5950a0a1a5fdSTejun Heo 595168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5952a0a1a5fdSTejun Heo 59536183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 5954a0a1a5fdSTejun Heo 595524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 595624b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 595724b8a847STejun Heo continue; 5958a0a1a5fdSTejun Heo /* 5959a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 5960a0a1a5fdSTejun Heo * to peek without lock. 5961a0a1a5fdSTejun Heo */ 596224acfb71SThomas Gleixner rcu_read_lock(); 596324b8a847STejun Heo for_each_pwq(pwq, wq) { 59646183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 5965112202d9STejun Heo if (pwq->nr_active) { 5966a0a1a5fdSTejun Heo busy = true; 596724acfb71SThomas Gleixner rcu_read_unlock(); 5968a0a1a5fdSTejun Heo goto out_unlock; 5969a0a1a5fdSTejun Heo } 5970a0a1a5fdSTejun Heo } 597124acfb71SThomas Gleixner rcu_read_unlock(); 5972a0a1a5fdSTejun Heo } 5973a0a1a5fdSTejun Heo out_unlock: 597468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 5975a0a1a5fdSTejun Heo return busy; 5976a0a1a5fdSTejun Heo } 5977a0a1a5fdSTejun Heo 5978a0a1a5fdSTejun Heo /** 5979a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 5980a0a1a5fdSTejun Heo * 5981a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 5982706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 5983a0a1a5fdSTejun Heo * 5984a0a1a5fdSTejun Heo * CONTEXT: 5985a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 5986a0a1a5fdSTejun Heo */ 5987a0a1a5fdSTejun Heo void thaw_workqueues(void) 5988a0a1a5fdSTejun Heo { 598924b8a847STejun Heo struct workqueue_struct *wq; 5990a0a1a5fdSTejun Heo 599168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5992a0a1a5fdSTejun Heo 5993a0a1a5fdSTejun Heo if (!workqueue_freezing) 5994a0a1a5fdSTejun Heo goto out_unlock; 5995a0a1a5fdSTejun Heo 599674b414eaSLai Jiangshan workqueue_freezing = false; 599724b8a847STejun Heo 599824b8a847STejun Heo /* restore max_active and repopulate worklist */ 599924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6000a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 600182e098f5STejun Heo wq_adjust_max_active(wq); 6002a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 600324b8a847STejun Heo } 600424b8a847STejun Heo 6005a0a1a5fdSTejun Heo out_unlock: 600668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6007a0a1a5fdSTejun Heo } 6008a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6009a0a1a5fdSTejun Heo 601099c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6011042f7df1SLai Jiangshan { 6012042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6013042f7df1SLai Jiangshan int ret = 0; 6014042f7df1SLai Jiangshan struct workqueue_struct *wq; 6015042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6016042f7df1SLai Jiangshan 6017042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6018042f7df1SLai Jiangshan 6019042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 6020042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 6021042f7df1SLai Jiangshan continue; 6022042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 60235ad73e10STejun Heo if (wq->flags & __WQ_ORDERED) 6024042f7df1SLai Jiangshan continue; 6025042f7df1SLai Jiangshan 602699c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 602784193c07STejun Heo if (IS_ERR(ctx)) { 602884193c07STejun Heo ret = PTR_ERR(ctx); 6029042f7df1SLai Jiangshan break; 6030042f7df1SLai Jiangshan } 6031042f7df1SLai Jiangshan 6032042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6033042f7df1SLai Jiangshan } 6034042f7df1SLai Jiangshan 6035042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6036042f7df1SLai Jiangshan if (!ret) 6037042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6038042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6039042f7df1SLai Jiangshan } 6040042f7df1SLai Jiangshan 604199c621efSLai Jiangshan if (!ret) { 604299c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 604399c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 604499c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 604599c621efSLai Jiangshan } 6046042f7df1SLai Jiangshan return ret; 6047042f7df1SLai Jiangshan } 6048042f7df1SLai Jiangshan 6049042f7df1SLai Jiangshan /** 6050042f7df1SLai Jiangshan * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 6051042f7df1SLai Jiangshan * @cpumask: the cpumask to set 6052042f7df1SLai Jiangshan * 6053042f7df1SLai Jiangshan * The low-level workqueues cpumask is a global cpumask that limits 6054042f7df1SLai Jiangshan * the affinity of all unbound workqueues. This function check the @cpumask 6055042f7df1SLai Jiangshan * and apply it to all unbound workqueues and updates all pwqs of them. 6056042f7df1SLai Jiangshan * 605767dc8325SCai Huoqing * Return: 0 - Success 6058042f7df1SLai Jiangshan * -EINVAL - Invalid @cpumask 6059042f7df1SLai Jiangshan * -ENOMEM - Failed to allocate memory for attrs or pwqs. 6060042f7df1SLai Jiangshan */ 6061042f7df1SLai Jiangshan int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 6062042f7df1SLai Jiangshan { 6063042f7df1SLai Jiangshan int ret = -EINVAL; 6064042f7df1SLai Jiangshan 6065c98a9805STal Shorer /* 6066c98a9805STal Shorer * Not excluding isolated cpus on purpose. 6067c98a9805STal Shorer * If the user wishes to include them, we allow that. 6068c98a9805STal Shorer */ 6069042f7df1SLai Jiangshan cpumask_and(cpumask, cpumask, cpu_possible_mask); 6070042f7df1SLai Jiangshan if (!cpumask_empty(cpumask)) { 6071a0111cf6SLai Jiangshan apply_wqattrs_lock(); 6072d25302e4SMenglong Dong if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 6073d25302e4SMenglong Dong ret = 0; 6074d25302e4SMenglong Dong goto out_unlock; 6075d25302e4SMenglong Dong } 6076d25302e4SMenglong Dong 607799c621efSLai Jiangshan ret = workqueue_apply_unbound_cpumask(cpumask); 6078042f7df1SLai Jiangshan 6079d25302e4SMenglong Dong out_unlock: 6080a0111cf6SLai Jiangshan apply_wqattrs_unlock(); 6081042f7df1SLai Jiangshan } 6082042f7df1SLai Jiangshan 6083042f7df1SLai Jiangshan return ret; 6084042f7df1SLai Jiangshan } 6085042f7df1SLai Jiangshan 608663c5484eSTejun Heo static int parse_affn_scope(const char *val) 608763c5484eSTejun Heo { 608863c5484eSTejun Heo int i; 608963c5484eSTejun Heo 609063c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 609163c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 609263c5484eSTejun Heo return i; 609363c5484eSTejun Heo } 609463c5484eSTejun Heo return -EINVAL; 609563c5484eSTejun Heo } 609663c5484eSTejun Heo 609763c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 609863c5484eSTejun Heo { 6099523a301eSTejun Heo struct workqueue_struct *wq; 6100523a301eSTejun Heo int affn, cpu; 610163c5484eSTejun Heo 610263c5484eSTejun Heo affn = parse_affn_scope(val); 610363c5484eSTejun Heo if (affn < 0) 610463c5484eSTejun Heo return affn; 6105523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6106523a301eSTejun Heo return -EINVAL; 6107523a301eSTejun Heo 6108523a301eSTejun Heo cpus_read_lock(); 6109523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 611063c5484eSTejun Heo 611163c5484eSTejun Heo wq_affn_dfl = affn; 6112523a301eSTejun Heo 6113523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6114523a301eSTejun Heo for_each_online_cpu(cpu) { 6115523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6116523a301eSTejun Heo } 6117523a301eSTejun Heo } 6118523a301eSTejun Heo 6119523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6120523a301eSTejun Heo cpus_read_unlock(); 6121523a301eSTejun Heo 612263c5484eSTejun Heo return 0; 612363c5484eSTejun Heo } 612463c5484eSTejun Heo 612563c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 612663c5484eSTejun Heo { 612763c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 612863c5484eSTejun Heo } 612963c5484eSTejun Heo 613063c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 613163c5484eSTejun Heo .set = wq_affn_dfl_set, 613263c5484eSTejun Heo .get = wq_affn_dfl_get, 613363c5484eSTejun Heo }; 613463c5484eSTejun Heo 613563c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 613663c5484eSTejun Heo 61376ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 61386ba94429SFrederic Weisbecker /* 61396ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 61406ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 61416ba94429SFrederic Weisbecker * following attributes. 61426ba94429SFrederic Weisbecker * 61436ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 61446ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 61456ba94429SFrederic Weisbecker * 61466ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 61476ba94429SFrederic Weisbecker * 61486ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 61496ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 615063c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 61518639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 61526ba94429SFrederic Weisbecker */ 61536ba94429SFrederic Weisbecker struct wq_device { 61546ba94429SFrederic Weisbecker struct workqueue_struct *wq; 61556ba94429SFrederic Weisbecker struct device dev; 61566ba94429SFrederic Weisbecker }; 61576ba94429SFrederic Weisbecker 61586ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 61596ba94429SFrederic Weisbecker { 61606ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 61616ba94429SFrederic Weisbecker 61626ba94429SFrederic Weisbecker return wq_dev->wq; 61636ba94429SFrederic Weisbecker } 61646ba94429SFrederic Weisbecker 61656ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 61666ba94429SFrederic Weisbecker char *buf) 61676ba94429SFrederic Weisbecker { 61686ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 61696ba94429SFrederic Weisbecker 61706ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 61716ba94429SFrederic Weisbecker } 61726ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 61736ba94429SFrederic Weisbecker 61746ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 61756ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 61766ba94429SFrederic Weisbecker { 61776ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 61786ba94429SFrederic Weisbecker 61796ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 61806ba94429SFrederic Weisbecker } 61816ba94429SFrederic Weisbecker 61826ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 61836ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 61846ba94429SFrederic Weisbecker size_t count) 61856ba94429SFrederic Weisbecker { 61866ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 61876ba94429SFrederic Weisbecker int val; 61886ba94429SFrederic Weisbecker 61896ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 61906ba94429SFrederic Weisbecker return -EINVAL; 61916ba94429SFrederic Weisbecker 61926ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 61936ba94429SFrederic Weisbecker return count; 61946ba94429SFrederic Weisbecker } 61956ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 61966ba94429SFrederic Weisbecker 61976ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 61986ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 61996ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 62006ba94429SFrederic Weisbecker NULL, 62016ba94429SFrederic Weisbecker }; 62026ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 62036ba94429SFrederic Weisbecker 62046ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 62056ba94429SFrederic Weisbecker char *buf) 62066ba94429SFrederic Weisbecker { 62076ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62086ba94429SFrederic Weisbecker int written; 62096ba94429SFrederic Weisbecker 62106ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 62116ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 62126ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 62136ba94429SFrederic Weisbecker 62146ba94429SFrederic Weisbecker return written; 62156ba94429SFrederic Weisbecker } 62166ba94429SFrederic Weisbecker 62176ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 62186ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 62196ba94429SFrederic Weisbecker { 62206ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 62216ba94429SFrederic Weisbecker 6222899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6223899a94feSLai Jiangshan 6224be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 62256ba94429SFrederic Weisbecker if (!attrs) 62266ba94429SFrederic Weisbecker return NULL; 62276ba94429SFrederic Weisbecker 62286ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 62296ba94429SFrederic Weisbecker return attrs; 62306ba94429SFrederic Weisbecker } 62316ba94429SFrederic Weisbecker 62326ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 62336ba94429SFrederic Weisbecker const char *buf, size_t count) 62346ba94429SFrederic Weisbecker { 62356ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62366ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6237d4d3e257SLai Jiangshan int ret = -ENOMEM; 6238d4d3e257SLai Jiangshan 6239d4d3e257SLai Jiangshan apply_wqattrs_lock(); 62406ba94429SFrederic Weisbecker 62416ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 62426ba94429SFrederic Weisbecker if (!attrs) 6243d4d3e257SLai Jiangshan goto out_unlock; 62446ba94429SFrederic Weisbecker 62456ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 62466ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6247d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 62486ba94429SFrederic Weisbecker else 62496ba94429SFrederic Weisbecker ret = -EINVAL; 62506ba94429SFrederic Weisbecker 6251d4d3e257SLai Jiangshan out_unlock: 6252d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 62536ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 62546ba94429SFrederic Weisbecker return ret ?: count; 62556ba94429SFrederic Weisbecker } 62566ba94429SFrederic Weisbecker 62576ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 62586ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 62596ba94429SFrederic Weisbecker { 62606ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62616ba94429SFrederic Weisbecker int written; 62626ba94429SFrederic Weisbecker 62636ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 62646ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 62656ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 62666ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 62676ba94429SFrederic Weisbecker return written; 62686ba94429SFrederic Weisbecker } 62696ba94429SFrederic Weisbecker 62706ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 62716ba94429SFrederic Weisbecker struct device_attribute *attr, 62726ba94429SFrederic Weisbecker const char *buf, size_t count) 62736ba94429SFrederic Weisbecker { 62746ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 62756ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6276d4d3e257SLai Jiangshan int ret = -ENOMEM; 6277d4d3e257SLai Jiangshan 6278d4d3e257SLai Jiangshan apply_wqattrs_lock(); 62796ba94429SFrederic Weisbecker 62806ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 62816ba94429SFrederic Weisbecker if (!attrs) 6282d4d3e257SLai Jiangshan goto out_unlock; 62836ba94429SFrederic Weisbecker 62846ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 62856ba94429SFrederic Weisbecker if (!ret) 6286d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 62876ba94429SFrederic Weisbecker 6288d4d3e257SLai Jiangshan out_unlock: 6289d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 62906ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 62916ba94429SFrederic Weisbecker return ret ?: count; 62926ba94429SFrederic Weisbecker } 62936ba94429SFrederic Weisbecker 629463c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 629563c5484eSTejun Heo struct device_attribute *attr, char *buf) 629663c5484eSTejun Heo { 629763c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 629863c5484eSTejun Heo int written; 629963c5484eSTejun Heo 630063c5484eSTejun Heo mutex_lock(&wq->mutex); 6301523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6302523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6303523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6304523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6305523a301eSTejun Heo else 630663c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 630763c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 630863c5484eSTejun Heo mutex_unlock(&wq->mutex); 630963c5484eSTejun Heo 631063c5484eSTejun Heo return written; 631163c5484eSTejun Heo } 631263c5484eSTejun Heo 631363c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 631463c5484eSTejun Heo struct device_attribute *attr, 631563c5484eSTejun Heo const char *buf, size_t count) 631663c5484eSTejun Heo { 631763c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 631863c5484eSTejun Heo struct workqueue_attrs *attrs; 631963c5484eSTejun Heo int affn, ret = -ENOMEM; 632063c5484eSTejun Heo 632163c5484eSTejun Heo affn = parse_affn_scope(buf); 632263c5484eSTejun Heo if (affn < 0) 632363c5484eSTejun Heo return affn; 632463c5484eSTejun Heo 632563c5484eSTejun Heo apply_wqattrs_lock(); 632663c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 632763c5484eSTejun Heo if (attrs) { 632863c5484eSTejun Heo attrs->affn_scope = affn; 632963c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 633063c5484eSTejun Heo } 633163c5484eSTejun Heo apply_wqattrs_unlock(); 633263c5484eSTejun Heo free_workqueue_attrs(attrs); 633363c5484eSTejun Heo return ret ?: count; 633463c5484eSTejun Heo } 633563c5484eSTejun Heo 63368639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 63378639ecebSTejun Heo struct device_attribute *attr, char *buf) 63388639ecebSTejun Heo { 63398639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 63408639ecebSTejun Heo 63418639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 63428639ecebSTejun Heo wq->unbound_attrs->affn_strict); 63438639ecebSTejun Heo } 63448639ecebSTejun Heo 63458639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 63468639ecebSTejun Heo struct device_attribute *attr, 63478639ecebSTejun Heo const char *buf, size_t count) 63488639ecebSTejun Heo { 63498639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 63508639ecebSTejun Heo struct workqueue_attrs *attrs; 63518639ecebSTejun Heo int v, ret = -ENOMEM; 63528639ecebSTejun Heo 63538639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 63548639ecebSTejun Heo return -EINVAL; 63558639ecebSTejun Heo 63568639ecebSTejun Heo apply_wqattrs_lock(); 63578639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 63588639ecebSTejun Heo if (attrs) { 63598639ecebSTejun Heo attrs->affn_strict = (bool)v; 63608639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 63618639ecebSTejun Heo } 63628639ecebSTejun Heo apply_wqattrs_unlock(); 63638639ecebSTejun Heo free_workqueue_attrs(attrs); 63648639ecebSTejun Heo return ret ?: count; 63658639ecebSTejun Heo } 63668639ecebSTejun Heo 63676ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 63686ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 63696ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 637063c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 63718639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 63726ba94429SFrederic Weisbecker __ATTR_NULL, 63736ba94429SFrederic Weisbecker }; 63746ba94429SFrederic Weisbecker 63756ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 63766ba94429SFrederic Weisbecker .name = "workqueue", 63776ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 63786ba94429SFrederic Weisbecker }; 63796ba94429SFrederic Weisbecker 6380b05a7928SFrederic Weisbecker static ssize_t wq_unbound_cpumask_show(struct device *dev, 6381b05a7928SFrederic Weisbecker struct device_attribute *attr, char *buf) 6382b05a7928SFrederic Weisbecker { 6383b05a7928SFrederic Weisbecker int written; 6384b05a7928SFrederic Weisbecker 6385042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6386b05a7928SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 6387b05a7928SFrederic Weisbecker cpumask_pr_args(wq_unbound_cpumask)); 6388042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6389b05a7928SFrederic Weisbecker 6390b05a7928SFrederic Weisbecker return written; 6391b05a7928SFrederic Weisbecker } 6392b05a7928SFrederic Weisbecker 6393042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6394042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6395042f7df1SLai Jiangshan { 6396042f7df1SLai Jiangshan cpumask_var_t cpumask; 6397042f7df1SLai Jiangshan int ret; 6398042f7df1SLai Jiangshan 6399042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6400042f7df1SLai Jiangshan return -ENOMEM; 6401042f7df1SLai Jiangshan 6402042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 6403042f7df1SLai Jiangshan if (!ret) 6404042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 6405042f7df1SLai Jiangshan 6406042f7df1SLai Jiangshan free_cpumask_var(cpumask); 6407042f7df1SLai Jiangshan return ret ? ret : count; 6408042f7df1SLai Jiangshan } 6409042f7df1SLai Jiangshan 6410b05a7928SFrederic Weisbecker static struct device_attribute wq_sysfs_cpumask_attr = 6411042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 6412042f7df1SLai Jiangshan wq_unbound_cpumask_store); 6413b05a7928SFrederic Weisbecker 64146ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 64156ba94429SFrederic Weisbecker { 6416686f6697SGreg Kroah-Hartman struct device *dev_root; 6417b05a7928SFrederic Weisbecker int err; 6418b05a7928SFrederic Weisbecker 6419b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 6420b05a7928SFrederic Weisbecker if (err) 6421b05a7928SFrederic Weisbecker return err; 6422b05a7928SFrederic Weisbecker 6423686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 6424686f6697SGreg Kroah-Hartman if (dev_root) { 6425686f6697SGreg Kroah-Hartman err = device_create_file(dev_root, &wq_sysfs_cpumask_attr); 6426686f6697SGreg Kroah-Hartman put_device(dev_root); 6427686f6697SGreg Kroah-Hartman } 6428686f6697SGreg Kroah-Hartman return err; 64296ba94429SFrederic Weisbecker } 64306ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 64316ba94429SFrederic Weisbecker 64326ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 64336ba94429SFrederic Weisbecker { 64346ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 64356ba94429SFrederic Weisbecker 64366ba94429SFrederic Weisbecker kfree(wq_dev); 64376ba94429SFrederic Weisbecker } 64386ba94429SFrederic Weisbecker 64396ba94429SFrederic Weisbecker /** 64406ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 64416ba94429SFrederic Weisbecker * @wq: the workqueue to register 64426ba94429SFrederic Weisbecker * 64436ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 64446ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 64456ba94429SFrederic Weisbecker * which is the preferred method. 64466ba94429SFrederic Weisbecker * 64476ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 64486ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 64496ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 64506ba94429SFrederic Weisbecker * attributes. 64516ba94429SFrederic Weisbecker * 64526ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 64536ba94429SFrederic Weisbecker */ 64546ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 64556ba94429SFrederic Weisbecker { 64566ba94429SFrederic Weisbecker struct wq_device *wq_dev; 64576ba94429SFrederic Weisbecker int ret; 64586ba94429SFrederic Weisbecker 64596ba94429SFrederic Weisbecker /* 6460402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 64616ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 64626ba94429SFrederic Weisbecker * workqueues. 64636ba94429SFrederic Weisbecker */ 64640a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 64656ba94429SFrederic Weisbecker return -EINVAL; 64666ba94429SFrederic Weisbecker 64676ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 64686ba94429SFrederic Weisbecker if (!wq_dev) 64696ba94429SFrederic Weisbecker return -ENOMEM; 64706ba94429SFrederic Weisbecker 64716ba94429SFrederic Weisbecker wq_dev->wq = wq; 64726ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 64736ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 647423217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 64756ba94429SFrederic Weisbecker 64766ba94429SFrederic Weisbecker /* 64776ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 64786ba94429SFrederic Weisbecker * everything is ready. 64796ba94429SFrederic Weisbecker */ 64806ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 64816ba94429SFrederic Weisbecker 64826ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 64836ba94429SFrederic Weisbecker if (ret) { 6484537f4146SArvind Yadav put_device(&wq_dev->dev); 64856ba94429SFrederic Weisbecker wq->wq_dev = NULL; 64866ba94429SFrederic Weisbecker return ret; 64876ba94429SFrederic Weisbecker } 64886ba94429SFrederic Weisbecker 64896ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 64906ba94429SFrederic Weisbecker struct device_attribute *attr; 64916ba94429SFrederic Weisbecker 64926ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 64936ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 64946ba94429SFrederic Weisbecker if (ret) { 64956ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 64966ba94429SFrederic Weisbecker wq->wq_dev = NULL; 64976ba94429SFrederic Weisbecker return ret; 64986ba94429SFrederic Weisbecker } 64996ba94429SFrederic Weisbecker } 65006ba94429SFrederic Weisbecker } 65016ba94429SFrederic Weisbecker 65026ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 65036ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 65046ba94429SFrederic Weisbecker return 0; 65056ba94429SFrederic Weisbecker } 65066ba94429SFrederic Weisbecker 65076ba94429SFrederic Weisbecker /** 65086ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 65096ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 65106ba94429SFrederic Weisbecker * 65116ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 65126ba94429SFrederic Weisbecker */ 65136ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 65146ba94429SFrederic Weisbecker { 65156ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 65166ba94429SFrederic Weisbecker 65176ba94429SFrederic Weisbecker if (!wq->wq_dev) 65186ba94429SFrederic Weisbecker return; 65196ba94429SFrederic Weisbecker 65206ba94429SFrederic Weisbecker wq->wq_dev = NULL; 65216ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 65226ba94429SFrederic Weisbecker } 65236ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 65246ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 65256ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 65266ba94429SFrederic Weisbecker 652782607adcSTejun Heo /* 652882607adcSTejun Heo * Workqueue watchdog. 652982607adcSTejun Heo * 653082607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 653182607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 653282607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 653382607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 653482607adcSTejun Heo * largely opaque. 653582607adcSTejun Heo * 653682607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 653782607adcSTejun Heo * state if some pools failed to make forward progress for a while where 653882607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 653982607adcSTejun Heo * 654082607adcSTejun Heo * This mechanism is controlled through the kernel parameter 654182607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 654282607adcSTejun Heo * corresponding sysfs parameter file. 654382607adcSTejun Heo */ 654482607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 654582607adcSTejun Heo 654682607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 65475cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 654882607adcSTejun Heo 654982607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 655082607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 655182607adcSTejun Heo 6552cd2440d6SPetr Mladek /* 6553cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6554cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6555cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6556cd2440d6SPetr Mladek * in all other situations. 6557cd2440d6SPetr Mladek */ 6558cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6559cd2440d6SPetr Mladek { 6560cd2440d6SPetr Mladek struct worker *worker; 6561cd2440d6SPetr Mladek unsigned long flags; 6562cd2440d6SPetr Mladek int bkt; 6563cd2440d6SPetr Mladek 6564cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6565cd2440d6SPetr Mladek 6566cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6567cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6568cd2440d6SPetr Mladek /* 6569cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6570cd2440d6SPetr Mladek * drivers that queue work while holding locks 6571cd2440d6SPetr Mladek * also taken in their write paths. 6572cd2440d6SPetr Mladek */ 6573cd2440d6SPetr Mladek printk_deferred_enter(); 6574cd2440d6SPetr Mladek 6575cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6576cd2440d6SPetr Mladek sched_show_task(worker->task); 6577cd2440d6SPetr Mladek 6578cd2440d6SPetr Mladek printk_deferred_exit(); 6579cd2440d6SPetr Mladek } 6580cd2440d6SPetr Mladek } 6581cd2440d6SPetr Mladek 6582cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6583cd2440d6SPetr Mladek } 6584cd2440d6SPetr Mladek 6585cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6586cd2440d6SPetr Mladek { 6587cd2440d6SPetr Mladek struct worker_pool *pool; 6588cd2440d6SPetr Mladek int pi; 6589cd2440d6SPetr Mladek 6590cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6591cd2440d6SPetr Mladek 6592cd2440d6SPetr Mladek rcu_read_lock(); 6593cd2440d6SPetr Mladek 6594cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6595cd2440d6SPetr Mladek if (pool->cpu_stall) 6596cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6597cd2440d6SPetr Mladek 6598cd2440d6SPetr Mladek } 6599cd2440d6SPetr Mladek 6600cd2440d6SPetr Mladek rcu_read_unlock(); 6601cd2440d6SPetr Mladek } 6602cd2440d6SPetr Mladek 660382607adcSTejun Heo static void wq_watchdog_reset_touched(void) 660482607adcSTejun Heo { 660582607adcSTejun Heo int cpu; 660682607adcSTejun Heo 660782607adcSTejun Heo wq_watchdog_touched = jiffies; 660882607adcSTejun Heo for_each_possible_cpu(cpu) 660982607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 661082607adcSTejun Heo } 661182607adcSTejun Heo 66125cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 661382607adcSTejun Heo { 661482607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 661582607adcSTejun Heo bool lockup_detected = false; 6616cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6617940d71c6SSergey Senozhatsky unsigned long now = jiffies; 661882607adcSTejun Heo struct worker_pool *pool; 661982607adcSTejun Heo int pi; 662082607adcSTejun Heo 662182607adcSTejun Heo if (!thresh) 662282607adcSTejun Heo return; 662382607adcSTejun Heo 662482607adcSTejun Heo rcu_read_lock(); 662582607adcSTejun Heo 662682607adcSTejun Heo for_each_pool(pool, pi) { 662782607adcSTejun Heo unsigned long pool_ts, touched, ts; 662882607adcSTejun Heo 6629cd2440d6SPetr Mladek pool->cpu_stall = false; 663082607adcSTejun Heo if (list_empty(&pool->worklist)) 663182607adcSTejun Heo continue; 663282607adcSTejun Heo 6633940d71c6SSergey Senozhatsky /* 6634940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 6635940d71c6SSergey Senozhatsky * the watchdog like a stall. 6636940d71c6SSergey Senozhatsky */ 6637940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 6638940d71c6SSergey Senozhatsky 663982607adcSTejun Heo /* get the latest of pool and touched timestamps */ 664089e28ce6SWang Qing if (pool->cpu >= 0) 664189e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 664289e28ce6SWang Qing else 664382607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 664489e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 664582607adcSTejun Heo 664682607adcSTejun Heo if (time_after(pool_ts, touched)) 664782607adcSTejun Heo ts = pool_ts; 664882607adcSTejun Heo else 664982607adcSTejun Heo ts = touched; 665082607adcSTejun Heo 665182607adcSTejun Heo /* did we stall? */ 6652940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 665382607adcSTejun Heo lockup_detected = true; 6654cd2440d6SPetr Mladek if (pool->cpu >= 0) { 6655cd2440d6SPetr Mladek pool->cpu_stall = true; 6656cd2440d6SPetr Mladek cpu_pool_stall = true; 6657cd2440d6SPetr Mladek } 665882607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 665982607adcSTejun Heo pr_cont_pool_info(pool); 666082607adcSTejun Heo pr_cont(" stuck for %us!\n", 6661940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 666282607adcSTejun Heo } 6663cd2440d6SPetr Mladek 6664cd2440d6SPetr Mladek 666582607adcSTejun Heo } 666682607adcSTejun Heo 666782607adcSTejun Heo rcu_read_unlock(); 666882607adcSTejun Heo 666982607adcSTejun Heo if (lockup_detected) 667055df0933SImran Khan show_all_workqueues(); 667182607adcSTejun Heo 6672cd2440d6SPetr Mladek if (cpu_pool_stall) 6673cd2440d6SPetr Mladek show_cpu_pools_hogs(); 6674cd2440d6SPetr Mladek 667582607adcSTejun Heo wq_watchdog_reset_touched(); 667682607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 667782607adcSTejun Heo } 667882607adcSTejun Heo 6679cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 668082607adcSTejun Heo { 668182607adcSTejun Heo if (cpu >= 0) 668282607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 668389e28ce6SWang Qing 668482607adcSTejun Heo wq_watchdog_touched = jiffies; 668582607adcSTejun Heo } 668682607adcSTejun Heo 668782607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 668882607adcSTejun Heo { 668982607adcSTejun Heo wq_watchdog_thresh = 0; 669082607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 669182607adcSTejun Heo 669282607adcSTejun Heo if (thresh) { 669382607adcSTejun Heo wq_watchdog_thresh = thresh; 669482607adcSTejun Heo wq_watchdog_reset_touched(); 669582607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 669682607adcSTejun Heo } 669782607adcSTejun Heo } 669882607adcSTejun Heo 669982607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 670082607adcSTejun Heo const struct kernel_param *kp) 670182607adcSTejun Heo { 670282607adcSTejun Heo unsigned long thresh; 670382607adcSTejun Heo int ret; 670482607adcSTejun Heo 670582607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 670682607adcSTejun Heo if (ret) 670782607adcSTejun Heo return ret; 670882607adcSTejun Heo 670982607adcSTejun Heo if (system_wq) 671082607adcSTejun Heo wq_watchdog_set_thresh(thresh); 671182607adcSTejun Heo else 671282607adcSTejun Heo wq_watchdog_thresh = thresh; 671382607adcSTejun Heo 671482607adcSTejun Heo return 0; 671582607adcSTejun Heo } 671682607adcSTejun Heo 671782607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 671882607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 671982607adcSTejun Heo .get = param_get_ulong, 672082607adcSTejun Heo }; 672182607adcSTejun Heo 672282607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 672382607adcSTejun Heo 0644); 672482607adcSTejun Heo 672582607adcSTejun Heo static void wq_watchdog_init(void) 672682607adcSTejun Heo { 67275cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 672882607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 672982607adcSTejun Heo } 673082607adcSTejun Heo 673182607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 673282607adcSTejun Heo 673382607adcSTejun Heo static inline void wq_watchdog_init(void) { } 673482607adcSTejun Heo 673582607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 673682607adcSTejun Heo 6737b2c562a7STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 6738b2c562a7STejun Heo { 6739b2c562a7STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 6740b2c562a7STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 6741b2c562a7STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 6742b2c562a7STejun Heo return; 6743b2c562a7STejun Heo } 6744b2c562a7STejun Heo 6745b2c562a7STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 6746b2c562a7STejun Heo } 6747b2c562a7STejun Heo 67483347fa09STejun Heo /** 67493347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 67503347fa09STejun Heo * 67512930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 67522930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 67532930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 67542930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 67552930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 67562930155bSTejun Heo * before early initcalls. 67573347fa09STejun Heo */ 67582333e829SYu Chen void __init workqueue_init_early(void) 67591da177e4SLinus Torvalds { 676084193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 67617a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 67627a4e344cSTejun Heo int i, cpu; 6763c34056a3STejun Heo 676410cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 6765e904e6c2STejun Heo 6766b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 6767b2c562a7STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 6768b2c562a7STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 6769b2c562a7STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 6770ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 6771b2c562a7STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 6772ace3c549Stiozhang 6773e904e6c2STejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 6774e904e6c2STejun Heo 67752930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 67762930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 67772930155bSTejun Heo 677884193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 677984193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 678084193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 678184193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 678284193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 678384193c07STejun Heo 678484193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 678584193c07STejun Heo 678684193c07STejun Heo pt->nr_pods = 1; 678784193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 678884193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 678984193c07STejun Heo pt->cpu_pod[0] = 0; 679084193c07STejun Heo 6791706026c2STejun Heo /* initialize CPU pools */ 679229c91e99STejun Heo for_each_possible_cpu(cpu) { 67934ce62e9eSTejun Heo struct worker_pool *pool; 67948b03ae3cSTejun Heo 67957a4e344cSTejun Heo i = 0; 6796f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 67977a4e344cSTejun Heo BUG_ON(init_worker_pool(pool)); 6798ec22ca5eSTejun Heo pool->cpu = cpu; 67997a4e344cSTejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 68009546b29eSTejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 68017a4e344cSTejun Heo pool->attrs->nice = std_nice[i++]; 68028639ecebSTejun Heo pool->attrs->affn_strict = true; 6803f3f90ad4STejun Heo pool->node = cpu_to_node(cpu); 68047a4e344cSTejun Heo 68059daf9e67STejun Heo /* alloc pool ID */ 680668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 68079daf9e67STejun Heo BUG_ON(worker_pool_assign_id(pool)); 680868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 68094ce62e9eSTejun Heo } 68108b03ae3cSTejun Heo } 68118b03ae3cSTejun Heo 68128a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 681329c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 681429c91e99STejun Heo struct workqueue_attrs *attrs; 681529c91e99STejun Heo 6816be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 681729c91e99STejun Heo attrs->nice = std_nice[i]; 681829c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 68198a2b7538STejun Heo 68208a2b7538STejun Heo /* 68218a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 68228a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 68238a2b7538STejun Heo */ 6824be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 68258a2b7538STejun Heo attrs->nice = std_nice[i]; 6826af73f5c9STejun Heo attrs->ordered = true; 68278a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 682829c91e99STejun Heo } 682929c91e99STejun Heo 6830d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 68311aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 6832d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 6833f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 6834636b927eSTejun Heo WQ_MAX_ACTIVE); 683524d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 683624d51addSTejun Heo WQ_FREEZABLE, 0); 68370668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 68380668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 68390668106cSViresh Kumar system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient", 68400668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 68410668106cSViresh Kumar 0); 68421aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 68430668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 68440668106cSViresh Kumar !system_power_efficient_wq || 68450668106cSViresh Kumar !system_freezable_power_efficient_wq); 68463347fa09STejun Heo } 68473347fa09STejun Heo 6848aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 6849aa6fde93STejun Heo { 6850aa6fde93STejun Heo unsigned long thresh; 6851aa6fde93STejun Heo unsigned long bogo; 6852aa6fde93STejun Heo 6853dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 6854dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 6855dd64c873SZqiang 6856aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 6857aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 6858aa6fde93STejun Heo return; 6859aa6fde93STejun Heo 6860aa6fde93STejun Heo /* 6861aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 6862aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 6863aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 6864aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 6865aa6fde93STejun Heo * too low. 6866aa6fde93STejun Heo * 6867aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 6868aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 6869aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 6870aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 6871aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 6872aa6fde93STejun Heo * usefulness. 6873aa6fde93STejun Heo */ 6874aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 6875aa6fde93STejun Heo 6876aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 6877aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 6878aa6fde93STejun Heo if (bogo < 4000) 6879aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 6880aa6fde93STejun Heo 6881aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 6882aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 6883aa6fde93STejun Heo 6884aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 6885aa6fde93STejun Heo } 6886aa6fde93STejun Heo 68873347fa09STejun Heo /** 68883347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 68893347fa09STejun Heo * 68902930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 68912930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 68922930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 68932930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 68942930155bSTejun Heo * workers and enable future kworker creations. 68953347fa09STejun Heo */ 68962333e829SYu Chen void __init workqueue_init(void) 68973347fa09STejun Heo { 68982186d9f9STejun Heo struct workqueue_struct *wq; 68993347fa09STejun Heo struct worker_pool *pool; 69003347fa09STejun Heo int cpu, bkt; 69013347fa09STejun Heo 6902aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 6903aa6fde93STejun Heo 69042186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 69052186d9f9STejun Heo 69062930155bSTejun Heo /* 69072930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 69082930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 69092930155bSTejun Heo */ 69102186d9f9STejun Heo for_each_possible_cpu(cpu) { 69112186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 69122186d9f9STejun Heo pool->node = cpu_to_node(cpu); 69132186d9f9STejun Heo } 69142186d9f9STejun Heo } 69152186d9f9STejun Heo 691640c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 691740c17f75STejun Heo WARN(init_rescuer(wq), 691840c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 691940c17f75STejun Heo wq->name); 692040c17f75STejun Heo } 69212186d9f9STejun Heo 69222186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 69232186d9f9STejun Heo 69243347fa09STejun Heo /* create the initial workers */ 69253347fa09STejun Heo for_each_online_cpu(cpu) { 69263347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 69273347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 69283347fa09STejun Heo BUG_ON(!create_worker(pool)); 69293347fa09STejun Heo } 69303347fa09STejun Heo } 69313347fa09STejun Heo 69323347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 69333347fa09STejun Heo BUG_ON(!create_worker(pool)); 69343347fa09STejun Heo 69353347fa09STejun Heo wq_online = true; 693682607adcSTejun Heo wq_watchdog_init(); 69371da177e4SLinus Torvalds } 6938c4f135d6STetsuo Handa 6939025e1684STejun Heo /* 6940025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 6941025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 6942025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 6943025e1684STejun Heo */ 6944025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 6945025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 6946025e1684STejun Heo { 6947025e1684STejun Heo int cur, pre, cpu, pod; 6948025e1684STejun Heo 6949025e1684STejun Heo pt->nr_pods = 0; 6950025e1684STejun Heo 6951025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 6952025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 6953025e1684STejun Heo BUG_ON(!pt->cpu_pod); 6954025e1684STejun Heo 6955025e1684STejun Heo for_each_possible_cpu(cur) { 6956025e1684STejun Heo for_each_possible_cpu(pre) { 6957025e1684STejun Heo if (pre >= cur) { 6958025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 6959025e1684STejun Heo break; 6960025e1684STejun Heo } 6961025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 6962025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 6963025e1684STejun Heo break; 6964025e1684STejun Heo } 6965025e1684STejun Heo } 6966025e1684STejun Heo } 6967025e1684STejun Heo 6968025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 6969025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 6970025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 6971025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 6972025e1684STejun Heo 6973025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 6974025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 6975025e1684STejun Heo 6976025e1684STejun Heo for_each_possible_cpu(cpu) { 6977025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 6978025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 6979025e1684STejun Heo } 6980025e1684STejun Heo } 6981025e1684STejun Heo 698263c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 698363c5484eSTejun Heo { 698463c5484eSTejun Heo return false; 698563c5484eSTejun Heo } 698663c5484eSTejun Heo 698763c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 698863c5484eSTejun Heo { 698963c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 699063c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 699163c5484eSTejun Heo #else 699263c5484eSTejun Heo return false; 699363c5484eSTejun Heo #endif 699463c5484eSTejun Heo } 699563c5484eSTejun Heo 6996025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 6997025e1684STejun Heo { 6998025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 6999025e1684STejun Heo } 7000025e1684STejun Heo 70012930155bSTejun Heo /** 70022930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 70032930155bSTejun Heo * 70042930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 70052930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 70062930155bSTejun Heo * initializes the unbound CPU pods accordingly. 70072930155bSTejun Heo */ 70082930155bSTejun Heo void __init workqueue_init_topology(void) 7009a86feae6STejun Heo { 70102930155bSTejun Heo struct workqueue_struct *wq; 7011025e1684STejun Heo int cpu; 7012a86feae6STejun Heo 701363c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 701463c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 701563c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7016025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7017a86feae6STejun Heo 70182930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7019a86feae6STejun Heo 7020a86feae6STejun Heo /* 70212930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 70222930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 70232930155bSTejun Heo * combinations to apply per-pod sharing. 70242930155bSTejun Heo */ 70252930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 70262930155bSTejun Heo for_each_online_cpu(cpu) { 70272930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 70282930155bSTejun Heo } 70292930155bSTejun Heo } 70302930155bSTejun Heo 70312930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7032a86feae6STejun Heo } 7033a86feae6STejun Heo 703420bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 703520bdedafSTetsuo Handa { 703620bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 703720bdedafSTetsuo Handa dump_stack(); 703820bdedafSTetsuo Handa } 7039c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7040ace3c549Stiozhang 7041ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7042ace3c549Stiozhang { 7043ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7044ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7045ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7046ace3c549Stiozhang } 7047ace3c549Stiozhang 7048ace3c549Stiozhang return 1; 7049ace3c549Stiozhang } 7050ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7051