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 389616db877STejun Heo /* 390616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 391616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 392616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 393aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 394aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 395616db877STejun Heo */ 396aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 397616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 398616db877STejun Heo 399cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 400552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 401cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 402cee22a15SViresh Kumar 403863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 4043347fa09STejun Heo 405fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 406fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4074c16bd32STejun Heo 40868e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4091258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 410a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 411d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 412d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4135bcab335STejun Heo 414e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 41568e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4167d19c5ceSTejun Heo 41799c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 418ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 419ef557180SMike Galbraith 420fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 421fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 422fe28f631SWaiman Long 423fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 424fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 425fe28f631SWaiman Long 426ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 427ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 428ace3c549Stiozhang 429ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 430ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 431b05a7928SFrederic Weisbecker 432f303fccbSTejun Heo /* 433f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 434f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 435f303fccbSTejun Heo * to uncover usages which depend on it. 436f303fccbSTejun Heo */ 437f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 438f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 439f303fccbSTejun Heo #else 440f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 441f303fccbSTejun Heo #endif 442f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 443f303fccbSTejun Heo 4447d19c5ceSTejun Heo /* the per-cpu worker pools */ 44525528213SPeter Zijlstra static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], cpu_worker_pools); 4467d19c5ceSTejun Heo 44768e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4487d19c5ceSTejun Heo 44968e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 45029c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 45129c91e99STejun Heo 452c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 45329c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 45429c91e99STejun Heo 4558a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4568a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4578a2b7538STejun Heo 458967b494eSTejun Heo /* 459967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 460967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 461967b494eSTejun Heo * worker to avoid A-A deadlocks. 462967b494eSTejun Heo */ 46368279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 464967b494eSTejun Heo 46568279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 466ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 46768279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 4681aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 46968279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 470d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 47168279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 472f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 47368279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 47424d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 47568279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 4760668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 47768279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 4780668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 479d320c038STejun Heo 4807d19c5ceSTejun Heo static int worker_thread(void *__worker); 4816ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 482c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 48355df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 4847d19c5ceSTejun Heo 48597bd2347STejun Heo #define CREATE_TRACE_POINTS 48697bd2347STejun Heo #include <trace/events/workqueue.h> 48797bd2347STejun Heo 48868e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 48924acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 490f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 49124acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 4925bcab335STejun Heo 4935b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 49424acfb71SThomas Gleixner RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 495f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 496f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 49724acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 4985b95e1afSLai Jiangshan 499f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 500f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 501f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5027a62c2c8STejun Heo (pool)++) 5034ce62e9eSTejun Heo 50449e3cf44STejun Heo /** 50517116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 50617116969STejun Heo * @pool: iteration cursor 507611c92a0STejun Heo * @pi: integer used for iteration 508fa1b54e6STejun Heo * 50924acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 51068e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 51168e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 512fa1b54e6STejun Heo * 513fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 514fa1b54e6STejun Heo * ignored. 51517116969STejun Heo */ 516611c92a0STejun Heo #define for_each_pool(pool, pi) \ 517611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 51868e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 519fa1b54e6STejun Heo else 52017116969STejun Heo 52117116969STejun Heo /** 522822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 523822d8405STejun Heo * @worker: iteration cursor 524822d8405STejun Heo * @pool: worker_pool to iterate workers of 525822d8405STejun Heo * 5261258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 527822d8405STejun Heo * 528822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 529822d8405STejun Heo * ignored. 530822d8405STejun Heo */ 531da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 532da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5331258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 534822d8405STejun Heo else 535822d8405STejun Heo 536822d8405STejun Heo /** 53749e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 53849e3cf44STejun Heo * @pwq: iteration cursor 53949e3cf44STejun Heo * @wq: the target workqueue 54076af4d93STejun Heo * 54124acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 542794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 543794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 54476af4d93STejun Heo * 54576af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 54676af4d93STejun Heo * ignored. 54749e3cf44STejun Heo */ 54849e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 54949e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5505a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 551f3421797STejun Heo 552dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 553dc186ad7SThomas Gleixner 554f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 555dc186ad7SThomas Gleixner 55699777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 55799777288SStanislaw Gruszka { 55899777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 55999777288SStanislaw Gruszka } 56099777288SStanislaw Gruszka 561b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 562b9fdac7fSDu, Changbin { 563b9fdac7fSDu, Changbin struct work_struct *work = addr; 564b9fdac7fSDu, Changbin 565b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 566b9fdac7fSDu, Changbin } 567b9fdac7fSDu, Changbin 568dc186ad7SThomas Gleixner /* 569dc186ad7SThomas Gleixner * fixup_init is called when: 570dc186ad7SThomas Gleixner * - an active object is initialized 571dc186ad7SThomas Gleixner */ 57202a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 573dc186ad7SThomas Gleixner { 574dc186ad7SThomas Gleixner struct work_struct *work = addr; 575dc186ad7SThomas Gleixner 576dc186ad7SThomas Gleixner switch (state) { 577dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 578dc186ad7SThomas Gleixner cancel_work_sync(work); 579dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 58002a982a6SDu, Changbin return true; 581dc186ad7SThomas Gleixner default: 58202a982a6SDu, Changbin return false; 583dc186ad7SThomas Gleixner } 584dc186ad7SThomas Gleixner } 585dc186ad7SThomas Gleixner 586dc186ad7SThomas Gleixner /* 587dc186ad7SThomas Gleixner * fixup_free is called when: 588dc186ad7SThomas Gleixner * - an active object is freed 589dc186ad7SThomas Gleixner */ 59002a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 591dc186ad7SThomas Gleixner { 592dc186ad7SThomas Gleixner struct work_struct *work = addr; 593dc186ad7SThomas Gleixner 594dc186ad7SThomas Gleixner switch (state) { 595dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 596dc186ad7SThomas Gleixner cancel_work_sync(work); 597dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 59802a982a6SDu, Changbin return true; 599dc186ad7SThomas Gleixner default: 60002a982a6SDu, Changbin return false; 601dc186ad7SThomas Gleixner } 602dc186ad7SThomas Gleixner } 603dc186ad7SThomas Gleixner 604f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 605dc186ad7SThomas Gleixner .name = "work_struct", 60699777288SStanislaw Gruszka .debug_hint = work_debug_hint, 607b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 608dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 609dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 610dc186ad7SThomas Gleixner }; 611dc186ad7SThomas Gleixner 612dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 613dc186ad7SThomas Gleixner { 614dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 615dc186ad7SThomas Gleixner } 616dc186ad7SThomas Gleixner 617dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 618dc186ad7SThomas Gleixner { 619dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 620dc186ad7SThomas Gleixner } 621dc186ad7SThomas Gleixner 622dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 623dc186ad7SThomas Gleixner { 624dc186ad7SThomas Gleixner if (onstack) 625dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 626dc186ad7SThomas Gleixner else 627dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 628dc186ad7SThomas Gleixner } 629dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 630dc186ad7SThomas Gleixner 631dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 632dc186ad7SThomas Gleixner { 633dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 634dc186ad7SThomas Gleixner } 635dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 636dc186ad7SThomas Gleixner 637ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 638ea2e64f2SThomas Gleixner { 639ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 640ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 641ea2e64f2SThomas Gleixner } 642ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 643ea2e64f2SThomas Gleixner 644dc186ad7SThomas Gleixner #else 645dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 646dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 647dc186ad7SThomas Gleixner #endif 648dc186ad7SThomas Gleixner 6494e8b22bdSLi Bin /** 65067dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6514e8b22bdSLi Bin * @pool: the pool pointer of interest 6524e8b22bdSLi Bin * 6534e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6544e8b22bdSLi Bin * successfully, -errno on failure. 6554e8b22bdSLi Bin */ 6569daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6579daf9e67STejun Heo { 6589daf9e67STejun Heo int ret; 6599daf9e67STejun Heo 66068e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6615bcab335STejun Heo 6624e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 6634e8b22bdSLi Bin GFP_KERNEL); 664229641a6STejun Heo if (ret >= 0) { 665e68035fbSTejun Heo pool->id = ret; 666229641a6STejun Heo return 0; 667229641a6STejun Heo } 6689daf9e67STejun Heo return ret; 6699daf9e67STejun Heo } 6709daf9e67STejun Heo 6719f66cff2STejun Heo static struct pool_workqueue __rcu ** 6729f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 6739f66cff2STejun Heo { 6749f66cff2STejun Heo if (cpu >= 0) 6759f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 6769f66cff2STejun Heo else 6779f66cff2STejun Heo return &wq->dfl_pwq; 6789f66cff2STejun Heo } 6799f66cff2STejun Heo 6809f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 6819f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 6829f66cff2STejun Heo { 6839f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 6849f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 6859f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 6869f66cff2STejun Heo } 6879f66cff2STejun Heo 6885797b1c1STejun Heo /** 6895797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 6905797b1c1STejun Heo * @wq: workqueue of interest 6915797b1c1STejun Heo * 6925797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 6935797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 6945797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 6955797b1c1STejun Heo */ 6965797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 6975797b1c1STejun Heo { 6985797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 6995797b1c1STejun Heo } 7005797b1c1STejun Heo 70173f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 70273f53c4aSTejun Heo { 70373f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 70473f53c4aSTejun Heo } 70573f53c4aSTejun Heo 706c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 70773f53c4aSTejun Heo { 708c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 70973f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 71073f53c4aSTejun Heo } 71173f53c4aSTejun Heo 71273f53c4aSTejun Heo static int work_next_color(int color) 71373f53c4aSTejun Heo { 71473f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 715a848e3b6SOleg Nesterov } 716a848e3b6SOleg Nesterov 7174594bf15SDavid Howells /* 718112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 719112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7207c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7217a22ad75STejun Heo * 722112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 723112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 724bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 725bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 7267a22ad75STejun Heo * 727112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7287c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 729112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7307c3eed5cSTejun Heo * available only while the work item is queued. 731bbb68dfaSTejun Heo * 732bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 733bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 734bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 735bbb68dfaSTejun Heo * try to steal the PENDING bit. 7364594bf15SDavid Howells */ 7377a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 7387a22ad75STejun Heo unsigned long flags) 7397a22ad75STejun Heo { 7406183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7417a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7427a22ad75STejun Heo } 7437a22ad75STejun Heo 744112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7454690c4abSTejun Heo unsigned long extra_flags) 746365970a1SDavid Howells { 747112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 748112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 749365970a1SDavid Howells } 750365970a1SDavid Howells 7514468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7524468a00fSLai Jiangshan int pool_id) 7534468a00fSLai Jiangshan { 7544468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7554468a00fSLai Jiangshan WORK_STRUCT_PENDING); 7564468a00fSLai Jiangshan } 7574468a00fSLai Jiangshan 7587c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7597c3eed5cSTejun Heo int pool_id) 7604d707b9fSOleg Nesterov { 76123657bb1STejun Heo /* 76223657bb1STejun Heo * The following wmb is paired with the implied mb in 76323657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 76423657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 76523657bb1STejun Heo * owner. 76623657bb1STejun Heo */ 76723657bb1STejun Heo smp_wmb(); 7687c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 769346c09f8SRoman Pen /* 770346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 771346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 772346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 7738bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 774346c09f8SRoman Pen * the same @work. E.g. consider this case: 775346c09f8SRoman Pen * 776346c09f8SRoman Pen * CPU#0 CPU#1 777346c09f8SRoman Pen * ---------------------------- -------------------------------- 778346c09f8SRoman Pen * 779346c09f8SRoman Pen * 1 STORE event_indicated 780346c09f8SRoman Pen * 2 queue_work_on() { 781346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 782346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 783346c09f8SRoman Pen * 5 set_work_data() # clear bit 784346c09f8SRoman Pen * 6 smp_mb() 785346c09f8SRoman Pen * 7 work->current_func() { 786346c09f8SRoman Pen * 8 LOAD event_indicated 787346c09f8SRoman Pen * } 788346c09f8SRoman Pen * 789346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 790346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 791346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 792346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 793346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 794346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 795346c09f8SRoman Pen * before actual STORE. 796346c09f8SRoman Pen */ 797346c09f8SRoman Pen smp_mb(); 7984d707b9fSOleg Nesterov } 7994d707b9fSOleg Nesterov 8007a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 801365970a1SDavid Howells { 8027c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 8037c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 8047a22ad75STejun Heo } 8057a22ad75STejun Heo 806afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 807afa4bb77SLinus Torvalds { 808afa4bb77SLinus Torvalds return (struct pool_workqueue *)(data & WORK_STRUCT_WQ_DATA_MASK); 809afa4bb77SLinus Torvalds } 810afa4bb77SLinus Torvalds 811112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8127a22ad75STejun Heo { 813e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8147a22ad75STejun Heo 815112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 816afa4bb77SLinus Torvalds return work_struct_pwq(data); 817e120153dSTejun Heo else 818e120153dSTejun Heo return NULL; 8197a22ad75STejun Heo } 8207a22ad75STejun Heo 8217c3eed5cSTejun Heo /** 8227c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8237c3eed5cSTejun Heo * @work: the work item of interest 8247c3eed5cSTejun Heo * 82568e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 82624acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 82724acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 828fa1b54e6STejun Heo * 829fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 830fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 831fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 832fa1b54e6STejun Heo * returned pool is and stays online. 833d185af30SYacine Belkadi * 834d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8357c3eed5cSTejun Heo */ 8367c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8377a22ad75STejun Heo { 838e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8397c3eed5cSTejun Heo int pool_id; 8407a22ad75STejun Heo 84168e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 842fa1b54e6STejun Heo 843112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 844afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8457a22ad75STejun Heo 8467c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8477c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8487a22ad75STejun Heo return NULL; 8497a22ad75STejun Heo 850fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8517c3eed5cSTejun Heo } 8527c3eed5cSTejun Heo 8537c3eed5cSTejun Heo /** 8547c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8557c3eed5cSTejun Heo * @work: the work item of interest 8567c3eed5cSTejun Heo * 857d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8587c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8597c3eed5cSTejun Heo */ 8607c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8617c3eed5cSTejun Heo { 86254d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 8637c3eed5cSTejun Heo 864112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 865afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 86654d5b7d0SLai Jiangshan 86754d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 8687c3eed5cSTejun Heo } 8697c3eed5cSTejun Heo 870bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 871bbb68dfaSTejun Heo { 8727c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 873bbb68dfaSTejun Heo 8747c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 8757c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 876bbb68dfaSTejun Heo } 877bbb68dfaSTejun Heo 878bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 879bbb68dfaSTejun Heo { 880bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 881bbb68dfaSTejun Heo 882112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 883bbb68dfaSTejun Heo } 884bbb68dfaSTejun Heo 885e22bee78STejun Heo /* 8863270476aSTejun Heo * Policy functions. These define the policies on how the global worker 8873270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 888d565ed63STejun Heo * they're being called with pool->lock held. 889e22bee78STejun Heo */ 890e22bee78STejun Heo 891e22bee78STejun Heo /* 892e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 893e22bee78STejun Heo * running workers. 894974271c4STejun Heo * 895974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 896706026c2STejun Heo * function will always return %true for unbound pools as long as the 897974271c4STejun Heo * worklist isn't empty. 898e22bee78STejun Heo */ 89963d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 900e22bee78STejun Heo { 9010219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 902e22bee78STejun Heo } 903e22bee78STejun Heo 904e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 90563d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 906e22bee78STejun Heo { 90763d95a91STejun Heo return pool->nr_idle; 908e22bee78STejun Heo } 909e22bee78STejun Heo 910e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 91163d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 912e22bee78STejun Heo { 913bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 914e22bee78STejun Heo } 915e22bee78STejun Heo 916e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 91763d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 918e22bee78STejun Heo { 91963d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 920e22bee78STejun Heo } 921e22bee78STejun Heo 922e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 92363d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 924e22bee78STejun Heo { 925692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 92663d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 92763d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 928e22bee78STejun Heo 929e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 930e22bee78STejun Heo } 931e22bee78STejun Heo 9324690c4abSTejun Heo /** 933e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 934cb444766STejun Heo * @worker: self 935d302f017STejun Heo * @flags: flags to set 936d302f017STejun Heo * 937228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 938d302f017STejun Heo */ 939228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 940d302f017STejun Heo { 941bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 942e22bee78STejun Heo 943bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 944cb444766STejun Heo 945228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 946e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 947e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 948bc35f7efSLai Jiangshan pool->nr_running--; 949e22bee78STejun Heo } 950e22bee78STejun Heo 951d302f017STejun Heo worker->flags |= flags; 952d302f017STejun Heo } 953d302f017STejun Heo 954d302f017STejun Heo /** 955e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 956cb444766STejun Heo * @worker: self 957d302f017STejun Heo * @flags: flags to clear 958d302f017STejun Heo * 959e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 960d302f017STejun Heo */ 961d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 962d302f017STejun Heo { 96363d95a91STejun Heo struct worker_pool *pool = worker->pool; 964e22bee78STejun Heo unsigned int oflags = worker->flags; 965e22bee78STejun Heo 966bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 967cb444766STejun Heo 968d302f017STejun Heo worker->flags &= ~flags; 969e22bee78STejun Heo 97042c025f3STejun Heo /* 97142c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 97242c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 97342c025f3STejun Heo * of multiple flags, not a single flag. 97442c025f3STejun Heo */ 975e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 976e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 977bc35f7efSLai Jiangshan pool->nr_running++; 978d302f017STejun Heo } 979d302f017STejun Heo 980797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 981797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 982797e8345STejun Heo { 983797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 984797e8345STejun Heo return NULL; 985797e8345STejun Heo 986797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 987797e8345STejun Heo } 988797e8345STejun Heo 989797e8345STejun Heo /** 990797e8345STejun Heo * worker_enter_idle - enter idle state 991797e8345STejun Heo * @worker: worker which is entering idle state 992797e8345STejun Heo * 993797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 994797e8345STejun Heo * necessary. 995797e8345STejun Heo * 996797e8345STejun Heo * LOCKING: 997797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 998797e8345STejun Heo */ 999797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1000797e8345STejun Heo { 1001797e8345STejun Heo struct worker_pool *pool = worker->pool; 1002797e8345STejun Heo 1003797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1004797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1005797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1006797e8345STejun Heo return; 1007797e8345STejun Heo 1008797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1009797e8345STejun Heo worker->flags |= WORKER_IDLE; 1010797e8345STejun Heo pool->nr_idle++; 1011797e8345STejun Heo worker->last_active = jiffies; 1012797e8345STejun Heo 1013797e8345STejun Heo /* idle_list is LIFO */ 1014797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1015797e8345STejun Heo 1016797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1017797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1018797e8345STejun Heo 1019797e8345STejun Heo /* Sanity check nr_running. */ 1020797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1021797e8345STejun Heo } 1022797e8345STejun Heo 1023797e8345STejun Heo /** 1024797e8345STejun Heo * worker_leave_idle - leave idle state 1025797e8345STejun Heo * @worker: worker which is leaving idle state 1026797e8345STejun Heo * 1027797e8345STejun Heo * @worker is leaving idle state. Update stats. 1028797e8345STejun Heo * 1029797e8345STejun Heo * LOCKING: 1030797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1031797e8345STejun Heo */ 1032797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1033797e8345STejun Heo { 1034797e8345STejun Heo struct worker_pool *pool = worker->pool; 1035797e8345STejun Heo 1036797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1037797e8345STejun Heo return; 1038797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1039797e8345STejun Heo pool->nr_idle--; 1040797e8345STejun Heo list_del_init(&worker->entry); 1041797e8345STejun Heo } 1042797e8345STejun Heo 1043797e8345STejun Heo /** 1044797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1045797e8345STejun Heo * @pool: pool of interest 1046797e8345STejun Heo * @work: work to find worker for 1047797e8345STejun Heo * 1048797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1049797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1050797e8345STejun Heo * to match, its current execution should match the address of @work and 1051797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1052797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1053797e8345STejun Heo * being executed. 1054797e8345STejun Heo * 1055797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1056797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1057797e8345STejun Heo * another work item. If the same work item address ends up being reused 1058797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1059797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1060797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1061797e8345STejun Heo * 1062797e8345STejun Heo * This function checks the work item address and work function to avoid 1063797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1064797e8345STejun Heo * work function which can introduce dependency onto itself through a 1065797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1066797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1067797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1068797e8345STejun Heo * 1069797e8345STejun Heo * CONTEXT: 1070797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1071797e8345STejun Heo * 1072797e8345STejun Heo * Return: 1073797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1074797e8345STejun Heo * otherwise. 1075797e8345STejun Heo */ 1076797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1077797e8345STejun Heo struct work_struct *work) 1078797e8345STejun Heo { 1079797e8345STejun Heo struct worker *worker; 1080797e8345STejun Heo 1081797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1082797e8345STejun Heo (unsigned long)work) 1083797e8345STejun Heo if (worker->current_work == work && 1084797e8345STejun Heo worker->current_func == work->func) 1085797e8345STejun Heo return worker; 1086797e8345STejun Heo 1087797e8345STejun Heo return NULL; 1088797e8345STejun Heo } 1089797e8345STejun Heo 1090797e8345STejun Heo /** 1091797e8345STejun Heo * move_linked_works - move linked works to a list 1092797e8345STejun Heo * @work: start of series of works to be scheduled 1093797e8345STejun Heo * @head: target list to append @work to 1094797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1095797e8345STejun Heo * 1096873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1097873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1098873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1099873eaca6STejun Heo * @nextp. 1100797e8345STejun Heo * 1101797e8345STejun Heo * CONTEXT: 1102797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1103797e8345STejun Heo */ 1104797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1105797e8345STejun Heo struct work_struct **nextp) 1106797e8345STejun Heo { 1107797e8345STejun Heo struct work_struct *n; 1108797e8345STejun Heo 1109797e8345STejun Heo /* 1110797e8345STejun Heo * Linked worklist will always end before the end of the list, 1111797e8345STejun Heo * use NULL for list head. 1112797e8345STejun Heo */ 1113797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1114797e8345STejun Heo list_move_tail(&work->entry, head); 1115797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1116797e8345STejun Heo break; 1117797e8345STejun Heo } 1118797e8345STejun Heo 1119797e8345STejun Heo /* 1120797e8345STejun Heo * If we're already inside safe list traversal and have moved 1121797e8345STejun Heo * multiple works to the scheduled queue, the next position 1122797e8345STejun Heo * needs to be updated. 1123797e8345STejun Heo */ 1124797e8345STejun Heo if (nextp) 1125797e8345STejun Heo *nextp = n; 1126797e8345STejun Heo } 1127797e8345STejun Heo 1128797e8345STejun Heo /** 1129873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1130873eaca6STejun Heo * @work: work to assign 1131873eaca6STejun Heo * @worker: worker to assign to 1132873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1133873eaca6STejun Heo * 1134873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1135873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1136873eaca6STejun Heo * 1137873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1138873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1139873eaca6STejun Heo * list_for_each_entry_safe(). 1140873eaca6STejun Heo * 1141873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1142873eaca6STejun Heo * was punted to another worker already executing it. 1143873eaca6STejun Heo */ 1144873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1145873eaca6STejun Heo struct work_struct **nextp) 1146873eaca6STejun Heo { 1147873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1148873eaca6STejun Heo struct worker *collision; 1149873eaca6STejun Heo 1150873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1151873eaca6STejun Heo 1152873eaca6STejun Heo /* 1153873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1154873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1155873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1156873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1157873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1158873eaca6STejun Heo * defer the work to the currently executing one. 1159873eaca6STejun Heo */ 1160873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1161873eaca6STejun Heo if (unlikely(collision)) { 1162873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1163873eaca6STejun Heo return false; 1164873eaca6STejun Heo } 1165873eaca6STejun Heo 1166873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1167873eaca6STejun Heo return true; 1168873eaca6STejun Heo } 1169873eaca6STejun Heo 1170873eaca6STejun Heo /** 11710219a352STejun Heo * kick_pool - wake up an idle worker if necessary 11720219a352STejun Heo * @pool: pool to kick 1173797e8345STejun Heo * 11740219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 11750219a352STejun Heo * whether a worker was woken up. 1176797e8345STejun Heo */ 11770219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1178797e8345STejun Heo { 1179797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 11808639ecebSTejun Heo struct task_struct *p; 1181797e8345STejun Heo 11820219a352STejun Heo lockdep_assert_held(&pool->lock); 11830219a352STejun Heo 11840219a352STejun Heo if (!need_more_worker(pool) || !worker) 11850219a352STejun Heo return false; 11860219a352STejun Heo 11878639ecebSTejun Heo p = worker->task; 11888639ecebSTejun Heo 11898639ecebSTejun Heo #ifdef CONFIG_SMP 11908639ecebSTejun Heo /* 11918639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 11928639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 11938639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 11948639ecebSTejun Heo * execution locality. 11958639ecebSTejun Heo * 11968639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 11978639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 11988639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 11998639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12008639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12018639ecebSTejun Heo * still on cpu when picking an idle worker. 12028639ecebSTejun Heo * 12038639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12048639ecebSTejun Heo * its affinity scope. Repatriate. 12058639ecebSTejun Heo */ 12068639ecebSTejun Heo if (!pool->attrs->affn_strict && 12078639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12088639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12098639ecebSTejun Heo struct work_struct, entry); 12108639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12118639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12128639ecebSTejun Heo } 12138639ecebSTejun Heo #endif 12148639ecebSTejun Heo wake_up_process(p); 12150219a352STejun Heo return true; 1216797e8345STejun Heo } 1217797e8345STejun Heo 121863638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 121963638450STejun Heo 122063638450STejun Heo /* 122163638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 122263638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 122363638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 122463638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 122563638450STejun Heo * should be using an unbound workqueue instead. 122663638450STejun Heo * 122763638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 122863638450STejun Heo * and report them so that they can be examined and converted to use unbound 122963638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 123063638450STejun Heo * function is tracked and reported with exponential backoff. 123163638450STejun Heo */ 123263638450STejun Heo #define WCI_MAX_ENTS 128 123363638450STejun Heo 123463638450STejun Heo struct wci_ent { 123563638450STejun Heo work_func_t func; 123663638450STejun Heo atomic64_t cnt; 123763638450STejun Heo struct hlist_node hash_node; 123863638450STejun Heo }; 123963638450STejun Heo 124063638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 124163638450STejun Heo static int wci_nr_ents; 124263638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 124363638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 124463638450STejun Heo 124563638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 124663638450STejun Heo { 124763638450STejun Heo struct wci_ent *ent; 124863638450STejun Heo 124963638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 125063638450STejun Heo (unsigned long)func) { 125163638450STejun Heo if (ent->func == func) 125263638450STejun Heo return ent; 125363638450STejun Heo } 125463638450STejun Heo return NULL; 125563638450STejun Heo } 125663638450STejun Heo 125763638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 125863638450STejun Heo { 125963638450STejun Heo struct wci_ent *ent; 126063638450STejun Heo 126163638450STejun Heo restart: 126263638450STejun Heo ent = wci_find_ent(func); 126363638450STejun Heo if (ent) { 126463638450STejun Heo u64 cnt; 126563638450STejun Heo 126663638450STejun Heo /* 126763638450STejun Heo * Start reporting from the fourth time and back off 126863638450STejun Heo * exponentially. 126963638450STejun Heo */ 127063638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 127163638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 127263638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 127363638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 127463638450STejun Heo atomic64_read(&ent->cnt)); 127563638450STejun Heo return; 127663638450STejun Heo } 127763638450STejun Heo 127863638450STejun Heo /* 127963638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 128063638450STejun Heo * is exhausted, something went really wrong and we probably made enough 128163638450STejun Heo * noise already. 128263638450STejun Heo */ 128363638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 128463638450STejun Heo return; 128563638450STejun Heo 128663638450STejun Heo raw_spin_lock(&wci_lock); 128763638450STejun Heo 128863638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 128963638450STejun Heo raw_spin_unlock(&wci_lock); 129063638450STejun Heo return; 129163638450STejun Heo } 129263638450STejun Heo 129363638450STejun Heo if (wci_find_ent(func)) { 129463638450STejun Heo raw_spin_unlock(&wci_lock); 129563638450STejun Heo goto restart; 129663638450STejun Heo } 129763638450STejun Heo 129863638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 129963638450STejun Heo ent->func = func; 130063638450STejun Heo atomic64_set(&ent->cnt, 1); 130163638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 130263638450STejun Heo 130363638450STejun Heo raw_spin_unlock(&wci_lock); 130463638450STejun Heo } 130563638450STejun Heo 130663638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 130763638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 130863638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 130963638450STejun Heo 1310c54d5046STejun Heo /** 13111da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13121da177e4SLinus Torvalds * @task: task waking up 13131da177e4SLinus Torvalds * 13141da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13151da177e4SLinus Torvalds */ 13161da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13171da177e4SLinus Torvalds { 13181da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13191da177e4SLinus Torvalds 1320c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13211da177e4SLinus Torvalds return; 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds /* 13241da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13251da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13261da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13271da177e4SLinus Torvalds * pool. Protect against such race. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds preempt_disable(); 13301da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13311da177e4SLinus Torvalds worker->pool->nr_running++; 13321da177e4SLinus Torvalds preempt_enable(); 1333616db877STejun Heo 1334616db877STejun Heo /* 1335616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1336616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1337616db877STejun Heo */ 1338616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1339616db877STejun Heo 1340c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds /** 13441da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 13451da177e4SLinus Torvalds * @task: task going to sleep 13461da177e4SLinus Torvalds * 13471da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 13481da177e4SLinus Torvalds * going to sleep. 13491da177e4SLinus Torvalds */ 13501da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13531da177e4SLinus Torvalds struct worker_pool *pool; 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds /* 13561da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 13571da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 13581da177e4SLinus Torvalds * checking NOT_RUNNING. 13591da177e4SLinus Torvalds */ 13601da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 13611da177e4SLinus Torvalds return; 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds pool = worker->pool; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1366c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 13671da177e4SLinus Torvalds return; 13681da177e4SLinus Torvalds 1369c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 13701da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds /* 13731da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 13741da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 13751da177e4SLinus Torvalds * and nr_running has been reset. 13761da177e4SLinus Torvalds */ 13771da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 13781da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13791da177e4SLinus Torvalds return; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 13821da177e4SLinus Torvalds pool->nr_running--; 13830219a352STejun Heo if (kick_pool(pool)) 1384725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 13850219a352STejun Heo 13861da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /** 1390616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1391616db877STejun Heo * @task: task currently running 1392616db877STejun Heo * 1393616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1394616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1395616db877STejun Heo */ 1396616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1397616db877STejun Heo { 1398616db877STejun Heo struct worker *worker = kthread_data(task); 1399616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1400616db877STejun Heo struct worker_pool *pool = worker->pool; 1401616db877STejun Heo 1402616db877STejun Heo if (!pwq) 1403616db877STejun Heo return; 1404616db877STejun Heo 14058a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14068a1dd1e5STejun Heo 140718c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 140818c8ae81SZqiang return; 140918c8ae81SZqiang 1410616db877STejun Heo /* 1411616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1412616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1413616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1414c8f6219bSZqiang * 1415c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1416c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1417c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1418c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1419c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1420c8f6219bSZqiang * We probably want to make this prettier in the future. 1421616db877STejun Heo */ 1422c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1423616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1424616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1425616db877STejun Heo return; 1426616db877STejun Heo 1427616db877STejun Heo raw_spin_lock(&pool->lock); 1428616db877STejun Heo 1429616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 143063638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1431616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1432616db877STejun Heo 14330219a352STejun Heo if (kick_pool(pool)) 1434616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1435616db877STejun Heo 1436616db877STejun Heo raw_spin_unlock(&pool->lock); 1437616db877STejun Heo } 1438616db877STejun Heo 1439616db877STejun Heo /** 14401da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 14411da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 14421da177e4SLinus Torvalds * 14431da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 14441da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 14451da177e4SLinus Torvalds * 14461da177e4SLinus Torvalds * CONTEXT: 14479b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 14481da177e4SLinus Torvalds * 14491da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1450f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1451f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 14521da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 14531da177e4SLinus Torvalds * 14541da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 14551da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 14561da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 14571da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1458365970a1SDavid Howells * 1459365970a1SDavid Howells * Return: 1460365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1461365970a1SDavid Howells * hasn't executed any work yet. 1462365970a1SDavid Howells */ 1463365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1464365970a1SDavid Howells { 1465365970a1SDavid Howells struct worker *worker = kthread_data(task); 1466365970a1SDavid Howells 1467365970a1SDavid Howells return worker->last_func; 1468365970a1SDavid Howells } 1469365970a1SDavid Howells 1470d302f017STejun Heo /** 147191ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 147291ccc6e7STejun Heo * @wq: workqueue of interest 147391ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 147491ccc6e7STejun Heo * 147591ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 147691ccc6e7STejun Heo * 147791ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 147891ccc6e7STejun Heo * 147991ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 148091ccc6e7STejun Heo * 148191ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 148291ccc6e7STejun Heo */ 148391ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 148491ccc6e7STejun Heo int node) 148591ccc6e7STejun Heo { 148691ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 148791ccc6e7STejun Heo return NULL; 148891ccc6e7STejun Heo 148991ccc6e7STejun Heo if (node == NUMA_NO_NODE) 149091ccc6e7STejun Heo node = nr_node_ids; 149191ccc6e7STejun Heo 149291ccc6e7STejun Heo return wq->node_nr_active[node]; 149391ccc6e7STejun Heo } 149491ccc6e7STejun Heo 149591ccc6e7STejun Heo /** 14965797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 14975797b1c1STejun Heo * @wq: workqueue to update 14985797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 14995797b1c1STejun Heo * 15005797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15015797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15025797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15035797b1c1STejun Heo */ 15045797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15055797b1c1STejun Heo { 15065797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15075797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15085797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15095797b1c1STejun Heo int total_cpus, node; 15105797b1c1STejun Heo 15115797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15125797b1c1STejun Heo 1513*15930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15145797b1c1STejun Heo off_cpu = -1; 15155797b1c1STejun Heo 15165797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15175797b1c1STejun Heo if (off_cpu >= 0) 15185797b1c1STejun Heo total_cpus--; 15195797b1c1STejun Heo 15205797b1c1STejun Heo for_each_node(node) { 15215797b1c1STejun Heo int node_cpus; 15225797b1c1STejun Heo 15235797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15245797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15255797b1c1STejun Heo node_cpus--; 15265797b1c1STejun Heo 15275797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 15285797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 15295797b1c1STejun Heo min_active, max_active); 15305797b1c1STejun Heo } 15315797b1c1STejun Heo 15325797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 15335797b1c1STejun Heo } 15345797b1c1STejun Heo 15355797b1c1STejun Heo /** 15368864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 15378864b4e5STejun Heo * @pwq: pool_workqueue to get 15388864b4e5STejun Heo * 15398864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 15408864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 15418864b4e5STejun Heo */ 15428864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 15438864b4e5STejun Heo { 15448864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 15458864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 15468864b4e5STejun Heo pwq->refcnt++; 15478864b4e5STejun Heo } 15488864b4e5STejun Heo 15498864b4e5STejun Heo /** 15508864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 15518864b4e5STejun Heo * @pwq: pool_workqueue to put 15528864b4e5STejun Heo * 15538864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 15548864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 15558864b4e5STejun Heo */ 15568864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 15578864b4e5STejun Heo { 15588864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 15598864b4e5STejun Heo if (likely(--pwq->refcnt)) 15608864b4e5STejun Heo return; 15618864b4e5STejun Heo /* 1562967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1563967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 15648864b4e5STejun Heo */ 1565687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 15668864b4e5STejun Heo } 15678864b4e5STejun Heo 1568dce90d47STejun Heo /** 1569dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1570dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1571dce90d47STejun Heo * 1572dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1573dce90d47STejun Heo */ 1574dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1575dce90d47STejun Heo { 1576dce90d47STejun Heo if (pwq) { 1577dce90d47STejun Heo /* 157824acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1579dce90d47STejun Heo * following lock operations are safe. 1580dce90d47STejun Heo */ 1581a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1582dce90d47STejun Heo put_pwq(pwq); 1583a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1584dce90d47STejun Heo } 1585dce90d47STejun Heo } 1586dce90d47STejun Heo 1587afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1588afa87ce8STejun Heo { 1589afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1590afa87ce8STejun Heo } 1591afa87ce8STejun Heo 15924c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 15934c638030STejun Heo struct work_struct *work) 1594bf4ede01STejun Heo { 15951c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 15961c270b79STejun Heo 15971c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1598bf4ede01STejun Heo trace_workqueue_activate_work(work); 159982607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 160082607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1601112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16021c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16034c638030STejun Heo } 16044c638030STejun Heo 16054c638030STejun Heo /** 16064c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16074c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16084c638030STejun Heo * @work: work item to activate 16094c638030STejun Heo * 16104c638030STejun Heo * Returns %true if activated. %false if already active. 16114c638030STejun Heo */ 16124c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16134c638030STejun Heo struct work_struct *work) 16144c638030STejun Heo { 16154c638030STejun Heo struct worker_pool *pool = pwq->pool; 161691ccc6e7STejun Heo struct wq_node_nr_active *nna; 16174c638030STejun Heo 16184c638030STejun Heo lockdep_assert_held(&pool->lock); 16194c638030STejun Heo 16204c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16214c638030STejun Heo return false; 16224c638030STejun Heo 162391ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 162491ccc6e7STejun Heo if (nna) 162591ccc6e7STejun Heo atomic_inc(&nna->nr); 162691ccc6e7STejun Heo 1627112202d9STejun Heo pwq->nr_active++; 16284c638030STejun Heo __pwq_activate_work(pwq, work); 16294c638030STejun Heo return true; 1630bf4ede01STejun Heo } 1631bf4ede01STejun Heo 16325797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 16335797b1c1STejun Heo { 16345797b1c1STejun Heo int max = READ_ONCE(nna->max); 16355797b1c1STejun Heo 16365797b1c1STejun Heo while (true) { 16375797b1c1STejun Heo int old, tmp; 16385797b1c1STejun Heo 16395797b1c1STejun Heo old = atomic_read(&nna->nr); 16405797b1c1STejun Heo if (old >= max) 16415797b1c1STejun Heo return false; 16425797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 16435797b1c1STejun Heo if (tmp == old) 16445797b1c1STejun Heo return true; 16455797b1c1STejun Heo } 16465797b1c1STejun Heo } 16475797b1c1STejun Heo 16481c270b79STejun Heo /** 16491c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 16501c270b79STejun Heo * @pwq: pool_workqueue of interest 16515797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 16521c270b79STejun Heo * 16531c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 16541c270b79STejun Heo * successfully obtained. %false otherwise. 16551c270b79STejun Heo */ 16565797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 16573aa62497SLai Jiangshan { 16581c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 16591c270b79STejun Heo struct worker_pool *pool = pwq->pool; 166091ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 16615797b1c1STejun Heo bool obtained = false; 16621c270b79STejun Heo 16631c270b79STejun Heo lockdep_assert_held(&pool->lock); 16641c270b79STejun Heo 16655797b1c1STejun Heo if (!nna) { 16665797b1c1STejun Heo /* per-cpu workqueue, pwq->nr_active is sufficient */ 16671c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 16685797b1c1STejun Heo goto out; 166991ccc6e7STejun Heo } 16705797b1c1STejun Heo 16715797b1c1STejun Heo /* 16725797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 16735797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 16745797b1c1STejun Heo * concurrency level. Don't jump the line. 16755797b1c1STejun Heo * 16765797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 16775797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 16785797b1c1STejun Heo * increase it. This is indicated by @fill. 16795797b1c1STejun Heo */ 16805797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 16815797b1c1STejun Heo goto out; 16825797b1c1STejun Heo 16835797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 16845797b1c1STejun Heo if (obtained) 16855797b1c1STejun Heo goto out; 16865797b1c1STejun Heo 16875797b1c1STejun Heo /* 16885797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 16895797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 16905797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 16915797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 16925797b1c1STejun Heo * $nna->pending_pwqs. 16935797b1c1STejun Heo */ 16945797b1c1STejun Heo raw_spin_lock(&nna->lock); 16955797b1c1STejun Heo 16965797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 16975797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 16985797b1c1STejun Heo else if (likely(!fill)) 16995797b1c1STejun Heo goto out_unlock; 17005797b1c1STejun Heo 17015797b1c1STejun Heo smp_mb(); 17025797b1c1STejun Heo 17035797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17045797b1c1STejun Heo 17055797b1c1STejun Heo /* 17065797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17075797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17085797b1c1STejun Heo */ 17095797b1c1STejun Heo if (obtained && likely(!fill)) 17105797b1c1STejun Heo list_del_init(&pwq->pending_node); 17115797b1c1STejun Heo 17125797b1c1STejun Heo out_unlock: 17135797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17145797b1c1STejun Heo out: 17155797b1c1STejun Heo if (obtained) 17165797b1c1STejun Heo pwq->nr_active++; 17171c270b79STejun Heo return obtained; 17181c270b79STejun Heo } 17191c270b79STejun Heo 17201c270b79STejun Heo /** 17211c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17221c270b79STejun Heo * @pwq: pool_workqueue of interest 17235797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17241c270b79STejun Heo * 17251c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 17261c270b79STejun Heo * max_active limit. 17271c270b79STejun Heo * 17281c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 17291c270b79STejun Heo * inactive work item is found or max_active limit is reached. 17301c270b79STejun Heo */ 17315797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 17321c270b79STejun Heo { 17331c270b79STejun Heo struct work_struct *work = 17341c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 17353aa62497SLai Jiangshan struct work_struct, entry); 17363aa62497SLai Jiangshan 17375797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 17381c270b79STejun Heo __pwq_activate_work(pwq, work); 17391c270b79STejun Heo return true; 17401c270b79STejun Heo } else { 17411c270b79STejun Heo return false; 17421c270b79STejun Heo } 17431c270b79STejun Heo } 17441c270b79STejun Heo 17451c270b79STejun Heo /** 17465797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 17475797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 17485797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 17495797b1c1STejun Heo * 17505797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 17515797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 17525797b1c1STejun Heo */ 17535797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 17545797b1c1STejun Heo struct worker_pool *caller_pool) 17555797b1c1STejun Heo { 17565797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 17575797b1c1STejun Heo struct pool_workqueue *pwq; 17585797b1c1STejun Heo struct work_struct *work; 17595797b1c1STejun Heo 17605797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 17615797b1c1STejun Heo 17625797b1c1STejun Heo raw_spin_lock(&nna->lock); 17635797b1c1STejun Heo retry: 17645797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 17655797b1c1STejun Heo struct pool_workqueue, pending_node); 17665797b1c1STejun Heo if (!pwq) 17675797b1c1STejun Heo goto out_unlock; 17685797b1c1STejun Heo 17695797b1c1STejun Heo /* 17705797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 17715797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 17725797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 17735797b1c1STejun Heo * nested inside pool locks. 17745797b1c1STejun Heo */ 17755797b1c1STejun Heo if (pwq->pool != locked_pool) { 17765797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 17775797b1c1STejun Heo locked_pool = pwq->pool; 17785797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 17795797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17805797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 17815797b1c1STejun Heo raw_spin_lock(&nna->lock); 17825797b1c1STejun Heo goto retry; 17835797b1c1STejun Heo } 17845797b1c1STejun Heo } 17855797b1c1STejun Heo 17865797b1c1STejun Heo /* 17875797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 17885797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 17895797b1c1STejun Heo */ 17905797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 17915797b1c1STejun Heo struct work_struct, entry); 17925797b1c1STejun Heo if (!work) { 17935797b1c1STejun Heo list_del_init(&pwq->pending_node); 17945797b1c1STejun Heo goto retry; 17955797b1c1STejun Heo } 17965797b1c1STejun Heo 17975797b1c1STejun Heo /* 17985797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 17995797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 18005797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 18015797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 18025797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 18035797b1c1STejun Heo */ 18045797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 18055797b1c1STejun Heo pwq->nr_active++; 18065797b1c1STejun Heo __pwq_activate_work(pwq, work); 18075797b1c1STejun Heo 18085797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 18095797b1c1STejun Heo list_del_init(&pwq->pending_node); 18105797b1c1STejun Heo else 18115797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 18125797b1c1STejun Heo 18135797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 18145797b1c1STejun Heo if (pwq->pool != caller_pool) 18155797b1c1STejun Heo kick_pool(pwq->pool); 18165797b1c1STejun Heo } 18175797b1c1STejun Heo 18185797b1c1STejun Heo out_unlock: 18195797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18205797b1c1STejun Heo if (locked_pool != caller_pool) { 18215797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18225797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 18235797b1c1STejun Heo } 18245797b1c1STejun Heo } 18255797b1c1STejun Heo 18265797b1c1STejun Heo /** 18271c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 18281c270b79STejun Heo * @pwq: pool_workqueue of interest 18291c270b79STejun Heo * 18301c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 18315797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 18321c270b79STejun Heo */ 18331c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 18341c270b79STejun Heo { 18351c270b79STejun Heo struct worker_pool *pool = pwq->pool; 183691ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 18371c270b79STejun Heo 18381c270b79STejun Heo lockdep_assert_held(&pool->lock); 18391c270b79STejun Heo 184091ccc6e7STejun Heo /* 184191ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 184291ccc6e7STejun Heo * workqueues. 184391ccc6e7STejun Heo */ 18441c270b79STejun Heo pwq->nr_active--; 184591ccc6e7STejun Heo 184691ccc6e7STejun Heo /* 184791ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 184891ccc6e7STejun Heo * inactive work item on @pwq itself. 184991ccc6e7STejun Heo */ 185091ccc6e7STejun Heo if (!nna) { 18515797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 185291ccc6e7STejun Heo return; 185391ccc6e7STejun Heo } 185491ccc6e7STejun Heo 18555797b1c1STejun Heo /* 18565797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 18575797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 18585797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 18595797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 18605797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 18615797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 18625797b1c1STejun Heo * decremented $nna->nr. 18635797b1c1STejun Heo * 18645797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 18655797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 18665797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 18675797b1c1STejun Heo * This maintains the forward progress guarantee. 18685797b1c1STejun Heo */ 18695797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 18705797b1c1STejun Heo return; 18715797b1c1STejun Heo 18725797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 18735797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 18743aa62497SLai Jiangshan } 18753aa62497SLai Jiangshan 1876bf4ede01STejun Heo /** 1877112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1878112202d9STejun Heo * @pwq: pwq of interest 1879c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1880bf4ede01STejun Heo * 1881bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1882112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1883bf4ede01STejun Heo * 1884dd6c3c54STejun Heo * NOTE: 1885dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1886dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1887dd6c3c54STejun Heo * work item is complete. 1888dd6c3c54STejun Heo * 1889bf4ede01STejun Heo * CONTEXT: 1890a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 1891bf4ede01STejun Heo */ 1892c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 1893bf4ede01STejun Heo { 1894c4560c2cSLai Jiangshan int color = get_work_color(work_data); 1895c4560c2cSLai Jiangshan 18961c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 18971c270b79STejun Heo pwq_dec_nr_active(pwq); 1898018f3a13SLai Jiangshan 1899018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 1900bf4ede01STejun Heo 1901bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 1902112202d9STejun Heo if (likely(pwq->flush_color != color)) 19038864b4e5STejun Heo goto out_put; 1904bf4ede01STejun Heo 1905bf4ede01STejun Heo /* are there still in-flight works? */ 1906112202d9STejun Heo if (pwq->nr_in_flight[color]) 19078864b4e5STejun Heo goto out_put; 1908bf4ede01STejun Heo 1909112202d9STejun Heo /* this pwq is done, clear flush_color */ 1910112202d9STejun Heo pwq->flush_color = -1; 1911bf4ede01STejun Heo 1912bf4ede01STejun Heo /* 1913112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 1914bf4ede01STejun Heo * will handle the rest. 1915bf4ede01STejun Heo */ 1916112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 1917112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 19188864b4e5STejun Heo out_put: 19198864b4e5STejun Heo put_pwq(pwq); 1920bf4ede01STejun Heo } 1921bf4ede01STejun Heo 192236e227d2STejun Heo /** 1923bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 192436e227d2STejun Heo * @work: work item to steal 192536e227d2STejun Heo * @is_dwork: @work is a delayed_work 1926bbb68dfaSTejun Heo * @flags: place to store irq state 192736e227d2STejun Heo * 192836e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 1929d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 193036e227d2STejun Heo * 1931d185af30SYacine Belkadi * Return: 19323eb6b31bSMauro Carvalho Chehab * 19333eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 193436e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 193536e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 193636e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 1937bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 1938bbb68dfaSTejun Heo * for arbitrarily long 19393eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 194036e227d2STejun Heo * 1941d185af30SYacine Belkadi * Note: 1942bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 1943e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 1944e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 1945e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 1946bbb68dfaSTejun Heo * 1947bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 1948bbb68dfaSTejun Heo * responsible for releasing it using local_irq_restore(*@flags). 1949bbb68dfaSTejun Heo * 1950e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 1951bf4ede01STejun Heo */ 1952bbb68dfaSTejun Heo static int try_to_grab_pending(struct work_struct *work, bool is_dwork, 1953bbb68dfaSTejun Heo unsigned long *flags) 1954bf4ede01STejun Heo { 1955d565ed63STejun Heo struct worker_pool *pool; 1956112202d9STejun Heo struct pool_workqueue *pwq; 1957bf4ede01STejun Heo 1958bbb68dfaSTejun Heo local_irq_save(*flags); 1959bbb68dfaSTejun Heo 196036e227d2STejun Heo /* try to steal the timer if it exists */ 196136e227d2STejun Heo if (is_dwork) { 196236e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 196336e227d2STejun Heo 1964e0aecdd8STejun Heo /* 1965e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 1966e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 1967e0aecdd8STejun Heo * running on the local CPU. 1968e0aecdd8STejun Heo */ 196936e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 197036e227d2STejun Heo return 1; 197136e227d2STejun Heo } 197236e227d2STejun Heo 197336e227d2STejun Heo /* try to claim PENDING the normal way */ 1974bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 1975bf4ede01STejun Heo return 0; 1976bf4ede01STejun Heo 197724acfb71SThomas Gleixner rcu_read_lock(); 1978bf4ede01STejun Heo /* 1979bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 1980bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 1981bf4ede01STejun Heo */ 1982d565ed63STejun Heo pool = get_work_pool(work); 1983d565ed63STejun Heo if (!pool) 1984bbb68dfaSTejun Heo goto fail; 1985bf4ede01STejun Heo 1986a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 1987bf4ede01STejun Heo /* 1988112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 1989112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 1990112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 1991112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 1992112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 19930b3dae68SLai Jiangshan * item is currently queued on that pool. 1994bf4ede01STejun Heo */ 1995112202d9STejun Heo pwq = get_work_pwq(work); 1996112202d9STejun Heo if (pwq && pwq->pool == pool) { 1997bf4ede01STejun Heo debug_work_deactivate(work); 19983aa62497SLai Jiangshan 19993aa62497SLai Jiangshan /* 2000018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2001018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2002018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2003018f3a13SLai Jiangshan * 2004f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2005d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2006f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 200716062836STejun Heo * management later on and cause stall. Make sure the work 200816062836STejun Heo * item is activated before grabbing. 20093aa62497SLai Jiangshan */ 20104c638030STejun Heo pwq_activate_work(pwq, work); 20113aa62497SLai Jiangshan 2012bf4ede01STejun Heo list_del_init(&work->entry); 201336e227d2STejun Heo 2014112202d9STejun Heo /* work->data points to pwq iff queued, point to pool */ 20154468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 20164468a00fSLai Jiangshan 2017dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2018dd6c3c54STejun Heo pwq_dec_nr_in_flight(pwq, *work_data_bits(work)); 2019dd6c3c54STejun Heo 2020a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 202124acfb71SThomas Gleixner rcu_read_unlock(); 202236e227d2STejun Heo return 1; 2023bf4ede01STejun Heo } 2024a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2025bbb68dfaSTejun Heo fail: 202624acfb71SThomas Gleixner rcu_read_unlock(); 2027bbb68dfaSTejun Heo local_irq_restore(*flags); 2028bbb68dfaSTejun Heo if (work_is_canceling(work)) 2029bbb68dfaSTejun Heo return -ENOENT; 2030bbb68dfaSTejun Heo cpu_relax(); 203136e227d2STejun Heo return -EAGAIN; 2032bf4ede01STejun Heo } 2033bf4ede01STejun Heo 2034bf4ede01STejun Heo /** 2035706026c2STejun Heo * insert_work - insert a work into a pool 2036112202d9STejun Heo * @pwq: pwq @work belongs to 20374690c4abSTejun Heo * @work: work to insert 20384690c4abSTejun Heo * @head: insertion point 20394690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 20404690c4abSTejun Heo * 2041112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2042706026c2STejun Heo * work_struct flags. 20434690c4abSTejun Heo * 20444690c4abSTejun Heo * CONTEXT: 2045a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2046365970a1SDavid Howells */ 2047112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2048112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2049b89deed3SOleg Nesterov { 2050fe089f87STejun Heo debug_work_activate(work); 2051e1d8aa9fSFrederic Weisbecker 2052e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2053f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2054e89a85d6SWalter Wu 20554690c4abSTejun Heo /* we own @work, set data and link */ 2056112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 20571a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 20588864b4e5STejun Heo get_pwq(pwq); 2059b89deed3SOleg Nesterov } 2060b89deed3SOleg Nesterov 2061c8efcc25STejun Heo /* 2062c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 20638d03ecfeSTejun Heo * same workqueue. 2064c8efcc25STejun Heo */ 2065c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2066c8efcc25STejun Heo { 2067c8efcc25STejun Heo struct worker *worker; 2068c8efcc25STejun Heo 20698d03ecfeSTejun Heo worker = current_wq_worker(); 2070c8efcc25STejun Heo /* 2071bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 20728d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2073c8efcc25STejun Heo */ 2074112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2075c8efcc25STejun Heo } 2076c8efcc25STejun Heo 2077ef557180SMike Galbraith /* 2078ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2079ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2080ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2081ef557180SMike Galbraith */ 2082ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2083ef557180SMike Galbraith { 2084ef557180SMike Galbraith int new_cpu; 2085ef557180SMike Galbraith 2086f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2087ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2088ef557180SMike Galbraith return cpu; 2089a8ec5880SAmmar Faizi } else { 2090a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2091f303fccbSTejun Heo } 2092f303fccbSTejun Heo 2093ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2094ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2095ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2096ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2097ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2098ef557180SMike Galbraith return cpu; 2099ef557180SMike Galbraith } 2100ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2101ef557180SMike Galbraith 2102ef557180SMike Galbraith return new_cpu; 2103ef557180SMike Galbraith } 2104ef557180SMike Galbraith 2105d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 21061da177e4SLinus Torvalds struct work_struct *work) 21071da177e4SLinus Torvalds { 2108112202d9STejun Heo struct pool_workqueue *pwq; 2109fe089f87STejun Heo struct worker_pool *last_pool, *pool; 21108a2e8e5dSTejun Heo unsigned int work_flags; 2111b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 21128930cabaSTejun Heo 21138930cabaSTejun Heo /* 21148930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 21158930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 21168930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 21178930cabaSTejun Heo * happen with IRQ disabled. 21188930cabaSTejun Heo */ 21198e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 21201da177e4SLinus Torvalds 21211e19ffc6STejun Heo 212233e3f0a3SRichard Clark /* 212333e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 212433e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 212533e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 212633e3f0a3SRichard Clark */ 212733e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 212833e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2129e41e704bSTejun Heo return; 213024acfb71SThomas Gleixner rcu_read_lock(); 21319e8cd2f5STejun Heo retry: 2132aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2133636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2134636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2135ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2136636b927eSTejun Heo else 2137aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2138aa202f1fSHillf Danton } 2139f3421797STejun Heo 2140636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2141fe089f87STejun Heo pool = pwq->pool; 2142fe089f87STejun Heo 214318aa9effSTejun Heo /* 2144c9178087STejun Heo * If @work was previously on a different pool, it might still be 2145c9178087STejun Heo * running there, in which case the work needs to be queued on that 2146c9178087STejun Heo * pool to guarantee non-reentrancy. 214718aa9effSTejun Heo */ 2148c9e7cf27STejun Heo last_pool = get_work_pool(work); 2149fe089f87STejun Heo if (last_pool && last_pool != pool) { 215018aa9effSTejun Heo struct worker *worker; 215118aa9effSTejun Heo 2152a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 215318aa9effSTejun Heo 2154c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 215518aa9effSTejun Heo 2156112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2157c9178087STejun Heo pwq = worker->current_pwq; 2158fe089f87STejun Heo pool = pwq->pool; 2159fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 21608594fadeSLai Jiangshan } else { 216118aa9effSTejun Heo /* meh... not running there, queue here */ 2162a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2163fe089f87STejun Heo raw_spin_lock(&pool->lock); 216418aa9effSTejun Heo } 21658930cabaSTejun Heo } else { 2166fe089f87STejun Heo raw_spin_lock(&pool->lock); 21678930cabaSTejun Heo } 2168502ca9d8STejun Heo 21699e8cd2f5STejun Heo /* 2170636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2171636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2172636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2173636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2174636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 21759e8cd2f5STejun Heo */ 21769e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 21779e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2178fe089f87STejun Heo raw_spin_unlock(&pool->lock); 21799e8cd2f5STejun Heo cpu_relax(); 21809e8cd2f5STejun Heo goto retry; 21819e8cd2f5STejun Heo } 21829e8cd2f5STejun Heo /* oops */ 21839e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 21849e8cd2f5STejun Heo wq->name, cpu); 21859e8cd2f5STejun Heo } 21869e8cd2f5STejun Heo 2187112202d9STejun Heo /* pwq determined, queue */ 2188112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2189502ca9d8STejun Heo 219024acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 219124acfb71SThomas Gleixner goto out; 21921e19ffc6STejun Heo 2193112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2194112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 21951e19ffc6STejun Heo 2196a045a272STejun Heo /* 2197a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2198a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2199a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2200a045a272STejun Heo */ 22015797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2202fe089f87STejun Heo if (list_empty(&pool->worklist)) 2203fe089f87STejun Heo pool->watchdog_ts = jiffies; 2204fe089f87STejun Heo 2205cdadf009STejun Heo trace_workqueue_activate_work(work); 2206fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 22070219a352STejun Heo kick_pool(pool); 22088a2e8e5dSTejun Heo } else { 2209f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2210fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 22118a2e8e5dSTejun Heo } 22121e19ffc6STejun Heo 221324acfb71SThomas Gleixner out: 2214fe089f87STejun Heo raw_spin_unlock(&pool->lock); 221524acfb71SThomas Gleixner rcu_read_unlock(); 22161da177e4SLinus Torvalds } 22171da177e4SLinus Torvalds 22180fcb78c2SRolf Eike Beer /** 2219c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2220c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2221c1a220e7SZhang Rui * @wq: workqueue to use 2222c1a220e7SZhang Rui * @work: work to queue 2223c1a220e7SZhang Rui * 2224c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2225443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2226443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2227854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2228854f5cc5SPaul E. McKenney * online will get a splat. 2229d185af30SYacine Belkadi * 2230d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2231c1a220e7SZhang Rui */ 2232d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2233d4283e93STejun Heo struct work_struct *work) 2234c1a220e7SZhang Rui { 2235d4283e93STejun Heo bool ret = false; 22368930cabaSTejun Heo unsigned long flags; 22378930cabaSTejun Heo 22388930cabaSTejun Heo local_irq_save(flags); 2239c1a220e7SZhang Rui 224022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 22414690c4abSTejun Heo __queue_work(cpu, wq, work); 2242d4283e93STejun Heo ret = true; 2243c1a220e7SZhang Rui } 22448930cabaSTejun Heo 22458930cabaSTejun Heo local_irq_restore(flags); 2246c1a220e7SZhang Rui return ret; 2247c1a220e7SZhang Rui } 2248ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2249c1a220e7SZhang Rui 22508204e0c1SAlexander Duyck /** 2251fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 22528204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 22538204e0c1SAlexander Duyck * 22548204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 22558204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 22568204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 22578204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 22588204e0c1SAlexander Duyck */ 2259fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 22608204e0c1SAlexander Duyck { 22618204e0c1SAlexander Duyck int cpu; 22628204e0c1SAlexander Duyck 22638204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 22648204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 22658204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 22668204e0c1SAlexander Duyck 22678204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 22688204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 22698204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 22708204e0c1SAlexander Duyck return cpu; 22718204e0c1SAlexander Duyck 22728204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 22738204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 22748204e0c1SAlexander Duyck 22758204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 22768204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 22778204e0c1SAlexander Duyck } 22788204e0c1SAlexander Duyck 22798204e0c1SAlexander Duyck /** 22808204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 22818204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 22828204e0c1SAlexander Duyck * @wq: workqueue to use 22838204e0c1SAlexander Duyck * @work: work to queue 22848204e0c1SAlexander Duyck * 22858204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 22868204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 22878204e0c1SAlexander Duyck * NUMA node. 22888204e0c1SAlexander Duyck * 22898204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 22908204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 22918204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 22928204e0c1SAlexander Duyck * 22938204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 22948204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 22958204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 22968204e0c1SAlexander Duyck * 22978204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 22988204e0c1SAlexander Duyck */ 22998204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 23008204e0c1SAlexander Duyck struct work_struct *work) 23018204e0c1SAlexander Duyck { 23028204e0c1SAlexander Duyck unsigned long flags; 23038204e0c1SAlexander Duyck bool ret = false; 23048204e0c1SAlexander Duyck 23058204e0c1SAlexander Duyck /* 23068204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 23078204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 23088204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 23098204e0c1SAlexander Duyck * 23108204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 23118204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 23128204e0c1SAlexander Duyck * some round robin type logic. 23138204e0c1SAlexander Duyck */ 23148204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 23158204e0c1SAlexander Duyck 23168204e0c1SAlexander Duyck local_irq_save(flags); 23178204e0c1SAlexander Duyck 23188204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2319fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 23208204e0c1SAlexander Duyck 23218204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 23228204e0c1SAlexander Duyck ret = true; 23238204e0c1SAlexander Duyck } 23248204e0c1SAlexander Duyck 23258204e0c1SAlexander Duyck local_irq_restore(flags); 23268204e0c1SAlexander Duyck return ret; 23278204e0c1SAlexander Duyck } 23288204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 23298204e0c1SAlexander Duyck 23308c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 23311da177e4SLinus Torvalds { 23328c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 23331da177e4SLinus Torvalds 2334e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 233560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 23361da177e4SLinus Torvalds } 23371438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 23381da177e4SLinus Torvalds 23397beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 234052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 23411da177e4SLinus Torvalds { 23427beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 23437beb2edfSTejun Heo struct work_struct *work = &dwork->work; 23441da177e4SLinus Torvalds 2345637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 23464b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2347fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2348fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 23497beb2edfSTejun Heo 23508852aac2STejun Heo /* 23518852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 23528852aac2STejun Heo * both optimization and correctness. The earliest @timer can 23538852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 23548852aac2STejun Heo * on that there's no such delay when @delay is 0. 23558852aac2STejun Heo */ 23568852aac2STejun Heo if (!delay) { 23578852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 23588852aac2STejun Heo return; 23598852aac2STejun Heo } 23608852aac2STejun Heo 236160c057bcSLai Jiangshan dwork->wq = wq; 23621265057fSTejun Heo dwork->cpu = cpu; 23637beb2edfSTejun Heo timer->expires = jiffies + delay; 23647beb2edfSTejun Heo 2365aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2366aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2367aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2368aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2369aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 23707beb2edfSTejun Heo add_timer_on(timer, cpu); 2371aae17ebbSLeonardo Bras } else { 2372aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2373041bd12eSTejun Heo add_timer(timer); 2374aae17ebbSLeonardo Bras else 2375aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2376aae17ebbSLeonardo Bras } 23777beb2edfSTejun Heo } 23781da177e4SLinus Torvalds 23790fcb78c2SRolf Eike Beer /** 23800fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 23810fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 23820fcb78c2SRolf Eike Beer * @wq: workqueue to use 2383af9997e4SRandy Dunlap * @dwork: work to queue 23840fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 23850fcb78c2SRolf Eike Beer * 2386d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2387715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2388715f1300STejun Heo * execution. 23890fcb78c2SRolf Eike Beer */ 2390d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 239152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 23927a6bc1cdSVenkatesh Pallipadi { 239352bad64dSDavid Howells struct work_struct *work = &dwork->work; 2394d4283e93STejun Heo bool ret = false; 23958930cabaSTejun Heo unsigned long flags; 23968930cabaSTejun Heo 23978930cabaSTejun Heo /* read the comment in __queue_work() */ 23988930cabaSTejun Heo local_irq_save(flags); 23997a6bc1cdSVenkatesh Pallipadi 240022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24017beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2402d4283e93STejun Heo ret = true; 24037a6bc1cdSVenkatesh Pallipadi } 24048930cabaSTejun Heo 24058930cabaSTejun Heo local_irq_restore(flags); 24067a6bc1cdSVenkatesh Pallipadi return ret; 24077a6bc1cdSVenkatesh Pallipadi } 2408ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 24091da177e4SLinus Torvalds 2410c8e55f36STejun Heo /** 24118376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 24128376fe22STejun Heo * @cpu: CPU number to execute work on 24138376fe22STejun Heo * @wq: workqueue to use 24148376fe22STejun Heo * @dwork: work to queue 24158376fe22STejun Heo * @delay: number of jiffies to wait before queueing 24168376fe22STejun Heo * 24178376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 24188376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 24198376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 24208376fe22STejun Heo * current state. 24218376fe22STejun Heo * 2422d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 24238376fe22STejun Heo * pending and its timer was modified. 24248376fe22STejun Heo * 2425e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 24268376fe22STejun Heo * See try_to_grab_pending() for details. 24278376fe22STejun Heo */ 24288376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 24298376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 24308376fe22STejun Heo { 24318376fe22STejun Heo unsigned long flags; 24328376fe22STejun Heo int ret; 24338376fe22STejun Heo 24348376fe22STejun Heo do { 24358376fe22STejun Heo ret = try_to_grab_pending(&dwork->work, true, &flags); 24368376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 24378376fe22STejun Heo 24388376fe22STejun Heo if (likely(ret >= 0)) { 24398376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 24408376fe22STejun Heo local_irq_restore(flags); 24418376fe22STejun Heo } 24428376fe22STejun Heo 24438376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 24448376fe22STejun Heo return ret; 24458376fe22STejun Heo } 24468376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 24478376fe22STejun Heo 244805f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 244905f0fe6bSTejun Heo { 245005f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 245105f0fe6bSTejun Heo 245205f0fe6bSTejun Heo /* read the comment in __queue_work() */ 245305f0fe6bSTejun Heo local_irq_disable(); 245405f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 245505f0fe6bSTejun Heo local_irq_enable(); 245605f0fe6bSTejun Heo } 245705f0fe6bSTejun Heo 245805f0fe6bSTejun Heo /** 245905f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 246005f0fe6bSTejun Heo * @wq: workqueue to use 246105f0fe6bSTejun Heo * @rwork: work to queue 246205f0fe6bSTejun Heo * 246305f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 246405f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2465bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 246605f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 246705f0fe6bSTejun Heo */ 246805f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 246905f0fe6bSTejun Heo { 247005f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 247105f0fe6bSTejun Heo 247205f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 247305f0fe6bSTejun Heo rwork->wq = wq; 2474a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 247505f0fe6bSTejun Heo return true; 247605f0fe6bSTejun Heo } 247705f0fe6bSTejun Heo 247805f0fe6bSTejun Heo return false; 247905f0fe6bSTejun Heo } 248005f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 248105f0fe6bSTejun Heo 2482f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2483c34056a3STejun Heo { 2484c34056a3STejun Heo struct worker *worker; 2485c34056a3STejun Heo 2486f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2487c8e55f36STejun Heo if (worker) { 2488c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2489affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2490da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2491e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2492e22bee78STejun Heo worker->flags = WORKER_PREP; 2493c8e55f36STejun Heo } 2494c34056a3STejun Heo return worker; 2495c34056a3STejun Heo } 2496c34056a3STejun Heo 24979546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 24989546b29eSTejun Heo { 24998639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 25009546b29eSTejun Heo return pool->attrs->__pod_cpumask; 25018639ecebSTejun Heo else 25028639ecebSTejun Heo return pool->attrs->cpumask; 25039546b29eSTejun Heo } 25049546b29eSTejun Heo 2505c34056a3STejun Heo /** 25064736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 25074736cbf7SLai Jiangshan * @worker: worker to be attached 25084736cbf7SLai Jiangshan * @pool: the target pool 25094736cbf7SLai Jiangshan * 25104736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 25114736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 25124736cbf7SLai Jiangshan * cpu-[un]hotplugs. 25134736cbf7SLai Jiangshan */ 25144736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 25154736cbf7SLai Jiangshan struct worker_pool *pool) 25164736cbf7SLai Jiangshan { 25171258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 25184736cbf7SLai Jiangshan 25194736cbf7SLai Jiangshan /* 25201258fae7STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains 25211258fae7STejun Heo * stable across this function. See the comments above the flag 25221258fae7STejun Heo * definition for details. 25234736cbf7SLai Jiangshan */ 25244736cbf7SLai Jiangshan if (pool->flags & POOL_DISASSOCIATED) 25254736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 25265c25b5ffSPeter Zijlstra else 25275c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 25284736cbf7SLai Jiangshan 2529640f17c8SPeter Zijlstra if (worker->rescue_wq) 25309546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2531640f17c8SPeter Zijlstra 25324736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2533a2d812a2STejun Heo worker->pool = pool; 25344736cbf7SLai Jiangshan 25351258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 25364736cbf7SLai Jiangshan } 25374736cbf7SLai Jiangshan 25384736cbf7SLai Jiangshan /** 253960f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 254060f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 254160f5a4bcSLai Jiangshan * 25424736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 25434736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 25444736cbf7SLai Jiangshan * other reference to the pool. 254560f5a4bcSLai Jiangshan */ 2546a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 254760f5a4bcSLai Jiangshan { 2548a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 254960f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 255060f5a4bcSLai Jiangshan 25511258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2552a2d812a2STejun Heo 25535c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2554da028469SLai Jiangshan list_del(&worker->node); 2555a2d812a2STejun Heo worker->pool = NULL; 2556a2d812a2STejun Heo 2557e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 255860f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 25591258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 256060f5a4bcSLai Jiangshan 2561b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2562b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2563b62c0751SLai Jiangshan 256460f5a4bcSLai Jiangshan if (detach_completion) 256560f5a4bcSLai Jiangshan complete(detach_completion); 256660f5a4bcSLai Jiangshan } 256760f5a4bcSLai Jiangshan 256860f5a4bcSLai Jiangshan /** 2569c34056a3STejun Heo * create_worker - create a new workqueue worker 257063d95a91STejun Heo * @pool: pool the new worker will belong to 2571c34056a3STejun Heo * 2572051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2573c34056a3STejun Heo * 2574c34056a3STejun Heo * CONTEXT: 2575c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2576c34056a3STejun Heo * 2577d185af30SYacine Belkadi * Return: 2578c34056a3STejun Heo * Pointer to the newly created worker. 2579c34056a3STejun Heo */ 2580bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2581c34056a3STejun Heo { 2582e441b56fSZhen Lei struct worker *worker; 2583e441b56fSZhen Lei int id; 25845d9c7a1eSLucy Mielke char id_buf[23]; 2585c34056a3STejun Heo 25867cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2587e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 25883f0ea0b8SPetr Mladek if (id < 0) { 25893f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 25903f0ea0b8SPetr Mladek ERR_PTR(id)); 2591e441b56fSZhen Lei return NULL; 25923f0ea0b8SPetr Mladek } 2593c34056a3STejun Heo 2594f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 25953f0ea0b8SPetr Mladek if (!worker) { 25963f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2597c34056a3STejun Heo goto fail; 25983f0ea0b8SPetr Mladek } 2599c34056a3STejun Heo 2600c34056a3STejun Heo worker->id = id; 2601c34056a3STejun Heo 260229c91e99STejun Heo if (pool->cpu >= 0) 2603e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2604e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2605f3421797STejun Heo else 2606e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2607e3c916a4STejun Heo 2608f3f90ad4STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, pool->node, 2609e3c916a4STejun Heo "kworker/%s", id_buf); 26103f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 261160f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 261260f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 261360f54038SPetr Mladek id_buf); 261460f54038SPetr Mladek } else { 26153f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 26163f0ea0b8SPetr Mladek worker->task); 261760f54038SPetr Mladek } 2618c34056a3STejun Heo goto fail; 26193f0ea0b8SPetr Mladek } 2620c34056a3STejun Heo 262191151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 26229546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 262391151228SOleg Nesterov 2624da028469SLai Jiangshan /* successful, attach the worker to the pool */ 26254736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2626822d8405STejun Heo 2627051e1850SLai Jiangshan /* start the newly created worker */ 2628a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 26290219a352STejun Heo 2630051e1850SLai Jiangshan worker->pool->nr_workers++; 2631051e1850SLai Jiangshan worker_enter_idle(worker); 26320219a352STejun Heo 26330219a352STejun Heo /* 26340219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 26356a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 26366a229b0eSTejun Heo * wake it up explicitly. 26370219a352STejun Heo */ 2638051e1850SLai Jiangshan wake_up_process(worker->task); 26390219a352STejun Heo 2640a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2641051e1850SLai Jiangshan 2642c34056a3STejun Heo return worker; 2643822d8405STejun Heo 2644c34056a3STejun Heo fail: 2645e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2646c34056a3STejun Heo kfree(worker); 2647c34056a3STejun Heo return NULL; 2648c34056a3STejun Heo } 2649c34056a3STejun Heo 2650793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2651793777bcSValentin Schneider { 2652793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2653793777bcSValentin Schneider 2654793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2655793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2656793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2657793777bcSValentin Schneider else 2658793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2659793777bcSValentin Schneider } 2660793777bcSValentin Schneider 2661e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2662e02b9312SValentin Schneider { 2663e02b9312SValentin Schneider struct worker *worker, *tmp; 2664e02b9312SValentin Schneider 2665e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2666e02b9312SValentin Schneider list_del_init(&worker->entry); 2667e02b9312SValentin Schneider unbind_worker(worker); 2668e02b9312SValentin Schneider /* 2669e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2670e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2671e02b9312SValentin Schneider * wouldn't have gotten here. 2672c34056a3STejun Heo * 2673e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2674e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2675e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2676e02b9312SValentin Schneider * outside of pool->lock. 2677e02b9312SValentin Schneider */ 2678e02b9312SValentin Schneider wake_up_process(worker->task); 2679e02b9312SValentin Schneider } 2680e02b9312SValentin Schneider } 2681e02b9312SValentin Schneider 2682e02b9312SValentin Schneider /** 2683e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2684e02b9312SValentin Schneider * @worker: worker to be destroyed 2685e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2686e02b9312SValentin Schneider * 2687e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2688e02b9312SValentin Schneider * should be idle. 2689c8e55f36STejun Heo * 2690c8e55f36STejun Heo * CONTEXT: 2691a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2692c34056a3STejun Heo */ 2693e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2694c34056a3STejun Heo { 2695bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2696c34056a3STejun Heo 2697cd549687STejun Heo lockdep_assert_held(&pool->lock); 2698e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2699cd549687STejun Heo 2700c34056a3STejun Heo /* sanity check frenzy */ 27016183c009STejun Heo if (WARN_ON(worker->current_work) || 270273eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 270373eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 27046183c009STejun Heo return; 2705c34056a3STejun Heo 2706bd7bdd43STejun Heo pool->nr_workers--; 2707bd7bdd43STejun Heo pool->nr_idle--; 2708c8e55f36STejun Heo 2709cb444766STejun Heo worker->flags |= WORKER_DIE; 2710e02b9312SValentin Schneider 2711e02b9312SValentin Schneider list_move(&worker->entry, list); 2712e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2713c34056a3STejun Heo } 2714c34056a3STejun Heo 27153f959aa3SValentin Schneider /** 27163f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 27173f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 27183f959aa3SValentin Schneider * 27193f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 27203f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 27213f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 27223f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 27233f959aa3SValentin Schneider * it expire and re-evaluate things from there. 27243f959aa3SValentin Schneider */ 272532a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2726e22bee78STejun Heo { 272732a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 27283f959aa3SValentin Schneider bool do_cull = false; 27293f959aa3SValentin Schneider 27303f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 27313f959aa3SValentin Schneider return; 27323f959aa3SValentin Schneider 27333f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 27343f959aa3SValentin Schneider 27353f959aa3SValentin Schneider if (too_many_workers(pool)) { 27363f959aa3SValentin Schneider struct worker *worker; 27373f959aa3SValentin Schneider unsigned long expires; 27383f959aa3SValentin Schneider 27393f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 27403f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 27413f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 27423f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 27433f959aa3SValentin Schneider 27443f959aa3SValentin Schneider if (!do_cull) 27453f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 27463f959aa3SValentin Schneider } 27473f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 27483f959aa3SValentin Schneider 27493f959aa3SValentin Schneider if (do_cull) 27503f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 27513f959aa3SValentin Schneider } 27523f959aa3SValentin Schneider 27533f959aa3SValentin Schneider /** 27543f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 27553f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 27563f959aa3SValentin Schneider * 27573f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 27583f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2759e02b9312SValentin Schneider * 2760e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2761e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2762e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 27633f959aa3SValentin Schneider */ 27643f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 27653f959aa3SValentin Schneider { 27663f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 27679680540cSYang Yingliang LIST_HEAD(cull_list); 2768e22bee78STejun Heo 2769e02b9312SValentin Schneider /* 2770e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2771e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2772e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2773e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2774e02b9312SValentin Schneider */ 2775e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2776a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2777e22bee78STejun Heo 27783347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2779e22bee78STejun Heo struct worker *worker; 2780e22bee78STejun Heo unsigned long expires; 2781e22bee78STejun Heo 278263d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2783e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2784e22bee78STejun Heo 27853347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 278663d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 27873347fc9fSLai Jiangshan break; 2788e22bee78STejun Heo } 27893347fc9fSLai Jiangshan 2790e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2791e22bee78STejun Heo } 2792e22bee78STejun Heo 2793a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2794e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2795e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2796e22bee78STejun Heo } 2797e22bee78STejun Heo 2798493a1724STejun Heo static void send_mayday(struct work_struct *work) 2799e22bee78STejun Heo { 2800112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2801112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2802493a1724STejun Heo 28032e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2804e22bee78STejun Heo 2805493008a8STejun Heo if (!wq->rescuer) 2806493a1724STejun Heo return; 2807e22bee78STejun Heo 2808e22bee78STejun Heo /* mayday mayday mayday */ 2809493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 281077668c8bSLai Jiangshan /* 281177668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 281277668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 281377668c8bSLai Jiangshan * rescuer is done with it. 281477668c8bSLai Jiangshan */ 281577668c8bSLai Jiangshan get_pwq(pwq); 2816493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2817e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2818725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2819493a1724STejun Heo } 2820e22bee78STejun Heo } 2821e22bee78STejun Heo 282232a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2823e22bee78STejun Heo { 282432a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2825e22bee78STejun Heo struct work_struct *work; 2826e22bee78STejun Heo 2827a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2828a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2829e22bee78STejun Heo 283063d95a91STejun Heo if (need_to_create_worker(pool)) { 2831e22bee78STejun Heo /* 2832e22bee78STejun Heo * We've been trying to create a new worker but 2833e22bee78STejun Heo * haven't been successful. We might be hitting an 2834e22bee78STejun Heo * allocation deadlock. Send distress signals to 2835e22bee78STejun Heo * rescuers. 2836e22bee78STejun Heo */ 283763d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2838e22bee78STejun Heo send_mayday(work); 2839e22bee78STejun Heo } 2840e22bee78STejun Heo 2841a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2842a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2843e22bee78STejun Heo 284463d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2845e22bee78STejun Heo } 2846e22bee78STejun Heo 2847e22bee78STejun Heo /** 2848e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 284963d95a91STejun Heo * @pool: pool to create a new worker for 2850e22bee78STejun Heo * 285163d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2852e22bee78STejun Heo * have at least one idle worker on return from this function. If 2853e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 285463d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2855e22bee78STejun Heo * possible allocation deadlock. 2856e22bee78STejun Heo * 2857c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2858c5aa87bbSTejun Heo * may_start_working() %true. 2859e22bee78STejun Heo * 2860e22bee78STejun Heo * LOCKING: 2861a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2862e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2863e22bee78STejun Heo * manager. 2864e22bee78STejun Heo */ 286529187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2866d565ed63STejun Heo __releases(&pool->lock) 2867d565ed63STejun Heo __acquires(&pool->lock) 2868e22bee78STejun Heo { 2869e22bee78STejun Heo restart: 2870a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 28719f9c2364STejun Heo 2872e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 287363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2874e22bee78STejun Heo 2875e22bee78STejun Heo while (true) { 2876051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 2877e22bee78STejun Heo break; 2878e22bee78STejun Heo 2879e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 28809f9c2364STejun Heo 288163d95a91STejun Heo if (!need_to_create_worker(pool)) 2882e22bee78STejun Heo break; 2883e22bee78STejun Heo } 2884e22bee78STejun Heo 288563d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 2886a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2887051e1850SLai Jiangshan /* 2888051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 2889051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 2890051e1850SLai Jiangshan * already become busy. 2891051e1850SLai Jiangshan */ 289263d95a91STejun Heo if (need_to_create_worker(pool)) 2893e22bee78STejun Heo goto restart; 2894e22bee78STejun Heo } 2895e22bee78STejun Heo 2896e22bee78STejun Heo /** 2897e22bee78STejun Heo * manage_workers - manage worker pool 2898e22bee78STejun Heo * @worker: self 2899e22bee78STejun Heo * 2900706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 2901e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 2902706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 2903e22bee78STejun Heo * 2904e22bee78STejun Heo * The caller can safely start processing works on false return. On 2905e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 2906e22bee78STejun Heo * and may_start_working() is true. 2907e22bee78STejun Heo * 2908e22bee78STejun Heo * CONTEXT: 2909a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2910e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 2911e22bee78STejun Heo * 2912d185af30SYacine Belkadi * Return: 291329187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 291429187a9eSTejun Heo * start processing works, %true if management function was performed and 291529187a9eSTejun Heo * the conditions that the caller verified before calling the function may 291629187a9eSTejun Heo * no longer be true. 2917e22bee78STejun Heo */ 2918e22bee78STejun Heo static bool manage_workers(struct worker *worker) 2919e22bee78STejun Heo { 292063d95a91STejun Heo struct worker_pool *pool = worker->pool; 2921e22bee78STejun Heo 2922692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 292329187a9eSTejun Heo return false; 2924692b4825STejun Heo 2925692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 29262607d7a6STejun Heo pool->manager = worker; 2927e22bee78STejun Heo 292829187a9eSTejun Heo maybe_create_worker(pool); 2929e22bee78STejun Heo 29302607d7a6STejun Heo pool->manager = NULL; 2931692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 2932d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 293329187a9eSTejun Heo return true; 2934e22bee78STejun Heo } 2935e22bee78STejun Heo 2936a62428c0STejun Heo /** 2937a62428c0STejun Heo * process_one_work - process single work 2938c34056a3STejun Heo * @worker: self 2939a62428c0STejun Heo * @work: work to process 2940a62428c0STejun Heo * 2941a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 2942a62428c0STejun Heo * process a single work including synchronization against and 2943a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 2944a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 2945a62428c0STejun Heo * call this function to process a work. 2946a62428c0STejun Heo * 2947a62428c0STejun Heo * CONTEXT: 2948a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 2949a62428c0STejun Heo */ 2950c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 2951d565ed63STejun Heo __releases(&pool->lock) 2952d565ed63STejun Heo __acquires(&pool->lock) 29531da177e4SLinus Torvalds { 2954112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2955bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2956c4560c2cSLai Jiangshan unsigned long work_data; 29574e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 29584e6045f1SJohannes Berg /* 2959a62428c0STejun Heo * It is permissible to free the struct work_struct from 2960a62428c0STejun Heo * inside the function that is called from it, this we need to 2961a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 2962a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 2963a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 29644e6045f1SJohannes Berg */ 29654d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 29664d82a1deSPeter Zijlstra 29674d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 29684e6045f1SJohannes Berg #endif 2969807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 297085327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 2971ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 297225511a47STejun Heo 29738930cabaSTejun Heo /* claim and dequeue */ 2974dc186ad7SThomas Gleixner debug_work_deactivate(work); 2975c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 2976c34056a3STejun Heo worker->current_work = work; 2977a2c1c57bSTejun Heo worker->current_func = work->func; 2978112202d9STejun Heo worker->current_pwq = pwq; 2979616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 2980c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 2981d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 29827a22ad75STejun Heo 29838bf89593STejun Heo /* 29848bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 29858bf89593STejun Heo * overridden through set_worker_desc(). 29868bf89593STejun Heo */ 29878bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 29888bf89593STejun Heo 2989a62428c0STejun Heo list_del_init(&work->entry); 2990a62428c0STejun Heo 2991649027d7STejun Heo /* 2992228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 2993228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 2994228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 2995228f1d00SLai Jiangshan * execution of the pending work items. 2996fb0e7bebSTejun Heo */ 2997616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 2998228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 2999fb0e7bebSTejun Heo 3000974271c4STejun Heo /* 30010219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 30020219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 30030219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 30040219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3005974271c4STejun Heo */ 30060219a352STejun Heo kick_pool(pool); 3007974271c4STejun Heo 30088930cabaSTejun Heo /* 30097c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3010d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 301123657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 301223657bb1STejun Heo * disabled. 30138930cabaSTejun Heo */ 30147c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 30151da177e4SLinus Torvalds 3016fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3017a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3018365970a1SDavid Howells 3019a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 30203295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3021e6f3faa7SPeter Zijlstra /* 3022f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3023f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3024e6f3faa7SPeter Zijlstra * 3025e6f3faa7SPeter Zijlstra * However, that would result in: 3026e6f3faa7SPeter Zijlstra * 3027e6f3faa7SPeter Zijlstra * A(W1) 3028e6f3faa7SPeter Zijlstra * WFC(C) 3029e6f3faa7SPeter Zijlstra * A(W1) 3030e6f3faa7SPeter Zijlstra * C(C) 3031e6f3faa7SPeter Zijlstra * 3032e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3033e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3034e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3035f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3036e6f3faa7SPeter Zijlstra * these locks. 3037e6f3faa7SPeter Zijlstra * 3038e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3039e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3040e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3041e6f3faa7SPeter Zijlstra */ 3042f52be570SPeter Zijlstra lockdep_invariant_state(true); 3043e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3044a2c1c57bSTejun Heo worker->current_func(work); 3045e36c886aSArjan van de Ven /* 3046e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3047e36c886aSArjan van de Ven * point will only record its address. 3048e36c886aSArjan van de Ven */ 30491c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3050725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 30513295f0efSIngo Molnar lock_map_release(&lockdep_map); 3052112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 30531da177e4SLinus Torvalds 30541a65a6d1SXuewen Yan if (unlikely(in_atomic() || lockdep_depth(current) > 0 || 30551a65a6d1SXuewen Yan rcu_preempt_depth() > 0)) { 30561a65a6d1SXuewen Yan pr_err("BUG: workqueue leaked lock or atomic: %s/0x%08x/%d/%d\n" 3057d75f773cSSakari Ailus " last function: %ps\n", 30581a65a6d1SXuewen Yan current->comm, preempt_count(), rcu_preempt_depth(), 30591a65a6d1SXuewen Yan task_pid_nr(current), worker->current_func); 3060d5abe669SPeter Zijlstra debug_show_held_locks(current); 3061d5abe669SPeter Zijlstra dump_stack(); 3062d5abe669SPeter Zijlstra } 3063d5abe669SPeter Zijlstra 3064b22ce278STejun Heo /* 3065025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3066b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3067b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3068b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3069789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3070789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3071b22ce278STejun Heo */ 3072a7e6425eSPaul E. McKenney cond_resched(); 3073b22ce278STejun Heo 3074a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3075a62428c0STejun Heo 3076616db877STejun Heo /* 3077616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3078616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3079616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3080616db877STejun Heo */ 3081fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3082fb0e7bebSTejun Heo 30831b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 30841b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 30851b69ac6bSJohannes Weiner 3086a62428c0STejun Heo /* we're done with it, release */ 308742f8570fSSasha Levin hash_del(&worker->hentry); 3088c34056a3STejun Heo worker->current_work = NULL; 3089a2c1c57bSTejun Heo worker->current_func = NULL; 3090112202d9STejun Heo worker->current_pwq = NULL; 3091d812796eSLai Jiangshan worker->current_color = INT_MAX; 3092dd6c3c54STejun Heo 3093dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3094c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 30951da177e4SLinus Torvalds } 30961da177e4SLinus Torvalds 3097affee4b2STejun Heo /** 3098affee4b2STejun Heo * process_scheduled_works - process scheduled works 3099affee4b2STejun Heo * @worker: self 3100affee4b2STejun Heo * 3101affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3102affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3103affee4b2STejun Heo * fetches a work from the top and executes it. 3104affee4b2STejun Heo * 3105affee4b2STejun Heo * CONTEXT: 3106a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3107affee4b2STejun Heo * multiple times. 3108affee4b2STejun Heo */ 3109affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 31101da177e4SLinus Torvalds { 3111c0ab017dSTejun Heo struct work_struct *work; 3112c0ab017dSTejun Heo bool first = true; 3113c0ab017dSTejun Heo 3114c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3115c0ab017dSTejun Heo struct work_struct, entry))) { 3116c0ab017dSTejun Heo if (first) { 3117c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3118c0ab017dSTejun Heo first = false; 3119c0ab017dSTejun Heo } 3120c34056a3STejun Heo process_one_work(worker, work); 3121a62428c0STejun Heo } 31221da177e4SLinus Torvalds } 31231da177e4SLinus Torvalds 3124197f6accSTejun Heo static void set_pf_worker(bool val) 3125197f6accSTejun Heo { 3126197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3127197f6accSTejun Heo if (val) 3128197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3129197f6accSTejun Heo else 3130197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3131197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3132197f6accSTejun Heo } 3133197f6accSTejun Heo 31344690c4abSTejun Heo /** 31354690c4abSTejun Heo * worker_thread - the worker thread function 3136c34056a3STejun Heo * @__worker: self 31374690c4abSTejun Heo * 3138c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3139c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3140c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3141c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3142c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3143d185af30SYacine Belkadi * 3144d185af30SYacine Belkadi * Return: 0 31454690c4abSTejun Heo */ 3146c34056a3STejun Heo static int worker_thread(void *__worker) 31471da177e4SLinus Torvalds { 3148c34056a3STejun Heo struct worker *worker = __worker; 3149bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 31501da177e4SLinus Torvalds 3151e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3152197f6accSTejun Heo set_pf_worker(true); 3153c8e55f36STejun Heo woke_up: 3154a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3155affee4b2STejun Heo 3156a9ab775bSTejun Heo /* am I supposed to die? */ 3157a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3158a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3159197f6accSTejun Heo set_pf_worker(false); 316060f5a4bcSLai Jiangshan 316160f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3162e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3163a2d812a2STejun Heo worker_detach_from_pool(worker); 3164e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 316560f5a4bcSLai Jiangshan kfree(worker); 3166c8e55f36STejun Heo return 0; 3167c8e55f36STejun Heo } 3168c8e55f36STejun Heo 3169c8e55f36STejun Heo worker_leave_idle(worker); 3170db7bccf4STejun Heo recheck: 3171e22bee78STejun Heo /* no more worker necessary? */ 317263d95a91STejun Heo if (!need_more_worker(pool)) 3173e22bee78STejun Heo goto sleep; 3174e22bee78STejun Heo 3175e22bee78STejun Heo /* do we need to manage? */ 317663d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3177e22bee78STejun Heo goto recheck; 3178e22bee78STejun Heo 3179c8e55f36STejun Heo /* 3180c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3181c8e55f36STejun Heo * preparing to process a work or actually processing it. 3182c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3183c8e55f36STejun Heo */ 31846183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3185c8e55f36STejun Heo 3186e22bee78STejun Heo /* 3187a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3188a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3189a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3190a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3191a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3192e22bee78STejun Heo */ 3193a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3194e22bee78STejun Heo 3195e22bee78STejun Heo do { 3196affee4b2STejun Heo struct work_struct *work = 3197bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3198affee4b2STejun Heo struct work_struct, entry); 3199affee4b2STejun Heo 3200873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3201affee4b2STejun Heo process_scheduled_works(worker); 320263d95a91STejun Heo } while (keep_working(pool)); 3203affee4b2STejun Heo 3204228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3205d313dd85STejun Heo sleep: 3206c8e55f36STejun Heo /* 3207d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3208d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3209d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3210d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3211d565ed63STejun Heo * event. 3212c8e55f36STejun Heo */ 3213c8e55f36STejun Heo worker_enter_idle(worker); 3214c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3215a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 32161da177e4SLinus Torvalds schedule(); 3217c8e55f36STejun Heo goto woke_up; 32181da177e4SLinus Torvalds } 32191da177e4SLinus Torvalds 3220e22bee78STejun Heo /** 3221e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3222111c225aSTejun Heo * @__rescuer: self 3223e22bee78STejun Heo * 3224e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3225493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3226e22bee78STejun Heo * 3227706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3228e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3229e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3230e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3231e22bee78STejun Heo * the problem rescuer solves. 3232e22bee78STejun Heo * 3233706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3234706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3235e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3236e22bee78STejun Heo * 3237e22bee78STejun Heo * This should happen rarely. 3238d185af30SYacine Belkadi * 3239d185af30SYacine Belkadi * Return: 0 3240e22bee78STejun Heo */ 3241111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3242e22bee78STejun Heo { 3243111c225aSTejun Heo struct worker *rescuer = __rescuer; 3244111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 32454d595b86SLai Jiangshan bool should_stop; 3246e22bee78STejun Heo 3247e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3248111c225aSTejun Heo 3249111c225aSTejun Heo /* 3250111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3251111c225aSTejun Heo * doesn't participate in concurrency management. 3252111c225aSTejun Heo */ 3253197f6accSTejun Heo set_pf_worker(true); 3254e22bee78STejun Heo repeat: 3255c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 32561da177e4SLinus Torvalds 32574d595b86SLai Jiangshan /* 32584d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 32594d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 32604d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 32614d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 32624d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 32634d595b86SLai Jiangshan * list is always empty on exit. 32644d595b86SLai Jiangshan */ 32654d595b86SLai Jiangshan should_stop = kthread_should_stop(); 32661da177e4SLinus Torvalds 3267493a1724STejun Heo /* see whether any pwq is asking for help */ 3268a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3269493a1724STejun Heo 3270493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3271493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3272493a1724STejun Heo struct pool_workqueue, mayday_node); 3273112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3274e22bee78STejun Heo struct work_struct *work, *n; 3275e22bee78STejun Heo 3276e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3277493a1724STejun Heo list_del_init(&pwq->mayday_node); 3278493a1724STejun Heo 3279a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3280e22bee78STejun Heo 328151697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 328251697d39SLai Jiangshan 3283a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3284e22bee78STejun Heo 3285e22bee78STejun Heo /* 3286e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3287e22bee78STejun Heo * process'em. 3288e22bee78STejun Heo */ 3289873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 329082607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3291873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3292873eaca6STejun Heo assign_work(work, rescuer, &n)) 3293725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 329482607adcSTejun Heo } 3295e22bee78STejun Heo 3296873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3297e22bee78STejun Heo process_scheduled_works(rescuer); 32987576958aSTejun Heo 32997576958aSTejun Heo /* 3300008847f6SNeilBrown * The above execution of rescued work items could 3301008847f6SNeilBrown * have created more to rescue through 3302f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3303008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3304008847f6SNeilBrown * that such back-to-back work items, which may be 3305008847f6SNeilBrown * being used to relieve memory pressure, don't 3306008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3307008847f6SNeilBrown */ 33084f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3309a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3310e66b39afSTejun Heo /* 3311e66b39afSTejun Heo * Queue iff we aren't racing destruction 3312e66b39afSTejun Heo * and somebody else hasn't queued it already. 3313e66b39afSTejun Heo */ 3314e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3315008847f6SNeilBrown get_pwq(pwq); 3316e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3317e66b39afSTejun Heo } 3318a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3319008847f6SNeilBrown } 3320008847f6SNeilBrown } 3321008847f6SNeilBrown 3322008847f6SNeilBrown /* 332377668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 332413b1d625SLai Jiangshan * go away while we're still attached to it. 332577668c8bSLai Jiangshan */ 332677668c8bSLai Jiangshan put_pwq(pwq); 332777668c8bSLai Jiangshan 332877668c8bSLai Jiangshan /* 33290219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 33300219a352STejun Heo * with 0 concurrency and stalling the execution. 33317576958aSTejun Heo */ 33320219a352STejun Heo kick_pool(pool); 33337576958aSTejun Heo 3334a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 333513b1d625SLai Jiangshan 3336a2d812a2STejun Heo worker_detach_from_pool(rescuer); 333713b1d625SLai Jiangshan 3338a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 33391da177e4SLinus Torvalds } 33401da177e4SLinus Torvalds 3341a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3342493a1724STejun Heo 33434d595b86SLai Jiangshan if (should_stop) { 33444d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3345197f6accSTejun Heo set_pf_worker(false); 33464d595b86SLai Jiangshan return 0; 33474d595b86SLai Jiangshan } 33484d595b86SLai Jiangshan 3349111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3350111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3351e22bee78STejun Heo schedule(); 3352e22bee78STejun Heo goto repeat; 33531da177e4SLinus Torvalds } 33541da177e4SLinus Torvalds 3355fca839c0STejun Heo /** 3356fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3357fca839c0STejun Heo * @target_wq: workqueue being flushed 3358fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3359fca839c0STejun Heo * 3360fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3361fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3362fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3363fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3364fca839c0STejun Heo * a deadlock. 3365fca839c0STejun Heo */ 3366fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3367fca839c0STejun Heo struct work_struct *target_work) 3368fca839c0STejun Heo { 3369fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3370fca839c0STejun Heo struct worker *worker; 3371fca839c0STejun Heo 3372fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3373fca839c0STejun Heo return; 3374fca839c0STejun Heo 3375fca839c0STejun Heo worker = current_wq_worker(); 3376fca839c0STejun Heo 3377fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3378d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3379fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 338023d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 338123d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3382d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3383fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3384fca839c0STejun Heo target_wq->name, target_func); 3385fca839c0STejun Heo } 3386fca839c0STejun Heo 3387fc2e4d70SOleg Nesterov struct wq_barrier { 3388fc2e4d70SOleg Nesterov struct work_struct work; 3389fc2e4d70SOleg Nesterov struct completion done; 33902607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3391fc2e4d70SOleg Nesterov }; 3392fc2e4d70SOleg Nesterov 3393fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3394fc2e4d70SOleg Nesterov { 3395fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3396fc2e4d70SOleg Nesterov complete(&barr->done); 3397fc2e4d70SOleg Nesterov } 3398fc2e4d70SOleg Nesterov 33994690c4abSTejun Heo /** 34004690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3401112202d9STejun Heo * @pwq: pwq to insert barrier into 34024690c4abSTejun Heo * @barr: wq_barrier to insert 3403affee4b2STejun Heo * @target: target work to attach @barr to 3404affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 34054690c4abSTejun Heo * 3406affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3407affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3408affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3409affee4b2STejun Heo * cpu. 3410affee4b2STejun Heo * 3411affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3412affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3413affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3414affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3415affee4b2STejun Heo * after a work with LINKED flag set. 3416affee4b2STejun Heo * 3417affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3418112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 34194690c4abSTejun Heo * 34204690c4abSTejun Heo * CONTEXT: 3421a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 34224690c4abSTejun Heo */ 3423112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3424affee4b2STejun Heo struct wq_barrier *barr, 3425affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3426fc2e4d70SOleg Nesterov { 3427d812796eSLai Jiangshan unsigned int work_flags = 0; 3428d812796eSLai Jiangshan unsigned int work_color; 3429affee4b2STejun Heo struct list_head *head; 3430affee4b2STejun Heo 3431dc186ad7SThomas Gleixner /* 3432d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3433dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3434dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3435dc186ad7SThomas Gleixner * might deadlock. 3436dc186ad7SThomas Gleixner */ 3437ca1cab37SAndrew Morton INIT_WORK_ONSTACK(&barr->work, wq_barrier_func); 343822df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 343952fa5bc5SBoqun Feng 3440fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3441fd1a5b04SByungchul Park 34422607d7a6STejun Heo barr->task = current; 344383c22520SOleg Nesterov 34445797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3445018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3446018f3a13SLai Jiangshan 3447affee4b2STejun Heo /* 3448affee4b2STejun Heo * If @target is currently being executed, schedule the 3449affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3450affee4b2STejun Heo */ 3451d812796eSLai Jiangshan if (worker) { 3452affee4b2STejun Heo head = worker->scheduled.next; 3453d812796eSLai Jiangshan work_color = worker->current_color; 3454d812796eSLai Jiangshan } else { 3455affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3456affee4b2STejun Heo 3457affee4b2STejun Heo head = target->entry.next; 3458affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3459d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3460d812796eSLai Jiangshan work_color = get_work_color(*bits); 3461affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3462affee4b2STejun Heo } 3463affee4b2STejun Heo 3464d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3465d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3466d812796eSLai Jiangshan 3467d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3468fc2e4d70SOleg Nesterov } 3469fc2e4d70SOleg Nesterov 347073f53c4aSTejun Heo /** 3471112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 347273f53c4aSTejun Heo * @wq: workqueue being flushed 347373f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 347473f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 347573f53c4aSTejun Heo * 3476112202d9STejun Heo * Prepare pwqs for workqueue flushing. 347773f53c4aSTejun Heo * 3478112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3479112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3480112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3481112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3482112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 348373f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 348473f53c4aSTejun Heo * 348573f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 348673f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 348773f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 348873f53c4aSTejun Heo * is returned. 348973f53c4aSTejun Heo * 3490112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 349173f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 349273f53c4aSTejun Heo * advanced to @work_color. 349373f53c4aSTejun Heo * 349473f53c4aSTejun Heo * CONTEXT: 34953c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 349673f53c4aSTejun Heo * 3497d185af30SYacine Belkadi * Return: 349873f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 349973f53c4aSTejun Heo * otherwise. 350073f53c4aSTejun Heo */ 3501112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 350273f53c4aSTejun Heo int flush_color, int work_color) 35031da177e4SLinus Torvalds { 350473f53c4aSTejun Heo bool wait = false; 350549e3cf44STejun Heo struct pool_workqueue *pwq; 35061da177e4SLinus Torvalds 350773f53c4aSTejun Heo if (flush_color >= 0) { 35086183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3509112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3510dc186ad7SThomas Gleixner } 351114441960SOleg Nesterov 351249e3cf44STejun Heo for_each_pwq(pwq, wq) { 3513112202d9STejun Heo struct worker_pool *pool = pwq->pool; 35141da177e4SLinus Torvalds 3515a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 351673f53c4aSTejun Heo 351773f53c4aSTejun Heo if (flush_color >= 0) { 35186183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 351973f53c4aSTejun Heo 3520112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3521112202d9STejun Heo pwq->flush_color = flush_color; 3522112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 352373f53c4aSTejun Heo wait = true; 35241da177e4SLinus Torvalds } 352573f53c4aSTejun Heo } 352673f53c4aSTejun Heo 352773f53c4aSTejun Heo if (work_color >= 0) { 35286183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3529112202d9STejun Heo pwq->work_color = work_color; 353073f53c4aSTejun Heo } 353173f53c4aSTejun Heo 3532a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 35331da177e4SLinus Torvalds } 35341da177e4SLinus Torvalds 3535112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 353673f53c4aSTejun Heo complete(&wq->first_flusher->done); 353773f53c4aSTejun Heo 353873f53c4aSTejun Heo return wait; 353983c22520SOleg Nesterov } 35401da177e4SLinus Torvalds 35410fcb78c2SRolf Eike Beer /** 3542c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 35430fcb78c2SRolf Eike Beer * @wq: workqueue to flush 35441da177e4SLinus Torvalds * 3545c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3546c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 35471da177e4SLinus Torvalds */ 3548c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 35491da177e4SLinus Torvalds { 355073f53c4aSTejun Heo struct wq_flusher this_flusher = { 355173f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 355273f53c4aSTejun Heo .flush_color = -1, 3553fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 355473f53c4aSTejun Heo }; 355573f53c4aSTejun Heo int next_color; 3556b1f4ec17SOleg Nesterov 35573347fa09STejun Heo if (WARN_ON(!wq_online)) 35583347fa09STejun Heo return; 35593347fa09STejun Heo 356087915adcSJohannes Berg lock_map_acquire(&wq->lockdep_map); 356187915adcSJohannes Berg lock_map_release(&wq->lockdep_map); 356287915adcSJohannes Berg 35633c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 356473f53c4aSTejun Heo 356573f53c4aSTejun Heo /* 356673f53c4aSTejun Heo * Start-to-wait phase 356773f53c4aSTejun Heo */ 356873f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 356973f53c4aSTejun Heo 357073f53c4aSTejun Heo if (next_color != wq->flush_color) { 357173f53c4aSTejun Heo /* 357273f53c4aSTejun Heo * Color space is not full. The current work_color 357373f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 357473f53c4aSTejun Heo * by one. 357573f53c4aSTejun Heo */ 35766183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 357773f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 357873f53c4aSTejun Heo wq->work_color = next_color; 357973f53c4aSTejun Heo 358073f53c4aSTejun Heo if (!wq->first_flusher) { 358173f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 35826183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 358373f53c4aSTejun Heo 358473f53c4aSTejun Heo wq->first_flusher = &this_flusher; 358573f53c4aSTejun Heo 3586112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 358773f53c4aSTejun Heo wq->work_color)) { 358873f53c4aSTejun Heo /* nothing to flush, done */ 358973f53c4aSTejun Heo wq->flush_color = next_color; 359073f53c4aSTejun Heo wq->first_flusher = NULL; 359173f53c4aSTejun Heo goto out_unlock; 359273f53c4aSTejun Heo } 359373f53c4aSTejun Heo } else { 359473f53c4aSTejun Heo /* wait in queue */ 35956183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 359673f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3597112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 359873f53c4aSTejun Heo } 359973f53c4aSTejun Heo } else { 360073f53c4aSTejun Heo /* 360173f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 360273f53c4aSTejun Heo * The next flush completion will assign us 360373f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 360473f53c4aSTejun Heo */ 360573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 360673f53c4aSTejun Heo } 360773f53c4aSTejun Heo 3608fca839c0STejun Heo check_flush_dependency(wq, NULL); 3609fca839c0STejun Heo 36103c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 361173f53c4aSTejun Heo 361273f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 361373f53c4aSTejun Heo 361473f53c4aSTejun Heo /* 361573f53c4aSTejun Heo * Wake-up-and-cascade phase 361673f53c4aSTejun Heo * 361773f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 361873f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 361973f53c4aSTejun Heo */ 362000d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 362173f53c4aSTejun Heo return; 362273f53c4aSTejun Heo 36233c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 362473f53c4aSTejun Heo 36254ce48b37STejun Heo /* we might have raced, check again with mutex held */ 36264ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 36274ce48b37STejun Heo goto out_unlock; 36284ce48b37STejun Heo 362900d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 363073f53c4aSTejun Heo 36316183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 36326183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 363373f53c4aSTejun Heo 363473f53c4aSTejun Heo while (true) { 363573f53c4aSTejun Heo struct wq_flusher *next, *tmp; 363673f53c4aSTejun Heo 363773f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 363873f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 363973f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 364073f53c4aSTejun Heo break; 364173f53c4aSTejun Heo list_del_init(&next->list); 364273f53c4aSTejun Heo complete(&next->done); 364373f53c4aSTejun Heo } 364473f53c4aSTejun Heo 36456183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 364673f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 364773f53c4aSTejun Heo 364873f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 364973f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 365073f53c4aSTejun Heo 365173f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 365273f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 365373f53c4aSTejun Heo /* 365473f53c4aSTejun Heo * Assign the same color to all overflowed 365573f53c4aSTejun Heo * flushers, advance work_color and append to 365673f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 365773f53c4aSTejun Heo * phase for these overflowed flushers. 365873f53c4aSTejun Heo */ 365973f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 366073f53c4aSTejun Heo tmp->flush_color = wq->work_color; 366173f53c4aSTejun Heo 366273f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 366373f53c4aSTejun Heo 366473f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 366573f53c4aSTejun Heo &wq->flusher_queue); 3666112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 366773f53c4aSTejun Heo } 366873f53c4aSTejun Heo 366973f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 36706183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 367173f53c4aSTejun Heo break; 367273f53c4aSTejun Heo } 367373f53c4aSTejun Heo 367473f53c4aSTejun Heo /* 367573f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3676112202d9STejun Heo * the new first flusher and arm pwqs. 367773f53c4aSTejun Heo */ 36786183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 36796183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 368073f53c4aSTejun Heo 368173f53c4aSTejun Heo list_del_init(&next->list); 368273f53c4aSTejun Heo wq->first_flusher = next; 368373f53c4aSTejun Heo 3684112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 368573f53c4aSTejun Heo break; 368673f53c4aSTejun Heo 368773f53c4aSTejun Heo /* 368873f53c4aSTejun Heo * Meh... this color is already done, clear first 368973f53c4aSTejun Heo * flusher and repeat cascading. 369073f53c4aSTejun Heo */ 369173f53c4aSTejun Heo wq->first_flusher = NULL; 369273f53c4aSTejun Heo } 369373f53c4aSTejun Heo 369473f53c4aSTejun Heo out_unlock: 36953c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 36961da177e4SLinus Torvalds } 3697c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 36981da177e4SLinus Torvalds 36999c5a2ba7STejun Heo /** 37009c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 37019c5a2ba7STejun Heo * @wq: workqueue to drain 37029c5a2ba7STejun Heo * 37039c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 37049c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 37059c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3706b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 37079c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 37089c5a2ba7STejun Heo * takes too long. 37099c5a2ba7STejun Heo */ 37109c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 37119c5a2ba7STejun Heo { 37129c5a2ba7STejun Heo unsigned int flush_cnt = 0; 371349e3cf44STejun Heo struct pool_workqueue *pwq; 37149c5a2ba7STejun Heo 37159c5a2ba7STejun Heo /* 37169c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 37179c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3718618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 37199c5a2ba7STejun Heo */ 372087fc741eSLai Jiangshan mutex_lock(&wq->mutex); 37219c5a2ba7STejun Heo if (!wq->nr_drainers++) 3722618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 372387fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 37249c5a2ba7STejun Heo reflush: 3725c4f135d6STetsuo Handa __flush_workqueue(wq); 37269c5a2ba7STejun Heo 3727b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 372876af4d93STejun Heo 372949e3cf44STejun Heo for_each_pwq(pwq, wq) { 3730fa2563e4SThomas Tuttle bool drained; 37319c5a2ba7STejun Heo 3732a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3733afa87ce8STejun Heo drained = pwq_is_empty(pwq); 3734a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3735fa2563e4SThomas Tuttle 3736fa2563e4SThomas Tuttle if (drained) 37379c5a2ba7STejun Heo continue; 37389c5a2ba7STejun Heo 37399c5a2ba7STejun Heo if (++flush_cnt == 10 || 37409c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3741e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3742e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 374376af4d93STejun Heo 3744b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 37459c5a2ba7STejun Heo goto reflush; 37469c5a2ba7STejun Heo } 37479c5a2ba7STejun Heo 37489c5a2ba7STejun Heo if (!--wq->nr_drainers) 3749618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 375087fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 37519c5a2ba7STejun Heo } 37529c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 37539c5a2ba7STejun Heo 3754d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3755d6e89786SJohannes Berg bool from_cancel) 3756baf59022STejun Heo { 3757baf59022STejun Heo struct worker *worker = NULL; 3758c9e7cf27STejun Heo struct worker_pool *pool; 3759112202d9STejun Heo struct pool_workqueue *pwq; 3760baf59022STejun Heo 3761baf59022STejun Heo might_sleep(); 3762baf59022STejun Heo 376324acfb71SThomas Gleixner rcu_read_lock(); 3764fa1b54e6STejun Heo pool = get_work_pool(work); 3765fa1b54e6STejun Heo if (!pool) { 376624acfb71SThomas Gleixner rcu_read_unlock(); 3767fa1b54e6STejun Heo return false; 3768fa1b54e6STejun Heo } 3769fa1b54e6STejun Heo 3770a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 37710b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3772112202d9STejun Heo pwq = get_work_pwq(work); 3773112202d9STejun Heo if (pwq) { 3774112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3775baf59022STejun Heo goto already_gone; 3776606a5020STejun Heo } else { 3777c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 3778baf59022STejun Heo if (!worker) 3779baf59022STejun Heo goto already_gone; 3780112202d9STejun Heo pwq = worker->current_pwq; 3781606a5020STejun Heo } 3782baf59022STejun Heo 3783fca839c0STejun Heo check_flush_dependency(pwq->wq, work); 3784fca839c0STejun Heo 3785112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 3786a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3787baf59022STejun Heo 3788e159489bSTejun Heo /* 3789a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 3790a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 3791a1d14934SPeter Zijlstra * 3792a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 3793a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 3794a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 3795a1d14934SPeter Zijlstra * forward progress. 3796e159489bSTejun Heo */ 3797d6e89786SJohannes Berg if (!from_cancel && 3798d6e89786SJohannes Berg (pwq->wq->saved_max_active == 1 || pwq->wq->rescuer)) { 3799112202d9STejun Heo lock_map_acquire(&pwq->wq->lockdep_map); 3800112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 3801a1d14934SPeter Zijlstra } 380224acfb71SThomas Gleixner rcu_read_unlock(); 3803baf59022STejun Heo return true; 3804baf59022STejun Heo already_gone: 3805a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 380624acfb71SThomas Gleixner rcu_read_unlock(); 3807baf59022STejun Heo return false; 3808baf59022STejun Heo } 3809baf59022STejun Heo 3810d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 3811d6e89786SJohannes Berg { 3812d6e89786SJohannes Berg struct wq_barrier barr; 3813d6e89786SJohannes Berg 3814d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 3815d6e89786SJohannes Berg return false; 3816d6e89786SJohannes Berg 38174d43d395STetsuo Handa if (WARN_ON(!work->func)) 38184d43d395STetsuo Handa return false; 38194d43d395STetsuo Handa 382087915adcSJohannes Berg lock_map_acquire(&work->lockdep_map); 382187915adcSJohannes Berg lock_map_release(&work->lockdep_map); 382287915adcSJohannes Berg 3823d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 3824d6e89786SJohannes Berg wait_for_completion(&barr.done); 3825d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 3826d6e89786SJohannes Berg return true; 3827d6e89786SJohannes Berg } else { 3828d6e89786SJohannes Berg return false; 3829d6e89786SJohannes Berg } 3830d6e89786SJohannes Berg } 3831d6e89786SJohannes Berg 3832db700897SOleg Nesterov /** 3833401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 3834401a8d04STejun Heo * @work: the work to flush 3835db700897SOleg Nesterov * 3836606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 3837606a5020STejun Heo * on return if it hasn't been requeued since flush started. 3838401a8d04STejun Heo * 3839d185af30SYacine Belkadi * Return: 3840401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3841401a8d04STejun Heo * %false if it was already idle. 3842db700897SOleg Nesterov */ 3843401a8d04STejun Heo bool flush_work(struct work_struct *work) 3844db700897SOleg Nesterov { 3845d6e89786SJohannes Berg return __flush_work(work, false); 3846606a5020STejun Heo } 3847db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 3848db700897SOleg Nesterov 38498603e1b3STejun Heo struct cwt_wait { 3850ac6424b9SIngo Molnar wait_queue_entry_t wait; 38518603e1b3STejun Heo struct work_struct *work; 38528603e1b3STejun Heo }; 38538603e1b3STejun Heo 3854ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 38558603e1b3STejun Heo { 38568603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 38578603e1b3STejun Heo 38588603e1b3STejun Heo if (cwait->work != key) 38598603e1b3STejun Heo return 0; 38608603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 38618603e1b3STejun Heo } 38628603e1b3STejun Heo 386336e227d2STejun Heo static bool __cancel_work_timer(struct work_struct *work, bool is_dwork) 3864401a8d04STejun Heo { 38658603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 3866bbb68dfaSTejun Heo unsigned long flags; 38671f1f642eSOleg Nesterov int ret; 38681f1f642eSOleg Nesterov 38691f1f642eSOleg Nesterov do { 3870bbb68dfaSTejun Heo ret = try_to_grab_pending(work, is_dwork, &flags); 3871bbb68dfaSTejun Heo /* 38728603e1b3STejun Heo * If someone else is already canceling, wait for it to 38738603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 38748603e1b3STejun Heo * because we may get scheduled between @work's completion 38758603e1b3STejun Heo * and the other canceling task resuming and clearing 38768603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 38778603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 38788603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 38798603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 38808603e1b3STejun Heo * we're hogging the CPU. 38818603e1b3STejun Heo * 38828603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 38838603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 38848603e1b3STejun Heo * wake function which matches @work along with exclusive 38858603e1b3STejun Heo * wait and wakeup. 3886bbb68dfaSTejun Heo */ 38878603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 38888603e1b3STejun Heo struct cwt_wait cwait; 38898603e1b3STejun Heo 38908603e1b3STejun Heo init_wait(&cwait.wait); 38918603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 38928603e1b3STejun Heo cwait.work = work; 38938603e1b3STejun Heo 38948603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 38958603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 38968603e1b3STejun Heo if (work_is_canceling(work)) 38978603e1b3STejun Heo schedule(); 38988603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 38998603e1b3STejun Heo } 39001f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 39011f1f642eSOleg Nesterov 3902bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 3903bbb68dfaSTejun Heo mark_work_canceling(work); 3904bbb68dfaSTejun Heo local_irq_restore(flags); 3905bbb68dfaSTejun Heo 39063347fa09STejun Heo /* 39073347fa09STejun Heo * This allows canceling during early boot. We know that @work 39083347fa09STejun Heo * isn't executing. 39093347fa09STejun Heo */ 39103347fa09STejun Heo if (wq_online) 3911d6e89786SJohannes Berg __flush_work(work, true); 39123347fa09STejun Heo 39137a22ad75STejun Heo clear_work_data(work); 39148603e1b3STejun Heo 39158603e1b3STejun Heo /* 39168603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 39178603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 39188603e1b3STejun Heo * visible there. 39198603e1b3STejun Heo */ 39208603e1b3STejun Heo smp_mb(); 39218603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 39228603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 39238603e1b3STejun Heo 39241f1f642eSOleg Nesterov return ret; 39251f1f642eSOleg Nesterov } 39261f1f642eSOleg Nesterov 39276e84d644SOleg Nesterov /** 3928401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 3929401a8d04STejun Heo * @work: the work to cancel 39306e84d644SOleg Nesterov * 3931401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 3932401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 3933401a8d04STejun Heo * another workqueue. On return from this function, @work is 3934401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 39351f1f642eSOleg Nesterov * 3936401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 3937401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 39386e84d644SOleg Nesterov * 3939401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 39406e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 3941401a8d04STejun Heo * 3942d185af30SYacine Belkadi * Return: 3943401a8d04STejun Heo * %true if @work was pending, %false otherwise. 39446e84d644SOleg Nesterov */ 3945401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 39466e84d644SOleg Nesterov { 394736e227d2STejun Heo return __cancel_work_timer(work, false); 3948b89deed3SOleg Nesterov } 394928e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 3950b89deed3SOleg Nesterov 39516e84d644SOleg Nesterov /** 3952401a8d04STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 3953401a8d04STejun Heo * @dwork: the delayed work to flush 39546e84d644SOleg Nesterov * 3955401a8d04STejun Heo * Delayed timer is cancelled and the pending work is queued for 3956401a8d04STejun Heo * immediate execution. Like flush_work(), this function only 3957401a8d04STejun Heo * considers the last queueing instance of @dwork. 39581f1f642eSOleg Nesterov * 3959d185af30SYacine Belkadi * Return: 3960401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 3961401a8d04STejun Heo * %false if it was already idle. 39626e84d644SOleg Nesterov */ 3963401a8d04STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 3964401a8d04STejun Heo { 39658930cabaSTejun Heo local_irq_disable(); 3966401a8d04STejun Heo if (del_timer_sync(&dwork->timer)) 396760c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 39688930cabaSTejun Heo local_irq_enable(); 3969401a8d04STejun Heo return flush_work(&dwork->work); 3970401a8d04STejun Heo } 3971401a8d04STejun Heo EXPORT_SYMBOL(flush_delayed_work); 3972401a8d04STejun Heo 397305f0fe6bSTejun Heo /** 397405f0fe6bSTejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 397505f0fe6bSTejun Heo * @rwork: the rcu work to flush 397605f0fe6bSTejun Heo * 397705f0fe6bSTejun Heo * Return: 397805f0fe6bSTejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 397905f0fe6bSTejun Heo * %false if it was already idle. 398005f0fe6bSTejun Heo */ 398105f0fe6bSTejun Heo bool flush_rcu_work(struct rcu_work *rwork) 398205f0fe6bSTejun Heo { 398305f0fe6bSTejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 398405f0fe6bSTejun Heo rcu_barrier(); 398505f0fe6bSTejun Heo flush_work(&rwork->work); 398605f0fe6bSTejun Heo return true; 398705f0fe6bSTejun Heo } else { 398805f0fe6bSTejun Heo return flush_work(&rwork->work); 398905f0fe6bSTejun Heo } 399005f0fe6bSTejun Heo } 399105f0fe6bSTejun Heo EXPORT_SYMBOL(flush_rcu_work); 399205f0fe6bSTejun Heo 3993f72b8792SJens Axboe static bool __cancel_work(struct work_struct *work, bool is_dwork) 3994f72b8792SJens Axboe { 3995f72b8792SJens Axboe unsigned long flags; 3996f72b8792SJens Axboe int ret; 3997f72b8792SJens Axboe 3998f72b8792SJens Axboe do { 3999f72b8792SJens Axboe ret = try_to_grab_pending(work, is_dwork, &flags); 4000f72b8792SJens Axboe } while (unlikely(ret == -EAGAIN)); 4001f72b8792SJens Axboe 4002f72b8792SJens Axboe if (unlikely(ret < 0)) 4003f72b8792SJens Axboe return false; 4004f72b8792SJens Axboe 4005f72b8792SJens Axboe set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 4006f72b8792SJens Axboe local_irq_restore(flags); 4007f72b8792SJens Axboe return ret; 4008f72b8792SJens Axboe } 4009f72b8792SJens Axboe 401073b4b532SAndrey Grodzovsky /* 401173b4b532SAndrey Grodzovsky * See cancel_delayed_work() 401273b4b532SAndrey Grodzovsky */ 401373b4b532SAndrey Grodzovsky bool cancel_work(struct work_struct *work) 401473b4b532SAndrey Grodzovsky { 401573b4b532SAndrey Grodzovsky return __cancel_work(work, false); 401673b4b532SAndrey Grodzovsky } 401773b4b532SAndrey Grodzovsky EXPORT_SYMBOL(cancel_work); 401873b4b532SAndrey Grodzovsky 4019401a8d04STejun Heo /** 402057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 402157b30ae7STejun Heo * @dwork: delayed_work to cancel 402209383498STejun Heo * 4023d185af30SYacine Belkadi * Kill off a pending delayed_work. 4024d185af30SYacine Belkadi * 4025d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4026d185af30SYacine Belkadi * pending. 4027d185af30SYacine Belkadi * 4028d185af30SYacine Belkadi * Note: 4029d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4030d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4031d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 403209383498STejun Heo * 403357b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 403409383498STejun Heo */ 403557b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 403609383498STejun Heo { 4037f72b8792SJens Axboe return __cancel_work(&dwork->work, true); 403809383498STejun Heo } 403957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 404009383498STejun Heo 404109383498STejun Heo /** 4042401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4043401a8d04STejun Heo * @dwork: the delayed work cancel 4044401a8d04STejun Heo * 4045401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4046401a8d04STejun Heo * 4047d185af30SYacine Belkadi * Return: 4048401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4049401a8d04STejun Heo */ 4050401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 40516e84d644SOleg Nesterov { 405236e227d2STejun Heo return __cancel_work_timer(&dwork->work, true); 40536e84d644SOleg Nesterov } 4054f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 40551da177e4SLinus Torvalds 40560fcb78c2SRolf Eike Beer /** 405731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4058b6136773SAndrew Morton * @func: the function to call 4059b6136773SAndrew Morton * 406031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 406131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4062b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 406331ddd871STejun Heo * 4064d185af30SYacine Belkadi * Return: 406531ddd871STejun Heo * 0 on success, -errno on failure. 4066b6136773SAndrew Morton */ 406765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 406815316ba8SChristoph Lameter { 406915316ba8SChristoph Lameter int cpu; 407038f51568SNamhyung Kim struct work_struct __percpu *works; 407115316ba8SChristoph Lameter 4072b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4073b6136773SAndrew Morton if (!works) 407415316ba8SChristoph Lameter return -ENOMEM; 4075b6136773SAndrew Morton 4076ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 407793981800STejun Heo 407815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 40799bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 40809bfb1839SIngo Molnar 40819bfb1839SIngo Molnar INIT_WORK(work, func); 40828de6d308SOleg Nesterov schedule_work_on(cpu, work); 408315316ba8SChristoph Lameter } 408493981800STejun Heo 408593981800STejun Heo for_each_online_cpu(cpu) 40868616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 408793981800STejun Heo 4088ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4089b6136773SAndrew Morton free_percpu(works); 409015316ba8SChristoph Lameter return 0; 409115316ba8SChristoph Lameter } 409215316ba8SChristoph Lameter 4093eef6a7d5SAlan Stern /** 40941fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 40951fa44ecaSJames Bottomley * @fn: the function to execute 40961fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 40971fa44ecaSJames Bottomley * be available when the work executes) 40981fa44ecaSJames Bottomley * 40991fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 41001fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 41011fa44ecaSJames Bottomley * 4102d185af30SYacine Belkadi * Return: 0 - function was executed 41031fa44ecaSJames Bottomley * 1 - function was scheduled for execution 41041fa44ecaSJames Bottomley */ 410565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 41061fa44ecaSJames Bottomley { 41071fa44ecaSJames Bottomley if (!in_interrupt()) { 410865f27f38SDavid Howells fn(&ew->work); 41091fa44ecaSJames Bottomley return 0; 41101fa44ecaSJames Bottomley } 41111fa44ecaSJames Bottomley 411265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 41131fa44ecaSJames Bottomley schedule_work(&ew->work); 41141fa44ecaSJames Bottomley 41151fa44ecaSJames Bottomley return 1; 41161fa44ecaSJames Bottomley } 41171fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 41181fa44ecaSJames Bottomley 41197a4e344cSTejun Heo /** 41207a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 41217a4e344cSTejun Heo * @attrs: workqueue_attrs to free 41227a4e344cSTejun Heo * 41237a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 41247a4e344cSTejun Heo */ 4125513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 41267a4e344cSTejun Heo { 41277a4e344cSTejun Heo if (attrs) { 41287a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 41299546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 41307a4e344cSTejun Heo kfree(attrs); 41317a4e344cSTejun Heo } 41327a4e344cSTejun Heo } 41337a4e344cSTejun Heo 41347a4e344cSTejun Heo /** 41357a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 41367a4e344cSTejun Heo * 41377a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4138d185af30SYacine Belkadi * return it. 4139d185af30SYacine Belkadi * 4140d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 41417a4e344cSTejun Heo */ 4142513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 41437a4e344cSTejun Heo { 41447a4e344cSTejun Heo struct workqueue_attrs *attrs; 41457a4e344cSTejun Heo 4146be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 41477a4e344cSTejun Heo if (!attrs) 41487a4e344cSTejun Heo goto fail; 4149be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 41507a4e344cSTejun Heo goto fail; 41519546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 41529546b29eSTejun Heo goto fail; 41537a4e344cSTejun Heo 415413e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4155523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 41567a4e344cSTejun Heo return attrs; 41577a4e344cSTejun Heo fail: 41587a4e344cSTejun Heo free_workqueue_attrs(attrs); 41597a4e344cSTejun Heo return NULL; 41607a4e344cSTejun Heo } 41617a4e344cSTejun Heo 416229c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 416329c91e99STejun Heo const struct workqueue_attrs *from) 416429c91e99STejun Heo { 416529c91e99STejun Heo to->nice = from->nice; 416629c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 41679546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 41688639ecebSTejun Heo to->affn_strict = from->affn_strict; 416984193c07STejun Heo 41702865a8fbSShaohua Li /* 417184193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 417284193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 417384193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 41742865a8fbSShaohua Li */ 417584193c07STejun Heo to->affn_scope = from->affn_scope; 4176af73f5c9STejun Heo to->ordered = from->ordered; 417729c91e99STejun Heo } 417829c91e99STejun Heo 41795de7a03cSTejun Heo /* 41805de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 41815de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 41825de7a03cSTejun Heo */ 41835de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 41845de7a03cSTejun Heo { 418584193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 41865de7a03cSTejun Heo attrs->ordered = false; 41875de7a03cSTejun Heo } 41885de7a03cSTejun Heo 418929c91e99STejun Heo /* hash value of the content of @attr */ 419029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 419129c91e99STejun Heo { 419229c91e99STejun Heo u32 hash = 0; 419329c91e99STejun Heo 419429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 419513e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 419613e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 41979546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 41989546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 41998639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 420029c91e99STejun Heo return hash; 420129c91e99STejun Heo } 420229c91e99STejun Heo 420329c91e99STejun Heo /* content equality test */ 420429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 420529c91e99STejun Heo const struct workqueue_attrs *b) 420629c91e99STejun Heo { 420729c91e99STejun Heo if (a->nice != b->nice) 420829c91e99STejun Heo return false; 420929c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 421029c91e99STejun Heo return false; 42119546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 42129546b29eSTejun Heo return false; 42138639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 42148639ecebSTejun Heo return false; 421529c91e99STejun Heo return true; 421629c91e99STejun Heo } 421729c91e99STejun Heo 42180f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 42190f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 42200f36ee24STejun Heo const cpumask_t *unbound_cpumask) 42210f36ee24STejun Heo { 42220f36ee24STejun Heo /* 42230f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 42240f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 42250f36ee24STejun Heo * @unbound_cpumask. 42260f36ee24STejun Heo */ 42270f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 42280f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 42290f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 42300f36ee24STejun Heo } 42310f36ee24STejun Heo 423284193c07STejun Heo /* find wq_pod_type to use for @attrs */ 423384193c07STejun Heo static const struct wq_pod_type * 423484193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 423584193c07STejun Heo { 4236523a301eSTejun Heo enum wq_affn_scope scope; 4237523a301eSTejun Heo struct wq_pod_type *pt; 4238523a301eSTejun Heo 4239523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4240523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4241523a301eSTejun Heo 4242523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4243523a301eSTejun Heo scope = wq_affn_dfl; 4244523a301eSTejun Heo else 4245523a301eSTejun Heo scope = attrs->affn_scope; 4246523a301eSTejun Heo 4247523a301eSTejun Heo pt = &wq_pod_types[scope]; 424884193c07STejun Heo 424984193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 425084193c07STejun Heo likely(pt->nr_pods)) 425184193c07STejun Heo return pt; 425284193c07STejun Heo 425384193c07STejun Heo /* 425484193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 425584193c07STejun Heo * initialized in workqueue_init_early(). 425684193c07STejun Heo */ 425784193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 425884193c07STejun Heo BUG_ON(!pt->nr_pods); 425984193c07STejun Heo return pt; 426084193c07STejun Heo } 426184193c07STejun Heo 42627a4e344cSTejun Heo /** 42637a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 42647a4e344cSTejun Heo * @pool: worker_pool to initialize 42657a4e344cSTejun Heo * 4266402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4267d185af30SYacine Belkadi * 4268d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 426929c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 427029c91e99STejun Heo * on @pool safely to release it. 42717a4e344cSTejun Heo */ 42727a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 42734e1a1f9aSTejun Heo { 4274a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 427529c91e99STejun Heo pool->id = -1; 427629c91e99STejun Heo pool->cpu = -1; 4277f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 42784e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 427982607adcSTejun Heo pool->watchdog_ts = jiffies; 42804e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 42814e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 42824e1a1f9aSTejun Heo hash_init(pool->busy_hash); 42834e1a1f9aSTejun Heo 428432a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 42853f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 42864e1a1f9aSTejun Heo 428732a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 42884e1a1f9aSTejun Heo 4289da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4290e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 42917a4e344cSTejun Heo 42927cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 429329c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 429429c91e99STejun Heo pool->refcnt = 1; 429529c91e99STejun Heo 429629c91e99STejun Heo /* shouldn't fail above this point */ 4297be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 42987a4e344cSTejun Heo if (!pool->attrs) 42997a4e344cSTejun Heo return -ENOMEM; 43005de7a03cSTejun Heo 43015de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 43025de7a03cSTejun Heo 43037a4e344cSTejun Heo return 0; 43044e1a1f9aSTejun Heo } 43054e1a1f9aSTejun Heo 4306669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4307669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4308669de8bdSBart Van Assche { 4309669de8bdSBart Van Assche char *lock_name; 4310669de8bdSBart Van Assche 4311669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4312669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4313669de8bdSBart Van Assche if (!lock_name) 4314669de8bdSBart Van Assche lock_name = wq->name; 431569a106c0SQian Cai 431669a106c0SQian Cai wq->lock_name = lock_name; 4317669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4318669de8bdSBart Van Assche } 4319669de8bdSBart Van Assche 4320669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4321669de8bdSBart Van Assche { 4322669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4323669de8bdSBart Van Assche } 4324669de8bdSBart Van Assche 4325669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4326669de8bdSBart Van Assche { 4327669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4328669de8bdSBart Van Assche kfree(wq->lock_name); 4329669de8bdSBart Van Assche } 4330669de8bdSBart Van Assche #else 4331669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4332669de8bdSBart Van Assche { 4333669de8bdSBart Van Assche } 4334669de8bdSBart Van Assche 4335669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4336669de8bdSBart Van Assche { 4337669de8bdSBart Van Assche } 4338669de8bdSBart Van Assche 4339669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4340669de8bdSBart Van Assche { 4341669de8bdSBart Van Assche } 4342669de8bdSBart Van Assche #endif 4343669de8bdSBart Van Assche 434491ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 434591ccc6e7STejun Heo { 434691ccc6e7STejun Heo int node; 434791ccc6e7STejun Heo 434891ccc6e7STejun Heo for_each_node(node) { 434991ccc6e7STejun Heo kfree(nna_ar[node]); 435091ccc6e7STejun Heo nna_ar[node] = NULL; 435191ccc6e7STejun Heo } 435291ccc6e7STejun Heo 435391ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 435491ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 435591ccc6e7STejun Heo } 435691ccc6e7STejun Heo 435791ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 435891ccc6e7STejun Heo { 435991ccc6e7STejun Heo atomic_set(&nna->nr, 0); 43605797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 43615797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 436291ccc6e7STejun Heo } 436391ccc6e7STejun Heo 436491ccc6e7STejun Heo /* 436591ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 436691ccc6e7STejun Heo * should be allocated in the node. 436791ccc6e7STejun Heo */ 436891ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 436991ccc6e7STejun Heo { 437091ccc6e7STejun Heo struct wq_node_nr_active *nna; 437191ccc6e7STejun Heo int node; 437291ccc6e7STejun Heo 437391ccc6e7STejun Heo for_each_node(node) { 437491ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 437591ccc6e7STejun Heo if (!nna) 437691ccc6e7STejun Heo goto err_free; 437791ccc6e7STejun Heo init_node_nr_active(nna); 437891ccc6e7STejun Heo nna_ar[node] = nna; 437991ccc6e7STejun Heo } 438091ccc6e7STejun Heo 438191ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 438291ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 438391ccc6e7STejun Heo if (!nna) 438491ccc6e7STejun Heo goto err_free; 438591ccc6e7STejun Heo init_node_nr_active(nna); 438691ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 438791ccc6e7STejun Heo 438891ccc6e7STejun Heo return 0; 438991ccc6e7STejun Heo 439091ccc6e7STejun Heo err_free: 439191ccc6e7STejun Heo free_node_nr_active(nna_ar); 439291ccc6e7STejun Heo return -ENOMEM; 439391ccc6e7STejun Heo } 439491ccc6e7STejun Heo 4395e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4396e2dca7adSTejun Heo { 4397e2dca7adSTejun Heo struct workqueue_struct *wq = 4398e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4399e2dca7adSTejun Heo 440091ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 440191ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 440291ccc6e7STejun Heo 4403669de8bdSBart Van Assche wq_free_lockdep(wq); 4404ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4405e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4406e2dca7adSTejun Heo kfree(wq); 4407e2dca7adSTejun Heo } 4408e2dca7adSTejun Heo 440929c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 441029c91e99STejun Heo { 441129c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 441229c91e99STejun Heo 44137cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 441429c91e99STejun Heo free_workqueue_attrs(pool->attrs); 441529c91e99STejun Heo kfree(pool); 441629c91e99STejun Heo } 441729c91e99STejun Heo 441829c91e99STejun Heo /** 441929c91e99STejun Heo * put_unbound_pool - put a worker_pool 442029c91e99STejun Heo * @pool: worker_pool to put 442129c91e99STejun Heo * 442224acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4423c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4424c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4425c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4426a892caccSTejun Heo * 4427a892caccSTejun Heo * Should be called with wq_pool_mutex held. 442829c91e99STejun Heo */ 442929c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 443029c91e99STejun Heo { 443160f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 443229c91e99STejun Heo struct worker *worker; 44339680540cSYang Yingliang LIST_HEAD(cull_list); 4434e02b9312SValentin Schneider 4435a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4436a892caccSTejun Heo 4437a892caccSTejun Heo if (--pool->refcnt) 443829c91e99STejun Heo return; 443929c91e99STejun Heo 444029c91e99STejun Heo /* sanity checks */ 444161d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4442a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 444329c91e99STejun Heo return; 444429c91e99STejun Heo 444529c91e99STejun Heo /* release id and unhash */ 444629c91e99STejun Heo if (pool->id >= 0) 444729c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 444829c91e99STejun Heo hash_del(&pool->hash_node); 444929c91e99STejun Heo 4450c5aa87bbSTejun Heo /* 4451692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4452692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4453692b4825STejun Heo * manager and @pool gets freed with the flag set. 44549ab03be4SValentin Schneider * 44559ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 44569ab03be4SValentin Schneider * only get here with 44579ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 44589ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 44599ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 44609ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 44619ab03be4SValentin Schneider * drops pool->lock 4462c5aa87bbSTejun Heo */ 44639ab03be4SValentin Schneider while (true) { 44649ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 44659ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4466d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4467e02b9312SValentin Schneider 4468e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 44699ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 44709ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4471692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 44729ab03be4SValentin Schneider break; 44739ab03be4SValentin Schneider } 44749ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4475e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 44769ab03be4SValentin Schneider } 4477692b4825STejun Heo 44781037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4479e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 448029c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4481a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 448260f5a4bcSLai Jiangshan 4483e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4484e02b9312SValentin Schneider 4485e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 448660f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 44871258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 448860f5a4bcSLai Jiangshan 448960f5a4bcSLai Jiangshan if (pool->detach_completion) 449060f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 449160f5a4bcSLai Jiangshan 449229c91e99STejun Heo /* shut down the timers */ 449329c91e99STejun Heo del_timer_sync(&pool->idle_timer); 44943f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 449529c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 449629c91e99STejun Heo 449724acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 449825b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 449929c91e99STejun Heo } 450029c91e99STejun Heo 450129c91e99STejun Heo /** 450229c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 450329c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 450429c91e99STejun Heo * 450529c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 450629c91e99STejun Heo * reference count and return it. If there already is a matching 450729c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4508d185af30SYacine Belkadi * create a new one. 4509a892caccSTejun Heo * 4510a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4511d185af30SYacine Belkadi * 4512d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4513d185af30SYacine Belkadi * On failure, %NULL. 451429c91e99STejun Heo */ 451529c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 451629c91e99STejun Heo { 451784193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 451829c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 451929c91e99STejun Heo struct worker_pool *pool; 452084193c07STejun Heo int pod, node = NUMA_NO_NODE; 452129c91e99STejun Heo 4522a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 452329c91e99STejun Heo 452429c91e99STejun Heo /* do we already have a matching pool? */ 452529c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 452629c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 452729c91e99STejun Heo pool->refcnt++; 45283fb1823cSLai Jiangshan return pool; 452929c91e99STejun Heo } 453029c91e99STejun Heo } 453129c91e99STejun Heo 45329546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 453384193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 45349546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 453584193c07STejun Heo node = pt->pod_node[pod]; 4536e2273584SXunlei Pang break; 4537e2273584SXunlei Pang } 4538e2273584SXunlei Pang } 4539e2273584SXunlei Pang 454029c91e99STejun Heo /* nope, create a new one */ 454184193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 454229c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 454329c91e99STejun Heo goto fail; 454429c91e99STejun Heo 454584193c07STejun Heo pool->node = node; 45465de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 45475de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 45482865a8fbSShaohua Li 454929c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 455029c91e99STejun Heo goto fail; 455129c91e99STejun Heo 455229c91e99STejun Heo /* create and start the initial worker */ 45533347fa09STejun Heo if (wq_online && !create_worker(pool)) 455429c91e99STejun Heo goto fail; 455529c91e99STejun Heo 455629c91e99STejun Heo /* install */ 455729c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 45583fb1823cSLai Jiangshan 455929c91e99STejun Heo return pool; 456029c91e99STejun Heo fail: 456129c91e99STejun Heo if (pool) 456229c91e99STejun Heo put_unbound_pool(pool); 456329c91e99STejun Heo return NULL; 456429c91e99STejun Heo } 456529c91e99STejun Heo 45668864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 45678864b4e5STejun Heo { 45688864b4e5STejun Heo kmem_cache_free(pwq_cache, 45698864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 45708864b4e5STejun Heo } 45718864b4e5STejun Heo 45728864b4e5STejun Heo /* 4573967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4574967b494eSTejun Heo * refcnt and needs to be destroyed. 45758864b4e5STejun Heo */ 4576687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 45778864b4e5STejun Heo { 45788864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4579687a9aa5STejun Heo release_work); 45808864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 45818864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4582b42b0bddSYang Yingliang bool is_last = false; 45838864b4e5STejun Heo 4584b42b0bddSYang Yingliang /* 4585687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4586b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4587b42b0bddSYang Yingliang */ 4588b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 45893c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 45908864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4591bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 45923c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4593b42b0bddSYang Yingliang } 45948864b4e5STejun Heo 4595687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4596a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 45978864b4e5STejun Heo put_unbound_pool(pool); 4598a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4599687a9aa5STejun Heo } 4600a892caccSTejun Heo 46015797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 46025797b1c1STejun Heo struct wq_node_nr_active *nna = 46035797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 46045797b1c1STejun Heo 46055797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 46065797b1c1STejun Heo list_del_init(&pwq->pending_node); 46075797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 46085797b1c1STejun Heo } 46095797b1c1STejun Heo 461025b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 46118864b4e5STejun Heo 46128864b4e5STejun Heo /* 46138864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4614e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 46158864b4e5STejun Heo */ 4616669de8bdSBart Van Assche if (is_last) { 4617669de8bdSBart Van Assche wq_unregister_lockdep(wq); 461825b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 46196029a918STejun Heo } 4620669de8bdSBart Van Assche } 46218864b4e5STejun Heo 462267dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4623f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4624f147f29eSTejun Heo struct worker_pool *pool) 4625d2c1d404STejun Heo { 4626d2c1d404STejun Heo BUG_ON((unsigned long)pwq & WORK_STRUCT_FLAG_MASK); 4627d2c1d404STejun Heo 4628e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4629e50aba9aSTejun Heo 4630d2c1d404STejun Heo pwq->pool = pool; 4631d2c1d404STejun Heo pwq->wq = wq; 4632d2c1d404STejun Heo pwq->flush_color = -1; 46338864b4e5STejun Heo pwq->refcnt = 1; 4634f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 46355797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 46361befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4637d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4638687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4639f147f29eSTejun Heo } 4640d2c1d404STejun Heo 4641f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 46421befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4643f147f29eSTejun Heo { 4644f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4645f147f29eSTejun Heo 4646f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 464775ccf595STejun Heo 46481befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 46491befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 46501befcf30STejun Heo return; 46511befcf30STejun Heo 465229b1cb41SLai Jiangshan /* set the matching work_color */ 465375ccf595STejun Heo pwq->work_color = wq->work_color; 4654983ca25eSTejun Heo 4655983ca25eSTejun Heo /* link in @pwq */ 46569e8cd2f5STejun Heo list_add_rcu(&pwq->pwqs_node, &wq->pwqs); 4657df2d5ae4STejun Heo } 46586029a918STejun Heo 4659f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4660f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4661f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4662f147f29eSTejun Heo { 4663f147f29eSTejun Heo struct worker_pool *pool; 4664f147f29eSTejun Heo struct pool_workqueue *pwq; 4665f147f29eSTejun Heo 4666f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4667f147f29eSTejun Heo 4668f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4669f147f29eSTejun Heo if (!pool) 4670f147f29eSTejun Heo return NULL; 4671f147f29eSTejun Heo 4672e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4673f147f29eSTejun Heo if (!pwq) { 4674f147f29eSTejun Heo put_unbound_pool(pool); 4675f147f29eSTejun Heo return NULL; 4676f147f29eSTejun Heo } 4677f147f29eSTejun Heo 4678f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4679f147f29eSTejun Heo return pwq; 4680d2c1d404STejun Heo } 4681d2c1d404STejun Heo 46824c16bd32STejun Heo /** 4683fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4684042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 468584193c07STejun Heo * @cpu: the target CPU 46864c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 46874c16bd32STejun Heo * 4688fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4689fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 46909546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 46914c16bd32STejun Heo * 4692fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4693fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4694fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 46954c16bd32STejun Heo * 4696fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 46974c16bd32STejun Heo */ 46989546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 46999546b29eSTejun Heo int cpu_going_down) 47004c16bd32STejun Heo { 470184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 470284193c07STejun Heo int pod = pt->cpu_pod[cpu]; 47034c16bd32STejun Heo 4704fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 47059546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 47069546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 47074c16bd32STejun Heo if (cpu_going_down >= 0) 47089546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 47094c16bd32STejun Heo 47109546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 47119546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 471284193c07STejun Heo return; 471384193c07STejun Heo } 47144c16bd32STejun Heo 4715fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 47169546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 47171ad0f0a7SMichael Bringmann 47189546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 47191ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 47201ad0f0a7SMichael Bringmann "possible intersect\n"); 47214c16bd32STejun Heo } 47224c16bd32STejun Heo 47239f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4724636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4725636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 47261befcf30STejun Heo { 47279f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 47281befcf30STejun Heo struct pool_workqueue *old_pwq; 47291befcf30STejun Heo 47305b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 47311befcf30STejun Heo lockdep_assert_held(&wq->mutex); 47321befcf30STejun Heo 47331befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 47341befcf30STejun Heo link_pwq(pwq); 47351befcf30STejun Heo 47369f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 47379f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 47381befcf30STejun Heo return old_pwq; 47391befcf30STejun Heo } 47401befcf30STejun Heo 47412d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 47422d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 47432d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 47442d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4745042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 47462d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 47472d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 47482d5f0764SLai Jiangshan }; 47492d5f0764SLai Jiangshan 47502d5f0764SLai Jiangshan /* free the resources after success or abort */ 47512d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 47522d5f0764SLai Jiangshan { 47532d5f0764SLai Jiangshan if (ctx) { 4754636b927eSTejun Heo int cpu; 47552d5f0764SLai Jiangshan 4756636b927eSTejun Heo for_each_possible_cpu(cpu) 4757636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 47582d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 47592d5f0764SLai Jiangshan 47602d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 47612d5f0764SLai Jiangshan 47622d5f0764SLai Jiangshan kfree(ctx); 47632d5f0764SLai Jiangshan } 47642d5f0764SLai Jiangshan } 47652d5f0764SLai Jiangshan 47662d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 47672d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 47682d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 476999c621efSLai Jiangshan const struct workqueue_attrs *attrs, 477099c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 47712d5f0764SLai Jiangshan { 47722d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 47739546b29eSTejun Heo struct workqueue_attrs *new_attrs; 4774636b927eSTejun Heo int cpu; 47752d5f0764SLai Jiangshan 47762d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 47772d5f0764SLai Jiangshan 477884193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 477984193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 478084193c07STejun Heo return ERR_PTR(-EINVAL); 478184193c07STejun Heo 4782636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 47832d5f0764SLai Jiangshan 4784be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 47859546b29eSTejun Heo if (!ctx || !new_attrs) 47862d5f0764SLai Jiangshan goto out_free; 47872d5f0764SLai Jiangshan 4788042f7df1SLai Jiangshan /* 47892d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 47902d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 47912d5f0764SLai Jiangshan * it even if we don't use it immediately. 47922d5f0764SLai Jiangshan */ 47930f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 47940f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 47959546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 47962d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 47972d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 47982d5f0764SLai Jiangshan goto out_free; 47992d5f0764SLai Jiangshan 4800636b927eSTejun Heo for_each_possible_cpu(cpu) { 4801af73f5c9STejun Heo if (new_attrs->ordered) { 48022d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 4803636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 4804636b927eSTejun Heo } else { 48059546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 48069546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 4807636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 4808636b927eSTejun Heo goto out_free; 48092d5f0764SLai Jiangshan } 48102d5f0764SLai Jiangshan } 48112d5f0764SLai Jiangshan 4812042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 4813042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 4814042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 48159546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 48162d5f0764SLai Jiangshan ctx->attrs = new_attrs; 4817042f7df1SLai Jiangshan 48182d5f0764SLai Jiangshan ctx->wq = wq; 48192d5f0764SLai Jiangshan return ctx; 48202d5f0764SLai Jiangshan 48212d5f0764SLai Jiangshan out_free: 48222d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 48232d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 482484193c07STejun Heo return ERR_PTR(-ENOMEM); 48252d5f0764SLai Jiangshan } 48262d5f0764SLai Jiangshan 48272d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 48282d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 48292d5f0764SLai Jiangshan { 4830636b927eSTejun Heo int cpu; 48312d5f0764SLai Jiangshan 48322d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 48332d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 48342d5f0764SLai Jiangshan 48352d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 48362d5f0764SLai Jiangshan 48379f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 4838636b927eSTejun Heo for_each_possible_cpu(cpu) 4839636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 4840636b927eSTejun Heo ctx->pwq_tbl[cpu]); 48419f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 48422d5f0764SLai Jiangshan 48435797b1c1STejun Heo /* update node_nr_active->max */ 48445797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 48455797b1c1STejun Heo 48462d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 48472d5f0764SLai Jiangshan } 48482d5f0764SLai Jiangshan 4849a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 4850a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 4851a0111cf6SLai Jiangshan { 4852a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 4853a0111cf6SLai Jiangshan 4854a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 4855a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 4856a0111cf6SLai Jiangshan return -EINVAL; 4857a0111cf6SLai Jiangshan 4858a0111cf6SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 48590a94efb5STejun Heo if (!list_empty(&wq->pwqs)) { 48600a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 4861a0111cf6SLai Jiangshan return -EINVAL; 4862a0111cf6SLai Jiangshan 48630a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 48640a94efb5STejun Heo } 48650a94efb5STejun Heo 486699c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 486784193c07STejun Heo if (IS_ERR(ctx)) 486884193c07STejun Heo return PTR_ERR(ctx); 4869a0111cf6SLai Jiangshan 4870a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 4871a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 4872a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 4873a0111cf6SLai Jiangshan 48746201171eSwanghaibin return 0; 4875a0111cf6SLai Jiangshan } 4876a0111cf6SLai Jiangshan 48779e8cd2f5STejun Heo /** 48789e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 48799e8cd2f5STejun Heo * @wq: the target workqueue 48809e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 48819e8cd2f5STejun Heo * 4882fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 4883fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 4884fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 4885fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 4886fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 48879e8cd2f5STejun Heo * 4888d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 4889d185af30SYacine Belkadi * 4890ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 4891509b3204SDaniel Jordan * 4892d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 48939e8cd2f5STejun Heo */ 4894513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 48959e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 48969e8cd2f5STejun Heo { 4897a0111cf6SLai Jiangshan int ret; 48989e8cd2f5STejun Heo 4899509b3204SDaniel Jordan lockdep_assert_cpus_held(); 4900509b3204SDaniel Jordan 4901509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 4902a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 4903509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 49042d5f0764SLai Jiangshan 49052d5f0764SLai Jiangshan return ret; 49069e8cd2f5STejun Heo } 49079e8cd2f5STejun Heo 49084c16bd32STejun Heo /** 4909fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 49104c16bd32STejun Heo * @wq: the target workqueue 49114cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 49124cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 49134c16bd32STejun Heo * @online: whether @cpu is coming up or going down 49144c16bd32STejun Heo * 49154c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 4916fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 49174c16bd32STejun Heo * @wq accordingly. 49184c16bd32STejun Heo * 49194c16bd32STejun Heo * 4920fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 4921fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 4922fef59c9cSTejun Heo * 4923fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 4924fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 4925fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 4926fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 4927fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 4928fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 49294c16bd32STejun Heo */ 4930fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 49314cbfd3deSTejun Heo int hotplug_cpu, bool online) 49324c16bd32STejun Heo { 49334cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 49344c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 49354c16bd32STejun Heo struct workqueue_attrs *target_attrs; 49364c16bd32STejun Heo 49374c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 49384c16bd32STejun Heo 493984193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 49404c16bd32STejun Heo return; 49414c16bd32STejun Heo 49424c16bd32STejun Heo /* 49434c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 49444c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 49454c16bd32STejun Heo * CPU hotplug exclusion. 49464c16bd32STejun Heo */ 4947fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 49484c16bd32STejun Heo 49494c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 49500f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 49514c16bd32STejun Heo 4952636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 49539546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 49549f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 4955f7142ed4SLai Jiangshan return; 49564c16bd32STejun Heo 49574c16bd32STejun Heo /* create a new pwq */ 49584c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 49594c16bd32STejun Heo if (!pwq) { 4960fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 49614c16bd32STejun Heo wq->name); 496277f300b1SDaeseok Youn goto use_dfl_pwq; 49634c16bd32STejun Heo } 49644c16bd32STejun Heo 4965f7142ed4SLai Jiangshan /* Install the new pwq. */ 49664c16bd32STejun Heo mutex_lock(&wq->mutex); 4967636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 49684c16bd32STejun Heo goto out_unlock; 49694c16bd32STejun Heo 49704c16bd32STejun Heo use_dfl_pwq: 4971f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 49729f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 49739f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 49749f66cff2STejun Heo get_pwq(pwq); 49759f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 49769f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 49774c16bd32STejun Heo out_unlock: 49784c16bd32STejun Heo mutex_unlock(&wq->mutex); 49794c16bd32STejun Heo put_pwq_unlocked(old_pwq); 49804c16bd32STejun Heo } 49814c16bd32STejun Heo 498230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 49831da177e4SLinus Torvalds { 498449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 49858a2b7538STejun Heo int cpu, ret; 4986e1d8aa9fSFrederic Weisbecker 4987687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 4988ee1ceef7STejun Heo if (!wq->cpu_pwq) 4989687a9aa5STejun Heo goto enomem; 499030cdf249STejun Heo 4991636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 499230cdf249STejun Heo for_each_possible_cpu(cpu) { 4993687a9aa5STejun Heo struct pool_workqueue **pwq_p = 4994ee1ceef7STejun Heo per_cpu_ptr(wq->cpu_pwq, cpu); 4995687a9aa5STejun Heo struct worker_pool *pool = 4996687a9aa5STejun Heo &(per_cpu_ptr(cpu_worker_pools, cpu)[highpri]); 499730cdf249STejun Heo 4998687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 4999687a9aa5STejun Heo pool->node); 5000687a9aa5STejun Heo if (!*pwq_p) 5001687a9aa5STejun Heo goto enomem; 5002687a9aa5STejun Heo 5003687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5004f147f29eSTejun Heo 5005f147f29eSTejun Heo mutex_lock(&wq->mutex); 5006687a9aa5STejun Heo link_pwq(*pwq_p); 5007f147f29eSTejun Heo mutex_unlock(&wq->mutex); 500830cdf249STejun Heo } 500930cdf249STejun Heo return 0; 5010509b3204SDaniel Jordan } 5011509b3204SDaniel Jordan 5012ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5013509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 50149f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 50159f66cff2STejun Heo 50168a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 50178a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 50189f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 50199f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 50209f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 50218a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 50229e8cd2f5STejun Heo } else { 5023509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 50249e8cd2f5STejun Heo } 5025ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5026509b3204SDaniel Jordan 502764344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 502864344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 502964344553SZqiang */ 503064344553SZqiang if (ret) 503164344553SZqiang kthread_flush_worker(pwq_release_worker); 503264344553SZqiang 5033509b3204SDaniel Jordan return ret; 5034687a9aa5STejun Heo 5035687a9aa5STejun Heo enomem: 5036687a9aa5STejun Heo if (wq->cpu_pwq) { 50377b42f401SZqiang for_each_possible_cpu(cpu) { 50387b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 50397b42f401SZqiang 50407b42f401SZqiang if (pwq) 50417b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 50427b42f401SZqiang } 5043687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5044687a9aa5STejun Heo wq->cpu_pwq = NULL; 5045687a9aa5STejun Heo } 5046687a9aa5STejun Heo return -ENOMEM; 50470f900049STejun Heo } 50480f900049STejun Heo 5049f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5050f3421797STejun Heo const char *name) 5051b71ab8c2STejun Heo { 5052636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5053044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5054636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5055b71ab8c2STejun Heo 5056636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5057b71ab8c2STejun Heo } 5058b71ab8c2STejun Heo 5059983c7515STejun Heo /* 5060983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5061983c7515STejun Heo * to guarantee forward progress. 5062983c7515STejun Heo */ 5063983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5064983c7515STejun Heo { 5065983c7515STejun Heo struct worker *rescuer; 5066b92b36eaSDan Carpenter int ret; 5067983c7515STejun Heo 5068983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5069983c7515STejun Heo return 0; 5070983c7515STejun Heo 5071983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 50724c0736a7SPetr Mladek if (!rescuer) { 50734c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 50744c0736a7SPetr Mladek wq->name); 5075983c7515STejun Heo return -ENOMEM; 50764c0736a7SPetr Mladek } 5077983c7515STejun Heo 5078983c7515STejun Heo rescuer->rescue_wq = wq; 5079b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5080f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5081b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 50824c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 50834c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5084983c7515STejun Heo kfree(rescuer); 5085b92b36eaSDan Carpenter return ret; 5086983c7515STejun Heo } 5087983c7515STejun Heo 5088983c7515STejun Heo wq->rescuer = rescuer; 508985f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 509085f0ab43SJuri Lelli kthread_bind_mask(rescuer->task, wq->unbound_attrs->cpumask); 509185f0ab43SJuri Lelli else 5092983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5093983c7515STejun Heo wake_up_process(rescuer->task); 5094983c7515STejun Heo 5095983c7515STejun Heo return 0; 5096983c7515STejun Heo } 5097983c7515STejun Heo 5098a045a272STejun Heo /** 5099a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5100a045a272STejun Heo * @wq: target workqueue 5101a045a272STejun Heo * 5102a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5103a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5104a045a272STejun Heo * @wq->max_active to zero. 5105a045a272STejun Heo */ 5106a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5107a045a272STejun Heo { 5108c5404d4eSTejun Heo bool activated; 51095797b1c1STejun Heo int new_max, new_min; 5110a045a272STejun Heo 5111a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5112a045a272STejun Heo 5113a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 51145797b1c1STejun Heo new_max = 0; 51155797b1c1STejun Heo new_min = 0; 51165797b1c1STejun Heo } else { 51175797b1c1STejun Heo new_max = wq->saved_max_active; 51185797b1c1STejun Heo new_min = wq->saved_min_active; 5119a045a272STejun Heo } 5120a045a272STejun Heo 51215797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5122a045a272STejun Heo return; 5123a045a272STejun Heo 5124a045a272STejun Heo /* 51255797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5126a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5127a045a272STejun Heo * because new work items are always queued behind existing inactive 5128a045a272STejun Heo * work items if there are any. 5129a045a272STejun Heo */ 51305797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 51315797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 51325797b1c1STejun Heo 51335797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 51345797b1c1STejun Heo wq_update_node_max_active(wq, -1); 51355797b1c1STejun Heo 51365797b1c1STejun Heo if (new_max == 0) 51375797b1c1STejun Heo return; 5138a045a272STejun Heo 5139c5404d4eSTejun Heo /* 5140c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5141c5404d4eSTejun Heo * until max_active is filled. 5142c5404d4eSTejun Heo */ 5143c5404d4eSTejun Heo do { 5144c5404d4eSTejun Heo struct pool_workqueue *pwq; 5145c5404d4eSTejun Heo 5146c5404d4eSTejun Heo activated = false; 5147a045a272STejun Heo for_each_pwq(pwq, wq) { 5148a045a272STejun Heo unsigned long flags; 5149a045a272STejun Heo 5150c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5151a045a272STejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, flags); 51525797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5153c5404d4eSTejun Heo activated = true; 5154a045a272STejun Heo kick_pool(pwq->pool); 5155c5404d4eSTejun Heo } 5156a045a272STejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 5157a045a272STejun Heo } 5158c5404d4eSTejun Heo } while (activated); 5159a045a272STejun Heo } 5160a045a272STejun Heo 5161a2775bbcSMathieu Malaterre __printf(1, 4) 5162669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 516397e37d7bSTejun Heo unsigned int flags, 5164669de8bdSBart Van Assche int max_active, ...) 51653af24433SOleg Nesterov { 5166ecf6881fSTejun Heo va_list args; 51673af24433SOleg Nesterov struct workqueue_struct *wq; 516891ccc6e7STejun Heo size_t wq_size; 516991ccc6e7STejun Heo int name_len; 5170b196be89STejun Heo 51715c0338c6STejun Heo /* 5172fef59c9cSTejun Heo * Unbound && max_active == 1 used to imply ordered, which is no longer 5173fef59c9cSTejun Heo * the case on many machines due to per-pod pools. While 51745c0338c6STejun Heo * alloc_ordered_workqueue() is the right way to create an ordered 5175fef59c9cSTejun Heo * workqueue, keep the previous behavior to avoid subtle breakages. 51765c0338c6STejun Heo */ 51775c0338c6STejun Heo if ((flags & WQ_UNBOUND) && max_active == 1) 51785c0338c6STejun Heo flags |= __WQ_ORDERED; 51795c0338c6STejun Heo 5180cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5181cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5182cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5183cee22a15SViresh Kumar 5184ecf6881fSTejun Heo /* allocate wq and format name */ 518591ccc6e7STejun Heo if (flags & WQ_UNBOUND) 518691ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 518791ccc6e7STejun Heo else 518891ccc6e7STejun Heo wq_size = sizeof(*wq); 518991ccc6e7STejun Heo 519091ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5191b196be89STejun Heo if (!wq) 5192d2c1d404STejun Heo return NULL; 5193b196be89STejun Heo 51946029a918STejun Heo if (flags & WQ_UNBOUND) { 5195be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 51966029a918STejun Heo if (!wq->unbound_attrs) 51976029a918STejun Heo goto err_free_wq; 51986029a918STejun Heo } 51996029a918STejun Heo 5200669de8bdSBart Van Assche va_start(args, max_active); 520191ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5202b196be89STejun Heo va_end(args); 52033af24433SOleg Nesterov 520491ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 520591ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 520691ccc6e7STejun Heo wq->name); 520731c89007SAudra Mitchell 5208d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5209b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 52103af24433SOleg Nesterov 5211b196be89STejun Heo /* init wq */ 521297e37d7bSTejun Heo wq->flags = flags; 5213a045a272STejun Heo wq->max_active = max_active; 52145797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 52155797b1c1STejun Heo wq->saved_max_active = wq->max_active; 52165797b1c1STejun Heo wq->saved_min_active = wq->min_active; 52173c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5218112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 521930cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 522073f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 522173f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5222493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 52233af24433SOleg Nesterov 5224669de8bdSBart Van Assche wq_init_lockdep(wq); 5225cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 52263af24433SOleg Nesterov 522791ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 522891ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 522982efcab3SBart Van Assche goto err_unreg_lockdep; 523091ccc6e7STejun Heo } 523191ccc6e7STejun Heo 523291ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 523391ccc6e7STejun Heo goto err_free_node_nr_active; 52341537663fSTejun Heo 523540c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5236d2c1d404STejun Heo goto err_destroy; 5237e22bee78STejun Heo 5238226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5239226223abSTejun Heo goto err_destroy; 5240226223abSTejun Heo 52416af8bf3dSOleg Nesterov /* 524268e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 524368e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 524468e13a67SLai Jiangshan * list. 52456af8bf3dSOleg Nesterov */ 524668e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5247a0a1a5fdSTejun Heo 5248a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5249a045a272STejun Heo wq_adjust_max_active(wq); 5250a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5251a0a1a5fdSTejun Heo 5252e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5253a0a1a5fdSTejun Heo 525468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 52553af24433SOleg Nesterov 52563af24433SOleg Nesterov return wq; 5257d2c1d404STejun Heo 525891ccc6e7STejun Heo err_free_node_nr_active: 525991ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 526091ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 526182efcab3SBart Van Assche err_unreg_lockdep: 5262009bb421SBart Van Assche wq_unregister_lockdep(wq); 5263009bb421SBart Van Assche wq_free_lockdep(wq); 526482efcab3SBart Van Assche err_free_wq: 52656029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 52664690c4abSTejun Heo kfree(wq); 5267d2c1d404STejun Heo return NULL; 5268d2c1d404STejun Heo err_destroy: 5269d2c1d404STejun Heo destroy_workqueue(wq); 52704690c4abSTejun Heo return NULL; 52711da177e4SLinus Torvalds } 5272669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 52731da177e4SLinus Torvalds 5274c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5275c29eb853STejun Heo { 5276c29eb853STejun Heo int i; 5277c29eb853STejun Heo 5278c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5279c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5280c29eb853STejun Heo return true; 5281c29eb853STejun Heo 52829f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5283c29eb853STejun Heo return true; 5284afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5285c29eb853STejun Heo return true; 5286c29eb853STejun Heo 5287c29eb853STejun Heo return false; 5288c29eb853STejun Heo } 5289c29eb853STejun Heo 52903af24433SOleg Nesterov /** 52913af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 52923af24433SOleg Nesterov * @wq: target workqueue 52933af24433SOleg Nesterov * 52943af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 52953af24433SOleg Nesterov */ 52963af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 52973af24433SOleg Nesterov { 529849e3cf44STejun Heo struct pool_workqueue *pwq; 5299636b927eSTejun Heo int cpu; 53003af24433SOleg Nesterov 5301def98c84STejun Heo /* 5302def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5303def98c84STejun Heo * lead to sysfs name conflicts. 5304def98c84STejun Heo */ 5305def98c84STejun Heo workqueue_sysfs_unregister(wq); 5306def98c84STejun Heo 530733e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 530833e3f0a3SRichard Clark mutex_lock(&wq->mutex); 530933e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 531033e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 531133e3f0a3SRichard Clark 53129c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 53139c5a2ba7STejun Heo drain_workqueue(wq); 5314c8efcc25STejun Heo 5315def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5316def98c84STejun Heo if (wq->rescuer) { 5317def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5318def98c84STejun Heo 5319def98c84STejun Heo /* this prevents new queueing */ 5320a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5321def98c84STejun Heo wq->rescuer = NULL; 5322a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5323def98c84STejun Heo 5324def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5325def98c84STejun Heo kthread_stop(rescuer->task); 53268efe1223STejun Heo kfree(rescuer); 5327def98c84STejun Heo } 5328def98c84STejun Heo 5329c29eb853STejun Heo /* 5330c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5331c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5332c29eb853STejun Heo */ 5333c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5334b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 533549e3cf44STejun Heo for_each_pwq(pwq, wq) { 5336a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5337c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 53381d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5339e66b39afSTejun Heo __func__, wq->name); 5340c29eb853STejun Heo show_pwq(pwq); 5341a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5342b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5343c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 534455df0933SImran Khan show_one_workqueue(wq); 53456183c009STejun Heo return; 534676af4d93STejun Heo } 5347a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 534876af4d93STejun Heo } 5349b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 53506183c009STejun Heo 5351a0a1a5fdSTejun Heo /* 5352a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5353a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5354a0a1a5fdSTejun Heo */ 5355e2dca7adSTejun Heo list_del_rcu(&wq->list); 535668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 53573af24433SOleg Nesterov 53588864b4e5STejun Heo /* 5359636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5360636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5361636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 53628864b4e5STejun Heo */ 5363636b927eSTejun Heo rcu_read_lock(); 5364636b927eSTejun Heo 5365636b927eSTejun Heo for_each_possible_cpu(cpu) { 53669f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 53679f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 53684c16bd32STejun Heo } 53694c16bd32STejun Heo 53709f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 53719f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5372636b927eSTejun Heo 5373636b927eSTejun Heo rcu_read_unlock(); 53743af24433SOleg Nesterov } 53753af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 53763af24433SOleg Nesterov 5377dcd989cbSTejun Heo /** 5378dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5379dcd989cbSTejun Heo * @wq: target workqueue 5380dcd989cbSTejun Heo * @max_active: new max_active value. 5381dcd989cbSTejun Heo * 53825797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 53835797b1c1STejun Heo * comment. 5384dcd989cbSTejun Heo * 5385dcd989cbSTejun Heo * CONTEXT: 5386dcd989cbSTejun Heo * Don't call from IRQ context. 5387dcd989cbSTejun Heo */ 5388dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5389dcd989cbSTejun Heo { 53908719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 53910a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 53928719dceaSTejun Heo return; 53938719dceaSTejun Heo 5394f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5395dcd989cbSTejun Heo 5396a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5397dcd989cbSTejun Heo 53980a94efb5STejun Heo wq->flags &= ~__WQ_ORDERED; 5399dcd989cbSTejun Heo wq->saved_max_active = max_active; 54005797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 54015797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 54025797b1c1STejun Heo 5403a045a272STejun Heo wq_adjust_max_active(wq); 5404dcd989cbSTejun Heo 5405a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5406dcd989cbSTejun Heo } 5407dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5408dcd989cbSTejun Heo 5409dcd989cbSTejun Heo /** 541027d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 541127d4ee03SLukas Wunner * 541227d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 541327d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 541427d4ee03SLukas Wunner * 541527d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 541627d4ee03SLukas Wunner */ 541727d4ee03SLukas Wunner struct work_struct *current_work(void) 541827d4ee03SLukas Wunner { 541927d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 542027d4ee03SLukas Wunner 542127d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 542227d4ee03SLukas Wunner } 542327d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 542427d4ee03SLukas Wunner 542527d4ee03SLukas Wunner /** 5426e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5427e6267616STejun Heo * 5428e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5429e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5430d185af30SYacine Belkadi * 5431d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5432e6267616STejun Heo */ 5433e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5434e6267616STejun Heo { 5435e6267616STejun Heo struct worker *worker = current_wq_worker(); 5436e6267616STejun Heo 54376a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5438e6267616STejun Heo } 5439e6267616STejun Heo 5440e6267616STejun Heo /** 5441dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5442dcd989cbSTejun Heo * @cpu: CPU in question 5443dcd989cbSTejun Heo * @wq: target workqueue 5444dcd989cbSTejun Heo * 5445dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5446dcd989cbSTejun Heo * no synchronization around this function and the test result is 5447dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5448dcd989cbSTejun Heo * 5449d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5450636b927eSTejun Heo * 5451636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5452636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5453636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5454636b927eSTejun Heo * other CPUs. 5455d3251859STejun Heo * 5456d185af30SYacine Belkadi * Return: 5457dcd989cbSTejun Heo * %true if congested, %false otherwise. 5458dcd989cbSTejun Heo */ 5459d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5460dcd989cbSTejun Heo { 54617fb98ea7STejun Heo struct pool_workqueue *pwq; 546276af4d93STejun Heo bool ret; 546376af4d93STejun Heo 546424acfb71SThomas Gleixner rcu_read_lock(); 546524acfb71SThomas Gleixner preempt_disable(); 54667fb98ea7STejun Heo 5467d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5468d3251859STejun Heo cpu = smp_processor_id(); 5469d3251859STejun Heo 5470687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5471f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5472636b927eSTejun Heo 547324acfb71SThomas Gleixner preempt_enable(); 547424acfb71SThomas Gleixner rcu_read_unlock(); 547576af4d93STejun Heo 547676af4d93STejun Heo return ret; 5477dcd989cbSTejun Heo } 5478dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5479dcd989cbSTejun Heo 5480dcd989cbSTejun Heo /** 5481dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5482dcd989cbSTejun Heo * @work: the work to be tested 5483dcd989cbSTejun Heo * 5484dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5485dcd989cbSTejun Heo * synchronization around this function and the test result is 5486dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5487dcd989cbSTejun Heo * 5488d185af30SYacine Belkadi * Return: 5489dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5490dcd989cbSTejun Heo */ 5491dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5492dcd989cbSTejun Heo { 5493fa1b54e6STejun Heo struct worker_pool *pool; 5494dcd989cbSTejun Heo unsigned long flags; 5495dcd989cbSTejun Heo unsigned int ret = 0; 5496dcd989cbSTejun Heo 5497dcd989cbSTejun Heo if (work_pending(work)) 5498dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5499038366c5SLai Jiangshan 550024acfb71SThomas Gleixner rcu_read_lock(); 5501fa1b54e6STejun Heo pool = get_work_pool(work); 5502038366c5SLai Jiangshan if (pool) { 5503a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 5504c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5505dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5506a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 5507038366c5SLai Jiangshan } 550824acfb71SThomas Gleixner rcu_read_unlock(); 5509dcd989cbSTejun Heo 5510dcd989cbSTejun Heo return ret; 5511dcd989cbSTejun Heo } 5512dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5513dcd989cbSTejun Heo 55143d1cb205STejun Heo /** 55153d1cb205STejun Heo * set_worker_desc - set description for the current work item 55163d1cb205STejun Heo * @fmt: printf-style format string 55173d1cb205STejun Heo * @...: arguments for the format string 55183d1cb205STejun Heo * 55193d1cb205STejun Heo * This function can be called by a running work function to describe what 55203d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 55213d1cb205STejun Heo * information will be printed out together to help debugging. The 55223d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 55233d1cb205STejun Heo */ 55243d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 55253d1cb205STejun Heo { 55263d1cb205STejun Heo struct worker *worker = current_wq_worker(); 55273d1cb205STejun Heo va_list args; 55283d1cb205STejun Heo 55293d1cb205STejun Heo if (worker) { 55303d1cb205STejun Heo va_start(args, fmt); 55313d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 55323d1cb205STejun Heo va_end(args); 55333d1cb205STejun Heo } 55343d1cb205STejun Heo } 55355c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 55363d1cb205STejun Heo 55373d1cb205STejun Heo /** 55383d1cb205STejun Heo * print_worker_info - print out worker information and description 55393d1cb205STejun Heo * @log_lvl: the log level to use when printing 55403d1cb205STejun Heo * @task: target task 55413d1cb205STejun Heo * 55423d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 55433d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 55443d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 55453d1cb205STejun Heo * 55463d1cb205STejun Heo * This function can be safely called on any task as long as the 55473d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 55483d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 55493d1cb205STejun Heo */ 55503d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 55513d1cb205STejun Heo { 55523d1cb205STejun Heo work_func_t *fn = NULL; 55533d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 55543d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 55553d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 55563d1cb205STejun Heo struct workqueue_struct *wq = NULL; 55573d1cb205STejun Heo struct worker *worker; 55583d1cb205STejun Heo 55593d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 55603d1cb205STejun Heo return; 55613d1cb205STejun Heo 55623d1cb205STejun Heo /* 55633d1cb205STejun Heo * This function is called without any synchronization and @task 55643d1cb205STejun Heo * could be in any state. Be careful with dereferences. 55653d1cb205STejun Heo */ 5566e700591aSPetr Mladek worker = kthread_probe_data(task); 55673d1cb205STejun Heo 55683d1cb205STejun Heo /* 55698bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 55708bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 55713d1cb205STejun Heo */ 5572fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5573fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5574fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5575fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5576fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 55773d1cb205STejun Heo 55783d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5579d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 55808bf89593STejun Heo if (strcmp(name, desc)) 55813d1cb205STejun Heo pr_cont(" (%s)", desc); 55823d1cb205STejun Heo pr_cont("\n"); 55833d1cb205STejun Heo } 55843d1cb205STejun Heo } 55853d1cb205STejun Heo 55863494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 55873494fc30STejun Heo { 55883494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 55893494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 55903494fc30STejun Heo pr_cont(" node=%d", pool->node); 55913494fc30STejun Heo pr_cont(" flags=0x%x nice=%d", pool->flags, pool->attrs->nice); 55923494fc30STejun Heo } 55933494fc30STejun Heo 5594c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5595c76feb0dSPaul E. McKenney bool comma; 5596c76feb0dSPaul E. McKenney work_func_t func; 5597c76feb0dSPaul E. McKenney long ctr; 5598c76feb0dSPaul E. McKenney }; 5599c76feb0dSPaul E. McKenney 5600c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5601c76feb0dSPaul E. McKenney { 5602c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5603c76feb0dSPaul E. McKenney goto out_record; 5604c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5605c76feb0dSPaul E. McKenney pcwsp->ctr++; 5606c76feb0dSPaul E. McKenney return; 5607c76feb0dSPaul E. McKenney } 5608c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5609c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5610c76feb0dSPaul E. McKenney else 5611c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5612c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5613c76feb0dSPaul E. McKenney out_record: 5614c76feb0dSPaul E. McKenney if ((long)func == -1L) 5615c76feb0dSPaul E. McKenney return; 5616c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5617c76feb0dSPaul E. McKenney pcwsp->func = func; 5618c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5619c76feb0dSPaul E. McKenney } 5620c76feb0dSPaul E. McKenney 5621c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 56223494fc30STejun Heo { 56233494fc30STejun Heo if (work->func == wq_barrier_func) { 56243494fc30STejun Heo struct wq_barrier *barr; 56253494fc30STejun Heo 56263494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 56273494fc30STejun Heo 5628c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 56293494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 56303494fc30STejun Heo task_pid_nr(barr->task)); 56313494fc30STejun Heo } else { 5632c76feb0dSPaul E. McKenney if (!comma) 5633c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5634c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 56353494fc30STejun Heo } 56363494fc30STejun Heo } 56373494fc30STejun Heo 56383494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 56393494fc30STejun Heo { 5640c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 56413494fc30STejun Heo struct worker_pool *pool = pwq->pool; 56423494fc30STejun Heo struct work_struct *work; 56433494fc30STejun Heo struct worker *worker; 56443494fc30STejun Heo bool has_in_flight = false, has_pending = false; 56453494fc30STejun Heo int bkt; 56463494fc30STejun Heo 56473494fc30STejun Heo pr_info(" pwq %d:", pool->id); 56483494fc30STejun Heo pr_cont_pool_info(pool); 56493494fc30STejun Heo 5650a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5651a045a272STejun Heo pwq->nr_active, pwq->refcnt, 56523494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 56533494fc30STejun Heo 56543494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 56553494fc30STejun Heo if (worker->current_pwq == pwq) { 56563494fc30STejun Heo has_in_flight = true; 56573494fc30STejun Heo break; 56583494fc30STejun Heo } 56593494fc30STejun Heo } 56603494fc30STejun Heo if (has_in_flight) { 56613494fc30STejun Heo bool comma = false; 56623494fc30STejun Heo 56633494fc30STejun Heo pr_info(" in-flight:"); 56643494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 56653494fc30STejun Heo if (worker->current_pwq != pwq) 56663494fc30STejun Heo continue; 56673494fc30STejun Heo 5668d75f773cSSakari Ailus pr_cont("%s %d%s:%ps", comma ? "," : "", 56693494fc30STejun Heo task_pid_nr(worker->task), 567030ae2fc0STejun Heo worker->rescue_wq ? "(RESCUER)" : "", 56713494fc30STejun Heo worker->current_func); 56723494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5673c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5674c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 56753494fc30STejun Heo comma = true; 56763494fc30STejun Heo } 56773494fc30STejun Heo pr_cont("\n"); 56783494fc30STejun Heo } 56793494fc30STejun Heo 56803494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 56813494fc30STejun Heo if (get_work_pwq(work) == pwq) { 56823494fc30STejun Heo has_pending = true; 56833494fc30STejun Heo break; 56843494fc30STejun Heo } 56853494fc30STejun Heo } 56863494fc30STejun Heo if (has_pending) { 56873494fc30STejun Heo bool comma = false; 56883494fc30STejun Heo 56893494fc30STejun Heo pr_info(" pending:"); 56903494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 56913494fc30STejun Heo if (get_work_pwq(work) != pwq) 56923494fc30STejun Heo continue; 56933494fc30STejun Heo 5694c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 56953494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 56963494fc30STejun Heo } 5697c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 56983494fc30STejun Heo pr_cont("\n"); 56993494fc30STejun Heo } 57003494fc30STejun Heo 5701f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 57023494fc30STejun Heo bool comma = false; 57033494fc30STejun Heo 5704f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5705f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5706c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 57073494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 57083494fc30STejun Heo } 5709c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 57103494fc30STejun Heo pr_cont("\n"); 57113494fc30STejun Heo } 57123494fc30STejun Heo } 57133494fc30STejun Heo 57143494fc30STejun Heo /** 571555df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 571655df0933SImran Khan * @wq: workqueue whose state will be printed 57173494fc30STejun Heo */ 571855df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 57193494fc30STejun Heo { 57203494fc30STejun Heo struct pool_workqueue *pwq; 57213494fc30STejun Heo bool idle = true; 572255df0933SImran Khan unsigned long flags; 57233494fc30STejun Heo 57243494fc30STejun Heo for_each_pwq(pwq, wq) { 5725afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 57263494fc30STejun Heo idle = false; 57273494fc30STejun Heo break; 57283494fc30STejun Heo } 57293494fc30STejun Heo } 573055df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 573155df0933SImran Khan return; 57323494fc30STejun Heo 57333494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 57343494fc30STejun Heo 57353494fc30STejun Heo for_each_pwq(pwq, wq) { 5736a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pwq->pool->lock, flags); 5737afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 573857116ce1SJohan Hovold /* 573957116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 574057116ce1SJohan Hovold * drivers that queue work while holding locks 574157116ce1SJohan Hovold * also taken in their write paths. 574257116ce1SJohan Hovold */ 574357116ce1SJohan Hovold printk_deferred_enter(); 57443494fc30STejun Heo show_pwq(pwq); 574557116ce1SJohan Hovold printk_deferred_exit(); 574657116ce1SJohan Hovold } 5747a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pwq->pool->lock, flags); 574862635ea8SSergey Senozhatsky /* 574962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 575055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 575162635ea8SSergey Senozhatsky * hard lockup. 575262635ea8SSergey Senozhatsky */ 575362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 57543494fc30STejun Heo } 575555df0933SImran Khan 57563494fc30STejun Heo } 57573494fc30STejun Heo 575855df0933SImran Khan /** 575955df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 576055df0933SImran Khan * @pool: worker pool whose state will be printed 576155df0933SImran Khan */ 576255df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 576355df0933SImran Khan { 57643494fc30STejun Heo struct worker *worker; 57653494fc30STejun Heo bool first = true; 576655df0933SImran Khan unsigned long flags; 5767335a42ebSPetr Mladek unsigned long hung = 0; 57683494fc30STejun Heo 5769a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irqsave(&pool->lock, flags); 57703494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 57713494fc30STejun Heo goto next_pool; 5772335a42ebSPetr Mladek 5773335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 5774335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 5775335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 5776335a42ebSPetr Mladek 577757116ce1SJohan Hovold /* 577857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 577957116ce1SJohan Hovold * queue work while holding locks also taken in their write 578057116ce1SJohan Hovold * paths. 578157116ce1SJohan Hovold */ 578257116ce1SJohan Hovold printk_deferred_enter(); 57833494fc30STejun Heo pr_info("pool %d:", pool->id); 57843494fc30STejun Heo pr_cont_pool_info(pool); 5785335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 57863494fc30STejun Heo if (pool->manager) 57873494fc30STejun Heo pr_cont(" manager: %d", 57883494fc30STejun Heo task_pid_nr(pool->manager->task)); 57893494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 57903494fc30STejun Heo pr_cont(" %s%d", first ? "idle: " : "", 57913494fc30STejun Heo task_pid_nr(worker->task)); 57923494fc30STejun Heo first = false; 57933494fc30STejun Heo } 57943494fc30STejun Heo pr_cont("\n"); 579557116ce1SJohan Hovold printk_deferred_exit(); 57963494fc30STejun Heo next_pool: 5797a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irqrestore(&pool->lock, flags); 579862635ea8SSergey Senozhatsky /* 579962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 580055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 580162635ea8SSergey Senozhatsky * hard lockup. 580262635ea8SSergey Senozhatsky */ 580362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 580455df0933SImran Khan 58053494fc30STejun Heo } 58063494fc30STejun Heo 580755df0933SImran Khan /** 580855df0933SImran Khan * show_all_workqueues - dump workqueue state 580955df0933SImran Khan * 5810704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 581155df0933SImran Khan */ 581255df0933SImran Khan void show_all_workqueues(void) 581355df0933SImran Khan { 581455df0933SImran Khan struct workqueue_struct *wq; 581555df0933SImran Khan struct worker_pool *pool; 581655df0933SImran Khan int pi; 581755df0933SImran Khan 581855df0933SImran Khan rcu_read_lock(); 581955df0933SImran Khan 582055df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 582155df0933SImran Khan 582255df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 582355df0933SImran Khan show_one_workqueue(wq); 582455df0933SImran Khan 582555df0933SImran Khan for_each_pool(pool, pi) 582655df0933SImran Khan show_one_worker_pool(pool); 582755df0933SImran Khan 582824acfb71SThomas Gleixner rcu_read_unlock(); 58293494fc30STejun Heo } 58303494fc30STejun Heo 5831704bc669SJungseung Lee /** 5832704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 5833704bc669SJungseung Lee * 5834704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 5835704bc669SJungseung Lee * still busy. 5836704bc669SJungseung Lee */ 5837704bc669SJungseung Lee void show_freezable_workqueues(void) 5838704bc669SJungseung Lee { 5839704bc669SJungseung Lee struct workqueue_struct *wq; 5840704bc669SJungseung Lee 5841704bc669SJungseung Lee rcu_read_lock(); 5842704bc669SJungseung Lee 5843704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 5844704bc669SJungseung Lee 5845704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 5846704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 5847704bc669SJungseung Lee continue; 5848704bc669SJungseung Lee show_one_workqueue(wq); 5849704bc669SJungseung Lee } 5850704bc669SJungseung Lee 5851704bc669SJungseung Lee rcu_read_unlock(); 5852704bc669SJungseung Lee } 5853704bc669SJungseung Lee 58546b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 58556b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 58566b59808bSTejun Heo { 58576b59808bSTejun Heo int off; 58586b59808bSTejun Heo 58596b59808bSTejun Heo /* always show the actual comm */ 58606b59808bSTejun Heo off = strscpy(buf, task->comm, size); 58616b59808bSTejun Heo if (off < 0) 58626b59808bSTejun Heo return; 58636b59808bSTejun Heo 5864197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 58656b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 58666b59808bSTejun Heo 5867197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 5868197f6accSTejun Heo struct worker *worker = kthread_data(task); 5869197f6accSTejun Heo struct worker_pool *pool = worker->pool; 58706b59808bSTejun Heo 58716b59808bSTejun Heo if (pool) { 5872a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 58736b59808bSTejun Heo /* 5874197f6accSTejun Heo * ->desc tracks information (wq name or 5875197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 5876197f6accSTejun Heo * current, prepend '+', otherwise '-'. 58776b59808bSTejun Heo */ 58786b59808bSTejun Heo if (worker->desc[0] != '\0') { 58796b59808bSTejun Heo if (worker->current_work) 58806b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 58816b59808bSTejun Heo worker->desc); 58826b59808bSTejun Heo else 58836b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 58846b59808bSTejun Heo worker->desc); 58856b59808bSTejun Heo } 5886a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 58876b59808bSTejun Heo } 5888197f6accSTejun Heo } 58896b59808bSTejun Heo 58906b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 58916b59808bSTejun Heo } 58926b59808bSTejun Heo 589366448bc2SMathieu Malaterre #ifdef CONFIG_SMP 589466448bc2SMathieu Malaterre 5895db7bccf4STejun Heo /* 5896db7bccf4STejun Heo * CPU hotplug. 5897db7bccf4STejun Heo * 5898e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 5899112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 5900706026c2STejun Heo * pool which make migrating pending and scheduled works very 5901e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 590294cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 5903e22bee78STejun Heo * blocked draining impractical. 5904e22bee78STejun Heo * 590524647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 5906628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 5907628c78e7STejun Heo * cpu comes back online. 5908db7bccf4STejun Heo */ 5909db7bccf4STejun Heo 5910e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 5911db7bccf4STejun Heo { 59124ce62e9eSTejun Heo struct worker_pool *pool; 5913db7bccf4STejun Heo struct worker *worker; 5914db7bccf4STejun Heo 5915f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 59161258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 5917a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5918e22bee78STejun Heo 5919f2d5a0eeSTejun Heo /* 592092f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 592194cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 592211b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 5923b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 5924b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 5925b4ac9384SLai Jiangshan * is on the same cpu. 5926f2d5a0eeSTejun Heo */ 5927da028469SLai Jiangshan for_each_pool_worker(worker, pool) 5928403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 5929db7bccf4STejun Heo 593024647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 5931f2d5a0eeSTejun Heo 5932e22bee78STejun Heo /* 5933989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 5934989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 5935989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 5936989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 5937989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 5938eb283428SLai Jiangshan * are served by workers tied to the pool. 5939e22bee78STejun Heo */ 5940bc35f7efSLai Jiangshan pool->nr_running = 0; 5941eb283428SLai Jiangshan 5942eb283428SLai Jiangshan /* 5943eb283428SLai Jiangshan * With concurrency management just turned off, a busy 5944eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 5945eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 5946eb283428SLai Jiangshan */ 59470219a352STejun Heo kick_pool(pool); 5948989442d7SLai Jiangshan 5949a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 5950989442d7SLai Jiangshan 5951793777bcSValentin Schneider for_each_pool_worker(worker, pool) 5952793777bcSValentin Schneider unbind_worker(worker); 5953989442d7SLai Jiangshan 5954989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 5955eb283428SLai Jiangshan } 5956db7bccf4STejun Heo } 5957db7bccf4STejun Heo 5958bd7c089eSTejun Heo /** 5959bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 5960bd7c089eSTejun Heo * @pool: pool of interest 5961bd7c089eSTejun Heo * 5962a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 5963bd7c089eSTejun Heo */ 5964bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 5965bd7c089eSTejun Heo { 5966a9ab775bSTejun Heo struct worker *worker; 5967bd7c089eSTejun Heo 59681258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 5969bd7c089eSTejun Heo 5970bd7c089eSTejun Heo /* 5971a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 5972a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 5973402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 5974a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 5975a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 5976bd7c089eSTejun Heo */ 5977c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 5978c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 5979c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 59809546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 5981c63a2e52SValentin Schneider } 5982a9ab775bSTejun Heo 5983a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 5984f7c17d26SWanpeng Li 59853de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 5986a9ab775bSTejun Heo 5987da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 5988a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 5989a9ab775bSTejun Heo 5990a9ab775bSTejun Heo /* 5991a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 5992a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 5993a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 5994a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 5995a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 5996a9ab775bSTejun Heo * concurrency management. Note that when or whether 5997a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 5998a9ab775bSTejun Heo * 5999c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6000a9ab775bSTejun Heo * tested without holding any lock in 60016d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6002a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6003a9ab775bSTejun Heo * management operations. 6004bd7c089eSTejun Heo */ 6005a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6006a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6007a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6008c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6009bd7c089eSTejun Heo } 6010a9ab775bSTejun Heo 6011a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6012bd7c089eSTejun Heo } 6013bd7c089eSTejun Heo 60147dbc725eSTejun Heo /** 60157dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 60167dbc725eSTejun Heo * @pool: unbound pool of interest 60177dbc725eSTejun Heo * @cpu: the CPU which is coming up 60187dbc725eSTejun Heo * 60197dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 60207dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 60217dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 60227dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 60237dbc725eSTejun Heo */ 60247dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 60257dbc725eSTejun Heo { 60267dbc725eSTejun Heo static cpumask_t cpumask; 60277dbc725eSTejun Heo struct worker *worker; 60287dbc725eSTejun Heo 60291258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 60307dbc725eSTejun Heo 60317dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 60327dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 60337dbc725eSTejun Heo return; 60347dbc725eSTejun Heo 60357dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 60367dbc725eSTejun Heo 60377dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6038da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6039d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 60407dbc725eSTejun Heo } 60417dbc725eSTejun Heo 60427ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 60431da177e4SLinus Torvalds { 60444ce62e9eSTejun Heo struct worker_pool *pool; 60451da177e4SLinus Torvalds 6046f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 60473ce63377STejun Heo if (pool->nr_workers) 60483ce63377STejun Heo continue; 6049051e1850SLai Jiangshan if (!create_worker(pool)) 60507ee681b2SThomas Gleixner return -ENOMEM; 60513af24433SOleg Nesterov } 60527ee681b2SThomas Gleixner return 0; 60537ee681b2SThomas Gleixner } 60541da177e4SLinus Torvalds 60557ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 60567ee681b2SThomas Gleixner { 60577ee681b2SThomas Gleixner struct worker_pool *pool; 60587ee681b2SThomas Gleixner struct workqueue_struct *wq; 60597ee681b2SThomas Gleixner int pi; 60607ee681b2SThomas Gleixner 606168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 60627dbc725eSTejun Heo 60637dbc725eSTejun Heo for_each_pool(pool, pi) { 60641258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 606594cf58bbSTejun Heo 6066f05b558dSLai Jiangshan if (pool->cpu == cpu) 606794cf58bbSTejun Heo rebind_workers(pool); 6068f05b558dSLai Jiangshan else if (pool->cpu < 0) 60697dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 607094cf58bbSTejun Heo 60711258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 607294cf58bbSTejun Heo } 60737dbc725eSTejun Heo 6074fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 60754cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 607684193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 607784193c07STejun Heo 607884193c07STejun Heo if (attrs) { 607984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 60804cbfd3deSTejun Heo int tcpu; 60814cbfd3deSTejun Heo 608284193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6083fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 60845797b1c1STejun Heo 60855797b1c1STejun Heo mutex_lock(&wq->mutex); 60865797b1c1STejun Heo wq_update_node_max_active(wq, -1); 60875797b1c1STejun Heo mutex_unlock(&wq->mutex); 60884cbfd3deSTejun Heo } 60894cbfd3deSTejun Heo } 60904c16bd32STejun Heo 609168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 60927ee681b2SThomas Gleixner return 0; 609365758202STejun Heo } 609465758202STejun Heo 60957ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 609665758202STejun Heo { 60974c16bd32STejun Heo struct workqueue_struct *wq; 60988db25e78STejun Heo 60994c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6100e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6101e8b3f8dbSLai Jiangshan return -1; 6102e8b3f8dbSLai Jiangshan 6103e8b3f8dbSLai Jiangshan unbind_workers(cpu); 61044c16bd32STejun Heo 6105fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 61064c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 61074cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 610884193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 610984193c07STejun Heo 611084193c07STejun Heo if (attrs) { 611184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 61124cbfd3deSTejun Heo int tcpu; 61134cbfd3deSTejun Heo 611484193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6115fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 61165797b1c1STejun Heo 61175797b1c1STejun Heo mutex_lock(&wq->mutex); 61185797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 61195797b1c1STejun Heo mutex_unlock(&wq->mutex); 61204cbfd3deSTejun Heo } 61214cbfd3deSTejun Heo } 61224c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 61234c16bd32STejun Heo 61247ee681b2SThomas Gleixner return 0; 612565758202STejun Heo } 612665758202STejun Heo 61272d3854a3SRusty Russell struct work_for_cpu { 6128ed48ece2STejun Heo struct work_struct work; 61292d3854a3SRusty Russell long (*fn)(void *); 61302d3854a3SRusty Russell void *arg; 61312d3854a3SRusty Russell long ret; 61322d3854a3SRusty Russell }; 61332d3854a3SRusty Russell 6134ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 61352d3854a3SRusty Russell { 6136ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6137ed48ece2STejun Heo 61382d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 61392d3854a3SRusty Russell } 61402d3854a3SRusty Russell 61412d3854a3SRusty Russell /** 6142265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 61432d3854a3SRusty Russell * @cpu: the cpu to run on 61442d3854a3SRusty Russell * @fn: the function to run 61452d3854a3SRusty Russell * @arg: the function arg 6146265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 61472d3854a3SRusty Russell * 614831ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 61496b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6150d185af30SYacine Belkadi * 6151d185af30SYacine Belkadi * Return: The value @fn returns. 61522d3854a3SRusty Russell */ 6153265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6154265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 61552d3854a3SRusty Russell { 6156ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 61572d3854a3SRusty Russell 6158265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6159ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 616012997d1aSBjorn Helgaas flush_work(&wfc.work); 6161440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 61622d3854a3SRusty Russell return wfc.ret; 61632d3854a3SRusty Russell } 6164265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 61650e8d6a93SThomas Gleixner 61660e8d6a93SThomas Gleixner /** 6167265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 61680e8d6a93SThomas Gleixner * @cpu: the cpu to run on 61690e8d6a93SThomas Gleixner * @fn: the function to run 61700e8d6a93SThomas Gleixner * @arg: the function argument 6171265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 61720e8d6a93SThomas Gleixner * 61730e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 61740e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 61750e8d6a93SThomas Gleixner * 61760e8d6a93SThomas Gleixner * Return: The value @fn returns. 61770e8d6a93SThomas Gleixner */ 6178265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6179265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 61800e8d6a93SThomas Gleixner { 61810e8d6a93SThomas Gleixner long ret = -ENODEV; 61820e8d6a93SThomas Gleixner 6183ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 61840e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6185265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6186ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 61870e8d6a93SThomas Gleixner return ret; 61880e8d6a93SThomas Gleixner } 6189265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 61902d3854a3SRusty Russell #endif /* CONFIG_SMP */ 61912d3854a3SRusty Russell 6192a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6193e7577c50SRusty Russell 6194a0a1a5fdSTejun Heo /** 6195a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6196a0a1a5fdSTejun Heo * 619758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6198f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6199706026c2STejun Heo * pool->worklist. 6200a0a1a5fdSTejun Heo * 6201a0a1a5fdSTejun Heo * CONTEXT: 6202a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6203a0a1a5fdSTejun Heo */ 6204a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6205a0a1a5fdSTejun Heo { 620624b8a847STejun Heo struct workqueue_struct *wq; 6207a0a1a5fdSTejun Heo 620868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6209a0a1a5fdSTejun Heo 62106183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6211a0a1a5fdSTejun Heo workqueue_freezing = true; 6212a0a1a5fdSTejun Heo 621324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6214a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6215a045a272STejun Heo wq_adjust_max_active(wq); 6216a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6217a1056305STejun Heo } 62185bcab335STejun Heo 621968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6220a0a1a5fdSTejun Heo } 6221a0a1a5fdSTejun Heo 6222a0a1a5fdSTejun Heo /** 622358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6224a0a1a5fdSTejun Heo * 6225a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6226a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6227a0a1a5fdSTejun Heo * 6228a0a1a5fdSTejun Heo * CONTEXT: 622968e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6230a0a1a5fdSTejun Heo * 6231d185af30SYacine Belkadi * Return: 623258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 623358a69cb4STejun Heo * is complete. 6234a0a1a5fdSTejun Heo */ 6235a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6236a0a1a5fdSTejun Heo { 6237a0a1a5fdSTejun Heo bool busy = false; 623824b8a847STejun Heo struct workqueue_struct *wq; 623924b8a847STejun Heo struct pool_workqueue *pwq; 6240a0a1a5fdSTejun Heo 624168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6242a0a1a5fdSTejun Heo 62436183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6244a0a1a5fdSTejun Heo 624524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 624624b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 624724b8a847STejun Heo continue; 6248a0a1a5fdSTejun Heo /* 6249a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6250a0a1a5fdSTejun Heo * to peek without lock. 6251a0a1a5fdSTejun Heo */ 625224acfb71SThomas Gleixner rcu_read_lock(); 625324b8a847STejun Heo for_each_pwq(pwq, wq) { 62546183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6255112202d9STejun Heo if (pwq->nr_active) { 6256a0a1a5fdSTejun Heo busy = true; 625724acfb71SThomas Gleixner rcu_read_unlock(); 6258a0a1a5fdSTejun Heo goto out_unlock; 6259a0a1a5fdSTejun Heo } 6260a0a1a5fdSTejun Heo } 626124acfb71SThomas Gleixner rcu_read_unlock(); 6262a0a1a5fdSTejun Heo } 6263a0a1a5fdSTejun Heo out_unlock: 626468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6265a0a1a5fdSTejun Heo return busy; 6266a0a1a5fdSTejun Heo } 6267a0a1a5fdSTejun Heo 6268a0a1a5fdSTejun Heo /** 6269a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6270a0a1a5fdSTejun Heo * 6271a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6272706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6273a0a1a5fdSTejun Heo * 6274a0a1a5fdSTejun Heo * CONTEXT: 6275a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6276a0a1a5fdSTejun Heo */ 6277a0a1a5fdSTejun Heo void thaw_workqueues(void) 6278a0a1a5fdSTejun Heo { 627924b8a847STejun Heo struct workqueue_struct *wq; 6280a0a1a5fdSTejun Heo 628168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6282a0a1a5fdSTejun Heo 6283a0a1a5fdSTejun Heo if (!workqueue_freezing) 6284a0a1a5fdSTejun Heo goto out_unlock; 6285a0a1a5fdSTejun Heo 628674b414eaSLai Jiangshan workqueue_freezing = false; 628724b8a847STejun Heo 628824b8a847STejun Heo /* restore max_active and repopulate worklist */ 628924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6290a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6291a045a272STejun Heo wq_adjust_max_active(wq); 6292a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 629324b8a847STejun Heo } 629424b8a847STejun Heo 6295a0a1a5fdSTejun Heo out_unlock: 629668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6297a0a1a5fdSTejun Heo } 6298a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6299a0a1a5fdSTejun Heo 630099c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6301042f7df1SLai Jiangshan { 6302042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6303042f7df1SLai Jiangshan int ret = 0; 6304042f7df1SLai Jiangshan struct workqueue_struct *wq; 6305042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6306042f7df1SLai Jiangshan 6307042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6308042f7df1SLai Jiangshan 6309042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 6310042f7df1SLai Jiangshan if (!(wq->flags & WQ_UNBOUND)) 6311042f7df1SLai Jiangshan continue; 6312ca10d851SWaiman Long 6313042f7df1SLai Jiangshan /* creating multiple pwqs breaks ordering guarantee */ 6314ca10d851SWaiman Long if (!list_empty(&wq->pwqs)) { 6315ca10d851SWaiman Long if (wq->flags & __WQ_ORDERED_EXPLICIT) 6316042f7df1SLai Jiangshan continue; 6317ca10d851SWaiman Long wq->flags &= ~__WQ_ORDERED; 6318ca10d851SWaiman Long } 6319042f7df1SLai Jiangshan 632099c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 632184193c07STejun Heo if (IS_ERR(ctx)) { 632284193c07STejun Heo ret = PTR_ERR(ctx); 6323042f7df1SLai Jiangshan break; 6324042f7df1SLai Jiangshan } 6325042f7df1SLai Jiangshan 6326042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6327042f7df1SLai Jiangshan } 6328042f7df1SLai Jiangshan 6329042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6330042f7df1SLai Jiangshan if (!ret) 6331042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6332042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6333042f7df1SLai Jiangshan } 6334042f7df1SLai Jiangshan 633599c621efSLai Jiangshan if (!ret) { 633699c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 633799c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 633899c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 633999c621efSLai Jiangshan } 6340042f7df1SLai Jiangshan return ret; 6341042f7df1SLai Jiangshan } 6342042f7df1SLai Jiangshan 6343042f7df1SLai Jiangshan /** 6344fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6345fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6346fe28f631SWaiman Long * 6347fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6348fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6349fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6350fe28f631SWaiman Long */ 6351fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6352fe28f631SWaiman Long { 6353fe28f631SWaiman Long cpumask_var_t cpumask; 6354fe28f631SWaiman Long int ret = 0; 6355fe28f631SWaiman Long 6356fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6357fe28f631SWaiman Long return -ENOMEM; 6358fe28f631SWaiman Long 6359fe28f631SWaiman Long lockdep_assert_cpus_held(); 6360fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6361fe28f631SWaiman Long 6362fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6363fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6364fe28f631SWaiman Long 6365fe28f631SWaiman Long /* 6366fe28f631SWaiman Long * If the operation fails, it will fall back to 6367fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6368fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6369fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6370fe28f631SWaiman Long */ 6371fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6372fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6373fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6374fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6375fe28f631SWaiman Long 6376fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6377fe28f631SWaiman Long free_cpumask_var(cpumask); 6378fe28f631SWaiman Long return ret; 6379fe28f631SWaiman Long } 6380fe28f631SWaiman Long 638163c5484eSTejun Heo static int parse_affn_scope(const char *val) 638263c5484eSTejun Heo { 638363c5484eSTejun Heo int i; 638463c5484eSTejun Heo 638563c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 638663c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 638763c5484eSTejun Heo return i; 638863c5484eSTejun Heo } 638963c5484eSTejun Heo return -EINVAL; 639063c5484eSTejun Heo } 639163c5484eSTejun Heo 639263c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 639363c5484eSTejun Heo { 6394523a301eSTejun Heo struct workqueue_struct *wq; 6395523a301eSTejun Heo int affn, cpu; 639663c5484eSTejun Heo 639763c5484eSTejun Heo affn = parse_affn_scope(val); 639863c5484eSTejun Heo if (affn < 0) 639963c5484eSTejun Heo return affn; 6400523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6401523a301eSTejun Heo return -EINVAL; 6402523a301eSTejun Heo 6403523a301eSTejun Heo cpus_read_lock(); 6404523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 640563c5484eSTejun Heo 640663c5484eSTejun Heo wq_affn_dfl = affn; 6407523a301eSTejun Heo 6408523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6409523a301eSTejun Heo for_each_online_cpu(cpu) { 6410523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6411523a301eSTejun Heo } 6412523a301eSTejun Heo } 6413523a301eSTejun Heo 6414523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6415523a301eSTejun Heo cpus_read_unlock(); 6416523a301eSTejun Heo 641763c5484eSTejun Heo return 0; 641863c5484eSTejun Heo } 641963c5484eSTejun Heo 642063c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 642163c5484eSTejun Heo { 642263c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 642363c5484eSTejun Heo } 642463c5484eSTejun Heo 642563c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 642663c5484eSTejun Heo .set = wq_affn_dfl_set, 642763c5484eSTejun Heo .get = wq_affn_dfl_get, 642863c5484eSTejun Heo }; 642963c5484eSTejun Heo 643063c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 643163c5484eSTejun Heo 64326ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 64336ba94429SFrederic Weisbecker /* 64346ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 64356ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 64366ba94429SFrederic Weisbecker * following attributes. 64376ba94429SFrederic Weisbecker * 64386ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 64396ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 64406ba94429SFrederic Weisbecker * 64416ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 64426ba94429SFrederic Weisbecker * 64436ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 64446ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 644563c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 64468639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 64476ba94429SFrederic Weisbecker */ 64486ba94429SFrederic Weisbecker struct wq_device { 64496ba94429SFrederic Weisbecker struct workqueue_struct *wq; 64506ba94429SFrederic Weisbecker struct device dev; 64516ba94429SFrederic Weisbecker }; 64526ba94429SFrederic Weisbecker 64536ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 64546ba94429SFrederic Weisbecker { 64556ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 64566ba94429SFrederic Weisbecker 64576ba94429SFrederic Weisbecker return wq_dev->wq; 64586ba94429SFrederic Weisbecker } 64596ba94429SFrederic Weisbecker 64606ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 64616ba94429SFrederic Weisbecker char *buf) 64626ba94429SFrederic Weisbecker { 64636ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 64646ba94429SFrederic Weisbecker 64656ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 64666ba94429SFrederic Weisbecker } 64676ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 64686ba94429SFrederic Weisbecker 64696ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 64706ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 64716ba94429SFrederic Weisbecker { 64726ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 64736ba94429SFrederic Weisbecker 64746ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 64756ba94429SFrederic Weisbecker } 64766ba94429SFrederic Weisbecker 64776ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 64786ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 64796ba94429SFrederic Weisbecker size_t count) 64806ba94429SFrederic Weisbecker { 64816ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 64826ba94429SFrederic Weisbecker int val; 64836ba94429SFrederic Weisbecker 64846ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 64856ba94429SFrederic Weisbecker return -EINVAL; 64866ba94429SFrederic Weisbecker 64876ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 64886ba94429SFrederic Weisbecker return count; 64896ba94429SFrederic Weisbecker } 64906ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 64916ba94429SFrederic Weisbecker 64926ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 64936ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 64946ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 64956ba94429SFrederic Weisbecker NULL, 64966ba94429SFrederic Weisbecker }; 64976ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 64986ba94429SFrederic Weisbecker 649949277a5bSWaiman Long static void apply_wqattrs_lock(void) 650049277a5bSWaiman Long { 650149277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 650249277a5bSWaiman Long cpus_read_lock(); 650349277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 650449277a5bSWaiman Long } 650549277a5bSWaiman Long 650649277a5bSWaiman Long static void apply_wqattrs_unlock(void) 650749277a5bSWaiman Long { 650849277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 650949277a5bSWaiman Long cpus_read_unlock(); 651049277a5bSWaiman Long } 651149277a5bSWaiman Long 65126ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 65136ba94429SFrederic Weisbecker char *buf) 65146ba94429SFrederic Weisbecker { 65156ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65166ba94429SFrederic Weisbecker int written; 65176ba94429SFrederic Weisbecker 65186ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 65196ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 65206ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 65216ba94429SFrederic Weisbecker 65226ba94429SFrederic Weisbecker return written; 65236ba94429SFrederic Weisbecker } 65246ba94429SFrederic Weisbecker 65256ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 65266ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 65276ba94429SFrederic Weisbecker { 65286ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 65296ba94429SFrederic Weisbecker 6530899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6531899a94feSLai Jiangshan 6532be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 65336ba94429SFrederic Weisbecker if (!attrs) 65346ba94429SFrederic Weisbecker return NULL; 65356ba94429SFrederic Weisbecker 65366ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 65376ba94429SFrederic Weisbecker return attrs; 65386ba94429SFrederic Weisbecker } 65396ba94429SFrederic Weisbecker 65406ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 65416ba94429SFrederic Weisbecker const char *buf, size_t count) 65426ba94429SFrederic Weisbecker { 65436ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65446ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6545d4d3e257SLai Jiangshan int ret = -ENOMEM; 6546d4d3e257SLai Jiangshan 6547d4d3e257SLai Jiangshan apply_wqattrs_lock(); 65486ba94429SFrederic Weisbecker 65496ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 65506ba94429SFrederic Weisbecker if (!attrs) 6551d4d3e257SLai Jiangshan goto out_unlock; 65526ba94429SFrederic Weisbecker 65536ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 65546ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6555d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 65566ba94429SFrederic Weisbecker else 65576ba94429SFrederic Weisbecker ret = -EINVAL; 65586ba94429SFrederic Weisbecker 6559d4d3e257SLai Jiangshan out_unlock: 6560d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 65616ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 65626ba94429SFrederic Weisbecker return ret ?: count; 65636ba94429SFrederic Weisbecker } 65646ba94429SFrederic Weisbecker 65656ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 65666ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 65676ba94429SFrederic Weisbecker { 65686ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65696ba94429SFrederic Weisbecker int written; 65706ba94429SFrederic Weisbecker 65716ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 65726ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 65736ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 65746ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 65756ba94429SFrederic Weisbecker return written; 65766ba94429SFrederic Weisbecker } 65776ba94429SFrederic Weisbecker 65786ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 65796ba94429SFrederic Weisbecker struct device_attribute *attr, 65806ba94429SFrederic Weisbecker const char *buf, size_t count) 65816ba94429SFrederic Weisbecker { 65826ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 65836ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6584d4d3e257SLai Jiangshan int ret = -ENOMEM; 6585d4d3e257SLai Jiangshan 6586d4d3e257SLai Jiangshan apply_wqattrs_lock(); 65876ba94429SFrederic Weisbecker 65886ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 65896ba94429SFrederic Weisbecker if (!attrs) 6590d4d3e257SLai Jiangshan goto out_unlock; 65916ba94429SFrederic Weisbecker 65926ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 65936ba94429SFrederic Weisbecker if (!ret) 6594d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 65956ba94429SFrederic Weisbecker 6596d4d3e257SLai Jiangshan out_unlock: 6597d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 65986ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 65996ba94429SFrederic Weisbecker return ret ?: count; 66006ba94429SFrederic Weisbecker } 66016ba94429SFrederic Weisbecker 660263c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 660363c5484eSTejun Heo struct device_attribute *attr, char *buf) 660463c5484eSTejun Heo { 660563c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 660663c5484eSTejun Heo int written; 660763c5484eSTejun Heo 660863c5484eSTejun Heo mutex_lock(&wq->mutex); 6609523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6610523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6611523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6612523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6613523a301eSTejun Heo else 661463c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 661563c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 661663c5484eSTejun Heo mutex_unlock(&wq->mutex); 661763c5484eSTejun Heo 661863c5484eSTejun Heo return written; 661963c5484eSTejun Heo } 662063c5484eSTejun Heo 662163c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 662263c5484eSTejun Heo struct device_attribute *attr, 662363c5484eSTejun Heo const char *buf, size_t count) 662463c5484eSTejun Heo { 662563c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 662663c5484eSTejun Heo struct workqueue_attrs *attrs; 662763c5484eSTejun Heo int affn, ret = -ENOMEM; 662863c5484eSTejun Heo 662963c5484eSTejun Heo affn = parse_affn_scope(buf); 663063c5484eSTejun Heo if (affn < 0) 663163c5484eSTejun Heo return affn; 663263c5484eSTejun Heo 663363c5484eSTejun Heo apply_wqattrs_lock(); 663463c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 663563c5484eSTejun Heo if (attrs) { 663663c5484eSTejun Heo attrs->affn_scope = affn; 663763c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 663863c5484eSTejun Heo } 663963c5484eSTejun Heo apply_wqattrs_unlock(); 664063c5484eSTejun Heo free_workqueue_attrs(attrs); 664163c5484eSTejun Heo return ret ?: count; 664263c5484eSTejun Heo } 664363c5484eSTejun Heo 66448639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 66458639ecebSTejun Heo struct device_attribute *attr, char *buf) 66468639ecebSTejun Heo { 66478639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 66488639ecebSTejun Heo 66498639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 66508639ecebSTejun Heo wq->unbound_attrs->affn_strict); 66518639ecebSTejun Heo } 66528639ecebSTejun Heo 66538639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 66548639ecebSTejun Heo struct device_attribute *attr, 66558639ecebSTejun Heo const char *buf, size_t count) 66568639ecebSTejun Heo { 66578639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 66588639ecebSTejun Heo struct workqueue_attrs *attrs; 66598639ecebSTejun Heo int v, ret = -ENOMEM; 66608639ecebSTejun Heo 66618639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 66628639ecebSTejun Heo return -EINVAL; 66638639ecebSTejun Heo 66648639ecebSTejun Heo apply_wqattrs_lock(); 66658639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 66668639ecebSTejun Heo if (attrs) { 66678639ecebSTejun Heo attrs->affn_strict = (bool)v; 66688639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 66698639ecebSTejun Heo } 66708639ecebSTejun Heo apply_wqattrs_unlock(); 66718639ecebSTejun Heo free_workqueue_attrs(attrs); 66728639ecebSTejun Heo return ret ?: count; 66738639ecebSTejun Heo } 66748639ecebSTejun Heo 66756ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 66766ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 66776ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 667863c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 66798639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 66806ba94429SFrederic Weisbecker __ATTR_NULL, 66816ba94429SFrederic Weisbecker }; 66826ba94429SFrederic Weisbecker 66836ba94429SFrederic Weisbecker static struct bus_type wq_subsys = { 66846ba94429SFrederic Weisbecker .name = "workqueue", 66856ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 66866ba94429SFrederic Weisbecker }; 66876ba94429SFrederic Weisbecker 668849277a5bSWaiman Long /** 668949277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 669049277a5bSWaiman Long * @cpumask: the cpumask to set 669149277a5bSWaiman Long * 669249277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 669349277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 669449277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 669549277a5bSWaiman Long * 669649277a5bSWaiman Long * Return: 0 - Success 669749277a5bSWaiman Long * -EINVAL - Invalid @cpumask 669849277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 669949277a5bSWaiman Long */ 670049277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 670149277a5bSWaiman Long { 670249277a5bSWaiman Long int ret = -EINVAL; 670349277a5bSWaiman Long 670449277a5bSWaiman Long /* 670549277a5bSWaiman Long * Not excluding isolated cpus on purpose. 670649277a5bSWaiman Long * If the user wishes to include them, we allow that. 670749277a5bSWaiman Long */ 670849277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 670949277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 671049277a5bSWaiman Long apply_wqattrs_lock(); 671149277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 671249277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 671349277a5bSWaiman Long ret = 0; 671449277a5bSWaiman Long goto out_unlock; 671549277a5bSWaiman Long } 671649277a5bSWaiman Long 671749277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 671849277a5bSWaiman Long 671949277a5bSWaiman Long out_unlock: 672049277a5bSWaiman Long apply_wqattrs_unlock(); 672149277a5bSWaiman Long } 672249277a5bSWaiman Long 672349277a5bSWaiman Long return ret; 672449277a5bSWaiman Long } 672549277a5bSWaiman Long 6726fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 6727fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 6728b05a7928SFrederic Weisbecker { 6729b05a7928SFrederic Weisbecker int written; 6730b05a7928SFrederic Weisbecker 6731042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 6732fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 6733042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6734b05a7928SFrederic Weisbecker 6735b05a7928SFrederic Weisbecker return written; 6736b05a7928SFrederic Weisbecker } 6737b05a7928SFrederic Weisbecker 6738fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 6739fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6740fe28f631SWaiman Long { 6741fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 6742fe28f631SWaiman Long } 6743fe28f631SWaiman Long 6744fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 6745fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6746fe28f631SWaiman Long { 6747fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 6748fe28f631SWaiman Long } 6749fe28f631SWaiman Long 6750fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 6751fe28f631SWaiman Long struct device_attribute *attr, char *buf) 6752fe28f631SWaiman Long { 6753fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 6754fe28f631SWaiman Long } 6755fe28f631SWaiman Long 6756042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 6757042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 6758042f7df1SLai Jiangshan { 6759042f7df1SLai Jiangshan cpumask_var_t cpumask; 6760042f7df1SLai Jiangshan int ret; 6761042f7df1SLai Jiangshan 6762042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6763042f7df1SLai Jiangshan return -ENOMEM; 6764042f7df1SLai Jiangshan 6765042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 6766042f7df1SLai Jiangshan if (!ret) 6767042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 6768042f7df1SLai Jiangshan 6769042f7df1SLai Jiangshan free_cpumask_var(cpumask); 6770042f7df1SLai Jiangshan return ret ? ret : count; 6771042f7df1SLai Jiangshan } 6772042f7df1SLai Jiangshan 6773fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 6774042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 6775fe28f631SWaiman Long wq_unbound_cpumask_store), 6776fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 6777fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 6778fe28f631SWaiman Long __ATTR_NULL, 6779fe28f631SWaiman Long }; 6780b05a7928SFrederic Weisbecker 67816ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 67826ba94429SFrederic Weisbecker { 6783686f6697SGreg Kroah-Hartman struct device *dev_root; 6784b05a7928SFrederic Weisbecker int err; 6785b05a7928SFrederic Weisbecker 6786b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 6787b05a7928SFrederic Weisbecker if (err) 6788b05a7928SFrederic Weisbecker return err; 6789b05a7928SFrederic Weisbecker 6790686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 6791686f6697SGreg Kroah-Hartman if (dev_root) { 6792fe28f631SWaiman Long struct device_attribute *attr; 6793fe28f631SWaiman Long 6794fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 6795fe28f631SWaiman Long err = device_create_file(dev_root, attr); 6796fe28f631SWaiman Long if (err) 6797fe28f631SWaiman Long break; 6798fe28f631SWaiman Long } 6799686f6697SGreg Kroah-Hartman put_device(dev_root); 6800686f6697SGreg Kroah-Hartman } 6801686f6697SGreg Kroah-Hartman return err; 68026ba94429SFrederic Weisbecker } 68036ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 68046ba94429SFrederic Weisbecker 68056ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 68066ba94429SFrederic Weisbecker { 68076ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 68086ba94429SFrederic Weisbecker 68096ba94429SFrederic Weisbecker kfree(wq_dev); 68106ba94429SFrederic Weisbecker } 68116ba94429SFrederic Weisbecker 68126ba94429SFrederic Weisbecker /** 68136ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 68146ba94429SFrederic Weisbecker * @wq: the workqueue to register 68156ba94429SFrederic Weisbecker * 68166ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 68176ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 68186ba94429SFrederic Weisbecker * which is the preferred method. 68196ba94429SFrederic Weisbecker * 68206ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 68216ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 68226ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 68236ba94429SFrederic Weisbecker * attributes. 68246ba94429SFrederic Weisbecker * 68256ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 68266ba94429SFrederic Weisbecker */ 68276ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 68286ba94429SFrederic Weisbecker { 68296ba94429SFrederic Weisbecker struct wq_device *wq_dev; 68306ba94429SFrederic Weisbecker int ret; 68316ba94429SFrederic Weisbecker 68326ba94429SFrederic Weisbecker /* 6833402dd89dSShailendra Verma * Adjusting max_active or creating new pwqs by applying 68346ba94429SFrederic Weisbecker * attributes breaks ordering guarantee. Disallow exposing ordered 68356ba94429SFrederic Weisbecker * workqueues. 68366ba94429SFrederic Weisbecker */ 68370a94efb5STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED_EXPLICIT)) 68386ba94429SFrederic Weisbecker return -EINVAL; 68396ba94429SFrederic Weisbecker 68406ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 68416ba94429SFrederic Weisbecker if (!wq_dev) 68426ba94429SFrederic Weisbecker return -ENOMEM; 68436ba94429SFrederic Weisbecker 68446ba94429SFrederic Weisbecker wq_dev->wq = wq; 68456ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 68466ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 684723217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 68486ba94429SFrederic Weisbecker 68496ba94429SFrederic Weisbecker /* 68506ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 68516ba94429SFrederic Weisbecker * everything is ready. 68526ba94429SFrederic Weisbecker */ 68536ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 68546ba94429SFrederic Weisbecker 68556ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 68566ba94429SFrederic Weisbecker if (ret) { 6857537f4146SArvind Yadav put_device(&wq_dev->dev); 68586ba94429SFrederic Weisbecker wq->wq_dev = NULL; 68596ba94429SFrederic Weisbecker return ret; 68606ba94429SFrederic Weisbecker } 68616ba94429SFrederic Weisbecker 68626ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 68636ba94429SFrederic Weisbecker struct device_attribute *attr; 68646ba94429SFrederic Weisbecker 68656ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 68666ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 68676ba94429SFrederic Weisbecker if (ret) { 68686ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 68696ba94429SFrederic Weisbecker wq->wq_dev = NULL; 68706ba94429SFrederic Weisbecker return ret; 68716ba94429SFrederic Weisbecker } 68726ba94429SFrederic Weisbecker } 68736ba94429SFrederic Weisbecker } 68746ba94429SFrederic Weisbecker 68756ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 68766ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 68776ba94429SFrederic Weisbecker return 0; 68786ba94429SFrederic Weisbecker } 68796ba94429SFrederic Weisbecker 68806ba94429SFrederic Weisbecker /** 68816ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 68826ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 68836ba94429SFrederic Weisbecker * 68846ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 68856ba94429SFrederic Weisbecker */ 68866ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 68876ba94429SFrederic Weisbecker { 68886ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 68896ba94429SFrederic Weisbecker 68906ba94429SFrederic Weisbecker if (!wq->wq_dev) 68916ba94429SFrederic Weisbecker return; 68926ba94429SFrederic Weisbecker 68936ba94429SFrederic Weisbecker wq->wq_dev = NULL; 68946ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 68956ba94429SFrederic Weisbecker } 68966ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 68976ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 68986ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 68996ba94429SFrederic Weisbecker 690082607adcSTejun Heo /* 690182607adcSTejun Heo * Workqueue watchdog. 690282607adcSTejun Heo * 690382607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 690482607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 690582607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 690682607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 690782607adcSTejun Heo * largely opaque. 690882607adcSTejun Heo * 690982607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 691082607adcSTejun Heo * state if some pools failed to make forward progress for a while where 691182607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 691282607adcSTejun Heo * 691382607adcSTejun Heo * This mechanism is controlled through the kernel parameter 691482607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 691582607adcSTejun Heo * corresponding sysfs parameter file. 691682607adcSTejun Heo */ 691782607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 691882607adcSTejun Heo 691982607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 69205cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 692182607adcSTejun Heo 692282607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 692382607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 692482607adcSTejun Heo 6925cd2440d6SPetr Mladek /* 6926cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 6927cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 6928cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 6929cd2440d6SPetr Mladek * in all other situations. 6930cd2440d6SPetr Mladek */ 6931cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 6932cd2440d6SPetr Mladek { 6933cd2440d6SPetr Mladek struct worker *worker; 6934cd2440d6SPetr Mladek unsigned long flags; 6935cd2440d6SPetr Mladek int bkt; 6936cd2440d6SPetr Mladek 6937cd2440d6SPetr Mladek raw_spin_lock_irqsave(&pool->lock, flags); 6938cd2440d6SPetr Mladek 6939cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 6940cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 6941cd2440d6SPetr Mladek /* 6942cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 6943cd2440d6SPetr Mladek * drivers that queue work while holding locks 6944cd2440d6SPetr Mladek * also taken in their write paths. 6945cd2440d6SPetr Mladek */ 6946cd2440d6SPetr Mladek printk_deferred_enter(); 6947cd2440d6SPetr Mladek 6948cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 6949cd2440d6SPetr Mladek sched_show_task(worker->task); 6950cd2440d6SPetr Mladek 6951cd2440d6SPetr Mladek printk_deferred_exit(); 6952cd2440d6SPetr Mladek } 6953cd2440d6SPetr Mladek } 6954cd2440d6SPetr Mladek 6955cd2440d6SPetr Mladek raw_spin_unlock_irqrestore(&pool->lock, flags); 6956cd2440d6SPetr Mladek } 6957cd2440d6SPetr Mladek 6958cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 6959cd2440d6SPetr Mladek { 6960cd2440d6SPetr Mladek struct worker_pool *pool; 6961cd2440d6SPetr Mladek int pi; 6962cd2440d6SPetr Mladek 6963cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 6964cd2440d6SPetr Mladek 6965cd2440d6SPetr Mladek rcu_read_lock(); 6966cd2440d6SPetr Mladek 6967cd2440d6SPetr Mladek for_each_pool(pool, pi) { 6968cd2440d6SPetr Mladek if (pool->cpu_stall) 6969cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 6970cd2440d6SPetr Mladek 6971cd2440d6SPetr Mladek } 6972cd2440d6SPetr Mladek 6973cd2440d6SPetr Mladek rcu_read_unlock(); 6974cd2440d6SPetr Mladek } 6975cd2440d6SPetr Mladek 697682607adcSTejun Heo static void wq_watchdog_reset_touched(void) 697782607adcSTejun Heo { 697882607adcSTejun Heo int cpu; 697982607adcSTejun Heo 698082607adcSTejun Heo wq_watchdog_touched = jiffies; 698182607adcSTejun Heo for_each_possible_cpu(cpu) 698282607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 698382607adcSTejun Heo } 698482607adcSTejun Heo 69855cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 698682607adcSTejun Heo { 698782607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 698882607adcSTejun Heo bool lockup_detected = false; 6989cd2440d6SPetr Mladek bool cpu_pool_stall = false; 6990940d71c6SSergey Senozhatsky unsigned long now = jiffies; 699182607adcSTejun Heo struct worker_pool *pool; 699282607adcSTejun Heo int pi; 699382607adcSTejun Heo 699482607adcSTejun Heo if (!thresh) 699582607adcSTejun Heo return; 699682607adcSTejun Heo 699782607adcSTejun Heo rcu_read_lock(); 699882607adcSTejun Heo 699982607adcSTejun Heo for_each_pool(pool, pi) { 700082607adcSTejun Heo unsigned long pool_ts, touched, ts; 700182607adcSTejun Heo 7002cd2440d6SPetr Mladek pool->cpu_stall = false; 700382607adcSTejun Heo if (list_empty(&pool->worklist)) 700482607adcSTejun Heo continue; 700582607adcSTejun Heo 7006940d71c6SSergey Senozhatsky /* 7007940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7008940d71c6SSergey Senozhatsky * the watchdog like a stall. 7009940d71c6SSergey Senozhatsky */ 7010940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7011940d71c6SSergey Senozhatsky 701282607adcSTejun Heo /* get the latest of pool and touched timestamps */ 701389e28ce6SWang Qing if (pool->cpu >= 0) 701489e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 701589e28ce6SWang Qing else 701682607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 701789e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 701882607adcSTejun Heo 701982607adcSTejun Heo if (time_after(pool_ts, touched)) 702082607adcSTejun Heo ts = pool_ts; 702182607adcSTejun Heo else 702282607adcSTejun Heo ts = touched; 702382607adcSTejun Heo 702482607adcSTejun Heo /* did we stall? */ 7025940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 702682607adcSTejun Heo lockup_detected = true; 7027cd2440d6SPetr Mladek if (pool->cpu >= 0) { 7028cd2440d6SPetr Mladek pool->cpu_stall = true; 7029cd2440d6SPetr Mladek cpu_pool_stall = true; 7030cd2440d6SPetr Mladek } 703182607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 703282607adcSTejun Heo pr_cont_pool_info(pool); 703382607adcSTejun Heo pr_cont(" stuck for %us!\n", 7034940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 703582607adcSTejun Heo } 7036cd2440d6SPetr Mladek 7037cd2440d6SPetr Mladek 703882607adcSTejun Heo } 703982607adcSTejun Heo 704082607adcSTejun Heo rcu_read_unlock(); 704182607adcSTejun Heo 704282607adcSTejun Heo if (lockup_detected) 704355df0933SImran Khan show_all_workqueues(); 704482607adcSTejun Heo 7045cd2440d6SPetr Mladek if (cpu_pool_stall) 7046cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7047cd2440d6SPetr Mladek 704882607adcSTejun Heo wq_watchdog_reset_touched(); 704982607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 705082607adcSTejun Heo } 705182607adcSTejun Heo 7052cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 705382607adcSTejun Heo { 705482607adcSTejun Heo if (cpu >= 0) 705582607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 705689e28ce6SWang Qing 705782607adcSTejun Heo wq_watchdog_touched = jiffies; 705882607adcSTejun Heo } 705982607adcSTejun Heo 706082607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 706182607adcSTejun Heo { 706282607adcSTejun Heo wq_watchdog_thresh = 0; 706382607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 706482607adcSTejun Heo 706582607adcSTejun Heo if (thresh) { 706682607adcSTejun Heo wq_watchdog_thresh = thresh; 706782607adcSTejun Heo wq_watchdog_reset_touched(); 706882607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 706982607adcSTejun Heo } 707082607adcSTejun Heo } 707182607adcSTejun Heo 707282607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 707382607adcSTejun Heo const struct kernel_param *kp) 707482607adcSTejun Heo { 707582607adcSTejun Heo unsigned long thresh; 707682607adcSTejun Heo int ret; 707782607adcSTejun Heo 707882607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 707982607adcSTejun Heo if (ret) 708082607adcSTejun Heo return ret; 708182607adcSTejun Heo 708282607adcSTejun Heo if (system_wq) 708382607adcSTejun Heo wq_watchdog_set_thresh(thresh); 708482607adcSTejun Heo else 708582607adcSTejun Heo wq_watchdog_thresh = thresh; 708682607adcSTejun Heo 708782607adcSTejun Heo return 0; 708882607adcSTejun Heo } 708982607adcSTejun Heo 709082607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 709182607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 709282607adcSTejun Heo .get = param_get_ulong, 709382607adcSTejun Heo }; 709482607adcSTejun Heo 709582607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 709682607adcSTejun Heo 0644); 709782607adcSTejun Heo 709882607adcSTejun Heo static void wq_watchdog_init(void) 709982607adcSTejun Heo { 71005cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 710182607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 710282607adcSTejun Heo } 710382607adcSTejun Heo 710482607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 710582607adcSTejun Heo 710682607adcSTejun Heo static inline void wq_watchdog_init(void) { } 710782607adcSTejun Heo 710882607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 710982607adcSTejun Heo 71104a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 71114a6c5607STejun Heo { 71124a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 71134a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 71144a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 71154a6c5607STejun Heo return; 71164a6c5607STejun Heo } 71174a6c5607STejun Heo 71184a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 71194a6c5607STejun Heo } 71204a6c5607STejun Heo 71213347fa09STejun Heo /** 71223347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 71233347fa09STejun Heo * 71242930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 71252930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 71262930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 71272930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 71282930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 71292930155bSTejun Heo * before early initcalls. 71303347fa09STejun Heo */ 71312333e829SYu Chen void __init workqueue_init_early(void) 71321da177e4SLinus Torvalds { 713384193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 71347a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 71357a4e344cSTejun Heo int i, cpu; 7136c34056a3STejun Heo 713710cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7138e904e6c2STejun Heo 7139b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7140fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7141fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7142b05a7928SFrederic Weisbecker 71434a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 71444a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 71454a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7146ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 71474a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7148ace3c549Stiozhang 7149fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 71508b03ae3cSTejun Heo 71518b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7152e22bee78STejun Heo 71532930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 71542930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 71552930155bSTejun Heo 71567bd20b6bSMarcelo Tosatti /* 71577bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 71587bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 71597bd20b6bSMarcelo Tosatti */ 71607bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 71617bd20b6bSMarcelo Tosatti wq_power_efficient = true; 71627bd20b6bSMarcelo Tosatti 716384193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 716484193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 716584193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 716684193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 716784193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 716884193c07STejun Heo 716984193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 717084193c07STejun Heo 717184193c07STejun Heo pt->nr_pods = 1; 717284193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 717384193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 717484193c07STejun Heo pt->cpu_pod[0] = 0; 717584193c07STejun Heo 7176f02ae73aSTejun Heo /* initialize CPU pools */ 717724647570STejun Heo for_each_possible_cpu(cpu) { 7178ebf44d16STejun Heo struct worker_pool *pool; 7179e22bee78STejun Heo 71804ce62e9eSTejun Heo i = 0; 7181e22bee78STejun Heo for_each_cpu_worker_pool(pool, cpu) { 718229c91e99STejun Heo BUG_ON(init_worker_pool(pool)); 718329c91e99STejun Heo pool->cpu = cpu; 718429c91e99STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 71859546b29eSTejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 71861da177e4SLinus Torvalds pool->attrs->nice = std_nice[i++]; 71878639ecebSTejun Heo pool->attrs->affn_strict = true; 71881da177e4SLinus Torvalds pool->node = cpu_to_node(cpu); 71891da177e4SLinus Torvalds 71901da177e4SLinus Torvalds /* alloc pool ID */ 71911da177e4SLinus Torvalds mutex_lock(&wq_pool_mutex); 71921da177e4SLinus Torvalds BUG_ON(worker_pool_assign_id(pool)); 71931da177e4SLinus Torvalds mutex_unlock(&wq_pool_mutex); 719429c91e99STejun Heo } 719529c91e99STejun Heo } 719629c91e99STejun Heo 71978a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 719829c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 719929c91e99STejun Heo struct workqueue_attrs *attrs; 720029c91e99STejun Heo 7201be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 720229c91e99STejun Heo attrs->nice = std_nice[i]; 720329c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 72048a2b7538STejun Heo 72058a2b7538STejun Heo /* 72068a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 72078a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 72088a2b7538STejun Heo */ 7209be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 72108a2b7538STejun Heo attrs->nice = std_nice[i]; 7211af73f5c9STejun Heo attrs->ordered = true; 72128a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 721329c91e99STejun Heo } 721429c91e99STejun Heo 72151da177e4SLinus Torvalds system_wq = alloc_workqueue("events", 0, 0); 72161aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 72171da177e4SLinus Torvalds system_long_wq = alloc_workqueue("events_long", 0, 0); 72181da177e4SLinus Torvalds system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7219636b927eSTejun Heo WQ_MAX_ACTIVE); 72201da177e4SLinus Torvalds system_freezable_wq = alloc_workqueue("events_freezable", 72211da177e4SLinus Torvalds WQ_FREEZABLE, 0); 72220668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 72230668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 72248318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 72250668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 72260668106cSViresh Kumar 0); 72271aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 72280668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 72290668106cSViresh Kumar !system_power_efficient_wq || 72300668106cSViresh Kumar !system_freezable_power_efficient_wq); 72313347fa09STejun Heo } 72323347fa09STejun Heo 7233aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7234aa6fde93STejun Heo { 7235aa6fde93STejun Heo unsigned long thresh; 7236aa6fde93STejun Heo unsigned long bogo; 7237aa6fde93STejun Heo 7238dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7239dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7240dd64c873SZqiang 7241aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7242aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7243aa6fde93STejun Heo return; 7244aa6fde93STejun Heo 7245aa6fde93STejun Heo /* 7246aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7247aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7248aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7249aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7250aa6fde93STejun Heo * too low. 7251aa6fde93STejun Heo * 7252aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7253aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7254aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7255aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7256aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7257aa6fde93STejun Heo * usefulness. 7258aa6fde93STejun Heo */ 7259aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7260aa6fde93STejun Heo 7261aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7262aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7263aa6fde93STejun Heo if (bogo < 4000) 7264aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7265aa6fde93STejun Heo 7266aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7267aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7268aa6fde93STejun Heo 7269aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7270aa6fde93STejun Heo } 7271aa6fde93STejun Heo 72723347fa09STejun Heo /** 72733347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 72743347fa09STejun Heo * 72752930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 72762930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 72772930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 72782930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 72792930155bSTejun Heo * workers and enable future kworker creations. 72803347fa09STejun Heo */ 72812333e829SYu Chen void __init workqueue_init(void) 72823347fa09STejun Heo { 72832186d9f9STejun Heo struct workqueue_struct *wq; 72843347fa09STejun Heo struct worker_pool *pool; 72853347fa09STejun Heo int cpu, bkt; 72863347fa09STejun Heo 7287aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7288aa6fde93STejun Heo 72892186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 72902186d9f9STejun Heo 72912930155bSTejun Heo /* 72922930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 72932930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 72942930155bSTejun Heo */ 72952186d9f9STejun Heo for_each_possible_cpu(cpu) { 72962186d9f9STejun Heo for_each_cpu_worker_pool(pool, cpu) { 72972186d9f9STejun Heo pool->node = cpu_to_node(cpu); 72982186d9f9STejun Heo } 72992186d9f9STejun Heo } 73002186d9f9STejun Heo 730140c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 730240c17f75STejun Heo WARN(init_rescuer(wq), 730340c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 730440c17f75STejun Heo wq->name); 730540c17f75STejun Heo } 73062186d9f9STejun Heo 73072186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 73082186d9f9STejun Heo 73093347fa09STejun Heo /* create the initial workers */ 73103347fa09STejun Heo for_each_online_cpu(cpu) { 73113347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 73123347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 73133347fa09STejun Heo BUG_ON(!create_worker(pool)); 73143347fa09STejun Heo } 73153347fa09STejun Heo } 73163347fa09STejun Heo 73173347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 73183347fa09STejun Heo BUG_ON(!create_worker(pool)); 73193347fa09STejun Heo 73203347fa09STejun Heo wq_online = true; 732182607adcSTejun Heo wq_watchdog_init(); 73221da177e4SLinus Torvalds } 7323c4f135d6STetsuo Handa 7324025e1684STejun Heo /* 7325025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7326025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7327025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7328025e1684STejun Heo */ 7329025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7330025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7331025e1684STejun Heo { 7332025e1684STejun Heo int cur, pre, cpu, pod; 7333025e1684STejun Heo 7334025e1684STejun Heo pt->nr_pods = 0; 7335025e1684STejun Heo 7336025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7337025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7338025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7339025e1684STejun Heo 7340025e1684STejun Heo for_each_possible_cpu(cur) { 7341025e1684STejun Heo for_each_possible_cpu(pre) { 7342025e1684STejun Heo if (pre >= cur) { 7343025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7344025e1684STejun Heo break; 7345025e1684STejun Heo } 7346025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7347025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7348025e1684STejun Heo break; 7349025e1684STejun Heo } 7350025e1684STejun Heo } 7351025e1684STejun Heo } 7352025e1684STejun Heo 7353025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7354025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7355025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7356025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7357025e1684STejun Heo 7358025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7359025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7360025e1684STejun Heo 7361025e1684STejun Heo for_each_possible_cpu(cpu) { 7362025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7363025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7364025e1684STejun Heo } 7365025e1684STejun Heo } 7366025e1684STejun Heo 736763c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 736863c5484eSTejun Heo { 736963c5484eSTejun Heo return false; 737063c5484eSTejun Heo } 737163c5484eSTejun Heo 737263c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 737363c5484eSTejun Heo { 737463c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 737563c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 737663c5484eSTejun Heo #else 737763c5484eSTejun Heo return false; 737863c5484eSTejun Heo #endif 737963c5484eSTejun Heo } 738063c5484eSTejun Heo 7381025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7382025e1684STejun Heo { 7383025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7384025e1684STejun Heo } 7385025e1684STejun Heo 73862930155bSTejun Heo /** 73872930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 73882930155bSTejun Heo * 73892930155bSTejun Heo * This is the third step of there-staged workqueue subsystem initialization and 73902930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 73912930155bSTejun Heo * initializes the unbound CPU pods accordingly. 73922930155bSTejun Heo */ 73932930155bSTejun Heo void __init workqueue_init_topology(void) 7394a86feae6STejun Heo { 73952930155bSTejun Heo struct workqueue_struct *wq; 7396025e1684STejun Heo int cpu; 7397a86feae6STejun Heo 739863c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 739963c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 740063c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7401025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7402a86feae6STejun Heo 74032930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7404a86feae6STejun Heo 7405a86feae6STejun Heo /* 74062930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 74072930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 74082930155bSTejun Heo * combinations to apply per-pod sharing. 74092930155bSTejun Heo */ 74102930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 74115797b1c1STejun Heo for_each_online_cpu(cpu) 74122930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 74135797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 74145797b1c1STejun Heo mutex_lock(&wq->mutex); 74155797b1c1STejun Heo wq_update_node_max_active(wq, -1); 74165797b1c1STejun Heo mutex_unlock(&wq->mutex); 74172930155bSTejun Heo } 74182930155bSTejun Heo } 74192930155bSTejun Heo 74202930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7421a86feae6STejun Heo } 7422a86feae6STejun Heo 742320bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 742420bdedafSTetsuo Handa { 742520bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 742620bdedafSTetsuo Handa dump_stack(); 742720bdedafSTetsuo Handa } 7428c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7429ace3c549Stiozhang 7430ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7431ace3c549Stiozhang { 7432ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7433ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7434ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7435ace3c549Stiozhang } 7436ace3c549Stiozhang 7437ace3c549Stiozhang return 1; 7438ace3c549Stiozhang } 7439ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7440