1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 41da177e4SLinus Torvalds * 5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 81da177e4SLinus Torvalds * David Woodhouse <[email protected]> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <[email protected]> 111da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1289ada679SChristoph Lameter * 13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 14c54fce6eSTejun Heo * 15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 17c54fce6eSTejun Heo * 18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 21b11895c4SLibin * normal work items and the other for high priority ones) and some extra 22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 23b11895c4SLibin * number of these backing pools is dynamic. 24c54fce6eSTejun Heo * 259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 289984de1aSPaul Gortmaker #include <linux/export.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/init.h> 321da177e4SLinus Torvalds #include <linux/signal.h> 331da177e4SLinus Torvalds #include <linux/completion.h> 341da177e4SLinus Torvalds #include <linux/workqueue.h> 351da177e4SLinus Torvalds #include <linux/slab.h> 361da177e4SLinus Torvalds #include <linux/cpu.h> 371da177e4SLinus Torvalds #include <linux/notifier.h> 381da177e4SLinus Torvalds #include <linux/kthread.h> 391fa44ecaSJames Bottomley #include <linux/hardirq.h> 4046934023SChristoph Lameter #include <linux/mempolicy.h> 41341a5958SRafael J. Wysocki #include <linux/freezer.h> 42d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 434e6045f1SJohannes Berg #include <linux/lockdep.h> 44c34056a3STejun Heo #include <linux/idr.h> 4529c91e99STejun Heo #include <linux/jhash.h> 4642f8570fSSasha Levin #include <linux/hashtable.h> 4776af4d93STejun Heo #include <linux/rculist.h> 48bce90380STejun Heo #include <linux/nodemask.h> 494c16bd32STejun Heo #include <linux/moduleparam.h> 503d1cb205STejun Heo #include <linux/uaccess.h> 51c98a9805STal Shorer #include <linux/sched/isolation.h> 52cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5362635ea8SSergey Senozhatsky #include <linux/nmi.h> 54940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 55aa6fde93STejun Heo #include <linux/delay.h> 56e22bee78STejun Heo 57ea138446STejun Heo #include "workqueue_internal.h" 581da177e4SLinus Torvalds 59e563d0a7STejun Heo enum worker_pool_flags { 60bc2ae0f5STejun Heo /* 6124647570STejun Heo * worker_pool flags 62bc2ae0f5STejun Heo * 6324647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 64bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 65bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 66bc2ae0f5STejun Heo * is in effect. 67bc2ae0f5STejun Heo * 68bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 69bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7024647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 71bc2ae0f5STejun Heo * 72bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 731258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 744736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 75bc2ae0f5STejun Heo */ 76692b4825STejun Heo POOL_MANAGER_ACTIVE = 1 << 0, /* being managed */ 7724647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 78e563d0a7STejun Heo }; 79db7bccf4STejun Heo 80e563d0a7STejun Heo enum worker_flags { 81c8e55f36STejun Heo /* worker flags */ 82c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 83c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 84e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 85fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 86f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 87a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 88e22bee78STejun Heo 89a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 90a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 91e563d0a7STejun Heo }; 92db7bccf4STejun Heo 93e563d0a7STejun Heo enum wq_internal_consts { 94e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 954ce62e9eSTejun Heo 9629c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 97c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 98db7bccf4STejun Heo 99e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 100e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 101e22bee78STejun Heo 1023233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1033233cdbdSTejun Heo /* call for help after 10ms 1043233cdbdSTejun Heo (min two ticks) */ 105e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 106e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 109e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1108698a745SDongsheng Yang * all cpus. Give MIN_NICE. 111e22bee78STejun Heo */ 1128698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1138698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 114ecf6881fSTejun Heo 11531c89007SAudra Mitchell WQ_NAME_LEN = 32, 116c8e55f36STejun Heo }; 117c8e55f36STejun Heo 1181da177e4SLinus Torvalds /* 1194690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1204690c4abSTejun Heo * 121e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 122e41e704bSTejun Heo * everyone else. 1234690c4abSTejun Heo * 124e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 125e22bee78STejun Heo * only be modified and accessed from the local cpu. 126e22bee78STejun Heo * 127d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1284690c4abSTejun Heo * 1295797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for 1305797b1c1STejun Heo * reads. 1315797b1c1STejun Heo * 132bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 133bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 134bdf8b9bfSTejun Heo * kworker. 135bdf8b9bfSTejun Heo * 136bdf8b9bfSTejun Heo * S: Only modified by worker self. 137bdf8b9bfSTejun Heo * 1381258fae7STejun Heo * A: wq_pool_attach_mutex protected. 139822d8405STejun Heo * 14068e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 14176af4d93STejun Heo * 14224acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1435bcab335STejun Heo * 1445b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1455b95e1afSLai Jiangshan * 1465b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 14724acfb71SThomas Gleixner * RCU for reads. 1485b95e1afSLai Jiangshan * 1493c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1503c25a55dSLai Jiangshan * 15124acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1522e109a28STejun Heo * 153a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 154a045a272STejun Heo * with READ_ONCE() without locking. 155a045a272STejun Heo * 1562e109a28STejun Heo * MD: wq_mayday_lock protected. 157cd2440d6SPetr Mladek * 158cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1594690c4abSTejun Heo */ 1604690c4abSTejun Heo 1612eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 162c34056a3STejun Heo 163bd7bdd43STejun Heo struct worker_pool { 164a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 165d84ff051STejun Heo int cpu; /* I: the associated cpu */ 166f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1679daf9e67STejun Heo int id; /* I: pool ID */ 168bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 169bd7bdd43STejun Heo 17082607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 171cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 17282607adcSTejun Heo 173bc35f7efSLai Jiangshan /* 174bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 175bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 176bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 177bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 178bc35f7efSLai Jiangshan */ 179bc35f7efSLai Jiangshan int nr_running; 18084f91c62SLai Jiangshan 181bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 182ea1abd61SLai Jiangshan 1835826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 1845826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 185bd7bdd43STejun Heo 1862c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 187bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 1883f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 1893f959aa3SValentin Schneider 190bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 191bd7bdd43STejun Heo 192c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 193c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 194c9e7cf27STejun Heo /* L: hash of busy workers */ 195c9e7cf27STejun Heo 1962607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 19792f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 198e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 19960f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 200e19e397aSTejun Heo 2017cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 202e19e397aSTejun Heo 2037a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 20468e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 20568e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2067a4e344cSTejun Heo 207e19e397aSTejun Heo /* 20824acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 20929c91e99STejun Heo * from get_work_pool(). 21029c91e99STejun Heo */ 21129c91e99STejun Heo struct rcu_head rcu; 21284f91c62SLai Jiangshan }; 2138b03ae3cSTejun Heo 2148b03ae3cSTejun Heo /* 215725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 216725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 217725e8ec5STejun Heo */ 218725e8ec5STejun Heo enum pool_workqueue_stats { 219725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 220725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2218a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 222616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 223725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2248639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 225725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 226725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 227725e8ec5STejun Heo 228725e8ec5STejun Heo PWQ_NR_STATS, 229725e8ec5STejun Heo }; 230725e8ec5STejun Heo 231725e8ec5STejun Heo /* 232112202d9STejun Heo * The per-pool workqueue. While queued, the lower WORK_STRUCT_FLAG_BITS 233112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 234112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 235112202d9STejun Heo * number of flag bits. 2361da177e4SLinus Torvalds */ 237112202d9STejun Heo struct pool_workqueue { 238bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2394690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 24073f53c4aSTejun Heo int work_color; /* L: current color */ 24173f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2428864b4e5STejun Heo int refcnt; /* L: reference count */ 24373f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 24473f53c4aSTejun Heo /* L: nr of in_flight works */ 245018f3a13SLai Jiangshan 246018f3a13SLai Jiangshan /* 247018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 248018f3a13SLai Jiangshan * 249018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 250018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 251018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 252018f3a13SLai Jiangshan * 2535797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in 2545797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with 2555797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are 2565797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in 2575797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct 2585797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate 2595797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with 2605797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 261018f3a13SLai Jiangshan */ 2621e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 263f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2645797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ 2653c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2662e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2678864b4e5STejun Heo 268725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 269725e8ec5STejun Heo 2708864b4e5STejun Heo /* 271967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 272687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 273687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 274967b494eSTejun Heo * grabbing wq->mutex. 2758864b4e5STejun Heo */ 276687a9aa5STejun Heo struct kthread_work release_work; 2778864b4e5STejun Heo struct rcu_head rcu; 278e904e6c2STejun Heo } __aligned(1 << WORK_STRUCT_FLAG_BITS); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* 28173f53c4aSTejun Heo * Structure used to wait for workqueue flush. 28273f53c4aSTejun Heo */ 28373f53c4aSTejun Heo struct wq_flusher { 2843c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 2853c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 28673f53c4aSTejun Heo struct completion done; /* flush completion */ 28773f53c4aSTejun Heo }; 2881da177e4SLinus Torvalds 289226223abSTejun Heo struct wq_device; 290226223abSTejun Heo 29173f53c4aSTejun Heo /* 29291ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 29391ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 29491ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 29591ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 2965797b1c1STejun Heo * 2975797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants 2985797b1c1STejun Heo * to start executing a work item, it should increment ->nr using 2995797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over 3005797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish 3015797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in 3025797b1c1STejun Heo * round-robin order. 30391ccc6e7STejun Heo */ 30491ccc6e7STejun Heo struct wq_node_nr_active { 3055797b1c1STejun Heo int max; /* per-node max_active */ 3065797b1c1STejun Heo atomic_t nr; /* per-node nr_active */ 3075797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */ 3085797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */ 30991ccc6e7STejun Heo }; 31091ccc6e7STejun Heo 31191ccc6e7STejun Heo /* 312c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 313c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3141da177e4SLinus Torvalds */ 3151da177e4SLinus Torvalds struct workqueue_struct { 3163c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 317e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 31873f53c4aSTejun Heo 3193c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3203c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3213c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 322112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3233c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3243c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3253c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 32673f53c4aSTejun Heo 3272e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 32830ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 329e22bee78STejun Heo 33087fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 3315797b1c1STejun Heo 3325797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */ 333a045a272STejun Heo int max_active; /* WO: max active works */ 3345797b1c1STejun Heo int min_active; /* WO: min active works */ 335a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 3365797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */ 337226223abSTejun Heo 3385b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3399f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3406029a918STejun Heo 341226223abSTejun Heo #ifdef CONFIG_SYSFS 342226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 343226223abSTejun Heo #endif 3444e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 345669de8bdSBart Van Assche char *lock_name; 346669de8bdSBart Van Assche struct lock_class_key key; 3474e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3484e6045f1SJohannes Berg #endif 349ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3502728fd2fSTejun Heo 351e2dca7adSTejun Heo /* 35224acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 35324acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 354e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 355e2dca7adSTejun Heo */ 356e2dca7adSTejun Heo struct rcu_head rcu; 357e2dca7adSTejun Heo 3582728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3592728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 360636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 36191ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3621da177e4SLinus Torvalds }; 3631da177e4SLinus Torvalds 364e904e6c2STejun Heo static struct kmem_cache *pwq_cache; 365e904e6c2STejun Heo 36684193c07STejun Heo /* 36784193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 36884193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 36984193c07STejun Heo */ 37084193c07STejun Heo struct wq_pod_type { 37184193c07STejun Heo int nr_pods; /* number of pods */ 37284193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 37384193c07STejun Heo int *pod_node; /* pod -> node */ 37484193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 37584193c07STejun Heo }; 37684193c07STejun Heo 37784193c07STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 378523a301eSTejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 37963c5484eSTejun Heo 38063c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 381523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 38263c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 38363c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 38463c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 38563c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 38663c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 38763c5484eSTejun Heo }; 388bce90380STejun Heo 389c5f8cd6cSTejun Heo static bool wq_topo_initialized __read_mostly = false; 390c5f8cd6cSTejun Heo 391616db877STejun Heo /* 392616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 393616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 394616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 395aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 396aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 397616db877STejun Heo */ 398aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 399616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 400616db877STejun Heo 401cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 402552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 403cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 404cee22a15SViresh Kumar 405863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 4063347fa09STejun Heo 407fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 408fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4094c16bd32STejun Heo 41068e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4111258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 412a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 413d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 414d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4155bcab335STejun Heo 416e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 41768e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4187d19c5ceSTejun Heo 41999c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 420ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 421ef557180SMike Galbraith 422fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 423fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 424fe28f631SWaiman Long 425fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 426fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 427fe28f631SWaiman Long 428ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 429ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 430ace3c549Stiozhang 431ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 432ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 433b05a7928SFrederic Weisbecker 434f303fccbSTejun Heo /* 435f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 436f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 437f303fccbSTejun Heo * to uncover usages which depend on it. 438f303fccbSTejun Heo */ 439f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 440f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 441f303fccbSTejun Heo #else 442f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 443f303fccbSTejun Heo #endif 444f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 445f303fccbSTejun Heo 4467d19c5ceSTejun Heo /* the per-cpu worker pools */ 44725528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 4487d19c5ceSTejun Heo 44968e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4507d19c5ceSTejun Heo 45168e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 45229c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 45329c91e99STejun Heo 454c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 45529c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 45629c91e99STejun Heo 4578a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4588a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4598a2b7538STejun Heo 460967b494eSTejun Heo /* 461967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 462967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 463967b494eSTejun Heo * worker to avoid A-A deadlocks. 464967b494eSTejun Heo */ 46568279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 466967b494eSTejun Heo 46768279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 468ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 46968279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 4701aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 47168279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 472d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 47368279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 474f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 47568279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 47624d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 47768279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 4780668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 47968279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 4800668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 481d320c038STejun Heo 4827d19c5ceSTejun Heo static int worker_thread(void *__worker); 4836ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 484c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 48555df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4867d19c5ceSTejun Heo 48797bd2347STejun Heo #define CREATE_TRACE_POINTS 48897bd2347STejun Heo #include <trace/events/workqueue.h> 48997bd2347STejun Heo 49068e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 49124acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 492f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 49324acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4945bcab335STejun Heo 4955b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 49624acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 497f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 498f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 49924acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5005b95e1afSLai Jiangshan 501f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 502f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 503f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5047a62c2c8STejun Heo (pool)++) 5054ce62e9eSTejun Heo 50649e3cf44STejun Heo /** 50717116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 50817116969STejun Heo * @pool: iteration cursor 509611c92a0STejun Heo * @pi: integer used for iteration 510fa1b54e6STejun Heo * 51124acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 51268e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 51368e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 514fa1b54e6STejun Heo * 515fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 516fa1b54e6STejun Heo * ignored. 51717116969STejun Heo */ 518611c92a0STejun Heo #define for_each_pool(pool, pi) \ 519611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 52068e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 521fa1b54e6STejun Heo else 52217116969STejun Heo 52317116969STejun Heo /** 524822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 525822d8405STejun Heo * @worker: iteration cursor 526822d8405STejun Heo * @pool: worker_pool to iterate workers of 527822d8405STejun Heo * 5281258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 529822d8405STejun Heo * 530822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 531822d8405STejun Heo * ignored. 532822d8405STejun Heo */ 533da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 534da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5351258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 536822d8405STejun Heo else 537822d8405STejun Heo 538822d8405STejun Heo /** 53949e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 54049e3cf44STejun Heo * @pwq: iteration cursor 54149e3cf44STejun Heo * @wq: the target workqueue 54276af4d93STejun Heo * 54324acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 544794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 545794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 54676af4d93STejun Heo * 54776af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 54876af4d93STejun Heo * ignored. 54949e3cf44STejun Heo */ 55049e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 55149e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5525a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 553f3421797STejun Heo 554dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 555dc186ad7SThomas Gleixner 556f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 557dc186ad7SThomas Gleixner 55899777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 55999777288SStanislaw Gruszka { 56099777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 56199777288SStanislaw Gruszka } 56299777288SStanislaw Gruszka 563b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 564b9fdac7fSDu, Changbin { 565b9fdac7fSDu, Changbin struct work_struct *work = addr; 566b9fdac7fSDu, Changbin 567b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 568b9fdac7fSDu, Changbin } 569b9fdac7fSDu, Changbin 570dc186ad7SThomas Gleixner /* 571dc186ad7SThomas Gleixner * fixup_init is called when: 572dc186ad7SThomas Gleixner * - an active object is initialized 573dc186ad7SThomas Gleixner */ 57402a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 575dc186ad7SThomas Gleixner { 576dc186ad7SThomas Gleixner struct work_struct *work = addr; 577dc186ad7SThomas Gleixner 578dc186ad7SThomas Gleixner switch (state) { 579dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 580dc186ad7SThomas Gleixner cancel_work_sync(work); 581dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 58202a982a6SDu, Changbin return true; 583dc186ad7SThomas Gleixner default: 58402a982a6SDu, Changbin return false; 585dc186ad7SThomas Gleixner } 586dc186ad7SThomas Gleixner } 587dc186ad7SThomas Gleixner 588dc186ad7SThomas Gleixner /* 589dc186ad7SThomas Gleixner * fixup_free is called when: 590dc186ad7SThomas Gleixner * - an active object is freed 591dc186ad7SThomas Gleixner */ 59202a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 593dc186ad7SThomas Gleixner { 594dc186ad7SThomas Gleixner struct work_struct *work = addr; 595dc186ad7SThomas Gleixner 596dc186ad7SThomas Gleixner switch (state) { 597dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 598dc186ad7SThomas Gleixner cancel_work_sync(work); 599dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 60002a982a6SDu, Changbin return true; 601dc186ad7SThomas Gleixner default: 60202a982a6SDu, Changbin return false; 603dc186ad7SThomas Gleixner } 604dc186ad7SThomas Gleixner } 605dc186ad7SThomas Gleixner 606f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 607dc186ad7SThomas Gleixner .name = "work_struct", 60899777288SStanislaw Gruszka .debug_hint = work_debug_hint, 609b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 610dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 611dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 612dc186ad7SThomas Gleixner }; 613dc186ad7SThomas Gleixner 614dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 615dc186ad7SThomas Gleixner { 616dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 617dc186ad7SThomas Gleixner } 618dc186ad7SThomas Gleixner 619dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 620dc186ad7SThomas Gleixner { 621dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 622dc186ad7SThomas Gleixner } 623dc186ad7SThomas Gleixner 624dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 625dc186ad7SThomas Gleixner { 626dc186ad7SThomas Gleixner if (onstack) 627dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 628dc186ad7SThomas Gleixner else 629dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 630dc186ad7SThomas Gleixner } 631dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 632dc186ad7SThomas Gleixner 633dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 634dc186ad7SThomas Gleixner { 635dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 636dc186ad7SThomas Gleixner } 637dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 638dc186ad7SThomas Gleixner 639ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 640ea2e64f2SThomas Gleixner { 641ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 642ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 643ea2e64f2SThomas Gleixner } 644ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 645ea2e64f2SThomas Gleixner 646dc186ad7SThomas Gleixner #else 647dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 648dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 649dc186ad7SThomas Gleixner #endif 650dc186ad7SThomas Gleixner 6514e8b22bdSLi Bin /** 65267dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6534e8b22bdSLi Bin * @pool: the pool pointer of interest 6544e8b22bdSLi Bin * 6554e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6564e8b22bdSLi Bin * successfully, -errno on failure. 6574e8b22bdSLi Bin */ 6589daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6599daf9e67STejun Heo { 6609daf9e67STejun Heo int ret; 6619daf9e67STejun Heo 66268e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6635bcab335STejun Heo 6644e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6654e8b22bdSLi Bin GFP_KERNEL); 666229641a6STejun Heo if (ret >= 0) { 667e68035fbSTejun Heo pool->id = ret; 668229641a6STejun Heo return 0; 669229641a6STejun Heo } 6709daf9e67STejun Heo return ret; 6719daf9e67STejun Heo } 6729daf9e67STejun Heo 6739f66cff2STejun Heo static struct pool_workqueue __rcu ** 6749f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 6759f66cff2STejun Heo { 6769f66cff2STejun Heo if (cpu >= 0) 6779f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 6789f66cff2STejun Heo else 6799f66cff2STejun Heo return &wq->dfl_pwq; 6809f66cff2STejun Heo } 6819f66cff2STejun Heo 6829f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 6839f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 6849f66cff2STejun Heo { 6859f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 6869f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 6879f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 6889f66cff2STejun Heo } 6899f66cff2STejun Heo 6905797b1c1STejun Heo /** 6915797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 6925797b1c1STejun Heo * @wq: workqueue of interest 6935797b1c1STejun Heo * 6945797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 6955797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 6965797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 6975797b1c1STejun Heo */ 6985797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 6995797b1c1STejun Heo { 7005797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7015797b1c1STejun Heo } 7025797b1c1STejun Heo 70373f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 70473f53c4aSTejun Heo { 70573f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 70673f53c4aSTejun Heo } 70773f53c4aSTejun Heo 708c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 70973f53c4aSTejun Heo { 710c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 71173f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 71273f53c4aSTejun Heo } 71373f53c4aSTejun Heo 71473f53c4aSTejun Heo static int work_next_color(int color) 71573f53c4aSTejun Heo { 71673f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 717a848e3b6SOleg Nesterov } 718a848e3b6SOleg Nesterov 7194594bf15SDavid Howells /* 720112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 721112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7227c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7237a22ad75STejun Heo * 724112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 725112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 726bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 727bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 7287a22ad75STejun Heo * 729112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7307c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 731112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7327c3eed5cSTejun Heo * available only while the work item is queued. 733bbb68dfaSTejun Heo * 734bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 735bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 736bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 737bbb68dfaSTejun Heo * try to steal the PENDING bit. 7384594bf15SDavid Howells */ 7397a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 7407a22ad75STejun Heo unsigned long flags) 7417a22ad75STejun Heo { 7426183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7437a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7447a22ad75STejun Heo } 7457a22ad75STejun Heo 746112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7474690c4abSTejun Heo unsigned long extra_flags) 748365970a1SDavid Howells { 749112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 750112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 751365970a1SDavid Howells } 752365970a1SDavid Howells 7534468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7544468a00fSLai Jiangshan int pool_id) 7554468a00fSLai Jiangshan { 7564468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7574468a00fSLai Jiangshan WORK_STRUCT_PENDING); 7584468a00fSLai Jiangshan } 7594468a00fSLai Jiangshan 7607c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7617c3eed5cSTejun Heo int pool_id) 7624d707b9fSOleg Nesterov { 76323657bb1STejun Heo /* 76423657bb1STejun Heo * The following wmb is paired with the implied mb in 76523657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 76623657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 76723657bb1STejun Heo * owner. 76823657bb1STejun Heo */ 76923657bb1STejun Heo smp_wmb(); 7707c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 771346c09f8SRoman Pen /* 772346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 773346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 774346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7758bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 776346c09f8SRoman Pen * the same @work. E.g. consider this case: 777346c09f8SRoman Pen * 778346c09f8SRoman Pen * CPU#0 CPU#1 779346c09f8SRoman Pen * ---------------------------- -------------------------------- 780346c09f8SRoman Pen * 781346c09f8SRoman Pen * 1 STORE event_indicated 782346c09f8SRoman Pen * 2 queue_work_on() { 783346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 784346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 785346c09f8SRoman Pen * 5 set_work_data() # clear bit 786346c09f8SRoman Pen * 6 smp_mb() 787346c09f8SRoman Pen * 7 work->current_func() { 788346c09f8SRoman Pen * 8 LOAD event_indicated 789346c09f8SRoman Pen * } 790346c09f8SRoman Pen * 791346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 792346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 793346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 794346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 795346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 796346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 797346c09f8SRoman Pen * before actual STORE. 798346c09f8SRoman Pen */ 799346c09f8SRoman Pen smp_mb(); 8004d707b9fSOleg Nesterov } 8014d707b9fSOleg Nesterov 8027a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 803365970a1SDavid Howells { 8047c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 8057c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 8067a22ad75STejun Heo } 8077a22ad75STejun Heo 808afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 809afa4bb77SLinus Torvalds { 810afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 811afa4bb77SLinus Torvalds } 812afa4bb77SLinus Torvalds 813112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8147a22ad75STejun Heo { 815e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8167a22ad75STejun Heo 817112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 818afa4bb77SLinus Torvalds return work_struct_pwq(data); 819e120153dSTejun Heo else 820e120153dSTejun Heo return NULL; 8217a22ad75STejun Heo } 8227a22ad75STejun Heo 8237c3eed5cSTejun Heo /** 8247c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8257c3eed5cSTejun Heo * @work: the work item of interest 8267c3eed5cSTejun Heo * 82768e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 82824acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 82924acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 830fa1b54e6STejun Heo * 831fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 832fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 833fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 834fa1b54e6STejun Heo * returned pool is and stays online. 835d185af30SYacine Belkadi * 836d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8377c3eed5cSTejun Heo */ 8387c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8397a22ad75STejun Heo { 840e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8417c3eed5cSTejun Heo int pool_id; 8427a22ad75STejun Heo 84368e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 844fa1b54e6STejun Heo 845112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 846afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8477a22ad75STejun Heo 8487c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8497c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8507a22ad75STejun Heo return NULL; 8517a22ad75STejun Heo 852fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8537c3eed5cSTejun Heo } 8547c3eed5cSTejun Heo 8557c3eed5cSTejun Heo /** 8567c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8577c3eed5cSTejun Heo * @work: the work item of interest 8587c3eed5cSTejun Heo * 859d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8607c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8617c3eed5cSTejun Heo */ 8627c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8637c3eed5cSTejun Heo { 86454d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 8657c3eed5cSTejun Heo 866112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 867afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 86854d5b7d0SLai Jiangshan 86954d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 8707c3eed5cSTejun Heo } 8717c3eed5cSTejun Heo 872bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 873bbb68dfaSTejun Heo { 8747c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 875bbb68dfaSTejun Heo 8767c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8777c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 878bbb68dfaSTejun Heo } 879bbb68dfaSTejun Heo 880bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 881bbb68dfaSTejun Heo { 882bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 883bbb68dfaSTejun Heo 884112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 885bbb68dfaSTejun Heo } 886bbb68dfaSTejun Heo 887e22bee78STejun Heo /* 8883270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8893270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 890d565ed63STejun Heo * they're being called with pool->lock held. 891e22bee78STejun Heo */ 892e22bee78STejun Heo 893e22bee78STejun Heo /* 894e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 895e22bee78STejun Heo * running workers. 896974271c4STejun Heo * 897974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 898706026c2STejun Heo * function will always return %true for unbound pools as long as the 899974271c4STejun Heo * worklist isn't empty. 900e22bee78STejun Heo */ 90163d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 902e22bee78STejun Heo { 9030219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 904e22bee78STejun Heo } 905e22bee78STejun Heo 906e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 90763d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 908e22bee78STejun Heo { 90963d95a91STejun Heo return pool->nr_idle; 910e22bee78STejun Heo } 911e22bee78STejun Heo 912e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 91363d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 914e22bee78STejun Heo { 915bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 916e22bee78STejun Heo } 917e22bee78STejun Heo 918e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 91963d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 920e22bee78STejun Heo { 92163d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 922e22bee78STejun Heo } 923e22bee78STejun Heo 924e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 92563d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 926e22bee78STejun Heo { 927692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 92863d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 92963d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 930e22bee78STejun Heo 931e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 932e22bee78STejun Heo } 933e22bee78STejun Heo 9344690c4abSTejun Heo /** 935e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 936cb444766STejun Heo * @worker: self 937d302f017STejun Heo * @flags: flags to set 938d302f017STejun Heo * 939228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 940d302f017STejun Heo */ 941228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 942d302f017STejun Heo { 943bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 944e22bee78STejun Heo 945bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 946cb444766STejun Heo 947228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 948e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 949e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 950bc35f7efSLai Jiangshan pool->nr_running--; 951e22bee78STejun Heo } 952e22bee78STejun Heo 953d302f017STejun Heo worker->flags |= flags; 954d302f017STejun Heo } 955d302f017STejun Heo 956d302f017STejun Heo /** 957e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 958cb444766STejun Heo * @worker: self 959d302f017STejun Heo * @flags: flags to clear 960d302f017STejun Heo * 961e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 962d302f017STejun Heo */ 963d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 964d302f017STejun Heo { 96563d95a91STejun Heo struct worker_pool *pool = worker->pool; 966e22bee78STejun Heo unsigned int oflags = worker->flags; 967e22bee78STejun Heo 968bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 969cb444766STejun Heo 970d302f017STejun Heo worker->flags &= ~flags; 971e22bee78STejun Heo 97242c025f3STejun Heo /* 97342c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 97442c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 97542c025f3STejun Heo * of multiple flags, not a single flag. 97642c025f3STejun Heo */ 977e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 978e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 979bc35f7efSLai Jiangshan pool->nr_running++; 980d302f017STejun Heo } 981d302f017STejun Heo 982797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 983797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 984797e8345STejun Heo { 985797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 986797e8345STejun Heo return NULL; 987797e8345STejun Heo 988797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 989797e8345STejun Heo } 990797e8345STejun Heo 991797e8345STejun Heo /** 992797e8345STejun Heo * worker_enter_idle - enter idle state 993797e8345STejun Heo * @worker: worker which is entering idle state 994797e8345STejun Heo * 995797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 996797e8345STejun Heo * necessary. 997797e8345STejun Heo * 998797e8345STejun Heo * LOCKING: 999797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1000797e8345STejun Heo */ 1001797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1002797e8345STejun Heo { 1003797e8345STejun Heo struct worker_pool *pool = worker->pool; 1004797e8345STejun Heo 1005797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1006797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1007797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1008797e8345STejun Heo return; 1009797e8345STejun Heo 1010797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1011797e8345STejun Heo worker->flags |= WORKER_IDLE; 1012797e8345STejun Heo pool->nr_idle++; 1013797e8345STejun Heo worker->last_active = jiffies; 1014797e8345STejun Heo 1015797e8345STejun Heo /* idle_list is LIFO */ 1016797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1017797e8345STejun Heo 1018797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1019797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1020797e8345STejun Heo 1021797e8345STejun Heo /* Sanity check nr_running. */ 1022797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1023797e8345STejun Heo } 1024797e8345STejun Heo 1025797e8345STejun Heo /** 1026797e8345STejun Heo * worker_leave_idle - leave idle state 1027797e8345STejun Heo * @worker: worker which is leaving idle state 1028797e8345STejun Heo * 1029797e8345STejun Heo * @worker is leaving idle state. Update stats. 1030797e8345STejun Heo * 1031797e8345STejun Heo * LOCKING: 1032797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1033797e8345STejun Heo */ 1034797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1035797e8345STejun Heo { 1036797e8345STejun Heo struct worker_pool *pool = worker->pool; 1037797e8345STejun Heo 1038797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1039797e8345STejun Heo return; 1040797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1041797e8345STejun Heo pool->nr_idle--; 1042797e8345STejun Heo list_del_init(&worker->entry); 1043797e8345STejun Heo } 1044797e8345STejun Heo 1045797e8345STejun Heo /** 1046797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1047797e8345STejun Heo * @pool: pool of interest 1048797e8345STejun Heo * @work: work to find worker for 1049797e8345STejun Heo * 1050797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1051797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1052797e8345STejun Heo * to match, its current execution should match the address of @work and 1053797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1054797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1055797e8345STejun Heo * being executed. 1056797e8345STejun Heo * 1057797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1058797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1059797e8345STejun Heo * another work item. If the same work item address ends up being reused 1060797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1061797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1062797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1063797e8345STejun Heo * 1064797e8345STejun Heo * This function checks the work item address and work function to avoid 1065797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1066797e8345STejun Heo * work function which can introduce dependency onto itself through a 1067797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1068797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1069797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1070797e8345STejun Heo * 1071797e8345STejun Heo * CONTEXT: 1072797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1073797e8345STejun Heo * 1074797e8345STejun Heo * Return: 1075797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1076797e8345STejun Heo * otherwise. 1077797e8345STejun Heo */ 1078797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1079797e8345STejun Heo struct work_struct *work) 1080797e8345STejun Heo { 1081797e8345STejun Heo struct worker *worker; 1082797e8345STejun Heo 1083797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1084797e8345STejun Heo (unsigned long)work) 1085797e8345STejun Heo if (worker->current_work == work && 1086797e8345STejun Heo worker->current_func == work->func) 1087797e8345STejun Heo return worker; 1088797e8345STejun Heo 1089797e8345STejun Heo return NULL; 1090797e8345STejun Heo } 1091797e8345STejun Heo 1092797e8345STejun Heo /** 1093797e8345STejun Heo * move_linked_works - move linked works to a list 1094797e8345STejun Heo * @work: start of series of works to be scheduled 1095797e8345STejun Heo * @head: target list to append @work to 1096797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1097797e8345STejun Heo * 1098873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1099873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1100873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1101873eaca6STejun Heo * @nextp. 1102797e8345STejun Heo * 1103797e8345STejun Heo * CONTEXT: 1104797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1105797e8345STejun Heo */ 1106797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1107797e8345STejun Heo struct work_struct **nextp) 1108797e8345STejun Heo { 1109797e8345STejun Heo struct work_struct *n; 1110797e8345STejun Heo 1111797e8345STejun Heo /* 1112797e8345STejun Heo * Linked worklist will always end before the end of the list, 1113797e8345STejun Heo * use NULL for list head. 1114797e8345STejun Heo */ 1115797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1116797e8345STejun Heo list_move_tail(&work->entry, head); 1117797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1118797e8345STejun Heo break; 1119797e8345STejun Heo } 1120797e8345STejun Heo 1121797e8345STejun Heo /* 1122797e8345STejun Heo * If we're already inside safe list traversal and have moved 1123797e8345STejun Heo * multiple works to the scheduled queue, the next position 1124797e8345STejun Heo * needs to be updated. 1125797e8345STejun Heo */ 1126797e8345STejun Heo if (nextp) 1127797e8345STejun Heo *nextp = n; 1128797e8345STejun Heo } 1129797e8345STejun Heo 1130797e8345STejun Heo /** 1131873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1132873eaca6STejun Heo * @work: work to assign 1133873eaca6STejun Heo * @worker: worker to assign to 1134873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1135873eaca6STejun Heo * 1136873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1137873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1138873eaca6STejun Heo * 1139873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1140873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1141873eaca6STejun Heo * list_for_each_entry_safe(). 1142873eaca6STejun Heo * 1143873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1144873eaca6STejun Heo * was punted to another worker already executing it. 1145873eaca6STejun Heo */ 1146873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1147873eaca6STejun Heo struct work_struct **nextp) 1148873eaca6STejun Heo { 1149873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1150873eaca6STejun Heo struct worker *collision; 1151873eaca6STejun Heo 1152873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1153873eaca6STejun Heo 1154873eaca6STejun Heo /* 1155873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1156873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1157873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1158873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1159873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1160873eaca6STejun Heo * defer the work to the currently executing one. 1161873eaca6STejun Heo */ 1162873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1163873eaca6STejun Heo if (unlikely(collision)) { 1164873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1165873eaca6STejun Heo return false; 1166873eaca6STejun Heo } 1167873eaca6STejun Heo 1168873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1169873eaca6STejun Heo return true; 1170873eaca6STejun Heo } 1171873eaca6STejun Heo 1172873eaca6STejun Heo /** 11730219a352STejun Heo * kick_pool - wake up an idle worker if necessary 11740219a352STejun Heo * @pool: pool to kick 1175797e8345STejun Heo * 11760219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 11770219a352STejun Heo * whether a worker was woken up. 1178797e8345STejun Heo */ 11790219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1180797e8345STejun Heo { 1181797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 11828639ecebSTejun Heo struct task_struct *p; 1183797e8345STejun Heo 11840219a352STejun Heo lockdep_assert_held(&pool->lock); 11850219a352STejun Heo 11860219a352STejun Heo if (!need_more_worker(pool) || !worker) 11870219a352STejun Heo return false; 11880219a352STejun Heo 11898639ecebSTejun Heo p = worker->task; 11908639ecebSTejun Heo 11918639ecebSTejun Heo #ifdef CONFIG_SMP 11928639ecebSTejun Heo /* 11938639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 11948639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 11958639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 11968639ecebSTejun Heo * execution locality. 11978639ecebSTejun Heo * 11988639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 11998639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12008639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12018639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12028639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12038639ecebSTejun Heo * still on cpu when picking an idle worker. 12048639ecebSTejun Heo * 12058639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12068639ecebSTejun Heo * its affinity scope. Repatriate. 12078639ecebSTejun Heo */ 12088639ecebSTejun Heo if (!pool->attrs->affn_strict && 12098639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12108639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12118639ecebSTejun Heo struct work_struct, entry); 12128639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12138639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12148639ecebSTejun Heo } 12158639ecebSTejun Heo #endif 12168639ecebSTejun Heo wake_up_process(p); 12170219a352STejun Heo return true; 1218797e8345STejun Heo } 1219797e8345STejun Heo 122063638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 122163638450STejun Heo 122263638450STejun Heo /* 122363638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 122463638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 122563638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 122663638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 122763638450STejun Heo * should be using an unbound workqueue instead. 122863638450STejun Heo * 122963638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 123063638450STejun Heo * and report them so that they can be examined and converted to use unbound 123163638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 123263638450STejun Heo * function is tracked and reported with exponential backoff. 123363638450STejun Heo */ 123463638450STejun Heo #define WCI_MAX_ENTS 128 123563638450STejun Heo 123663638450STejun Heo struct wci_ent { 123763638450STejun Heo work_func_t func; 123863638450STejun Heo atomic64_t cnt; 123963638450STejun Heo struct hlist_node hash_node; 124063638450STejun Heo }; 124163638450STejun Heo 124263638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 124363638450STejun Heo static int wci_nr_ents; 124463638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 124563638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 124663638450STejun Heo 124763638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 124863638450STejun Heo { 124963638450STejun Heo struct wci_ent *ent; 125063638450STejun Heo 125163638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 125263638450STejun Heo (unsigned long)func) { 125363638450STejun Heo if (ent->func == func) 125463638450STejun Heo return ent; 125563638450STejun Heo } 125663638450STejun Heo return NULL; 125763638450STejun Heo } 125863638450STejun Heo 125963638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 126063638450STejun Heo { 126163638450STejun Heo struct wci_ent *ent; 126263638450STejun Heo 126363638450STejun Heo restart: 126463638450STejun Heo ent = wci_find_ent(func); 126563638450STejun Heo if (ent) { 126663638450STejun Heo u64 cnt; 126763638450STejun Heo 126863638450STejun Heo /* 126963638450STejun Heo * Start reporting from the fourth time and back off 127063638450STejun Heo * exponentially. 127163638450STejun Heo */ 127263638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 127363638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 127463638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 127563638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 127663638450STejun Heo atomic64_read(&ent->cnt)); 127763638450STejun Heo return; 127863638450STejun Heo } 127963638450STejun Heo 128063638450STejun Heo /* 128163638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 128263638450STejun Heo * is exhausted, something went really wrong and we probably made enough 128363638450STejun Heo * noise already. 128463638450STejun Heo */ 128563638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 128663638450STejun Heo return; 128763638450STejun Heo 128863638450STejun Heo raw_spin_lock(&wci_lock); 128963638450STejun Heo 129063638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 129163638450STejun Heo raw_spin_unlock(&wci_lock); 129263638450STejun Heo return; 129363638450STejun Heo } 129463638450STejun Heo 129563638450STejun Heo if (wci_find_ent(func)) { 129663638450STejun Heo raw_spin_unlock(&wci_lock); 129763638450STejun Heo goto restart; 129863638450STejun Heo } 129963638450STejun Heo 130063638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 130163638450STejun Heo ent->func = func; 130263638450STejun Heo atomic64_set(&ent->cnt, 1); 130363638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 130463638450STejun Heo 130563638450STejun Heo raw_spin_unlock(&wci_lock); 130663638450STejun Heo } 130763638450STejun Heo 130863638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 130963638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 131063638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 131163638450STejun Heo 1312c54d5046STejun Heo /** 13131da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13141da177e4SLinus Torvalds * @task: task waking up 13151da177e4SLinus Torvalds * 13161da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13171da177e4SLinus Torvalds */ 13181da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13191da177e4SLinus Torvalds { 13201da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13211da177e4SLinus Torvalds 1322c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13231da177e4SLinus Torvalds return; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds /* 13261da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13271da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13281da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13291da177e4SLinus Torvalds * pool. Protect against such race. 13301da177e4SLinus Torvalds */ 13311da177e4SLinus Torvalds preempt_disable(); 13321da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13331da177e4SLinus Torvalds worker->pool->nr_running++; 13341da177e4SLinus Torvalds preempt_enable(); 1335616db877STejun Heo 1336616db877STejun Heo /* 1337616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1338616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1339616db877STejun Heo */ 1340616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1341616db877STejun Heo 1342c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 13431da177e4SLinus Torvalds } 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /** 13461da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 13471da177e4SLinus Torvalds * @task: task going to sleep 13481da177e4SLinus Torvalds * 13491da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 13501da177e4SLinus Torvalds * going to sleep. 13511da177e4SLinus Torvalds */ 13521da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13551da177e4SLinus Torvalds struct worker_pool *pool; 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 13591da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 13601da177e4SLinus Torvalds * checking NOT_RUNNING. 13611da177e4SLinus Torvalds */ 13621da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 13631da177e4SLinus Torvalds return; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds pool = worker->pool; 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1368c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 13691da177e4SLinus Torvalds return; 13701da177e4SLinus Torvalds 1371c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 13721da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /* 13751da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 13761da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 13771da177e4SLinus Torvalds * and nr_running has been reset. 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 13801da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13811da177e4SLinus Torvalds return; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds pool->nr_running--; 13850219a352STejun Heo if (kick_pool(pool)) 1386725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 13870219a352STejun Heo 13881da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds /** 1392616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1393616db877STejun Heo * @task: task currently running 1394616db877STejun Heo * 1395616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1396616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1397616db877STejun Heo */ 1398616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1399616db877STejun Heo { 1400616db877STejun Heo struct worker *worker = kthread_data(task); 1401616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1402616db877STejun Heo struct worker_pool *pool = worker->pool; 1403616db877STejun Heo 1404616db877STejun Heo if (!pwq) 1405616db877STejun Heo return; 1406616db877STejun Heo 14078a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14088a1dd1e5STejun Heo 140918c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 141018c8ae81SZqiang return; 141118c8ae81SZqiang 1412616db877STejun Heo /* 1413616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1414616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1415616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1416c8f6219bSZqiang * 1417c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1418c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1419c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1420c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1421c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1422c8f6219bSZqiang * We probably want to make this prettier in the future. 1423616db877STejun Heo */ 1424c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1425616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1426616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1427616db877STejun Heo return; 1428616db877STejun Heo 1429616db877STejun Heo raw_spin_lock(&pool->lock); 1430616db877STejun Heo 1431616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 143263638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1433616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1434616db877STejun Heo 14350219a352STejun Heo if (kick_pool(pool)) 1436616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1437616db877STejun Heo 1438616db877STejun Heo raw_spin_unlock(&pool->lock); 1439616db877STejun Heo } 1440616db877STejun Heo 1441616db877STejun Heo /** 14421da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 14431da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 14441da177e4SLinus Torvalds * 14451da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 14461da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 14471da177e4SLinus Torvalds * 14481da177e4SLinus Torvalds * CONTEXT: 14499b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 14501da177e4SLinus Torvalds * 14511da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1452f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1453f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 14541da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 14551da177e4SLinus Torvalds * 14561da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 14571da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 14581da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 14591da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1460365970a1SDavid Howells * 1461365970a1SDavid Howells * Return: 1462365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1463365970a1SDavid Howells * hasn't executed any work yet. 1464365970a1SDavid Howells */ 1465365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1466365970a1SDavid Howells { 1467365970a1SDavid Howells struct worker *worker = kthread_data(task); 1468365970a1SDavid Howells 1469365970a1SDavid Howells return worker->last_func; 1470365970a1SDavid Howells } 1471365970a1SDavid Howells 1472d302f017STejun Heo /** 147391ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 147491ccc6e7STejun Heo * @wq: workqueue of interest 147591ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 147691ccc6e7STejun Heo * 147791ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 147891ccc6e7STejun Heo * 147991ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 148091ccc6e7STejun Heo * 148191ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 148291ccc6e7STejun Heo * 148391ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 148491ccc6e7STejun Heo */ 148591ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 148691ccc6e7STejun Heo int node) 148791ccc6e7STejun Heo { 148891ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 148991ccc6e7STejun Heo return NULL; 149091ccc6e7STejun Heo 149191ccc6e7STejun Heo if (node == NUMA_NO_NODE) 149291ccc6e7STejun Heo node = nr_node_ids; 149391ccc6e7STejun Heo 149491ccc6e7STejun Heo return wq->node_nr_active[node]; 149591ccc6e7STejun Heo } 149691ccc6e7STejun Heo 149791ccc6e7STejun Heo /** 14985797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 14995797b1c1STejun Heo * @wq: workqueue to update 15005797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15015797b1c1STejun Heo * 15025797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15035797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15045797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15055797b1c1STejun Heo */ 15065797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15075797b1c1STejun Heo { 15085797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15095797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15105797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15115797b1c1STejun Heo int total_cpus, node; 15125797b1c1STejun Heo 15135797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15145797b1c1STejun Heo 1515c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1516c5f8cd6cSTejun Heo return; 1517c5f8cd6cSTejun Heo 151815930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15195797b1c1STejun Heo off_cpu = -1; 15205797b1c1STejun Heo 15215797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15225797b1c1STejun Heo if (off_cpu >= 0) 15235797b1c1STejun Heo total_cpus--; 15245797b1c1STejun Heo 15255797b1c1STejun Heo for_each_node(node) { 15265797b1c1STejun Heo int node_cpus; 15275797b1c1STejun Heo 15285797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15295797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15305797b1c1STejun Heo node_cpus--; 15315797b1c1STejun Heo 15325797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 15335797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 15345797b1c1STejun Heo min_active, max_active); 15355797b1c1STejun Heo } 15365797b1c1STejun Heo 15375797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 15385797b1c1STejun Heo } 15395797b1c1STejun Heo 15405797b1c1STejun Heo /** 15418864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 15428864b4e5STejun Heo * @pwq: pool_workqueue to get 15438864b4e5STejun Heo * 15448864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 15458864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 15468864b4e5STejun Heo */ 15478864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 15488864b4e5STejun Heo { 15498864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 15508864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 15518864b4e5STejun Heo pwq->refcnt++; 15528864b4e5STejun Heo } 15538864b4e5STejun Heo 15548864b4e5STejun Heo /** 15558864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 15568864b4e5STejun Heo * @pwq: pool_workqueue to put 15578864b4e5STejun Heo * 15588864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 15598864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 15608864b4e5STejun Heo */ 15618864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 15628864b4e5STejun Heo { 15638864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 15648864b4e5STejun Heo if (likely(--pwq->refcnt)) 15658864b4e5STejun Heo return; 15668864b4e5STejun Heo /* 1567967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1568967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 15698864b4e5STejun Heo */ 1570687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 15718864b4e5STejun Heo } 15728864b4e5STejun Heo 1573dce90d47STejun Heo /** 1574dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1575dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1576dce90d47STejun Heo * 1577dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1578dce90d47STejun Heo */ 1579dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1580dce90d47STejun Heo { 1581dce90d47STejun Heo if (pwq) { 1582dce90d47STejun Heo /* 158324acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1584dce90d47STejun Heo * following lock operations are safe. 1585dce90d47STejun Heo */ 1586a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1587dce90d47STejun Heo put_pwq(pwq); 1588a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1589dce90d47STejun Heo } 1590dce90d47STejun Heo } 1591dce90d47STejun Heo 1592afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1593afa87ce8STejun Heo { 1594afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1595afa87ce8STejun Heo } 1596afa87ce8STejun Heo 15974c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 15984c638030STejun Heo struct work_struct *work) 1599bf4ede01STejun Heo { 16001c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16011c270b79STejun Heo 16021c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1603bf4ede01STejun Heo trace_workqueue_activate_work(work); 160482607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 160582607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1606112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16071c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16084c638030STejun Heo } 16094c638030STejun Heo 16104c638030STejun Heo /** 16114c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16124c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16134c638030STejun Heo * @work: work item to activate 16144c638030STejun Heo * 16154c638030STejun Heo * Returns %true if activated. %false if already active. 16164c638030STejun Heo */ 16174c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16184c638030STejun Heo struct work_struct *work) 16194c638030STejun Heo { 16204c638030STejun Heo struct worker_pool *pool = pwq->pool; 162191ccc6e7STejun Heo struct wq_node_nr_active *nna; 16224c638030STejun Heo 16234c638030STejun Heo lockdep_assert_held(&pool->lock); 16244c638030STejun Heo 16254c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16264c638030STejun Heo return false; 16274c638030STejun Heo 162891ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 162991ccc6e7STejun Heo if (nna) 163091ccc6e7STejun Heo atomic_inc(&nna->nr); 163191ccc6e7STejun Heo 1632112202d9STejun Heo pwq->nr_active++; 16334c638030STejun Heo __pwq_activate_work(pwq, work); 16344c638030STejun Heo return true; 1635bf4ede01STejun Heo } 1636bf4ede01STejun Heo 16375797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 16385797b1c1STejun Heo { 16395797b1c1STejun Heo int max = READ_ONCE(nna->max); 16405797b1c1STejun Heo 16415797b1c1STejun Heo while (true) { 16425797b1c1STejun Heo int old, tmp; 16435797b1c1STejun Heo 16445797b1c1STejun Heo old = atomic_read(&nna->nr); 16455797b1c1STejun Heo if (old >= max) 16465797b1c1STejun Heo return false; 16475797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 16485797b1c1STejun Heo if (tmp == old) 16495797b1c1STejun Heo return true; 16505797b1c1STejun Heo } 16515797b1c1STejun Heo } 16525797b1c1STejun Heo 16531c270b79STejun Heo /** 16541c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 16551c270b79STejun Heo * @pwq: pool_workqueue of interest 16565797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 16571c270b79STejun Heo * 16581c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 16591c270b79STejun Heo * successfully obtained. %false otherwise. 16601c270b79STejun Heo */ 16615797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 16623aa62497SLai Jiangshan { 16631c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 16641c270b79STejun Heo struct worker_pool *pool = pwq->pool; 166591ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 16665797b1c1STejun Heo bool obtained = false; 16671c270b79STejun Heo 16681c270b79STejun Heo lockdep_assert_held(&pool->lock); 16691c270b79STejun Heo 16705797b1c1STejun Heo if (!nna) { 16715797b1c1STejun Heo /* per-cpu workqueue, pwq->nr_active is sufficient */ 16721c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 16735797b1c1STejun Heo goto out; 167491ccc6e7STejun Heo } 16755797b1c1STejun Heo 16765797b1c1STejun Heo /* 16775797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 16785797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 16795797b1c1STejun Heo * concurrency level. Don't jump the line. 16805797b1c1STejun Heo * 16815797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 16825797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 16835797b1c1STejun Heo * increase it. This is indicated by @fill. 16845797b1c1STejun Heo */ 16855797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 16865797b1c1STejun Heo goto out; 16875797b1c1STejun Heo 16885797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 16895797b1c1STejun Heo if (obtained) 16905797b1c1STejun Heo goto out; 16915797b1c1STejun Heo 16925797b1c1STejun Heo /* 16935797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 16945797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 16955797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 16965797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 16975797b1c1STejun Heo * $nna->pending_pwqs. 16985797b1c1STejun Heo */ 16995797b1c1STejun Heo raw_spin_lock(&nna->lock); 17005797b1c1STejun Heo 17015797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17025797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17035797b1c1STejun Heo else if (likely(!fill)) 17045797b1c1STejun Heo goto out_unlock; 17055797b1c1STejun Heo 17065797b1c1STejun Heo smp_mb(); 17075797b1c1STejun Heo 17085797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17095797b1c1STejun Heo 17105797b1c1STejun Heo /* 17115797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17125797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17135797b1c1STejun Heo */ 17145797b1c1STejun Heo if (obtained && likely(!fill)) 17155797b1c1STejun Heo list_del_init(&pwq->pending_node); 17165797b1c1STejun Heo 17175797b1c1STejun Heo out_unlock: 17185797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17195797b1c1STejun Heo out: 17205797b1c1STejun Heo if (obtained) 17215797b1c1STejun Heo pwq->nr_active++; 17221c270b79STejun Heo return obtained; 17231c270b79STejun Heo } 17241c270b79STejun Heo 17251c270b79STejun Heo /** 17261c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17271c270b79STejun Heo * @pwq: pool_workqueue of interest 17285797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17291c270b79STejun Heo * 17301c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 17311c270b79STejun Heo * max_active limit. 17321c270b79STejun Heo * 17331c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 17341c270b79STejun Heo * inactive work item is found or max_active limit is reached. 17351c270b79STejun Heo */ 17365797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 17371c270b79STejun Heo { 17381c270b79STejun Heo struct work_struct *work = 17391c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 17403aa62497SLai Jiangshan struct work_struct, entry); 17413aa62497SLai Jiangshan 17425797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 17431c270b79STejun Heo __pwq_activate_work(pwq, work); 17441c270b79STejun Heo return true; 17451c270b79STejun Heo } else { 17461c270b79STejun Heo return false; 17471c270b79STejun Heo } 17481c270b79STejun Heo } 17491c270b79STejun Heo 17501c270b79STejun Heo /** 17515797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 17525797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 17535797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 17545797b1c1STejun Heo * 17555797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 17565797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 17575797b1c1STejun Heo */ 17585797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 17595797b1c1STejun Heo struct worker_pool *caller_pool) 17605797b1c1STejun Heo { 17615797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 17625797b1c1STejun Heo struct pool_workqueue *pwq; 17635797b1c1STejun Heo struct work_struct *work; 17645797b1c1STejun Heo 17655797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 17665797b1c1STejun Heo 17675797b1c1STejun Heo raw_spin_lock(&nna->lock); 17685797b1c1STejun Heo retry: 17695797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 17705797b1c1STejun Heo struct pool_workqueue, pending_node); 17715797b1c1STejun Heo if (!pwq) 17725797b1c1STejun Heo goto out_unlock; 17735797b1c1STejun Heo 17745797b1c1STejun Heo /* 17755797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 17765797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 17775797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 17785797b1c1STejun Heo * nested inside pool locks. 17795797b1c1STejun Heo */ 17805797b1c1STejun Heo if (pwq->pool != locked_pool) { 17815797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 17825797b1c1STejun Heo locked_pool = pwq->pool; 17835797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 17845797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17855797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 17865797b1c1STejun Heo raw_spin_lock(&nna->lock); 17875797b1c1STejun Heo goto retry; 17885797b1c1STejun Heo } 17895797b1c1STejun Heo } 17905797b1c1STejun Heo 17915797b1c1STejun Heo /* 17925797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 17935797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 17945797b1c1STejun Heo */ 17955797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 17965797b1c1STejun Heo struct work_struct, entry); 17975797b1c1STejun Heo if (!work) { 17985797b1c1STejun Heo list_del_init(&pwq->pending_node); 17995797b1c1STejun Heo goto retry; 18005797b1c1STejun Heo } 18015797b1c1STejun Heo 18025797b1c1STejun Heo /* 18035797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 18045797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 18055797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 18065797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 18075797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 18085797b1c1STejun Heo */ 18095797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 18105797b1c1STejun Heo pwq->nr_active++; 18115797b1c1STejun Heo __pwq_activate_work(pwq, work); 18125797b1c1STejun Heo 18135797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 18145797b1c1STejun Heo list_del_init(&pwq->pending_node); 18155797b1c1STejun Heo else 18165797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 18175797b1c1STejun Heo 18185797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 18195797b1c1STejun Heo if (pwq->pool != caller_pool) 18205797b1c1STejun Heo kick_pool(pwq->pool); 18215797b1c1STejun Heo } 18225797b1c1STejun Heo 18235797b1c1STejun Heo out_unlock: 18245797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18255797b1c1STejun Heo if (locked_pool != caller_pool) { 18265797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18275797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 18285797b1c1STejun Heo } 18295797b1c1STejun Heo } 18305797b1c1STejun Heo 18315797b1c1STejun Heo /** 18321c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 18331c270b79STejun Heo * @pwq: pool_workqueue of interest 18341c270b79STejun Heo * 18351c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 18365797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 18371c270b79STejun Heo */ 18381c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 18391c270b79STejun Heo { 18401c270b79STejun Heo struct worker_pool *pool = pwq->pool; 184191ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 18421c270b79STejun Heo 18431c270b79STejun Heo lockdep_assert_held(&pool->lock); 18441c270b79STejun Heo 184591ccc6e7STejun Heo /* 184691ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 184791ccc6e7STejun Heo * workqueues. 184891ccc6e7STejun Heo */ 18491c270b79STejun Heo pwq->nr_active--; 185091ccc6e7STejun Heo 185191ccc6e7STejun Heo /* 185291ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 185391ccc6e7STejun Heo * inactive work item on @pwq itself. 185491ccc6e7STejun Heo */ 185591ccc6e7STejun Heo if (!nna) { 18565797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 185791ccc6e7STejun Heo return; 185891ccc6e7STejun Heo } 185991ccc6e7STejun Heo 18605797b1c1STejun Heo /* 18615797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 18625797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 18635797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 18645797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 18655797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 18665797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 18675797b1c1STejun Heo * decremented $nna->nr. 18685797b1c1STejun Heo * 18695797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 18705797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 18715797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 18725797b1c1STejun Heo * This maintains the forward progress guarantee. 18735797b1c1STejun Heo */ 18745797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 18755797b1c1STejun Heo return; 18765797b1c1STejun Heo 18775797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 18785797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 18793aa62497SLai Jiangshan } 18803aa62497SLai Jiangshan 1881bf4ede01STejun Heo /** 1882112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1883112202d9STejun Heo * @pwq: pwq of interest 1884c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1885bf4ede01STejun Heo * 1886bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1887112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1888bf4ede01STejun Heo * 1889dd6c3c54STejun Heo * NOTE: 1890dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1891dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1892dd6c3c54STejun Heo * work item is complete. 1893dd6c3c54STejun Heo * 1894bf4ede01STejun Heo * CONTEXT: 1895a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1896bf4ede01STejun Heo */ 1897c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1898bf4ede01STejun Heo { 1899c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1900c4560c2cSLai Jiangshan 19011c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 19021c270b79STejun Heo pwq_dec_nr_active(pwq); 1903018f3a13SLai Jiangshan 1904018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1905bf4ede01STejun Heo 1906bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1907112202d9STejun Heo if (likely(pwq->flush_color != color)) 19088864b4e5STejun Heo goto out_put; 1909bf4ede01STejun Heo 1910bf4ede01STejun Heo /* are there still in-flight works? */ 1911112202d9STejun Heo if (pwq->nr_in_flight[color]) 19128864b4e5STejun Heo goto out_put; 1913bf4ede01STejun Heo 1914112202d9STejun Heo /* this pwq is done, clear flush_color */ 1915112202d9STejun Heo pwq->flush_color = -1; 1916bf4ede01STejun Heo 1917bf4ede01STejun Heo /* 1918112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1919bf4ede01STejun Heo * will handle the rest. 1920bf4ede01STejun Heo */ 1921112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1922112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 19238864b4e5STejun Heo out_put: 19248864b4e5STejun Heo put_pwq(pwq); 1925bf4ede01STejun Heo } 1926bf4ede01STejun Heo 192736e227d2STejun Heo /** 1928bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 192936e227d2STejun Heo * @work: work item to steal 193036e227d2STejun Heo * @is_dwork: @work is a delayed_work 1931bbb68dfaSTejun Heo * @flags: place to store irq state 193236e227d2STejun Heo * 193336e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1934d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 193536e227d2STejun Heo * 1936d185af30SYacine Belkadi * Return: 19373eb6b31bSMauro Carvalho Chehab * 19383eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 193936e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 194036e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 194136e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1942bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1943bbb68dfaSTejun Heo * for arbitrarily long 19443eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 194536e227d2STejun Heo * 1946d185af30SYacine Belkadi * Note: 1947bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1948e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1949e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1950e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1951bbb68dfaSTejun Heo * 1952bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1953bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1954bbb68dfaSTejun Heo * 1955e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1956bf4ede01STejun Heo */ 1957bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1958bbb68dfaSTejun Heo unsigned long *flags) 1959bf4ede01STejun Heo { 1960d565ed63STejun Heo struct worker_pool *pool; 1961112202d9STejun Heo struct pool_workqueue *pwq; 1962bf4ede01STejun Heo 1963bbb68dfaSTejun Heo local_irq_save(*flags); 1964bbb68dfaSTejun Heo 196536e227d2STejun Heo /* try to steal the timer if it exists */ 196636e227d2STejun Heo if (is_dwork) { 196736e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 196836e227d2STejun Heo 1969e0aecdd8STejun Heo /* 1970e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1971e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1972e0aecdd8STejun Heo * running on the local CPU. 1973e0aecdd8STejun Heo */ 197436e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 197536e227d2STejun Heo return 1; 197636e227d2STejun Heo } 197736e227d2STejun Heo 197836e227d2STejun Heo /* try to claim PENDING the normal way */ 1979bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1980bf4ede01STejun Heo return 0; 1981bf4ede01STejun Heo 198224acfb71SThomas Gleixner rcu_read_lock(); 1983bf4ede01STejun Heo /* 1984bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1985bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1986bf4ede01STejun Heo */ 1987d565ed63STejun Heo pool = get_work_pool(work); 1988d565ed63STejun Heo if (!pool) 1989bbb68dfaSTejun Heo goto fail; 1990bf4ede01STejun Heo 1991a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1992bf4ede01STejun Heo /* 1993112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1994112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1995112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1996112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1997112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 19980b3dae68SLai Jiangshan * item is currently queued on that pool. 1999bf4ede01STejun Heo */ 2000112202d9STejun Heo pwq = get_work_pwq(work); 2001112202d9STejun Heo if (pwq && pwq->pool == pool) { 2002c70e1779STejun Heo unsigned long work_data; 2003c70e1779STejun Heo 2004bf4ede01STejun Heo debug_work_deactivate(work); 20053aa62497SLai Jiangshan 20063aa62497SLai Jiangshan /* 2007018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2008018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2009018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2010018f3a13SLai Jiangshan * 2011f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2012d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2013f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 201416062836STejun Heo * management later on and cause stall. Make sure the work 201516062836STejun Heo * item is activated before grabbing. 20163aa62497SLai Jiangshan */ 20174c638030STejun Heo pwq_activate_work(pwq, work); 20183aa62497SLai Jiangshan 2019bf4ede01STejun Heo list_del_init(&work->entry); 202036e227d2STejun Heo 2021c70e1779STejun Heo /* 2022c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2023c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2024c70e1779STejun Heo */ 2025c70e1779STejun Heo work_data = *work_data_bits(work); 20264468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 20274468a00fSLai Jiangshan 2028dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2029c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2030dd6c3c54STejun Heo 2031a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 203224acfb71SThomas Gleixner rcu_read_unlock(); 203336e227d2STejun Heo return 1; 2034bf4ede01STejun Heo } 2035a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2036bbb68dfaSTejun Heo fail: 203724acfb71SThomas Gleixner rcu_read_unlock(); 2038bbb68dfaSTejun Heo local_irq_restore(*flags); 2039bbb68dfaSTejun Heo if (work_is_canceling(work)) 2040bbb68dfaSTejun Heo return -ENOENT; 2041bbb68dfaSTejun Heo cpu_relax(); 204236e227d2STejun Heo return -EAGAIN; 2043bf4ede01STejun Heo } 2044bf4ede01STejun Heo 2045bf4ede01STejun Heo /** 2046706026c2STejun Heo * insert_work - insert a work into a pool 2047112202d9STejun Heo * @pwq: pwq @work belongs to 20484690c4abSTejun Heo * @work: work to insert 20494690c4abSTejun Heo * @head: insertion point 20504690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 20514690c4abSTejun Heo * 2052112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2053706026c2STejun Heo * work_struct flags. 20544690c4abSTejun Heo * 20554690c4abSTejun Heo * CONTEXT: 2056a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2057365970a1SDavid Howells */ 2058112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2059112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2060b89deed3SOleg Nesterov { 2061fe089f87STejun Heo debug_work_activate(work); 2062e1d8aa9fSFrederic Weisbecker 2063e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2064f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2065e89a85d6SWalter Wu 20664690c4abSTejun Heo /* we own @work, set data and link */ 2067112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 20681a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 20698864b4e5STejun Heo get_pwq(pwq); 2070b89deed3SOleg Nesterov } 2071b89deed3SOleg Nesterov 2072c8efcc25STejun Heo /* 2073c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 20748d03ecfeSTejun Heo * same workqueue. 2075c8efcc25STejun Heo */ 2076c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2077c8efcc25STejun Heo { 2078c8efcc25STejun Heo struct worker *worker; 2079c8efcc25STejun Heo 20808d03ecfeSTejun Heo worker = current_wq_worker(); 2081c8efcc25STejun Heo /* 2082bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 20838d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2084c8efcc25STejun Heo */ 2085112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2086c8efcc25STejun Heo } 2087c8efcc25STejun Heo 2088ef557180SMike Galbraith /* 2089ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2090ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2091ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2092ef557180SMike Galbraith */ 2093ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2094ef557180SMike Galbraith { 2095ef557180SMike Galbraith int new_cpu; 2096ef557180SMike Galbraith 2097f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2098ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2099ef557180SMike Galbraith return cpu; 2100a8ec5880SAmmar Faizi } else { 2101a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2102f303fccbSTejun Heo } 2103f303fccbSTejun Heo 2104ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2105ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2106ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2107ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2108ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2109ef557180SMike Galbraith return cpu; 2110ef557180SMike Galbraith } 2111ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2112ef557180SMike Galbraith 2113ef557180SMike Galbraith return new_cpu; 2114ef557180SMike Galbraith } 2115ef557180SMike Galbraith 2116d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 21171da177e4SLinus Torvalds struct work_struct *work) 21181da177e4SLinus Torvalds { 2119112202d9STejun Heo struct pool_workqueue *pwq; 2120fe089f87STejun Heo struct worker_pool *last_pool, *pool; 21218a2e8e5dSTejun Heo unsigned int work_flags; 2122b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 21238930cabaSTejun Heo 21248930cabaSTejun Heo /* 21258930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 21268930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 21278930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 21288930cabaSTejun Heo * happen with IRQ disabled. 21298930cabaSTejun Heo */ 21308e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 21311da177e4SLinus Torvalds 21321e19ffc6STejun Heo 213333e3f0a3SRichard Clark /* 213433e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 213533e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 213633e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 213733e3f0a3SRichard Clark */ 213833e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 213933e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2140e41e704bSTejun Heo return; 214124acfb71SThomas Gleixner rcu_read_lock(); 21429e8cd2f5STejun Heo retry: 2143aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2144636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2145636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2146ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2147636b927eSTejun Heo else 2148aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2149aa202f1fSHillf Danton } 2150f3421797STejun Heo 2151636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2152fe089f87STejun Heo pool = pwq->pool; 2153fe089f87STejun Heo 215418aa9effSTejun Heo /* 2155c9178087STejun Heo * If @work was previously on a different pool, it might still be 2156c9178087STejun Heo * running there, in which case the work needs to be queued on that 2157c9178087STejun Heo * pool to guarantee non-reentrancy. 215818aa9effSTejun Heo */ 2159c9e7cf27STejun Heo last_pool = get_work_pool(work); 2160fe089f87STejun Heo if (last_pool && last_pool != pool) { 216118aa9effSTejun Heo struct worker *worker; 216218aa9effSTejun Heo 2163a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 216418aa9effSTejun Heo 2165c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 216618aa9effSTejun Heo 2167112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2168c9178087STejun Heo pwq = worker->current_pwq; 2169fe089f87STejun Heo pool = pwq->pool; 2170fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 21718594fadeSLai Jiangshan } else { 217218aa9effSTejun Heo /* meh... not running there, queue here */ 2173a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2174fe089f87STejun Heo raw_spin_lock(&pool->lock); 217518aa9effSTejun Heo } 21768930cabaSTejun Heo } else { 2177fe089f87STejun Heo raw_spin_lock(&pool->lock); 21788930cabaSTejun Heo } 2179502ca9d8STejun Heo 21809e8cd2f5STejun Heo /* 2181636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2182636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2183636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2184636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2185636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 21869e8cd2f5STejun Heo */ 21879e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 21889e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2189fe089f87STejun Heo raw_spin_unlock(&pool->lock); 21909e8cd2f5STejun Heo cpu_relax(); 21919e8cd2f5STejun Heo goto retry; 21929e8cd2f5STejun Heo } 21939e8cd2f5STejun Heo /* oops */ 21949e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 21959e8cd2f5STejun Heo wq->name, cpu); 21969e8cd2f5STejun Heo } 21979e8cd2f5STejun Heo 2198112202d9STejun Heo /* pwq determined, queue */ 2199112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2200502ca9d8STejun Heo 220124acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 220224acfb71SThomas Gleixner goto out; 22031e19ffc6STejun Heo 2204112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2205112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 22061e19ffc6STejun Heo 2207a045a272STejun Heo /* 2208a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2209a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2210a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2211a045a272STejun Heo */ 22125797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2213fe089f87STejun Heo if (list_empty(&pool->worklist)) 2214fe089f87STejun Heo pool->watchdog_ts = jiffies; 2215fe089f87STejun Heo 2216cdadf009STejun Heo trace_workqueue_activate_work(work); 2217fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 22180219a352STejun Heo kick_pool(pool); 22198a2e8e5dSTejun Heo } else { 2220f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2221fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 22228a2e8e5dSTejun Heo } 22231e19ffc6STejun Heo 222424acfb71SThomas Gleixner out: 2225fe089f87STejun Heo raw_spin_unlock(&pool->lock); 222624acfb71SThomas Gleixner rcu_read_unlock(); 22271da177e4SLinus Torvalds } 22281da177e4SLinus Torvalds 22290fcb78c2SRolf Eike Beer /** 2230c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2231c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2232c1a220e7SZhang Rui * @wq: workqueue to use 2233c1a220e7SZhang Rui * @work: work to queue 2234c1a220e7SZhang Rui * 2235c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2236443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2237443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2238854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2239854f5cc5SPaul E. McKenney * online will get a splat. 2240d185af30SYacine Belkadi * 2241d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2242c1a220e7SZhang Rui */ 2243d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2244d4283e93STejun Heo struct work_struct *work) 2245c1a220e7SZhang Rui { 2246d4283e93STejun Heo bool ret = false; 22478930cabaSTejun Heo unsigned long flags; 22488930cabaSTejun Heo 22498930cabaSTejun Heo local_irq_save(flags); 2250c1a220e7SZhang Rui 225122df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 22524690c4abSTejun Heo __queue_work(cpu, wq, work); 2253d4283e93STejun Heo ret = true; 2254c1a220e7SZhang Rui } 22558930cabaSTejun Heo 22568930cabaSTejun Heo local_irq_restore(flags); 2257c1a220e7SZhang Rui return ret; 2258c1a220e7SZhang Rui } 2259ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2260c1a220e7SZhang Rui 22618204e0c1SAlexander Duyck /** 2262fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 22638204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 22648204e0c1SAlexander Duyck * 22658204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 22668204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 22678204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 22688204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 22698204e0c1SAlexander Duyck */ 2270fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 22718204e0c1SAlexander Duyck { 22728204e0c1SAlexander Duyck int cpu; 22738204e0c1SAlexander Duyck 22748204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 22758204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 22768204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 22778204e0c1SAlexander Duyck 22788204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 22798204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 22808204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 22818204e0c1SAlexander Duyck return cpu; 22828204e0c1SAlexander Duyck 22838204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 22848204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 22858204e0c1SAlexander Duyck 22868204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 22878204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 22888204e0c1SAlexander Duyck } 22898204e0c1SAlexander Duyck 22908204e0c1SAlexander Duyck /** 22918204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 22928204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 22938204e0c1SAlexander Duyck * @wq: workqueue to use 22948204e0c1SAlexander Duyck * @work: work to queue 22958204e0c1SAlexander Duyck * 22968204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 22978204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 22988204e0c1SAlexander Duyck * NUMA node. 22998204e0c1SAlexander Duyck * 23008204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 23018204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 23028204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 23038204e0c1SAlexander Duyck * 23048204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 23058204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 23068204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 23078204e0c1SAlexander Duyck * 23088204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 23098204e0c1SAlexander Duyck */ 23108204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 23118204e0c1SAlexander Duyck struct work_struct *work) 23128204e0c1SAlexander Duyck { 23138204e0c1SAlexander Duyck unsigned long flags; 23148204e0c1SAlexander Duyck bool ret = false; 23158204e0c1SAlexander Duyck 23168204e0c1SAlexander Duyck /* 23178204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 23188204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 23198204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 23208204e0c1SAlexander Duyck * 23218204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 23228204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 23238204e0c1SAlexander Duyck * some round robin type logic. 23248204e0c1SAlexander Duyck */ 23258204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 23268204e0c1SAlexander Duyck 23278204e0c1SAlexander Duyck local_irq_save(flags); 23288204e0c1SAlexander Duyck 23298204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2330fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 23318204e0c1SAlexander Duyck 23328204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 23338204e0c1SAlexander Duyck ret = true; 23348204e0c1SAlexander Duyck } 23358204e0c1SAlexander Duyck 23368204e0c1SAlexander Duyck local_irq_restore(flags); 23378204e0c1SAlexander Duyck return ret; 23388204e0c1SAlexander Duyck } 23398204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 23408204e0c1SAlexander Duyck 23418c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 23421da177e4SLinus Torvalds { 23438c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 23441da177e4SLinus Torvalds 2345e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 234660c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 23471da177e4SLinus Torvalds } 23481438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 23491da177e4SLinus Torvalds 23507beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 235152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 23521da177e4SLinus Torvalds { 23537beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 23547beb2edfSTejun Heo struct work_struct *work = &dwork->work; 23551da177e4SLinus Torvalds 2356637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 23574b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2358fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2359fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 23607beb2edfSTejun Heo 23618852aac2STejun Heo /* 23628852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 23638852aac2STejun Heo * both optimization and correctness. The earliest @timer can 23648852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 23658852aac2STejun Heo * on that there's no such delay when @delay is 0. 23668852aac2STejun Heo */ 23678852aac2STejun Heo if (!delay) { 23688852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 23698852aac2STejun Heo return; 23708852aac2STejun Heo } 23718852aac2STejun Heo 237260c057bcSLai Jiangshan dwork->wq = wq; 23731265057fSTejun Heo dwork->cpu = cpu; 23747beb2edfSTejun Heo timer->expires = jiffies + delay; 23757beb2edfSTejun Heo 2376aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2377aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2378aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2379aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2380aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 23817beb2edfSTejun Heo add_timer_on(timer, cpu); 2382aae17ebbSLeonardo Bras } else { 2383aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2384041bd12eSTejun Heo add_timer(timer); 2385aae17ebbSLeonardo Bras else 2386aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2387aae17ebbSLeonardo Bras } 23887beb2edfSTejun Heo } 23891da177e4SLinus Torvalds 23900fcb78c2SRolf Eike Beer /** 23910fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 23920fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 23930fcb78c2SRolf Eike Beer * @wq: workqueue to use 2394af9997e4SRandy Dunlap * @dwork: work to queue 23950fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 23960fcb78c2SRolf Eike Beer * 2397d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2398715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2399715f1300STejun Heo * execution. 24000fcb78c2SRolf Eike Beer */ 2401d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 240252bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 24037a6bc1cdSVenkatesh Pallipadi { 240452bad64dSDavid Howells struct work_struct *work = &dwork->work; 2405d4283e93STejun Heo bool ret = false; 24068930cabaSTejun Heo unsigned long flags; 24078930cabaSTejun Heo 24088930cabaSTejun Heo /* read the comment in __queue_work() */ 24098930cabaSTejun Heo local_irq_save(flags); 24107a6bc1cdSVenkatesh Pallipadi 241122df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24127beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2413d4283e93STejun Heo ret = true; 24147a6bc1cdSVenkatesh Pallipadi } 24158930cabaSTejun Heo 24168930cabaSTejun Heo local_irq_restore(flags); 24177a6bc1cdSVenkatesh Pallipadi return ret; 24187a6bc1cdSVenkatesh Pallipadi } 2419ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 24201da177e4SLinus Torvalds 2421c8e55f36STejun Heo /** 24228376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 24238376fe22STejun Heo * @cpu: CPU number to execute work on 24248376fe22STejun Heo * @wq: workqueue to use 24258376fe22STejun Heo * @dwork: work to queue 24268376fe22STejun Heo * @delay: number of jiffies to wait before queueing 24278376fe22STejun Heo * 24288376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 24298376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 24308376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 24318376fe22STejun Heo * current state. 24328376fe22STejun Heo * 2433d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 24348376fe22STejun Heo * pending and its timer was modified. 24358376fe22STejun Heo * 2436e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 24378376fe22STejun Heo * See try_to_grab_pending() for details. 24388376fe22STejun Heo */ 24398376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 24408376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 24418376fe22STejun Heo { 24428376fe22STejun Heo unsigned long flags; 24438376fe22STejun Heo int ret; 24448376fe22STejun Heo 24458376fe22STejun Heo do { 24468376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 24478376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 24488376fe22STejun Heo 24498376fe22STejun Heo if (likely(ret >= 0)) { 24508376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 24518376fe22STejun Heo local_irq_restore(flags); 24528376fe22STejun Heo } 24538376fe22STejun Heo 24548376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 24558376fe22STejun Heo return ret; 24568376fe22STejun Heo } 24578376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 24588376fe22STejun Heo 245905f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 246005f0fe6bSTejun Heo { 246105f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 246205f0fe6bSTejun Heo 246305f0fe6bSTejun Heo /* read the comment in __queue_work() */ 246405f0fe6bSTejun Heo local_irq_disable(); 246505f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 246605f0fe6bSTejun Heo local_irq_enable(); 246705f0fe6bSTejun Heo } 246805f0fe6bSTejun Heo 246905f0fe6bSTejun Heo /** 247005f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 247105f0fe6bSTejun Heo * @wq: workqueue to use 247205f0fe6bSTejun Heo * @rwork: work to queue 247305f0fe6bSTejun Heo * 247405f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 247505f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2476bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 247705f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 247805f0fe6bSTejun Heo */ 247905f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 248005f0fe6bSTejun Heo { 248105f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 248205f0fe6bSTejun Heo 248305f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 248405f0fe6bSTejun Heo rwork->wq = wq; 2485a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 248605f0fe6bSTejun Heo return true; 248705f0fe6bSTejun Heo } 248805f0fe6bSTejun Heo 248905f0fe6bSTejun Heo return false; 249005f0fe6bSTejun Heo } 249105f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 249205f0fe6bSTejun Heo 2493f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2494c34056a3STejun Heo { 2495c34056a3STejun Heo struct worker *worker; 2496c34056a3STejun Heo 2497f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2498c8e55f36STejun Heo if (worker) { 2499c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2500affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2501da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2502e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2503e22bee78STejun Heo worker->flags = WORKER_PREP; 2504c8e55f36STejun Heo } 2505c34056a3STejun Heo return worker; 2506c34056a3STejun Heo } 2507c34056a3STejun Heo 25089546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 25099546b29eSTejun Heo { 25108639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 25119546b29eSTejun Heo return pool->attrs->__pod_cpumask; 25128639ecebSTejun Heo else 25138639ecebSTejun Heo return pool->attrs->cpumask; 25149546b29eSTejun Heo } 25159546b29eSTejun Heo 2516c34056a3STejun Heo /** 25174736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 25184736cbf7SLai Jiangshan * @worker: worker to be attached 25194736cbf7SLai Jiangshan * @pool: the target pool 25204736cbf7SLai Jiangshan * 25214736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 25224736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 25234736cbf7SLai Jiangshan * cpu-[un]hotplugs. 25244736cbf7SLai Jiangshan */ 25254736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 25264736cbf7SLai Jiangshan struct worker_pool *pool) 25274736cbf7SLai Jiangshan { 25281258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 25294736cbf7SLai Jiangshan 25304736cbf7SLai Jiangshan /* 25311258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 25321258fae7STejun Heo * stable across this function. See the comments above the flag 25331258fae7STejun Heo * definition for details. 25344736cbf7SLai Jiangshan */ 25354736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 25364736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 25375c25b5ffSPeter Zijlstra else 25385c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 25394736cbf7SLai Jiangshan 2540640f17c8SPeter Zijlstra if (worker->rescue_wq) 25419546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2542640f17c8SPeter Zijlstra 25434736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2544a2d812a2STejun Heo worker->pool = pool; 25454736cbf7SLai Jiangshan 25461258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 25474736cbf7SLai Jiangshan } 25484736cbf7SLai Jiangshan 25494736cbf7SLai Jiangshan /** 255060f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 255160f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 255260f5a4bcSLai Jiangshan * 25534736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 25544736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 25554736cbf7SLai Jiangshan * other reference to the pool. 255660f5a4bcSLai Jiangshan */ 2557a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 255860f5a4bcSLai Jiangshan { 2559a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 256060f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 256160f5a4bcSLai Jiangshan 25621258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2563a2d812a2STejun Heo 25645c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2565da028469SLai Jiangshan list_del(&worker->node); 2566a2d812a2STejun Heo worker->pool = NULL; 2567a2d812a2STejun Heo 2568e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 256960f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 25701258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 257160f5a4bcSLai Jiangshan 2572b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2573b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2574b62c0751SLai Jiangshan 257560f5a4bcSLai Jiangshan if (detach_completion) 257660f5a4bcSLai Jiangshan complete(detach_completion); 257760f5a4bcSLai Jiangshan } 257860f5a4bcSLai Jiangshan 257960f5a4bcSLai Jiangshan /** 2580c34056a3STejun Heo * create_worker - create a new workqueue worker 258163d95a91STejun Heo * @pool: pool the new worker will belong to 2582c34056a3STejun Heo * 2583051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2584c34056a3STejun Heo * 2585c34056a3STejun Heo * CONTEXT: 2586c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2587c34056a3STejun Heo * 2588d185af30SYacine Belkadi * Return: 2589c34056a3STejun Heo * Pointer to the newly created worker. 2590c34056a3STejun Heo */ 2591bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2592c34056a3STejun Heo { 2593e441b56fSZhen Lei struct worker *worker; 2594e441b56fSZhen Lei int id; 25955d9c7a1eSLucy Mielke char id_buf[23]; 2596c34056a3STejun Heo 25977cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2598e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 25993f0ea0b8SPetr Mladek if (id < 0) { 26003f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 26013f0ea0b8SPetr Mladek ERR_PTR(id)); 2602e441b56fSZhen Lei return NULL; 26033f0ea0b8SPetr Mladek } 2604c34056a3STejun Heo 2605f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 26063f0ea0b8SPetr Mladek if (!worker) { 26073f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2608c34056a3STejun Heo goto fail; 26093f0ea0b8SPetr Mladek } 2610c34056a3STejun Heo 2611c34056a3STejun Heo worker->id = id; 2612c34056a3STejun Heo 261329c91e99STejun Heo if (pool->cpu >= 0) 2614e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2615e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2616f3421797STejun Heo else 2617e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2618e3c916a4STejun Heo 2619f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2620e3c916a4STejun Heo "kworker/%s", id_buf); 26213f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 262260f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 262360f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 262460f54038SPetr Mladek id_buf); 262560f54038SPetr Mladek } else { 26263f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 26273f0ea0b8SPetr Mladek worker->task); 262860f54038SPetr Mladek } 2629c34056a3STejun Heo goto fail; 26303f0ea0b8SPetr Mladek } 2631c34056a3STejun Heo 263291151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 26339546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 263491151228SOleg Nesterov 2635da028469SLai Jiangshan /* successful, attach the worker to the pool */ 26364736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2637822d8405STejun Heo 2638051e1850SLai Jiangshan /* start the newly created worker */ 2639a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 26400219a352STejun Heo 2641051e1850SLai Jiangshan worker->pool->nr_workers++; 2642051e1850SLai Jiangshan worker_enter_idle(worker); 26430219a352STejun Heo 26440219a352STejun Heo /* 26450219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 26466a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 26476a229b0eSTejun Heo * wake it up explicitly. 26480219a352STejun Heo */ 2649051e1850SLai Jiangshan wake_up_process(worker->task); 26500219a352STejun Heo 2651a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2652051e1850SLai Jiangshan 2653c34056a3STejun Heo return worker; 2654822d8405STejun Heo 2655c34056a3STejun Heo fail: 2656e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2657c34056a3STejun Heo kfree(worker); 2658c34056a3STejun Heo return NULL; 2659c34056a3STejun Heo } 2660c34056a3STejun Heo 2661793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2662793777bcSValentin Schneider { 2663793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2664793777bcSValentin Schneider 2665793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2666793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2667793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2668793777bcSValentin Schneider else 2669793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2670793777bcSValentin Schneider } 2671793777bcSValentin Schneider 2672e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2673e02b9312SValentin Schneider { 2674e02b9312SValentin Schneider struct worker *worker, *tmp; 2675e02b9312SValentin Schneider 2676e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2677e02b9312SValentin Schneider list_del_init(&worker->entry); 2678e02b9312SValentin Schneider unbind_worker(worker); 2679e02b9312SValentin Schneider /* 2680e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2681e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2682e02b9312SValentin Schneider * wouldn't have gotten here. 2683c34056a3STejun Heo * 2684e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2685e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2686e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2687e02b9312SValentin Schneider * outside of pool->lock. 2688e02b9312SValentin Schneider */ 2689e02b9312SValentin Schneider wake_up_process(worker->task); 2690e02b9312SValentin Schneider } 2691e02b9312SValentin Schneider } 2692e02b9312SValentin Schneider 2693e02b9312SValentin Schneider /** 2694e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2695e02b9312SValentin Schneider * @worker: worker to be destroyed 2696e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2697e02b9312SValentin Schneider * 2698e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2699e02b9312SValentin Schneider * should be idle. 2700c8e55f36STejun Heo * 2701c8e55f36STejun Heo * CONTEXT: 2702a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2703c34056a3STejun Heo */ 2704e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2705c34056a3STejun Heo { 2706bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2707c34056a3STejun Heo 2708cd549687STejun Heo lockdep_assert_held(&pool->lock); 2709e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2710cd549687STejun Heo 2711c34056a3STejun Heo /* sanity check frenzy */ 27126183c009STejun Heo if (WARN_ON(worker->current_work) || 271373eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 271473eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 27156183c009STejun Heo return; 2716c34056a3STejun Heo 2717bd7bdd43STejun Heo pool->nr_workers--; 2718bd7bdd43STejun Heo pool->nr_idle--; 2719c8e55f36STejun Heo 2720cb444766STejun Heo worker->flags |= WORKER_DIE; 2721e02b9312SValentin Schneider 2722e02b9312SValentin Schneider list_move(&worker->entry, list); 2723e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2724c34056a3STejun Heo } 2725c34056a3STejun Heo 27263f959aa3SValentin Schneider /** 27273f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 27283f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 27293f959aa3SValentin Schneider * 27303f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 27313f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 27323f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 27333f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 27343f959aa3SValentin Schneider * it expire and re-evaluate things from there. 27353f959aa3SValentin Schneider */ 273632a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2737e22bee78STejun Heo { 273832a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 27393f959aa3SValentin Schneider bool do_cull = false; 27403f959aa3SValentin Schneider 27413f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 27423f959aa3SValentin Schneider return; 27433f959aa3SValentin Schneider 27443f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 27453f959aa3SValentin Schneider 27463f959aa3SValentin Schneider if (too_many_workers(pool)) { 27473f959aa3SValentin Schneider struct worker *worker; 27483f959aa3SValentin Schneider unsigned long expires; 27493f959aa3SValentin Schneider 27503f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 27513f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 27523f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 27533f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 27543f959aa3SValentin Schneider 27553f959aa3SValentin Schneider if (!do_cull) 27563f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 27573f959aa3SValentin Schneider } 27583f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 27593f959aa3SValentin Schneider 27603f959aa3SValentin Schneider if (do_cull) 27613f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 27623f959aa3SValentin Schneider } 27633f959aa3SValentin Schneider 27643f959aa3SValentin Schneider /** 27653f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 27663f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 27673f959aa3SValentin Schneider * 27683f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 27693f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2770e02b9312SValentin Schneider * 2771e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2772e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2773e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 27743f959aa3SValentin Schneider */ 27753f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 27763f959aa3SValentin Schneider { 27773f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 27789680540cSYang Yingliang LIST_HEAD(cull_list); 2779e22bee78STejun Heo 2780e02b9312SValentin Schneider /* 2781e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2782e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2783e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2784e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2785e02b9312SValentin Schneider */ 2786e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2787a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2788e22bee78STejun Heo 27893347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2790e22bee78STejun Heo struct worker *worker; 2791e22bee78STejun Heo unsigned long expires; 2792e22bee78STejun Heo 279363d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2794e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2795e22bee78STejun Heo 27963347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 279763d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 27983347fc9fSLai Jiangshan break; 2799e22bee78STejun Heo } 28003347fc9fSLai Jiangshan 2801e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2802e22bee78STejun Heo } 2803e22bee78STejun Heo 2804a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2805e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2806e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2807e22bee78STejun Heo } 2808e22bee78STejun Heo 2809493a1724STejun Heo static void send_mayday(struct work_struct *work) 2810e22bee78STejun Heo { 2811112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2812112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2813493a1724STejun Heo 28142e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2815e22bee78STejun Heo 2816493008a8STejun Heo if (!wq->rescuer) 2817493a1724STejun Heo return; 2818e22bee78STejun Heo 2819e22bee78STejun Heo /* mayday mayday mayday */ 2820493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 282177668c8bSLai Jiangshan /* 282277668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 282377668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 282477668c8bSLai Jiangshan * rescuer is done with it. 282577668c8bSLai Jiangshan */ 282677668c8bSLai Jiangshan get_pwq(pwq); 2827493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2828e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2829725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2830493a1724STejun Heo } 2831e22bee78STejun Heo } 2832e22bee78STejun Heo 283332a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2834e22bee78STejun Heo { 283532a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2836e22bee78STejun Heo struct work_struct *work; 2837e22bee78STejun Heo 2838a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2839a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2840e22bee78STejun Heo 284163d95a91STejun Heo if (need_to_create_worker(pool)) { 2842e22bee78STejun Heo /* 2843e22bee78STejun Heo * We've been trying to create a new worker but 2844e22bee78STejun Heo * haven't been successful. We might be hitting an 2845e22bee78STejun Heo * allocation deadlock. Send distress signals to 2846e22bee78STejun Heo * rescuers. 2847e22bee78STejun Heo */ 284863d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2849e22bee78STejun Heo send_mayday(work); 2850e22bee78STejun Heo } 2851e22bee78STejun Heo 2852a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2853a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2854e22bee78STejun Heo 285563d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2856e22bee78STejun Heo } 2857e22bee78STejun Heo 2858e22bee78STejun Heo /** 2859e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 286063d95a91STejun Heo * @pool: pool to create a new worker for 2861e22bee78STejun Heo * 286263d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2863e22bee78STejun Heo * have at least one idle worker on return from this function. If 2864e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 286563d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2866e22bee78STejun Heo * possible allocation deadlock. 2867e22bee78STejun Heo * 2868c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2869c5aa87bbSTejun Heo * may_start_working() %true. 2870e22bee78STejun Heo * 2871e22bee78STejun Heo * LOCKING: 2872a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2873e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2874e22bee78STejun Heo * manager. 2875e22bee78STejun Heo */ 287629187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2877d565ed63STejun Heo __releases(&pool->lock) 2878d565ed63STejun Heo __acquires(&pool->lock) 2879e22bee78STejun Heo { 2880e22bee78STejun Heo restart: 2881a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 28829f9c2364STejun Heo 2883e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 288463d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2885e22bee78STejun Heo 2886e22bee78STejun Heo while (true) { 2887051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2888e22bee78STejun Heo break; 2889e22bee78STejun Heo 2890e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 28919f9c2364STejun Heo 289263d95a91STejun Heo if (!need_to_create_worker(pool)) 2893e22bee78STejun Heo break; 2894e22bee78STejun Heo } 2895e22bee78STejun Heo 289663d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2897a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2898051e1850SLai Jiangshan /* 2899051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2900051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2901051e1850SLai Jiangshan * already become busy. 2902051e1850SLai Jiangshan */ 290363d95a91STejun Heo if (need_to_create_worker(pool)) 2904e22bee78STejun Heo goto restart; 2905e22bee78STejun Heo } 2906e22bee78STejun Heo 2907e22bee78STejun Heo /** 2908e22bee78STejun Heo * manage_workers - manage worker pool 2909e22bee78STejun Heo * @worker: self 2910e22bee78STejun Heo * 2911706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2912e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2913706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2914e22bee78STejun Heo * 2915e22bee78STejun Heo * The caller can safely start processing works on false return. On 2916e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2917e22bee78STejun Heo * and may_start_working() is true. 2918e22bee78STejun Heo * 2919e22bee78STejun Heo * CONTEXT: 2920a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2921e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2922e22bee78STejun Heo * 2923d185af30SYacine Belkadi * Return: 292429187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 292529187a9eSTejun Heo * start processing works, %true if management function was performed and 292629187a9eSTejun Heo * the conditions that the caller verified before calling the function may 292729187a9eSTejun Heo * no longer be true. 2928e22bee78STejun Heo */ 2929e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2930e22bee78STejun Heo { 293163d95a91STejun Heo struct worker_pool *pool = worker->pool; 2932e22bee78STejun Heo 2933692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 293429187a9eSTejun Heo return false; 2935692b4825STejun Heo 2936692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 29372607d7a6STejun Heo pool->manager = worker; 2938e22bee78STejun Heo 293929187a9eSTejun Heo maybe_create_worker(pool); 2940e22bee78STejun Heo 29412607d7a6STejun Heo pool->manager = NULL; 2942692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2943d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 294429187a9eSTejun Heo return true; 2945e22bee78STejun Heo } 2946e22bee78STejun Heo 2947a62428c0STejun Heo /** 2948a62428c0STejun Heo * process_one_work - process single work 2949c34056a3STejun Heo * @worker: self 2950a62428c0STejun Heo * @work: work to process 2951a62428c0STejun Heo * 2952a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2953a62428c0STejun Heo * process a single work including synchronization against and 2954a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2955a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2956a62428c0STejun Heo * call this function to process a work. 2957a62428c0STejun Heo * 2958a62428c0STejun Heo * CONTEXT: 2959a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2960a62428c0STejun Heo */ 2961c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2962d565ed63STejun Heo __releases(&pool->lock) 2963d565ed63STejun Heo __acquires(&pool->lock) 29641da177e4SLinus Torvalds { 2965112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2966bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2967c4560c2cSLai Jiangshan unsigned long work_data; 2968c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 29694e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 29704e6045f1SJohannes Berg /* 2971a62428c0STejun Heo * It is permissible to free the struct work_struct from 2972a62428c0STejun Heo * inside the function that is called from it, this we need to 2973a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2974a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2975a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 29764e6045f1SJohannes Berg */ 29774d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 29784d82a1deSPeter Zijlstra 29794d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 29804e6045f1SJohannes Berg #endif 2981807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 298285327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2983ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 298425511a47STejun Heo 29858930cabaSTejun Heo /* claim and dequeue */ 2986dc186ad7SThomas Gleixner debug_work_deactivate(work); 2987c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2988c34056a3STejun Heo worker->current_work = work; 2989a2c1c57bSTejun Heo worker->current_func = work->func; 2990112202d9STejun Heo worker->current_pwq = pwq; 2991616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2992c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2993d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 29947a22ad75STejun Heo 29958bf89593STejun Heo /* 29968bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 29978bf89593STejun Heo * overridden through set_worker_desc(). 29988bf89593STejun Heo */ 29998bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 30008bf89593STejun Heo 3001a62428c0STejun Heo list_del_init(&work->entry); 3002a62428c0STejun Heo 3003649027d7STejun Heo /* 3004228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3005228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3006228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3007228f1d00SLai Jiangshan * execution of the pending work items. 3008fb0e7bebSTejun Heo */ 3009616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3010228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3011fb0e7bebSTejun Heo 3012974271c4STejun Heo /* 30130219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 30140219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 30150219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 30160219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3017974271c4STejun Heo */ 30180219a352STejun Heo kick_pool(pool); 3019974271c4STejun Heo 30208930cabaSTejun Heo /* 30217c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3022d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 302323657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 302423657bb1STejun Heo * disabled. 30258930cabaSTejun Heo */ 30267c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 30271da177e4SLinus Torvalds 3028fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3029a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3030365970a1SDavid Howells 3031c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3032c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 3033a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 30343295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3035e6f3faa7SPeter Zijlstra /* 3036f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3037f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3038e6f3faa7SPeter Zijlstra * 3039e6f3faa7SPeter Zijlstra * However, that would result in: 3040e6f3faa7SPeter Zijlstra * 3041e6f3faa7SPeter Zijlstra * A(W1) 3042e6f3faa7SPeter Zijlstra * WFC(C) 3043e6f3faa7SPeter Zijlstra * A(W1) 3044e6f3faa7SPeter Zijlstra * C(C) 3045e6f3faa7SPeter Zijlstra * 3046e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3047e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3048e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3049f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3050e6f3faa7SPeter Zijlstra * these locks. 3051e6f3faa7SPeter Zijlstra * 3052e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3053e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3054e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3055e6f3faa7SPeter Zijlstra */ 3056f52be570SPeter Zijlstra lockdep_invariant_state(true); 3057e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3058a2c1c57bSTejun Heo worker->current_func(work); 3059e36c886aSArjan van de Ven /* 3060e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3061e36c886aSArjan van de Ven * point will only record its address. 3062e36c886aSArjan van de Ven */ 30631c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3064725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 30653295f0efSIngo Molnar lock_map_release(&lockdep_map); 3066112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 30671da177e4SLinus Torvalds 3068c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3069c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3070c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3071c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3072c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3073c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3074c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3075c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3076c35aea39STejun Heo worker->current_func); 3077d5abe669SPeter Zijlstra debug_show_held_locks(current); 3078d5abe669SPeter Zijlstra dump_stack(); 3079d5abe669SPeter Zijlstra } 3080d5abe669SPeter Zijlstra 3081b22ce278STejun Heo /* 3082025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3083b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3084b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3085b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3086789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3087789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3088b22ce278STejun Heo */ 3089a7e6425eSPaul E. McKenney cond_resched(); 3090b22ce278STejun Heo 3091a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3092a62428c0STejun Heo 3093616db877STejun Heo /* 3094616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3095616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3096616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3097616db877STejun Heo */ 3098fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3099fb0e7bebSTejun Heo 31001b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 31011b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 31021b69ac6bSJohannes Weiner 3103a62428c0STejun Heo /* we're done with it, release */ 310442f8570fSSasha Levin hash_del(&worker->hentry); 3105c34056a3STejun Heo worker->current_work = NULL; 3106a2c1c57bSTejun Heo worker->current_func = NULL; 3107112202d9STejun Heo worker->current_pwq = NULL; 3108d812796eSLai Jiangshan worker->current_color = INT_MAX; 3109dd6c3c54STejun Heo 3110dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3111c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 31121da177e4SLinus Torvalds } 31131da177e4SLinus Torvalds 3114affee4b2STejun Heo /** 3115affee4b2STejun Heo * process_scheduled_works - process scheduled works 3116affee4b2STejun Heo * @worker: self 3117affee4b2STejun Heo * 3118affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3119affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3120affee4b2STejun Heo * fetches a work from the top and executes it. 3121affee4b2STejun Heo * 3122affee4b2STejun Heo * CONTEXT: 3123a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3124affee4b2STejun Heo * multiple times. 3125affee4b2STejun Heo */ 3126affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 31271da177e4SLinus Torvalds { 3128c0ab017dSTejun Heo struct work_struct *work; 3129c0ab017dSTejun Heo bool first = true; 3130c0ab017dSTejun Heo 3131c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3132c0ab017dSTejun Heo struct work_struct, entry))) { 3133c0ab017dSTejun Heo if (first) { 3134c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3135c0ab017dSTejun Heo first = false; 3136c0ab017dSTejun Heo } 3137c34056a3STejun Heo process_one_work(worker, work); 3138a62428c0STejun Heo } 31391da177e4SLinus Torvalds } 31401da177e4SLinus Torvalds 3141197f6accSTejun Heo static void set_pf_worker(bool val) 3142197f6accSTejun Heo { 3143197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3144197f6accSTejun Heo if (val) 3145197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3146197f6accSTejun Heo else 3147197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3148197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3149197f6accSTejun Heo } 3150197f6accSTejun Heo 31514690c4abSTejun Heo /** 31524690c4abSTejun Heo * worker_thread - the worker thread function 3153c34056a3STejun Heo * @__worker: self 31544690c4abSTejun Heo * 3155c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3156c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3157c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3158c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3159c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3160d185af30SYacine Belkadi * 3161d185af30SYacine Belkadi * Return: 0 31624690c4abSTejun Heo */ 3163c34056a3STejun Heo static int worker_thread(void *__worker) 31641da177e4SLinus Torvalds { 3165c34056a3STejun Heo struct worker *worker = __worker; 3166bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 31671da177e4SLinus Torvalds 3168e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3169197f6accSTejun Heo set_pf_worker(true); 3170c8e55f36STejun Heo woke_up: 3171a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3172affee4b2STejun Heo 3173a9ab775bSTejun Heo /* am I supposed to die? */ 3174a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3175a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3176197f6accSTejun Heo set_pf_worker(false); 317760f5a4bcSLai Jiangshan 317860f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3179e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3180a2d812a2STejun Heo worker_detach_from_pool(worker); 3181e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 318260f5a4bcSLai Jiangshan kfree(worker); 3183c8e55f36STejun Heo return 0; 3184c8e55f36STejun Heo } 3185c8e55f36STejun Heo 3186c8e55f36STejun Heo worker_leave_idle(worker); 3187db7bccf4STejun Heo recheck: 3188e22bee78STejun Heo /* no more worker necessary? */ 318963d95a91STejun Heo if (!need_more_worker(pool)) 3190e22bee78STejun Heo goto sleep; 3191e22bee78STejun Heo 3192e22bee78STejun Heo /* do we need to manage? */ 319363d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3194e22bee78STejun Heo goto recheck; 3195e22bee78STejun Heo 3196c8e55f36STejun Heo /* 3197c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3198c8e55f36STejun Heo * preparing to process a work or actually processing it. 3199c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3200c8e55f36STejun Heo */ 32016183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3202c8e55f36STejun Heo 3203e22bee78STejun Heo /* 3204a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3205a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3206a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3207a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3208a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3209e22bee78STejun Heo */ 3210a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3211e22bee78STejun Heo 3212e22bee78STejun Heo do { 3213affee4b2STejun Heo struct work_struct *work = 3214bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3215affee4b2STejun Heo struct work_struct, entry); 3216affee4b2STejun Heo 3217873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3218affee4b2STejun Heo process_scheduled_works(worker); 321963d95a91STejun Heo } while (keep_working(pool)); 3220affee4b2STejun Heo 3221228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3222d313dd85STejun Heo sleep: 3223c8e55f36STejun Heo /* 3224d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3225d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3226d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3227d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3228d565ed63STejun Heo * event. 3229c8e55f36STejun Heo */ 3230c8e55f36STejun Heo worker_enter_idle(worker); 3231c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3232a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 32331da177e4SLinus Torvalds schedule(); 3234c8e55f36STejun Heo goto woke_up; 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds 3237e22bee78STejun Heo /** 3238e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3239111c225aSTejun Heo * @__rescuer: self 3240e22bee78STejun Heo * 3241e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3242493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3243e22bee78STejun Heo * 3244706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3245e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3246e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3247e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3248e22bee78STejun Heo * the problem rescuer solves. 3249e22bee78STejun Heo * 3250706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3251706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3252e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3253e22bee78STejun Heo * 3254e22bee78STejun Heo * This should happen rarely. 3255d185af30SYacine Belkadi * 3256d185af30SYacine Belkadi * Return: 0 3257e22bee78STejun Heo */ 3258111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3259e22bee78STejun Heo { 3260111c225aSTejun Heo struct worker *rescuer = __rescuer; 3261111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 32624d595b86SLai Jiangshan bool should_stop; 3263e22bee78STejun Heo 3264e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3265111c225aSTejun Heo 3266111c225aSTejun Heo /* 3267111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3268111c225aSTejun Heo * doesn't participate in concurrency management. 3269111c225aSTejun Heo */ 3270197f6accSTejun Heo set_pf_worker(true); 3271e22bee78STejun Heo repeat: 3272c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 32731da177e4SLinus Torvalds 32744d595b86SLai Jiangshan /* 32754d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 32764d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 32774d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 32784d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 32794d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 32804d595b86SLai Jiangshan * list is always empty on exit. 32814d595b86SLai Jiangshan */ 32824d595b86SLai Jiangshan should_stop = kthread_should_stop(); 32831da177e4SLinus Torvalds 3284493a1724STejun Heo /* see whether any pwq is asking for help */ 3285a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3286493a1724STejun Heo 3287493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3288493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3289493a1724STejun Heo struct pool_workqueue, mayday_node); 3290112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3291e22bee78STejun Heo struct work_struct *work, *n; 3292e22bee78STejun Heo 3293e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3294493a1724STejun Heo list_del_init(&pwq->mayday_node); 3295493a1724STejun Heo 3296a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3297e22bee78STejun Heo 329851697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 329951697d39SLai Jiangshan 3300a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3301e22bee78STejun Heo 3302e22bee78STejun Heo /* 3303e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3304e22bee78STejun Heo * process'em. 3305e22bee78STejun Heo */ 3306873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 330782607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3308873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3309873eaca6STejun Heo assign_work(work, rescuer, &n)) 3310725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 331182607adcSTejun Heo } 3312e22bee78STejun Heo 3313873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3314e22bee78STejun Heo process_scheduled_works(rescuer); 33157576958aSTejun Heo 33167576958aSTejun Heo /* 3317008847f6SNeilBrown * The above execution of rescued work items could 3318008847f6SNeilBrown * have created more to rescue through 3319f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3320008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3321008847f6SNeilBrown * that such back-to-back work items, which may be 3322008847f6SNeilBrown * being used to relieve memory pressure, don't 3323008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3324008847f6SNeilBrown */ 33254f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3326a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3327e66b39afSTejun Heo /* 3328e66b39afSTejun Heo * Queue iff we aren't racing destruction 3329e66b39afSTejun Heo * and somebody else hasn't queued it already. 3330e66b39afSTejun Heo */ 3331e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3332008847f6SNeilBrown get_pwq(pwq); 3333e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3334e66b39afSTejun Heo } 3335a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3336008847f6SNeilBrown } 3337008847f6SNeilBrown } 3338008847f6SNeilBrown 3339008847f6SNeilBrown /* 334077668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 334113b1d625SLai Jiangshan * go away while we're still attached to it. 334277668c8bSLai Jiangshan */ 334377668c8bSLai Jiangshan put_pwq(pwq); 334477668c8bSLai Jiangshan 334577668c8bSLai Jiangshan /* 33460219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 33470219a352STejun Heo * with 0 concurrency and stalling the execution. 33487576958aSTejun Heo */ 33490219a352STejun Heo kick_pool(pool); 33507576958aSTejun Heo 3351a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 335213b1d625SLai Jiangshan 3353a2d812a2STejun Heo worker_detach_from_pool(rescuer); 335413b1d625SLai Jiangshan 3355a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 33561da177e4SLinus Torvalds } 33571da177e4SLinus Torvalds 3358a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3359493a1724STejun Heo 33604d595b86SLai Jiangshan if (should_stop) { 33614d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3362197f6accSTejun Heo set_pf_worker(false); 33634d595b86SLai Jiangshan return 0; 33644d595b86SLai Jiangshan } 33654d595b86SLai Jiangshan 3366111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3367111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3368e22bee78STejun Heo schedule(); 3369e22bee78STejun Heo goto repeat; 33701da177e4SLinus Torvalds } 33711da177e4SLinus Torvalds 3372fca839c0STejun Heo /** 3373fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3374fca839c0STejun Heo * @target_wq: workqueue being flushed 3375fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3376fca839c0STejun Heo * 3377fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3378fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3379fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3380fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3381fca839c0STejun Heo * a deadlock. 3382fca839c0STejun Heo */ 3383fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3384fca839c0STejun Heo struct work_struct *target_work) 3385fca839c0STejun Heo { 3386fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3387fca839c0STejun Heo struct worker *worker; 3388fca839c0STejun Heo 3389fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3390fca839c0STejun Heo return; 3391fca839c0STejun Heo 3392fca839c0STejun Heo worker = current_wq_worker(); 3393fca839c0STejun Heo 3394fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3395d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3396fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 339723d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 339823d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3399d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3400fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3401fca839c0STejun Heo target_wq->name, target_func); 3402fca839c0STejun Heo } 3403fca839c0STejun Heo 3404fc2e4d70SOleg Nesterov struct wq_barrier { 3405fc2e4d70SOleg Nesterov struct work_struct work; 3406fc2e4d70SOleg Nesterov struct completion done; 34072607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3408fc2e4d70SOleg Nesterov }; 3409fc2e4d70SOleg Nesterov 3410fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3411fc2e4d70SOleg Nesterov { 3412fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3413fc2e4d70SOleg Nesterov complete(&barr->done); 3414fc2e4d70SOleg Nesterov } 3415fc2e4d70SOleg Nesterov 34164690c4abSTejun Heo /** 34174690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3418112202d9STejun Heo * @pwq: pwq to insert barrier into 34194690c4abSTejun Heo * @barr: wq_barrier to insert 3420affee4b2STejun Heo * @target: target work to attach @barr to 3421affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 34224690c4abSTejun Heo * 3423affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3424affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3425affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3426affee4b2STejun Heo * cpu. 3427affee4b2STejun Heo * 3428affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3429affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3430affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3431affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3432affee4b2STejun Heo * after a work with LINKED flag set. 3433affee4b2STejun Heo * 3434affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3435112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 34364690c4abSTejun Heo * 34374690c4abSTejun Heo * CONTEXT: 3438a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 34394690c4abSTejun Heo */ 3440112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3441affee4b2STejun Heo struct wq_barrier *barr, 3442affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3443fc2e4d70SOleg Nesterov { 3444d812796eSLai Jiangshan unsigned int work_flags = 0; 3445d812796eSLai Jiangshan unsigned int work_color; 3446affee4b2STejun Heo struct list_head *head; 3447affee4b2STejun Heo 3448dc186ad7SThomas Gleixner /* 3449d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3450dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3451dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3452dc186ad7SThomas Gleixner * might deadlock. 3453dc186ad7SThomas Gleixner */ 3454ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 345522df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 345652fa5bc5SBoqun Feng 3457fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3458fd1a5b04SByungchul Park 34592607d7a6STejun Heo barr->task = current; 346083c22520SOleg Nesterov 34615797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3462018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3463018f3a13SLai Jiangshan 3464affee4b2STejun Heo /* 3465affee4b2STejun Heo * If @target is currently being executed, schedule the 3466affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3467affee4b2STejun Heo */ 3468d812796eSLai Jiangshan if (worker) { 3469affee4b2STejun Heo head = worker->scheduled.next; 3470d812796eSLai Jiangshan work_color = worker->current_color; 3471d812796eSLai Jiangshan } else { 3472affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3473affee4b2STejun Heo 3474affee4b2STejun Heo head = target->entry.next; 3475affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3476d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3477d812796eSLai Jiangshan work_color = get_work_color(*bits); 3478affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3479affee4b2STejun Heo } 3480affee4b2STejun Heo 3481d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3482d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3483d812796eSLai Jiangshan 3484d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3485fc2e4d70SOleg Nesterov } 3486fc2e4d70SOleg Nesterov 348773f53c4aSTejun Heo /** 3488112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 348973f53c4aSTejun Heo * @wq: workqueue being flushed 349073f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 349173f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 349273f53c4aSTejun Heo * 3493112202d9STejun Heo * Prepare pwqs for workqueue flushing. 349473f53c4aSTejun Heo * 3495112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3496112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3497112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3498112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3499112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 350073f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 350173f53c4aSTejun Heo * 350273f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 350373f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 350473f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 350573f53c4aSTejun Heo * is returned. 350673f53c4aSTejun Heo * 3507112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 350873f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 350973f53c4aSTejun Heo * advanced to @work_color. 351073f53c4aSTejun Heo * 351173f53c4aSTejun Heo * CONTEXT: 35123c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 351373f53c4aSTejun Heo * 3514d185af30SYacine Belkadi * Return: 351573f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 351673f53c4aSTejun Heo * otherwise. 351773f53c4aSTejun Heo */ 3518112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 351973f53c4aSTejun Heo int flush_color, int work_color) 35201da177e4SLinus Torvalds { 352173f53c4aSTejun Heo bool wait = false; 352249e3cf44STejun Heo struct pool_workqueue *pwq; 35231da177e4SLinus Torvalds 352473f53c4aSTejun Heo if (flush_color >= 0) { 35256183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3526112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3527dc186ad7SThomas Gleixner } 352814441960SOleg Nesterov 352949e3cf44STejun Heo for_each_pwq(pwq, wq) { 3530112202d9STejun Heo struct worker_pool *pool = pwq->pool; 35311da177e4SLinus Torvalds 3532a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 353373f53c4aSTejun Heo 353473f53c4aSTejun Heo if (flush_color >= 0) { 35356183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 353673f53c4aSTejun Heo 3537112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3538112202d9STejun Heo pwq->flush_color = flush_color; 3539112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 354073f53c4aSTejun Heo wait = true; 35411da177e4SLinus Torvalds } 354273f53c4aSTejun Heo } 354373f53c4aSTejun Heo 354473f53c4aSTejun Heo if (work_color >= 0) { 35456183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3546112202d9STejun Heo pwq->work_color = work_color; 354773f53c4aSTejun Heo } 354873f53c4aSTejun Heo 3549a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 3552112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 355373f53c4aSTejun Heo complete(&wq->first_flusher->done); 355473f53c4aSTejun Heo 355573f53c4aSTejun Heo return wait; 355683c22520SOleg Nesterov } 35571da177e4SLinus Torvalds 3558c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3559c35aea39STejun Heo { 3560c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3561c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 3562c35aea39STejun Heo } 3563c35aea39STejun Heo 3564c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3565c35aea39STejun Heo struct workqueue_struct *wq) 3566c35aea39STejun Heo { 3567c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3568c35aea39STejun Heo lock_map_release(&work->lockdep_map); 3569c35aea39STejun Heo } 3570c35aea39STejun Heo 35710fcb78c2SRolf Eike Beer /** 3572c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 35730fcb78c2SRolf Eike Beer * @wq: workqueue to flush 35741da177e4SLinus Torvalds * 3575c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3576c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 35771da177e4SLinus Torvalds */ 3578c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 35791da177e4SLinus Torvalds { 358073f53c4aSTejun Heo struct wq_flusher this_flusher = { 358173f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 358273f53c4aSTejun Heo .flush_color = -1, 3583fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 358473f53c4aSTejun Heo }; 358573f53c4aSTejun Heo int next_color; 3586b1f4ec17SOleg Nesterov 35873347fa09STejun Heo if (WARN_ON(!wq_online)) 35883347fa09STejun Heo return; 35893347fa09STejun Heo 3590c35aea39STejun Heo touch_wq_lockdep_map(wq); 359187915adcSJohannes Berg 35923c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 359373f53c4aSTejun Heo 359473f53c4aSTejun Heo /* 359573f53c4aSTejun Heo * Start-to-wait phase 359673f53c4aSTejun Heo */ 359773f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 359873f53c4aSTejun Heo 359973f53c4aSTejun Heo if (next_color != wq->flush_color) { 360073f53c4aSTejun Heo /* 360173f53c4aSTejun Heo * Color space is not full. The current work_color 360273f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 360373f53c4aSTejun Heo * by one. 360473f53c4aSTejun Heo */ 36056183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 360673f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 360773f53c4aSTejun Heo wq->work_color = next_color; 360873f53c4aSTejun Heo 360973f53c4aSTejun Heo if (!wq->first_flusher) { 361073f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 36116183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 361273f53c4aSTejun Heo 361373f53c4aSTejun Heo wq->first_flusher = &this_flusher; 361473f53c4aSTejun Heo 3615112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 361673f53c4aSTejun Heo wq->work_color)) { 361773f53c4aSTejun Heo /* nothing to flush, done */ 361873f53c4aSTejun Heo wq->flush_color = next_color; 361973f53c4aSTejun Heo wq->first_flusher = NULL; 362073f53c4aSTejun Heo goto out_unlock; 362173f53c4aSTejun Heo } 362273f53c4aSTejun Heo } else { 362373f53c4aSTejun Heo /* wait in queue */ 36246183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 362573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3626112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 362773f53c4aSTejun Heo } 362873f53c4aSTejun Heo } else { 362973f53c4aSTejun Heo /* 363073f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 363173f53c4aSTejun Heo * The next flush completion will assign us 363273f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 363373f53c4aSTejun Heo */ 363473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 363573f53c4aSTejun Heo } 363673f53c4aSTejun Heo 3637fca839c0STejun Heo check_flush_dependency(wq, NULL); 3638fca839c0STejun Heo 36393c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 364073f53c4aSTejun Heo 364173f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 364273f53c4aSTejun Heo 364373f53c4aSTejun Heo /* 364473f53c4aSTejun Heo * Wake-up-and-cascade phase 364573f53c4aSTejun Heo * 364673f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 364773f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 364873f53c4aSTejun Heo */ 364900d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 365073f53c4aSTejun Heo return; 365173f53c4aSTejun Heo 36523c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 365373f53c4aSTejun Heo 36544ce48b37STejun Heo /* we might have raced, check again with mutex held */ 36554ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 36564ce48b37STejun Heo goto out_unlock; 36574ce48b37STejun Heo 365800d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 365973f53c4aSTejun Heo 36606183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 36616183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 366273f53c4aSTejun Heo 366373f53c4aSTejun Heo while (true) { 366473f53c4aSTejun Heo struct wq_flusher *next, *tmp; 366573f53c4aSTejun Heo 366673f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 366773f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 366873f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 366973f53c4aSTejun Heo break; 367073f53c4aSTejun Heo list_del_init(&next->list); 367173f53c4aSTejun Heo complete(&next->done); 367273f53c4aSTejun Heo } 367373f53c4aSTejun Heo 36746183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 367573f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 367673f53c4aSTejun Heo 367773f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 367873f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 367973f53c4aSTejun Heo 368073f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 368173f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 368273f53c4aSTejun Heo /* 368373f53c4aSTejun Heo * Assign the same color to all overflowed 368473f53c4aSTejun Heo * flushers, advance work_color and append to 368573f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 368673f53c4aSTejun Heo * phase for these overflowed flushers. 368773f53c4aSTejun Heo */ 368873f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 368973f53c4aSTejun Heo tmp->flush_color = wq->work_color; 369073f53c4aSTejun Heo 369173f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 369273f53c4aSTejun Heo 369373f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 369473f53c4aSTejun Heo &wq->flusher_queue); 3695112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 369673f53c4aSTejun Heo } 369773f53c4aSTejun Heo 369873f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 36996183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 370073f53c4aSTejun Heo break; 370173f53c4aSTejun Heo } 370273f53c4aSTejun Heo 370373f53c4aSTejun Heo /* 370473f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3705112202d9STejun Heo * the new first flusher and arm pwqs. 370673f53c4aSTejun Heo */ 37076183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 37086183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 370973f53c4aSTejun Heo 371073f53c4aSTejun Heo list_del_init(&next->list); 371173f53c4aSTejun Heo wq->first_flusher = next; 371273f53c4aSTejun Heo 3713112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 371473f53c4aSTejun Heo break; 371573f53c4aSTejun Heo 371673f53c4aSTejun Heo /* 371773f53c4aSTejun Heo * Meh... this color is already done, clear first 371873f53c4aSTejun Heo * flusher and repeat cascading. 371973f53c4aSTejun Heo */ 372073f53c4aSTejun Heo wq->first_flusher = NULL; 372173f53c4aSTejun Heo } 372273f53c4aSTejun Heo 372373f53c4aSTejun Heo out_unlock: 37243c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 37251da177e4SLinus Torvalds } 3726c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 37271da177e4SLinus Torvalds 37289c5a2ba7STejun Heo /** 37299c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 37309c5a2ba7STejun Heo * @wq: workqueue to drain 37319c5a2ba7STejun Heo * 37329c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 37339c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 37349c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3735b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 37369c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 37379c5a2ba7STejun Heo * takes too long. 37389c5a2ba7STejun Heo */ 37399c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 37409c5a2ba7STejun Heo { 37419c5a2ba7STejun Heo unsigned int flush_cnt = 0; 374249e3cf44STejun Heo struct pool_workqueue *pwq; 37439c5a2ba7STejun Heo 37449c5a2ba7STejun Heo /* 37459c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 37469c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3747618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 37489c5a2ba7STejun Heo */ 374987fc741eSLai Jiangshan mutex_lock(&wq->mutex); 37509c5a2ba7STejun Heo if (!wq->nr_drainers++) 3751618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 375287fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 37539c5a2ba7STejun Heo reflush: 3754c4f135d6STetsuo Handa __flush_workqueue(wq); 37559c5a2ba7STejun Heo 3756b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 375776af4d93STejun Heo 375849e3cf44STejun Heo for_each_pwq(pwq, wq) { 3759fa2563e4SThomas Tuttle bool drained; 37609c5a2ba7STejun Heo 3761a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3762afa87ce8STejun Heo drained = pwq_is_empty(pwq); 3763a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3764fa2563e4SThomas Tuttle 3765fa2563e4SThomas Tuttle if (drained) 37669c5a2ba7STejun Heo continue; 37679c5a2ba7STejun Heo 37689c5a2ba7STejun Heo if (++flush_cnt == 10 || 37699c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3770e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3771e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 377276af4d93STejun Heo 3773b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 37749c5a2ba7STejun Heo goto reflush; 37759c5a2ba7STejun Heo } 37769c5a2ba7STejun Heo 37779c5a2ba7STejun Heo if (!--wq->nr_drainers) 3778618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 377987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 37809c5a2ba7STejun Heo } 37819c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 37829c5a2ba7STejun Heo 3783d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3784d6e89786SJohannes Berg bool from_cancel) 3785baf59022STejun Heo { 3786baf59022STejun Heo struct worker *worker = NULL; 3787c9e7cf27STejun Heo struct worker_pool *pool; 3788112202d9STejun Heo struct pool_workqueue *pwq; 3789c35aea39STejun Heo struct workqueue_struct *wq; 3790baf59022STejun Heo 3791baf59022STejun Heo might_sleep(); 3792baf59022STejun Heo 379324acfb71SThomas Gleixner rcu_read_lock(); 3794fa1b54e6STejun Heo pool = get_work_pool(work); 3795fa1b54e6STejun Heo if (!pool) { 379624acfb71SThomas Gleixner rcu_read_unlock(); 3797fa1b54e6STejun Heo return false; 3798fa1b54e6STejun Heo } 3799fa1b54e6STejun Heo 3800a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 38010b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3802112202d9STejun Heo pwq = get_work_pwq(work); 3803112202d9STejun Heo if (pwq) { 3804112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3805baf59022STejun Heo goto already_gone; 3806606a5020STejun Heo } else { 3807c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3808baf59022STejun Heo if (!worker) 3809baf59022STejun Heo goto already_gone; 3810112202d9STejun Heo pwq = worker->current_pwq; 3811606a5020STejun Heo } 3812baf59022STejun Heo 3813c35aea39STejun Heo wq = pwq->wq; 3814c35aea39STejun Heo check_flush_dependency(wq, work); 3815fca839c0STejun Heo 3816112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3817a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3818baf59022STejun Heo 3819c35aea39STejun Heo touch_work_lockdep_map(work, wq); 3820c35aea39STejun Heo 3821e159489bSTejun Heo /* 3822a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3823a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3824a1d14934SPeter Zijlstra * 3825a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3826a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3827a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3828a1d14934SPeter Zijlstra * forward progress. 3829e159489bSTejun Heo */ 3830c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 3831c35aea39STejun Heo touch_wq_lockdep_map(wq); 3832c35aea39STejun Heo 383324acfb71SThomas Gleixner rcu_read_unlock(); 3834baf59022STejun Heo return true; 3835baf59022STejun Heo already_gone: 3836a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 383724acfb71SThomas Gleixner rcu_read_unlock(); 3838baf59022STejun Heo return false; 3839baf59022STejun Heo } 3840baf59022STejun Heo 3841d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3842d6e89786SJohannes Berg { 3843d6e89786SJohannes Berg struct wq_barrier barr; 3844d6e89786SJohannes Berg 3845d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3846d6e89786SJohannes Berg return false; 3847d6e89786SJohannes Berg 38484d43d395STetsuo Handa if (WARN_ON(!work->func)) 38494d43d395STetsuo Handa return false; 38504d43d395STetsuo Handa 3851d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3852d6e89786SJohannes Berg wait_for_completion(&barr.done); 3853d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3854d6e89786SJohannes Berg return true; 3855d6e89786SJohannes Berg } else { 3856d6e89786SJohannes Berg return false; 3857d6e89786SJohannes Berg } 3858d6e89786SJohannes Berg } 3859d6e89786SJohannes Berg 3860db700897SOleg Nesterov /** 3861401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3862401a8d04STejun Heo * @work: the work to flush 3863db700897SOleg Nesterov * 3864606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3865606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3866401a8d04STejun Heo * 3867d185af30SYacine Belkadi * Return: 3868401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3869401a8d04STejun Heo * %false if it was already idle. 3870db700897SOleg Nesterov */ 3871401a8d04STejun Heo bool flush_work(struct work_struct *work) 3872db700897SOleg Nesterov { 3873d6e89786SJohannes Berg return __flush_work(work, false); 3874606a5020STejun Heo } 3875db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3876db700897SOleg Nesterov 38778603e1b3STejun Heo struct cwt_wait { 3878ac6424b9SIngo Molnar wait_queue_entry_t wait; 38798603e1b3STejun Heo struct work_struct *work; 38808603e1b3STejun Heo }; 38818603e1b3STejun Heo 3882ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 38838603e1b3STejun Heo { 38848603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 38858603e1b3STejun Heo 38868603e1b3STejun Heo if (cwait->work != key) 38878603e1b3STejun Heo return 0; 38888603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 38898603e1b3STejun Heo } 38908603e1b3STejun Heo 389136e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3892401a8d04STejun Heo { 38938603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3894bbb68dfaSTejun Heo unsigned long flags; 38951f1f642eSOleg Nesterov int ret; 38961f1f642eSOleg Nesterov 38971f1f642eSOleg Nesterov do { 3898bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3899bbb68dfaSTejun Heo /* 39008603e1b3STejun Heo * If someone else is already canceling, wait for it to 39018603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 39028603e1b3STejun Heo * because we may get scheduled between @work's completion 39038603e1b3STejun Heo * and the other canceling task resuming and clearing 39048603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 39058603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 39068603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 39078603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 39088603e1b3STejun Heo * we're hogging the CPU. 39098603e1b3STejun Heo * 39108603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 39118603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 39128603e1b3STejun Heo * wake function which matches @work along with exclusive 39138603e1b3STejun Heo * wait and wakeup. 3914bbb68dfaSTejun Heo */ 39158603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 39168603e1b3STejun Heo struct cwt_wait cwait; 39178603e1b3STejun Heo 39188603e1b3STejun Heo init_wait(&cwait.wait); 39198603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 39208603e1b3STejun Heo cwait.work = work; 39218603e1b3STejun Heo 39228603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 39238603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 39248603e1b3STejun Heo if (work_is_canceling(work)) 39258603e1b3STejun Heo schedule(); 39268603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 39278603e1b3STejun Heo } 39281f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 39291f1f642eSOleg Nesterov 3930bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3931bbb68dfaSTejun Heo mark_work_canceling(work); 3932bbb68dfaSTejun Heo local_irq_restore(flags); 3933bbb68dfaSTejun Heo 39343347fa09STejun Heo /* 39353347fa09STejun Heo * This allows canceling during early boot. We know that @work 39363347fa09STejun Heo * isn't executing. 39373347fa09STejun Heo */ 39383347fa09STejun Heo if (wq_online) 3939d6e89786SJohannes Berg __flush_work(work, true); 39403347fa09STejun Heo 39417a22ad75STejun Heo clear_work_data(work); 39428603e1b3STejun Heo 39438603e1b3STejun Heo /* 39448603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 39458603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 39468603e1b3STejun Heo * visible there. 39478603e1b3STejun Heo */ 39488603e1b3STejun Heo smp_mb(); 39498603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 39508603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 39518603e1b3STejun Heo 39521f1f642eSOleg Nesterov return ret; 39531f1f642eSOleg Nesterov } 39541f1f642eSOleg Nesterov 39556e84d644SOleg Nesterov /** 3956401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3957401a8d04STejun Heo * @work: the work to cancel 39586e84d644SOleg Nesterov * 3959401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3960401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3961401a8d04STejun Heo * another workqueue. On return from this function, @work is 3962401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 39631f1f642eSOleg Nesterov * 3964401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3965401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 39666e84d644SOleg Nesterov * 3967401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 39686e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3969401a8d04STejun Heo * 3970d185af30SYacine Belkadi * Return: 3971401a8d04STejun Heo * %true if @work was pending, %false otherwise. 39726e84d644SOleg Nesterov */ 3973401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 39746e84d644SOleg Nesterov { 397536e227d2STejun Heo return __cancel_work_timer(work, false); 3976b89deed3SOleg Nesterov } 397728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3978b89deed3SOleg Nesterov 39796e84d644SOleg Nesterov /** 3980401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3981401a8d04STejun Heo * @dwork: the delayed work to flush 39826e84d644SOleg Nesterov * 3983401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3984401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3985401a8d04STejun Heo * considers the last queueing instance of @dwork. 39861f1f642eSOleg Nesterov * 3987d185af30SYacine Belkadi * Return: 3988401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3989401a8d04STejun Heo * %false if it was already idle. 39906e84d644SOleg Nesterov */ 3991401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3992401a8d04STejun Heo { 39938930cabaSTejun Heo local_irq_disable(); 3994401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 399560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 39968930cabaSTejun Heo local_irq_enable(); 3997401a8d04STejun Heo return flush_work(&dwork->work); 3998401a8d04STejun Heo } 3999401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4000401a8d04STejun Heo 400105f0fe6bSTejun Heo /** 400205f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 400305f0fe6bSTejun Heo * @rwork: the rcu work to flush 400405f0fe6bSTejun Heo * 400505f0fe6bSTejun Heo * Return: 400605f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 400705f0fe6bSTejun Heo * %false if it was already idle. 400805f0fe6bSTejun Heo */ 400905f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 401005f0fe6bSTejun Heo { 401105f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 401205f0fe6bSTejun Heo rcu_barrier(); 401305f0fe6bSTejun Heo flush_work(&rwork->work); 401405f0fe6bSTejun Heo return true; 401505f0fe6bSTejun Heo } else { 401605f0fe6bSTejun Heo return flush_work(&rwork->work); 401705f0fe6bSTejun Heo } 401805f0fe6bSTejun Heo } 401905f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 402005f0fe6bSTejun Heo 4021f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 4022f72b8792SJens Axboe { 4023f72b8792SJens Axboe unsigned long flags; 4024f72b8792SJens Axboe int ret; 4025f72b8792SJens Axboe 4026f72b8792SJens Axboe do { 4027f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 4028f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 4029f72b8792SJens Axboe 4030f72b8792SJens Axboe if (unlikely(ret < 0)) 4031f72b8792SJens Axboe return false; 4032f72b8792SJens Axboe 4033f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 4034f72b8792SJens Axboe local_irq_restore(flags); 4035f72b8792SJens Axboe return ret; 4036f72b8792SJens Axboe } 4037f72b8792SJens Axboe 403873b4b532SAndrey Grodzovsky /* 403973b4b532SAndrey Grodzovsky * See cancel_delayed_work() 404073b4b532SAndrey Grodzovsky */ 404173b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 404273b4b532SAndrey Grodzovsky { 404373b4b532SAndrey Grodzovsky return __cancel_work(work, false); 404473b4b532SAndrey Grodzovsky } 404573b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 404673b4b532SAndrey Grodzovsky 4047401a8d04STejun Heo /** 404857b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 404957b30ae7STejun Heo * @dwork: delayed_work to cancel 405009383498STejun Heo * 4051d185af30SYacine Belkadi * Kill off a pending delayed_work. 4052d185af30SYacine Belkadi * 4053d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4054d185af30SYacine Belkadi * pending. 4055d185af30SYacine Belkadi * 4056d185af30SYacine Belkadi * Note: 4057d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4058d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4059d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 406009383498STejun Heo * 406157b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 406209383498STejun Heo */ 406357b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 406409383498STejun Heo { 4065f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 406609383498STejun Heo } 406757b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 406809383498STejun Heo 406909383498STejun Heo /** 4070401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4071401a8d04STejun Heo * @dwork: the delayed work cancel 4072401a8d04STejun Heo * 4073401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4074401a8d04STejun Heo * 4075d185af30SYacine Belkadi * Return: 4076401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4077401a8d04STejun Heo */ 4078401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 40796e84d644SOleg Nesterov { 408036e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 40816e84d644SOleg Nesterov } 4082f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 40831da177e4SLinus Torvalds 40840fcb78c2SRolf Eike Beer /** 408531ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4086b6136773SAndrew Morton * @func: the function to call 4087b6136773SAndrew Morton * 408831ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 408931ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4090b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 409131ddd871STejun Heo * 4092d185af30SYacine Belkadi * Return: 409331ddd871STejun Heo * 0 on success, -errno on failure. 4094b6136773SAndrew Morton */ 409565f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 409615316ba8SChristoph Lameter { 409715316ba8SChristoph Lameter int cpu; 409838f51568SNamhyung Kim struct work_struct __percpu *works; 409915316ba8SChristoph Lameter 4100b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4101b6136773SAndrew Morton if (!works) 410215316ba8SChristoph Lameter return -ENOMEM; 4103b6136773SAndrew Morton 4104ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 410593981800STejun Heo 410615316ba8SChristoph Lameter for_each_online_cpu(cpu) { 41079bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 41089bfb1839SIngo Molnar 41099bfb1839SIngo Molnar INIT_WORK(work, func); 41108de6d308SOleg Nesterov schedule_work_on(cpu, work); 411115316ba8SChristoph Lameter } 411293981800STejun Heo 411393981800STejun Heo for_each_online_cpu(cpu) 41148616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 411593981800STejun Heo 4116ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4117b6136773SAndrew Morton free_percpu(works); 411815316ba8SChristoph Lameter return 0; 411915316ba8SChristoph Lameter } 412015316ba8SChristoph Lameter 4121eef6a7d5SAlan Stern /** 41221fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 41231fa44ecaSJames Bottomley * @fn: the function to execute 41241fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 41251fa44ecaSJames Bottomley * be available when the work executes) 41261fa44ecaSJames Bottomley * 41271fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 41281fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 41291fa44ecaSJames Bottomley * 4130d185af30SYacine Belkadi * Return: 0 - function was executed 41311fa44ecaSJames Bottomley * 1 - function was scheduled for execution 41321fa44ecaSJames Bottomley */ 413365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 41341fa44ecaSJames Bottomley { 41351fa44ecaSJames Bottomley if (!in_interrupt()) { 413665f27f38SDavid Howells fn(&ew->work); 41371fa44ecaSJames Bottomley return 0; 41381fa44ecaSJames Bottomley } 41391fa44ecaSJames Bottomley 414065f27f38SDavid Howells INIT_WORK(&ew->work, fn); 41411fa44ecaSJames Bottomley schedule_work(&ew->work); 41421fa44ecaSJames Bottomley 41431fa44ecaSJames Bottomley return 1; 41441fa44ecaSJames Bottomley } 41451fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 41461fa44ecaSJames Bottomley 41477a4e344cSTejun Heo /** 41487a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 41497a4e344cSTejun Heo * @attrs: workqueue_attrs to free 41507a4e344cSTejun Heo * 41517a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 41527a4e344cSTejun Heo */ 4153513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 41547a4e344cSTejun Heo { 41557a4e344cSTejun Heo if (attrs) { 41567a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 41579546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 41587a4e344cSTejun Heo kfree(attrs); 41597a4e344cSTejun Heo } 41607a4e344cSTejun Heo } 41617a4e344cSTejun Heo 41627a4e344cSTejun Heo /** 41637a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 41647a4e344cSTejun Heo * 41657a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4166d185af30SYacine Belkadi * return it. 4167d185af30SYacine Belkadi * 4168d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 41697a4e344cSTejun Heo */ 4170513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 41717a4e344cSTejun Heo { 41727a4e344cSTejun Heo struct workqueue_attrs *attrs; 41737a4e344cSTejun Heo 4174be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 41757a4e344cSTejun Heo if (!attrs) 41767a4e344cSTejun Heo goto fail; 4177be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 41787a4e344cSTejun Heo goto fail; 41799546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 41809546b29eSTejun Heo goto fail; 41817a4e344cSTejun Heo 418213e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4183523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 41847a4e344cSTejun Heo return attrs; 41857a4e344cSTejun Heo fail: 41867a4e344cSTejun Heo free_workqueue_attrs(attrs); 41877a4e344cSTejun Heo return NULL; 41887a4e344cSTejun Heo } 41897a4e344cSTejun Heo 419029c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 419129c91e99STejun Heo const struct workqueue_attrs *from) 419229c91e99STejun Heo { 419329c91e99STejun Heo to->nice = from->nice; 419429c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 41959546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 41968639ecebSTejun Heo to->affn_strict = from->affn_strict; 419784193c07STejun Heo 41982865a8fbSShaohua Li /* 419984193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 420084193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 420184193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 42022865a8fbSShaohua Li */ 420384193c07STejun Heo to->affn_scope = from->affn_scope; 4204af73f5c9STejun Heo to->ordered = from->ordered; 420529c91e99STejun Heo } 420629c91e99STejun Heo 42075de7a03cSTejun Heo /* 42085de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 42095de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 42105de7a03cSTejun Heo */ 42115de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 42125de7a03cSTejun Heo { 421384193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 42145de7a03cSTejun Heo attrs->ordered = false; 42155de7a03cSTejun Heo } 42165de7a03cSTejun Heo 421729c91e99STejun Heo /* hash value of the content of @attr */ 421829c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 421929c91e99STejun Heo { 422029c91e99STejun Heo u32 hash = 0; 422129c91e99STejun Heo 422229c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 422313e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 422413e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 42259546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 42269546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 42278639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 422829c91e99STejun Heo return hash; 422929c91e99STejun Heo } 423029c91e99STejun Heo 423129c91e99STejun Heo /* content equality test */ 423229c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 423329c91e99STejun Heo const struct workqueue_attrs *b) 423429c91e99STejun Heo { 423529c91e99STejun Heo if (a->nice != b->nice) 423629c91e99STejun Heo return false; 423729c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 423829c91e99STejun Heo return false; 42399546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 42409546b29eSTejun Heo return false; 42418639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 42428639ecebSTejun Heo return false; 424329c91e99STejun Heo return true; 424429c91e99STejun Heo } 424529c91e99STejun Heo 42460f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 42470f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 42480f36ee24STejun Heo const cpumask_t *unbound_cpumask) 42490f36ee24STejun Heo { 42500f36ee24STejun Heo /* 42510f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 42520f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 42530f36ee24STejun Heo * @unbound_cpumask. 42540f36ee24STejun Heo */ 42550f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 42560f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 42570f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 42580f36ee24STejun Heo } 42590f36ee24STejun Heo 426084193c07STejun Heo /* find wq_pod_type to use for @attrs */ 426184193c07STejun Heo static const struct wq_pod_type * 426284193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 426384193c07STejun Heo { 4264523a301eSTejun Heo enum wq_affn_scope scope; 4265523a301eSTejun Heo struct wq_pod_type *pt; 4266523a301eSTejun Heo 4267523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4268523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4269523a301eSTejun Heo 4270523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4271523a301eSTejun Heo scope = wq_affn_dfl; 4272523a301eSTejun Heo else 4273523a301eSTejun Heo scope = attrs->affn_scope; 4274523a301eSTejun Heo 4275523a301eSTejun Heo pt = &wq_pod_types[scope]; 427684193c07STejun Heo 427784193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 427884193c07STejun Heo likely(pt->nr_pods)) 427984193c07STejun Heo return pt; 428084193c07STejun Heo 428184193c07STejun Heo /* 428284193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 428384193c07STejun Heo * initialized in workqueue_init_early(). 428484193c07STejun Heo */ 428584193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 428684193c07STejun Heo BUG_ON(!pt->nr_pods); 428784193c07STejun Heo return pt; 428884193c07STejun Heo } 428984193c07STejun Heo 42907a4e344cSTejun Heo /** 42917a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 42927a4e344cSTejun Heo * @pool: worker_pool to initialize 42937a4e344cSTejun Heo * 4294402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4295d185af30SYacine Belkadi * 4296d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 429729c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 429829c91e99STejun Heo * on @pool safely to release it. 42997a4e344cSTejun Heo */ 43007a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 43014e1a1f9aSTejun Heo { 4302a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 430329c91e99STejun Heo pool->id = -1; 430429c91e99STejun Heo pool->cpu = -1; 4305f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 43064e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 430782607adcSTejun Heo pool->watchdog_ts = jiffies; 43084e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 43094e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 43104e1a1f9aSTejun Heo hash_init(pool->busy_hash); 43114e1a1f9aSTejun Heo 431232a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 43133f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 43144e1a1f9aSTejun Heo 431532a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 43164e1a1f9aSTejun Heo 4317da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4318e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 43197a4e344cSTejun Heo 43207cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 432129c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 432229c91e99STejun Heo pool->refcnt = 1; 432329c91e99STejun Heo 432429c91e99STejun Heo /* shouldn't fail above this point */ 4325be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 43267a4e344cSTejun Heo if (!pool->attrs) 43277a4e344cSTejun Heo return -ENOMEM; 43285de7a03cSTejun Heo 43295de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 43305de7a03cSTejun Heo 43317a4e344cSTejun Heo return 0; 43324e1a1f9aSTejun Heo } 43334e1a1f9aSTejun Heo 4334669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4335669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4336669de8bdSBart Van Assche { 4337669de8bdSBart Van Assche char *lock_name; 4338669de8bdSBart Van Assche 4339669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4340669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4341669de8bdSBart Van Assche if (!lock_name) 4342669de8bdSBart Van Assche lock_name = wq->name; 434369a106c0SQian Cai 434469a106c0SQian Cai wq->lock_name = lock_name; 4345669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4346669de8bdSBart Van Assche } 4347669de8bdSBart Van Assche 4348669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4349669de8bdSBart Van Assche { 4350669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4351669de8bdSBart Van Assche } 4352669de8bdSBart Van Assche 4353669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4354669de8bdSBart Van Assche { 4355669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4356669de8bdSBart Van Assche kfree(wq->lock_name); 4357669de8bdSBart Van Assche } 4358669de8bdSBart Van Assche #else 4359669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4360669de8bdSBart Van Assche { 4361669de8bdSBart Van Assche } 4362669de8bdSBart Van Assche 4363669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4364669de8bdSBart Van Assche { 4365669de8bdSBart Van Assche } 4366669de8bdSBart Van Assche 4367669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4368669de8bdSBart Van Assche { 4369669de8bdSBart Van Assche } 4370669de8bdSBart Van Assche #endif 4371669de8bdSBart Van Assche 437291ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 437391ccc6e7STejun Heo { 437491ccc6e7STejun Heo int node; 437591ccc6e7STejun Heo 437691ccc6e7STejun Heo for_each_node(node) { 437791ccc6e7STejun Heo kfree(nna_ar[node]); 437891ccc6e7STejun Heo nna_ar[node] = NULL; 437991ccc6e7STejun Heo } 438091ccc6e7STejun Heo 438191ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 438291ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 438391ccc6e7STejun Heo } 438491ccc6e7STejun Heo 438591ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 438691ccc6e7STejun Heo { 4387c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 438891ccc6e7STejun Heo atomic_set(&nna->nr, 0); 43895797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 43905797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 439191ccc6e7STejun Heo } 439291ccc6e7STejun Heo 439391ccc6e7STejun Heo /* 439491ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 439591ccc6e7STejun Heo * should be allocated in the node. 439691ccc6e7STejun Heo */ 439791ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 439891ccc6e7STejun Heo { 439991ccc6e7STejun Heo struct wq_node_nr_active *nna; 440091ccc6e7STejun Heo int node; 440191ccc6e7STejun Heo 440291ccc6e7STejun Heo for_each_node(node) { 440391ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 440491ccc6e7STejun Heo if (!nna) 440591ccc6e7STejun Heo goto err_free; 440691ccc6e7STejun Heo init_node_nr_active(nna); 440791ccc6e7STejun Heo nna_ar[node] = nna; 440891ccc6e7STejun Heo } 440991ccc6e7STejun Heo 441091ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 441191ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 441291ccc6e7STejun Heo if (!nna) 441391ccc6e7STejun Heo goto err_free; 441491ccc6e7STejun Heo init_node_nr_active(nna); 441591ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 441691ccc6e7STejun Heo 441791ccc6e7STejun Heo return 0; 441891ccc6e7STejun Heo 441991ccc6e7STejun Heo err_free: 442091ccc6e7STejun Heo free_node_nr_active(nna_ar); 442191ccc6e7STejun Heo return -ENOMEM; 442291ccc6e7STejun Heo } 442391ccc6e7STejun Heo 4424e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4425e2dca7adSTejun Heo { 4426e2dca7adSTejun Heo struct workqueue_struct *wq = 4427e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4428e2dca7adSTejun Heo 442991ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 443091ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 443191ccc6e7STejun Heo 4432669de8bdSBart Van Assche wq_free_lockdep(wq); 4433ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4434e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4435e2dca7adSTejun Heo kfree(wq); 4436e2dca7adSTejun Heo } 4437e2dca7adSTejun Heo 443829c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 443929c91e99STejun Heo { 444029c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 444129c91e99STejun Heo 44427cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 444329c91e99STejun Heo free_workqueue_attrs(pool->attrs); 444429c91e99STejun Heo kfree(pool); 444529c91e99STejun Heo } 444629c91e99STejun Heo 444729c91e99STejun Heo /** 444829c91e99STejun Heo * put_unbound_pool - put a worker_pool 444929c91e99STejun Heo * @pool: worker_pool to put 445029c91e99STejun Heo * 445124acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4452c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4453c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4454c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4455a892caccSTejun Heo * 4456a892caccSTejun Heo * Should be called with wq_pool_mutex held. 445729c91e99STejun Heo */ 445829c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 445929c91e99STejun Heo { 446060f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 446129c91e99STejun Heo struct worker *worker; 44629680540cSYang Yingliang LIST_HEAD(cull_list); 4463e02b9312SValentin Schneider 4464a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4465a892caccSTejun Heo 4466a892caccSTejun Heo if (--pool->refcnt) 446729c91e99STejun Heo return; 446829c91e99STejun Heo 446929c91e99STejun Heo /* sanity checks */ 447061d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4471a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 447229c91e99STejun Heo return; 447329c91e99STejun Heo 447429c91e99STejun Heo /* release id and unhash */ 447529c91e99STejun Heo if (pool->id >= 0) 447629c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 447729c91e99STejun Heo hash_del(&pool->hash_node); 447829c91e99STejun Heo 4479c5aa87bbSTejun Heo /* 4480692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4481692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4482692b4825STejun Heo * manager and @pool gets freed with the flag set. 44839ab03be4SValentin Schneider * 44849ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 44859ab03be4SValentin Schneider * only get here with 44869ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 44879ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 44889ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 44899ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 44909ab03be4SValentin Schneider * drops pool->lock 4491c5aa87bbSTejun Heo */ 44929ab03be4SValentin Schneider while (true) { 44939ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 44949ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4495d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4496e02b9312SValentin Schneider 4497e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 44989ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 44999ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4500692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 45019ab03be4SValentin Schneider break; 45029ab03be4SValentin Schneider } 45039ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4504e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 45059ab03be4SValentin Schneider } 4506692b4825STejun Heo 45071037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4508e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 450929c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4510a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 451160f5a4bcSLai Jiangshan 4512e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4513e02b9312SValentin Schneider 4514e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 451560f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 45161258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 451760f5a4bcSLai Jiangshan 451860f5a4bcSLai Jiangshan if (pool->detach_completion) 451960f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 452060f5a4bcSLai Jiangshan 452129c91e99STejun Heo /* shut down the timers */ 452229c91e99STejun Heo del_timer_sync(&pool->idle_timer); 45233f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 452429c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 452529c91e99STejun Heo 452624acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 452725b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 452829c91e99STejun Heo } 452929c91e99STejun Heo 453029c91e99STejun Heo /** 453129c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 453229c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 453329c91e99STejun Heo * 453429c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 453529c91e99STejun Heo * reference count and return it. If there already is a matching 453629c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4537d185af30SYacine Belkadi * create a new one. 4538a892caccSTejun Heo * 4539a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4540d185af30SYacine Belkadi * 4541d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4542d185af30SYacine Belkadi * On failure, %NULL. 454329c91e99STejun Heo */ 454429c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 454529c91e99STejun Heo { 454684193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 454729c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 454829c91e99STejun Heo struct worker_pool *pool; 454984193c07STejun Heo int pod, node = NUMA_NO_NODE; 455029c91e99STejun Heo 4551a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 455229c91e99STejun Heo 455329c91e99STejun Heo /* do we already have a matching pool? */ 455429c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 455529c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 455629c91e99STejun Heo pool->refcnt++; 45573fb1823cSLai Jiangshan return pool; 455829c91e99STejun Heo } 455929c91e99STejun Heo } 456029c91e99STejun Heo 45619546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 456284193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 45639546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 456484193c07STejun Heo node = pt->pod_node[pod]; 4565e2273584SXunlei Pang break; 4566e2273584SXunlei Pang } 4567e2273584SXunlei Pang } 4568e2273584SXunlei Pang 456929c91e99STejun Heo /* nope, create a new one */ 457084193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 457129c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 457229c91e99STejun Heo goto fail; 457329c91e99STejun Heo 457484193c07STejun Heo pool->node = node; 45755de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 45765de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 45772865a8fbSShaohua Li 457829c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 457929c91e99STejun Heo goto fail; 458029c91e99STejun Heo 458129c91e99STejun Heo /* create and start the initial worker */ 45823347fa09STejun Heo if (wq_online && !create_worker(pool)) 458329c91e99STejun Heo goto fail; 458429c91e99STejun Heo 458529c91e99STejun Heo /* install */ 458629c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 45873fb1823cSLai Jiangshan 458829c91e99STejun Heo return pool; 458929c91e99STejun Heo fail: 459029c91e99STejun Heo if (pool) 459129c91e99STejun Heo put_unbound_pool(pool); 459229c91e99STejun Heo return NULL; 459329c91e99STejun Heo } 459429c91e99STejun Heo 45958864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 45968864b4e5STejun Heo { 45978864b4e5STejun Heo kmem_cache_free(pwq_cache, 45988864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 45998864b4e5STejun Heo } 46008864b4e5STejun Heo 46018864b4e5STejun Heo /* 4602967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4603967b494eSTejun Heo * refcnt and needs to be destroyed. 46048864b4e5STejun Heo */ 4605687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 46068864b4e5STejun Heo { 46078864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4608687a9aa5STejun Heo release_work); 46098864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 46108864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4611b42b0bddSYang Yingliang bool is_last = false; 46128864b4e5STejun Heo 4613b42b0bddSYang Yingliang /* 4614687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4615b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4616b42b0bddSYang Yingliang */ 4617b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 46183c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 46198864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4620bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 46213c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4622b42b0bddSYang Yingliang } 46238864b4e5STejun Heo 4624687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4625a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 46268864b4e5STejun Heo put_unbound_pool(pool); 4627a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4628687a9aa5STejun Heo } 4629a892caccSTejun Heo 46305797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 46315797b1c1STejun Heo struct wq_node_nr_active *nna = 46325797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 46335797b1c1STejun Heo 46345797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 46355797b1c1STejun Heo list_del_init(&pwq->pending_node); 46365797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 46375797b1c1STejun Heo } 46385797b1c1STejun Heo 463925b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 46408864b4e5STejun Heo 46418864b4e5STejun Heo /* 46428864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4643e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 46448864b4e5STejun Heo */ 4645669de8bdSBart Van Assche if (is_last) { 4646669de8bdSBart Van Assche wq_unregister_lockdep(wq); 464725b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 46486029a918STejun Heo } 4649669de8bdSBart Van Assche } 46508864b4e5STejun Heo 465167dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4652f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4653f147f29eSTejun Heo struct worker_pool *pool) 4654d2c1d404STejun Heo { 4655d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4656d2c1d404STejun Heo 4657e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4658e50aba9aSTejun Heo 4659d2c1d404STejun Heo pwq->pool = pool; 4660d2c1d404STejun Heo pwq->wq = wq; 4661d2c1d404STejun Heo pwq->flush_color = -1; 46628864b4e5STejun Heo pwq->refcnt = 1; 4663f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 46645797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 46651befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4666d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4667687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4668f147f29eSTejun Heo } 4669d2c1d404STejun Heo 4670f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 46711befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4672f147f29eSTejun Heo { 4673f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4674f147f29eSTejun Heo 4675f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 467675ccf595STejun Heo 46771befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 46781befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 46791befcf30STejun Heo return; 46801befcf30STejun Heo 468129b1cb41SLai Jiangshan /* set the matching work_color */ 468275ccf595STejun Heo pwq->work_color = wq->work_color; 4683983ca25eSTejun Heo 4684983ca25eSTejun Heo /* link in @pwq */ 46859e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4686df2d5ae4STejun Heo } 46876029a918STejun Heo 4688f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4689f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4690f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4691f147f29eSTejun Heo { 4692f147f29eSTejun Heo struct worker_pool *pool; 4693f147f29eSTejun Heo struct pool_workqueue *pwq; 4694f147f29eSTejun Heo 4695f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4696f147f29eSTejun Heo 4697f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4698f147f29eSTejun Heo if (!pool) 4699f147f29eSTejun Heo return NULL; 4700f147f29eSTejun Heo 4701e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4702f147f29eSTejun Heo if (!pwq) { 4703f147f29eSTejun Heo put_unbound_pool(pool); 4704f147f29eSTejun Heo return NULL; 4705f147f29eSTejun Heo } 4706f147f29eSTejun Heo 4707f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4708f147f29eSTejun Heo return pwq; 4709d2c1d404STejun Heo } 4710d2c1d404STejun Heo 47114c16bd32STejun Heo /** 4712fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4713042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 471484193c07STejun Heo * @cpu: the target CPU 47154c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 47164c16bd32STejun Heo * 4717fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4718fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 47199546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 47204c16bd32STejun Heo * 4721fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4722fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4723fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 47244c16bd32STejun Heo * 4725fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 47264c16bd32STejun Heo */ 47279546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 47289546b29eSTejun Heo int cpu_going_down) 47294c16bd32STejun Heo { 473084193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 473184193c07STejun Heo int pod = pt->cpu_pod[cpu]; 47324c16bd32STejun Heo 4733fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 47349546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 47359546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 47364c16bd32STejun Heo if (cpu_going_down >= 0) 47379546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 47384c16bd32STejun Heo 47399546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 47409546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 474184193c07STejun Heo return; 474284193c07STejun Heo } 47434c16bd32STejun Heo 4744fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 47459546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 47461ad0f0a7SMichael Bringmann 47479546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 47481ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 47491ad0f0a7SMichael Bringmann "possible intersect\n"); 47504c16bd32STejun Heo } 47514c16bd32STejun Heo 47529f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4753636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4754636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 47551befcf30STejun Heo { 47569f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 47571befcf30STejun Heo struct pool_workqueue *old_pwq; 47581befcf30STejun Heo 47595b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 47601befcf30STejun Heo lockdep_assert_held(&wq->mutex); 47611befcf30STejun Heo 47621befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 47631befcf30STejun Heo link_pwq(pwq); 47641befcf30STejun Heo 47659f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 47669f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 47671befcf30STejun Heo return old_pwq; 47681befcf30STejun Heo } 47691befcf30STejun Heo 47702d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 47712d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 47722d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 47732d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4774042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 47752d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 47762d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 47772d5f0764SLai Jiangshan }; 47782d5f0764SLai Jiangshan 47792d5f0764SLai Jiangshan /* free the resources after success or abort */ 47802d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 47812d5f0764SLai Jiangshan { 47822d5f0764SLai Jiangshan if (ctx) { 4783636b927eSTejun Heo int cpu; 47842d5f0764SLai Jiangshan 4785636b927eSTejun Heo for_each_possible_cpu(cpu) 4786636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 47872d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 47882d5f0764SLai Jiangshan 47892d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 47902d5f0764SLai Jiangshan 47912d5f0764SLai Jiangshan kfree(ctx); 47922d5f0764SLai Jiangshan } 47932d5f0764SLai Jiangshan } 47942d5f0764SLai Jiangshan 47952d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 47962d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 47972d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 479899c621efSLai Jiangshan const struct workqueue_attrs *attrs, 479999c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 48002d5f0764SLai Jiangshan { 48012d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 48029546b29eSTejun Heo struct workqueue_attrs *new_attrs; 4803636b927eSTejun Heo int cpu; 48042d5f0764SLai Jiangshan 48052d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 48062d5f0764SLai Jiangshan 480784193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 480884193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 480984193c07STejun Heo return ERR_PTR(-EINVAL); 481084193c07STejun Heo 4811636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 48122d5f0764SLai Jiangshan 4813be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 48149546b29eSTejun Heo if (!ctx || !new_attrs) 48152d5f0764SLai Jiangshan goto out_free; 48162d5f0764SLai Jiangshan 4817042f7df1SLai Jiangshan /* 48182d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 48192d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 48202d5f0764SLai Jiangshan * it even if we don't use it immediately. 48212d5f0764SLai Jiangshan */ 48220f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 48230f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 48249546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 48252d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 48262d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 48272d5f0764SLai Jiangshan goto out_free; 48282d5f0764SLai Jiangshan 4829636b927eSTejun Heo for_each_possible_cpu(cpu) { 4830af73f5c9STejun Heo if (new_attrs->ordered) { 48312d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4832636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4833636b927eSTejun Heo } else { 48349546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 48359546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 4836636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4837636b927eSTejun Heo goto out_free; 48382d5f0764SLai Jiangshan } 48392d5f0764SLai Jiangshan } 48402d5f0764SLai Jiangshan 4841042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4842042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4843042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 48449546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 48452d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4846042f7df1SLai Jiangshan 48472d5f0764SLai Jiangshan ctx->wq = wq; 48482d5f0764SLai Jiangshan return ctx; 48492d5f0764SLai Jiangshan 48502d5f0764SLai Jiangshan out_free: 48512d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 48522d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 485384193c07STejun Heo return ERR_PTR(-ENOMEM); 48542d5f0764SLai Jiangshan } 48552d5f0764SLai Jiangshan 48562d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 48572d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 48582d5f0764SLai Jiangshan { 4859636b927eSTejun Heo int cpu; 48602d5f0764SLai Jiangshan 48612d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 48622d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 48632d5f0764SLai Jiangshan 48642d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 48652d5f0764SLai Jiangshan 48669f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 4867636b927eSTejun Heo for_each_possible_cpu(cpu) 4868636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4869636b927eSTejun Heo ctx->pwq_tbl[cpu]); 48709f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 48712d5f0764SLai Jiangshan 48725797b1c1STejun Heo /* update node_nr_active->max */ 48735797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 48745797b1c1STejun Heo 48752d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 48762d5f0764SLai Jiangshan } 48772d5f0764SLai Jiangshan 4878a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4879a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4880a0111cf6SLai Jiangshan { 4881a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4882a0111cf6SLai Jiangshan 4883a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4884a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4885a0111cf6SLai Jiangshan return -EINVAL; 4886a0111cf6SLai Jiangshan 4887a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 48880a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 48890a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4890a0111cf6SLai Jiangshan return -EINVAL; 4891a0111cf6SLai Jiangshan 48920a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 48930a94efb5STejun Heo } 48940a94efb5STejun Heo 489599c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 489684193c07STejun Heo if (IS_ERR(ctx)) 489784193c07STejun Heo return PTR_ERR(ctx); 4898a0111cf6SLai Jiangshan 4899a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4900a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4901a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4902a0111cf6SLai Jiangshan 49036201171eSwanghaibin return 0; 4904a0111cf6SLai Jiangshan } 4905a0111cf6SLai Jiangshan 49069e8cd2f5STejun Heo /** 49079e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 49089e8cd2f5STejun Heo * @wq: the target workqueue 49099e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 49109e8cd2f5STejun Heo * 4911fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4912fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4913fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4914fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4915fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 49169e8cd2f5STejun Heo * 4917d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4918d185af30SYacine Belkadi * 4919ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4920509b3204SDaniel Jordan * 4921d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 49229e8cd2f5STejun Heo */ 4923513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 49249e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 49259e8cd2f5STejun Heo { 4926a0111cf6SLai Jiangshan int ret; 49279e8cd2f5STejun Heo 4928509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4929509b3204SDaniel Jordan 4930509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4931a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4932509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 49332d5f0764SLai Jiangshan 49342d5f0764SLai Jiangshan return ret; 49359e8cd2f5STejun Heo } 49369e8cd2f5STejun Heo 49374c16bd32STejun Heo /** 4938fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 49394c16bd32STejun Heo * @wq: the target workqueue 49404cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 49414cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 49424c16bd32STejun Heo * @online: whether @cpu is coming up or going down 49434c16bd32STejun Heo * 49444c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4945fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 49464c16bd32STejun Heo * @wq accordingly. 49474c16bd32STejun Heo * 49484c16bd32STejun Heo * 4949fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4950fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4951fef59c9cSTejun Heo * 4952fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4953fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4954fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4955fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4956fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4957fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 49584c16bd32STejun Heo */ 4959fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 49604cbfd3deSTejun Heo int hotplug_cpu, bool online) 49614c16bd32STejun Heo { 49624cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 49634c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 49644c16bd32STejun Heo struct workqueue_attrs *target_attrs; 49654c16bd32STejun Heo 49664c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 49674c16bd32STejun Heo 496884193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 49694c16bd32STejun Heo return; 49704c16bd32STejun Heo 49714c16bd32STejun Heo /* 49724c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 49734c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 49744c16bd32STejun Heo * CPU hotplug exclusion. 49754c16bd32STejun Heo */ 4976fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 49774c16bd32STejun Heo 49784c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 49790f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 49804c16bd32STejun Heo 4981636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 49829546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 49839f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 4984f7142ed4SLai Jiangshan return; 49854c16bd32STejun Heo 49864c16bd32STejun Heo /* create a new pwq */ 49874c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 49884c16bd32STejun Heo if (!pwq) { 4989fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 49904c16bd32STejun Heo wq->name); 499177f300b1SDaeseok Youn goto use_dfl_pwq; 49924c16bd32STejun Heo } 49934c16bd32STejun Heo 4994f7142ed4SLai Jiangshan /* Install the new pwq. */ 49954c16bd32STejun Heo mutex_lock(&wq->mutex); 4996636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 49974c16bd32STejun Heo goto out_unlock; 49984c16bd32STejun Heo 49994c16bd32STejun Heo use_dfl_pwq: 5000f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 50019f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 50029f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 50039f66cff2STejun Heo get_pwq(pwq); 50049f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 50059f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 50064c16bd32STejun Heo out_unlock: 50074c16bd32STejun Heo mutex_unlock(&wq->mutex); 50084c16bd32STejun Heo put_pwq_unlocked(old_pwq); 50094c16bd32STejun Heo } 50104c16bd32STejun Heo 501130cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 50121da177e4SLinus Torvalds { 501349e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 50148a2b7538STejun Heo int cpu, ret; 5015e1d8aa9fSFrederic Weisbecker 5016687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5017ee1ceef7STejun Heo if (!wq->cpu_pwq) 5018687a9aa5STejun Heo goto enomem; 501930cdf249STejun Heo 5020636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 502130cdf249STejun Heo for_each_possible_cpu(cpu) { 5022687a9aa5STejun Heo struct pool_workqueue **pwq_p = 5023ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 5024687a9aa5STejun Heo struct worker_pool *pool = 5025687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 502630cdf249STejun Heo 5027687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5028687a9aa5STejun Heo pool->node); 5029687a9aa5STejun Heo if (!*pwq_p) 5030687a9aa5STejun Heo goto enomem; 5031687a9aa5STejun Heo 5032687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5033f147f29eSTejun Heo 5034f147f29eSTejun Heo mutex_lock(&wq->mutex); 5035687a9aa5STejun Heo link_pwq(*pwq_p); 5036f147f29eSTejun Heo mutex_unlock(&wq->mutex); 503730cdf249STejun Heo } 503830cdf249STejun Heo return 0; 5039509b3204SDaniel Jordan } 5040509b3204SDaniel Jordan 5041ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5042509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 50439f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 50449f66cff2STejun Heo 50458a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 50468a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 50479f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 50489f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 50499f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 50508a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 50519e8cd2f5STejun Heo } else { 5052509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 50539e8cd2f5STejun Heo } 5054ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5055509b3204SDaniel Jordan 505664344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 505764344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 505864344553SZqiang */ 505964344553SZqiang if (ret) 506064344553SZqiang kthread_flush_worker(pwq_release_worker); 506164344553SZqiang 5062509b3204SDaniel Jordan return ret; 5063687a9aa5STejun Heo 5064687a9aa5STejun Heo enomem: 5065687a9aa5STejun Heo if (wq->cpu_pwq) { 50667b42f401SZqiang for_each_possible_cpu(cpu) { 50677b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 50687b42f401SZqiang 50697b42f401SZqiang if (pwq) 50707b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 50717b42f401SZqiang } 5072687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5073687a9aa5STejun Heo wq->cpu_pwq = NULL; 5074687a9aa5STejun Heo } 5075687a9aa5STejun Heo return -ENOMEM; 50760f900049STejun Heo } 50770f900049STejun Heo 5078f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5079f3421797STejun Heo const char *name) 5080b71ab8c2STejun Heo { 5081636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5082044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5083636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5084b71ab8c2STejun Heo 5085636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5086b71ab8c2STejun Heo } 5087b71ab8c2STejun Heo 5088983c7515STejun Heo /* 5089983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5090983c7515STejun Heo * to guarantee forward progress. 5091983c7515STejun Heo */ 5092983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5093983c7515STejun Heo { 5094983c7515STejun Heo struct worker *rescuer; 5095b92b36eaSDan Carpenter int ret; 5096983c7515STejun Heo 5097983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5098983c7515STejun Heo return 0; 5099983c7515STejun Heo 5100983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 51014c0736a7SPetr Mladek if (!rescuer) { 51024c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 51034c0736a7SPetr Mladek wq->name); 5104983c7515STejun Heo return -ENOMEM; 51054c0736a7SPetr Mladek } 5106983c7515STejun Heo 5107983c7515STejun Heo rescuer->rescue_wq = wq; 5108b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5109f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5110b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 51114c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 51124c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5113983c7515STejun Heo kfree(rescuer); 5114b92b36eaSDan Carpenter return ret; 5115983c7515STejun Heo } 5116983c7515STejun Heo 5117983c7515STejun Heo wq->rescuer = rescuer; 511885f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 511985f0ab43SJuri Lelli kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); 512085f0ab43SJuri Lelli else 5121983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5122983c7515STejun Heo wake_up_process(rescuer->task); 5123983c7515STejun Heo 5124983c7515STejun Heo return 0; 5125983c7515STejun Heo } 5126983c7515STejun Heo 5127a045a272STejun Heo /** 5128a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5129a045a272STejun Heo * @wq: target workqueue 5130a045a272STejun Heo * 5131a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5132a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5133a045a272STejun Heo * @wq->max_active to zero. 5134a045a272STejun Heo */ 5135a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5136a045a272STejun Heo { 5137c5404d4eSTejun Heo bool activated; 51385797b1c1STejun Heo int new_max, new_min; 5139a045a272STejun Heo 5140a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5141a045a272STejun Heo 5142a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 51435797b1c1STejun Heo new_max = 0; 51445797b1c1STejun Heo new_min = 0; 51455797b1c1STejun Heo } else { 51465797b1c1STejun Heo new_max = wq->saved_max_active; 51475797b1c1STejun Heo new_min = wq->saved_min_active; 5148a045a272STejun Heo } 5149a045a272STejun Heo 51505797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5151a045a272STejun Heo return; 5152a045a272STejun Heo 5153a045a272STejun Heo /* 51545797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5155a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5156a045a272STejun Heo * because new work items are always queued behind existing inactive 5157a045a272STejun Heo * work items if there are any. 5158a045a272STejun Heo */ 51595797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 51605797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 51615797b1c1STejun Heo 51625797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 51635797b1c1STejun Heo wq_update_node_max_active(wq, -1); 51645797b1c1STejun Heo 51655797b1c1STejun Heo if (new_max == 0) 51665797b1c1STejun Heo return; 5167a045a272STejun Heo 5168c5404d4eSTejun Heo /* 5169c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5170c5404d4eSTejun Heo * until max_active is filled. 5171c5404d4eSTejun Heo */ 5172c5404d4eSTejun Heo do { 5173c5404d4eSTejun Heo struct pool_workqueue *pwq; 5174c5404d4eSTejun Heo 5175c5404d4eSTejun Heo activated = false; 5176a045a272STejun Heo for_each_pwq(pwq, wq) { 5177a045a272STejun Heo unsigned long flags; 5178a045a272STejun Heo 5179c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5180a045a272STejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, flags); 51815797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5182c5404d4eSTejun Heo activated = true; 5183a045a272STejun Heo kick_pool(pwq->pool); 5184c5404d4eSTejun Heo } 5185a045a272STejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 5186a045a272STejun Heo } 5187c5404d4eSTejun Heo } while (activated); 5188a045a272STejun Heo } 5189a045a272STejun Heo 5190a2775bbcSMathieu Malaterre __printf(1, 4) 5191669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 519297e37d7bSTejun Heo unsigned int flags, 5193669de8bdSBart Van Assche int max_active, ...) 51943af24433SOleg Nesterov { 5195ecf6881fSTejun Heo va_list args; 51963af24433SOleg Nesterov struct workqueue_struct *wq; 519791ccc6e7STejun Heo size_t wq_size; 519891ccc6e7STejun Heo int name_len; 5199b196be89STejun Heo 52005c0338c6STejun Heo /* 5201fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 5202fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 52035c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 5204fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 52055c0338c6STejun Heo */ 52065c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 52075c0338c6STejun Heo flags |= __WQ_ORDERED; 52085c0338c6STejun Heo 5209cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5210cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5211cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5212cee22a15SViresh Kumar 5213ecf6881fSTejun Heo /* allocate wq and format name */ 521491ccc6e7STejun Heo if (flags & WQ_UNBOUND) 521591ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 521691ccc6e7STejun Heo else 521791ccc6e7STejun Heo wq_size = sizeof(*wq); 521891ccc6e7STejun Heo 521991ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5220b196be89STejun Heo if (!wq) 5221d2c1d404STejun Heo return NULL; 5222b196be89STejun Heo 52236029a918STejun Heo if (flags & WQ_UNBOUND) { 5224be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 52256029a918STejun Heo if (!wq->unbound_attrs) 52266029a918STejun Heo goto err_free_wq; 52276029a918STejun Heo } 52286029a918STejun Heo 5229669de8bdSBart Van Assche va_start(args, max_active); 523091ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5231b196be89STejun Heo va_end(args); 52323af24433SOleg Nesterov 523391ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 523491ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 523591ccc6e7STejun Heo wq->name); 523631c89007SAudra Mitchell 5237d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5238b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 52393af24433SOleg Nesterov 5240b196be89STejun Heo /* init wq */ 524197e37d7bSTejun Heo wq->flags = flags; 5242a045a272STejun Heo wq->max_active = max_active; 52435797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 52445797b1c1STejun Heo wq->saved_max_active = wq->max_active; 52455797b1c1STejun Heo wq->saved_min_active = wq->min_active; 52463c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5247112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 524830cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 524973f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 525073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5251493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 52523af24433SOleg Nesterov 5253669de8bdSBart Van Assche wq_init_lockdep(wq); 5254cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 52553af24433SOleg Nesterov 525691ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 525791ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 525882efcab3SBart Van Assche goto err_unreg_lockdep; 525991ccc6e7STejun Heo } 526091ccc6e7STejun Heo 526191ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 526291ccc6e7STejun Heo goto err_free_node_nr_active; 52631537663fSTejun Heo 526440c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5265d2c1d404STejun Heo goto err_destroy; 5266e22bee78STejun Heo 5267226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5268226223abSTejun Heo goto err_destroy; 5269226223abSTejun Heo 52706af8bf3dSOleg Nesterov /* 527168e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 527268e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 527368e13a67SLai Jiangshan * list. 52746af8bf3dSOleg Nesterov */ 527568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5276a0a1a5fdSTejun Heo 5277a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5278a045a272STejun Heo wq_adjust_max_active(wq); 5279a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5280a0a1a5fdSTejun Heo 5281e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5282a0a1a5fdSTejun Heo 528368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 52843af24433SOleg Nesterov 52853af24433SOleg Nesterov return wq; 5286d2c1d404STejun Heo 528791ccc6e7STejun Heo err_free_node_nr_active: 528891ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 528991ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 529082efcab3SBart Van Assche err_unreg_lockdep: 5291009bb421SBart Van Assche wq_unregister_lockdep(wq); 5292009bb421SBart Van Assche wq_free_lockdep(wq); 529382efcab3SBart Van Assche err_free_wq: 52946029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 52954690c4abSTejun Heo kfree(wq); 5296d2c1d404STejun Heo return NULL; 5297d2c1d404STejun Heo err_destroy: 5298d2c1d404STejun Heo destroy_workqueue(wq); 52994690c4abSTejun Heo return NULL; 53001da177e4SLinus Torvalds } 5301669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 53021da177e4SLinus Torvalds 5303c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5304c29eb853STejun Heo { 5305c29eb853STejun Heo int i; 5306c29eb853STejun Heo 5307c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5308c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5309c29eb853STejun Heo return true; 5310c29eb853STejun Heo 53119f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5312c29eb853STejun Heo return true; 5313afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5314c29eb853STejun Heo return true; 5315c29eb853STejun Heo 5316c29eb853STejun Heo return false; 5317c29eb853STejun Heo } 5318c29eb853STejun Heo 53193af24433SOleg Nesterov /** 53203af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 53213af24433SOleg Nesterov * @wq: target workqueue 53223af24433SOleg Nesterov * 53233af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 53243af24433SOleg Nesterov */ 53253af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 53263af24433SOleg Nesterov { 532749e3cf44STejun Heo struct pool_workqueue *pwq; 5328636b927eSTejun Heo int cpu; 53293af24433SOleg Nesterov 5330def98c84STejun Heo /* 5331def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5332def98c84STejun Heo * lead to sysfs name conflicts. 5333def98c84STejun Heo */ 5334def98c84STejun Heo workqueue_sysfs_unregister(wq); 5335def98c84STejun Heo 533633e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 533733e3f0a3SRichard Clark mutex_lock(&wq->mutex); 533833e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 533933e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 534033e3f0a3SRichard Clark 53419c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 53429c5a2ba7STejun Heo drain_workqueue(wq); 5343c8efcc25STejun Heo 5344def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5345def98c84STejun Heo if (wq->rescuer) { 5346def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5347def98c84STejun Heo 5348def98c84STejun Heo /* this prevents new queueing */ 5349a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5350def98c84STejun Heo wq->rescuer = NULL; 5351a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5352def98c84STejun Heo 5353def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5354def98c84STejun Heo kthread_stop(rescuer->task); 53558efe1223STejun Heo kfree(rescuer); 5356def98c84STejun Heo } 5357def98c84STejun Heo 5358c29eb853STejun Heo /* 5359c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5360c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5361c29eb853STejun Heo */ 5362c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5363b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 536449e3cf44STejun Heo for_each_pwq(pwq, wq) { 5365a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5366c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 53671d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5368e66b39afSTejun Heo __func__, wq->name); 5369c29eb853STejun Heo show_pwq(pwq); 5370a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5371b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5372c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 537355df0933SImran Khan show_one_workqueue(wq); 53746183c009STejun Heo return; 537576af4d93STejun Heo } 5376a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 537776af4d93STejun Heo } 5378b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 53796183c009STejun Heo 5380a0a1a5fdSTejun Heo /* 5381a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5382a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5383a0a1a5fdSTejun Heo */ 5384e2dca7adSTejun Heo list_del_rcu(&wq->list); 538568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 53863af24433SOleg Nesterov 53878864b4e5STejun Heo /* 5388636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5389636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5390636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 53918864b4e5STejun Heo */ 5392636b927eSTejun Heo rcu_read_lock(); 5393636b927eSTejun Heo 5394636b927eSTejun Heo for_each_possible_cpu(cpu) { 53959f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 53969f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 53974c16bd32STejun Heo } 53984c16bd32STejun Heo 53999f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 54009f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5401636b927eSTejun Heo 5402636b927eSTejun Heo rcu_read_unlock(); 54033af24433SOleg Nesterov } 54043af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 54053af24433SOleg Nesterov 5406dcd989cbSTejun Heo /** 5407dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5408dcd989cbSTejun Heo * @wq: target workqueue 5409dcd989cbSTejun Heo * @max_active: new max_active value. 5410dcd989cbSTejun Heo * 54115797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 54125797b1c1STejun Heo * comment. 5413dcd989cbSTejun Heo * 5414dcd989cbSTejun Heo * CONTEXT: 5415dcd989cbSTejun Heo * Don't call from IRQ context. 5416dcd989cbSTejun Heo */ 5417dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5418dcd989cbSTejun Heo { 54198719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 54200a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 54218719dceaSTejun Heo return; 54228719dceaSTejun Heo 5423f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5424dcd989cbSTejun Heo 5425a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5426dcd989cbSTejun Heo 54270a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 5428dcd989cbSTejun Heo wq->saved_max_active = max_active; 54295797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 54305797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 54315797b1c1STejun Heo 5432a045a272STejun Heo wq_adjust_max_active(wq); 5433dcd989cbSTejun Heo 5434a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5435dcd989cbSTejun Heo } 5436dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5437dcd989cbSTejun Heo 5438dcd989cbSTejun Heo /** 543927d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 544027d4ee03SLukas Wunner * 544127d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 544227d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 544327d4ee03SLukas Wunner * 544427d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 544527d4ee03SLukas Wunner */ 544627d4ee03SLukas Wunner struct work_struct *current_work(void) 544727d4ee03SLukas Wunner { 544827d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 544927d4ee03SLukas Wunner 545027d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 545127d4ee03SLukas Wunner } 545227d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 545327d4ee03SLukas Wunner 545427d4ee03SLukas Wunner /** 5455e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5456e6267616STejun Heo * 5457e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5458e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5459d185af30SYacine Belkadi * 5460d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5461e6267616STejun Heo */ 5462e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5463e6267616STejun Heo { 5464e6267616STejun Heo struct worker *worker = current_wq_worker(); 5465e6267616STejun Heo 54666a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5467e6267616STejun Heo } 5468e6267616STejun Heo 5469e6267616STejun Heo /** 5470dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5471dcd989cbSTejun Heo * @cpu: CPU in question 5472dcd989cbSTejun Heo * @wq: target workqueue 5473dcd989cbSTejun Heo * 5474dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5475dcd989cbSTejun Heo * no synchronization around this function and the test result is 5476dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5477dcd989cbSTejun Heo * 5478d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5479636b927eSTejun Heo * 5480636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5481636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5482636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5483636b927eSTejun Heo * other CPUs. 5484d3251859STejun Heo * 5485d185af30SYacine Belkadi * Return: 5486dcd989cbSTejun Heo * %true if congested, %false otherwise. 5487dcd989cbSTejun Heo */ 5488d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5489dcd989cbSTejun Heo { 54907fb98ea7STejun Heo struct pool_workqueue *pwq; 549176af4d93STejun Heo bool ret; 549276af4d93STejun Heo 549324acfb71SThomas Gleixner rcu_read_lock(); 549424acfb71SThomas Gleixner preempt_disable(); 54957fb98ea7STejun Heo 5496d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5497d3251859STejun Heo cpu = smp_processor_id(); 5498d3251859STejun Heo 5499687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5500f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5501636b927eSTejun Heo 550224acfb71SThomas Gleixner preempt_enable(); 550324acfb71SThomas Gleixner rcu_read_unlock(); 550476af4d93STejun Heo 550576af4d93STejun Heo return ret; 5506dcd989cbSTejun Heo } 5507dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5508dcd989cbSTejun Heo 5509dcd989cbSTejun Heo /** 5510dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5511dcd989cbSTejun Heo * @work: the work to be tested 5512dcd989cbSTejun Heo * 5513dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5514dcd989cbSTejun Heo * synchronization around this function and the test result is 5515dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5516dcd989cbSTejun Heo * 5517d185af30SYacine Belkadi * Return: 5518dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5519dcd989cbSTejun Heo */ 5520dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5521dcd989cbSTejun Heo { 5522fa1b54e6STejun Heo struct worker_pool *pool; 5523dcd989cbSTejun Heo unsigned long flags; 5524dcd989cbSTejun Heo unsigned int ret = 0; 5525dcd989cbSTejun Heo 5526dcd989cbSTejun Heo if (work_pending(work)) 5527dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5528038366c5SLai Jiangshan 552924acfb71SThomas Gleixner rcu_read_lock(); 5530fa1b54e6STejun Heo pool = get_work_pool(work); 5531038366c5SLai Jiangshan if (pool) { 5532a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 5533c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5534dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5535a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 5536038366c5SLai Jiangshan } 553724acfb71SThomas Gleixner rcu_read_unlock(); 5538dcd989cbSTejun Heo 5539dcd989cbSTejun Heo return ret; 5540dcd989cbSTejun Heo } 5541dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5542dcd989cbSTejun Heo 55433d1cb205STejun Heo /** 55443d1cb205STejun Heo * set_worker_desc - set description for the current work item 55453d1cb205STejun Heo * @fmt: printf-style format string 55463d1cb205STejun Heo * @...: arguments for the format string 55473d1cb205STejun Heo * 55483d1cb205STejun Heo * This function can be called by a running work function to describe what 55493d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 55503d1cb205STejun Heo * information will be printed out together to help debugging. The 55513d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 55523d1cb205STejun Heo */ 55533d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 55543d1cb205STejun Heo { 55553d1cb205STejun Heo struct worker *worker = current_wq_worker(); 55563d1cb205STejun Heo va_list args; 55573d1cb205STejun Heo 55583d1cb205STejun Heo if (worker) { 55593d1cb205STejun Heo va_start(args, fmt); 55603d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 55613d1cb205STejun Heo va_end(args); 55623d1cb205STejun Heo } 55633d1cb205STejun Heo } 55645c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 55653d1cb205STejun Heo 55663d1cb205STejun Heo /** 55673d1cb205STejun Heo * print_worker_info - print out worker information and description 55683d1cb205STejun Heo * @log_lvl: the log level to use when printing 55693d1cb205STejun Heo * @task: target task 55703d1cb205STejun Heo * 55713d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 55723d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 55733d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 55743d1cb205STejun Heo * 55753d1cb205STejun Heo * This function can be safely called on any task as long as the 55763d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 55773d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 55783d1cb205STejun Heo */ 55793d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 55803d1cb205STejun Heo { 55813d1cb205STejun Heo work_func_t *fn = NULL; 55823d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 55833d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 55843d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 55853d1cb205STejun Heo struct workqueue_struct *wq = NULL; 55863d1cb205STejun Heo struct worker *worker; 55873d1cb205STejun Heo 55883d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 55893d1cb205STejun Heo return; 55903d1cb205STejun Heo 55913d1cb205STejun Heo /* 55923d1cb205STejun Heo * This function is called without any synchronization and @task 55933d1cb205STejun Heo * could be in any state. Be careful with dereferences. 55943d1cb205STejun Heo */ 5595e700591aSPetr Mladek worker = kthread_probe_data(task); 55963d1cb205STejun Heo 55973d1cb205STejun Heo /* 55988bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 55998bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 56003d1cb205STejun Heo */ 5601fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5602fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5603fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5604fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5605fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 56063d1cb205STejun Heo 56073d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5608d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 56098bf89593STejun Heo if (strcmp(name, desc)) 56103d1cb205STejun Heo pr_cont(" (%s)", desc); 56113d1cb205STejun Heo pr_cont("\n"); 56123d1cb205STejun Heo } 56133d1cb205STejun Heo } 56143d1cb205STejun Heo 56153494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 56163494fc30STejun Heo { 56173494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 56183494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 56193494fc30STejun Heo pr_cont(" node=%d", pool->node); 56203494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 56213494fc30STejun Heo } 56223494fc30STejun Heo 5623c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5624c76feb0dSPaul E. McKenney bool comma; 5625c76feb0dSPaul E. McKenney work_func_t func; 5626c76feb0dSPaul E. McKenney long ctr; 5627c76feb0dSPaul E. McKenney }; 5628c76feb0dSPaul E. McKenney 5629c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5630c76feb0dSPaul E. McKenney { 5631c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5632c76feb0dSPaul E. McKenney goto out_record; 5633c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5634c76feb0dSPaul E. McKenney pcwsp->ctr++; 5635c76feb0dSPaul E. McKenney return; 5636c76feb0dSPaul E. McKenney } 5637c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5638c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5639c76feb0dSPaul E. McKenney else 5640c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5641c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5642c76feb0dSPaul E. McKenney out_record: 5643c76feb0dSPaul E. McKenney if ((long)func == -1L) 5644c76feb0dSPaul E. McKenney return; 5645c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5646c76feb0dSPaul E. McKenney pcwsp->func = func; 5647c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5648c76feb0dSPaul E. McKenney } 5649c76feb0dSPaul E. McKenney 5650c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 56513494fc30STejun Heo { 56523494fc30STejun Heo if (work->func == wq_barrier_func) { 56533494fc30STejun Heo struct wq_barrier *barr; 56543494fc30STejun Heo 56553494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 56563494fc30STejun Heo 5657c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 56583494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 56593494fc30STejun Heo task_pid_nr(barr->task)); 56603494fc30STejun Heo } else { 5661c76feb0dSPaul E. McKenney if (!comma) 5662c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5663c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 56643494fc30STejun Heo } 56653494fc30STejun Heo } 56663494fc30STejun Heo 56673494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 56683494fc30STejun Heo { 5669c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 56703494fc30STejun Heo struct worker_pool *pool = pwq->pool; 56713494fc30STejun Heo struct work_struct *work; 56723494fc30STejun Heo struct worker *worker; 56733494fc30STejun Heo bool has_in_flight = false, has_pending = false; 56743494fc30STejun Heo int bkt; 56753494fc30STejun Heo 56763494fc30STejun Heo pr_info(" pwq %d:", pool->id); 56773494fc30STejun Heo pr_cont_pool_info(pool); 56783494fc30STejun Heo 5679a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5680a045a272STejun Heo pwq->nr_active, pwq->refcnt, 56813494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 56823494fc30STejun Heo 56833494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 56843494fc30STejun Heo if (worker->current_pwq == pwq) { 56853494fc30STejun Heo has_in_flight = true; 56863494fc30STejun Heo break; 56873494fc30STejun Heo } 56883494fc30STejun Heo } 56893494fc30STejun Heo if (has_in_flight) { 56903494fc30STejun Heo bool comma = false; 56913494fc30STejun Heo 56923494fc30STejun Heo pr_info(" in-flight:"); 56933494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 56943494fc30STejun Heo if (worker->current_pwq != pwq) 56953494fc30STejun Heo continue; 56963494fc30STejun Heo 5697d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 56983494fc30STejun Heo task_pid_nr(worker->task), 569930ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 57003494fc30STejun Heo worker->current_func); 57013494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5702c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5703c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 57043494fc30STejun Heo comma = true; 57053494fc30STejun Heo } 57063494fc30STejun Heo pr_cont("\n"); 57073494fc30STejun Heo } 57083494fc30STejun Heo 57093494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 57103494fc30STejun Heo if (get_work_pwq(work) == pwq) { 57113494fc30STejun Heo has_pending = true; 57123494fc30STejun Heo break; 57133494fc30STejun Heo } 57143494fc30STejun Heo } 57153494fc30STejun Heo if (has_pending) { 57163494fc30STejun Heo bool comma = false; 57173494fc30STejun Heo 57183494fc30STejun Heo pr_info(" pending:"); 57193494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 57203494fc30STejun Heo if (get_work_pwq(work) != pwq) 57213494fc30STejun Heo continue; 57223494fc30STejun Heo 5723c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 57243494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 57253494fc30STejun Heo } 5726c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 57273494fc30STejun Heo pr_cont("\n"); 57283494fc30STejun Heo } 57293494fc30STejun Heo 5730f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 57313494fc30STejun Heo bool comma = false; 57323494fc30STejun Heo 5733f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5734f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5735c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 57363494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 57373494fc30STejun Heo } 5738c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 57393494fc30STejun Heo pr_cont("\n"); 57403494fc30STejun Heo } 57413494fc30STejun Heo } 57423494fc30STejun Heo 57433494fc30STejun Heo /** 574455df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 574555df0933SImran Khan * @wq: workqueue whose state will be printed 57463494fc30STejun Heo */ 574755df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 57483494fc30STejun Heo { 57493494fc30STejun Heo struct pool_workqueue *pwq; 57503494fc30STejun Heo bool idle = true; 575155df0933SImran Khan unsigned long flags; 57523494fc30STejun Heo 57533494fc30STejun Heo for_each_pwq(pwq, wq) { 5754afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 57553494fc30STejun Heo idle = false; 57563494fc30STejun Heo break; 57573494fc30STejun Heo } 57583494fc30STejun Heo } 575955df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 576055df0933SImran Khan return; 57613494fc30STejun Heo 57623494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 57633494fc30STejun Heo 57643494fc30STejun Heo for_each_pwq(pwq, wq) { 5765a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 5766afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 576757116ce1SJohan Hovold /* 576857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 576957116ce1SJohan Hovold * drivers that queue work while holding locks 577057116ce1SJohan Hovold * also taken in their write paths. 577157116ce1SJohan Hovold */ 577257116ce1SJohan Hovold printk_deferred_enter(); 57733494fc30STejun Heo show_pwq(pwq); 577457116ce1SJohan Hovold printk_deferred_exit(); 577557116ce1SJohan Hovold } 5776a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 577762635ea8SSergey Senozhatsky /* 577862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 577955df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 578062635ea8SSergey Senozhatsky * hard lockup. 578162635ea8SSergey Senozhatsky */ 578262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 57833494fc30STejun Heo } 578455df0933SImran Khan 57853494fc30STejun Heo } 57863494fc30STejun Heo 578755df0933SImran Khan /** 578855df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 578955df0933SImran Khan * @pool: worker pool whose state will be printed 579055df0933SImran Khan */ 579155df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 579255df0933SImran Khan { 57933494fc30STejun Heo struct worker *worker; 57943494fc30STejun Heo bool first = true; 579555df0933SImran Khan unsigned long flags; 5796335a42ebSPetr Mladek unsigned long hung = 0; 57973494fc30STejun Heo 5798a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 57993494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 58003494fc30STejun Heo goto next_pool; 5801335a42ebSPetr Mladek 5802335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5803335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5804335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5805335a42ebSPetr Mladek 580657116ce1SJohan Hovold /* 580757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 580857116ce1SJohan Hovold * queue work while holding locks also taken in their write 580957116ce1SJohan Hovold * paths. 581057116ce1SJohan Hovold */ 581157116ce1SJohan Hovold printk_deferred_enter(); 58123494fc30STejun Heo pr_info("pool %d:", pool->id); 58133494fc30STejun Heo pr_cont_pool_info(pool); 5814335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 58153494fc30STejun Heo if (pool->manager) 58163494fc30STejun Heo pr_cont(" manager: %d", 58173494fc30STejun Heo task_pid_nr(pool->manager->task)); 58183494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 58193494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 58203494fc30STejun Heo task_pid_nr(worker->task)); 58213494fc30STejun Heo first = false; 58223494fc30STejun Heo } 58233494fc30STejun Heo pr_cont("\n"); 582457116ce1SJohan Hovold printk_deferred_exit(); 58253494fc30STejun Heo next_pool: 5826a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 582762635ea8SSergey Senozhatsky /* 582862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 582955df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 583062635ea8SSergey Senozhatsky * hard lockup. 583162635ea8SSergey Senozhatsky */ 583262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 583355df0933SImran Khan 58343494fc30STejun Heo } 58353494fc30STejun Heo 583655df0933SImran Khan /** 583755df0933SImran Khan * show_all_workqueues - dump workqueue state 583855df0933SImran Khan * 5839704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 584055df0933SImran Khan */ 584155df0933SImran Khan void show_all_workqueues(void) 584255df0933SImran Khan { 584355df0933SImran Khan struct workqueue_struct *wq; 584455df0933SImran Khan struct worker_pool *pool; 584555df0933SImran Khan int pi; 584655df0933SImran Khan 584755df0933SImran Khan rcu_read_lock(); 584855df0933SImran Khan 584955df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 585055df0933SImran Khan 585155df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 585255df0933SImran Khan show_one_workqueue(wq); 585355df0933SImran Khan 585455df0933SImran Khan for_each_pool(pool, pi) 585555df0933SImran Khan show_one_worker_pool(pool); 585655df0933SImran Khan 585724acfb71SThomas Gleixner rcu_read_unlock(); 58583494fc30STejun Heo } 58593494fc30STejun Heo 5860704bc669SJungseung Lee /** 5861704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5862704bc669SJungseung Lee * 5863704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5864704bc669SJungseung Lee * still busy. 5865704bc669SJungseung Lee */ 5866704bc669SJungseung Lee void show_freezable_workqueues(void) 5867704bc669SJungseung Lee { 5868704bc669SJungseung Lee struct workqueue_struct *wq; 5869704bc669SJungseung Lee 5870704bc669SJungseung Lee rcu_read_lock(); 5871704bc669SJungseung Lee 5872704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5873704bc669SJungseung Lee 5874704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5875704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5876704bc669SJungseung Lee continue; 5877704bc669SJungseung Lee show_one_workqueue(wq); 5878704bc669SJungseung Lee } 5879704bc669SJungseung Lee 5880704bc669SJungseung Lee rcu_read_unlock(); 5881704bc669SJungseung Lee } 5882704bc669SJungseung Lee 58836b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 58846b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 58856b59808bSTejun Heo { 58866b59808bSTejun Heo int off; 58876b59808bSTejun Heo 58886b59808bSTejun Heo /* always show the actual comm */ 58896b59808bSTejun Heo off = strscpy(buf, task->comm, size); 58906b59808bSTejun Heo if (off < 0) 58916b59808bSTejun Heo return; 58926b59808bSTejun Heo 5893197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 58946b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 58956b59808bSTejun Heo 5896197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5897197f6accSTejun Heo struct worker *worker = kthread_data(task); 5898197f6accSTejun Heo struct worker_pool *pool = worker->pool; 58996b59808bSTejun Heo 59006b59808bSTejun Heo if (pool) { 5901a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 59026b59808bSTejun Heo /* 5903197f6accSTejun Heo * ->desc tracks information (wq name or 5904197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5905197f6accSTejun Heo * current, prepend '+', otherwise '-'. 59066b59808bSTejun Heo */ 59076b59808bSTejun Heo if (worker->desc[0] != '\0') { 59086b59808bSTejun Heo if (worker->current_work) 59096b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 59106b59808bSTejun Heo worker->desc); 59116b59808bSTejun Heo else 59126b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 59136b59808bSTejun Heo worker->desc); 59146b59808bSTejun Heo } 5915a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 59166b59808bSTejun Heo } 5917197f6accSTejun Heo } 59186b59808bSTejun Heo 59196b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 59206b59808bSTejun Heo } 59216b59808bSTejun Heo 592266448bc2SMathieu Malaterre #ifdef CONFIG_SMP 592366448bc2SMathieu Malaterre 5924db7bccf4STejun Heo /* 5925db7bccf4STejun Heo * CPU hotplug. 5926db7bccf4STejun Heo * 5927e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5928112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5929706026c2STejun Heo * pool which make migrating pending and scheduled works very 5930e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 593194cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5932e22bee78STejun Heo * blocked draining impractical. 5933e22bee78STejun Heo * 593424647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5935628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5936628c78e7STejun Heo * cpu comes back online. 5937db7bccf4STejun Heo */ 5938db7bccf4STejun Heo 5939e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5940db7bccf4STejun Heo { 59414ce62e9eSTejun Heo struct worker_pool *pool; 5942db7bccf4STejun Heo struct worker *worker; 5943db7bccf4STejun Heo 5944f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 59451258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5946a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5947e22bee78STejun Heo 5948f2d5a0eeSTejun Heo /* 594992f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 595094cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 595111b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5952b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5953b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5954b4ac9384SLai Jiangshan * is on the same cpu. 5955f2d5a0eeSTejun Heo */ 5956da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5957403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5958db7bccf4STejun Heo 595924647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5960f2d5a0eeSTejun Heo 5961e22bee78STejun Heo /* 5962989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5963989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5964989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5965989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5966989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5967eb283428SLai Jiangshan * are served by workers tied to the pool. 5968e22bee78STejun Heo */ 5969bc35f7efSLai Jiangshan pool->nr_running = 0; 5970eb283428SLai Jiangshan 5971eb283428SLai Jiangshan /* 5972eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5973eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5974eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5975eb283428SLai Jiangshan */ 59760219a352STejun Heo kick_pool(pool); 5977989442d7SLai Jiangshan 5978a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5979989442d7SLai Jiangshan 5980793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5981793777bcSValentin Schneider unbind_worker(worker); 5982989442d7SLai Jiangshan 5983989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5984eb283428SLai Jiangshan } 5985db7bccf4STejun Heo } 5986db7bccf4STejun Heo 5987bd7c089eSTejun Heo /** 5988bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5989bd7c089eSTejun Heo * @pool: pool of interest 5990bd7c089eSTejun Heo * 5991a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5992bd7c089eSTejun Heo */ 5993bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5994bd7c089eSTejun Heo { 5995a9ab775bSTejun Heo struct worker *worker; 5996bd7c089eSTejun Heo 59971258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5998bd7c089eSTejun Heo 5999bd7c089eSTejun Heo /* 6000a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6001a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6002402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6003a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6004a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6005bd7c089eSTejun Heo */ 6006c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6007c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6008c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 60099546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6010c63a2e52SValentin Schneider } 6011a9ab775bSTejun Heo 6012a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6013f7c17d26SWanpeng Li 60143de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6015a9ab775bSTejun Heo 6016da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6017a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6018a9ab775bSTejun Heo 6019a9ab775bSTejun Heo /* 6020a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6021a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6022a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6023a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6024a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6025a9ab775bSTejun Heo * concurrency management. Note that when or whether 6026a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6027a9ab775bSTejun Heo * 6028c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6029a9ab775bSTejun Heo * tested without holding any lock in 60306d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6031a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6032a9ab775bSTejun Heo * management operations. 6033bd7c089eSTejun Heo */ 6034a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6035a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6036a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6037c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6038bd7c089eSTejun Heo } 6039a9ab775bSTejun Heo 6040a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6041bd7c089eSTejun Heo } 6042bd7c089eSTejun Heo 60437dbc725eSTejun Heo /** 60447dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 60457dbc725eSTejun Heo * @pool: unbound pool of interest 60467dbc725eSTejun Heo * @cpu: the CPU which is coming up 60477dbc725eSTejun Heo * 60487dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 60497dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 60507dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 60517dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 60527dbc725eSTejun Heo */ 60537dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 60547dbc725eSTejun Heo { 60557dbc725eSTejun Heo static cpumask_t cpumask; 60567dbc725eSTejun Heo struct worker *worker; 60577dbc725eSTejun Heo 60581258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 60597dbc725eSTejun Heo 60607dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 60617dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 60627dbc725eSTejun Heo return; 60637dbc725eSTejun Heo 60647dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 60657dbc725eSTejun Heo 60667dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6067da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6068d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 60697dbc725eSTejun Heo } 60707dbc725eSTejun Heo 60717ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 60721da177e4SLinus Torvalds { 60734ce62e9eSTejun Heo struct worker_pool *pool; 60741da177e4SLinus Torvalds 6075f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 60763ce63377STejun Heo if (pool->nr_workers) 60773ce63377STejun Heo continue; 6078051e1850SLai Jiangshan if (!create_worker(pool)) 60797ee681b2SThomas Gleixner return -ENOMEM; 60803af24433SOleg Nesterov } 60817ee681b2SThomas Gleixner return 0; 60827ee681b2SThomas Gleixner } 60831da177e4SLinus Torvalds 60847ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 60857ee681b2SThomas Gleixner { 60867ee681b2SThomas Gleixner struct worker_pool *pool; 60877ee681b2SThomas Gleixner struct workqueue_struct *wq; 60887ee681b2SThomas Gleixner int pi; 60897ee681b2SThomas Gleixner 609068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 60917dbc725eSTejun Heo 60927dbc725eSTejun Heo for_each_pool(pool, pi) { 60931258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 609494cf58bbSTejun Heo 6095f05b558dSLai Jiangshan if (pool->cpu == cpu) 609694cf58bbSTejun Heo rebind_workers(pool); 6097f05b558dSLai Jiangshan else if (pool->cpu < 0) 60987dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 609994cf58bbSTejun Heo 61001258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 610194cf58bbSTejun Heo } 61027dbc725eSTejun Heo 6103fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 61044cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 610584193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 610684193c07STejun Heo 610784193c07STejun Heo if (attrs) { 610884193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 61094cbfd3deSTejun Heo int tcpu; 61104cbfd3deSTejun Heo 611184193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6112fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 61135797b1c1STejun Heo 61145797b1c1STejun Heo mutex_lock(&wq->mutex); 61155797b1c1STejun Heo wq_update_node_max_active(wq, -1); 61165797b1c1STejun Heo mutex_unlock(&wq->mutex); 61174cbfd3deSTejun Heo } 61184cbfd3deSTejun Heo } 61194c16bd32STejun Heo 612068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 61217ee681b2SThomas Gleixner return 0; 612265758202STejun Heo } 612365758202STejun Heo 61247ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 612565758202STejun Heo { 61264c16bd32STejun Heo struct workqueue_struct *wq; 61278db25e78STejun Heo 61284c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6129e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6130e8b3f8dbSLai Jiangshan return -1; 6131e8b3f8dbSLai Jiangshan 6132e8b3f8dbSLai Jiangshan unbind_workers(cpu); 61334c16bd32STejun Heo 6134fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 61354c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 61364cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 613784193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 613884193c07STejun Heo 613984193c07STejun Heo if (attrs) { 614084193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 61414cbfd3deSTejun Heo int tcpu; 61424cbfd3deSTejun Heo 614384193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6144fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 61455797b1c1STejun Heo 61465797b1c1STejun Heo mutex_lock(&wq->mutex); 61475797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 61485797b1c1STejun Heo mutex_unlock(&wq->mutex); 61494cbfd3deSTejun Heo } 61504cbfd3deSTejun Heo } 61514c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 61524c16bd32STejun Heo 61537ee681b2SThomas Gleixner return 0; 615465758202STejun Heo } 615565758202STejun Heo 61562d3854a3SRusty Russell struct work_for_cpu { 6157ed48ece2STejun Heo struct work_struct work; 61582d3854a3SRusty Russell long (*fn)(void *); 61592d3854a3SRusty Russell void *arg; 61602d3854a3SRusty Russell long ret; 61612d3854a3SRusty Russell }; 61622d3854a3SRusty Russell 6163ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 61642d3854a3SRusty Russell { 6165ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6166ed48ece2STejun Heo 61672d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 61682d3854a3SRusty Russell } 61692d3854a3SRusty Russell 61702d3854a3SRusty Russell /** 6171265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 61722d3854a3SRusty Russell * @cpu: the cpu to run on 61732d3854a3SRusty Russell * @fn: the function to run 61742d3854a3SRusty Russell * @arg: the function arg 6175265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 61762d3854a3SRusty Russell * 617731ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 61786b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6179d185af30SYacine Belkadi * 6180d185af30SYacine Belkadi * Return: The value @fn returns. 61812d3854a3SRusty Russell */ 6182265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6183265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 61842d3854a3SRusty Russell { 6185ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 61862d3854a3SRusty Russell 6187265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6188ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 618912997d1aSBjorn Helgaas flush_work(&wfc.work); 6190440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 61912d3854a3SRusty Russell return wfc.ret; 61922d3854a3SRusty Russell } 6193265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 61940e8d6a93SThomas Gleixner 61950e8d6a93SThomas Gleixner /** 6196265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 61970e8d6a93SThomas Gleixner * @cpu: the cpu to run on 61980e8d6a93SThomas Gleixner * @fn: the function to run 61990e8d6a93SThomas Gleixner * @arg: the function argument 6200265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 62010e8d6a93SThomas Gleixner * 62020e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 62030e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 62040e8d6a93SThomas Gleixner * 62050e8d6a93SThomas Gleixner * Return: The value @fn returns. 62060e8d6a93SThomas Gleixner */ 6207265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6208265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 62090e8d6a93SThomas Gleixner { 62100e8d6a93SThomas Gleixner long ret = -ENODEV; 62110e8d6a93SThomas Gleixner 6212ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 62130e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6214265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6215ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 62160e8d6a93SThomas Gleixner return ret; 62170e8d6a93SThomas Gleixner } 6218265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 62192d3854a3SRusty Russell #endif /* CONFIG_SMP */ 62202d3854a3SRusty Russell 6221a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6222e7577c50SRusty Russell 6223a0a1a5fdSTejun Heo /** 6224a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6225a0a1a5fdSTejun Heo * 622658a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6227f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6228706026c2STejun Heo * pool->worklist. 6229a0a1a5fdSTejun Heo * 6230a0a1a5fdSTejun Heo * CONTEXT: 6231a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6232a0a1a5fdSTejun Heo */ 6233a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6234a0a1a5fdSTejun Heo { 623524b8a847STejun Heo struct workqueue_struct *wq; 6236a0a1a5fdSTejun Heo 623768e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6238a0a1a5fdSTejun Heo 62396183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6240a0a1a5fdSTejun Heo workqueue_freezing = true; 6241a0a1a5fdSTejun Heo 624224b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6243a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6244a045a272STejun Heo wq_adjust_max_active(wq); 6245a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6246a1056305STejun Heo } 62475bcab335STejun Heo 624868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6249a0a1a5fdSTejun Heo } 6250a0a1a5fdSTejun Heo 6251a0a1a5fdSTejun Heo /** 625258a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6253a0a1a5fdSTejun Heo * 6254a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6255a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6256a0a1a5fdSTejun Heo * 6257a0a1a5fdSTejun Heo * CONTEXT: 625868e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6259a0a1a5fdSTejun Heo * 6260d185af30SYacine Belkadi * Return: 626158a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 626258a69cb4STejun Heo * is complete. 6263a0a1a5fdSTejun Heo */ 6264a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6265a0a1a5fdSTejun Heo { 6266a0a1a5fdSTejun Heo bool busy = false; 626724b8a847STejun Heo struct workqueue_struct *wq; 626824b8a847STejun Heo struct pool_workqueue *pwq; 6269a0a1a5fdSTejun Heo 627068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6271a0a1a5fdSTejun Heo 62726183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6273a0a1a5fdSTejun Heo 627424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 627524b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 627624b8a847STejun Heo continue; 6277a0a1a5fdSTejun Heo /* 6278a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6279a0a1a5fdSTejun Heo * to peek without lock. 6280a0a1a5fdSTejun Heo */ 628124acfb71SThomas Gleixner rcu_read_lock(); 628224b8a847STejun Heo for_each_pwq(pwq, wq) { 62836183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6284112202d9STejun Heo if (pwq->nr_active) { 6285a0a1a5fdSTejun Heo busy = true; 628624acfb71SThomas Gleixner rcu_read_unlock(); 6287a0a1a5fdSTejun Heo goto out_unlock; 6288a0a1a5fdSTejun Heo } 6289a0a1a5fdSTejun Heo } 629024acfb71SThomas Gleixner rcu_read_unlock(); 6291a0a1a5fdSTejun Heo } 6292a0a1a5fdSTejun Heo out_unlock: 629368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6294a0a1a5fdSTejun Heo return busy; 6295a0a1a5fdSTejun Heo } 6296a0a1a5fdSTejun Heo 6297a0a1a5fdSTejun Heo /** 6298a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6299a0a1a5fdSTejun Heo * 6300a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6301706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6302a0a1a5fdSTejun Heo * 6303a0a1a5fdSTejun Heo * CONTEXT: 6304a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6305a0a1a5fdSTejun Heo */ 6306a0a1a5fdSTejun Heo void thaw_workqueues(void) 6307a0a1a5fdSTejun Heo { 630824b8a847STejun Heo struct workqueue_struct *wq; 6309a0a1a5fdSTejun Heo 631068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6311a0a1a5fdSTejun Heo 6312a0a1a5fdSTejun Heo if (!workqueue_freezing) 6313a0a1a5fdSTejun Heo goto out_unlock; 6314a0a1a5fdSTejun Heo 631574b414eaSLai Jiangshan workqueue_freezing = false; 631624b8a847STejun Heo 631724b8a847STejun Heo /* restore max_active and repopulate worklist */ 631824b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6319a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6320a045a272STejun Heo wq_adjust_max_active(wq); 6321a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 632224b8a847STejun Heo } 632324b8a847STejun Heo 6324a0a1a5fdSTejun Heo out_unlock: 632568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6326a0a1a5fdSTejun Heo } 6327a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6328a0a1a5fdSTejun Heo 632999c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6330042f7df1SLai Jiangshan { 6331042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6332042f7df1SLai Jiangshan int ret = 0; 6333042f7df1SLai Jiangshan struct workqueue_struct *wq; 6334042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6335042f7df1SLai Jiangshan 6336042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6337042f7df1SLai Jiangshan 6338042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 6339042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 6340042f7df1SLai Jiangshan continue; 6341ca10d851SWaiman Long 6342042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 6343ca10d851SWaiman Long if (!list_empty(&wq->pwqs)) { 6344ca10d851SWaiman Long if (wq->flags & __WQ_ORDERED_EXPLICIT) 6345042f7df1SLai Jiangshan continue; 6346ca10d851SWaiman Long wq->flags &= ~__WQ_ORDERED; 6347ca10d851SWaiman Long } 6348042f7df1SLai Jiangshan 634999c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 635084193c07STejun Heo if (IS_ERR(ctx)) { 635184193c07STejun Heo ret = PTR_ERR(ctx); 6352042f7df1SLai Jiangshan break; 6353042f7df1SLai Jiangshan } 6354042f7df1SLai Jiangshan 6355042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6356042f7df1SLai Jiangshan } 6357042f7df1SLai Jiangshan 6358042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6359042f7df1SLai Jiangshan if (!ret) 6360042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6361042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6362042f7df1SLai Jiangshan } 6363042f7df1SLai Jiangshan 636499c621efSLai Jiangshan if (!ret) { 636599c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 636699c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 636799c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 636899c621efSLai Jiangshan } 6369042f7df1SLai Jiangshan return ret; 6370042f7df1SLai Jiangshan } 6371042f7df1SLai Jiangshan 6372042f7df1SLai Jiangshan /** 6373fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6374fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6375fe28f631SWaiman Long * 6376fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6377fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6378fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6379fe28f631SWaiman Long */ 6380fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6381fe28f631SWaiman Long { 6382fe28f631SWaiman Long cpumask_var_t cpumask; 6383fe28f631SWaiman Long int ret = 0; 6384fe28f631SWaiman Long 6385fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6386fe28f631SWaiman Long return -ENOMEM; 6387fe28f631SWaiman Long 6388fe28f631SWaiman Long lockdep_assert_cpus_held(); 6389fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6390fe28f631SWaiman Long 6391fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6392fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6393fe28f631SWaiman Long 6394fe28f631SWaiman Long /* 6395fe28f631SWaiman Long * If the operation fails, it will fall back to 6396fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6397fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6398fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6399fe28f631SWaiman Long */ 6400fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6401fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6402fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6403fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6404fe28f631SWaiman Long 6405fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6406fe28f631SWaiman Long free_cpumask_var(cpumask); 6407fe28f631SWaiman Long return ret; 6408fe28f631SWaiman Long } 6409fe28f631SWaiman Long 641063c5484eSTejun Heo static int parse_affn_scope(const char *val) 641163c5484eSTejun Heo { 641263c5484eSTejun Heo int i; 641363c5484eSTejun Heo 641463c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 641563c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 641663c5484eSTejun Heo return i; 641763c5484eSTejun Heo } 641863c5484eSTejun Heo return -EINVAL; 641963c5484eSTejun Heo } 642063c5484eSTejun Heo 642163c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 642263c5484eSTejun Heo { 6423523a301eSTejun Heo struct workqueue_struct *wq; 6424523a301eSTejun Heo int affn, cpu; 642563c5484eSTejun Heo 642663c5484eSTejun Heo affn = parse_affn_scope(val); 642763c5484eSTejun Heo if (affn < 0) 642863c5484eSTejun Heo return affn; 6429523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6430523a301eSTejun Heo return -EINVAL; 6431523a301eSTejun Heo 6432523a301eSTejun Heo cpus_read_lock(); 6433523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 643463c5484eSTejun Heo 643563c5484eSTejun Heo wq_affn_dfl = affn; 6436523a301eSTejun Heo 6437523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6438523a301eSTejun Heo for_each_online_cpu(cpu) { 6439523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6440523a301eSTejun Heo } 6441523a301eSTejun Heo } 6442523a301eSTejun Heo 6443523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6444523a301eSTejun Heo cpus_read_unlock(); 6445523a301eSTejun Heo 644663c5484eSTejun Heo return 0; 644763c5484eSTejun Heo } 644863c5484eSTejun Heo 644963c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 645063c5484eSTejun Heo { 645163c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 645263c5484eSTejun Heo } 645363c5484eSTejun Heo 645463c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 645563c5484eSTejun Heo .set = wq_affn_dfl_set, 645663c5484eSTejun Heo .get = wq_affn_dfl_get, 645763c5484eSTejun Heo }; 645863c5484eSTejun Heo 645963c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 646063c5484eSTejun Heo 64616ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 64626ba94429SFrederic Weisbecker /* 64636ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 64646ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 64656ba94429SFrederic Weisbecker * following attributes. 64666ba94429SFrederic Weisbecker * 64676ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 64686ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 64696ba94429SFrederic Weisbecker * 64706ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 64716ba94429SFrederic Weisbecker * 64726ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 64736ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 647463c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 64758639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 64766ba94429SFrederic Weisbecker */ 64776ba94429SFrederic Weisbecker struct wq_device { 64786ba94429SFrederic Weisbecker struct workqueue_struct *wq; 64796ba94429SFrederic Weisbecker struct device dev; 64806ba94429SFrederic Weisbecker }; 64816ba94429SFrederic Weisbecker 64826ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 64836ba94429SFrederic Weisbecker { 64846ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 64856ba94429SFrederic Weisbecker 64866ba94429SFrederic Weisbecker return wq_dev->wq; 64876ba94429SFrederic Weisbecker } 64886ba94429SFrederic Weisbecker 64896ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 64906ba94429SFrederic Weisbecker char *buf) 64916ba94429SFrederic Weisbecker { 64926ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 64936ba94429SFrederic Weisbecker 64946ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 64956ba94429SFrederic Weisbecker } 64966ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 64976ba94429SFrederic Weisbecker 64986ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 64996ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 65006ba94429SFrederic Weisbecker { 65016ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65026ba94429SFrederic Weisbecker 65036ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 65046ba94429SFrederic Weisbecker } 65056ba94429SFrederic Weisbecker 65066ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 65076ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 65086ba94429SFrederic Weisbecker size_t count) 65096ba94429SFrederic Weisbecker { 65106ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65116ba94429SFrederic Weisbecker int val; 65126ba94429SFrederic Weisbecker 65136ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 65146ba94429SFrederic Weisbecker return -EINVAL; 65156ba94429SFrederic Weisbecker 65166ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 65176ba94429SFrederic Weisbecker return count; 65186ba94429SFrederic Weisbecker } 65196ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 65206ba94429SFrederic Weisbecker 65216ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 65226ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 65236ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 65246ba94429SFrederic Weisbecker NULL, 65256ba94429SFrederic Weisbecker }; 65266ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 65276ba94429SFrederic Weisbecker 652849277a5bSWaiman Long static void apply_wqattrs_lock(void) 652949277a5bSWaiman Long { 653049277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 653149277a5bSWaiman Long cpus_read_lock(); 653249277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 653349277a5bSWaiman Long } 653449277a5bSWaiman Long 653549277a5bSWaiman Long static void apply_wqattrs_unlock(void) 653649277a5bSWaiman Long { 653749277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 653849277a5bSWaiman Long cpus_read_unlock(); 653949277a5bSWaiman Long } 654049277a5bSWaiman Long 65416ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 65426ba94429SFrederic Weisbecker char *buf) 65436ba94429SFrederic Weisbecker { 65446ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65456ba94429SFrederic Weisbecker int written; 65466ba94429SFrederic Weisbecker 65476ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 65486ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 65496ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 65506ba94429SFrederic Weisbecker 65516ba94429SFrederic Weisbecker return written; 65526ba94429SFrederic Weisbecker } 65536ba94429SFrederic Weisbecker 65546ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 65556ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 65566ba94429SFrederic Weisbecker { 65576ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 65586ba94429SFrederic Weisbecker 6559899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6560899a94feSLai Jiangshan 6561be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 65626ba94429SFrederic Weisbecker if (!attrs) 65636ba94429SFrederic Weisbecker return NULL; 65646ba94429SFrederic Weisbecker 65656ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 65666ba94429SFrederic Weisbecker return attrs; 65676ba94429SFrederic Weisbecker } 65686ba94429SFrederic Weisbecker 65696ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 65706ba94429SFrederic Weisbecker const char *buf, size_t count) 65716ba94429SFrederic Weisbecker { 65726ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65736ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6574d4d3e257SLai Jiangshan int ret = -ENOMEM; 6575d4d3e257SLai Jiangshan 6576d4d3e257SLai Jiangshan apply_wqattrs_lock(); 65776ba94429SFrederic Weisbecker 65786ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 65796ba94429SFrederic Weisbecker if (!attrs) 6580d4d3e257SLai Jiangshan goto out_unlock; 65816ba94429SFrederic Weisbecker 65826ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 65836ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6584d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 65856ba94429SFrederic Weisbecker else 65866ba94429SFrederic Weisbecker ret = -EINVAL; 65876ba94429SFrederic Weisbecker 6588d4d3e257SLai Jiangshan out_unlock: 6589d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 65906ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 65916ba94429SFrederic Weisbecker return ret ?: count; 65926ba94429SFrederic Weisbecker } 65936ba94429SFrederic Weisbecker 65946ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 65956ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 65966ba94429SFrederic Weisbecker { 65976ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65986ba94429SFrederic Weisbecker int written; 65996ba94429SFrederic Weisbecker 66006ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 66016ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 66026ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 66036ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 66046ba94429SFrederic Weisbecker return written; 66056ba94429SFrederic Weisbecker } 66066ba94429SFrederic Weisbecker 66076ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 66086ba94429SFrederic Weisbecker struct device_attribute *attr, 66096ba94429SFrederic Weisbecker const char *buf, size_t count) 66106ba94429SFrederic Weisbecker { 66116ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 66126ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6613d4d3e257SLai Jiangshan int ret = -ENOMEM; 6614d4d3e257SLai Jiangshan 6615d4d3e257SLai Jiangshan apply_wqattrs_lock(); 66166ba94429SFrederic Weisbecker 66176ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 66186ba94429SFrederic Weisbecker if (!attrs) 6619d4d3e257SLai Jiangshan goto out_unlock; 66206ba94429SFrederic Weisbecker 66216ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 66226ba94429SFrederic Weisbecker if (!ret) 6623d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 66246ba94429SFrederic Weisbecker 6625d4d3e257SLai Jiangshan out_unlock: 6626d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 66276ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 66286ba94429SFrederic Weisbecker return ret ?: count; 66296ba94429SFrederic Weisbecker } 66306ba94429SFrederic Weisbecker 663163c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 663263c5484eSTejun Heo struct device_attribute *attr, char *buf) 663363c5484eSTejun Heo { 663463c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 663563c5484eSTejun Heo int written; 663663c5484eSTejun Heo 663763c5484eSTejun Heo mutex_lock(&wq->mutex); 6638523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6639523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6640523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6641523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6642523a301eSTejun Heo else 664363c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 664463c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 664563c5484eSTejun Heo mutex_unlock(&wq->mutex); 664663c5484eSTejun Heo 664763c5484eSTejun Heo return written; 664863c5484eSTejun Heo } 664963c5484eSTejun Heo 665063c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 665163c5484eSTejun Heo struct device_attribute *attr, 665263c5484eSTejun Heo const char *buf, size_t count) 665363c5484eSTejun Heo { 665463c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 665563c5484eSTejun Heo struct workqueue_attrs *attrs; 665663c5484eSTejun Heo int affn, ret = -ENOMEM; 665763c5484eSTejun Heo 665863c5484eSTejun Heo affn = parse_affn_scope(buf); 665963c5484eSTejun Heo if (affn < 0) 666063c5484eSTejun Heo return affn; 666163c5484eSTejun Heo 666263c5484eSTejun Heo apply_wqattrs_lock(); 666363c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 666463c5484eSTejun Heo if (attrs) { 666563c5484eSTejun Heo attrs->affn_scope = affn; 666663c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 666763c5484eSTejun Heo } 666863c5484eSTejun Heo apply_wqattrs_unlock(); 666963c5484eSTejun Heo free_workqueue_attrs(attrs); 667063c5484eSTejun Heo return ret ?: count; 667163c5484eSTejun Heo } 667263c5484eSTejun Heo 66738639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 66748639ecebSTejun Heo struct device_attribute *attr, char *buf) 66758639ecebSTejun Heo { 66768639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 66778639ecebSTejun Heo 66788639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 66798639ecebSTejun Heo wq->unbound_attrs->affn_strict); 66808639ecebSTejun Heo } 66818639ecebSTejun Heo 66828639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 66838639ecebSTejun Heo struct device_attribute *attr, 66848639ecebSTejun Heo const char *buf, size_t count) 66858639ecebSTejun Heo { 66868639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 66878639ecebSTejun Heo struct workqueue_attrs *attrs; 66888639ecebSTejun Heo int v, ret = -ENOMEM; 66898639ecebSTejun Heo 66908639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 66918639ecebSTejun Heo return -EINVAL; 66928639ecebSTejun Heo 66938639ecebSTejun Heo apply_wqattrs_lock(); 66948639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 66958639ecebSTejun Heo if (attrs) { 66968639ecebSTejun Heo attrs->affn_strict = (bool)v; 66978639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 66988639ecebSTejun Heo } 66998639ecebSTejun Heo apply_wqattrs_unlock(); 67008639ecebSTejun Heo free_workqueue_attrs(attrs); 67018639ecebSTejun Heo return ret ?: count; 67028639ecebSTejun Heo } 67038639ecebSTejun Heo 67046ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 67056ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 67066ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 670763c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 67088639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 67096ba94429SFrederic Weisbecker __ATTR_NULL, 67106ba94429SFrederic Weisbecker }; 67116ba94429SFrederic Weisbecker 6712d412ace1SRicardo B. Marliere static const struct bus_type wq_subsys = { 67136ba94429SFrederic Weisbecker .name = "workqueue", 67146ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 67156ba94429SFrederic Weisbecker }; 67166ba94429SFrederic Weisbecker 671749277a5bSWaiman Long /** 671849277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 671949277a5bSWaiman Long * @cpumask: the cpumask to set 672049277a5bSWaiman Long * 672149277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 672249277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 672349277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 672449277a5bSWaiman Long * 672549277a5bSWaiman Long * Return: 0 - Success 672649277a5bSWaiman Long * -EINVAL - Invalid @cpumask 672749277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 672849277a5bSWaiman Long */ 672949277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 673049277a5bSWaiman Long { 673149277a5bSWaiman Long int ret = -EINVAL; 673249277a5bSWaiman Long 673349277a5bSWaiman Long /* 673449277a5bSWaiman Long * Not excluding isolated cpus on purpose. 673549277a5bSWaiman Long * If the user wishes to include them, we allow that. 673649277a5bSWaiman Long */ 673749277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 673849277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 673949277a5bSWaiman Long apply_wqattrs_lock(); 674049277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 674149277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 674249277a5bSWaiman Long ret = 0; 674349277a5bSWaiman Long goto out_unlock; 674449277a5bSWaiman Long } 674549277a5bSWaiman Long 674649277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 674749277a5bSWaiman Long 674849277a5bSWaiman Long out_unlock: 674949277a5bSWaiman Long apply_wqattrs_unlock(); 675049277a5bSWaiman Long } 675149277a5bSWaiman Long 675249277a5bSWaiman Long return ret; 675349277a5bSWaiman Long } 675449277a5bSWaiman Long 6755fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 6756fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 6757b05a7928SFrederic Weisbecker { 6758b05a7928SFrederic Weisbecker int written; 6759b05a7928SFrederic Weisbecker 6760042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6761fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 6762042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6763b05a7928SFrederic Weisbecker 6764b05a7928SFrederic Weisbecker return written; 6765b05a7928SFrederic Weisbecker } 6766b05a7928SFrederic Weisbecker 6767fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 6768fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6769fe28f631SWaiman Long { 6770fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 6771fe28f631SWaiman Long } 6772fe28f631SWaiman Long 6773fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 6774fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6775fe28f631SWaiman Long { 6776fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 6777fe28f631SWaiman Long } 6778fe28f631SWaiman Long 6779fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 6780fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6781fe28f631SWaiman Long { 6782fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 6783fe28f631SWaiman Long } 6784fe28f631SWaiman Long 6785042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6786042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6787042f7df1SLai Jiangshan { 6788042f7df1SLai Jiangshan cpumask_var_t cpumask; 6789042f7df1SLai Jiangshan int ret; 6790042f7df1SLai Jiangshan 6791042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6792042f7df1SLai Jiangshan return -ENOMEM; 6793042f7df1SLai Jiangshan 6794042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 6795042f7df1SLai Jiangshan if (!ret) 6796042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 6797042f7df1SLai Jiangshan 6798042f7df1SLai Jiangshan free_cpumask_var(cpumask); 6799042f7df1SLai Jiangshan return ret ? ret : count; 6800042f7df1SLai Jiangshan } 6801042f7df1SLai Jiangshan 6802fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 6803042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 6804fe28f631SWaiman Long wq_unbound_cpumask_store), 6805fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 6806fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 6807fe28f631SWaiman Long __ATTR_NULL, 6808fe28f631SWaiman Long }; 6809b05a7928SFrederic Weisbecker 68106ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 68116ba94429SFrederic Weisbecker { 6812686f6697SGreg Kroah-Hartman struct device *dev_root; 6813b05a7928SFrederic Weisbecker int err; 6814b05a7928SFrederic Weisbecker 6815b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 6816b05a7928SFrederic Weisbecker if (err) 6817b05a7928SFrederic Weisbecker return err; 6818b05a7928SFrederic Weisbecker 6819686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 6820686f6697SGreg Kroah-Hartman if (dev_root) { 6821fe28f631SWaiman Long struct device_attribute *attr; 6822fe28f631SWaiman Long 6823fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 6824fe28f631SWaiman Long err = device_create_file(dev_root, attr); 6825fe28f631SWaiman Long if (err) 6826fe28f631SWaiman Long break; 6827fe28f631SWaiman Long } 6828686f6697SGreg Kroah-Hartman put_device(dev_root); 6829686f6697SGreg Kroah-Hartman } 6830686f6697SGreg Kroah-Hartman return err; 68316ba94429SFrederic Weisbecker } 68326ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 68336ba94429SFrederic Weisbecker 68346ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 68356ba94429SFrederic Weisbecker { 68366ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 68376ba94429SFrederic Weisbecker 68386ba94429SFrederic Weisbecker kfree(wq_dev); 68396ba94429SFrederic Weisbecker } 68406ba94429SFrederic Weisbecker 68416ba94429SFrederic Weisbecker /** 68426ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 68436ba94429SFrederic Weisbecker * @wq: the workqueue to register 68446ba94429SFrederic Weisbecker * 68456ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 68466ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 68476ba94429SFrederic Weisbecker * which is the preferred method. 68486ba94429SFrederic Weisbecker * 68496ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 68506ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 68516ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 68526ba94429SFrederic Weisbecker * attributes. 68536ba94429SFrederic Weisbecker * 68546ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 68556ba94429SFrederic Weisbecker */ 68566ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 68576ba94429SFrederic Weisbecker { 68586ba94429SFrederic Weisbecker struct wq_device *wq_dev; 68596ba94429SFrederic Weisbecker int ret; 68606ba94429SFrederic Weisbecker 68616ba94429SFrederic Weisbecker /* 6862402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 68636ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 68646ba94429SFrederic Weisbecker * workqueues. 68656ba94429SFrederic Weisbecker */ 68660a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 68676ba94429SFrederic Weisbecker return -EINVAL; 68686ba94429SFrederic Weisbecker 68696ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 68706ba94429SFrederic Weisbecker if (!wq_dev) 68716ba94429SFrederic Weisbecker return -ENOMEM; 68726ba94429SFrederic Weisbecker 68736ba94429SFrederic Weisbecker wq_dev->wq = wq; 68746ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 68756ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 687623217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 68776ba94429SFrederic Weisbecker 68786ba94429SFrederic Weisbecker /* 68796ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 68806ba94429SFrederic Weisbecker * everything is ready. 68816ba94429SFrederic Weisbecker */ 68826ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 68836ba94429SFrederic Weisbecker 68846ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 68856ba94429SFrederic Weisbecker if (ret) { 6886537f4146SArvind Yadav put_device(&wq_dev->dev); 68876ba94429SFrederic Weisbecker wq->wq_dev = NULL; 68886ba94429SFrederic Weisbecker return ret; 68896ba94429SFrederic Weisbecker } 68906ba94429SFrederic Weisbecker 68916ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 68926ba94429SFrederic Weisbecker struct device_attribute *attr; 68936ba94429SFrederic Weisbecker 68946ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 68956ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 68966ba94429SFrederic Weisbecker if (ret) { 68976ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 68986ba94429SFrederic Weisbecker wq->wq_dev = NULL; 68996ba94429SFrederic Weisbecker return ret; 69006ba94429SFrederic Weisbecker } 69016ba94429SFrederic Weisbecker } 69026ba94429SFrederic Weisbecker } 69036ba94429SFrederic Weisbecker 69046ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 69056ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 69066ba94429SFrederic Weisbecker return 0; 69076ba94429SFrederic Weisbecker } 69086ba94429SFrederic Weisbecker 69096ba94429SFrederic Weisbecker /** 69106ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 69116ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 69126ba94429SFrederic Weisbecker * 69136ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 69146ba94429SFrederic Weisbecker */ 69156ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 69166ba94429SFrederic Weisbecker { 69176ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 69186ba94429SFrederic Weisbecker 69196ba94429SFrederic Weisbecker if (!wq->wq_dev) 69206ba94429SFrederic Weisbecker return; 69216ba94429SFrederic Weisbecker 69226ba94429SFrederic Weisbecker wq->wq_dev = NULL; 69236ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 69246ba94429SFrederic Weisbecker } 69256ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 69266ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 69276ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 69286ba94429SFrederic Weisbecker 692982607adcSTejun Heo /* 693082607adcSTejun Heo * Workqueue watchdog. 693182607adcSTejun Heo * 693282607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 693382607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 693482607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 693582607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 693682607adcSTejun Heo * largely opaque. 693782607adcSTejun Heo * 693882607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 693982607adcSTejun Heo * state if some pools failed to make forward progress for a while where 694082607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 694182607adcSTejun Heo * 694282607adcSTejun Heo * This mechanism is controlled through the kernel parameter 694382607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 694482607adcSTejun Heo * corresponding sysfs parameter file. 694582607adcSTejun Heo */ 694682607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 694782607adcSTejun Heo 694882607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 69495cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 695082607adcSTejun Heo 695182607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 695282607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 695382607adcSTejun Heo 6954cd2440d6SPetr Mladek /* 6955cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6956cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6957cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6958cd2440d6SPetr Mladek * in all other situations. 6959cd2440d6SPetr Mladek */ 6960cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6961cd2440d6SPetr Mladek { 6962cd2440d6SPetr Mladek struct worker *worker; 6963cd2440d6SPetr Mladek unsigned long flags; 6964cd2440d6SPetr Mladek int bkt; 6965cd2440d6SPetr Mladek 6966cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6967cd2440d6SPetr Mladek 6968cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6969cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6970cd2440d6SPetr Mladek /* 6971cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6972cd2440d6SPetr Mladek * drivers that queue work while holding locks 6973cd2440d6SPetr Mladek * also taken in their write paths. 6974cd2440d6SPetr Mladek */ 6975cd2440d6SPetr Mladek printk_deferred_enter(); 6976cd2440d6SPetr Mladek 6977cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6978cd2440d6SPetr Mladek sched_show_task(worker->task); 6979cd2440d6SPetr Mladek 6980cd2440d6SPetr Mladek printk_deferred_exit(); 6981cd2440d6SPetr Mladek } 6982cd2440d6SPetr Mladek } 6983cd2440d6SPetr Mladek 6984cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6985cd2440d6SPetr Mladek } 6986cd2440d6SPetr Mladek 6987cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6988cd2440d6SPetr Mladek { 6989cd2440d6SPetr Mladek struct worker_pool *pool; 6990cd2440d6SPetr Mladek int pi; 6991cd2440d6SPetr Mladek 6992cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6993cd2440d6SPetr Mladek 6994cd2440d6SPetr Mladek rcu_read_lock(); 6995cd2440d6SPetr Mladek 6996cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6997cd2440d6SPetr Mladek if (pool->cpu_stall) 6998cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6999cd2440d6SPetr Mladek 7000cd2440d6SPetr Mladek } 7001cd2440d6SPetr Mladek 7002cd2440d6SPetr Mladek rcu_read_unlock(); 7003cd2440d6SPetr Mladek } 7004cd2440d6SPetr Mladek 700582607adcSTejun Heo static void wq_watchdog_reset_touched(void) 700682607adcSTejun Heo { 700782607adcSTejun Heo int cpu; 700882607adcSTejun Heo 700982607adcSTejun Heo wq_watchdog_touched = jiffies; 701082607adcSTejun Heo for_each_possible_cpu(cpu) 701182607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 701282607adcSTejun Heo } 701382607adcSTejun Heo 70145cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 701582607adcSTejun Heo { 701682607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 701782607adcSTejun Heo bool lockup_detected = false; 7018cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7019940d71c6SSergey Senozhatsky unsigned long now = jiffies; 702082607adcSTejun Heo struct worker_pool *pool; 702182607adcSTejun Heo int pi; 702282607adcSTejun Heo 702382607adcSTejun Heo if (!thresh) 702482607adcSTejun Heo return; 702582607adcSTejun Heo 702682607adcSTejun Heo rcu_read_lock(); 702782607adcSTejun Heo 702882607adcSTejun Heo for_each_pool(pool, pi) { 702982607adcSTejun Heo unsigned long pool_ts, touched, ts; 703082607adcSTejun Heo 7031cd2440d6SPetr Mladek pool->cpu_stall = false; 703282607adcSTejun Heo if (list_empty(&pool->worklist)) 703382607adcSTejun Heo continue; 703482607adcSTejun Heo 7035940d71c6SSergey Senozhatsky /* 7036940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7037940d71c6SSergey Senozhatsky * the watchdog like a stall. 7038940d71c6SSergey Senozhatsky */ 7039940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7040940d71c6SSergey Senozhatsky 704182607adcSTejun Heo /* get the latest of pool and touched timestamps */ 704289e28ce6SWang Qing if (pool->cpu >= 0) 704389e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 704489e28ce6SWang Qing else 704582607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 704689e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 704782607adcSTejun Heo 704882607adcSTejun Heo if (time_after(pool_ts, touched)) 704982607adcSTejun Heo ts = pool_ts; 705082607adcSTejun Heo else 705182607adcSTejun Heo ts = touched; 705282607adcSTejun Heo 705382607adcSTejun Heo /* did we stall? */ 7054940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 705582607adcSTejun Heo lockup_detected = true; 7056cd2440d6SPetr Mladek if (pool->cpu >= 0) { 7057cd2440d6SPetr Mladek pool->cpu_stall = true; 7058cd2440d6SPetr Mladek cpu_pool_stall = true; 7059cd2440d6SPetr Mladek } 706082607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 706182607adcSTejun Heo pr_cont_pool_info(pool); 706282607adcSTejun Heo pr_cont(" stuck for %us!\n", 7063940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 706482607adcSTejun Heo } 7065cd2440d6SPetr Mladek 7066cd2440d6SPetr Mladek 706782607adcSTejun Heo } 706882607adcSTejun Heo 706982607adcSTejun Heo rcu_read_unlock(); 707082607adcSTejun Heo 707182607adcSTejun Heo if (lockup_detected) 707255df0933SImran Khan show_all_workqueues(); 707382607adcSTejun Heo 7074cd2440d6SPetr Mladek if (cpu_pool_stall) 7075cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7076cd2440d6SPetr Mladek 707782607adcSTejun Heo wq_watchdog_reset_touched(); 707882607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 707982607adcSTejun Heo } 708082607adcSTejun Heo 7081cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 708282607adcSTejun Heo { 708382607adcSTejun Heo if (cpu >= 0) 708482607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 708589e28ce6SWang Qing 708682607adcSTejun Heo wq_watchdog_touched = jiffies; 708782607adcSTejun Heo } 708882607adcSTejun Heo 708982607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 709082607adcSTejun Heo { 709182607adcSTejun Heo wq_watchdog_thresh = 0; 709282607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 709382607adcSTejun Heo 709482607adcSTejun Heo if (thresh) { 709582607adcSTejun Heo wq_watchdog_thresh = thresh; 709682607adcSTejun Heo wq_watchdog_reset_touched(); 709782607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 709882607adcSTejun Heo } 709982607adcSTejun Heo } 710082607adcSTejun Heo 710182607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 710282607adcSTejun Heo const struct kernel_param *kp) 710382607adcSTejun Heo { 710482607adcSTejun Heo unsigned long thresh; 710582607adcSTejun Heo int ret; 710682607adcSTejun Heo 710782607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 710882607adcSTejun Heo if (ret) 710982607adcSTejun Heo return ret; 711082607adcSTejun Heo 711182607adcSTejun Heo if (system_wq) 711282607adcSTejun Heo wq_watchdog_set_thresh(thresh); 711382607adcSTejun Heo else 711482607adcSTejun Heo wq_watchdog_thresh = thresh; 711582607adcSTejun Heo 711682607adcSTejun Heo return 0; 711782607adcSTejun Heo } 711882607adcSTejun Heo 711982607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 712082607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 712182607adcSTejun Heo .get = param_get_ulong, 712282607adcSTejun Heo }; 712382607adcSTejun Heo 712482607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 712582607adcSTejun Heo 0644); 712682607adcSTejun Heo 712782607adcSTejun Heo static void wq_watchdog_init(void) 712882607adcSTejun Heo { 71295cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 713082607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 713182607adcSTejun Heo } 713282607adcSTejun Heo 713382607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 713482607adcSTejun Heo 713582607adcSTejun Heo static inline void wq_watchdog_init(void) { } 713682607adcSTejun Heo 713782607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 713882607adcSTejun Heo 71394a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 71404a6c5607STejun Heo { 71414a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 71424a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 71434a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 71444a6c5607STejun Heo return; 71454a6c5607STejun Heo } 71464a6c5607STejun Heo 71474a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 71484a6c5607STejun Heo } 71494a6c5607STejun Heo 7150*2fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 7151*2fcdb1b4STejun Heo { 7152*2fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 7153*2fcdb1b4STejun Heo pool->cpu = cpu; 7154*2fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 7155*2fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 7156*2fcdb1b4STejun Heo pool->attrs->nice = nice; 7157*2fcdb1b4STejun Heo pool->attrs->affn_strict = true; 7158*2fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 7159*2fcdb1b4STejun Heo 7160*2fcdb1b4STejun Heo /* alloc pool ID */ 7161*2fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 7162*2fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 7163*2fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 7164*2fcdb1b4STejun Heo } 7165*2fcdb1b4STejun Heo 71663347fa09STejun Heo /** 71673347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 71683347fa09STejun Heo * 71692930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 71702930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 71712930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 71722930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 71732930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 71742930155bSTejun Heo * before early initcalls. 71753347fa09STejun Heo */ 71762333e829SYu Chen void __init workqueue_init_early(void) 71771da177e4SLinus Torvalds { 717884193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 71797a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 71807a4e344cSTejun Heo int i, cpu; 7181c34056a3STejun Heo 718210cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7183e904e6c2STejun Heo 7184b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7185fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7186fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7187b05a7928SFrederic Weisbecker 71884a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 71894a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 71904a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7191ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 71924a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7193ace3c549Stiozhang 7194fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 71958b03ae3cSTejun Heo 71968b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7197e22bee78STejun Heo 71982930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 71992930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 72002930155bSTejun Heo 72017bd20b6bSMarcelo Tosatti /* 72027bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 72037bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 72047bd20b6bSMarcelo Tosatti */ 72057bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 72067bd20b6bSMarcelo Tosatti wq_power_efficient = true; 72077bd20b6bSMarcelo Tosatti 720884193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 720984193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 721084193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 721184193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 721284193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 721384193c07STejun Heo 721484193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 721584193c07STejun Heo 721684193c07STejun Heo pt->nr_pods = 1; 721784193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 721884193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 721984193c07STejun Heo pt->cpu_pod[0] = 0; 722084193c07STejun Heo 7221f02ae73aSTejun Heo /* initialize CPU pools */ 722224647570STejun Heo for_each_possible_cpu(cpu) { 7223ebf44d16STejun Heo struct worker_pool *pool; 7224e22bee78STejun Heo 72254ce62e9eSTejun Heo i = 0; 7226*2fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 7227*2fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 7228e5cba24eSHitoshi Mitake } 722962d3c543SAlan Stern 72308a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 723129c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 723229c91e99STejun Heo struct workqueue_attrs *attrs; 723329c91e99STejun Heo 7234be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 723529c91e99STejun Heo attrs->nice = std_nice[i]; 723629c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 72378a2b7538STejun Heo 72388a2b7538STejun Heo /* 72398a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 72408a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 72418a2b7538STejun Heo */ 7242be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 72438a2b7538STejun Heo attrs->nice = std_nice[i]; 7244af73f5c9STejun Heo attrs->ordered = true; 72458a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 724629c91e99STejun Heo } 724729c91e99STejun Heo 7248e22bee78STejun Heo system_wq = alloc_workqueue("events", 0, 0); 72491aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7250e22bee78STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7251e22bee78STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7252636b927eSTejun Heo WQ_MAX_ACTIVE); 7253d320c038STejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 7254d320c038STejun Heo WQ_FREEZABLE, 0); 72550668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 72560668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 72578318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 72580668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 72590668106cSViresh Kumar 0); 72601aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 72610668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 72620668106cSViresh Kumar !system_power_efficient_wq || 72630668106cSViresh Kumar !system_freezable_power_efficient_wq); 72643347fa09STejun Heo } 72653347fa09STejun Heo 7266aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7267aa6fde93STejun Heo { 7268aa6fde93STejun Heo unsigned long thresh; 7269aa6fde93STejun Heo unsigned long bogo; 7270aa6fde93STejun Heo 7271dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7272dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7273dd64c873SZqiang 7274aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7275aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7276aa6fde93STejun Heo return; 7277aa6fde93STejun Heo 7278aa6fde93STejun Heo /* 7279aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7280aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7281aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7282aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7283aa6fde93STejun Heo * too low. 7284aa6fde93STejun Heo * 7285aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7286aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7287aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7288aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7289aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7290aa6fde93STejun Heo * usefulness. 7291aa6fde93STejun Heo */ 7292aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7293aa6fde93STejun Heo 7294aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7295aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7296aa6fde93STejun Heo if (bogo < 4000) 7297aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7298aa6fde93STejun Heo 7299aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7300aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7301aa6fde93STejun Heo 7302aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7303aa6fde93STejun Heo } 7304aa6fde93STejun Heo 73053347fa09STejun Heo /** 73063347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 73073347fa09STejun Heo * 73082930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 73092930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 73102930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 73112930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 73122930155bSTejun Heo * workers and enable future kworker creations. 73133347fa09STejun Heo */ 73142333e829SYu Chen void __init workqueue_init(void) 73153347fa09STejun Heo { 73162186d9f9STejun Heo struct workqueue_struct *wq; 73173347fa09STejun Heo struct worker_pool *pool; 73183347fa09STejun Heo int cpu, bkt; 73193347fa09STejun Heo 7320aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7321aa6fde93STejun Heo 73222186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 73232186d9f9STejun Heo 73242930155bSTejun Heo /* 73252930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 73262930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 73272930155bSTejun Heo */ 73282186d9f9STejun Heo for_each_possible_cpu(cpu) { 73292186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 73302186d9f9STejun Heo pool->node = cpu_to_node(cpu); 73312186d9f9STejun Heo } 73322186d9f9STejun Heo } 73332186d9f9STejun Heo 733440c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 733540c17f75STejun Heo WARN(init_rescuer(wq), 733640c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 733740c17f75STejun Heo wq->name); 733840c17f75STejun Heo } 73392186d9f9STejun Heo 73402186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 73412186d9f9STejun Heo 73423347fa09STejun Heo /* create the initial workers */ 73433347fa09STejun Heo for_each_online_cpu(cpu) { 73443347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 73453347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 73463347fa09STejun Heo BUG_ON(!create_worker(pool)); 73473347fa09STejun Heo } 73483347fa09STejun Heo } 73493347fa09STejun Heo 73503347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 73513347fa09STejun Heo BUG_ON(!create_worker(pool)); 73523347fa09STejun Heo 73533347fa09STejun Heo wq_online = true; 735482607adcSTejun Heo wq_watchdog_init(); 73551da177e4SLinus Torvalds } 7356c4f135d6STetsuo Handa 7357025e1684STejun Heo /* 7358025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7359025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7360025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7361025e1684STejun Heo */ 7362025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7363025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7364025e1684STejun Heo { 7365025e1684STejun Heo int cur, pre, cpu, pod; 7366025e1684STejun Heo 7367025e1684STejun Heo pt->nr_pods = 0; 7368025e1684STejun Heo 7369025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7370025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7371025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7372025e1684STejun Heo 7373025e1684STejun Heo for_each_possible_cpu(cur) { 7374025e1684STejun Heo for_each_possible_cpu(pre) { 7375025e1684STejun Heo if (pre >= cur) { 7376025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7377025e1684STejun Heo break; 7378025e1684STejun Heo } 7379025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7380025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7381025e1684STejun Heo break; 7382025e1684STejun Heo } 7383025e1684STejun Heo } 7384025e1684STejun Heo } 7385025e1684STejun Heo 7386025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7387025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7388025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7389025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7390025e1684STejun Heo 7391025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7392025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7393025e1684STejun Heo 7394025e1684STejun Heo for_each_possible_cpu(cpu) { 7395025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7396025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7397025e1684STejun Heo } 7398025e1684STejun Heo } 7399025e1684STejun Heo 740063c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 740163c5484eSTejun Heo { 740263c5484eSTejun Heo return false; 740363c5484eSTejun Heo } 740463c5484eSTejun Heo 740563c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 740663c5484eSTejun Heo { 740763c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 740863c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 740963c5484eSTejun Heo #else 741063c5484eSTejun Heo return false; 741163c5484eSTejun Heo #endif 741263c5484eSTejun Heo } 741363c5484eSTejun Heo 7414025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7415025e1684STejun Heo { 7416025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7417025e1684STejun Heo } 7418025e1684STejun Heo 74192930155bSTejun Heo /** 74202930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 74212930155bSTejun Heo * 74222930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 74232930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 74242930155bSTejun Heo * initializes the unbound CPU pods accordingly. 74252930155bSTejun Heo */ 74262930155bSTejun Heo void __init workqueue_init_topology(void) 7427a86feae6STejun Heo { 74282930155bSTejun Heo struct workqueue_struct *wq; 7429025e1684STejun Heo int cpu; 7430a86feae6STejun Heo 743163c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 743263c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 743363c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7434025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7435a86feae6STejun Heo 7436c5f8cd6cSTejun Heo wq_topo_initialized = true; 7437c5f8cd6cSTejun Heo 74382930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7439a86feae6STejun Heo 7440a86feae6STejun Heo /* 74412930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 74422930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 74432930155bSTejun Heo * combinations to apply per-pod sharing. 74442930155bSTejun Heo */ 74452930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 74465797b1c1STejun Heo for_each_online_cpu(cpu) 74472930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 74485797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 74495797b1c1STejun Heo mutex_lock(&wq->mutex); 74505797b1c1STejun Heo wq_update_node_max_active(wq, -1); 74515797b1c1STejun Heo mutex_unlock(&wq->mutex); 74522930155bSTejun Heo } 74532930155bSTejun Heo } 74542930155bSTejun Heo 74552930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7456a86feae6STejun Heo } 7457a86feae6STejun Heo 745820bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 745920bdedafSTetsuo Handa { 746020bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 746120bdedafSTetsuo Handa dump_stack(); 746220bdedafSTetsuo Handa } 7463c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7464ace3c549Stiozhang 7465ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7466ace3c549Stiozhang { 7467ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7468ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7469ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7470ace3c549Stiozhang } 7471ace3c549Stiozhang 7472ace3c549Stiozhang return 1; 7473ace3c549Stiozhang } 7474ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7475