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> 324cb1ef64STejun Heo #include <linux/interrupt.h> 331da177e4SLinus Torvalds #include <linux/signal.h> 341da177e4SLinus Torvalds #include <linux/completion.h> 351da177e4SLinus Torvalds #include <linux/workqueue.h> 361da177e4SLinus Torvalds #include <linux/slab.h> 371da177e4SLinus Torvalds #include <linux/cpu.h> 381da177e4SLinus Torvalds #include <linux/notifier.h> 391da177e4SLinus Torvalds #include <linux/kthread.h> 401fa44ecaSJames Bottomley #include <linux/hardirq.h> 4146934023SChristoph Lameter #include <linux/mempolicy.h> 42341a5958SRafael J. Wysocki #include <linux/freezer.h> 43d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 444e6045f1SJohannes Berg #include <linux/lockdep.h> 45c34056a3STejun Heo #include <linux/idr.h> 4629c91e99STejun Heo #include <linux/jhash.h> 4742f8570fSSasha Levin #include <linux/hashtable.h> 4876af4d93STejun Heo #include <linux/rculist.h> 49bce90380STejun Heo #include <linux/nodemask.h> 504c16bd32STejun Heo #include <linux/moduleparam.h> 513d1cb205STejun Heo #include <linux/uaccess.h> 52c98a9805STal Shorer #include <linux/sched/isolation.h> 53cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5462635ea8SSergey Senozhatsky #include <linux/nmi.h> 55940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 56aa6fde93STejun Heo #include <linux/delay.h> 572f34d733STejun Heo #include <linux/irq_work.h> 58e22bee78STejun Heo 59ea138446STejun Heo #include "workqueue_internal.h" 601da177e4SLinus Torvalds 61e563d0a7STejun Heo enum worker_pool_flags { 62bc2ae0f5STejun Heo /* 6324647570STejun Heo * worker_pool flags 64bc2ae0f5STejun Heo * 6524647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 66bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 67bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 68bc2ae0f5STejun Heo * is in effect. 69bc2ae0f5STejun Heo * 70bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 71bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7224647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 73bc2ae0f5STejun Heo * 74bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 751258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 764736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 774cb1ef64STejun Heo * 784cb1ef64STejun Heo * As there can only be one concurrent BH execution context per CPU, a 794cb1ef64STejun Heo * BH pool is per-CPU and always DISASSOCIATED. 80bc2ae0f5STejun Heo */ 814cb1ef64STejun Heo POOL_BH = 1 << 0, /* is a BH pool */ 824cb1ef64STejun Heo POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */ 8324647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 84e563d0a7STejun Heo }; 85db7bccf4STejun Heo 86e563d0a7STejun Heo enum worker_flags { 87c8e55f36STejun Heo /* worker flags */ 88c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 89c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 90e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 91fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 92f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 93a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 94e22bee78STejun Heo 95a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 96a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 97e563d0a7STejun Heo }; 98db7bccf4STejun Heo 99c5f5b942STejun Heo enum work_cancel_flags { 100c5f5b942STejun Heo WORK_CANCEL_DELAYED = 1 << 0, /* canceling a delayed_work */ 101c5f5b942STejun Heo }; 102c5f5b942STejun Heo 103e563d0a7STejun Heo enum wq_internal_consts { 104e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 1054ce62e9eSTejun Heo 10629c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 107c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 108db7bccf4STejun Heo 109e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 110e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 111e22bee78STejun Heo 1123233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1133233cdbdSTejun Heo /* call for help after 10ms 1143233cdbdSTejun Heo (min two ticks) */ 115e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 116e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 119e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1208698a745SDongsheng Yang * all cpus. Give MIN_NICE. 121e22bee78STejun Heo */ 1228698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1238698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 124ecf6881fSTejun Heo 12531c89007SAudra Mitchell WQ_NAME_LEN = 32, 126c8e55f36STejun Heo }; 127c8e55f36STejun Heo 1281da177e4SLinus Torvalds /* 1294cb1ef64STejun Heo * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and 1304cb1ef64STejun Heo * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because 1314cb1ef64STejun Heo * msecs_to_jiffies() can't be an initializer. 1324cb1ef64STejun Heo */ 1334cb1ef64STejun Heo #define BH_WORKER_JIFFIES msecs_to_jiffies(2) 1344cb1ef64STejun Heo #define BH_WORKER_RESTARTS 10 1354cb1ef64STejun Heo 1364cb1ef64STejun Heo /* 1374690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1384690c4abSTejun Heo * 139e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 140e41e704bSTejun Heo * everyone else. 1414690c4abSTejun Heo * 142e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 143e22bee78STejun Heo * only be modified and accessed from the local cpu. 144e22bee78STejun Heo * 145d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1464690c4abSTejun Heo * 1475797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for 1485797b1c1STejun Heo * reads. 1495797b1c1STejun Heo * 150bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 151bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 152bdf8b9bfSTejun Heo * kworker. 153bdf8b9bfSTejun Heo * 154bdf8b9bfSTejun Heo * S: Only modified by worker self. 155bdf8b9bfSTejun Heo * 1561258fae7STejun Heo * A: wq_pool_attach_mutex protected. 157822d8405STejun Heo * 15868e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 15976af4d93STejun Heo * 16024acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1615bcab335STejun Heo * 1625b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1635b95e1afSLai Jiangshan * 1645b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 16524acfb71SThomas Gleixner * RCU for reads. 1665b95e1afSLai Jiangshan * 1673c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1683c25a55dSLai Jiangshan * 16924acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1702e109a28STejun Heo * 171a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 172a045a272STejun Heo * with READ_ONCE() without locking. 173a045a272STejun Heo * 1742e109a28STejun Heo * MD: wq_mayday_lock protected. 175cd2440d6SPetr Mladek * 176cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1774690c4abSTejun Heo */ 1784690c4abSTejun Heo 1792eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 180c34056a3STejun Heo 181bd7bdd43STejun Heo struct worker_pool { 182a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 183d84ff051STejun Heo int cpu; /* I: the associated cpu */ 184f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1859daf9e67STejun Heo int id; /* I: pool ID */ 186bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 187bd7bdd43STejun Heo 18882607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 189cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 19082607adcSTejun Heo 191bc35f7efSLai Jiangshan /* 192bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 193bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 194bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 195bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 196bc35f7efSLai Jiangshan */ 197bc35f7efSLai Jiangshan int nr_running; 19884f91c62SLai Jiangshan 199bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 200ea1abd61SLai Jiangshan 2015826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 2025826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 203bd7bdd43STejun Heo 2042c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 205bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 2063f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 2073f959aa3SValentin Schneider 208bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 209bd7bdd43STejun Heo 210c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 211c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 212c9e7cf27STejun Heo /* L: hash of busy workers */ 213c9e7cf27STejun Heo 2142607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 21592f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 216e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 21760f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 218e19e397aSTejun Heo 2197cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 220e19e397aSTejun Heo 2217a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 22268e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 22368e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2247a4e344cSTejun Heo 225e19e397aSTejun Heo /* 22624acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 22729c91e99STejun Heo * from get_work_pool(). 22829c91e99STejun Heo */ 22929c91e99STejun Heo struct rcu_head rcu; 23084f91c62SLai Jiangshan }; 2318b03ae3cSTejun Heo 2328b03ae3cSTejun Heo /* 233725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 234725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 235725e8ec5STejun Heo */ 236725e8ec5STejun Heo enum pool_workqueue_stats { 237725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 238725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2398a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 240616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 241725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2428639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 243725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 244725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 245725e8ec5STejun Heo 246725e8ec5STejun Heo PWQ_NR_STATS, 247725e8ec5STejun Heo }; 248725e8ec5STejun Heo 249725e8ec5STejun Heo /* 250e9a8e01fSTejun Heo * The per-pool workqueue. While queued, bits below WORK_PWQ_SHIFT 251112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 252112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 253112202d9STejun Heo * number of flag bits. 2541da177e4SLinus Torvalds */ 255112202d9STejun Heo struct pool_workqueue { 256bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2574690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 25873f53c4aSTejun Heo int work_color; /* L: current color */ 25973f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2608864b4e5STejun Heo int refcnt; /* L: reference count */ 26173f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 26273f53c4aSTejun Heo /* L: nr of in_flight works */ 2634c065dbcSWaiman Long bool plugged; /* L: execution suspended */ 264018f3a13SLai Jiangshan 265018f3a13SLai Jiangshan /* 266018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 267018f3a13SLai Jiangshan * 268018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 269018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 270018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 271018f3a13SLai Jiangshan * 2725797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in 2735797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with 2745797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are 2755797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in 2765797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct 2775797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate 2785797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with 2795797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 280018f3a13SLai Jiangshan */ 2811e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 282f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2835797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ 2843c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2852e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2868864b4e5STejun Heo 287725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 288725e8ec5STejun Heo 2898864b4e5STejun Heo /* 290967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 291687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 292687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 293967b494eSTejun Heo * grabbing wq->mutex. 2948864b4e5STejun Heo */ 295687a9aa5STejun Heo struct kthread_work release_work; 2968864b4e5STejun Heo struct rcu_head rcu; 297e9a8e01fSTejun Heo } __aligned(1 << WORK_STRUCT_PWQ_SHIFT); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* 30073f53c4aSTejun Heo * Structure used to wait for workqueue flush. 30173f53c4aSTejun Heo */ 30273f53c4aSTejun Heo struct wq_flusher { 3033c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 3043c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 30573f53c4aSTejun Heo struct completion done; /* flush completion */ 30673f53c4aSTejun Heo }; 3071da177e4SLinus Torvalds 308226223abSTejun Heo struct wq_device; 309226223abSTejun Heo 31073f53c4aSTejun Heo /* 31191ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 31291ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 31391ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 31491ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 3155797b1c1STejun Heo * 3165797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants 3175797b1c1STejun Heo * to start executing a work item, it should increment ->nr using 3185797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over 3195797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish 3205797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in 3215797b1c1STejun Heo * round-robin order. 32291ccc6e7STejun Heo */ 32391ccc6e7STejun Heo struct wq_node_nr_active { 3245797b1c1STejun Heo int max; /* per-node max_active */ 3255797b1c1STejun Heo atomic_t nr; /* per-node nr_active */ 3265797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */ 3275797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */ 32891ccc6e7STejun Heo }; 32991ccc6e7STejun Heo 33091ccc6e7STejun Heo /* 331c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 332c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds struct workqueue_struct { 3353c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 336e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 33773f53c4aSTejun Heo 3383c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3393c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3403c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 341112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3423c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3433c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3443c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 34573f53c4aSTejun Heo 3462e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 34730ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 348e22bee78STejun Heo 34987fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 3505797b1c1STejun Heo 3515797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */ 352a045a272STejun Heo int max_active; /* WO: max active works */ 3535797b1c1STejun Heo int min_active; /* WO: min active works */ 354a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 3555797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */ 356226223abSTejun Heo 3575b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3589f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3596029a918STejun Heo 360226223abSTejun Heo #ifdef CONFIG_SYSFS 361226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 362226223abSTejun Heo #endif 3634e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 364669de8bdSBart Van Assche char *lock_name; 365669de8bdSBart Van Assche struct lock_class_key key; 3664e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3674e6045f1SJohannes Berg #endif 368ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3692728fd2fSTejun Heo 370e2dca7adSTejun Heo /* 37124acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 37224acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 373e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 374e2dca7adSTejun Heo */ 375e2dca7adSTejun Heo struct rcu_head rcu; 376e2dca7adSTejun Heo 3772728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3782728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 379636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 38091ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3811da177e4SLinus Torvalds }; 3821da177e4SLinus Torvalds 38384193c07STejun Heo /* 38484193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 38584193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 38684193c07STejun Heo */ 38784193c07STejun Heo struct wq_pod_type { 38884193c07STejun Heo int nr_pods; /* number of pods */ 38984193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 39084193c07STejun Heo int *pod_node; /* pod -> node */ 39184193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 39284193c07STejun Heo }; 39384193c07STejun Heo 39463c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 395523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 39663c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 39763c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 39863c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 39963c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 40063c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 40163c5484eSTejun Heo }; 402bce90380STejun Heo 403616db877STejun Heo /* 404616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 405616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 406616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 407aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 408aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 409616db877STejun Heo */ 410aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 411616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 412616db877STejun Heo 413cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 414552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 415cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 416cee22a15SViresh Kumar 417863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 418c7a40c49STejun Heo static bool wq_topo_initialized __read_mostly = false; 419c7a40c49STejun Heo 420c7a40c49STejun Heo static struct kmem_cache *pwq_cache; 421c7a40c49STejun Heo 422c7a40c49STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 423c7a40c49STejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 4243347fa09STejun Heo 425fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 426fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4274c16bd32STejun Heo 42868e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4291258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 430a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 431d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 432d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4335bcab335STejun Heo 434e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 43568e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4367d19c5ceSTejun Heo 43799c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 438ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 439ef557180SMike Galbraith 440fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 441fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 442fe28f631SWaiman Long 443fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 444fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 445fe28f631SWaiman Long 446ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 447ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 448ace3c549Stiozhang 449ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 450ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 451b05a7928SFrederic Weisbecker 452f303fccbSTejun Heo /* 453f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 454f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 455f303fccbSTejun Heo * to uncover usages which depend on it. 456f303fccbSTejun Heo */ 457f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 458f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 459f303fccbSTejun Heo #else 460f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 461f303fccbSTejun Heo #endif 462f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 463f303fccbSTejun Heo 4642f34d733STejun Heo /* to raise softirq for the BH worker pools on other CPUs */ 4652f34d733STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct irq_work [NR_STD_WORKER_POOLS], 4662f34d733STejun Heo bh_pool_irq_works); 4672f34d733STejun Heo 4684cb1ef64STejun Heo /* the BH worker pools */ 4694cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4704cb1ef64STejun Heo bh_worker_pools); 4714cb1ef64STejun Heo 4727d19c5ceSTejun Heo /* the per-cpu worker pools */ 4734cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4744cb1ef64STejun Heo cpu_worker_pools); 4757d19c5ceSTejun Heo 47668e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4777d19c5ceSTejun Heo 47868e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 47929c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 48029c91e99STejun Heo 481c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 48229c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 48329c91e99STejun Heo 4848a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4858a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4868a2b7538STejun Heo 487967b494eSTejun Heo /* 488*978b8409STejun Heo * Used to synchronize multiple cancel_sync attempts on the same work item. See 489*978b8409STejun Heo * work_grab_pending() and __cancel_work_sync(). 490*978b8409STejun Heo */ 491*978b8409STejun Heo static DECLARE_WAIT_QUEUE_HEAD(wq_cancel_waitq); 492*978b8409STejun Heo 493*978b8409STejun Heo /* 494967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 495967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 496967b494eSTejun Heo * worker to avoid A-A deadlocks. 497967b494eSTejun Heo */ 49868279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 499967b494eSTejun Heo 50068279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 501ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 50268279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 5031aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 50468279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 505d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 50668279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 507f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 50868279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 50924d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 51068279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 5110668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 51268279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 5130668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 5144cb1ef64STejun Heo struct workqueue_struct *system_bh_wq; 5154cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq); 5164cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq; 5174cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq); 518d320c038STejun Heo 5197d19c5ceSTejun Heo static int worker_thread(void *__worker); 5206ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 521c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 52255df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 5237d19c5ceSTejun Heo 52497bd2347STejun Heo #define CREATE_TRACE_POINTS 52597bd2347STejun Heo #include <trace/events/workqueue.h> 52697bd2347STejun Heo 52768e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 528d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 529f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53024acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 5315bcab335STejun Heo 5325b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 533d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 534f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 535f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53624acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5375b95e1afSLai Jiangshan 5384cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \ 5394cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ 5404cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5414cb1ef64STejun Heo (pool)++) 5424cb1ef64STejun Heo 543f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 544f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 545f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5467a62c2c8STejun Heo (pool)++) 5474ce62e9eSTejun Heo 54849e3cf44STejun Heo /** 54917116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 55017116969STejun Heo * @pool: iteration cursor 551611c92a0STejun Heo * @pi: integer used for iteration 552fa1b54e6STejun Heo * 55324acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 55468e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 55568e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 556fa1b54e6STejun Heo * 557fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 558fa1b54e6STejun Heo * ignored. 55917116969STejun Heo */ 560611c92a0STejun Heo #define for_each_pool(pool, pi) \ 561611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 56268e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 563fa1b54e6STejun Heo else 56417116969STejun Heo 56517116969STejun Heo /** 566822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 567822d8405STejun Heo * @worker: iteration cursor 568822d8405STejun Heo * @pool: worker_pool to iterate workers of 569822d8405STejun Heo * 5701258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 571822d8405STejun Heo * 572822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 573822d8405STejun Heo * ignored. 574822d8405STejun Heo */ 575da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 576da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5771258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 578822d8405STejun Heo else 579822d8405STejun Heo 580822d8405STejun Heo /** 58149e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 58249e3cf44STejun Heo * @pwq: iteration cursor 58349e3cf44STejun Heo * @wq: the target workqueue 58476af4d93STejun Heo * 58524acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 586794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 587794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 58876af4d93STejun Heo * 58976af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 59076af4d93STejun Heo * ignored. 59149e3cf44STejun Heo */ 59249e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 59349e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5945a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 595f3421797STejun Heo 596dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 597dc186ad7SThomas Gleixner 598f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 599dc186ad7SThomas Gleixner 60099777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 60199777288SStanislaw Gruszka { 60299777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 60399777288SStanislaw Gruszka } 60499777288SStanislaw Gruszka 605b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 606b9fdac7fSDu, Changbin { 607b9fdac7fSDu, Changbin struct work_struct *work = addr; 608b9fdac7fSDu, Changbin 609b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 610b9fdac7fSDu, Changbin } 611b9fdac7fSDu, Changbin 612dc186ad7SThomas Gleixner /* 613dc186ad7SThomas Gleixner * fixup_init is called when: 614dc186ad7SThomas Gleixner * - an active object is initialized 615dc186ad7SThomas Gleixner */ 61602a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 617dc186ad7SThomas Gleixner { 618dc186ad7SThomas Gleixner struct work_struct *work = addr; 619dc186ad7SThomas Gleixner 620dc186ad7SThomas Gleixner switch (state) { 621dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 622dc186ad7SThomas Gleixner cancel_work_sync(work); 623dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 62402a982a6SDu, Changbin return true; 625dc186ad7SThomas Gleixner default: 62602a982a6SDu, Changbin return false; 627dc186ad7SThomas Gleixner } 628dc186ad7SThomas Gleixner } 629dc186ad7SThomas Gleixner 630dc186ad7SThomas Gleixner /* 631dc186ad7SThomas Gleixner * fixup_free is called when: 632dc186ad7SThomas Gleixner * - an active object is freed 633dc186ad7SThomas Gleixner */ 63402a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 635dc186ad7SThomas Gleixner { 636dc186ad7SThomas Gleixner struct work_struct *work = addr; 637dc186ad7SThomas Gleixner 638dc186ad7SThomas Gleixner switch (state) { 639dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 640dc186ad7SThomas Gleixner cancel_work_sync(work); 641dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 64202a982a6SDu, Changbin return true; 643dc186ad7SThomas Gleixner default: 64402a982a6SDu, Changbin return false; 645dc186ad7SThomas Gleixner } 646dc186ad7SThomas Gleixner } 647dc186ad7SThomas Gleixner 648f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 649dc186ad7SThomas Gleixner .name = "work_struct", 65099777288SStanislaw Gruszka .debug_hint = work_debug_hint, 651b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 652dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 653dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 654dc186ad7SThomas Gleixner }; 655dc186ad7SThomas Gleixner 656dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 657dc186ad7SThomas Gleixner { 658dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 659dc186ad7SThomas Gleixner } 660dc186ad7SThomas Gleixner 661dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 662dc186ad7SThomas Gleixner { 663dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 664dc186ad7SThomas Gleixner } 665dc186ad7SThomas Gleixner 666dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 667dc186ad7SThomas Gleixner { 668dc186ad7SThomas Gleixner if (onstack) 669dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 670dc186ad7SThomas Gleixner else 671dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 672dc186ad7SThomas Gleixner } 673dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 674dc186ad7SThomas Gleixner 675dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 676dc186ad7SThomas Gleixner { 677dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 678dc186ad7SThomas Gleixner } 679dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 680dc186ad7SThomas Gleixner 681ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 682ea2e64f2SThomas Gleixner { 683ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 684ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 685ea2e64f2SThomas Gleixner } 686ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 687ea2e64f2SThomas Gleixner 688dc186ad7SThomas Gleixner #else 689dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 690dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 691dc186ad7SThomas Gleixner #endif 692dc186ad7SThomas Gleixner 6934e8b22bdSLi Bin /** 69467dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6954e8b22bdSLi Bin * @pool: the pool pointer of interest 6964e8b22bdSLi Bin * 6974e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6984e8b22bdSLi Bin * successfully, -errno on failure. 6994e8b22bdSLi Bin */ 7009daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 7019daf9e67STejun Heo { 7029daf9e67STejun Heo int ret; 7039daf9e67STejun Heo 70468e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 7055bcab335STejun Heo 7064e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 7074e8b22bdSLi Bin GFP_KERNEL); 708229641a6STejun Heo if (ret >= 0) { 709e68035fbSTejun Heo pool->id = ret; 710229641a6STejun Heo return 0; 711229641a6STejun Heo } 7129daf9e67STejun Heo return ret; 7139daf9e67STejun Heo } 7149daf9e67STejun Heo 7159f66cff2STejun Heo static struct pool_workqueue __rcu ** 7169f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 7179f66cff2STejun Heo { 7189f66cff2STejun Heo if (cpu >= 0) 7199f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 7209f66cff2STejun Heo else 7219f66cff2STejun Heo return &wq->dfl_pwq; 7229f66cff2STejun Heo } 7239f66cff2STejun Heo 7249f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 7259f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 7269f66cff2STejun Heo { 7279f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 7289f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 7299f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 7309f66cff2STejun Heo } 7319f66cff2STejun Heo 7325797b1c1STejun Heo /** 7335797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 7345797b1c1STejun Heo * @wq: workqueue of interest 7355797b1c1STejun Heo * 7365797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 7375797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 7385797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 7395797b1c1STejun Heo */ 7405797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 7415797b1c1STejun Heo { 7425797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7435797b1c1STejun Heo } 7445797b1c1STejun Heo 74573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 74673f53c4aSTejun Heo { 74773f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 74873f53c4aSTejun Heo } 74973f53c4aSTejun Heo 750c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 75173f53c4aSTejun Heo { 752c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 75373f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 75473f53c4aSTejun Heo } 75573f53c4aSTejun Heo 75673f53c4aSTejun Heo static int work_next_color(int color) 75773f53c4aSTejun Heo { 75873f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 759a848e3b6SOleg Nesterov } 760a848e3b6SOleg Nesterov 7614594bf15SDavid Howells /* 762112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 763112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7647c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7657a22ad75STejun Heo * 766112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 767112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 768bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 769bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 7707a22ad75STejun Heo * 771112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7727c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 773112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7747c3eed5cSTejun Heo * available only while the work item is queued. 775bbb68dfaSTejun Heo * 776bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 777bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 778bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 779bbb68dfaSTejun Heo * try to steal the PENDING bit. 7804594bf15SDavid Howells */ 7817a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 7827a22ad75STejun Heo unsigned long flags) 7837a22ad75STejun Heo { 7846183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7857a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7867a22ad75STejun Heo } 7877a22ad75STejun Heo 788112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7894690c4abSTejun Heo unsigned long extra_flags) 790365970a1SDavid Howells { 791112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 792112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 793365970a1SDavid Howells } 794365970a1SDavid Howells 7954468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7964468a00fSLai Jiangshan int pool_id) 7974468a00fSLai Jiangshan { 7984468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7994468a00fSLai Jiangshan WORK_STRUCT_PENDING); 8004468a00fSLai Jiangshan } 8014468a00fSLai Jiangshan 8027c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 8037c3eed5cSTejun Heo int pool_id) 8044d707b9fSOleg Nesterov { 80523657bb1STejun Heo /* 80623657bb1STejun Heo * The following wmb is paired with the implied mb in 80723657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 80823657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 80923657bb1STejun Heo * owner. 81023657bb1STejun Heo */ 81123657bb1STejun Heo smp_wmb(); 8127c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 813346c09f8SRoman Pen /* 814346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 815346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 816346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 8178bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 818346c09f8SRoman Pen * the same @work. E.g. consider this case: 819346c09f8SRoman Pen * 820346c09f8SRoman Pen * CPU#0 CPU#1 821346c09f8SRoman Pen * ---------------------------- -------------------------------- 822346c09f8SRoman Pen * 823346c09f8SRoman Pen * 1 STORE event_indicated 824346c09f8SRoman Pen * 2 queue_work_on() { 825346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 826346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 827346c09f8SRoman Pen * 5 set_work_data() # clear bit 828346c09f8SRoman Pen * 6 smp_mb() 829346c09f8SRoman Pen * 7 work->current_func() { 830346c09f8SRoman Pen * 8 LOAD event_indicated 831346c09f8SRoman Pen * } 832346c09f8SRoman Pen * 833346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 834346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 835346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 836346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 837346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 838346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 839346c09f8SRoman Pen * before actual STORE. 840346c09f8SRoman Pen */ 841346c09f8SRoman Pen smp_mb(); 8424d707b9fSOleg Nesterov } 8434d707b9fSOleg Nesterov 8447a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 845365970a1SDavid Howells { 8467c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 8477c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 8487a22ad75STejun Heo } 8497a22ad75STejun Heo 850afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 851afa4bb77SLinus Torvalds { 852e9a8e01fSTejun Heo return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK); 853afa4bb77SLinus Torvalds } 854afa4bb77SLinus Torvalds 855112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8567a22ad75STejun Heo { 857e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8587a22ad75STejun Heo 859112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 860afa4bb77SLinus Torvalds return work_struct_pwq(data); 861e120153dSTejun Heo else 862e120153dSTejun Heo return NULL; 8637a22ad75STejun Heo } 8647a22ad75STejun Heo 8657c3eed5cSTejun Heo /** 8667c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8677c3eed5cSTejun Heo * @work: the work item of interest 8687c3eed5cSTejun Heo * 86968e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 87024acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 87124acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 872fa1b54e6STejun Heo * 873fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 874fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 875fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 876fa1b54e6STejun Heo * returned pool is and stays online. 877d185af30SYacine Belkadi * 878d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8797c3eed5cSTejun Heo */ 8807c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8817a22ad75STejun Heo { 882e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8837c3eed5cSTejun Heo int pool_id; 8847a22ad75STejun Heo 88568e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 886fa1b54e6STejun Heo 887112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 888afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8897a22ad75STejun Heo 8907c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8917c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8927a22ad75STejun Heo return NULL; 8937a22ad75STejun Heo 894fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8957c3eed5cSTejun Heo } 8967c3eed5cSTejun Heo 8977c3eed5cSTejun Heo /** 8987c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8997c3eed5cSTejun Heo * @work: the work item of interest 9007c3eed5cSTejun Heo * 901d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 9027c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 9037c3eed5cSTejun Heo */ 9047c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 9057c3eed5cSTejun Heo { 90654d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 9077c3eed5cSTejun Heo 908112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 909afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 91054d5b7d0SLai Jiangshan 91154d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 9127c3eed5cSTejun Heo } 9137c3eed5cSTejun Heo 914bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 915bbb68dfaSTejun Heo { 9167c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 917bbb68dfaSTejun Heo 9187c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 9197c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 920bbb68dfaSTejun Heo } 921bbb68dfaSTejun Heo 922bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 923bbb68dfaSTejun Heo { 924bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 925bbb68dfaSTejun Heo 926112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 927bbb68dfaSTejun Heo } 928bbb68dfaSTejun Heo 929e22bee78STejun Heo /* 9303270476aSTejun Heo * Policy functions. These define the policies on how the global worker 9313270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 932d565ed63STejun Heo * they're being called with pool->lock held. 933e22bee78STejun Heo */ 934e22bee78STejun Heo 935e22bee78STejun Heo /* 936e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 937e22bee78STejun Heo * running workers. 938974271c4STejun Heo * 939974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 940706026c2STejun Heo * function will always return %true for unbound pools as long as the 941974271c4STejun Heo * worklist isn't empty. 942e22bee78STejun Heo */ 94363d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 944e22bee78STejun Heo { 9450219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 946e22bee78STejun Heo } 947e22bee78STejun Heo 948e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 94963d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 950e22bee78STejun Heo { 95163d95a91STejun Heo return pool->nr_idle; 952e22bee78STejun Heo } 953e22bee78STejun Heo 954e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 95563d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 956e22bee78STejun Heo { 957bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 958e22bee78STejun Heo } 959e22bee78STejun Heo 960e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 96163d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 962e22bee78STejun Heo { 96363d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 964e22bee78STejun Heo } 965e22bee78STejun Heo 966e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 96763d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 968e22bee78STejun Heo { 969692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 97063d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 97163d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 972e22bee78STejun Heo 973e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 974e22bee78STejun Heo } 975e22bee78STejun Heo 9764690c4abSTejun Heo /** 977e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 978cb444766STejun Heo * @worker: self 979d302f017STejun Heo * @flags: flags to set 980d302f017STejun Heo * 981228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 982d302f017STejun Heo */ 983228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 984d302f017STejun Heo { 985bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 986e22bee78STejun Heo 987bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 988cb444766STejun Heo 989228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 990e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 991e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 992bc35f7efSLai Jiangshan pool->nr_running--; 993e22bee78STejun Heo } 994e22bee78STejun Heo 995d302f017STejun Heo worker->flags |= flags; 996d302f017STejun Heo } 997d302f017STejun Heo 998d302f017STejun Heo /** 999e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 1000cb444766STejun Heo * @worker: self 1001d302f017STejun Heo * @flags: flags to clear 1002d302f017STejun Heo * 1003e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 1004d302f017STejun Heo */ 1005d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 1006d302f017STejun Heo { 100763d95a91STejun Heo struct worker_pool *pool = worker->pool; 1008e22bee78STejun Heo unsigned int oflags = worker->flags; 1009e22bee78STejun Heo 1010bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 1011cb444766STejun Heo 1012d302f017STejun Heo worker->flags &= ~flags; 1013e22bee78STejun Heo 101442c025f3STejun Heo /* 101542c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 101642c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 101742c025f3STejun Heo * of multiple flags, not a single flag. 101842c025f3STejun Heo */ 1019e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1020e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1021bc35f7efSLai Jiangshan pool->nr_running++; 1022d302f017STejun Heo } 1023d302f017STejun Heo 1024797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 1025797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 1026797e8345STejun Heo { 1027797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 1028797e8345STejun Heo return NULL; 1029797e8345STejun Heo 1030797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 1031797e8345STejun Heo } 1032797e8345STejun Heo 1033797e8345STejun Heo /** 1034797e8345STejun Heo * worker_enter_idle - enter idle state 1035797e8345STejun Heo * @worker: worker which is entering idle state 1036797e8345STejun Heo * 1037797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 1038797e8345STejun Heo * necessary. 1039797e8345STejun Heo * 1040797e8345STejun Heo * LOCKING: 1041797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1042797e8345STejun Heo */ 1043797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1044797e8345STejun Heo { 1045797e8345STejun Heo struct worker_pool *pool = worker->pool; 1046797e8345STejun Heo 1047797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1048797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1049797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1050797e8345STejun Heo return; 1051797e8345STejun Heo 1052797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1053797e8345STejun Heo worker->flags |= WORKER_IDLE; 1054797e8345STejun Heo pool->nr_idle++; 1055797e8345STejun Heo worker->last_active = jiffies; 1056797e8345STejun Heo 1057797e8345STejun Heo /* idle_list is LIFO */ 1058797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1059797e8345STejun Heo 1060797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1061797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1062797e8345STejun Heo 1063797e8345STejun Heo /* Sanity check nr_running. */ 1064797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1065797e8345STejun Heo } 1066797e8345STejun Heo 1067797e8345STejun Heo /** 1068797e8345STejun Heo * worker_leave_idle - leave idle state 1069797e8345STejun Heo * @worker: worker which is leaving idle state 1070797e8345STejun Heo * 1071797e8345STejun Heo * @worker is leaving idle state. Update stats. 1072797e8345STejun Heo * 1073797e8345STejun Heo * LOCKING: 1074797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1075797e8345STejun Heo */ 1076797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1077797e8345STejun Heo { 1078797e8345STejun Heo struct worker_pool *pool = worker->pool; 1079797e8345STejun Heo 1080797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1081797e8345STejun Heo return; 1082797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1083797e8345STejun Heo pool->nr_idle--; 1084797e8345STejun Heo list_del_init(&worker->entry); 1085797e8345STejun Heo } 1086797e8345STejun Heo 1087797e8345STejun Heo /** 1088797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1089797e8345STejun Heo * @pool: pool of interest 1090797e8345STejun Heo * @work: work to find worker for 1091797e8345STejun Heo * 1092797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1093797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1094797e8345STejun Heo * to match, its current execution should match the address of @work and 1095797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1096797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1097797e8345STejun Heo * being executed. 1098797e8345STejun Heo * 1099797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1100797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1101797e8345STejun Heo * another work item. If the same work item address ends up being reused 1102797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1103797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1104797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1105797e8345STejun Heo * 1106797e8345STejun Heo * This function checks the work item address and work function to avoid 1107797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1108797e8345STejun Heo * work function which can introduce dependency onto itself through a 1109797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1110797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1111797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1112797e8345STejun Heo * 1113797e8345STejun Heo * CONTEXT: 1114797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1115797e8345STejun Heo * 1116797e8345STejun Heo * Return: 1117797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1118797e8345STejun Heo * otherwise. 1119797e8345STejun Heo */ 1120797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1121797e8345STejun Heo struct work_struct *work) 1122797e8345STejun Heo { 1123797e8345STejun Heo struct worker *worker; 1124797e8345STejun Heo 1125797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1126797e8345STejun Heo (unsigned long)work) 1127797e8345STejun Heo if (worker->current_work == work && 1128797e8345STejun Heo worker->current_func == work->func) 1129797e8345STejun Heo return worker; 1130797e8345STejun Heo 1131797e8345STejun Heo return NULL; 1132797e8345STejun Heo } 1133797e8345STejun Heo 1134797e8345STejun Heo /** 1135797e8345STejun Heo * move_linked_works - move linked works to a list 1136797e8345STejun Heo * @work: start of series of works to be scheduled 1137797e8345STejun Heo * @head: target list to append @work to 1138797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1139797e8345STejun Heo * 1140873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1141873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1142873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1143873eaca6STejun Heo * @nextp. 1144797e8345STejun Heo * 1145797e8345STejun Heo * CONTEXT: 1146797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1147797e8345STejun Heo */ 1148797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1149797e8345STejun Heo struct work_struct **nextp) 1150797e8345STejun Heo { 1151797e8345STejun Heo struct work_struct *n; 1152797e8345STejun Heo 1153797e8345STejun Heo /* 1154797e8345STejun Heo * Linked worklist will always end before the end of the list, 1155797e8345STejun Heo * use NULL for list head. 1156797e8345STejun Heo */ 1157797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1158797e8345STejun Heo list_move_tail(&work->entry, head); 1159797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1160797e8345STejun Heo break; 1161797e8345STejun Heo } 1162797e8345STejun Heo 1163797e8345STejun Heo /* 1164797e8345STejun Heo * If we're already inside safe list traversal and have moved 1165797e8345STejun Heo * multiple works to the scheduled queue, the next position 1166797e8345STejun Heo * needs to be updated. 1167797e8345STejun Heo */ 1168797e8345STejun Heo if (nextp) 1169797e8345STejun Heo *nextp = n; 1170797e8345STejun Heo } 1171797e8345STejun Heo 1172797e8345STejun Heo /** 1173873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1174873eaca6STejun Heo * @work: work to assign 1175873eaca6STejun Heo * @worker: worker to assign to 1176873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1177873eaca6STejun Heo * 1178873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1179873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1180873eaca6STejun Heo * 1181873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1182873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1183873eaca6STejun Heo * list_for_each_entry_safe(). 1184873eaca6STejun Heo * 1185873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1186873eaca6STejun Heo * was punted to another worker already executing it. 1187873eaca6STejun Heo */ 1188873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1189873eaca6STejun Heo struct work_struct **nextp) 1190873eaca6STejun Heo { 1191873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1192873eaca6STejun Heo struct worker *collision; 1193873eaca6STejun Heo 1194873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1195873eaca6STejun Heo 1196873eaca6STejun Heo /* 1197873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1198873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1199873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1200873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1201873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1202873eaca6STejun Heo * defer the work to the currently executing one. 1203873eaca6STejun Heo */ 1204873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1205873eaca6STejun Heo if (unlikely(collision)) { 1206873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1207873eaca6STejun Heo return false; 1208873eaca6STejun Heo } 1209873eaca6STejun Heo 1210873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1211873eaca6STejun Heo return true; 1212873eaca6STejun Heo } 1213873eaca6STejun Heo 12142f34d733STejun Heo static struct irq_work *bh_pool_irq_work(struct worker_pool *pool) 12152f34d733STejun Heo { 12162f34d733STejun Heo int high = pool->attrs->nice == HIGHPRI_NICE_LEVEL ? 1 : 0; 12172f34d733STejun Heo 12182f34d733STejun Heo return &per_cpu(bh_pool_irq_works, pool->cpu)[high]; 12192f34d733STejun Heo } 12202f34d733STejun Heo 1221fd0a68a2STejun Heo static void kick_bh_pool(struct worker_pool *pool) 1222fd0a68a2STejun Heo { 1223fd0a68a2STejun Heo #ifdef CONFIG_SMP 1224fd0a68a2STejun Heo if (unlikely(pool->cpu != smp_processor_id())) { 1225fd0a68a2STejun Heo irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu); 1226fd0a68a2STejun Heo return; 1227fd0a68a2STejun Heo } 1228fd0a68a2STejun Heo #endif 1229fd0a68a2STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 1230fd0a68a2STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 1231fd0a68a2STejun Heo else 1232fd0a68a2STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 1233fd0a68a2STejun Heo } 1234fd0a68a2STejun Heo 1235873eaca6STejun Heo /** 12360219a352STejun Heo * kick_pool - wake up an idle worker if necessary 12370219a352STejun Heo * @pool: pool to kick 1238797e8345STejun Heo * 12390219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 12400219a352STejun Heo * whether a worker was woken up. 1241797e8345STejun Heo */ 12420219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1243797e8345STejun Heo { 1244797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 12458639ecebSTejun Heo struct task_struct *p; 1246797e8345STejun Heo 12470219a352STejun Heo lockdep_assert_held(&pool->lock); 12480219a352STejun Heo 12490219a352STejun Heo if (!need_more_worker(pool) || !worker) 12500219a352STejun Heo return false; 12510219a352STejun Heo 12524cb1ef64STejun Heo if (pool->flags & POOL_BH) { 1253fd0a68a2STejun Heo kick_bh_pool(pool); 12544cb1ef64STejun Heo return true; 12554cb1ef64STejun Heo } 12564cb1ef64STejun Heo 12578639ecebSTejun Heo p = worker->task; 12588639ecebSTejun Heo 12598639ecebSTejun Heo #ifdef CONFIG_SMP 12608639ecebSTejun Heo /* 12618639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 12628639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 12638639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 12648639ecebSTejun Heo * execution locality. 12658639ecebSTejun Heo * 12668639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 12678639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12688639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12698639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12708639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12718639ecebSTejun Heo * still on cpu when picking an idle worker. 12728639ecebSTejun Heo * 12738639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12748639ecebSTejun Heo * its affinity scope. Repatriate. 12758639ecebSTejun Heo */ 12768639ecebSTejun Heo if (!pool->attrs->affn_strict && 12778639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12788639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12798639ecebSTejun Heo struct work_struct, entry); 12808639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12818639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12828639ecebSTejun Heo } 12838639ecebSTejun Heo #endif 12848639ecebSTejun Heo wake_up_process(p); 12850219a352STejun Heo return true; 1286797e8345STejun Heo } 1287797e8345STejun Heo 128863638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 128963638450STejun Heo 129063638450STejun Heo /* 129163638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 129263638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 129363638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 129463638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 129563638450STejun Heo * should be using an unbound workqueue instead. 129663638450STejun Heo * 129763638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 129863638450STejun Heo * and report them so that they can be examined and converted to use unbound 129963638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 130063638450STejun Heo * function is tracked and reported with exponential backoff. 130163638450STejun Heo */ 130263638450STejun Heo #define WCI_MAX_ENTS 128 130363638450STejun Heo 130463638450STejun Heo struct wci_ent { 130563638450STejun Heo work_func_t func; 130663638450STejun Heo atomic64_t cnt; 130763638450STejun Heo struct hlist_node hash_node; 130863638450STejun Heo }; 130963638450STejun Heo 131063638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 131163638450STejun Heo static int wci_nr_ents; 131263638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 131363638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 131463638450STejun Heo 131563638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 131663638450STejun Heo { 131763638450STejun Heo struct wci_ent *ent; 131863638450STejun Heo 131963638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 132063638450STejun Heo (unsigned long)func) { 132163638450STejun Heo if (ent->func == func) 132263638450STejun Heo return ent; 132363638450STejun Heo } 132463638450STejun Heo return NULL; 132563638450STejun Heo } 132663638450STejun Heo 132763638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 132863638450STejun Heo { 132963638450STejun Heo struct wci_ent *ent; 133063638450STejun Heo 133163638450STejun Heo restart: 133263638450STejun Heo ent = wci_find_ent(func); 133363638450STejun Heo if (ent) { 133463638450STejun Heo u64 cnt; 133563638450STejun Heo 133663638450STejun Heo /* 133763638450STejun Heo * Start reporting from the fourth time and back off 133863638450STejun Heo * exponentially. 133963638450STejun Heo */ 134063638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 134163638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 134263638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 134363638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 134463638450STejun Heo atomic64_read(&ent->cnt)); 134563638450STejun Heo return; 134663638450STejun Heo } 134763638450STejun Heo 134863638450STejun Heo /* 134963638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 135063638450STejun Heo * is exhausted, something went really wrong and we probably made enough 135163638450STejun Heo * noise already. 135263638450STejun Heo */ 135363638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 135463638450STejun Heo return; 135563638450STejun Heo 135663638450STejun Heo raw_spin_lock(&wci_lock); 135763638450STejun Heo 135863638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 135963638450STejun Heo raw_spin_unlock(&wci_lock); 136063638450STejun Heo return; 136163638450STejun Heo } 136263638450STejun Heo 136363638450STejun Heo if (wci_find_ent(func)) { 136463638450STejun Heo raw_spin_unlock(&wci_lock); 136563638450STejun Heo goto restart; 136663638450STejun Heo } 136763638450STejun Heo 136863638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 136963638450STejun Heo ent->func = func; 137063638450STejun Heo atomic64_set(&ent->cnt, 1); 137163638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 137263638450STejun Heo 137363638450STejun Heo raw_spin_unlock(&wci_lock); 137463638450STejun Heo } 137563638450STejun Heo 137663638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 137763638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 137863638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 137963638450STejun Heo 1380c54d5046STejun Heo /** 13811da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13821da177e4SLinus Torvalds * @task: task waking up 13831da177e4SLinus Torvalds * 13841da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13851da177e4SLinus Torvalds */ 13861da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13871da177e4SLinus Torvalds { 13881da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13891da177e4SLinus Torvalds 1390c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13911da177e4SLinus Torvalds return; 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /* 13941da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13951da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13961da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13971da177e4SLinus Torvalds * pool. Protect against such race. 13981da177e4SLinus Torvalds */ 13991da177e4SLinus Torvalds preempt_disable(); 14001da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 14011da177e4SLinus Torvalds worker->pool->nr_running++; 14021da177e4SLinus Torvalds preempt_enable(); 1403616db877STejun Heo 1404616db877STejun Heo /* 1405616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1406616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1407616db877STejun Heo */ 1408616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1409616db877STejun Heo 1410c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /** 14141da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 14151da177e4SLinus Torvalds * @task: task going to sleep 14161da177e4SLinus Torvalds * 14171da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 14181da177e4SLinus Torvalds * going to sleep. 14191da177e4SLinus Torvalds */ 14201da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 14211da177e4SLinus Torvalds { 14221da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 14231da177e4SLinus Torvalds struct worker_pool *pool; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds /* 14261da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 14271da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 14281da177e4SLinus Torvalds * checking NOT_RUNNING. 14291da177e4SLinus Torvalds */ 14301da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 14311da177e4SLinus Torvalds return; 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds pool = worker->pool; 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1436c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 14371da177e4SLinus Torvalds return; 14381da177e4SLinus Torvalds 1439c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 14401da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds /* 14431da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 14441da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 14451da177e4SLinus Torvalds * and nr_running has been reset. 14461da177e4SLinus Torvalds */ 14471da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 14481da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14491da177e4SLinus Torvalds return; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds pool->nr_running--; 14530219a352STejun Heo if (kick_pool(pool)) 1454725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 14550219a352STejun Heo 14561da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds /** 1460616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1461616db877STejun Heo * @task: task currently running 1462616db877STejun Heo * 1463616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1464616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1465616db877STejun Heo */ 1466616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1467616db877STejun Heo { 1468616db877STejun Heo struct worker *worker = kthread_data(task); 1469616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1470616db877STejun Heo struct worker_pool *pool = worker->pool; 1471616db877STejun Heo 1472616db877STejun Heo if (!pwq) 1473616db877STejun Heo return; 1474616db877STejun Heo 14758a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14768a1dd1e5STejun Heo 147718c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 147818c8ae81SZqiang return; 147918c8ae81SZqiang 1480616db877STejun Heo /* 1481616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1482616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1483616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1484c8f6219bSZqiang * 1485c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1486c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1487c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1488c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1489c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1490c8f6219bSZqiang * We probably want to make this prettier in the future. 1491616db877STejun Heo */ 1492c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1493616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1494616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1495616db877STejun Heo return; 1496616db877STejun Heo 1497616db877STejun Heo raw_spin_lock(&pool->lock); 1498616db877STejun Heo 1499616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 150063638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1501616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1502616db877STejun Heo 15030219a352STejun Heo if (kick_pool(pool)) 1504616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1505616db877STejun Heo 1506616db877STejun Heo raw_spin_unlock(&pool->lock); 1507616db877STejun Heo } 1508616db877STejun Heo 1509616db877STejun Heo /** 15101da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 15111da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 15121da177e4SLinus Torvalds * 15131da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 15141da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 15151da177e4SLinus Torvalds * 15161da177e4SLinus Torvalds * CONTEXT: 15179b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 15181da177e4SLinus Torvalds * 15191da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1520f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1521f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 15221da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 15231da177e4SLinus Torvalds * 15241da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 15251da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 15261da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 15271da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1528365970a1SDavid Howells * 1529365970a1SDavid Howells * Return: 1530365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1531365970a1SDavid Howells * hasn't executed any work yet. 1532365970a1SDavid Howells */ 1533365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1534365970a1SDavid Howells { 1535365970a1SDavid Howells struct worker *worker = kthread_data(task); 1536365970a1SDavid Howells 1537365970a1SDavid Howells return worker->last_func; 1538365970a1SDavid Howells } 1539365970a1SDavid Howells 1540d302f017STejun Heo /** 154191ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 154291ccc6e7STejun Heo * @wq: workqueue of interest 154391ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 154491ccc6e7STejun Heo * 154591ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 154691ccc6e7STejun Heo * 154791ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 154891ccc6e7STejun Heo * 154991ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 155091ccc6e7STejun Heo * 155191ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 155291ccc6e7STejun Heo */ 155391ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 155491ccc6e7STejun Heo int node) 155591ccc6e7STejun Heo { 155691ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 155791ccc6e7STejun Heo return NULL; 155891ccc6e7STejun Heo 155991ccc6e7STejun Heo if (node == NUMA_NO_NODE) 156091ccc6e7STejun Heo node = nr_node_ids; 156191ccc6e7STejun Heo 156291ccc6e7STejun Heo return wq->node_nr_active[node]; 156391ccc6e7STejun Heo } 156491ccc6e7STejun Heo 156591ccc6e7STejun Heo /** 15665797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 15675797b1c1STejun Heo * @wq: workqueue to update 15685797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15695797b1c1STejun Heo * 15705797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15715797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15725797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15735797b1c1STejun Heo */ 15745797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15755797b1c1STejun Heo { 15765797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15775797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15785797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15795797b1c1STejun Heo int total_cpus, node; 15805797b1c1STejun Heo 15815797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15825797b1c1STejun Heo 1583c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1584c5f8cd6cSTejun Heo return; 1585c5f8cd6cSTejun Heo 158615930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15875797b1c1STejun Heo off_cpu = -1; 15885797b1c1STejun Heo 15895797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15905797b1c1STejun Heo if (off_cpu >= 0) 15915797b1c1STejun Heo total_cpus--; 15925797b1c1STejun Heo 15935797b1c1STejun Heo for_each_node(node) { 15945797b1c1STejun Heo int node_cpus; 15955797b1c1STejun Heo 15965797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15975797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15985797b1c1STejun Heo node_cpus--; 15995797b1c1STejun Heo 16005797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 16015797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 16025797b1c1STejun Heo min_active, max_active); 16035797b1c1STejun Heo } 16045797b1c1STejun Heo 16055797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 16065797b1c1STejun Heo } 16075797b1c1STejun Heo 16085797b1c1STejun Heo /** 16098864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 16108864b4e5STejun Heo * @pwq: pool_workqueue to get 16118864b4e5STejun Heo * 16128864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 16138864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 16148864b4e5STejun Heo */ 16158864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 16168864b4e5STejun Heo { 16178864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16188864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 16198864b4e5STejun Heo pwq->refcnt++; 16208864b4e5STejun Heo } 16218864b4e5STejun Heo 16228864b4e5STejun Heo /** 16238864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 16248864b4e5STejun Heo * @pwq: pool_workqueue to put 16258864b4e5STejun Heo * 16268864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 16278864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 16288864b4e5STejun Heo */ 16298864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 16308864b4e5STejun Heo { 16318864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16328864b4e5STejun Heo if (likely(--pwq->refcnt)) 16338864b4e5STejun Heo return; 16348864b4e5STejun Heo /* 1635967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1636967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16378864b4e5STejun Heo */ 1638687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16398864b4e5STejun Heo } 16408864b4e5STejun Heo 1641dce90d47STejun Heo /** 1642dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1643dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1644dce90d47STejun Heo * 1645dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1646dce90d47STejun Heo */ 1647dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1648dce90d47STejun Heo { 1649dce90d47STejun Heo if (pwq) { 1650dce90d47STejun Heo /* 165124acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1652dce90d47STejun Heo * following lock operations are safe. 1653dce90d47STejun Heo */ 1654a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1655dce90d47STejun Heo put_pwq(pwq); 1656a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1657dce90d47STejun Heo } 1658dce90d47STejun Heo } 1659dce90d47STejun Heo 1660afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1661afa87ce8STejun Heo { 1662afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1663afa87ce8STejun Heo } 1664afa87ce8STejun Heo 16654c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16664c638030STejun Heo struct work_struct *work) 1667bf4ede01STejun Heo { 16681c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16691c270b79STejun Heo 16701c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1671bf4ede01STejun Heo trace_workqueue_activate_work(work); 167282607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 167382607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1674112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16751c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16764c638030STejun Heo } 16774c638030STejun Heo 16784c638030STejun Heo /** 16794c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16804c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16814c638030STejun Heo * @work: work item to activate 16824c638030STejun Heo * 16834c638030STejun Heo * Returns %true if activated. %false if already active. 16844c638030STejun Heo */ 16854c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16864c638030STejun Heo struct work_struct *work) 16874c638030STejun Heo { 16884c638030STejun Heo struct worker_pool *pool = pwq->pool; 168991ccc6e7STejun Heo struct wq_node_nr_active *nna; 16904c638030STejun Heo 16914c638030STejun Heo lockdep_assert_held(&pool->lock); 16924c638030STejun Heo 16934c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16944c638030STejun Heo return false; 16954c638030STejun Heo 169691ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 169791ccc6e7STejun Heo if (nna) 169891ccc6e7STejun Heo atomic_inc(&nna->nr); 169991ccc6e7STejun Heo 1700112202d9STejun Heo pwq->nr_active++; 17014c638030STejun Heo __pwq_activate_work(pwq, work); 17024c638030STejun Heo return true; 1703bf4ede01STejun Heo } 1704bf4ede01STejun Heo 17055797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 17065797b1c1STejun Heo { 17075797b1c1STejun Heo int max = READ_ONCE(nna->max); 17085797b1c1STejun Heo 17095797b1c1STejun Heo while (true) { 17105797b1c1STejun Heo int old, tmp; 17115797b1c1STejun Heo 17125797b1c1STejun Heo old = atomic_read(&nna->nr); 17135797b1c1STejun Heo if (old >= max) 17145797b1c1STejun Heo return false; 17155797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 17165797b1c1STejun Heo if (tmp == old) 17175797b1c1STejun Heo return true; 17185797b1c1STejun Heo } 17195797b1c1STejun Heo } 17205797b1c1STejun Heo 17211c270b79STejun Heo /** 17221c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 17231c270b79STejun Heo * @pwq: pool_workqueue of interest 17245797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17251c270b79STejun Heo * 17261c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 17271c270b79STejun Heo * successfully obtained. %false otherwise. 17281c270b79STejun Heo */ 17295797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 17303aa62497SLai Jiangshan { 17311c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17321c270b79STejun Heo struct worker_pool *pool = pwq->pool; 173391ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17345797b1c1STejun Heo bool obtained = false; 17351c270b79STejun Heo 17361c270b79STejun Heo lockdep_assert_held(&pool->lock); 17371c270b79STejun Heo 17385797b1c1STejun Heo if (!nna) { 17394cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17401c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17415797b1c1STejun Heo goto out; 174291ccc6e7STejun Heo } 17435797b1c1STejun Heo 17444c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17454c065dbcSWaiman Long return false; 17464c065dbcSWaiman Long 17475797b1c1STejun Heo /* 17485797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17495797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17505797b1c1STejun Heo * concurrency level. Don't jump the line. 17515797b1c1STejun Heo * 17525797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17535797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17545797b1c1STejun Heo * increase it. This is indicated by @fill. 17555797b1c1STejun Heo */ 17565797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17575797b1c1STejun Heo goto out; 17585797b1c1STejun Heo 17595797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17605797b1c1STejun Heo if (obtained) 17615797b1c1STejun Heo goto out; 17625797b1c1STejun Heo 17635797b1c1STejun Heo /* 17645797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17655797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17665797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17675797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17685797b1c1STejun Heo * $nna->pending_pwqs. 17695797b1c1STejun Heo */ 17705797b1c1STejun Heo raw_spin_lock(&nna->lock); 17715797b1c1STejun Heo 17725797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17735797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17745797b1c1STejun Heo else if (likely(!fill)) 17755797b1c1STejun Heo goto out_unlock; 17765797b1c1STejun Heo 17775797b1c1STejun Heo smp_mb(); 17785797b1c1STejun Heo 17795797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17805797b1c1STejun Heo 17815797b1c1STejun Heo /* 17825797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17835797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17845797b1c1STejun Heo */ 17855797b1c1STejun Heo if (obtained && likely(!fill)) 17865797b1c1STejun Heo list_del_init(&pwq->pending_node); 17875797b1c1STejun Heo 17885797b1c1STejun Heo out_unlock: 17895797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17905797b1c1STejun Heo out: 17915797b1c1STejun Heo if (obtained) 17925797b1c1STejun Heo pwq->nr_active++; 17931c270b79STejun Heo return obtained; 17941c270b79STejun Heo } 17951c270b79STejun Heo 17961c270b79STejun Heo /** 17971c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17981c270b79STejun Heo * @pwq: pool_workqueue of interest 17995797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 18001c270b79STejun Heo * 18011c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 18021c270b79STejun Heo * max_active limit. 18031c270b79STejun Heo * 18041c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 18051c270b79STejun Heo * inactive work item is found or max_active limit is reached. 18061c270b79STejun Heo */ 18075797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 18081c270b79STejun Heo { 18091c270b79STejun Heo struct work_struct *work = 18101c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 18113aa62497SLai Jiangshan struct work_struct, entry); 18123aa62497SLai Jiangshan 18135797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 18141c270b79STejun Heo __pwq_activate_work(pwq, work); 18151c270b79STejun Heo return true; 18161c270b79STejun Heo } else { 18171c270b79STejun Heo return false; 18181c270b79STejun Heo } 18191c270b79STejun Heo } 18201c270b79STejun Heo 18211c270b79STejun Heo /** 1822516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1823516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 18244c065dbcSWaiman Long * 1825516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1826516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1827516d3dc9SWaiman Long * ensure proper work item ordering:: 18284c065dbcSWaiman Long * 18294c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 18304c065dbcSWaiman Long * | 18314c065dbcSWaiman Long * v 18324c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18334c065dbcSWaiman Long * | | | 18344c065dbcSWaiman Long * 1 3 5 18354c065dbcSWaiman Long * | | | 18364c065dbcSWaiman Long * 2 4 6 1837516d3dc9SWaiman Long * 1838516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1839516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1840516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1841516d3dc9SWaiman Long * the list is the oldest. 18424c065dbcSWaiman Long */ 18434c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18444c065dbcSWaiman Long { 18454c065dbcSWaiman Long struct pool_workqueue *pwq; 18464c065dbcSWaiman Long 18474c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18484c065dbcSWaiman Long 18494c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18504c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18514c065dbcSWaiman Long pwqs_node); 18524c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18534c065dbcSWaiman Long if (pwq->plugged) { 18544c065dbcSWaiman Long pwq->plugged = false; 18554c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18564c065dbcSWaiman Long kick_pool(pwq->pool); 18574c065dbcSWaiman Long } 18584c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18594c065dbcSWaiman Long } 18604c065dbcSWaiman Long 18614c065dbcSWaiman Long /** 18625797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18635797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18645797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18655797b1c1STejun Heo * 18665797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18675797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18685797b1c1STejun Heo */ 18695797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18705797b1c1STejun Heo struct worker_pool *caller_pool) 18715797b1c1STejun Heo { 18725797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18735797b1c1STejun Heo struct pool_workqueue *pwq; 18745797b1c1STejun Heo struct work_struct *work; 18755797b1c1STejun Heo 18765797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18775797b1c1STejun Heo 18785797b1c1STejun Heo raw_spin_lock(&nna->lock); 18795797b1c1STejun Heo retry: 18805797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18815797b1c1STejun Heo struct pool_workqueue, pending_node); 18825797b1c1STejun Heo if (!pwq) 18835797b1c1STejun Heo goto out_unlock; 18845797b1c1STejun Heo 18855797b1c1STejun Heo /* 18865797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 18875797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 18885797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 18895797b1c1STejun Heo * nested inside pool locks. 18905797b1c1STejun Heo */ 18915797b1c1STejun Heo if (pwq->pool != locked_pool) { 18925797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18935797b1c1STejun Heo locked_pool = pwq->pool; 18945797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 18955797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18965797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 18975797b1c1STejun Heo raw_spin_lock(&nna->lock); 18985797b1c1STejun Heo goto retry; 18995797b1c1STejun Heo } 19005797b1c1STejun Heo } 19015797b1c1STejun Heo 19025797b1c1STejun Heo /* 19035797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 19045797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 19055797b1c1STejun Heo */ 19065797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 19075797b1c1STejun Heo struct work_struct, entry); 19085797b1c1STejun Heo if (!work) { 19095797b1c1STejun Heo list_del_init(&pwq->pending_node); 19105797b1c1STejun Heo goto retry; 19115797b1c1STejun Heo } 19125797b1c1STejun Heo 19135797b1c1STejun Heo /* 19145797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 19155797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 19165797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 19175797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 19185797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 19195797b1c1STejun Heo */ 19205797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 19215797b1c1STejun Heo pwq->nr_active++; 19225797b1c1STejun Heo __pwq_activate_work(pwq, work); 19235797b1c1STejun Heo 19245797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 19255797b1c1STejun Heo list_del_init(&pwq->pending_node); 19265797b1c1STejun Heo else 19275797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 19285797b1c1STejun Heo 19295797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 19305797b1c1STejun Heo if (pwq->pool != caller_pool) 19315797b1c1STejun Heo kick_pool(pwq->pool); 19325797b1c1STejun Heo } 19335797b1c1STejun Heo 19345797b1c1STejun Heo out_unlock: 19355797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19365797b1c1STejun Heo if (locked_pool != caller_pool) { 19375797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19385797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19395797b1c1STejun Heo } 19405797b1c1STejun Heo } 19415797b1c1STejun Heo 19425797b1c1STejun Heo /** 19431c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19441c270b79STejun Heo * @pwq: pool_workqueue of interest 19451c270b79STejun Heo * 19461c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19475797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19481c270b79STejun Heo */ 19491c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19501c270b79STejun Heo { 19511c270b79STejun Heo struct worker_pool *pool = pwq->pool; 195291ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19531c270b79STejun Heo 19541c270b79STejun Heo lockdep_assert_held(&pool->lock); 19551c270b79STejun Heo 195691ccc6e7STejun Heo /* 195791ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 195891ccc6e7STejun Heo * workqueues. 195991ccc6e7STejun Heo */ 19601c270b79STejun Heo pwq->nr_active--; 196191ccc6e7STejun Heo 196291ccc6e7STejun Heo /* 196391ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 196491ccc6e7STejun Heo * inactive work item on @pwq itself. 196591ccc6e7STejun Heo */ 196691ccc6e7STejun Heo if (!nna) { 19675797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 196891ccc6e7STejun Heo return; 196991ccc6e7STejun Heo } 197091ccc6e7STejun Heo 19715797b1c1STejun Heo /* 19725797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19735797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19745797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19755797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19765797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19775797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19785797b1c1STejun Heo * decremented $nna->nr. 19795797b1c1STejun Heo * 19805797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19815797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19825797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 19835797b1c1STejun Heo * This maintains the forward progress guarantee. 19845797b1c1STejun Heo */ 19855797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 19865797b1c1STejun Heo return; 19875797b1c1STejun Heo 19885797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 19895797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 19903aa62497SLai Jiangshan } 19913aa62497SLai Jiangshan 1992bf4ede01STejun Heo /** 1993112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1994112202d9STejun Heo * @pwq: pwq of interest 1995c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1996bf4ede01STejun Heo * 1997bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1998112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1999bf4ede01STejun Heo * 2000dd6c3c54STejun Heo * NOTE: 2001dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 2002dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 2003dd6c3c54STejun Heo * work item is complete. 2004dd6c3c54STejun Heo * 2005bf4ede01STejun Heo * CONTEXT: 2006a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2007bf4ede01STejun Heo */ 2008c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 2009bf4ede01STejun Heo { 2010c4560c2cSLai Jiangshan int color = get_work_color(work_data); 2011c4560c2cSLai Jiangshan 20121c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 20131c270b79STejun Heo pwq_dec_nr_active(pwq); 2014018f3a13SLai Jiangshan 2015018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 2016bf4ede01STejun Heo 2017bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 2018112202d9STejun Heo if (likely(pwq->flush_color != color)) 20198864b4e5STejun Heo goto out_put; 2020bf4ede01STejun Heo 2021bf4ede01STejun Heo /* are there still in-flight works? */ 2022112202d9STejun Heo if (pwq->nr_in_flight[color]) 20238864b4e5STejun Heo goto out_put; 2024bf4ede01STejun Heo 2025112202d9STejun Heo /* this pwq is done, clear flush_color */ 2026112202d9STejun Heo pwq->flush_color = -1; 2027bf4ede01STejun Heo 2028bf4ede01STejun Heo /* 2029112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 2030bf4ede01STejun Heo * will handle the rest. 2031bf4ede01STejun Heo */ 2032112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2033112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20348864b4e5STejun Heo out_put: 20358864b4e5STejun Heo put_pwq(pwq); 2036bf4ede01STejun Heo } 2037bf4ede01STejun Heo 203836e227d2STejun Heo /** 2039bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 204036e227d2STejun Heo * @work: work item to steal 2041c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags 2042c26e2f2eSTejun Heo * @irq_flags: place to store irq state 204336e227d2STejun Heo * 204436e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2045d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 204636e227d2STejun Heo * 2047d185af30SYacine Belkadi * Return: 20483eb6b31bSMauro Carvalho Chehab * 20493eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 205036e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 205136e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 205236e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 2053bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 2054bbb68dfaSTejun Heo * for arbitrarily long 20553eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 205636e227d2STejun Heo * 2057d185af30SYacine Belkadi * Note: 2058bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2059e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2060e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2061e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2062bbb68dfaSTejun Heo * 2063bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2064c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags). 2065bbb68dfaSTejun Heo * 2066e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2067bf4ede01STejun Heo */ 2068c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags, 2069c26e2f2eSTejun Heo unsigned long *irq_flags) 2070bf4ede01STejun Heo { 2071d565ed63STejun Heo struct worker_pool *pool; 2072112202d9STejun Heo struct pool_workqueue *pwq; 2073bf4ede01STejun Heo 2074c26e2f2eSTejun Heo local_irq_save(*irq_flags); 2075bbb68dfaSTejun Heo 207636e227d2STejun Heo /* try to steal the timer if it exists */ 2077c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) { 207836e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 207936e227d2STejun Heo 2080e0aecdd8STejun Heo /* 2081e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2082e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2083e0aecdd8STejun Heo * running on the local CPU. 2084e0aecdd8STejun Heo */ 208536e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 208636e227d2STejun Heo return 1; 208736e227d2STejun Heo } 208836e227d2STejun Heo 208936e227d2STejun Heo /* try to claim PENDING the normal way */ 2090bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2091bf4ede01STejun Heo return 0; 2092bf4ede01STejun Heo 209324acfb71SThomas Gleixner rcu_read_lock(); 2094bf4ede01STejun Heo /* 2095bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2096bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2097bf4ede01STejun Heo */ 2098d565ed63STejun Heo pool = get_work_pool(work); 2099d565ed63STejun Heo if (!pool) 2100bbb68dfaSTejun Heo goto fail; 2101bf4ede01STejun Heo 2102a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2103bf4ede01STejun Heo /* 2104112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2105112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2106112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2107112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2108112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 21090b3dae68SLai Jiangshan * item is currently queued on that pool. 2110bf4ede01STejun Heo */ 2111112202d9STejun Heo pwq = get_work_pwq(work); 2112112202d9STejun Heo if (pwq && pwq->pool == pool) { 2113c70e1779STejun Heo unsigned long work_data; 2114c70e1779STejun Heo 2115bf4ede01STejun Heo debug_work_deactivate(work); 21163aa62497SLai Jiangshan 21173aa62497SLai Jiangshan /* 2118018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2119018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2120018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2121018f3a13SLai Jiangshan * 2122f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2123d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2124f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 212516062836STejun Heo * management later on and cause stall. Make sure the work 212616062836STejun Heo * item is activated before grabbing. 21273aa62497SLai Jiangshan */ 21284c638030STejun Heo pwq_activate_work(pwq, work); 21293aa62497SLai Jiangshan 2130bf4ede01STejun Heo list_del_init(&work->entry); 213136e227d2STejun Heo 2132c70e1779STejun Heo /* 2133c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2134c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2135c70e1779STejun Heo */ 2136c70e1779STejun Heo work_data = *work_data_bits(work); 21374468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 21384468a00fSLai Jiangshan 2139dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2140c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2141dd6c3c54STejun Heo 2142a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 214324acfb71SThomas Gleixner rcu_read_unlock(); 214436e227d2STejun Heo return 1; 2145bf4ede01STejun Heo } 2146a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2147bbb68dfaSTejun Heo fail: 214824acfb71SThomas Gleixner rcu_read_unlock(); 2149c26e2f2eSTejun Heo local_irq_restore(*irq_flags); 2150bbb68dfaSTejun Heo if (work_is_canceling(work)) 2151bbb68dfaSTejun Heo return -ENOENT; 2152bbb68dfaSTejun Heo cpu_relax(); 215336e227d2STejun Heo return -EAGAIN; 2154bf4ede01STejun Heo } 2155bf4ede01STejun Heo 2156*978b8409STejun Heo struct cwt_wait { 2157*978b8409STejun Heo wait_queue_entry_t wait; 2158*978b8409STejun Heo struct work_struct *work; 2159*978b8409STejun Heo }; 2160*978b8409STejun Heo 2161*978b8409STejun Heo static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 2162*978b8409STejun Heo { 2163*978b8409STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 2164*978b8409STejun Heo 2165*978b8409STejun Heo if (cwait->work != key) 2166*978b8409STejun Heo return 0; 2167*978b8409STejun Heo return autoremove_wake_function(wait, mode, sync, key); 2168*978b8409STejun Heo } 2169*978b8409STejun Heo 2170*978b8409STejun Heo /** 2171*978b8409STejun Heo * work_grab_pending - steal work item from worklist and disable irq 2172*978b8409STejun Heo * @work: work item to steal 2173*978b8409STejun Heo * @cflags: %WORK_CANCEL_ flags 2174*978b8409STejun Heo * @irq_flags: place to store IRQ state 2175*978b8409STejun Heo * 2176*978b8409STejun Heo * Grab PENDING bit of @work. @work can be in any stable state - idle, on timer 2177*978b8409STejun Heo * or on worklist. 2178*978b8409STejun Heo * 2179*978b8409STejun Heo * Must be called in process context. IRQ is disabled on return with IRQ state 2180*978b8409STejun Heo * stored in *@irq_flags. The caller is responsible for re-enabling it using 2181*978b8409STejun Heo * local_irq_restore(). 2182*978b8409STejun Heo * 2183*978b8409STejun Heo * Returns %true if @work was pending. %false if idle. 2184*978b8409STejun Heo */ 2185*978b8409STejun Heo static bool work_grab_pending(struct work_struct *work, u32 cflags, 2186*978b8409STejun Heo unsigned long *irq_flags) 2187*978b8409STejun Heo { 2188*978b8409STejun Heo struct cwt_wait cwait; 2189*978b8409STejun Heo int ret; 2190*978b8409STejun Heo 2191*978b8409STejun Heo might_sleep(); 2192*978b8409STejun Heo repeat: 2193*978b8409STejun Heo ret = try_to_grab_pending(work, cflags, irq_flags); 2194*978b8409STejun Heo if (likely(ret >= 0)) 2195*978b8409STejun Heo return ret; 2196*978b8409STejun Heo if (ret != -ENOENT) 2197*978b8409STejun Heo goto repeat; 2198*978b8409STejun Heo 2199*978b8409STejun Heo /* 2200*978b8409STejun Heo * Someone is already canceling. Wait for it to finish. flush_work() 2201*978b8409STejun Heo * doesn't work for PREEMPT_NONE because we may get woken up between 2202*978b8409STejun Heo * @work's completion and the other canceling task resuming and clearing 2203*978b8409STejun Heo * CANCELING - flush_work() will return false immediately as @work is no 2204*978b8409STejun Heo * longer busy, try_to_grab_pending() will return -ENOENT as @work is 2205*978b8409STejun Heo * still being canceled and the other canceling task won't be able to 2206*978b8409STejun Heo * clear CANCELING as we're hogging the CPU. 2207*978b8409STejun Heo * 2208*978b8409STejun Heo * Let's wait for completion using a waitqueue. As this may lead to the 2209*978b8409STejun Heo * thundering herd problem, use a custom wake function which matches 2210*978b8409STejun Heo * @work along with exclusive wait and wakeup. 2211*978b8409STejun Heo */ 2212*978b8409STejun Heo init_wait(&cwait.wait); 2213*978b8409STejun Heo cwait.wait.func = cwt_wakefn; 2214*978b8409STejun Heo cwait.work = work; 2215*978b8409STejun Heo 2216*978b8409STejun Heo prepare_to_wait_exclusive(&wq_cancel_waitq, &cwait.wait, 2217*978b8409STejun Heo TASK_UNINTERRUPTIBLE); 2218*978b8409STejun Heo if (work_is_canceling(work)) 2219*978b8409STejun Heo schedule(); 2220*978b8409STejun Heo finish_wait(&wq_cancel_waitq, &cwait.wait); 2221*978b8409STejun Heo 2222*978b8409STejun Heo goto repeat; 2223*978b8409STejun Heo } 2224*978b8409STejun Heo 2225bf4ede01STejun Heo /** 2226706026c2STejun Heo * insert_work - insert a work into a pool 2227112202d9STejun Heo * @pwq: pwq @work belongs to 22284690c4abSTejun Heo * @work: work to insert 22294690c4abSTejun Heo * @head: insertion point 22304690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 22314690c4abSTejun Heo * 2232112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2233706026c2STejun Heo * work_struct flags. 22344690c4abSTejun Heo * 22354690c4abSTejun Heo * CONTEXT: 2236a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2237365970a1SDavid Howells */ 2238112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2239112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2240b89deed3SOleg Nesterov { 2241fe089f87STejun Heo debug_work_activate(work); 2242e1d8aa9fSFrederic Weisbecker 2243e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2244f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2245e89a85d6SWalter Wu 22464690c4abSTejun Heo /* we own @work, set data and link */ 2247112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 22481a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 22498864b4e5STejun Heo get_pwq(pwq); 2250b89deed3SOleg Nesterov } 2251b89deed3SOleg Nesterov 2252c8efcc25STejun Heo /* 2253c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 22548d03ecfeSTejun Heo * same workqueue. 2255c8efcc25STejun Heo */ 2256c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2257c8efcc25STejun Heo { 2258c8efcc25STejun Heo struct worker *worker; 2259c8efcc25STejun Heo 22608d03ecfeSTejun Heo worker = current_wq_worker(); 2261c8efcc25STejun Heo /* 2262bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 22638d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2264c8efcc25STejun Heo */ 2265112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2266c8efcc25STejun Heo } 2267c8efcc25STejun Heo 2268ef557180SMike Galbraith /* 2269ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2270ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2271ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2272ef557180SMike Galbraith */ 2273ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2274ef557180SMike Galbraith { 2275ef557180SMike Galbraith int new_cpu; 2276ef557180SMike Galbraith 2277f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2278ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2279ef557180SMike Galbraith return cpu; 2280a8ec5880SAmmar Faizi } else { 2281a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2282f303fccbSTejun Heo } 2283f303fccbSTejun Heo 2284ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2285ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2286ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2287ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2288ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2289ef557180SMike Galbraith return cpu; 2290ef557180SMike Galbraith } 2291ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2292ef557180SMike Galbraith 2293ef557180SMike Galbraith return new_cpu; 2294ef557180SMike Galbraith } 2295ef557180SMike Galbraith 2296d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 22971da177e4SLinus Torvalds struct work_struct *work) 22981da177e4SLinus Torvalds { 2299112202d9STejun Heo struct pool_workqueue *pwq; 2300fe089f87STejun Heo struct worker_pool *last_pool, *pool; 23018a2e8e5dSTejun Heo unsigned int work_flags; 2302b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 23038930cabaSTejun Heo 23048930cabaSTejun Heo /* 23058930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 23068930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 23078930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 23088930cabaSTejun Heo * happen with IRQ disabled. 23098930cabaSTejun Heo */ 23108e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 23111da177e4SLinus Torvalds 231233e3f0a3SRichard Clark /* 231333e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 231433e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 231533e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 231633e3f0a3SRichard Clark */ 231733e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 231833e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2319e41e704bSTejun Heo return; 232024acfb71SThomas Gleixner rcu_read_lock(); 23219e8cd2f5STejun Heo retry: 2322aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2323636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2324636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2325ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2326636b927eSTejun Heo else 2327aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2328aa202f1fSHillf Danton } 2329f3421797STejun Heo 2330636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2331fe089f87STejun Heo pool = pwq->pool; 2332fe089f87STejun Heo 233318aa9effSTejun Heo /* 2334c9178087STejun Heo * If @work was previously on a different pool, it might still be 2335c9178087STejun Heo * running there, in which case the work needs to be queued on that 2336c9178087STejun Heo * pool to guarantee non-reentrancy. 233718aa9effSTejun Heo */ 2338c9e7cf27STejun Heo last_pool = get_work_pool(work); 2339fe089f87STejun Heo if (last_pool && last_pool != pool) { 234018aa9effSTejun Heo struct worker *worker; 234118aa9effSTejun Heo 2342a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 234318aa9effSTejun Heo 2344c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 234518aa9effSTejun Heo 2346112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2347c9178087STejun Heo pwq = worker->current_pwq; 2348fe089f87STejun Heo pool = pwq->pool; 2349fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 23508594fadeSLai Jiangshan } else { 235118aa9effSTejun Heo /* meh... not running there, queue here */ 2352a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2353fe089f87STejun Heo raw_spin_lock(&pool->lock); 235418aa9effSTejun Heo } 23558930cabaSTejun Heo } else { 2356fe089f87STejun Heo raw_spin_lock(&pool->lock); 23578930cabaSTejun Heo } 2358502ca9d8STejun Heo 23599e8cd2f5STejun Heo /* 2360636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2361636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2362636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2363636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2364636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 23659e8cd2f5STejun Heo */ 23669e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 23679e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2368fe089f87STejun Heo raw_spin_unlock(&pool->lock); 23699e8cd2f5STejun Heo cpu_relax(); 23709e8cd2f5STejun Heo goto retry; 23719e8cd2f5STejun Heo } 23729e8cd2f5STejun Heo /* oops */ 23739e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 23749e8cd2f5STejun Heo wq->name, cpu); 23759e8cd2f5STejun Heo } 23769e8cd2f5STejun Heo 2377112202d9STejun Heo /* pwq determined, queue */ 2378112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2379502ca9d8STejun Heo 238024acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 238124acfb71SThomas Gleixner goto out; 23821e19ffc6STejun Heo 2383112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2384112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 23851e19ffc6STejun Heo 2386a045a272STejun Heo /* 2387a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2388a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2389a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2390a045a272STejun Heo */ 23915797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2392fe089f87STejun Heo if (list_empty(&pool->worklist)) 2393fe089f87STejun Heo pool->watchdog_ts = jiffies; 2394fe089f87STejun Heo 2395cdadf009STejun Heo trace_workqueue_activate_work(work); 2396fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 23970219a352STejun Heo kick_pool(pool); 23988a2e8e5dSTejun Heo } else { 2399f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2400fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 24018a2e8e5dSTejun Heo } 24021e19ffc6STejun Heo 240324acfb71SThomas Gleixner out: 2404fe089f87STejun Heo raw_spin_unlock(&pool->lock); 240524acfb71SThomas Gleixner rcu_read_unlock(); 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds 24080fcb78c2SRolf Eike Beer /** 2409c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2410c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2411c1a220e7SZhang Rui * @wq: workqueue to use 2412c1a220e7SZhang Rui * @work: work to queue 2413c1a220e7SZhang Rui * 2414c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2415443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2416443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2417854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2418854f5cc5SPaul E. McKenney * online will get a splat. 2419d185af30SYacine Belkadi * 2420d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2421c1a220e7SZhang Rui */ 2422d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2423d4283e93STejun Heo struct work_struct *work) 2424c1a220e7SZhang Rui { 2425d4283e93STejun Heo bool ret = false; 2426c26e2f2eSTejun Heo unsigned long irq_flags; 24278930cabaSTejun Heo 2428c26e2f2eSTejun Heo local_irq_save(irq_flags); 2429c1a220e7SZhang Rui 243022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24314690c4abSTejun Heo __queue_work(cpu, wq, work); 2432d4283e93STejun Heo ret = true; 2433c1a220e7SZhang Rui } 24348930cabaSTejun Heo 2435c26e2f2eSTejun Heo local_irq_restore(irq_flags); 2436c1a220e7SZhang Rui return ret; 2437c1a220e7SZhang Rui } 2438ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2439c1a220e7SZhang Rui 24408204e0c1SAlexander Duyck /** 2441fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 24428204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 24438204e0c1SAlexander Duyck * 24448204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 24458204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 24468204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 24478204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 24488204e0c1SAlexander Duyck */ 2449fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 24508204e0c1SAlexander Duyck { 24518204e0c1SAlexander Duyck int cpu; 24528204e0c1SAlexander Duyck 24538204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 24548204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 24558204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 24568204e0c1SAlexander Duyck 24578204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 24588204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 24598204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 24608204e0c1SAlexander Duyck return cpu; 24618204e0c1SAlexander Duyck 24628204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 24638204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 24648204e0c1SAlexander Duyck 24658204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 24668204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 24678204e0c1SAlexander Duyck } 24688204e0c1SAlexander Duyck 24698204e0c1SAlexander Duyck /** 24708204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 24718204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 24728204e0c1SAlexander Duyck * @wq: workqueue to use 24738204e0c1SAlexander Duyck * @work: work to queue 24748204e0c1SAlexander Duyck * 24758204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 24768204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 24778204e0c1SAlexander Duyck * NUMA node. 24788204e0c1SAlexander Duyck * 24798204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 24808204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 24818204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 24828204e0c1SAlexander Duyck * 24838204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 24848204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 24858204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 24868204e0c1SAlexander Duyck * 24878204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 24888204e0c1SAlexander Duyck */ 24898204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 24908204e0c1SAlexander Duyck struct work_struct *work) 24918204e0c1SAlexander Duyck { 2492c26e2f2eSTejun Heo unsigned long irq_flags; 24938204e0c1SAlexander Duyck bool ret = false; 24948204e0c1SAlexander Duyck 24958204e0c1SAlexander Duyck /* 24968204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 24978204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 24988204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 24998204e0c1SAlexander Duyck * 25008204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 25018204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 25028204e0c1SAlexander Duyck * some round robin type logic. 25038204e0c1SAlexander Duyck */ 25048204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 25058204e0c1SAlexander Duyck 2506c26e2f2eSTejun Heo local_irq_save(irq_flags); 25078204e0c1SAlexander Duyck 25088204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2509fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 25108204e0c1SAlexander Duyck 25118204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 25128204e0c1SAlexander Duyck ret = true; 25138204e0c1SAlexander Duyck } 25148204e0c1SAlexander Duyck 2515c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25168204e0c1SAlexander Duyck return ret; 25178204e0c1SAlexander Duyck } 25188204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 25198204e0c1SAlexander Duyck 25208c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 25211da177e4SLinus Torvalds { 25228c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 25231da177e4SLinus Torvalds 2524e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 252560c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 25261da177e4SLinus Torvalds } 25271438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 25281da177e4SLinus Torvalds 25297beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 253052bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25311da177e4SLinus Torvalds { 25327beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 25337beb2edfSTejun Heo struct work_struct *work = &dwork->work; 25341da177e4SLinus Torvalds 2535637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 25364b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2537fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2538fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 25397beb2edfSTejun Heo 25408852aac2STejun Heo /* 25418852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 25428852aac2STejun Heo * both optimization and correctness. The earliest @timer can 25438852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 25448852aac2STejun Heo * on that there's no such delay when @delay is 0. 25458852aac2STejun Heo */ 25468852aac2STejun Heo if (!delay) { 25478852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 25488852aac2STejun Heo return; 25498852aac2STejun Heo } 25508852aac2STejun Heo 255160c057bcSLai Jiangshan dwork->wq = wq; 25521265057fSTejun Heo dwork->cpu = cpu; 25537beb2edfSTejun Heo timer->expires = jiffies + delay; 25547beb2edfSTejun Heo 2555aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2556aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2557aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2558aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2559aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 25607beb2edfSTejun Heo add_timer_on(timer, cpu); 2561aae17ebbSLeonardo Bras } else { 2562aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2563041bd12eSTejun Heo add_timer(timer); 2564aae17ebbSLeonardo Bras else 2565aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2566aae17ebbSLeonardo Bras } 25677beb2edfSTejun Heo } 25681da177e4SLinus Torvalds 25690fcb78c2SRolf Eike Beer /** 25700fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 25710fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 25720fcb78c2SRolf Eike Beer * @wq: workqueue to use 2573af9997e4SRandy Dunlap * @dwork: work to queue 25740fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 25750fcb78c2SRolf Eike Beer * 2576d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2577715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2578715f1300STejun Heo * execution. 25790fcb78c2SRolf Eike Beer */ 2580d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 258152bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25827a6bc1cdSVenkatesh Pallipadi { 258352bad64dSDavid Howells struct work_struct *work = &dwork->work; 2584d4283e93STejun Heo bool ret = false; 2585c26e2f2eSTejun Heo unsigned long irq_flags; 25868930cabaSTejun Heo 25878930cabaSTejun Heo /* read the comment in __queue_work() */ 2588c26e2f2eSTejun Heo local_irq_save(irq_flags); 25897a6bc1cdSVenkatesh Pallipadi 259022df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 25917beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2592d4283e93STejun Heo ret = true; 25937a6bc1cdSVenkatesh Pallipadi } 25948930cabaSTejun Heo 2595c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25967a6bc1cdSVenkatesh Pallipadi return ret; 25977a6bc1cdSVenkatesh Pallipadi } 2598ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 25991da177e4SLinus Torvalds 2600c8e55f36STejun Heo /** 26018376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 26028376fe22STejun Heo * @cpu: CPU number to execute work on 26038376fe22STejun Heo * @wq: workqueue to use 26048376fe22STejun Heo * @dwork: work to queue 26058376fe22STejun Heo * @delay: number of jiffies to wait before queueing 26068376fe22STejun Heo * 26078376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 26088376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 26098376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 26108376fe22STejun Heo * current state. 26118376fe22STejun Heo * 2612d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 26138376fe22STejun Heo * pending and its timer was modified. 26148376fe22STejun Heo * 2615e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 26168376fe22STejun Heo * See try_to_grab_pending() for details. 26178376fe22STejun Heo */ 26188376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 26198376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 26208376fe22STejun Heo { 2621c26e2f2eSTejun Heo unsigned long irq_flags; 26228376fe22STejun Heo int ret; 26238376fe22STejun Heo 26248376fe22STejun Heo do { 2625c5f5b942STejun Heo ret = try_to_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, 2626c5f5b942STejun Heo &irq_flags); 26278376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 26288376fe22STejun Heo 26298376fe22STejun Heo if (likely(ret >= 0)) { 26308376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2631c26e2f2eSTejun Heo local_irq_restore(irq_flags); 26328376fe22STejun Heo } 26338376fe22STejun Heo 26348376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 26358376fe22STejun Heo return ret; 26368376fe22STejun Heo } 26378376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 26388376fe22STejun Heo 263905f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 264005f0fe6bSTejun Heo { 264105f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 264205f0fe6bSTejun Heo 264305f0fe6bSTejun Heo /* read the comment in __queue_work() */ 264405f0fe6bSTejun Heo local_irq_disable(); 264505f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 264605f0fe6bSTejun Heo local_irq_enable(); 264705f0fe6bSTejun Heo } 264805f0fe6bSTejun Heo 264905f0fe6bSTejun Heo /** 265005f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 265105f0fe6bSTejun Heo * @wq: workqueue to use 265205f0fe6bSTejun Heo * @rwork: work to queue 265305f0fe6bSTejun Heo * 265405f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 265505f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2656bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 265705f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 265805f0fe6bSTejun Heo */ 265905f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 266005f0fe6bSTejun Heo { 266105f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 266205f0fe6bSTejun Heo 266305f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 266405f0fe6bSTejun Heo rwork->wq = wq; 2665a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 266605f0fe6bSTejun Heo return true; 266705f0fe6bSTejun Heo } 266805f0fe6bSTejun Heo 266905f0fe6bSTejun Heo return false; 267005f0fe6bSTejun Heo } 267105f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 267205f0fe6bSTejun Heo 2673f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2674c34056a3STejun Heo { 2675c34056a3STejun Heo struct worker *worker; 2676c34056a3STejun Heo 2677f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2678c8e55f36STejun Heo if (worker) { 2679c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2680affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2681da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2682e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2683e22bee78STejun Heo worker->flags = WORKER_PREP; 2684c8e55f36STejun Heo } 2685c34056a3STejun Heo return worker; 2686c34056a3STejun Heo } 2687c34056a3STejun Heo 26889546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 26899546b29eSTejun Heo { 26908639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 26919546b29eSTejun Heo return pool->attrs->__pod_cpumask; 26928639ecebSTejun Heo else 26938639ecebSTejun Heo return pool->attrs->cpumask; 26949546b29eSTejun Heo } 26959546b29eSTejun Heo 2696c34056a3STejun Heo /** 26974736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 26984736cbf7SLai Jiangshan * @worker: worker to be attached 26994736cbf7SLai Jiangshan * @pool: the target pool 27004736cbf7SLai Jiangshan * 27014736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 27024736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 27034736cbf7SLai Jiangshan * cpu-[un]hotplugs. 27044736cbf7SLai Jiangshan */ 27054736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 27064736cbf7SLai Jiangshan struct worker_pool *pool) 27074736cbf7SLai Jiangshan { 27081258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 27094736cbf7SLai Jiangshan 27104736cbf7SLai Jiangshan /* 27114cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 27124cb1ef64STejun Heo * across this function. See the comments above the flag definition for 27134cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 27144736cbf7SLai Jiangshan */ 27154cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 27164736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 27174cb1ef64STejun Heo } else { 27184cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27195c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 27204cb1ef64STejun Heo } 27214736cbf7SLai Jiangshan 2722640f17c8SPeter Zijlstra if (worker->rescue_wq) 27239546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2724640f17c8SPeter Zijlstra 27254736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2726a2d812a2STejun Heo worker->pool = pool; 27274736cbf7SLai Jiangshan 27281258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 27294736cbf7SLai Jiangshan } 27304736cbf7SLai Jiangshan 27314736cbf7SLai Jiangshan /** 273260f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 273360f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 273460f5a4bcSLai Jiangshan * 27354736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 27364736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 27374736cbf7SLai Jiangshan * other reference to the pool. 273860f5a4bcSLai Jiangshan */ 2739a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 274060f5a4bcSLai Jiangshan { 2741a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 274260f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 274360f5a4bcSLai Jiangshan 27444cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 27454cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27464cb1ef64STejun Heo 27471258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2748a2d812a2STejun Heo 27495c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2750da028469SLai Jiangshan list_del(&worker->node); 2751a2d812a2STejun Heo worker->pool = NULL; 2752a2d812a2STejun Heo 2753e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 275460f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 27551258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 275660f5a4bcSLai Jiangshan 2757b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2758b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2759b62c0751SLai Jiangshan 276060f5a4bcSLai Jiangshan if (detach_completion) 276160f5a4bcSLai Jiangshan complete(detach_completion); 276260f5a4bcSLai Jiangshan } 276360f5a4bcSLai Jiangshan 276460f5a4bcSLai Jiangshan /** 2765c34056a3STejun Heo * create_worker - create a new workqueue worker 276663d95a91STejun Heo * @pool: pool the new worker will belong to 2767c34056a3STejun Heo * 2768051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2769c34056a3STejun Heo * 2770c34056a3STejun Heo * CONTEXT: 2771c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2772c34056a3STejun Heo * 2773d185af30SYacine Belkadi * Return: 2774c34056a3STejun Heo * Pointer to the newly created worker. 2775c34056a3STejun Heo */ 2776bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2777c34056a3STejun Heo { 2778e441b56fSZhen Lei struct worker *worker; 2779e441b56fSZhen Lei int id; 27805d9c7a1eSLucy Mielke char id_buf[23]; 2781c34056a3STejun Heo 27827cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2783e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 27843f0ea0b8SPetr Mladek if (id < 0) { 27853f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 27863f0ea0b8SPetr Mladek ERR_PTR(id)); 2787e441b56fSZhen Lei return NULL; 27883f0ea0b8SPetr Mladek } 2789c34056a3STejun Heo 2790f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 27913f0ea0b8SPetr Mladek if (!worker) { 27923f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2793c34056a3STejun Heo goto fail; 27943f0ea0b8SPetr Mladek } 2795c34056a3STejun Heo 2796c34056a3STejun Heo worker->id = id; 2797c34056a3STejun Heo 27984cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 279929c91e99STejun Heo if (pool->cpu >= 0) 2800e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2801e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2802f3421797STejun Heo else 2803e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2804e3c916a4STejun Heo 28054cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 28064cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 28073f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 280860f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 280960f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 281060f54038SPetr Mladek id_buf); 281160f54038SPetr Mladek } else { 28123f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 28133f0ea0b8SPetr Mladek worker->task); 281460f54038SPetr Mladek } 2815c34056a3STejun Heo goto fail; 28163f0ea0b8SPetr Mladek } 2817c34056a3STejun Heo 281891151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 28199546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 28204cb1ef64STejun Heo } 282191151228SOleg Nesterov 2822da028469SLai Jiangshan /* successful, attach the worker to the pool */ 28234736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2824822d8405STejun Heo 2825051e1850SLai Jiangshan /* start the newly created worker */ 2826a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 28270219a352STejun Heo 2828051e1850SLai Jiangshan worker->pool->nr_workers++; 2829051e1850SLai Jiangshan worker_enter_idle(worker); 28300219a352STejun Heo 28310219a352STejun Heo /* 28320219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 28336a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 28346a229b0eSTejun Heo * wake it up explicitly. 28350219a352STejun Heo */ 28364cb1ef64STejun Heo if (worker->task) 2837051e1850SLai Jiangshan wake_up_process(worker->task); 28380219a352STejun Heo 2839a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2840051e1850SLai Jiangshan 2841c34056a3STejun Heo return worker; 2842822d8405STejun Heo 2843c34056a3STejun Heo fail: 2844e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2845c34056a3STejun Heo kfree(worker); 2846c34056a3STejun Heo return NULL; 2847c34056a3STejun Heo } 2848c34056a3STejun Heo 2849793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2850793777bcSValentin Schneider { 2851793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2852793777bcSValentin Schneider 2853793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2854793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2855793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2856793777bcSValentin Schneider else 2857793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2858793777bcSValentin Schneider } 2859793777bcSValentin Schneider 2860e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2861e02b9312SValentin Schneider { 2862e02b9312SValentin Schneider struct worker *worker, *tmp; 2863e02b9312SValentin Schneider 2864e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2865e02b9312SValentin Schneider list_del_init(&worker->entry); 2866e02b9312SValentin Schneider unbind_worker(worker); 2867e02b9312SValentin Schneider /* 2868e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2869e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2870e02b9312SValentin Schneider * wouldn't have gotten here. 2871c34056a3STejun Heo * 2872e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2873e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2874e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2875e02b9312SValentin Schneider * outside of pool->lock. 2876e02b9312SValentin Schneider */ 2877e02b9312SValentin Schneider wake_up_process(worker->task); 2878e02b9312SValentin Schneider } 2879e02b9312SValentin Schneider } 2880e02b9312SValentin Schneider 2881e02b9312SValentin Schneider /** 2882e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2883e02b9312SValentin Schneider * @worker: worker to be destroyed 2884e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2885e02b9312SValentin Schneider * 2886e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2887e02b9312SValentin Schneider * should be idle. 2888c8e55f36STejun Heo * 2889c8e55f36STejun Heo * CONTEXT: 2890a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2891c34056a3STejun Heo */ 2892e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2893c34056a3STejun Heo { 2894bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2895c34056a3STejun Heo 2896cd549687STejun Heo lockdep_assert_held(&pool->lock); 2897e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2898cd549687STejun Heo 2899c34056a3STejun Heo /* sanity check frenzy */ 29006183c009STejun Heo if (WARN_ON(worker->current_work) || 290173eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 290273eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 29036183c009STejun Heo return; 2904c34056a3STejun Heo 2905bd7bdd43STejun Heo pool->nr_workers--; 2906bd7bdd43STejun Heo pool->nr_idle--; 2907c8e55f36STejun Heo 2908cb444766STejun Heo worker->flags |= WORKER_DIE; 2909e02b9312SValentin Schneider 2910e02b9312SValentin Schneider list_move(&worker->entry, list); 2911e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2912c34056a3STejun Heo } 2913c34056a3STejun Heo 29143f959aa3SValentin Schneider /** 29153f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 29163f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 29173f959aa3SValentin Schneider * 29183f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 29193f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 29203f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 29213f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 29223f959aa3SValentin Schneider * it expire and re-evaluate things from there. 29233f959aa3SValentin Schneider */ 292432a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2925e22bee78STejun Heo { 292632a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 29273f959aa3SValentin Schneider bool do_cull = false; 29283f959aa3SValentin Schneider 29293f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 29303f959aa3SValentin Schneider return; 29313f959aa3SValentin Schneider 29323f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 29333f959aa3SValentin Schneider 29343f959aa3SValentin Schneider if (too_many_workers(pool)) { 29353f959aa3SValentin Schneider struct worker *worker; 29363f959aa3SValentin Schneider unsigned long expires; 29373f959aa3SValentin Schneider 29383f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 29393f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 29403f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 29413f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 29423f959aa3SValentin Schneider 29433f959aa3SValentin Schneider if (!do_cull) 29443f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 29453f959aa3SValentin Schneider } 29463f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 29473f959aa3SValentin Schneider 29483f959aa3SValentin Schneider if (do_cull) 29493f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 29503f959aa3SValentin Schneider } 29513f959aa3SValentin Schneider 29523f959aa3SValentin Schneider /** 29533f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 29543f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 29553f959aa3SValentin Schneider * 29563f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 29573f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2958e02b9312SValentin Schneider * 2959e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2960e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2961e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 29623f959aa3SValentin Schneider */ 29633f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 29643f959aa3SValentin Schneider { 29653f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 29669680540cSYang Yingliang LIST_HEAD(cull_list); 2967e22bee78STejun Heo 2968e02b9312SValentin Schneider /* 2969e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2970e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2971e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2972e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2973e02b9312SValentin Schneider */ 2974e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2975a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2976e22bee78STejun Heo 29773347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2978e22bee78STejun Heo struct worker *worker; 2979e22bee78STejun Heo unsigned long expires; 2980e22bee78STejun Heo 298163d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2982e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2983e22bee78STejun Heo 29843347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 298563d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 29863347fc9fSLai Jiangshan break; 2987e22bee78STejun Heo } 29883347fc9fSLai Jiangshan 2989e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2990e22bee78STejun Heo } 2991e22bee78STejun Heo 2992a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2993e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2994e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2995e22bee78STejun Heo } 2996e22bee78STejun Heo 2997493a1724STejun Heo static void send_mayday(struct work_struct *work) 2998e22bee78STejun Heo { 2999112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3000112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 3001493a1724STejun Heo 30022e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 3003e22bee78STejun Heo 3004493008a8STejun Heo if (!wq->rescuer) 3005493a1724STejun Heo return; 3006e22bee78STejun Heo 3007e22bee78STejun Heo /* mayday mayday mayday */ 3008493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 300977668c8bSLai Jiangshan /* 301077668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 301177668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 301277668c8bSLai Jiangshan * rescuer is done with it. 301377668c8bSLai Jiangshan */ 301477668c8bSLai Jiangshan get_pwq(pwq); 3015493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3016e22bee78STejun Heo wake_up_process(wq->rescuer->task); 3017725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 3018493a1724STejun Heo } 3019e22bee78STejun Heo } 3020e22bee78STejun Heo 302132a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 3022e22bee78STejun Heo { 302332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 3024e22bee78STejun Heo struct work_struct *work; 3025e22bee78STejun Heo 3026a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3027a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 3028e22bee78STejun Heo 302963d95a91STejun Heo if (need_to_create_worker(pool)) { 3030e22bee78STejun Heo /* 3031e22bee78STejun Heo * We've been trying to create a new worker but 3032e22bee78STejun Heo * haven't been successful. We might be hitting an 3033e22bee78STejun Heo * allocation deadlock. Send distress signals to 3034e22bee78STejun Heo * rescuers. 3035e22bee78STejun Heo */ 303663d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 3037e22bee78STejun Heo send_mayday(work); 3038e22bee78STejun Heo } 3039e22bee78STejun Heo 3040a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3041a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3042e22bee78STejun Heo 304363d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 3044e22bee78STejun Heo } 3045e22bee78STejun Heo 3046e22bee78STejun Heo /** 3047e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 304863d95a91STejun Heo * @pool: pool to create a new worker for 3049e22bee78STejun Heo * 305063d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 3051e22bee78STejun Heo * have at least one idle worker on return from this function. If 3052e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 305363d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 3054e22bee78STejun Heo * possible allocation deadlock. 3055e22bee78STejun Heo * 3056c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 3057c5aa87bbSTejun Heo * may_start_working() %true. 3058e22bee78STejun Heo * 3059e22bee78STejun Heo * LOCKING: 3060a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3061e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 3062e22bee78STejun Heo * manager. 3063e22bee78STejun Heo */ 306429187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 3065d565ed63STejun Heo __releases(&pool->lock) 3066d565ed63STejun Heo __acquires(&pool->lock) 3067e22bee78STejun Heo { 3068e22bee78STejun Heo restart: 3069a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30709f9c2364STejun Heo 3071e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 307263d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 3073e22bee78STejun Heo 3074e22bee78STejun Heo while (true) { 3075051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 3076e22bee78STejun Heo break; 3077e22bee78STejun Heo 3078e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 30799f9c2364STejun Heo 308063d95a91STejun Heo if (!need_to_create_worker(pool)) 3081e22bee78STejun Heo break; 3082e22bee78STejun Heo } 3083e22bee78STejun Heo 308463d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 3085a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3086051e1850SLai Jiangshan /* 3087051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 3088051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 3089051e1850SLai Jiangshan * already become busy. 3090051e1850SLai Jiangshan */ 309163d95a91STejun Heo if (need_to_create_worker(pool)) 3092e22bee78STejun Heo goto restart; 3093e22bee78STejun Heo } 3094e22bee78STejun Heo 3095e22bee78STejun Heo /** 3096e22bee78STejun Heo * manage_workers - manage worker pool 3097e22bee78STejun Heo * @worker: self 3098e22bee78STejun Heo * 3099706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 3100e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3101706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3102e22bee78STejun Heo * 3103e22bee78STejun Heo * The caller can safely start processing works on false return. On 3104e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3105e22bee78STejun Heo * and may_start_working() is true. 3106e22bee78STejun Heo * 3107e22bee78STejun Heo * CONTEXT: 3108a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3109e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3110e22bee78STejun Heo * 3111d185af30SYacine Belkadi * Return: 311229187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 311329187a9eSTejun Heo * start processing works, %true if management function was performed and 311429187a9eSTejun Heo * the conditions that the caller verified before calling the function may 311529187a9eSTejun Heo * no longer be true. 3116e22bee78STejun Heo */ 3117e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3118e22bee78STejun Heo { 311963d95a91STejun Heo struct worker_pool *pool = worker->pool; 3120e22bee78STejun Heo 3121692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 312229187a9eSTejun Heo return false; 3123692b4825STejun Heo 3124692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 31252607d7a6STejun Heo pool->manager = worker; 3126e22bee78STejun Heo 312729187a9eSTejun Heo maybe_create_worker(pool); 3128e22bee78STejun Heo 31292607d7a6STejun Heo pool->manager = NULL; 3130692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3131d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 313229187a9eSTejun Heo return true; 3133e22bee78STejun Heo } 3134e22bee78STejun Heo 3135a62428c0STejun Heo /** 3136a62428c0STejun Heo * process_one_work - process single work 3137c34056a3STejun Heo * @worker: self 3138a62428c0STejun Heo * @work: work to process 3139a62428c0STejun Heo * 3140a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3141a62428c0STejun Heo * process a single work including synchronization against and 3142a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3143a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3144a62428c0STejun Heo * call this function to process a work. 3145a62428c0STejun Heo * 3146a62428c0STejun Heo * CONTEXT: 3147a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3148a62428c0STejun Heo */ 3149c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3150d565ed63STejun Heo __releases(&pool->lock) 3151d565ed63STejun Heo __acquires(&pool->lock) 31521da177e4SLinus Torvalds { 3153112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3154bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3155c4560c2cSLai Jiangshan unsigned long work_data; 3156c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 31574e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 31584e6045f1SJohannes Berg /* 3159a62428c0STejun Heo * It is permissible to free the struct work_struct from 3160a62428c0STejun Heo * inside the function that is called from it, this we need to 3161a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3162a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3163a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 31644e6045f1SJohannes Berg */ 31654d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 31664d82a1deSPeter Zijlstra 31674d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 31684e6045f1SJohannes Berg #endif 3169807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 317085327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3171ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 317225511a47STejun Heo 31738930cabaSTejun Heo /* claim and dequeue */ 3174dc186ad7SThomas Gleixner debug_work_deactivate(work); 3175c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3176c34056a3STejun Heo worker->current_work = work; 3177a2c1c57bSTejun Heo worker->current_func = work->func; 3178112202d9STejun Heo worker->current_pwq = pwq; 31794cb1ef64STejun Heo if (worker->task) 3180616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3181c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3182d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 31837a22ad75STejun Heo 31848bf89593STejun Heo /* 31858bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 31868bf89593STejun Heo * overridden through set_worker_desc(). 31878bf89593STejun Heo */ 31888bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 31898bf89593STejun Heo 3190a62428c0STejun Heo list_del_init(&work->entry); 3191a62428c0STejun Heo 3192649027d7STejun Heo /* 3193228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3194228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3195228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3196228f1d00SLai Jiangshan * execution of the pending work items. 3197fb0e7bebSTejun Heo */ 3198616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3199228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3200fb0e7bebSTejun Heo 3201974271c4STejun Heo /* 32020219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 32030219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 32040219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 32050219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3206974271c4STejun Heo */ 32070219a352STejun Heo kick_pool(pool); 3208974271c4STejun Heo 32098930cabaSTejun Heo /* 32107c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3211d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 321223657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 321323657bb1STejun Heo * disabled. 32148930cabaSTejun Heo */ 32157c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 32161da177e4SLinus Torvalds 3217fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3218a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3219365970a1SDavid Howells 3220c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3221c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 3222a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 32233295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3224e6f3faa7SPeter Zijlstra /* 3225f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3226f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3227e6f3faa7SPeter Zijlstra * 3228e6f3faa7SPeter Zijlstra * However, that would result in: 3229e6f3faa7SPeter Zijlstra * 3230e6f3faa7SPeter Zijlstra * A(W1) 3231e6f3faa7SPeter Zijlstra * WFC(C) 3232e6f3faa7SPeter Zijlstra * A(W1) 3233e6f3faa7SPeter Zijlstra * C(C) 3234e6f3faa7SPeter Zijlstra * 3235e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3236e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3237e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3238f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3239e6f3faa7SPeter Zijlstra * these locks. 3240e6f3faa7SPeter Zijlstra * 3241e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3242e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3243e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3244e6f3faa7SPeter Zijlstra */ 3245f52be570SPeter Zijlstra lockdep_invariant_state(true); 3246e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3247a2c1c57bSTejun Heo worker->current_func(work); 3248e36c886aSArjan van de Ven /* 3249e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3250e36c886aSArjan van de Ven * point will only record its address. 3251e36c886aSArjan van de Ven */ 32521c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3253725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 32543295f0efSIngo Molnar lock_map_release(&lockdep_map); 3255112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 32561da177e4SLinus Torvalds 3257c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3258c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3259c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3260c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3261c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3262c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3263c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3264c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3265c35aea39STejun Heo worker->current_func); 3266d5abe669SPeter Zijlstra debug_show_held_locks(current); 3267d5abe669SPeter Zijlstra dump_stack(); 3268d5abe669SPeter Zijlstra } 3269d5abe669SPeter Zijlstra 3270b22ce278STejun Heo /* 3271025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3272b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3273b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3274b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3275789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3276789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3277b22ce278STejun Heo */ 32784cb1ef64STejun Heo if (worker->task) 3279a7e6425eSPaul E. McKenney cond_resched(); 3280b22ce278STejun Heo 3281a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3282a62428c0STejun Heo 3283616db877STejun Heo /* 3284616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3285616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3286616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3287616db877STejun Heo */ 3288fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3289fb0e7bebSTejun Heo 32901b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 32911b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 32921b69ac6bSJohannes Weiner 3293a62428c0STejun Heo /* we're done with it, release */ 329442f8570fSSasha Levin hash_del(&worker->hentry); 3295c34056a3STejun Heo worker->current_work = NULL; 3296a2c1c57bSTejun Heo worker->current_func = NULL; 3297112202d9STejun Heo worker->current_pwq = NULL; 3298d812796eSLai Jiangshan worker->current_color = INT_MAX; 3299dd6c3c54STejun Heo 3300dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3301c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 33021da177e4SLinus Torvalds } 33031da177e4SLinus Torvalds 3304affee4b2STejun Heo /** 3305affee4b2STejun Heo * process_scheduled_works - process scheduled works 3306affee4b2STejun Heo * @worker: self 3307affee4b2STejun Heo * 3308affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3309affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3310affee4b2STejun Heo * fetches a work from the top and executes it. 3311affee4b2STejun Heo * 3312affee4b2STejun Heo * CONTEXT: 3313a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3314affee4b2STejun Heo * multiple times. 3315affee4b2STejun Heo */ 3316affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 33171da177e4SLinus Torvalds { 3318c0ab017dSTejun Heo struct work_struct *work; 3319c0ab017dSTejun Heo bool first = true; 3320c0ab017dSTejun Heo 3321c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3322c0ab017dSTejun Heo struct work_struct, entry))) { 3323c0ab017dSTejun Heo if (first) { 3324c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3325c0ab017dSTejun Heo first = false; 3326c0ab017dSTejun Heo } 3327c34056a3STejun Heo process_one_work(worker, work); 3328a62428c0STejun Heo } 33291da177e4SLinus Torvalds } 33301da177e4SLinus Torvalds 3331197f6accSTejun Heo static void set_pf_worker(bool val) 3332197f6accSTejun Heo { 3333197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3334197f6accSTejun Heo if (val) 3335197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3336197f6accSTejun Heo else 3337197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3338197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3339197f6accSTejun Heo } 3340197f6accSTejun Heo 33414690c4abSTejun Heo /** 33424690c4abSTejun Heo * worker_thread - the worker thread function 3343c34056a3STejun Heo * @__worker: self 33444690c4abSTejun Heo * 3345c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3346c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3347c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3348c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3349c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3350d185af30SYacine Belkadi * 3351d185af30SYacine Belkadi * Return: 0 33524690c4abSTejun Heo */ 3353c34056a3STejun Heo static int worker_thread(void *__worker) 33541da177e4SLinus Torvalds { 3355c34056a3STejun Heo struct worker *worker = __worker; 3356bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 33571da177e4SLinus Torvalds 3358e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3359197f6accSTejun Heo set_pf_worker(true); 3360c8e55f36STejun Heo woke_up: 3361a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3362affee4b2STejun Heo 3363a9ab775bSTejun Heo /* am I supposed to die? */ 3364a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3365a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3366197f6accSTejun Heo set_pf_worker(false); 336760f5a4bcSLai Jiangshan 336860f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3369e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3370a2d812a2STejun Heo worker_detach_from_pool(worker); 3371e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 337260f5a4bcSLai Jiangshan kfree(worker); 3373c8e55f36STejun Heo return 0; 3374c8e55f36STejun Heo } 3375c8e55f36STejun Heo 3376c8e55f36STejun Heo worker_leave_idle(worker); 3377db7bccf4STejun Heo recheck: 3378e22bee78STejun Heo /* no more worker necessary? */ 337963d95a91STejun Heo if (!need_more_worker(pool)) 3380e22bee78STejun Heo goto sleep; 3381e22bee78STejun Heo 3382e22bee78STejun Heo /* do we need to manage? */ 338363d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3384e22bee78STejun Heo goto recheck; 3385e22bee78STejun Heo 3386c8e55f36STejun Heo /* 3387c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3388c8e55f36STejun Heo * preparing to process a work or actually processing it. 3389c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3390c8e55f36STejun Heo */ 33916183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3392c8e55f36STejun Heo 3393e22bee78STejun Heo /* 3394a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3395a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3396a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3397a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3398a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3399e22bee78STejun Heo */ 3400a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3401e22bee78STejun Heo 3402e22bee78STejun Heo do { 3403affee4b2STejun Heo struct work_struct *work = 3404bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3405affee4b2STejun Heo struct work_struct, entry); 3406affee4b2STejun Heo 3407873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3408affee4b2STejun Heo process_scheduled_works(worker); 340963d95a91STejun Heo } while (keep_working(pool)); 3410affee4b2STejun Heo 3411228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3412d313dd85STejun Heo sleep: 3413c8e55f36STejun Heo /* 3414d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3415d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3416d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3417d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3418d565ed63STejun Heo * event. 3419c8e55f36STejun Heo */ 3420c8e55f36STejun Heo worker_enter_idle(worker); 3421c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3422a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 34231da177e4SLinus Torvalds schedule(); 3424c8e55f36STejun Heo goto woke_up; 34251da177e4SLinus Torvalds } 34261da177e4SLinus Torvalds 3427e22bee78STejun Heo /** 3428e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3429111c225aSTejun Heo * @__rescuer: self 3430e22bee78STejun Heo * 3431e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3432493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3433e22bee78STejun Heo * 3434706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3435e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3436e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3437e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3438e22bee78STejun Heo * the problem rescuer solves. 3439e22bee78STejun Heo * 3440706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3441706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3442e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3443e22bee78STejun Heo * 3444e22bee78STejun Heo * This should happen rarely. 3445d185af30SYacine Belkadi * 3446d185af30SYacine Belkadi * Return: 0 3447e22bee78STejun Heo */ 3448111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3449e22bee78STejun Heo { 3450111c225aSTejun Heo struct worker *rescuer = __rescuer; 3451111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 34524d595b86SLai Jiangshan bool should_stop; 3453e22bee78STejun Heo 3454e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3455111c225aSTejun Heo 3456111c225aSTejun Heo /* 3457111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3458111c225aSTejun Heo * doesn't participate in concurrency management. 3459111c225aSTejun Heo */ 3460197f6accSTejun Heo set_pf_worker(true); 3461e22bee78STejun Heo repeat: 3462c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 34631da177e4SLinus Torvalds 34644d595b86SLai Jiangshan /* 34654d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 34664d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 34674d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 34684d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 34694d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 34704d595b86SLai Jiangshan * list is always empty on exit. 34714d595b86SLai Jiangshan */ 34724d595b86SLai Jiangshan should_stop = kthread_should_stop(); 34731da177e4SLinus Torvalds 3474493a1724STejun Heo /* see whether any pwq is asking for help */ 3475a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3476493a1724STejun Heo 3477493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3478493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3479493a1724STejun Heo struct pool_workqueue, mayday_node); 3480112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3481e22bee78STejun Heo struct work_struct *work, *n; 3482e22bee78STejun Heo 3483e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3484493a1724STejun Heo list_del_init(&pwq->mayday_node); 3485493a1724STejun Heo 3486a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3487e22bee78STejun Heo 348851697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 348951697d39SLai Jiangshan 3490a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3491e22bee78STejun Heo 3492e22bee78STejun Heo /* 3493e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3494e22bee78STejun Heo * process'em. 3495e22bee78STejun Heo */ 3496873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 349782607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3498873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3499873eaca6STejun Heo assign_work(work, rescuer, &n)) 3500725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 350182607adcSTejun Heo } 3502e22bee78STejun Heo 3503873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3504e22bee78STejun Heo process_scheduled_works(rescuer); 35057576958aSTejun Heo 35067576958aSTejun Heo /* 3507008847f6SNeilBrown * The above execution of rescued work items could 3508008847f6SNeilBrown * have created more to rescue through 3509f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3510008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3511008847f6SNeilBrown * that such back-to-back work items, which may be 3512008847f6SNeilBrown * being used to relieve memory pressure, don't 3513008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3514008847f6SNeilBrown */ 35154f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3516a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3517e66b39afSTejun Heo /* 3518e66b39afSTejun Heo * Queue iff we aren't racing destruction 3519e66b39afSTejun Heo * and somebody else hasn't queued it already. 3520e66b39afSTejun Heo */ 3521e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3522008847f6SNeilBrown get_pwq(pwq); 3523e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3524e66b39afSTejun Heo } 3525a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3526008847f6SNeilBrown } 3527008847f6SNeilBrown } 3528008847f6SNeilBrown 3529008847f6SNeilBrown /* 353077668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 353113b1d625SLai Jiangshan * go away while we're still attached to it. 353277668c8bSLai Jiangshan */ 353377668c8bSLai Jiangshan put_pwq(pwq); 353477668c8bSLai Jiangshan 353577668c8bSLai Jiangshan /* 35360219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 35370219a352STejun Heo * with 0 concurrency and stalling the execution. 35387576958aSTejun Heo */ 35390219a352STejun Heo kick_pool(pool); 35407576958aSTejun Heo 3541a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 354213b1d625SLai Jiangshan 3543a2d812a2STejun Heo worker_detach_from_pool(rescuer); 354413b1d625SLai Jiangshan 3545a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 35461da177e4SLinus Torvalds } 35471da177e4SLinus Torvalds 3548a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3549493a1724STejun Heo 35504d595b86SLai Jiangshan if (should_stop) { 35514d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3552197f6accSTejun Heo set_pf_worker(false); 35534d595b86SLai Jiangshan return 0; 35544d595b86SLai Jiangshan } 35554d595b86SLai Jiangshan 3556111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3557111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3558e22bee78STejun Heo schedule(); 3559e22bee78STejun Heo goto repeat; 35601da177e4SLinus Torvalds } 35611da177e4SLinus Torvalds 35624cb1ef64STejun Heo static void bh_worker(struct worker *worker) 35634cb1ef64STejun Heo { 35644cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 35654cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 35664cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 35674cb1ef64STejun Heo 35684cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 35694cb1ef64STejun Heo worker_leave_idle(worker); 35704cb1ef64STejun Heo 35714cb1ef64STejun Heo /* 35724cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 35734cb1ef64STejun Heo * explanations on each step. 35744cb1ef64STejun Heo */ 35754cb1ef64STejun Heo if (!need_more_worker(pool)) 35764cb1ef64STejun Heo goto done; 35774cb1ef64STejun Heo 35784cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 35794cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 35804cb1ef64STejun Heo 35814cb1ef64STejun Heo do { 35824cb1ef64STejun Heo struct work_struct *work = 35834cb1ef64STejun Heo list_first_entry(&pool->worklist, 35844cb1ef64STejun Heo struct work_struct, entry); 35854cb1ef64STejun Heo 35864cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 35874cb1ef64STejun Heo process_scheduled_works(worker); 35884cb1ef64STejun Heo } while (keep_working(pool) && 35894cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 35904cb1ef64STejun Heo 35914cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 35924cb1ef64STejun Heo done: 35934cb1ef64STejun Heo worker_enter_idle(worker); 35944cb1ef64STejun Heo kick_pool(pool); 35954cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 35964cb1ef64STejun Heo } 35974cb1ef64STejun Heo 35984cb1ef64STejun Heo /* 35994cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 36004cb1ef64STejun Heo * 36014cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 36024cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 36034cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 36044cb1ef64STejun Heo * can be dropped. 36054cb1ef64STejun Heo * 36064cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 36074cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 36084cb1ef64STejun Heo */ 36094cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 36104cb1ef64STejun Heo { 36114cb1ef64STejun Heo struct worker_pool *pool = 36124cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 36134cb1ef64STejun Heo if (need_more_worker(pool)) 36144cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36154cb1ef64STejun Heo } 36164cb1ef64STejun Heo 3617fca839c0STejun Heo /** 3618fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3619fca839c0STejun Heo * @target_wq: workqueue being flushed 3620fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3621fca839c0STejun Heo * 3622fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3623fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3624fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3625fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3626fca839c0STejun Heo * a deadlock. 3627fca839c0STejun Heo */ 3628fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3629fca839c0STejun Heo struct work_struct *target_work) 3630fca839c0STejun Heo { 3631fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3632fca839c0STejun Heo struct worker *worker; 3633fca839c0STejun Heo 3634fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3635fca839c0STejun Heo return; 3636fca839c0STejun Heo 3637fca839c0STejun Heo worker = current_wq_worker(); 3638fca839c0STejun Heo 3639fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3640d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3641fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 364223d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 364323d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3644d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3645fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3646fca839c0STejun Heo target_wq->name, target_func); 3647fca839c0STejun Heo } 3648fca839c0STejun Heo 3649fc2e4d70SOleg Nesterov struct wq_barrier { 3650fc2e4d70SOleg Nesterov struct work_struct work; 3651fc2e4d70SOleg Nesterov struct completion done; 36522607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3653fc2e4d70SOleg Nesterov }; 3654fc2e4d70SOleg Nesterov 3655fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3656fc2e4d70SOleg Nesterov { 3657fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3658fc2e4d70SOleg Nesterov complete(&barr->done); 3659fc2e4d70SOleg Nesterov } 3660fc2e4d70SOleg Nesterov 36614690c4abSTejun Heo /** 36624690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3663112202d9STejun Heo * @pwq: pwq to insert barrier into 36644690c4abSTejun Heo * @barr: wq_barrier to insert 3665affee4b2STejun Heo * @target: target work to attach @barr to 3666affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 36674690c4abSTejun Heo * 3668affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3669affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3670affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3671affee4b2STejun Heo * cpu. 3672affee4b2STejun Heo * 3673affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3674affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3675affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3676affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3677affee4b2STejun Heo * after a work with LINKED flag set. 3678affee4b2STejun Heo * 3679affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3680112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 36814690c4abSTejun Heo * 36824690c4abSTejun Heo * CONTEXT: 3683a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 36844690c4abSTejun Heo */ 3685112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3686affee4b2STejun Heo struct wq_barrier *barr, 3687affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3688fc2e4d70SOleg Nesterov { 36894cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3690d812796eSLai Jiangshan unsigned int work_flags = 0; 3691d812796eSLai Jiangshan unsigned int work_color; 3692affee4b2STejun Heo struct list_head *head; 3693affee4b2STejun Heo 3694dc186ad7SThomas Gleixner /* 3695d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3696dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3697dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3698dc186ad7SThomas Gleixner * might deadlock. 36994cb1ef64STejun Heo * 37004cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 37014cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 37024cb1ef64STejun Heo * usage". 3703dc186ad7SThomas Gleixner */ 37044cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 37054cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 370622df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 370752fa5bc5SBoqun Feng 3708fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3709fd1a5b04SByungchul Park 37102607d7a6STejun Heo barr->task = current; 371183c22520SOleg Nesterov 37125797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3713018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3714018f3a13SLai Jiangshan 3715affee4b2STejun Heo /* 3716affee4b2STejun Heo * If @target is currently being executed, schedule the 3717affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3718affee4b2STejun Heo */ 3719d812796eSLai Jiangshan if (worker) { 3720affee4b2STejun Heo head = worker->scheduled.next; 3721d812796eSLai Jiangshan work_color = worker->current_color; 3722d812796eSLai Jiangshan } else { 3723affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3724affee4b2STejun Heo 3725affee4b2STejun Heo head = target->entry.next; 3726affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3727d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3728d812796eSLai Jiangshan work_color = get_work_color(*bits); 3729affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3730affee4b2STejun Heo } 3731affee4b2STejun Heo 3732d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3733d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3734d812796eSLai Jiangshan 3735d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3736fc2e4d70SOleg Nesterov } 3737fc2e4d70SOleg Nesterov 373873f53c4aSTejun Heo /** 3739112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 374073f53c4aSTejun Heo * @wq: workqueue being flushed 374173f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 374273f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 374373f53c4aSTejun Heo * 3744112202d9STejun Heo * Prepare pwqs for workqueue flushing. 374573f53c4aSTejun Heo * 3746112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3747112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3748112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3749112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3750112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 375173f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 375273f53c4aSTejun Heo * 375373f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 375473f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 375573f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 375673f53c4aSTejun Heo * is returned. 375773f53c4aSTejun Heo * 3758112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 375973f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 376073f53c4aSTejun Heo * advanced to @work_color. 376173f53c4aSTejun Heo * 376273f53c4aSTejun Heo * CONTEXT: 37633c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 376473f53c4aSTejun Heo * 3765d185af30SYacine Belkadi * Return: 376673f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 376773f53c4aSTejun Heo * otherwise. 376873f53c4aSTejun Heo */ 3769112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 377073f53c4aSTejun Heo int flush_color, int work_color) 37711da177e4SLinus Torvalds { 377273f53c4aSTejun Heo bool wait = false; 377349e3cf44STejun Heo struct pool_workqueue *pwq; 37741da177e4SLinus Torvalds 377573f53c4aSTejun Heo if (flush_color >= 0) { 37766183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3777112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3778dc186ad7SThomas Gleixner } 377914441960SOleg Nesterov 378049e3cf44STejun Heo for_each_pwq(pwq, wq) { 3781112202d9STejun Heo struct worker_pool *pool = pwq->pool; 37821da177e4SLinus Torvalds 3783a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 378473f53c4aSTejun Heo 378573f53c4aSTejun Heo if (flush_color >= 0) { 37866183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 378773f53c4aSTejun Heo 3788112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3789112202d9STejun Heo pwq->flush_color = flush_color; 3790112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 379173f53c4aSTejun Heo wait = true; 37921da177e4SLinus Torvalds } 379373f53c4aSTejun Heo } 379473f53c4aSTejun Heo 379573f53c4aSTejun Heo if (work_color >= 0) { 37966183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3797112202d9STejun Heo pwq->work_color = work_color; 379873f53c4aSTejun Heo } 379973f53c4aSTejun Heo 3800a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 38011da177e4SLinus Torvalds } 38021da177e4SLinus Torvalds 3803112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 380473f53c4aSTejun Heo complete(&wq->first_flusher->done); 380573f53c4aSTejun Heo 380673f53c4aSTejun Heo return wait; 380783c22520SOleg Nesterov } 38081da177e4SLinus Torvalds 3809c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3810c35aea39STejun Heo { 38114cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 38124cb1ef64STejun Heo if (wq->flags & WQ_BH) 38134cb1ef64STejun Heo local_bh_disable(); 38144cb1ef64STejun Heo 3815c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3816c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 38174cb1ef64STejun Heo 38184cb1ef64STejun Heo if (wq->flags & WQ_BH) 38194cb1ef64STejun Heo local_bh_enable(); 38204cb1ef64STejun Heo #endif 3821c35aea39STejun Heo } 3822c35aea39STejun Heo 3823c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3824c35aea39STejun Heo struct workqueue_struct *wq) 3825c35aea39STejun Heo { 38264cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 38274cb1ef64STejun Heo if (wq->flags & WQ_BH) 38284cb1ef64STejun Heo local_bh_disable(); 38294cb1ef64STejun Heo 3830c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3831c35aea39STejun Heo lock_map_release(&work->lockdep_map); 38324cb1ef64STejun Heo 38334cb1ef64STejun Heo if (wq->flags & WQ_BH) 38344cb1ef64STejun Heo local_bh_enable(); 38354cb1ef64STejun Heo #endif 3836c35aea39STejun Heo } 3837c35aea39STejun Heo 38380fcb78c2SRolf Eike Beer /** 3839c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 38400fcb78c2SRolf Eike Beer * @wq: workqueue to flush 38411da177e4SLinus Torvalds * 3842c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3843c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 38441da177e4SLinus Torvalds */ 3845c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 38461da177e4SLinus Torvalds { 384773f53c4aSTejun Heo struct wq_flusher this_flusher = { 384873f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 384973f53c4aSTejun Heo .flush_color = -1, 3850fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 385173f53c4aSTejun Heo }; 385273f53c4aSTejun Heo int next_color; 3853b1f4ec17SOleg Nesterov 38543347fa09STejun Heo if (WARN_ON(!wq_online)) 38553347fa09STejun Heo return; 38563347fa09STejun Heo 3857c35aea39STejun Heo touch_wq_lockdep_map(wq); 385887915adcSJohannes Berg 38593c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 386073f53c4aSTejun Heo 386173f53c4aSTejun Heo /* 386273f53c4aSTejun Heo * Start-to-wait phase 386373f53c4aSTejun Heo */ 386473f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 386573f53c4aSTejun Heo 386673f53c4aSTejun Heo if (next_color != wq->flush_color) { 386773f53c4aSTejun Heo /* 386873f53c4aSTejun Heo * Color space is not full. The current work_color 386973f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 387073f53c4aSTejun Heo * by one. 387173f53c4aSTejun Heo */ 38726183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 387373f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 387473f53c4aSTejun Heo wq->work_color = next_color; 387573f53c4aSTejun Heo 387673f53c4aSTejun Heo if (!wq->first_flusher) { 387773f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 38786183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 387973f53c4aSTejun Heo 388073f53c4aSTejun Heo wq->first_flusher = &this_flusher; 388173f53c4aSTejun Heo 3882112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 388373f53c4aSTejun Heo wq->work_color)) { 388473f53c4aSTejun Heo /* nothing to flush, done */ 388573f53c4aSTejun Heo wq->flush_color = next_color; 388673f53c4aSTejun Heo wq->first_flusher = NULL; 388773f53c4aSTejun Heo goto out_unlock; 388873f53c4aSTejun Heo } 388973f53c4aSTejun Heo } else { 389073f53c4aSTejun Heo /* wait in queue */ 38916183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 389273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3893112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 389473f53c4aSTejun Heo } 389573f53c4aSTejun Heo } else { 389673f53c4aSTejun Heo /* 389773f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 389873f53c4aSTejun Heo * The next flush completion will assign us 389973f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 390073f53c4aSTejun Heo */ 390173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 390273f53c4aSTejun Heo } 390373f53c4aSTejun Heo 3904fca839c0STejun Heo check_flush_dependency(wq, NULL); 3905fca839c0STejun Heo 39063c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 390773f53c4aSTejun Heo 390873f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 390973f53c4aSTejun Heo 391073f53c4aSTejun Heo /* 391173f53c4aSTejun Heo * Wake-up-and-cascade phase 391273f53c4aSTejun Heo * 391373f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 391473f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 391573f53c4aSTejun Heo */ 391600d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 391773f53c4aSTejun Heo return; 391873f53c4aSTejun Heo 39193c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 392073f53c4aSTejun Heo 39214ce48b37STejun Heo /* we might have raced, check again with mutex held */ 39224ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 39234ce48b37STejun Heo goto out_unlock; 39244ce48b37STejun Heo 392500d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 392673f53c4aSTejun Heo 39276183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 39286183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 392973f53c4aSTejun Heo 393073f53c4aSTejun Heo while (true) { 393173f53c4aSTejun Heo struct wq_flusher *next, *tmp; 393273f53c4aSTejun Heo 393373f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 393473f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 393573f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 393673f53c4aSTejun Heo break; 393773f53c4aSTejun Heo list_del_init(&next->list); 393873f53c4aSTejun Heo complete(&next->done); 393973f53c4aSTejun Heo } 394073f53c4aSTejun Heo 39416183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 394273f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 394373f53c4aSTejun Heo 394473f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 394573f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 394673f53c4aSTejun Heo 394773f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 394873f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 394973f53c4aSTejun Heo /* 395073f53c4aSTejun Heo * Assign the same color to all overflowed 395173f53c4aSTejun Heo * flushers, advance work_color and append to 395273f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 395373f53c4aSTejun Heo * phase for these overflowed flushers. 395473f53c4aSTejun Heo */ 395573f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 395673f53c4aSTejun Heo tmp->flush_color = wq->work_color; 395773f53c4aSTejun Heo 395873f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 395973f53c4aSTejun Heo 396073f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 396173f53c4aSTejun Heo &wq->flusher_queue); 3962112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 396373f53c4aSTejun Heo } 396473f53c4aSTejun Heo 396573f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 39666183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 396773f53c4aSTejun Heo break; 396873f53c4aSTejun Heo } 396973f53c4aSTejun Heo 397073f53c4aSTejun Heo /* 397173f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3972112202d9STejun Heo * the new first flusher and arm pwqs. 397373f53c4aSTejun Heo */ 39746183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 39756183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 397673f53c4aSTejun Heo 397773f53c4aSTejun Heo list_del_init(&next->list); 397873f53c4aSTejun Heo wq->first_flusher = next; 397973f53c4aSTejun Heo 3980112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 398173f53c4aSTejun Heo break; 398273f53c4aSTejun Heo 398373f53c4aSTejun Heo /* 398473f53c4aSTejun Heo * Meh... this color is already done, clear first 398573f53c4aSTejun Heo * flusher and repeat cascading. 398673f53c4aSTejun Heo */ 398773f53c4aSTejun Heo wq->first_flusher = NULL; 398873f53c4aSTejun Heo } 398973f53c4aSTejun Heo 399073f53c4aSTejun Heo out_unlock: 39913c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 39921da177e4SLinus Torvalds } 3993c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 39941da177e4SLinus Torvalds 39959c5a2ba7STejun Heo /** 39969c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 39979c5a2ba7STejun Heo * @wq: workqueue to drain 39989c5a2ba7STejun Heo * 39999c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 40009c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 40019c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 4002b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 40039c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 40049c5a2ba7STejun Heo * takes too long. 40059c5a2ba7STejun Heo */ 40069c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 40079c5a2ba7STejun Heo { 40089c5a2ba7STejun Heo unsigned int flush_cnt = 0; 400949e3cf44STejun Heo struct pool_workqueue *pwq; 40109c5a2ba7STejun Heo 40119c5a2ba7STejun Heo /* 40129c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 40139c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 4014618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 40159c5a2ba7STejun Heo */ 401687fc741eSLai Jiangshan mutex_lock(&wq->mutex); 40179c5a2ba7STejun Heo if (!wq->nr_drainers++) 4018618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 401987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 40209c5a2ba7STejun Heo reflush: 4021c4f135d6STetsuo Handa __flush_workqueue(wq); 40229c5a2ba7STejun Heo 4023b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 402476af4d93STejun Heo 402549e3cf44STejun Heo for_each_pwq(pwq, wq) { 4026fa2563e4SThomas Tuttle bool drained; 40279c5a2ba7STejun Heo 4028a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4029afa87ce8STejun Heo drained = pwq_is_empty(pwq); 4030a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4031fa2563e4SThomas Tuttle 4032fa2563e4SThomas Tuttle if (drained) 40339c5a2ba7STejun Heo continue; 40349c5a2ba7STejun Heo 40359c5a2ba7STejun Heo if (++flush_cnt == 10 || 40369c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 4037e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 4038e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 403976af4d93STejun Heo 4040b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40419c5a2ba7STejun Heo goto reflush; 40429c5a2ba7STejun Heo } 40439c5a2ba7STejun Heo 40449c5a2ba7STejun Heo if (!--wq->nr_drainers) 4045618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 404687fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 40479c5a2ba7STejun Heo } 40489c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 40499c5a2ba7STejun Heo 4050d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 4051d6e89786SJohannes Berg bool from_cancel) 4052baf59022STejun Heo { 4053baf59022STejun Heo struct worker *worker = NULL; 4054c9e7cf27STejun Heo struct worker_pool *pool; 4055112202d9STejun Heo struct pool_workqueue *pwq; 4056c35aea39STejun Heo struct workqueue_struct *wq; 4057baf59022STejun Heo 4058baf59022STejun Heo might_sleep(); 4059baf59022STejun Heo 406024acfb71SThomas Gleixner rcu_read_lock(); 4061fa1b54e6STejun Heo pool = get_work_pool(work); 4062fa1b54e6STejun Heo if (!pool) { 406324acfb71SThomas Gleixner rcu_read_unlock(); 4064fa1b54e6STejun Heo return false; 4065fa1b54e6STejun Heo } 4066fa1b54e6STejun Heo 4067a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 40680b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 4069112202d9STejun Heo pwq = get_work_pwq(work); 4070112202d9STejun Heo if (pwq) { 4071112202d9STejun Heo if (unlikely(pwq->pool != pool)) 4072baf59022STejun Heo goto already_gone; 4073606a5020STejun Heo } else { 4074c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 4075baf59022STejun Heo if (!worker) 4076baf59022STejun Heo goto already_gone; 4077112202d9STejun Heo pwq = worker->current_pwq; 4078606a5020STejun Heo } 4079baf59022STejun Heo 4080c35aea39STejun Heo wq = pwq->wq; 4081c35aea39STejun Heo check_flush_dependency(wq, work); 4082fca839c0STejun Heo 4083112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 4084a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4085baf59022STejun Heo 4086c35aea39STejun Heo touch_work_lockdep_map(work, wq); 4087c35aea39STejun Heo 4088e159489bSTejun Heo /* 4089a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 4090a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 4091a1d14934SPeter Zijlstra * 4092a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 4093a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 4094a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 4095a1d14934SPeter Zijlstra * forward progress. 4096e159489bSTejun Heo */ 4097c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 4098c35aea39STejun Heo touch_wq_lockdep_map(wq); 4099c35aea39STejun Heo 410024acfb71SThomas Gleixner rcu_read_unlock(); 4101baf59022STejun Heo return true; 4102baf59022STejun Heo already_gone: 4103a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 410424acfb71SThomas Gleixner rcu_read_unlock(); 4105baf59022STejun Heo return false; 4106baf59022STejun Heo } 4107baf59022STejun Heo 4108d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4109d6e89786SJohannes Berg { 4110d6e89786SJohannes Berg struct wq_barrier barr; 4111d6e89786SJohannes Berg 4112d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4113d6e89786SJohannes Berg return false; 4114d6e89786SJohannes Berg 41154d43d395STetsuo Handa if (WARN_ON(!work->func)) 41164d43d395STetsuo Handa return false; 41174d43d395STetsuo Handa 4118d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4119d6e89786SJohannes Berg wait_for_completion(&barr.done); 4120d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4121d6e89786SJohannes Berg return true; 4122d6e89786SJohannes Berg } else { 4123d6e89786SJohannes Berg return false; 4124d6e89786SJohannes Berg } 4125d6e89786SJohannes Berg } 4126d6e89786SJohannes Berg 4127db700897SOleg Nesterov /** 4128401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4129401a8d04STejun Heo * @work: the work to flush 4130db700897SOleg Nesterov * 4131606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4132606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4133401a8d04STejun Heo * 4134d185af30SYacine Belkadi * Return: 4135401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4136401a8d04STejun Heo * %false if it was already idle. 4137db700897SOleg Nesterov */ 4138401a8d04STejun Heo bool flush_work(struct work_struct *work) 4139db700897SOleg Nesterov { 4140d6e89786SJohannes Berg return __flush_work(work, false); 4141606a5020STejun Heo } 4142db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4143db700897SOleg Nesterov 4144cdc6e4b3STejun Heo /** 4145cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4146cdc6e4b3STejun Heo * @dwork: the delayed work to flush 4147cdc6e4b3STejun Heo * 4148cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for 4149cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only 4150cdc6e4b3STejun Heo * considers the last queueing instance of @dwork. 4151cdc6e4b3STejun Heo * 4152cdc6e4b3STejun Heo * Return: 4153cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution, 4154cdc6e4b3STejun Heo * %false if it was already idle. 4155cdc6e4b3STejun Heo */ 4156cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4157cdc6e4b3STejun Heo { 4158cdc6e4b3STejun Heo local_irq_disable(); 4159cdc6e4b3STejun Heo if (del_timer_sync(&dwork->timer)) 4160cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work); 4161cdc6e4b3STejun Heo local_irq_enable(); 4162cdc6e4b3STejun Heo return flush_work(&dwork->work); 4163cdc6e4b3STejun Heo } 4164cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4165cdc6e4b3STejun Heo 4166cdc6e4b3STejun Heo /** 4167cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 4168cdc6e4b3STejun Heo * @rwork: the rcu work to flush 4169cdc6e4b3STejun Heo * 4170cdc6e4b3STejun Heo * Return: 4171cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 4172cdc6e4b3STejun Heo * %false if it was already idle. 4173cdc6e4b3STejun Heo */ 4174cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork) 4175cdc6e4b3STejun Heo { 4176cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 4177cdc6e4b3STejun Heo rcu_barrier(); 4178cdc6e4b3STejun Heo flush_work(&rwork->work); 4179cdc6e4b3STejun Heo return true; 4180cdc6e4b3STejun Heo } else { 4181cdc6e4b3STejun Heo return flush_work(&rwork->work); 4182cdc6e4b3STejun Heo } 4183cdc6e4b3STejun Heo } 4184cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work); 4185cdc6e4b3STejun Heo 4186c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags) 4187cdc6e4b3STejun Heo { 4188c26e2f2eSTejun Heo unsigned long irq_flags; 4189cdc6e4b3STejun Heo int ret; 4190cdc6e4b3STejun Heo 4191cdc6e4b3STejun Heo do { 4192c5f5b942STejun Heo ret = try_to_grab_pending(work, cflags, &irq_flags); 4193cdc6e4b3STejun Heo } while (unlikely(ret == -EAGAIN)); 4194cdc6e4b3STejun Heo 4195cdc6e4b3STejun Heo if (unlikely(ret < 0)) 4196cdc6e4b3STejun Heo return false; 4197cdc6e4b3STejun Heo 4198cdc6e4b3STejun Heo set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 4199c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4200cdc6e4b3STejun Heo return ret; 4201cdc6e4b3STejun Heo } 4202cdc6e4b3STejun Heo 4203c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags) 4204401a8d04STejun Heo { 4205c26e2f2eSTejun Heo unsigned long irq_flags; 4206*978b8409STejun Heo bool ret; 42071f1f642eSOleg Nesterov 4208*978b8409STejun Heo /* claim @work and tell other tasks trying to grab @work to back off */ 4209*978b8409STejun Heo ret = work_grab_pending(work, cflags, &irq_flags); 4210bbb68dfaSTejun Heo mark_work_canceling(work); 4211c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4212bbb68dfaSTejun Heo 42133347fa09STejun Heo /* 4214c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't 4215c7a40c49STejun Heo * executing. This allows canceling during early boot. 42163347fa09STejun Heo */ 42173347fa09STejun Heo if (wq_online) 4218d6e89786SJohannes Berg __flush_work(work, true); 42193347fa09STejun Heo 42207a22ad75STejun Heo clear_work_data(work); 42218603e1b3STejun Heo 42228603e1b3STejun Heo /* 42238603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 42248603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 42258603e1b3STejun Heo * visible there. 42268603e1b3STejun Heo */ 42278603e1b3STejun Heo smp_mb(); 4228*978b8409STejun Heo if (waitqueue_active(&wq_cancel_waitq)) 4229*978b8409STejun Heo __wake_up(&wq_cancel_waitq, TASK_NORMAL, 1, work); 42308603e1b3STejun Heo 42311f1f642eSOleg Nesterov return ret; 42321f1f642eSOleg Nesterov } 42331f1f642eSOleg Nesterov 4234cdc6e4b3STejun Heo /* 4235cdc6e4b3STejun Heo * See cancel_delayed_work() 4236cdc6e4b3STejun Heo */ 4237cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work) 4238cdc6e4b3STejun Heo { 4239c5f5b942STejun Heo return __cancel_work(work, 0); 4240cdc6e4b3STejun Heo } 4241cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work); 4242cdc6e4b3STejun Heo 42436e84d644SOleg Nesterov /** 4244401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4245401a8d04STejun Heo * @work: the work to cancel 42466e84d644SOleg Nesterov * 4247401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4248401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4249401a8d04STejun Heo * another workqueue. On return from this function, @work is 4250401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 42511f1f642eSOleg Nesterov * 4252401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4253401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 42546e84d644SOleg Nesterov * 4255401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 42566e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4257401a8d04STejun Heo * 4258d185af30SYacine Belkadi * Return: 4259401a8d04STejun Heo * %true if @work was pending, %false otherwise. 42606e84d644SOleg Nesterov */ 4261401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 42626e84d644SOleg Nesterov { 4263c5f5b942STejun Heo return __cancel_work_sync(work, 0); 4264b89deed3SOleg Nesterov } 426528e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4266b89deed3SOleg Nesterov 42676e84d644SOleg Nesterov /** 426857b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 426957b30ae7STejun Heo * @dwork: delayed_work to cancel 427009383498STejun Heo * 4271d185af30SYacine Belkadi * Kill off a pending delayed_work. 4272d185af30SYacine Belkadi * 4273d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4274d185af30SYacine Belkadi * pending. 4275d185af30SYacine Belkadi * 4276d185af30SYacine Belkadi * Note: 4277d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4278d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4279d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 428009383498STejun Heo * 428157b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 428209383498STejun Heo */ 428357b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 428409383498STejun Heo { 4285c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED); 428609383498STejun Heo } 428757b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 428809383498STejun Heo 428909383498STejun Heo /** 4290401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4291401a8d04STejun Heo * @dwork: the delayed work cancel 4292401a8d04STejun Heo * 4293401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4294401a8d04STejun Heo * 4295d185af30SYacine Belkadi * Return: 4296401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4297401a8d04STejun Heo */ 4298401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 42996e84d644SOleg Nesterov { 4300c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED); 43016e84d644SOleg Nesterov } 4302f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 43031da177e4SLinus Torvalds 43040fcb78c2SRolf Eike Beer /** 430531ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4306b6136773SAndrew Morton * @func: the function to call 4307b6136773SAndrew Morton * 430831ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 430931ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4310b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 431131ddd871STejun Heo * 4312d185af30SYacine Belkadi * Return: 431331ddd871STejun Heo * 0 on success, -errno on failure. 4314b6136773SAndrew Morton */ 431565f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 431615316ba8SChristoph Lameter { 431715316ba8SChristoph Lameter int cpu; 431838f51568SNamhyung Kim struct work_struct __percpu *works; 431915316ba8SChristoph Lameter 4320b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4321b6136773SAndrew Morton if (!works) 432215316ba8SChristoph Lameter return -ENOMEM; 4323b6136773SAndrew Morton 4324ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 432593981800STejun Heo 432615316ba8SChristoph Lameter for_each_online_cpu(cpu) { 43279bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 43289bfb1839SIngo Molnar 43299bfb1839SIngo Molnar INIT_WORK(work, func); 43308de6d308SOleg Nesterov schedule_work_on(cpu, work); 433115316ba8SChristoph Lameter } 433293981800STejun Heo 433393981800STejun Heo for_each_online_cpu(cpu) 43348616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 433593981800STejun Heo 4336ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4337b6136773SAndrew Morton free_percpu(works); 433815316ba8SChristoph Lameter return 0; 433915316ba8SChristoph Lameter } 434015316ba8SChristoph Lameter 4341eef6a7d5SAlan Stern /** 43421fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 43431fa44ecaSJames Bottomley * @fn: the function to execute 43441fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 43451fa44ecaSJames Bottomley * be available when the work executes) 43461fa44ecaSJames Bottomley * 43471fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 43481fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 43491fa44ecaSJames Bottomley * 4350d185af30SYacine Belkadi * Return: 0 - function was executed 43511fa44ecaSJames Bottomley * 1 - function was scheduled for execution 43521fa44ecaSJames Bottomley */ 435365f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 43541fa44ecaSJames Bottomley { 43551fa44ecaSJames Bottomley if (!in_interrupt()) { 435665f27f38SDavid Howells fn(&ew->work); 43571fa44ecaSJames Bottomley return 0; 43581fa44ecaSJames Bottomley } 43591fa44ecaSJames Bottomley 436065f27f38SDavid Howells INIT_WORK(&ew->work, fn); 43611fa44ecaSJames Bottomley schedule_work(&ew->work); 43621fa44ecaSJames Bottomley 43631fa44ecaSJames Bottomley return 1; 43641fa44ecaSJames Bottomley } 43651fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 43661fa44ecaSJames Bottomley 43677a4e344cSTejun Heo /** 43687a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 43697a4e344cSTejun Heo * @attrs: workqueue_attrs to free 43707a4e344cSTejun Heo * 43717a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 43727a4e344cSTejun Heo */ 4373513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 43747a4e344cSTejun Heo { 43757a4e344cSTejun Heo if (attrs) { 43767a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 43779546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 43787a4e344cSTejun Heo kfree(attrs); 43797a4e344cSTejun Heo } 43807a4e344cSTejun Heo } 43817a4e344cSTejun Heo 43827a4e344cSTejun Heo /** 43837a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 43847a4e344cSTejun Heo * 43857a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4386d185af30SYacine Belkadi * return it. 4387d185af30SYacine Belkadi * 4388d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 43897a4e344cSTejun Heo */ 4390513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 43917a4e344cSTejun Heo { 43927a4e344cSTejun Heo struct workqueue_attrs *attrs; 43937a4e344cSTejun Heo 4394be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 43957a4e344cSTejun Heo if (!attrs) 43967a4e344cSTejun Heo goto fail; 4397be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 43987a4e344cSTejun Heo goto fail; 43999546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 44009546b29eSTejun Heo goto fail; 44017a4e344cSTejun Heo 440213e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4403523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 44047a4e344cSTejun Heo return attrs; 44057a4e344cSTejun Heo fail: 44067a4e344cSTejun Heo free_workqueue_attrs(attrs); 44077a4e344cSTejun Heo return NULL; 44087a4e344cSTejun Heo } 44097a4e344cSTejun Heo 441029c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 441129c91e99STejun Heo const struct workqueue_attrs *from) 441229c91e99STejun Heo { 441329c91e99STejun Heo to->nice = from->nice; 441429c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 44159546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 44168639ecebSTejun Heo to->affn_strict = from->affn_strict; 441784193c07STejun Heo 44182865a8fbSShaohua Li /* 441984193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 442084193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 442184193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 44222865a8fbSShaohua Li */ 442384193c07STejun Heo to->affn_scope = from->affn_scope; 4424af73f5c9STejun Heo to->ordered = from->ordered; 442529c91e99STejun Heo } 442629c91e99STejun Heo 44275de7a03cSTejun Heo /* 44285de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 44295de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 44305de7a03cSTejun Heo */ 44315de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 44325de7a03cSTejun Heo { 443384193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 44345de7a03cSTejun Heo attrs->ordered = false; 44355de7a03cSTejun Heo } 44365de7a03cSTejun Heo 443729c91e99STejun Heo /* hash value of the content of @attr */ 443829c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 443929c91e99STejun Heo { 444029c91e99STejun Heo u32 hash = 0; 444129c91e99STejun Heo 444229c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 444313e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 444413e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 44459546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 44469546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 44478639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 444829c91e99STejun Heo return hash; 444929c91e99STejun Heo } 445029c91e99STejun Heo 445129c91e99STejun Heo /* content equality test */ 445229c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 445329c91e99STejun Heo const struct workqueue_attrs *b) 445429c91e99STejun Heo { 445529c91e99STejun Heo if (a->nice != b->nice) 445629c91e99STejun Heo return false; 445729c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 445829c91e99STejun Heo return false; 44599546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 44609546b29eSTejun Heo return false; 44618639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 44628639ecebSTejun Heo return false; 446329c91e99STejun Heo return true; 446429c91e99STejun Heo } 446529c91e99STejun Heo 44660f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 44670f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 44680f36ee24STejun Heo const cpumask_t *unbound_cpumask) 44690f36ee24STejun Heo { 44700f36ee24STejun Heo /* 44710f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 44720f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 44730f36ee24STejun Heo * @unbound_cpumask. 44740f36ee24STejun Heo */ 44750f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 44760f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 44770f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 44780f36ee24STejun Heo } 44790f36ee24STejun Heo 448084193c07STejun Heo /* find wq_pod_type to use for @attrs */ 448184193c07STejun Heo static const struct wq_pod_type * 448284193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 448384193c07STejun Heo { 4484523a301eSTejun Heo enum wq_affn_scope scope; 4485523a301eSTejun Heo struct wq_pod_type *pt; 4486523a301eSTejun Heo 4487523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4488523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4489523a301eSTejun Heo 4490523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4491523a301eSTejun Heo scope = wq_affn_dfl; 4492523a301eSTejun Heo else 4493523a301eSTejun Heo scope = attrs->affn_scope; 4494523a301eSTejun Heo 4495523a301eSTejun Heo pt = &wq_pod_types[scope]; 449684193c07STejun Heo 449784193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 449884193c07STejun Heo likely(pt->nr_pods)) 449984193c07STejun Heo return pt; 450084193c07STejun Heo 450184193c07STejun Heo /* 450284193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 450384193c07STejun Heo * initialized in workqueue_init_early(). 450484193c07STejun Heo */ 450584193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 450684193c07STejun Heo BUG_ON(!pt->nr_pods); 450784193c07STejun Heo return pt; 450884193c07STejun Heo } 450984193c07STejun Heo 45107a4e344cSTejun Heo /** 45117a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 45127a4e344cSTejun Heo * @pool: worker_pool to initialize 45137a4e344cSTejun Heo * 4514402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4515d185af30SYacine Belkadi * 4516d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 451729c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 451829c91e99STejun Heo * on @pool safely to release it. 45197a4e344cSTejun Heo */ 45207a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 45214e1a1f9aSTejun Heo { 4522a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 452329c91e99STejun Heo pool->id = -1; 452429c91e99STejun Heo pool->cpu = -1; 4525f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 45264e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 452782607adcSTejun Heo pool->watchdog_ts = jiffies; 45284e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 45294e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 45304e1a1f9aSTejun Heo hash_init(pool->busy_hash); 45314e1a1f9aSTejun Heo 453232a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 45333f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 45344e1a1f9aSTejun Heo 453532a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 45364e1a1f9aSTejun Heo 4537da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4538e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 45397a4e344cSTejun Heo 45407cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 454129c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 454229c91e99STejun Heo pool->refcnt = 1; 454329c91e99STejun Heo 454429c91e99STejun Heo /* shouldn't fail above this point */ 4545be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 45467a4e344cSTejun Heo if (!pool->attrs) 45477a4e344cSTejun Heo return -ENOMEM; 45485de7a03cSTejun Heo 45495de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 45505de7a03cSTejun Heo 45517a4e344cSTejun Heo return 0; 45524e1a1f9aSTejun Heo } 45534e1a1f9aSTejun Heo 4554669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4555669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4556669de8bdSBart Van Assche { 4557669de8bdSBart Van Assche char *lock_name; 4558669de8bdSBart Van Assche 4559669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4560669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4561669de8bdSBart Van Assche if (!lock_name) 4562669de8bdSBart Van Assche lock_name = wq->name; 456369a106c0SQian Cai 456469a106c0SQian Cai wq->lock_name = lock_name; 4565669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4566669de8bdSBart Van Assche } 4567669de8bdSBart Van Assche 4568669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4569669de8bdSBart Van Assche { 4570669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4571669de8bdSBart Van Assche } 4572669de8bdSBart Van Assche 4573669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4574669de8bdSBart Van Assche { 4575669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4576669de8bdSBart Van Assche kfree(wq->lock_name); 4577669de8bdSBart Van Assche } 4578669de8bdSBart Van Assche #else 4579669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4580669de8bdSBart Van Assche { 4581669de8bdSBart Van Assche } 4582669de8bdSBart Van Assche 4583669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4584669de8bdSBart Van Assche { 4585669de8bdSBart Van Assche } 4586669de8bdSBart Van Assche 4587669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4588669de8bdSBart Van Assche { 4589669de8bdSBart Van Assche } 4590669de8bdSBart Van Assche #endif 4591669de8bdSBart Van Assche 459291ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 459391ccc6e7STejun Heo { 459491ccc6e7STejun Heo int node; 459591ccc6e7STejun Heo 459691ccc6e7STejun Heo for_each_node(node) { 459791ccc6e7STejun Heo kfree(nna_ar[node]); 459891ccc6e7STejun Heo nna_ar[node] = NULL; 459991ccc6e7STejun Heo } 460091ccc6e7STejun Heo 460191ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 460291ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 460391ccc6e7STejun Heo } 460491ccc6e7STejun Heo 460591ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 460691ccc6e7STejun Heo { 4607c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 460891ccc6e7STejun Heo atomic_set(&nna->nr, 0); 46095797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 46105797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 461191ccc6e7STejun Heo } 461291ccc6e7STejun Heo 461391ccc6e7STejun Heo /* 461491ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 461591ccc6e7STejun Heo * should be allocated in the node. 461691ccc6e7STejun Heo */ 461791ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 461891ccc6e7STejun Heo { 461991ccc6e7STejun Heo struct wq_node_nr_active *nna; 462091ccc6e7STejun Heo int node; 462191ccc6e7STejun Heo 462291ccc6e7STejun Heo for_each_node(node) { 462391ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 462491ccc6e7STejun Heo if (!nna) 462591ccc6e7STejun Heo goto err_free; 462691ccc6e7STejun Heo init_node_nr_active(nna); 462791ccc6e7STejun Heo nna_ar[node] = nna; 462891ccc6e7STejun Heo } 462991ccc6e7STejun Heo 463091ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 463191ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 463291ccc6e7STejun Heo if (!nna) 463391ccc6e7STejun Heo goto err_free; 463491ccc6e7STejun Heo init_node_nr_active(nna); 463591ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 463691ccc6e7STejun Heo 463791ccc6e7STejun Heo return 0; 463891ccc6e7STejun Heo 463991ccc6e7STejun Heo err_free: 464091ccc6e7STejun Heo free_node_nr_active(nna_ar); 464191ccc6e7STejun Heo return -ENOMEM; 464291ccc6e7STejun Heo } 464391ccc6e7STejun Heo 4644e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4645e2dca7adSTejun Heo { 4646e2dca7adSTejun Heo struct workqueue_struct *wq = 4647e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4648e2dca7adSTejun Heo 464991ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 465091ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 465191ccc6e7STejun Heo 4652669de8bdSBart Van Assche wq_free_lockdep(wq); 4653ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4654e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4655e2dca7adSTejun Heo kfree(wq); 4656e2dca7adSTejun Heo } 4657e2dca7adSTejun Heo 465829c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 465929c91e99STejun Heo { 466029c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 466129c91e99STejun Heo 46627cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 466329c91e99STejun Heo free_workqueue_attrs(pool->attrs); 466429c91e99STejun Heo kfree(pool); 466529c91e99STejun Heo } 466629c91e99STejun Heo 466729c91e99STejun Heo /** 466829c91e99STejun Heo * put_unbound_pool - put a worker_pool 466929c91e99STejun Heo * @pool: worker_pool to put 467029c91e99STejun Heo * 467124acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4672c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4673c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4674c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4675a892caccSTejun Heo * 4676a892caccSTejun Heo * Should be called with wq_pool_mutex held. 467729c91e99STejun Heo */ 467829c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 467929c91e99STejun Heo { 468060f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 468129c91e99STejun Heo struct worker *worker; 46829680540cSYang Yingliang LIST_HEAD(cull_list); 4683e02b9312SValentin Schneider 4684a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4685a892caccSTejun Heo 4686a892caccSTejun Heo if (--pool->refcnt) 468729c91e99STejun Heo return; 468829c91e99STejun Heo 468929c91e99STejun Heo /* sanity checks */ 469061d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4691a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 469229c91e99STejun Heo return; 469329c91e99STejun Heo 469429c91e99STejun Heo /* release id and unhash */ 469529c91e99STejun Heo if (pool->id >= 0) 469629c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 469729c91e99STejun Heo hash_del(&pool->hash_node); 469829c91e99STejun Heo 4699c5aa87bbSTejun Heo /* 4700692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4701692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4702692b4825STejun Heo * manager and @pool gets freed with the flag set. 47039ab03be4SValentin Schneider * 47049ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 47059ab03be4SValentin Schneider * only get here with 47069ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 47079ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 47089ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 47099ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 47109ab03be4SValentin Schneider * drops pool->lock 4711c5aa87bbSTejun Heo */ 47129ab03be4SValentin Schneider while (true) { 47139ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 47149ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4715d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4716e02b9312SValentin Schneider 4717e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 47189ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 47199ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4720692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 47219ab03be4SValentin Schneider break; 47229ab03be4SValentin Schneider } 47239ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4724e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 47259ab03be4SValentin Schneider } 4726692b4825STejun Heo 47271037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4728e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 472929c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4730a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 473160f5a4bcSLai Jiangshan 4732e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4733e02b9312SValentin Schneider 4734e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 473560f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 47361258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 473760f5a4bcSLai Jiangshan 473860f5a4bcSLai Jiangshan if (pool->detach_completion) 473960f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 474060f5a4bcSLai Jiangshan 474129c91e99STejun Heo /* shut down the timers */ 474229c91e99STejun Heo del_timer_sync(&pool->idle_timer); 47433f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 474429c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 474529c91e99STejun Heo 474624acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 474725b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 474829c91e99STejun Heo } 474929c91e99STejun Heo 475029c91e99STejun Heo /** 475129c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 475229c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 475329c91e99STejun Heo * 475429c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 475529c91e99STejun Heo * reference count and return it. If there already is a matching 475629c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4757d185af30SYacine Belkadi * create a new one. 4758a892caccSTejun Heo * 4759a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4760d185af30SYacine Belkadi * 4761d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4762d185af30SYacine Belkadi * On failure, %NULL. 476329c91e99STejun Heo */ 476429c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 476529c91e99STejun Heo { 476684193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 476729c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 476829c91e99STejun Heo struct worker_pool *pool; 476984193c07STejun Heo int pod, node = NUMA_NO_NODE; 477029c91e99STejun Heo 4771a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 477229c91e99STejun Heo 477329c91e99STejun Heo /* do we already have a matching pool? */ 477429c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 477529c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 477629c91e99STejun Heo pool->refcnt++; 47773fb1823cSLai Jiangshan return pool; 477829c91e99STejun Heo } 477929c91e99STejun Heo } 478029c91e99STejun Heo 47819546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 478284193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 47839546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 478484193c07STejun Heo node = pt->pod_node[pod]; 4785e2273584SXunlei Pang break; 4786e2273584SXunlei Pang } 4787e2273584SXunlei Pang } 4788e2273584SXunlei Pang 478929c91e99STejun Heo /* nope, create a new one */ 479084193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 479129c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 479229c91e99STejun Heo goto fail; 479329c91e99STejun Heo 479484193c07STejun Heo pool->node = node; 47955de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 47965de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 47972865a8fbSShaohua Li 479829c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 479929c91e99STejun Heo goto fail; 480029c91e99STejun Heo 480129c91e99STejun Heo /* create and start the initial worker */ 48023347fa09STejun Heo if (wq_online && !create_worker(pool)) 480329c91e99STejun Heo goto fail; 480429c91e99STejun Heo 480529c91e99STejun Heo /* install */ 480629c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 48073fb1823cSLai Jiangshan 480829c91e99STejun Heo return pool; 480929c91e99STejun Heo fail: 481029c91e99STejun Heo if (pool) 481129c91e99STejun Heo put_unbound_pool(pool); 481229c91e99STejun Heo return NULL; 481329c91e99STejun Heo } 481429c91e99STejun Heo 48158864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 48168864b4e5STejun Heo { 48178864b4e5STejun Heo kmem_cache_free(pwq_cache, 48188864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 48198864b4e5STejun Heo } 48208864b4e5STejun Heo 48218864b4e5STejun Heo /* 4822967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4823967b494eSTejun Heo * refcnt and needs to be destroyed. 48248864b4e5STejun Heo */ 4825687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 48268864b4e5STejun Heo { 48278864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4828687a9aa5STejun Heo release_work); 48298864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 48308864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4831b42b0bddSYang Yingliang bool is_last = false; 48328864b4e5STejun Heo 4833b42b0bddSYang Yingliang /* 4834687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4835b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4836b42b0bddSYang Yingliang */ 4837b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 48383c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 48398864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4840bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 48414c065dbcSWaiman Long 48424c065dbcSWaiman Long /* 48434c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 48444c065dbcSWaiman Long */ 48454c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 48464c065dbcSWaiman Long unplug_oldest_pwq(wq); 48474c065dbcSWaiman Long 48483c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4849b42b0bddSYang Yingliang } 48508864b4e5STejun Heo 4851687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4852a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 48538864b4e5STejun Heo put_unbound_pool(pool); 4854a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4855687a9aa5STejun Heo } 4856a892caccSTejun Heo 48575797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 48585797b1c1STejun Heo struct wq_node_nr_active *nna = 48595797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 48605797b1c1STejun Heo 48615797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 48625797b1c1STejun Heo list_del_init(&pwq->pending_node); 48635797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 48645797b1c1STejun Heo } 48655797b1c1STejun Heo 486625b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 48678864b4e5STejun Heo 48688864b4e5STejun Heo /* 48698864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4870e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 48718864b4e5STejun Heo */ 4872669de8bdSBart Van Assche if (is_last) { 4873669de8bdSBart Van Assche wq_unregister_lockdep(wq); 487425b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 48756029a918STejun Heo } 4876669de8bdSBart Van Assche } 48778864b4e5STejun Heo 487867dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4879f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4880f147f29eSTejun Heo struct worker_pool *pool) 4881d2c1d404STejun Heo { 4882e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK); 4883d2c1d404STejun Heo 4884e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4885e50aba9aSTejun Heo 4886d2c1d404STejun Heo pwq->pool = pool; 4887d2c1d404STejun Heo pwq->wq = wq; 4888d2c1d404STejun Heo pwq->flush_color = -1; 48898864b4e5STejun Heo pwq->refcnt = 1; 4890f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 48915797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 48921befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4893d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4894687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4895f147f29eSTejun Heo } 4896d2c1d404STejun Heo 4897f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 48981befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4899f147f29eSTejun Heo { 4900f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4901f147f29eSTejun Heo 4902f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 490375ccf595STejun Heo 49041befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 49051befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 49061befcf30STejun Heo return; 49071befcf30STejun Heo 490829b1cb41SLai Jiangshan /* set the matching work_color */ 490975ccf595STejun Heo pwq->work_color = wq->work_color; 4910983ca25eSTejun Heo 4911983ca25eSTejun Heo /* link in @pwq */ 491226fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 4913df2d5ae4STejun Heo } 49146029a918STejun Heo 4915f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4916f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4917f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4918f147f29eSTejun Heo { 4919f147f29eSTejun Heo struct worker_pool *pool; 4920f147f29eSTejun Heo struct pool_workqueue *pwq; 4921f147f29eSTejun Heo 4922f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4923f147f29eSTejun Heo 4924f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4925f147f29eSTejun Heo if (!pool) 4926f147f29eSTejun Heo return NULL; 4927f147f29eSTejun Heo 4928e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4929f147f29eSTejun Heo if (!pwq) { 4930f147f29eSTejun Heo put_unbound_pool(pool); 4931f147f29eSTejun Heo return NULL; 4932f147f29eSTejun Heo } 4933f147f29eSTejun Heo 4934f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4935f147f29eSTejun Heo return pwq; 4936d2c1d404STejun Heo } 4937d2c1d404STejun Heo 49384c16bd32STejun Heo /** 4939fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4940042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 494184193c07STejun Heo * @cpu: the target CPU 49424c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 49434c16bd32STejun Heo * 4944fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4945fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 49469546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 49474c16bd32STejun Heo * 4948fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4949fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4950fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 49514c16bd32STejun Heo * 4952fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 49534c16bd32STejun Heo */ 49549546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 49559546b29eSTejun Heo int cpu_going_down) 49564c16bd32STejun Heo { 495784193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 495884193c07STejun Heo int pod = pt->cpu_pod[cpu]; 49594c16bd32STejun Heo 4960fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 49619546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 49629546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 49634c16bd32STejun Heo if (cpu_going_down >= 0) 49649546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 49654c16bd32STejun Heo 49669546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 49679546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 496884193c07STejun Heo return; 496984193c07STejun Heo } 49704c16bd32STejun Heo 4971fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 49729546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 49731ad0f0a7SMichael Bringmann 49749546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 49751ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 49761ad0f0a7SMichael Bringmann "possible intersect\n"); 49774c16bd32STejun Heo } 49784c16bd32STejun Heo 49799f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4980636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4981636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 49821befcf30STejun Heo { 49839f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 49841befcf30STejun Heo struct pool_workqueue *old_pwq; 49851befcf30STejun Heo 49865b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 49871befcf30STejun Heo lockdep_assert_held(&wq->mutex); 49881befcf30STejun Heo 49891befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 49901befcf30STejun Heo link_pwq(pwq); 49911befcf30STejun Heo 49929f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 49939f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 49941befcf30STejun Heo return old_pwq; 49951befcf30STejun Heo } 49961befcf30STejun Heo 49972d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 49982d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 49992d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 50002d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 5001042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 50022d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 50032d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 50042d5f0764SLai Jiangshan }; 50052d5f0764SLai Jiangshan 50062d5f0764SLai Jiangshan /* free the resources after success or abort */ 50072d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 50082d5f0764SLai Jiangshan { 50092d5f0764SLai Jiangshan if (ctx) { 5010636b927eSTejun Heo int cpu; 50112d5f0764SLai Jiangshan 5012636b927eSTejun Heo for_each_possible_cpu(cpu) 5013636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 50142d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 50152d5f0764SLai Jiangshan 50162d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 50172d5f0764SLai Jiangshan 50182d5f0764SLai Jiangshan kfree(ctx); 50192d5f0764SLai Jiangshan } 50202d5f0764SLai Jiangshan } 50212d5f0764SLai Jiangshan 50222d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 50232d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 50242d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 502599c621efSLai Jiangshan const struct workqueue_attrs *attrs, 502699c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 50272d5f0764SLai Jiangshan { 50282d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 50299546b29eSTejun Heo struct workqueue_attrs *new_attrs; 5030636b927eSTejun Heo int cpu; 50312d5f0764SLai Jiangshan 50322d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 50332d5f0764SLai Jiangshan 503484193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 503584193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 503684193c07STejun Heo return ERR_PTR(-EINVAL); 503784193c07STejun Heo 5038636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 50392d5f0764SLai Jiangshan 5040be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 50419546b29eSTejun Heo if (!ctx || !new_attrs) 50422d5f0764SLai Jiangshan goto out_free; 50432d5f0764SLai Jiangshan 5044042f7df1SLai Jiangshan /* 50452d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 50462d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 50472d5f0764SLai Jiangshan * it even if we don't use it immediately. 50482d5f0764SLai Jiangshan */ 50490f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 50500f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 50519546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50522d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 50532d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 50542d5f0764SLai Jiangshan goto out_free; 50552d5f0764SLai Jiangshan 5056636b927eSTejun Heo for_each_possible_cpu(cpu) { 5057af73f5c9STejun Heo if (new_attrs->ordered) { 50582d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5059636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5060636b927eSTejun Heo } else { 50619546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 50629546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5063636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5064636b927eSTejun Heo goto out_free; 50652d5f0764SLai Jiangshan } 50662d5f0764SLai Jiangshan } 50672d5f0764SLai Jiangshan 5068042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5069042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5070042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 50719546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50722d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5073042f7df1SLai Jiangshan 50744c065dbcSWaiman Long /* 50754c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 50764c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 50774c065dbcSWaiman Long * of newly queued work items until execution of older work items in 50784c065dbcSWaiman Long * the old pwq's have completed. 50794c065dbcSWaiman Long */ 50804c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 50814c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 50824c065dbcSWaiman Long 50832d5f0764SLai Jiangshan ctx->wq = wq; 50842d5f0764SLai Jiangshan return ctx; 50852d5f0764SLai Jiangshan 50862d5f0764SLai Jiangshan out_free: 50872d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 50882d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 508984193c07STejun Heo return ERR_PTR(-ENOMEM); 50902d5f0764SLai Jiangshan } 50912d5f0764SLai Jiangshan 50922d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 50932d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 50942d5f0764SLai Jiangshan { 5095636b927eSTejun Heo int cpu; 50962d5f0764SLai Jiangshan 50972d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 50982d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 50992d5f0764SLai Jiangshan 51002d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 51012d5f0764SLai Jiangshan 51029f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5103636b927eSTejun Heo for_each_possible_cpu(cpu) 5104636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5105636b927eSTejun Heo ctx->pwq_tbl[cpu]); 51069f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 51072d5f0764SLai Jiangshan 51085797b1c1STejun Heo /* update node_nr_active->max */ 51095797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 51105797b1c1STejun Heo 5111d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5112d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5113d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5114d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5115d64f2fa0SJuri Lelli 51162d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 51172d5f0764SLai Jiangshan } 51182d5f0764SLai Jiangshan 5119a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5120a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5121a0111cf6SLai Jiangshan { 5122a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5123a0111cf6SLai Jiangshan 5124a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5125a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5126a0111cf6SLai Jiangshan return -EINVAL; 5127a0111cf6SLai Jiangshan 512899c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 512984193c07STejun Heo if (IS_ERR(ctx)) 513084193c07STejun Heo return PTR_ERR(ctx); 5131a0111cf6SLai Jiangshan 5132a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5133a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5134a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5135a0111cf6SLai Jiangshan 51366201171eSwanghaibin return 0; 5137a0111cf6SLai Jiangshan } 5138a0111cf6SLai Jiangshan 51399e8cd2f5STejun Heo /** 51409e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 51419e8cd2f5STejun Heo * @wq: the target workqueue 51429e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 51439e8cd2f5STejun Heo * 5144fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5145fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5146fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5147fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5148fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 51499e8cd2f5STejun Heo * 5150d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5151d185af30SYacine Belkadi * 5152ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5153509b3204SDaniel Jordan * 5154d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 51559e8cd2f5STejun Heo */ 5156513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 51579e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 51589e8cd2f5STejun Heo { 5159a0111cf6SLai Jiangshan int ret; 51609e8cd2f5STejun Heo 5161509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5162509b3204SDaniel Jordan 5163509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5164a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5165509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 51662d5f0764SLai Jiangshan 51672d5f0764SLai Jiangshan return ret; 51689e8cd2f5STejun Heo } 51699e8cd2f5STejun Heo 51704c16bd32STejun Heo /** 5171fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 51724c16bd32STejun Heo * @wq: the target workqueue 51734cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 51744cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 51754c16bd32STejun Heo * @online: whether @cpu is coming up or going down 51764c16bd32STejun Heo * 51774c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5178fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 51794c16bd32STejun Heo * @wq accordingly. 51804c16bd32STejun Heo * 51814c16bd32STejun Heo * 5182fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5183fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5184fef59c9cSTejun Heo * 5185fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5186fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5187fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5188fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5189fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5190fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 51914c16bd32STejun Heo */ 5192fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 51934cbfd3deSTejun Heo int hotplug_cpu, bool online) 51944c16bd32STejun Heo { 51954cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 51964c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 51974c16bd32STejun Heo struct workqueue_attrs *target_attrs; 51984c16bd32STejun Heo 51994c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 52004c16bd32STejun Heo 520184193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 52024c16bd32STejun Heo return; 52034c16bd32STejun Heo 52044c16bd32STejun Heo /* 52054c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 52064c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 52074c16bd32STejun Heo * CPU hotplug exclusion. 52084c16bd32STejun Heo */ 5209fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 52104c16bd32STejun Heo 52114c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 52120f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 52134c16bd32STejun Heo 5214636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 52159546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 52169f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5217f7142ed4SLai Jiangshan return; 52184c16bd32STejun Heo 52194c16bd32STejun Heo /* create a new pwq */ 52204c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 52214c16bd32STejun Heo if (!pwq) { 5222fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 52234c16bd32STejun Heo wq->name); 522477f300b1SDaeseok Youn goto use_dfl_pwq; 52254c16bd32STejun Heo } 52264c16bd32STejun Heo 5227f7142ed4SLai Jiangshan /* Install the new pwq. */ 52284c16bd32STejun Heo mutex_lock(&wq->mutex); 5229636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 52304c16bd32STejun Heo goto out_unlock; 52314c16bd32STejun Heo 52324c16bd32STejun Heo use_dfl_pwq: 5233f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 52349f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 52359f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 52369f66cff2STejun Heo get_pwq(pwq); 52379f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 52389f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 52394c16bd32STejun Heo out_unlock: 52404c16bd32STejun Heo mutex_unlock(&wq->mutex); 52414c16bd32STejun Heo put_pwq_unlocked(old_pwq); 52424c16bd32STejun Heo } 52434c16bd32STejun Heo 524430cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 52451da177e4SLinus Torvalds { 524649e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 52478a2b7538STejun Heo int cpu, ret; 5248e1d8aa9fSFrederic Weisbecker 5249687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5250ee1ceef7STejun Heo if (!wq->cpu_pwq) 5251687a9aa5STejun Heo goto enomem; 525230cdf249STejun Heo 5253636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 525430cdf249STejun Heo for_each_possible_cpu(cpu) { 52554cb1ef64STejun Heo struct pool_workqueue **pwq_p; 52564cb1ef64STejun Heo struct worker_pool __percpu *pools; 52574cb1ef64STejun Heo struct worker_pool *pool; 52584cb1ef64STejun Heo 52594cb1ef64STejun Heo if (wq->flags & WQ_BH) 52604cb1ef64STejun Heo pools = bh_worker_pools; 52614cb1ef64STejun Heo else 52624cb1ef64STejun Heo pools = cpu_worker_pools; 52634cb1ef64STejun Heo 52644cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 52654cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 526630cdf249STejun Heo 5267687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5268687a9aa5STejun Heo pool->node); 5269687a9aa5STejun Heo if (!*pwq_p) 5270687a9aa5STejun Heo goto enomem; 5271687a9aa5STejun Heo 5272687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5273f147f29eSTejun Heo 5274f147f29eSTejun Heo mutex_lock(&wq->mutex); 5275687a9aa5STejun Heo link_pwq(*pwq_p); 5276f147f29eSTejun Heo mutex_unlock(&wq->mutex); 527730cdf249STejun Heo } 527830cdf249STejun Heo return 0; 5279509b3204SDaniel Jordan } 5280509b3204SDaniel Jordan 5281ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5282509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 52839f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 52849f66cff2STejun Heo 52858a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 52868a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 52879f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 52889f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 52899f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 52908a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 52919e8cd2f5STejun Heo } else { 5292509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 52939e8cd2f5STejun Heo } 5294ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5295509b3204SDaniel Jordan 529664344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 529764344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 529864344553SZqiang */ 529964344553SZqiang if (ret) 530064344553SZqiang kthread_flush_worker(pwq_release_worker); 530164344553SZqiang 5302509b3204SDaniel Jordan return ret; 5303687a9aa5STejun Heo 5304687a9aa5STejun Heo enomem: 5305687a9aa5STejun Heo if (wq->cpu_pwq) { 53067b42f401SZqiang for_each_possible_cpu(cpu) { 53077b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 53087b42f401SZqiang 53097b42f401SZqiang if (pwq) 53107b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 53117b42f401SZqiang } 5312687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5313687a9aa5STejun Heo wq->cpu_pwq = NULL; 5314687a9aa5STejun Heo } 5315687a9aa5STejun Heo return -ENOMEM; 53160f900049STejun Heo } 53170f900049STejun Heo 5318f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5319f3421797STejun Heo const char *name) 5320b71ab8c2STejun Heo { 5321636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5322044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5323636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5324b71ab8c2STejun Heo 5325636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5326b71ab8c2STejun Heo } 5327b71ab8c2STejun Heo 5328983c7515STejun Heo /* 5329983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5330983c7515STejun Heo * to guarantee forward progress. 5331983c7515STejun Heo */ 5332983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5333983c7515STejun Heo { 5334983c7515STejun Heo struct worker *rescuer; 5335b92b36eaSDan Carpenter int ret; 5336983c7515STejun Heo 5337983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5338983c7515STejun Heo return 0; 5339983c7515STejun Heo 5340983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 53414c0736a7SPetr Mladek if (!rescuer) { 53424c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 53434c0736a7SPetr Mladek wq->name); 5344983c7515STejun Heo return -ENOMEM; 53454c0736a7SPetr Mladek } 5346983c7515STejun Heo 5347983c7515STejun Heo rescuer->rescue_wq = wq; 5348b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5349f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5350b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 53514c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 53524c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5353983c7515STejun Heo kfree(rescuer); 5354b92b36eaSDan Carpenter return ret; 5355983c7515STejun Heo } 5356983c7515STejun Heo 5357983c7515STejun Heo wq->rescuer = rescuer; 535885f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 535949584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 536085f0ab43SJuri Lelli else 5361983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5362983c7515STejun Heo wake_up_process(rescuer->task); 5363983c7515STejun Heo 5364983c7515STejun Heo return 0; 5365983c7515STejun Heo } 5366983c7515STejun Heo 5367a045a272STejun Heo /** 5368a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5369a045a272STejun Heo * @wq: target workqueue 5370a045a272STejun Heo * 5371a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5372a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5373a045a272STejun Heo * @wq->max_active to zero. 5374a045a272STejun Heo */ 5375a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5376a045a272STejun Heo { 5377c5404d4eSTejun Heo bool activated; 53785797b1c1STejun Heo int new_max, new_min; 5379a045a272STejun Heo 5380a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5381a045a272STejun Heo 5382a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 53835797b1c1STejun Heo new_max = 0; 53845797b1c1STejun Heo new_min = 0; 53855797b1c1STejun Heo } else { 53865797b1c1STejun Heo new_max = wq->saved_max_active; 53875797b1c1STejun Heo new_min = wq->saved_min_active; 5388a045a272STejun Heo } 5389a045a272STejun Heo 53905797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5391a045a272STejun Heo return; 5392a045a272STejun Heo 5393a045a272STejun Heo /* 53945797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5395a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5396a045a272STejun Heo * because new work items are always queued behind existing inactive 5397a045a272STejun Heo * work items if there are any. 5398a045a272STejun Heo */ 53995797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 54005797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 54015797b1c1STejun Heo 54025797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 54035797b1c1STejun Heo wq_update_node_max_active(wq, -1); 54045797b1c1STejun Heo 54055797b1c1STejun Heo if (new_max == 0) 54065797b1c1STejun Heo return; 5407a045a272STejun Heo 5408c5404d4eSTejun Heo /* 5409c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5410c5404d4eSTejun Heo * until max_active is filled. 5411c5404d4eSTejun Heo */ 5412c5404d4eSTejun Heo do { 5413c5404d4eSTejun Heo struct pool_workqueue *pwq; 5414c5404d4eSTejun Heo 5415c5404d4eSTejun Heo activated = false; 5416a045a272STejun Heo for_each_pwq(pwq, wq) { 5417c26e2f2eSTejun Heo unsigned long irq_flags; 5418a045a272STejun Heo 5419c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5420c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 54215797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5422c5404d4eSTejun Heo activated = true; 5423a045a272STejun Heo kick_pool(pwq->pool); 5424c5404d4eSTejun Heo } 5425c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 5426a045a272STejun Heo } 5427c5404d4eSTejun Heo } while (activated); 5428a045a272STejun Heo } 5429a045a272STejun Heo 5430a2775bbcSMathieu Malaterre __printf(1, 4) 5431669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 543297e37d7bSTejun Heo unsigned int flags, 5433669de8bdSBart Van Assche int max_active, ...) 54343af24433SOleg Nesterov { 5435ecf6881fSTejun Heo va_list args; 54363af24433SOleg Nesterov struct workqueue_struct *wq; 543791ccc6e7STejun Heo size_t wq_size; 543891ccc6e7STejun Heo int name_len; 5439b196be89STejun Heo 54404cb1ef64STejun Heo if (flags & WQ_BH) { 54414cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 54424cb1ef64STejun Heo return NULL; 54434cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 54444cb1ef64STejun Heo return NULL; 54454cb1ef64STejun Heo } 54464cb1ef64STejun Heo 5447cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5448cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5449cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5450cee22a15SViresh Kumar 5451ecf6881fSTejun Heo /* allocate wq and format name */ 545291ccc6e7STejun Heo if (flags & WQ_UNBOUND) 545391ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 545491ccc6e7STejun Heo else 545591ccc6e7STejun Heo wq_size = sizeof(*wq); 545691ccc6e7STejun Heo 545791ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5458b196be89STejun Heo if (!wq) 5459d2c1d404STejun Heo return NULL; 5460b196be89STejun Heo 54616029a918STejun Heo if (flags & WQ_UNBOUND) { 5462be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 54636029a918STejun Heo if (!wq->unbound_attrs) 54646029a918STejun Heo goto err_free_wq; 54656029a918STejun Heo } 54666029a918STejun Heo 5467669de8bdSBart Van Assche va_start(args, max_active); 546891ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5469b196be89STejun Heo va_end(args); 54703af24433SOleg Nesterov 547191ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 547291ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 547391ccc6e7STejun Heo wq->name); 547431c89007SAudra Mitchell 54754cb1ef64STejun Heo if (flags & WQ_BH) { 54764cb1ef64STejun Heo /* 54774cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 54784cb1ef64STejun Heo * and don't impose any max_active limit. 54794cb1ef64STejun Heo */ 54804cb1ef64STejun Heo max_active = INT_MAX; 54814cb1ef64STejun Heo } else { 5482d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5483b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 54844cb1ef64STejun Heo } 54853af24433SOleg Nesterov 5486b196be89STejun Heo /* init wq */ 548797e37d7bSTejun Heo wq->flags = flags; 5488a045a272STejun Heo wq->max_active = max_active; 54895797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 54905797b1c1STejun Heo wq->saved_max_active = wq->max_active; 54915797b1c1STejun Heo wq->saved_min_active = wq->min_active; 54923c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5493112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 549430cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 549573f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 549673f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5497493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 54983af24433SOleg Nesterov 5499669de8bdSBart Van Assche wq_init_lockdep(wq); 5500cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 55013af24433SOleg Nesterov 550291ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 550391ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 550482efcab3SBart Van Assche goto err_unreg_lockdep; 550591ccc6e7STejun Heo } 550691ccc6e7STejun Heo 550791ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 550891ccc6e7STejun Heo goto err_free_node_nr_active; 55091537663fSTejun Heo 551040c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5511d2c1d404STejun Heo goto err_destroy; 5512e22bee78STejun Heo 5513226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5514226223abSTejun Heo goto err_destroy; 5515226223abSTejun Heo 55166af8bf3dSOleg Nesterov /* 551768e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 551868e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 551968e13a67SLai Jiangshan * list. 55206af8bf3dSOleg Nesterov */ 552168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5522a0a1a5fdSTejun Heo 5523a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5524a045a272STejun Heo wq_adjust_max_active(wq); 5525a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5526a0a1a5fdSTejun Heo 5527e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5528a0a1a5fdSTejun Heo 552968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55303af24433SOleg Nesterov 55313af24433SOleg Nesterov return wq; 5532d2c1d404STejun Heo 553391ccc6e7STejun Heo err_free_node_nr_active: 553491ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 553591ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 553682efcab3SBart Van Assche err_unreg_lockdep: 5537009bb421SBart Van Assche wq_unregister_lockdep(wq); 5538009bb421SBart Van Assche wq_free_lockdep(wq); 553982efcab3SBart Van Assche err_free_wq: 55406029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 55414690c4abSTejun Heo kfree(wq); 5542d2c1d404STejun Heo return NULL; 5543d2c1d404STejun Heo err_destroy: 5544d2c1d404STejun Heo destroy_workqueue(wq); 55454690c4abSTejun Heo return NULL; 55461da177e4SLinus Torvalds } 5547669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 55481da177e4SLinus Torvalds 5549c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5550c29eb853STejun Heo { 5551c29eb853STejun Heo int i; 5552c29eb853STejun Heo 5553c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5554c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5555c29eb853STejun Heo return true; 5556c29eb853STejun Heo 55579f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5558c29eb853STejun Heo return true; 5559afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5560c29eb853STejun Heo return true; 5561c29eb853STejun Heo 5562c29eb853STejun Heo return false; 5563c29eb853STejun Heo } 5564c29eb853STejun Heo 55653af24433SOleg Nesterov /** 55663af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 55673af24433SOleg Nesterov * @wq: target workqueue 55683af24433SOleg Nesterov * 55693af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 55703af24433SOleg Nesterov */ 55713af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 55723af24433SOleg Nesterov { 557349e3cf44STejun Heo struct pool_workqueue *pwq; 5574636b927eSTejun Heo int cpu; 55753af24433SOleg Nesterov 5576def98c84STejun Heo /* 5577def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5578def98c84STejun Heo * lead to sysfs name conflicts. 5579def98c84STejun Heo */ 5580def98c84STejun Heo workqueue_sysfs_unregister(wq); 5581def98c84STejun Heo 558233e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 558333e3f0a3SRichard Clark mutex_lock(&wq->mutex); 558433e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 558533e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 558633e3f0a3SRichard Clark 55879c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 55889c5a2ba7STejun Heo drain_workqueue(wq); 5589c8efcc25STejun Heo 5590def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5591def98c84STejun Heo if (wq->rescuer) { 5592def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5593def98c84STejun Heo 5594def98c84STejun Heo /* this prevents new queueing */ 5595a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5596def98c84STejun Heo wq->rescuer = NULL; 5597a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5598def98c84STejun Heo 5599def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5600def98c84STejun Heo kthread_stop(rescuer->task); 56018efe1223STejun Heo kfree(rescuer); 5602def98c84STejun Heo } 5603def98c84STejun Heo 5604c29eb853STejun Heo /* 5605c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5606c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5607c29eb853STejun Heo */ 5608c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5609b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 561049e3cf44STejun Heo for_each_pwq(pwq, wq) { 5611a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5612c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 56131d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5614e66b39afSTejun Heo __func__, wq->name); 5615c29eb853STejun Heo show_pwq(pwq); 5616a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5617b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5618c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 561955df0933SImran Khan show_one_workqueue(wq); 56206183c009STejun Heo return; 562176af4d93STejun Heo } 5622a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 562376af4d93STejun Heo } 5624b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 56256183c009STejun Heo 5626a0a1a5fdSTejun Heo /* 5627a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5628a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5629a0a1a5fdSTejun Heo */ 5630e2dca7adSTejun Heo list_del_rcu(&wq->list); 563168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 56323af24433SOleg Nesterov 56338864b4e5STejun Heo /* 5634636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5635636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5636636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 56378864b4e5STejun Heo */ 5638636b927eSTejun Heo rcu_read_lock(); 5639636b927eSTejun Heo 5640636b927eSTejun Heo for_each_possible_cpu(cpu) { 56419f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 56429f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 56434c16bd32STejun Heo } 56444c16bd32STejun Heo 56459f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 56469f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5647636b927eSTejun Heo 5648636b927eSTejun Heo rcu_read_unlock(); 56493af24433SOleg Nesterov } 56503af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 56513af24433SOleg Nesterov 5652dcd989cbSTejun Heo /** 5653dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5654dcd989cbSTejun Heo * @wq: target workqueue 5655dcd989cbSTejun Heo * @max_active: new max_active value. 5656dcd989cbSTejun Heo * 56575797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 56585797b1c1STejun Heo * comment. 5659dcd989cbSTejun Heo * 5660dcd989cbSTejun Heo * CONTEXT: 5661dcd989cbSTejun Heo * Don't call from IRQ context. 5662dcd989cbSTejun Heo */ 5663dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5664dcd989cbSTejun Heo { 56654cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 56664cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 56674cb1ef64STejun Heo return; 56688719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 56693bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 56708719dceaSTejun Heo return; 56718719dceaSTejun Heo 5672f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5673dcd989cbSTejun Heo 5674a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5675dcd989cbSTejun Heo 5676dcd989cbSTejun Heo wq->saved_max_active = max_active; 56775797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 56785797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 56795797b1c1STejun Heo 5680a045a272STejun Heo wq_adjust_max_active(wq); 5681dcd989cbSTejun Heo 5682a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5683dcd989cbSTejun Heo } 5684dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5685dcd989cbSTejun Heo 5686dcd989cbSTejun Heo /** 56878f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue 56888f172181STejun Heo * @wq: target unbound workqueue 56898f172181STejun Heo * @min_active: new min_active value 56908f172181STejun Heo * 56918f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an 56928f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active 56938f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be 56948f172181STejun Heo * able to process min_active number of interdependent work items which is 56958f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default. 56968f172181STejun Heo * 56978f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current 56988f172181STejun Heo * max_active. 56998f172181STejun Heo */ 57008f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) 57018f172181STejun Heo { 57028f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */ 57038f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != 57048f172181STejun Heo WQ_UNBOUND)) 57058f172181STejun Heo return; 57068f172181STejun Heo 57078f172181STejun Heo mutex_lock(&wq->mutex); 57088f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); 57098f172181STejun Heo wq_adjust_max_active(wq); 57108f172181STejun Heo mutex_unlock(&wq->mutex); 57118f172181STejun Heo } 57128f172181STejun Heo 57138f172181STejun Heo /** 571427d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 571527d4ee03SLukas Wunner * 571627d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 571727d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 571827d4ee03SLukas Wunner * 571927d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 572027d4ee03SLukas Wunner */ 572127d4ee03SLukas Wunner struct work_struct *current_work(void) 572227d4ee03SLukas Wunner { 572327d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 572427d4ee03SLukas Wunner 572527d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 572627d4ee03SLukas Wunner } 572727d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 572827d4ee03SLukas Wunner 572927d4ee03SLukas Wunner /** 5730e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5731e6267616STejun Heo * 5732e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5733e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5734d185af30SYacine Belkadi * 5735d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5736e6267616STejun Heo */ 5737e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5738e6267616STejun Heo { 5739e6267616STejun Heo struct worker *worker = current_wq_worker(); 5740e6267616STejun Heo 57416a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5742e6267616STejun Heo } 5743e6267616STejun Heo 5744e6267616STejun Heo /** 5745dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5746dcd989cbSTejun Heo * @cpu: CPU in question 5747dcd989cbSTejun Heo * @wq: target workqueue 5748dcd989cbSTejun Heo * 5749dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5750dcd989cbSTejun Heo * no synchronization around this function and the test result is 5751dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5752dcd989cbSTejun Heo * 5753d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5754636b927eSTejun Heo * 5755636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5756636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5757636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5758636b927eSTejun Heo * other CPUs. 5759d3251859STejun Heo * 5760d185af30SYacine Belkadi * Return: 5761dcd989cbSTejun Heo * %true if congested, %false otherwise. 5762dcd989cbSTejun Heo */ 5763d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5764dcd989cbSTejun Heo { 57657fb98ea7STejun Heo struct pool_workqueue *pwq; 576676af4d93STejun Heo bool ret; 576776af4d93STejun Heo 576824acfb71SThomas Gleixner rcu_read_lock(); 576924acfb71SThomas Gleixner preempt_disable(); 57707fb98ea7STejun Heo 5771d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5772d3251859STejun Heo cpu = smp_processor_id(); 5773d3251859STejun Heo 5774687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5775f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5776636b927eSTejun Heo 577724acfb71SThomas Gleixner preempt_enable(); 577824acfb71SThomas Gleixner rcu_read_unlock(); 577976af4d93STejun Heo 578076af4d93STejun Heo return ret; 5781dcd989cbSTejun Heo } 5782dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5783dcd989cbSTejun Heo 5784dcd989cbSTejun Heo /** 5785dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5786dcd989cbSTejun Heo * @work: the work to be tested 5787dcd989cbSTejun Heo * 5788dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5789dcd989cbSTejun Heo * synchronization around this function and the test result is 5790dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5791dcd989cbSTejun Heo * 5792d185af30SYacine Belkadi * Return: 5793dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5794dcd989cbSTejun Heo */ 5795dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5796dcd989cbSTejun Heo { 5797fa1b54e6STejun Heo struct worker_pool *pool; 5798c26e2f2eSTejun Heo unsigned long irq_flags; 5799dcd989cbSTejun Heo unsigned int ret = 0; 5800dcd989cbSTejun Heo 5801dcd989cbSTejun Heo if (work_pending(work)) 5802dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5803038366c5SLai Jiangshan 580424acfb71SThomas Gleixner rcu_read_lock(); 5805fa1b54e6STejun Heo pool = get_work_pool(work); 5806038366c5SLai Jiangshan if (pool) { 5807c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 5808c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5809dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5810c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 5811038366c5SLai Jiangshan } 581224acfb71SThomas Gleixner rcu_read_unlock(); 5813dcd989cbSTejun Heo 5814dcd989cbSTejun Heo return ret; 5815dcd989cbSTejun Heo } 5816dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5817dcd989cbSTejun Heo 58183d1cb205STejun Heo /** 58193d1cb205STejun Heo * set_worker_desc - set description for the current work item 58203d1cb205STejun Heo * @fmt: printf-style format string 58213d1cb205STejun Heo * @...: arguments for the format string 58223d1cb205STejun Heo * 58233d1cb205STejun Heo * This function can be called by a running work function to describe what 58243d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 58253d1cb205STejun Heo * information will be printed out together to help debugging. The 58263d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 58273d1cb205STejun Heo */ 58283d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 58293d1cb205STejun Heo { 58303d1cb205STejun Heo struct worker *worker = current_wq_worker(); 58313d1cb205STejun Heo va_list args; 58323d1cb205STejun Heo 58333d1cb205STejun Heo if (worker) { 58343d1cb205STejun Heo va_start(args, fmt); 58353d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 58363d1cb205STejun Heo va_end(args); 58373d1cb205STejun Heo } 58383d1cb205STejun Heo } 58395c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 58403d1cb205STejun Heo 58413d1cb205STejun Heo /** 58423d1cb205STejun Heo * print_worker_info - print out worker information and description 58433d1cb205STejun Heo * @log_lvl: the log level to use when printing 58443d1cb205STejun Heo * @task: target task 58453d1cb205STejun Heo * 58463d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 58473d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 58483d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 58493d1cb205STejun Heo * 58503d1cb205STejun Heo * This function can be safely called on any task as long as the 58513d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 58523d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 58533d1cb205STejun Heo */ 58543d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 58553d1cb205STejun Heo { 58563d1cb205STejun Heo work_func_t *fn = NULL; 58573d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 58583d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 58593d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 58603d1cb205STejun Heo struct workqueue_struct *wq = NULL; 58613d1cb205STejun Heo struct worker *worker; 58623d1cb205STejun Heo 58633d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 58643d1cb205STejun Heo return; 58653d1cb205STejun Heo 58663d1cb205STejun Heo /* 58673d1cb205STejun Heo * This function is called without any synchronization and @task 58683d1cb205STejun Heo * could be in any state. Be careful with dereferences. 58693d1cb205STejun Heo */ 5870e700591aSPetr Mladek worker = kthread_probe_data(task); 58713d1cb205STejun Heo 58723d1cb205STejun Heo /* 58738bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 58748bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 58753d1cb205STejun Heo */ 5876fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5877fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5878fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5879fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5880fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 58813d1cb205STejun Heo 58823d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5883d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 58848bf89593STejun Heo if (strcmp(name, desc)) 58853d1cb205STejun Heo pr_cont(" (%s)", desc); 58863d1cb205STejun Heo pr_cont("\n"); 58873d1cb205STejun Heo } 58883d1cb205STejun Heo } 58893d1cb205STejun Heo 58903494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 58913494fc30STejun Heo { 58923494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 58933494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 58943494fc30STejun Heo pr_cont(" node=%d", pool->node); 58954cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 58964cb1ef64STejun Heo if (pool->flags & POOL_BH) 58974cb1ef64STejun Heo pr_cont(" bh%s", 58984cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 58994cb1ef64STejun Heo else 59004cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 59014cb1ef64STejun Heo } 59024cb1ef64STejun Heo 59034cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 59044cb1ef64STejun Heo { 59054cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 59064cb1ef64STejun Heo 59074cb1ef64STejun Heo if (pool->flags & WQ_BH) 59084cb1ef64STejun Heo pr_cont("bh%s", 59094cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 59104cb1ef64STejun Heo else 59114cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 59124cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 59133494fc30STejun Heo } 59143494fc30STejun Heo 5915c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5916c76feb0dSPaul E. McKenney bool comma; 5917c76feb0dSPaul E. McKenney work_func_t func; 5918c76feb0dSPaul E. McKenney long ctr; 5919c76feb0dSPaul E. McKenney }; 5920c76feb0dSPaul E. McKenney 5921c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5922c76feb0dSPaul E. McKenney { 5923c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5924c76feb0dSPaul E. McKenney goto out_record; 5925c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5926c76feb0dSPaul E. McKenney pcwsp->ctr++; 5927c76feb0dSPaul E. McKenney return; 5928c76feb0dSPaul E. McKenney } 5929c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5930c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5931c76feb0dSPaul E. McKenney else 5932c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5933c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5934c76feb0dSPaul E. McKenney out_record: 5935c76feb0dSPaul E. McKenney if ((long)func == -1L) 5936c76feb0dSPaul E. McKenney return; 5937c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5938c76feb0dSPaul E. McKenney pcwsp->func = func; 5939c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5940c76feb0dSPaul E. McKenney } 5941c76feb0dSPaul E. McKenney 5942c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 59433494fc30STejun Heo { 59443494fc30STejun Heo if (work->func == wq_barrier_func) { 59453494fc30STejun Heo struct wq_barrier *barr; 59463494fc30STejun Heo 59473494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 59483494fc30STejun Heo 5949c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 59503494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 59513494fc30STejun Heo task_pid_nr(barr->task)); 59523494fc30STejun Heo } else { 5953c76feb0dSPaul E. McKenney if (!comma) 5954c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5955c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 59563494fc30STejun Heo } 59573494fc30STejun Heo } 59583494fc30STejun Heo 59593494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 59603494fc30STejun Heo { 5961c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 59623494fc30STejun Heo struct worker_pool *pool = pwq->pool; 59633494fc30STejun Heo struct work_struct *work; 59643494fc30STejun Heo struct worker *worker; 59653494fc30STejun Heo bool has_in_flight = false, has_pending = false; 59663494fc30STejun Heo int bkt; 59673494fc30STejun Heo 59683494fc30STejun Heo pr_info(" pwq %d:", pool->id); 59693494fc30STejun Heo pr_cont_pool_info(pool); 59703494fc30STejun Heo 5971a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5972a045a272STejun Heo pwq->nr_active, pwq->refcnt, 59733494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 59743494fc30STejun Heo 59753494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59763494fc30STejun Heo if (worker->current_pwq == pwq) { 59773494fc30STejun Heo has_in_flight = true; 59783494fc30STejun Heo break; 59793494fc30STejun Heo } 59803494fc30STejun Heo } 59813494fc30STejun Heo if (has_in_flight) { 59823494fc30STejun Heo bool comma = false; 59833494fc30STejun Heo 59843494fc30STejun Heo pr_info(" in-flight:"); 59853494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59863494fc30STejun Heo if (worker->current_pwq != pwq) 59873494fc30STejun Heo continue; 59883494fc30STejun Heo 59894cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 59904cb1ef64STejun Heo pr_cont_worker_id(worker); 59914cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 59923494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5993c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5994c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59953494fc30STejun Heo comma = true; 59963494fc30STejun Heo } 59973494fc30STejun Heo pr_cont("\n"); 59983494fc30STejun Heo } 59993494fc30STejun Heo 60003494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 60013494fc30STejun Heo if (get_work_pwq(work) == pwq) { 60023494fc30STejun Heo has_pending = true; 60033494fc30STejun Heo break; 60043494fc30STejun Heo } 60053494fc30STejun Heo } 60063494fc30STejun Heo if (has_pending) { 60073494fc30STejun Heo bool comma = false; 60083494fc30STejun Heo 60093494fc30STejun Heo pr_info(" pending:"); 60103494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 60113494fc30STejun Heo if (get_work_pwq(work) != pwq) 60123494fc30STejun Heo continue; 60133494fc30STejun Heo 6014c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 60153494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 60163494fc30STejun Heo } 6017c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60183494fc30STejun Heo pr_cont("\n"); 60193494fc30STejun Heo } 60203494fc30STejun Heo 6021f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 60223494fc30STejun Heo bool comma = false; 60233494fc30STejun Heo 6024f97a4a1aSLai Jiangshan pr_info(" inactive:"); 6025f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 6026c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 60273494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 60283494fc30STejun Heo } 6029c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60303494fc30STejun Heo pr_cont("\n"); 60313494fc30STejun Heo } 60323494fc30STejun Heo } 60333494fc30STejun Heo 60343494fc30STejun Heo /** 603555df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 603655df0933SImran Khan * @wq: workqueue whose state will be printed 60373494fc30STejun Heo */ 603855df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 60393494fc30STejun Heo { 60403494fc30STejun Heo struct pool_workqueue *pwq; 60413494fc30STejun Heo bool idle = true; 6042c26e2f2eSTejun Heo unsigned long irq_flags; 60433494fc30STejun Heo 60443494fc30STejun Heo for_each_pwq(pwq, wq) { 6045afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 60463494fc30STejun Heo idle = false; 60473494fc30STejun Heo break; 60483494fc30STejun Heo } 60493494fc30STejun Heo } 605055df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 605155df0933SImran Khan return; 60523494fc30STejun Heo 60533494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 60543494fc30STejun Heo 60553494fc30STejun Heo for_each_pwq(pwq, wq) { 6056c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 6057afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 605857116ce1SJohan Hovold /* 605957116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 606057116ce1SJohan Hovold * drivers that queue work while holding locks 606157116ce1SJohan Hovold * also taken in their write paths. 606257116ce1SJohan Hovold */ 606357116ce1SJohan Hovold printk_deferred_enter(); 60643494fc30STejun Heo show_pwq(pwq); 606557116ce1SJohan Hovold printk_deferred_exit(); 606657116ce1SJohan Hovold } 6067c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 606862635ea8SSergey Senozhatsky /* 606962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 607055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 607162635ea8SSergey Senozhatsky * hard lockup. 607262635ea8SSergey Senozhatsky */ 607362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 60743494fc30STejun Heo } 607555df0933SImran Khan 60763494fc30STejun Heo } 60773494fc30STejun Heo 607855df0933SImran Khan /** 607955df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 608055df0933SImran Khan * @pool: worker pool whose state will be printed 608155df0933SImran Khan */ 608255df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 608355df0933SImran Khan { 60843494fc30STejun Heo struct worker *worker; 60853494fc30STejun Heo bool first = true; 6086c26e2f2eSTejun Heo unsigned long irq_flags; 6087335a42ebSPetr Mladek unsigned long hung = 0; 60883494fc30STejun Heo 6089c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 60903494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 60913494fc30STejun Heo goto next_pool; 6092335a42ebSPetr Mladek 6093335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6094335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6095335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6096335a42ebSPetr Mladek 609757116ce1SJohan Hovold /* 609857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 609957116ce1SJohan Hovold * queue work while holding locks also taken in their write 610057116ce1SJohan Hovold * paths. 610157116ce1SJohan Hovold */ 610257116ce1SJohan Hovold printk_deferred_enter(); 61033494fc30STejun Heo pr_info("pool %d:", pool->id); 61043494fc30STejun Heo pr_cont_pool_info(pool); 6105335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 61063494fc30STejun Heo if (pool->manager) 61073494fc30STejun Heo pr_cont(" manager: %d", 61083494fc30STejun Heo task_pid_nr(pool->manager->task)); 61093494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 61104cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 61114cb1ef64STejun Heo pr_cont_worker_id(worker); 61123494fc30STejun Heo first = false; 61133494fc30STejun Heo } 61143494fc30STejun Heo pr_cont("\n"); 611557116ce1SJohan Hovold printk_deferred_exit(); 61163494fc30STejun Heo next_pool: 6117c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 611862635ea8SSergey Senozhatsky /* 611962635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 612055df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 612162635ea8SSergey Senozhatsky * hard lockup. 612262635ea8SSergey Senozhatsky */ 612362635ea8SSergey Senozhatsky touch_nmi_watchdog(); 612455df0933SImran Khan 61253494fc30STejun Heo } 61263494fc30STejun Heo 612755df0933SImran Khan /** 612855df0933SImran Khan * show_all_workqueues - dump workqueue state 612955df0933SImran Khan * 6130704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 613155df0933SImran Khan */ 613255df0933SImran Khan void show_all_workqueues(void) 613355df0933SImran Khan { 613455df0933SImran Khan struct workqueue_struct *wq; 613555df0933SImran Khan struct worker_pool *pool; 613655df0933SImran Khan int pi; 613755df0933SImran Khan 613855df0933SImran Khan rcu_read_lock(); 613955df0933SImran Khan 614055df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 614155df0933SImran Khan 614255df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 614355df0933SImran Khan show_one_workqueue(wq); 614455df0933SImran Khan 614555df0933SImran Khan for_each_pool(pool, pi) 614655df0933SImran Khan show_one_worker_pool(pool); 614755df0933SImran Khan 614824acfb71SThomas Gleixner rcu_read_unlock(); 61493494fc30STejun Heo } 61503494fc30STejun Heo 6151704bc669SJungseung Lee /** 6152704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6153704bc669SJungseung Lee * 6154704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6155704bc669SJungseung Lee * still busy. 6156704bc669SJungseung Lee */ 6157704bc669SJungseung Lee void show_freezable_workqueues(void) 6158704bc669SJungseung Lee { 6159704bc669SJungseung Lee struct workqueue_struct *wq; 6160704bc669SJungseung Lee 6161704bc669SJungseung Lee rcu_read_lock(); 6162704bc669SJungseung Lee 6163704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6164704bc669SJungseung Lee 6165704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6166704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6167704bc669SJungseung Lee continue; 6168704bc669SJungseung Lee show_one_workqueue(wq); 6169704bc669SJungseung Lee } 6170704bc669SJungseung Lee 6171704bc669SJungseung Lee rcu_read_unlock(); 6172704bc669SJungseung Lee } 6173704bc669SJungseung Lee 61746b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 61756b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 61766b59808bSTejun Heo { 61776b59808bSTejun Heo int off; 61786b59808bSTejun Heo 61796b59808bSTejun Heo /* always show the actual comm */ 61806b59808bSTejun Heo off = strscpy(buf, task->comm, size); 61816b59808bSTejun Heo if (off < 0) 61826b59808bSTejun Heo return; 61836b59808bSTejun Heo 6184197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 61856b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 61866b59808bSTejun Heo 6187197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6188197f6accSTejun Heo struct worker *worker = kthread_data(task); 6189197f6accSTejun Heo struct worker_pool *pool = worker->pool; 61906b59808bSTejun Heo 61916b59808bSTejun Heo if (pool) { 6192a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 61936b59808bSTejun Heo /* 6194197f6accSTejun Heo * ->desc tracks information (wq name or 6195197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6196197f6accSTejun Heo * current, prepend '+', otherwise '-'. 61976b59808bSTejun Heo */ 61986b59808bSTejun Heo if (worker->desc[0] != '\0') { 61996b59808bSTejun Heo if (worker->current_work) 62006b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 62016b59808bSTejun Heo worker->desc); 62026b59808bSTejun Heo else 62036b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 62046b59808bSTejun Heo worker->desc); 62056b59808bSTejun Heo } 6206a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 62076b59808bSTejun Heo } 6208197f6accSTejun Heo } 62096b59808bSTejun Heo 62106b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 62116b59808bSTejun Heo } 62126b59808bSTejun Heo 621366448bc2SMathieu Malaterre #ifdef CONFIG_SMP 621466448bc2SMathieu Malaterre 6215db7bccf4STejun Heo /* 6216db7bccf4STejun Heo * CPU hotplug. 6217db7bccf4STejun Heo * 6218e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6219112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6220706026c2STejun Heo * pool which make migrating pending and scheduled works very 6221e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 622294cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6223e22bee78STejun Heo * blocked draining impractical. 6224e22bee78STejun Heo * 622524647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6226628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6227628c78e7STejun Heo * cpu comes back online. 6228db7bccf4STejun Heo */ 6229db7bccf4STejun Heo 6230e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6231db7bccf4STejun Heo { 62324ce62e9eSTejun Heo struct worker_pool *pool; 6233db7bccf4STejun Heo struct worker *worker; 6234db7bccf4STejun Heo 6235f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 62361258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6237a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6238e22bee78STejun Heo 6239f2d5a0eeSTejun Heo /* 624092f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 624194cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 624211b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6243b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6244b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6245b4ac9384SLai Jiangshan * is on the same cpu. 6246f2d5a0eeSTejun Heo */ 6247da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6248403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6249db7bccf4STejun Heo 625024647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6251f2d5a0eeSTejun Heo 6252e22bee78STejun Heo /* 6253989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6254989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6255989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6256989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6257989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6258eb283428SLai Jiangshan * are served by workers tied to the pool. 6259e22bee78STejun Heo */ 6260bc35f7efSLai Jiangshan pool->nr_running = 0; 6261eb283428SLai Jiangshan 6262eb283428SLai Jiangshan /* 6263eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6264eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6265eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6266eb283428SLai Jiangshan */ 62670219a352STejun Heo kick_pool(pool); 6268989442d7SLai Jiangshan 6269a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6270989442d7SLai Jiangshan 6271793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6272793777bcSValentin Schneider unbind_worker(worker); 6273989442d7SLai Jiangshan 6274989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6275eb283428SLai Jiangshan } 6276db7bccf4STejun Heo } 6277db7bccf4STejun Heo 6278bd7c089eSTejun Heo /** 6279bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6280bd7c089eSTejun Heo * @pool: pool of interest 6281bd7c089eSTejun Heo * 6282a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6283bd7c089eSTejun Heo */ 6284bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6285bd7c089eSTejun Heo { 6286a9ab775bSTejun Heo struct worker *worker; 6287bd7c089eSTejun Heo 62881258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6289bd7c089eSTejun Heo 6290bd7c089eSTejun Heo /* 6291a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6292a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6293402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6294a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6295a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6296bd7c089eSTejun Heo */ 6297c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6298c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6299c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 63009546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6301c63a2e52SValentin Schneider } 6302a9ab775bSTejun Heo 6303a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6304f7c17d26SWanpeng Li 63053de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6306a9ab775bSTejun Heo 6307da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6308a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6309a9ab775bSTejun Heo 6310a9ab775bSTejun Heo /* 6311a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6312a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6313a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6314a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6315a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6316a9ab775bSTejun Heo * concurrency management. Note that when or whether 6317a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6318a9ab775bSTejun Heo * 6319c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6320a9ab775bSTejun Heo * tested without holding any lock in 63216d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6322a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6323a9ab775bSTejun Heo * management operations. 6324bd7c089eSTejun Heo */ 6325a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6326a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6327a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6328c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6329bd7c089eSTejun Heo } 6330a9ab775bSTejun Heo 6331a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6332bd7c089eSTejun Heo } 6333bd7c089eSTejun Heo 63347dbc725eSTejun Heo /** 63357dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 63367dbc725eSTejun Heo * @pool: unbound pool of interest 63377dbc725eSTejun Heo * @cpu: the CPU which is coming up 63387dbc725eSTejun Heo * 63397dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 63407dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 63417dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 63427dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 63437dbc725eSTejun Heo */ 63447dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 63457dbc725eSTejun Heo { 63467dbc725eSTejun Heo static cpumask_t cpumask; 63477dbc725eSTejun Heo struct worker *worker; 63487dbc725eSTejun Heo 63491258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 63507dbc725eSTejun Heo 63517dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 63527dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 63537dbc725eSTejun Heo return; 63547dbc725eSTejun Heo 63557dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 63567dbc725eSTejun Heo 63577dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6358da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6359d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 63607dbc725eSTejun Heo } 63617dbc725eSTejun Heo 63627ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 63631da177e4SLinus Torvalds { 63644ce62e9eSTejun Heo struct worker_pool *pool; 63651da177e4SLinus Torvalds 6366f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63673ce63377STejun Heo if (pool->nr_workers) 63683ce63377STejun Heo continue; 6369051e1850SLai Jiangshan if (!create_worker(pool)) 63707ee681b2SThomas Gleixner return -ENOMEM; 63713af24433SOleg Nesterov } 63727ee681b2SThomas Gleixner return 0; 63737ee681b2SThomas Gleixner } 63741da177e4SLinus Torvalds 63757ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 63767ee681b2SThomas Gleixner { 63777ee681b2SThomas Gleixner struct worker_pool *pool; 63787ee681b2SThomas Gleixner struct workqueue_struct *wq; 63797ee681b2SThomas Gleixner int pi; 63807ee681b2SThomas Gleixner 638168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63827dbc725eSTejun Heo 63837dbc725eSTejun Heo for_each_pool(pool, pi) { 63844cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 63854cb1ef64STejun Heo if (pool->flags & POOL_BH) 63864cb1ef64STejun Heo continue; 638794cf58bbSTejun Heo 63884cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6389f05b558dSLai Jiangshan if (pool->cpu == cpu) 639094cf58bbSTejun Heo rebind_workers(pool); 6391f05b558dSLai Jiangshan else if (pool->cpu < 0) 63927dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 63931258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 639494cf58bbSTejun Heo } 63957dbc725eSTejun Heo 6396fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 63974cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 639884193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 639984193c07STejun Heo 640084193c07STejun Heo if (attrs) { 640184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 64024cbfd3deSTejun Heo int tcpu; 64034cbfd3deSTejun Heo 640484193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6405fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 64065797b1c1STejun Heo 64075797b1c1STejun Heo mutex_lock(&wq->mutex); 64085797b1c1STejun Heo wq_update_node_max_active(wq, -1); 64095797b1c1STejun Heo mutex_unlock(&wq->mutex); 64104cbfd3deSTejun Heo } 64114cbfd3deSTejun Heo } 64124c16bd32STejun Heo 641368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 64147ee681b2SThomas Gleixner return 0; 641565758202STejun Heo } 641665758202STejun Heo 64177ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 641865758202STejun Heo { 64194c16bd32STejun Heo struct workqueue_struct *wq; 64208db25e78STejun Heo 64214c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6422e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6423e8b3f8dbSLai Jiangshan return -1; 6424e8b3f8dbSLai Jiangshan 6425e8b3f8dbSLai Jiangshan unbind_workers(cpu); 64264c16bd32STejun Heo 6427fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 64284c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 64294cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 643084193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 643184193c07STejun Heo 643284193c07STejun Heo if (attrs) { 643384193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 64344cbfd3deSTejun Heo int tcpu; 64354cbfd3deSTejun Heo 643684193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6437fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 64385797b1c1STejun Heo 64395797b1c1STejun Heo mutex_lock(&wq->mutex); 64405797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 64415797b1c1STejun Heo mutex_unlock(&wq->mutex); 64424cbfd3deSTejun Heo } 64434cbfd3deSTejun Heo } 64444c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 64454c16bd32STejun Heo 64467ee681b2SThomas Gleixner return 0; 644765758202STejun Heo } 644865758202STejun Heo 64492d3854a3SRusty Russell struct work_for_cpu { 6450ed48ece2STejun Heo struct work_struct work; 64512d3854a3SRusty Russell long (*fn)(void *); 64522d3854a3SRusty Russell void *arg; 64532d3854a3SRusty Russell long ret; 64542d3854a3SRusty Russell }; 64552d3854a3SRusty Russell 6456ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 64572d3854a3SRusty Russell { 6458ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6459ed48ece2STejun Heo 64602d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 64612d3854a3SRusty Russell } 64622d3854a3SRusty Russell 64632d3854a3SRusty Russell /** 6464265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 64652d3854a3SRusty Russell * @cpu: the cpu to run on 64662d3854a3SRusty Russell * @fn: the function to run 64672d3854a3SRusty Russell * @arg: the function arg 6468265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64692d3854a3SRusty Russell * 647031ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 64716b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6472d185af30SYacine Belkadi * 6473d185af30SYacine Belkadi * Return: The value @fn returns. 64742d3854a3SRusty Russell */ 6475265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6476265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 64772d3854a3SRusty Russell { 6478ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 64792d3854a3SRusty Russell 6480265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6481ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 648212997d1aSBjorn Helgaas flush_work(&wfc.work); 6483440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 64842d3854a3SRusty Russell return wfc.ret; 64852d3854a3SRusty Russell } 6486265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 64870e8d6a93SThomas Gleixner 64880e8d6a93SThomas Gleixner /** 6489265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 64900e8d6a93SThomas Gleixner * @cpu: the cpu to run on 64910e8d6a93SThomas Gleixner * @fn: the function to run 64920e8d6a93SThomas Gleixner * @arg: the function argument 6493265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64940e8d6a93SThomas Gleixner * 64950e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 64960e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 64970e8d6a93SThomas Gleixner * 64980e8d6a93SThomas Gleixner * Return: The value @fn returns. 64990e8d6a93SThomas Gleixner */ 6500265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6501265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 65020e8d6a93SThomas Gleixner { 65030e8d6a93SThomas Gleixner long ret = -ENODEV; 65040e8d6a93SThomas Gleixner 6505ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 65060e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6507265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6508ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 65090e8d6a93SThomas Gleixner return ret; 65100e8d6a93SThomas Gleixner } 6511265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 65122d3854a3SRusty Russell #endif /* CONFIG_SMP */ 65132d3854a3SRusty Russell 6514a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6515e7577c50SRusty Russell 6516a0a1a5fdSTejun Heo /** 6517a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6518a0a1a5fdSTejun Heo * 651958a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6520f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6521706026c2STejun Heo * pool->worklist. 6522a0a1a5fdSTejun Heo * 6523a0a1a5fdSTejun Heo * CONTEXT: 6524a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6525a0a1a5fdSTejun Heo */ 6526a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6527a0a1a5fdSTejun Heo { 652824b8a847STejun Heo struct workqueue_struct *wq; 6529a0a1a5fdSTejun Heo 653068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6531a0a1a5fdSTejun Heo 65326183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6533a0a1a5fdSTejun Heo workqueue_freezing = true; 6534a0a1a5fdSTejun Heo 653524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6536a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6537a045a272STejun Heo wq_adjust_max_active(wq); 6538a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6539a1056305STejun Heo } 65405bcab335STejun Heo 654168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6542a0a1a5fdSTejun Heo } 6543a0a1a5fdSTejun Heo 6544a0a1a5fdSTejun Heo /** 654558a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6546a0a1a5fdSTejun Heo * 6547a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6548a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6549a0a1a5fdSTejun Heo * 6550a0a1a5fdSTejun Heo * CONTEXT: 655168e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6552a0a1a5fdSTejun Heo * 6553d185af30SYacine Belkadi * Return: 655458a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 655558a69cb4STejun Heo * is complete. 6556a0a1a5fdSTejun Heo */ 6557a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6558a0a1a5fdSTejun Heo { 6559a0a1a5fdSTejun Heo bool busy = false; 656024b8a847STejun Heo struct workqueue_struct *wq; 656124b8a847STejun Heo struct pool_workqueue *pwq; 6562a0a1a5fdSTejun Heo 656368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6564a0a1a5fdSTejun Heo 65656183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6566a0a1a5fdSTejun Heo 656724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 656824b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 656924b8a847STejun Heo continue; 6570a0a1a5fdSTejun Heo /* 6571a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6572a0a1a5fdSTejun Heo * to peek without lock. 6573a0a1a5fdSTejun Heo */ 657424acfb71SThomas Gleixner rcu_read_lock(); 657524b8a847STejun Heo for_each_pwq(pwq, wq) { 65766183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6577112202d9STejun Heo if (pwq->nr_active) { 6578a0a1a5fdSTejun Heo busy = true; 657924acfb71SThomas Gleixner rcu_read_unlock(); 6580a0a1a5fdSTejun Heo goto out_unlock; 6581a0a1a5fdSTejun Heo } 6582a0a1a5fdSTejun Heo } 658324acfb71SThomas Gleixner rcu_read_unlock(); 6584a0a1a5fdSTejun Heo } 6585a0a1a5fdSTejun Heo out_unlock: 658668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6587a0a1a5fdSTejun Heo return busy; 6588a0a1a5fdSTejun Heo } 6589a0a1a5fdSTejun Heo 6590a0a1a5fdSTejun Heo /** 6591a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6592a0a1a5fdSTejun Heo * 6593a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6594706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6595a0a1a5fdSTejun Heo * 6596a0a1a5fdSTejun Heo * CONTEXT: 6597a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6598a0a1a5fdSTejun Heo */ 6599a0a1a5fdSTejun Heo void thaw_workqueues(void) 6600a0a1a5fdSTejun Heo { 660124b8a847STejun Heo struct workqueue_struct *wq; 6602a0a1a5fdSTejun Heo 660368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6604a0a1a5fdSTejun Heo 6605a0a1a5fdSTejun Heo if (!workqueue_freezing) 6606a0a1a5fdSTejun Heo goto out_unlock; 6607a0a1a5fdSTejun Heo 660874b414eaSLai Jiangshan workqueue_freezing = false; 660924b8a847STejun Heo 661024b8a847STejun Heo /* restore max_active and repopulate worklist */ 661124b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6612a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6613a045a272STejun Heo wq_adjust_max_active(wq); 6614a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 661524b8a847STejun Heo } 661624b8a847STejun Heo 6617a0a1a5fdSTejun Heo out_unlock: 661868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6619a0a1a5fdSTejun Heo } 6620a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6621a0a1a5fdSTejun Heo 662299c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6623042f7df1SLai Jiangshan { 6624042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6625042f7df1SLai Jiangshan int ret = 0; 6626042f7df1SLai Jiangshan struct workqueue_struct *wq; 6627042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6628042f7df1SLai Jiangshan 6629042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6630042f7df1SLai Jiangshan 6631042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 66328eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6633042f7df1SLai Jiangshan continue; 6634042f7df1SLai Jiangshan 663599c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 663684193c07STejun Heo if (IS_ERR(ctx)) { 663784193c07STejun Heo ret = PTR_ERR(ctx); 6638042f7df1SLai Jiangshan break; 6639042f7df1SLai Jiangshan } 6640042f7df1SLai Jiangshan 6641042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6642042f7df1SLai Jiangshan } 6643042f7df1SLai Jiangshan 6644042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6645042f7df1SLai Jiangshan if (!ret) 6646042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6647042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6648042f7df1SLai Jiangshan } 6649042f7df1SLai Jiangshan 665099c621efSLai Jiangshan if (!ret) { 665199c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 665299c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 665399c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 665499c621efSLai Jiangshan } 6655042f7df1SLai Jiangshan return ret; 6656042f7df1SLai Jiangshan } 6657042f7df1SLai Jiangshan 6658042f7df1SLai Jiangshan /** 6659fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6660fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6661fe28f631SWaiman Long * 6662fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6663fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6664fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6665fe28f631SWaiman Long */ 6666fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6667fe28f631SWaiman Long { 6668fe28f631SWaiman Long cpumask_var_t cpumask; 6669fe28f631SWaiman Long int ret = 0; 6670fe28f631SWaiman Long 6671fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6672fe28f631SWaiman Long return -ENOMEM; 6673fe28f631SWaiman Long 6674fe28f631SWaiman Long lockdep_assert_cpus_held(); 6675fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6676fe28f631SWaiman Long 6677fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6678fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6679fe28f631SWaiman Long 6680fe28f631SWaiman Long /* 6681fe28f631SWaiman Long * If the operation fails, it will fall back to 6682fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6683fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6684fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6685fe28f631SWaiman Long */ 6686fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6687fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6688fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6689fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6690fe28f631SWaiman Long 6691fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6692fe28f631SWaiman Long free_cpumask_var(cpumask); 6693fe28f631SWaiman Long return ret; 6694fe28f631SWaiman Long } 6695fe28f631SWaiman Long 669663c5484eSTejun Heo static int parse_affn_scope(const char *val) 669763c5484eSTejun Heo { 669863c5484eSTejun Heo int i; 669963c5484eSTejun Heo 670063c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 670163c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 670263c5484eSTejun Heo return i; 670363c5484eSTejun Heo } 670463c5484eSTejun Heo return -EINVAL; 670563c5484eSTejun Heo } 670663c5484eSTejun Heo 670763c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 670863c5484eSTejun Heo { 6709523a301eSTejun Heo struct workqueue_struct *wq; 6710523a301eSTejun Heo int affn, cpu; 671163c5484eSTejun Heo 671263c5484eSTejun Heo affn = parse_affn_scope(val); 671363c5484eSTejun Heo if (affn < 0) 671463c5484eSTejun Heo return affn; 6715523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6716523a301eSTejun Heo return -EINVAL; 6717523a301eSTejun Heo 6718523a301eSTejun Heo cpus_read_lock(); 6719523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 672063c5484eSTejun Heo 672163c5484eSTejun Heo wq_affn_dfl = affn; 6722523a301eSTejun Heo 6723523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6724523a301eSTejun Heo for_each_online_cpu(cpu) { 6725523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6726523a301eSTejun Heo } 6727523a301eSTejun Heo } 6728523a301eSTejun Heo 6729523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6730523a301eSTejun Heo cpus_read_unlock(); 6731523a301eSTejun Heo 673263c5484eSTejun Heo return 0; 673363c5484eSTejun Heo } 673463c5484eSTejun Heo 673563c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 673663c5484eSTejun Heo { 673763c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 673863c5484eSTejun Heo } 673963c5484eSTejun Heo 674063c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 674163c5484eSTejun Heo .set = wq_affn_dfl_set, 674263c5484eSTejun Heo .get = wq_affn_dfl_get, 674363c5484eSTejun Heo }; 674463c5484eSTejun Heo 674563c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 674663c5484eSTejun Heo 67476ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 67486ba94429SFrederic Weisbecker /* 67496ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 67506ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 67516ba94429SFrederic Weisbecker * following attributes. 67526ba94429SFrederic Weisbecker * 67536ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 67546ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 67556ba94429SFrederic Weisbecker * 67566ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 67576ba94429SFrederic Weisbecker * 67586ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 67596ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 676063c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 67618639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 67626ba94429SFrederic Weisbecker */ 67636ba94429SFrederic Weisbecker struct wq_device { 67646ba94429SFrederic Weisbecker struct workqueue_struct *wq; 67656ba94429SFrederic Weisbecker struct device dev; 67666ba94429SFrederic Weisbecker }; 67676ba94429SFrederic Weisbecker 67686ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 67696ba94429SFrederic Weisbecker { 67706ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 67716ba94429SFrederic Weisbecker 67726ba94429SFrederic Weisbecker return wq_dev->wq; 67736ba94429SFrederic Weisbecker } 67746ba94429SFrederic Weisbecker 67756ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 67766ba94429SFrederic Weisbecker char *buf) 67776ba94429SFrederic Weisbecker { 67786ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67796ba94429SFrederic Weisbecker 67806ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 67816ba94429SFrederic Weisbecker } 67826ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 67836ba94429SFrederic Weisbecker 67846ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 67856ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 67866ba94429SFrederic Weisbecker { 67876ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67886ba94429SFrederic Weisbecker 67896ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 67906ba94429SFrederic Weisbecker } 67916ba94429SFrederic Weisbecker 67926ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 67936ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 67946ba94429SFrederic Weisbecker size_t count) 67956ba94429SFrederic Weisbecker { 67966ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67976ba94429SFrederic Weisbecker int val; 67986ba94429SFrederic Weisbecker 67996ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 68006ba94429SFrederic Weisbecker return -EINVAL; 68016ba94429SFrederic Weisbecker 68026ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 68036ba94429SFrederic Weisbecker return count; 68046ba94429SFrederic Weisbecker } 68056ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 68066ba94429SFrederic Weisbecker 68076ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 68086ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 68096ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 68106ba94429SFrederic Weisbecker NULL, 68116ba94429SFrederic Weisbecker }; 68126ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 68136ba94429SFrederic Weisbecker 681449277a5bSWaiman Long static void apply_wqattrs_lock(void) 681549277a5bSWaiman Long { 681649277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 681749277a5bSWaiman Long cpus_read_lock(); 681849277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 681949277a5bSWaiman Long } 682049277a5bSWaiman Long 682149277a5bSWaiman Long static void apply_wqattrs_unlock(void) 682249277a5bSWaiman Long { 682349277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 682449277a5bSWaiman Long cpus_read_unlock(); 682549277a5bSWaiman Long } 682649277a5bSWaiman Long 68276ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 68286ba94429SFrederic Weisbecker char *buf) 68296ba94429SFrederic Weisbecker { 68306ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68316ba94429SFrederic Weisbecker int written; 68326ba94429SFrederic Weisbecker 68336ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68346ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 68356ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68366ba94429SFrederic Weisbecker 68376ba94429SFrederic Weisbecker return written; 68386ba94429SFrederic Weisbecker } 68396ba94429SFrederic Weisbecker 68406ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 68416ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 68426ba94429SFrederic Weisbecker { 68436ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 68446ba94429SFrederic Weisbecker 6845899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6846899a94feSLai Jiangshan 6847be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 68486ba94429SFrederic Weisbecker if (!attrs) 68496ba94429SFrederic Weisbecker return NULL; 68506ba94429SFrederic Weisbecker 68516ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 68526ba94429SFrederic Weisbecker return attrs; 68536ba94429SFrederic Weisbecker } 68546ba94429SFrederic Weisbecker 68556ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 68566ba94429SFrederic Weisbecker const char *buf, size_t count) 68576ba94429SFrederic Weisbecker { 68586ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68596ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6860d4d3e257SLai Jiangshan int ret = -ENOMEM; 6861d4d3e257SLai Jiangshan 6862d4d3e257SLai Jiangshan apply_wqattrs_lock(); 68636ba94429SFrederic Weisbecker 68646ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 68656ba94429SFrederic Weisbecker if (!attrs) 6866d4d3e257SLai Jiangshan goto out_unlock; 68676ba94429SFrederic Weisbecker 68686ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 68696ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6870d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 68716ba94429SFrederic Weisbecker else 68726ba94429SFrederic Weisbecker ret = -EINVAL; 68736ba94429SFrederic Weisbecker 6874d4d3e257SLai Jiangshan out_unlock: 6875d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 68766ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 68776ba94429SFrederic Weisbecker return ret ?: count; 68786ba94429SFrederic Weisbecker } 68796ba94429SFrederic Weisbecker 68806ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 68816ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 68826ba94429SFrederic Weisbecker { 68836ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68846ba94429SFrederic Weisbecker int written; 68856ba94429SFrederic Weisbecker 68866ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68876ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 68886ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 68896ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68906ba94429SFrederic Weisbecker return written; 68916ba94429SFrederic Weisbecker } 68926ba94429SFrederic Weisbecker 68936ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 68946ba94429SFrederic Weisbecker struct device_attribute *attr, 68956ba94429SFrederic Weisbecker const char *buf, size_t count) 68966ba94429SFrederic Weisbecker { 68976ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68986ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6899d4d3e257SLai Jiangshan int ret = -ENOMEM; 6900d4d3e257SLai Jiangshan 6901d4d3e257SLai Jiangshan apply_wqattrs_lock(); 69026ba94429SFrederic Weisbecker 69036ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 69046ba94429SFrederic Weisbecker if (!attrs) 6905d4d3e257SLai Jiangshan goto out_unlock; 69066ba94429SFrederic Weisbecker 69076ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 69086ba94429SFrederic Weisbecker if (!ret) 6909d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 69106ba94429SFrederic Weisbecker 6911d4d3e257SLai Jiangshan out_unlock: 6912d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 69136ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 69146ba94429SFrederic Weisbecker return ret ?: count; 69156ba94429SFrederic Weisbecker } 69166ba94429SFrederic Weisbecker 691763c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 691863c5484eSTejun Heo struct device_attribute *attr, char *buf) 691963c5484eSTejun Heo { 692063c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 692163c5484eSTejun Heo int written; 692263c5484eSTejun Heo 692363c5484eSTejun Heo mutex_lock(&wq->mutex); 6924523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6925523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6926523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6927523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6928523a301eSTejun Heo else 692963c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 693063c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 693163c5484eSTejun Heo mutex_unlock(&wq->mutex); 693263c5484eSTejun Heo 693363c5484eSTejun Heo return written; 693463c5484eSTejun Heo } 693563c5484eSTejun Heo 693663c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 693763c5484eSTejun Heo struct device_attribute *attr, 693863c5484eSTejun Heo const char *buf, size_t count) 693963c5484eSTejun Heo { 694063c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 694163c5484eSTejun Heo struct workqueue_attrs *attrs; 694263c5484eSTejun Heo int affn, ret = -ENOMEM; 694363c5484eSTejun Heo 694463c5484eSTejun Heo affn = parse_affn_scope(buf); 694563c5484eSTejun Heo if (affn < 0) 694663c5484eSTejun Heo return affn; 694763c5484eSTejun Heo 694863c5484eSTejun Heo apply_wqattrs_lock(); 694963c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 695063c5484eSTejun Heo if (attrs) { 695163c5484eSTejun Heo attrs->affn_scope = affn; 695263c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 695363c5484eSTejun Heo } 695463c5484eSTejun Heo apply_wqattrs_unlock(); 695563c5484eSTejun Heo free_workqueue_attrs(attrs); 695663c5484eSTejun Heo return ret ?: count; 695763c5484eSTejun Heo } 695863c5484eSTejun Heo 69598639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 69608639ecebSTejun Heo struct device_attribute *attr, char *buf) 69618639ecebSTejun Heo { 69628639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 69638639ecebSTejun Heo 69648639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 69658639ecebSTejun Heo wq->unbound_attrs->affn_strict); 69668639ecebSTejun Heo } 69678639ecebSTejun Heo 69688639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 69698639ecebSTejun Heo struct device_attribute *attr, 69708639ecebSTejun Heo const char *buf, size_t count) 69718639ecebSTejun Heo { 69728639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 69738639ecebSTejun Heo struct workqueue_attrs *attrs; 69748639ecebSTejun Heo int v, ret = -ENOMEM; 69758639ecebSTejun Heo 69768639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 69778639ecebSTejun Heo return -EINVAL; 69788639ecebSTejun Heo 69798639ecebSTejun Heo apply_wqattrs_lock(); 69808639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 69818639ecebSTejun Heo if (attrs) { 69828639ecebSTejun Heo attrs->affn_strict = (bool)v; 69838639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 69848639ecebSTejun Heo } 69858639ecebSTejun Heo apply_wqattrs_unlock(); 69868639ecebSTejun Heo free_workqueue_attrs(attrs); 69878639ecebSTejun Heo return ret ?: count; 69888639ecebSTejun Heo } 69898639ecebSTejun Heo 69906ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 69916ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 69926ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 699363c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 69948639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 69956ba94429SFrederic Weisbecker __ATTR_NULL, 69966ba94429SFrederic Weisbecker }; 69976ba94429SFrederic Weisbecker 69984f19b8e0STejun Heo static struct bus_type wq_subsys = { 69996ba94429SFrederic Weisbecker .name = "workqueue", 70006ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 70016ba94429SFrederic Weisbecker }; 70026ba94429SFrederic Weisbecker 700349277a5bSWaiman Long /** 700449277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 700549277a5bSWaiman Long * @cpumask: the cpumask to set 700649277a5bSWaiman Long * 700749277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 700849277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 700949277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 701049277a5bSWaiman Long * 701149277a5bSWaiman Long * Return: 0 - Success 701249277a5bSWaiman Long * -EINVAL - Invalid @cpumask 701349277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 701449277a5bSWaiman Long */ 701549277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 701649277a5bSWaiman Long { 701749277a5bSWaiman Long int ret = -EINVAL; 701849277a5bSWaiman Long 701949277a5bSWaiman Long /* 702049277a5bSWaiman Long * Not excluding isolated cpus on purpose. 702149277a5bSWaiman Long * If the user wishes to include them, we allow that. 702249277a5bSWaiman Long */ 702349277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 702449277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 702549277a5bSWaiman Long apply_wqattrs_lock(); 702649277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 702749277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 702849277a5bSWaiman Long ret = 0; 702949277a5bSWaiman Long goto out_unlock; 703049277a5bSWaiman Long } 703149277a5bSWaiman Long 703249277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 703349277a5bSWaiman Long 703449277a5bSWaiman Long out_unlock: 703549277a5bSWaiman Long apply_wqattrs_unlock(); 703649277a5bSWaiman Long } 703749277a5bSWaiman Long 703849277a5bSWaiman Long return ret; 703949277a5bSWaiman Long } 704049277a5bSWaiman Long 7041fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 7042fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 7043b05a7928SFrederic Weisbecker { 7044b05a7928SFrederic Weisbecker int written; 7045b05a7928SFrederic Weisbecker 7046042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 7047fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 7048042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 7049b05a7928SFrederic Weisbecker 7050b05a7928SFrederic Weisbecker return written; 7051b05a7928SFrederic Weisbecker } 7052b05a7928SFrederic Weisbecker 7053fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 7054fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7055fe28f631SWaiman Long { 7056fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 7057fe28f631SWaiman Long } 7058fe28f631SWaiman Long 7059fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 7060fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7061fe28f631SWaiman Long { 7062fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 7063fe28f631SWaiman Long } 7064fe28f631SWaiman Long 7065fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 7066fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7067fe28f631SWaiman Long { 7068fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 7069fe28f631SWaiman Long } 7070fe28f631SWaiman Long 7071042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 7072042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 7073042f7df1SLai Jiangshan { 7074042f7df1SLai Jiangshan cpumask_var_t cpumask; 7075042f7df1SLai Jiangshan int ret; 7076042f7df1SLai Jiangshan 7077042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 7078042f7df1SLai Jiangshan return -ENOMEM; 7079042f7df1SLai Jiangshan 7080042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7081042f7df1SLai Jiangshan if (!ret) 7082042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7083042f7df1SLai Jiangshan 7084042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7085042f7df1SLai Jiangshan return ret ? ret : count; 7086042f7df1SLai Jiangshan } 7087042f7df1SLai Jiangshan 7088fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7089042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7090fe28f631SWaiman Long wq_unbound_cpumask_store), 7091fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7092fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7093fe28f631SWaiman Long __ATTR_NULL, 7094fe28f631SWaiman Long }; 7095b05a7928SFrederic Weisbecker 70966ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 70976ba94429SFrederic Weisbecker { 7098686f6697SGreg Kroah-Hartman struct device *dev_root; 7099b05a7928SFrederic Weisbecker int err; 7100b05a7928SFrederic Weisbecker 7101b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7102b05a7928SFrederic Weisbecker if (err) 7103b05a7928SFrederic Weisbecker return err; 7104b05a7928SFrederic Weisbecker 7105686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7106686f6697SGreg Kroah-Hartman if (dev_root) { 7107fe28f631SWaiman Long struct device_attribute *attr; 7108fe28f631SWaiman Long 7109fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7110fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7111fe28f631SWaiman Long if (err) 7112fe28f631SWaiman Long break; 7113fe28f631SWaiman Long } 7114686f6697SGreg Kroah-Hartman put_device(dev_root); 7115686f6697SGreg Kroah-Hartman } 7116686f6697SGreg Kroah-Hartman return err; 71176ba94429SFrederic Weisbecker } 71186ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 71196ba94429SFrederic Weisbecker 71206ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 71216ba94429SFrederic Weisbecker { 71226ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 71236ba94429SFrederic Weisbecker 71246ba94429SFrederic Weisbecker kfree(wq_dev); 71256ba94429SFrederic Weisbecker } 71266ba94429SFrederic Weisbecker 71276ba94429SFrederic Weisbecker /** 71286ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 71296ba94429SFrederic Weisbecker * @wq: the workqueue to register 71306ba94429SFrederic Weisbecker * 71316ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 71326ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 71336ba94429SFrederic Weisbecker * which is the preferred method. 71346ba94429SFrederic Weisbecker * 71356ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 71366ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 71376ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 71386ba94429SFrederic Weisbecker * attributes. 71396ba94429SFrederic Weisbecker * 71406ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 71416ba94429SFrederic Weisbecker */ 71426ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 71436ba94429SFrederic Weisbecker { 71446ba94429SFrederic Weisbecker struct wq_device *wq_dev; 71456ba94429SFrederic Weisbecker int ret; 71466ba94429SFrederic Weisbecker 71476ba94429SFrederic Weisbecker /* 71484c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 71494c065dbcSWaiman Long * ordered workqueues. 71506ba94429SFrederic Weisbecker */ 71513bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 71526ba94429SFrederic Weisbecker return -EINVAL; 71536ba94429SFrederic Weisbecker 71546ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 71556ba94429SFrederic Weisbecker if (!wq_dev) 71566ba94429SFrederic Weisbecker return -ENOMEM; 71576ba94429SFrederic Weisbecker 71586ba94429SFrederic Weisbecker wq_dev->wq = wq; 71596ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 71606ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 716123217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 71626ba94429SFrederic Weisbecker 71636ba94429SFrederic Weisbecker /* 71646ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 71656ba94429SFrederic Weisbecker * everything is ready. 71666ba94429SFrederic Weisbecker */ 71676ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 71686ba94429SFrederic Weisbecker 71696ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 71706ba94429SFrederic Weisbecker if (ret) { 7171537f4146SArvind Yadav put_device(&wq_dev->dev); 71726ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71736ba94429SFrederic Weisbecker return ret; 71746ba94429SFrederic Weisbecker } 71756ba94429SFrederic Weisbecker 71766ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 71776ba94429SFrederic Weisbecker struct device_attribute *attr; 71786ba94429SFrederic Weisbecker 71796ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 71806ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 71816ba94429SFrederic Weisbecker if (ret) { 71826ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 71836ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71846ba94429SFrederic Weisbecker return ret; 71856ba94429SFrederic Weisbecker } 71866ba94429SFrederic Weisbecker } 71876ba94429SFrederic Weisbecker } 71886ba94429SFrederic Weisbecker 71896ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 71906ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 71916ba94429SFrederic Weisbecker return 0; 71926ba94429SFrederic Weisbecker } 71936ba94429SFrederic Weisbecker 71946ba94429SFrederic Weisbecker /** 71956ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 71966ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 71976ba94429SFrederic Weisbecker * 71986ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 71996ba94429SFrederic Weisbecker */ 72006ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 72016ba94429SFrederic Weisbecker { 72026ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 72036ba94429SFrederic Weisbecker 72046ba94429SFrederic Weisbecker if (!wq->wq_dev) 72056ba94429SFrederic Weisbecker return; 72066ba94429SFrederic Weisbecker 72076ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72086ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 72096ba94429SFrederic Weisbecker } 72106ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 72116ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 72126ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 72136ba94429SFrederic Weisbecker 721482607adcSTejun Heo /* 721582607adcSTejun Heo * Workqueue watchdog. 721682607adcSTejun Heo * 721782607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 721882607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 721982607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 722082607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 722182607adcSTejun Heo * largely opaque. 722282607adcSTejun Heo * 722382607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 722482607adcSTejun Heo * state if some pools failed to make forward progress for a while where 722582607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 722682607adcSTejun Heo * 722782607adcSTejun Heo * This mechanism is controlled through the kernel parameter 722882607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 722982607adcSTejun Heo * corresponding sysfs parameter file. 723082607adcSTejun Heo */ 723182607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 723282607adcSTejun Heo 723382607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 72345cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 723582607adcSTejun Heo 723682607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 723782607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 723882607adcSTejun Heo 7239cd2440d6SPetr Mladek /* 7240cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7241cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7242cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7243cd2440d6SPetr Mladek * in all other situations. 7244cd2440d6SPetr Mladek */ 7245cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7246cd2440d6SPetr Mladek { 7247cd2440d6SPetr Mladek struct worker *worker; 7248c26e2f2eSTejun Heo unsigned long irq_flags; 7249cd2440d6SPetr Mladek int bkt; 7250cd2440d6SPetr Mladek 7251c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 7252cd2440d6SPetr Mladek 7253cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7254cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7255cd2440d6SPetr Mladek /* 7256cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7257cd2440d6SPetr Mladek * drivers that queue work while holding locks 7258cd2440d6SPetr Mladek * also taken in their write paths. 7259cd2440d6SPetr Mladek */ 7260cd2440d6SPetr Mladek printk_deferred_enter(); 7261cd2440d6SPetr Mladek 7262cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7263cd2440d6SPetr Mladek sched_show_task(worker->task); 7264cd2440d6SPetr Mladek 7265cd2440d6SPetr Mladek printk_deferred_exit(); 7266cd2440d6SPetr Mladek } 7267cd2440d6SPetr Mladek } 7268cd2440d6SPetr Mladek 7269c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 7270cd2440d6SPetr Mladek } 7271cd2440d6SPetr Mladek 7272cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7273cd2440d6SPetr Mladek { 7274cd2440d6SPetr Mladek struct worker_pool *pool; 7275cd2440d6SPetr Mladek int pi; 7276cd2440d6SPetr Mladek 7277cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7278cd2440d6SPetr Mladek 7279cd2440d6SPetr Mladek rcu_read_lock(); 7280cd2440d6SPetr Mladek 7281cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7282cd2440d6SPetr Mladek if (pool->cpu_stall) 7283cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7284cd2440d6SPetr Mladek 7285cd2440d6SPetr Mladek } 7286cd2440d6SPetr Mladek 7287cd2440d6SPetr Mladek rcu_read_unlock(); 7288cd2440d6SPetr Mladek } 7289cd2440d6SPetr Mladek 729082607adcSTejun Heo static void wq_watchdog_reset_touched(void) 729182607adcSTejun Heo { 729282607adcSTejun Heo int cpu; 729382607adcSTejun Heo 729482607adcSTejun Heo wq_watchdog_touched = jiffies; 729582607adcSTejun Heo for_each_possible_cpu(cpu) 729682607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 729782607adcSTejun Heo } 729882607adcSTejun Heo 72995cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 730082607adcSTejun Heo { 730182607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 730282607adcSTejun Heo bool lockup_detected = false; 7303cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7304940d71c6SSergey Senozhatsky unsigned long now = jiffies; 730582607adcSTejun Heo struct worker_pool *pool; 730682607adcSTejun Heo int pi; 730782607adcSTejun Heo 730882607adcSTejun Heo if (!thresh) 730982607adcSTejun Heo return; 731082607adcSTejun Heo 731182607adcSTejun Heo rcu_read_lock(); 731282607adcSTejun Heo 731382607adcSTejun Heo for_each_pool(pool, pi) { 731482607adcSTejun Heo unsigned long pool_ts, touched, ts; 731582607adcSTejun Heo 7316cd2440d6SPetr Mladek pool->cpu_stall = false; 731782607adcSTejun Heo if (list_empty(&pool->worklist)) 731882607adcSTejun Heo continue; 731982607adcSTejun Heo 7320940d71c6SSergey Senozhatsky /* 7321940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7322940d71c6SSergey Senozhatsky * the watchdog like a stall. 7323940d71c6SSergey Senozhatsky */ 7324940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7325940d71c6SSergey Senozhatsky 732682607adcSTejun Heo /* get the latest of pool and touched timestamps */ 732789e28ce6SWang Qing if (pool->cpu >= 0) 732889e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 732989e28ce6SWang Qing else 733082607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 733189e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 733282607adcSTejun Heo 733382607adcSTejun Heo if (time_after(pool_ts, touched)) 733482607adcSTejun Heo ts = pool_ts; 733582607adcSTejun Heo else 733682607adcSTejun Heo ts = touched; 733782607adcSTejun Heo 733882607adcSTejun Heo /* did we stall? */ 7339940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 734082607adcSTejun Heo lockup_detected = true; 73414cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7342cd2440d6SPetr Mladek pool->cpu_stall = true; 7343cd2440d6SPetr Mladek cpu_pool_stall = true; 7344cd2440d6SPetr Mladek } 734582607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 734682607adcSTejun Heo pr_cont_pool_info(pool); 734782607adcSTejun Heo pr_cont(" stuck for %us!\n", 7348940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 734982607adcSTejun Heo } 7350cd2440d6SPetr Mladek 7351cd2440d6SPetr Mladek 735282607adcSTejun Heo } 735382607adcSTejun Heo 735482607adcSTejun Heo rcu_read_unlock(); 735582607adcSTejun Heo 735682607adcSTejun Heo if (lockup_detected) 735755df0933SImran Khan show_all_workqueues(); 735882607adcSTejun Heo 7359cd2440d6SPetr Mladek if (cpu_pool_stall) 7360cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7361cd2440d6SPetr Mladek 736282607adcSTejun Heo wq_watchdog_reset_touched(); 736382607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 736482607adcSTejun Heo } 736582607adcSTejun Heo 7366cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 736782607adcSTejun Heo { 736882607adcSTejun Heo if (cpu >= 0) 736982607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 737089e28ce6SWang Qing 737182607adcSTejun Heo wq_watchdog_touched = jiffies; 737282607adcSTejun Heo } 737382607adcSTejun Heo 737482607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 737582607adcSTejun Heo { 737682607adcSTejun Heo wq_watchdog_thresh = 0; 737782607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 737882607adcSTejun Heo 737982607adcSTejun Heo if (thresh) { 738082607adcSTejun Heo wq_watchdog_thresh = thresh; 738182607adcSTejun Heo wq_watchdog_reset_touched(); 738282607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 738382607adcSTejun Heo } 738482607adcSTejun Heo } 738582607adcSTejun Heo 738682607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 738782607adcSTejun Heo const struct kernel_param *kp) 738882607adcSTejun Heo { 738982607adcSTejun Heo unsigned long thresh; 739082607adcSTejun Heo int ret; 739182607adcSTejun Heo 739282607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 739382607adcSTejun Heo if (ret) 739482607adcSTejun Heo return ret; 739582607adcSTejun Heo 739682607adcSTejun Heo if (system_wq) 739782607adcSTejun Heo wq_watchdog_set_thresh(thresh); 739882607adcSTejun Heo else 739982607adcSTejun Heo wq_watchdog_thresh = thresh; 740082607adcSTejun Heo 740182607adcSTejun Heo return 0; 740282607adcSTejun Heo } 740382607adcSTejun Heo 740482607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 740582607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 740682607adcSTejun Heo .get = param_get_ulong, 740782607adcSTejun Heo }; 740882607adcSTejun Heo 740982607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 741082607adcSTejun Heo 0644); 741182607adcSTejun Heo 741282607adcSTejun Heo static void wq_watchdog_init(void) 741382607adcSTejun Heo { 74145cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 741582607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 741682607adcSTejun Heo } 741782607adcSTejun Heo 741882607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 741982607adcSTejun Heo 742082607adcSTejun Heo static inline void wq_watchdog_init(void) { } 742182607adcSTejun Heo 742282607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 742382607adcSTejun Heo 74242f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work) 74252f34d733STejun Heo { 74262f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 74272f34d733STejun Heo } 74282f34d733STejun Heo 74292f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work) 74302f34d733STejun Heo { 74312f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 74322f34d733STejun Heo } 74332f34d733STejun Heo 74344a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 74354a6c5607STejun Heo { 74364a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 74374a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 74384a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 74394a6c5607STejun Heo return; 74404a6c5607STejun Heo } 74414a6c5607STejun Heo 74424a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 74434a6c5607STejun Heo } 74444a6c5607STejun Heo 74452fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 74462fcdb1b4STejun Heo { 74472fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 74482fcdb1b4STejun Heo pool->cpu = cpu; 74492fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 74502fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 74512fcdb1b4STejun Heo pool->attrs->nice = nice; 74522fcdb1b4STejun Heo pool->attrs->affn_strict = true; 74532fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 74542fcdb1b4STejun Heo 74552fcdb1b4STejun Heo /* alloc pool ID */ 74562fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 74572fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 74582fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 74592fcdb1b4STejun Heo } 74602fcdb1b4STejun Heo 74613347fa09STejun Heo /** 74623347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 74633347fa09STejun Heo * 74642930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 74652930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 74662930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 74672930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 74682930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 74692930155bSTejun Heo * before early initcalls. 74703347fa09STejun Heo */ 74712333e829SYu Chen void __init workqueue_init_early(void) 74721da177e4SLinus Torvalds { 747384193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 74747a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 74752f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal, 74762f34d733STejun Heo bh_pool_kick_highpri }; 74777a4e344cSTejun Heo int i, cpu; 7478c34056a3STejun Heo 747910cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7480e904e6c2STejun Heo 7481b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7482fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7483fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7484b05a7928SFrederic Weisbecker 74854a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 74864a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 74874a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7488ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 74894a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7490ace3c549Stiozhang 7491fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 74928b03ae3cSTejun Heo 74938b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7494e22bee78STejun Heo 74952930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 74962930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 74972930155bSTejun Heo 74987bd20b6bSMarcelo Tosatti /* 74997bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 75007bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 75017bd20b6bSMarcelo Tosatti */ 75027bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 75037bd20b6bSMarcelo Tosatti wq_power_efficient = true; 75047bd20b6bSMarcelo Tosatti 750584193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 750684193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 750784193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 750884193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 750984193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 751084193c07STejun Heo 751184193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 751284193c07STejun Heo 751384193c07STejun Heo pt->nr_pods = 1; 751484193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 751584193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 751684193c07STejun Heo pt->cpu_pod[0] = 0; 751784193c07STejun Heo 75184cb1ef64STejun Heo /* initialize BH and CPU pools */ 75191da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 75201da177e4SLinus Torvalds struct worker_pool *pool; 75211da177e4SLinus Torvalds 75221da177e4SLinus Torvalds i = 0; 75234cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 75242f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]); 75254cb1ef64STejun Heo pool->flags |= POOL_BH; 75262f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]); 75272f34d733STejun Heo i++; 75284cb1ef64STejun Heo } 75294cb1ef64STejun Heo 75304cb1ef64STejun Heo i = 0; 75312fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 75322fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 75331da177e4SLinus Torvalds } 75341da177e4SLinus Torvalds 75358a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 753629c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 753729c91e99STejun Heo struct workqueue_attrs *attrs; 753829c91e99STejun Heo 7539be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 754029c91e99STejun Heo attrs->nice = std_nice[i]; 754129c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 75428a2b7538STejun Heo 75438a2b7538STejun Heo /* 75448a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 75458a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 75468a2b7538STejun Heo */ 7547be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 75488a2b7538STejun Heo attrs->nice = std_nice[i]; 7549af73f5c9STejun Heo attrs->ordered = true; 75508a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 755129c91e99STejun Heo } 755229c91e99STejun Heo 7553d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 75541aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7555d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7556f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7557636b927eSTejun Heo WQ_MAX_ACTIVE); 755824d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 755924d51addSTejun Heo WQ_FREEZABLE, 0); 75600668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 75610668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 75628318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 75630668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 75640668106cSViresh Kumar 0); 75654cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 75664cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 75674cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 75681aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 75690668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 75700668106cSViresh Kumar !system_power_efficient_wq || 75714cb1ef64STejun Heo !system_freezable_power_efficient_wq || 75724cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 75733347fa09STejun Heo } 75743347fa09STejun Heo 7575aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7576aa6fde93STejun Heo { 7577aa6fde93STejun Heo unsigned long thresh; 7578aa6fde93STejun Heo unsigned long bogo; 7579aa6fde93STejun Heo 7580dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7581dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7582dd64c873SZqiang 7583aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7584aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7585aa6fde93STejun Heo return; 7586aa6fde93STejun Heo 7587aa6fde93STejun Heo /* 7588aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7589aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7590aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7591aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7592aa6fde93STejun Heo * too low. 7593aa6fde93STejun Heo * 7594aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7595aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7596aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7597aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7598aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7599aa6fde93STejun Heo * usefulness. 7600aa6fde93STejun Heo */ 7601aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7602aa6fde93STejun Heo 7603aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7604aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7605aa6fde93STejun Heo if (bogo < 4000) 7606aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7607aa6fde93STejun Heo 7608aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7609aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7610aa6fde93STejun Heo 7611aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7612aa6fde93STejun Heo } 7613aa6fde93STejun Heo 76143347fa09STejun Heo /** 76153347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 76163347fa09STejun Heo * 76172930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 76182930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 76192930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 76202930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 76212930155bSTejun Heo * workers and enable future kworker creations. 76223347fa09STejun Heo */ 76232333e829SYu Chen void __init workqueue_init(void) 76243347fa09STejun Heo { 76252186d9f9STejun Heo struct workqueue_struct *wq; 76263347fa09STejun Heo struct worker_pool *pool; 76273347fa09STejun Heo int cpu, bkt; 76283347fa09STejun Heo 7629aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7630aa6fde93STejun Heo 76312186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 76322186d9f9STejun Heo 76332930155bSTejun Heo /* 76342930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 76352930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 76362930155bSTejun Heo */ 76372186d9f9STejun Heo for_each_possible_cpu(cpu) { 76384cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 76392186d9f9STejun Heo pool->node = cpu_to_node(cpu); 76404cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 76414cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 76422186d9f9STejun Heo } 76432186d9f9STejun Heo 764440c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 764540c17f75STejun Heo WARN(init_rescuer(wq), 764640c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 764740c17f75STejun Heo wq->name); 764840c17f75STejun Heo } 76492186d9f9STejun Heo 76502186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 76512186d9f9STejun Heo 76524cb1ef64STejun Heo /* 76534cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 76544cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 76554cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 76564cb1ef64STejun Heo * possible CPUs here. 76574cb1ef64STejun Heo */ 76584cb1ef64STejun Heo for_each_possible_cpu(cpu) 76594cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 76604cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 76614cb1ef64STejun Heo 76623347fa09STejun Heo for_each_online_cpu(cpu) { 76633347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 76643347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 76653347fa09STejun Heo BUG_ON(!create_worker(pool)); 76663347fa09STejun Heo } 76673347fa09STejun Heo } 76683347fa09STejun Heo 76693347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 76703347fa09STejun Heo BUG_ON(!create_worker(pool)); 76713347fa09STejun Heo 76723347fa09STejun Heo wq_online = true; 767382607adcSTejun Heo wq_watchdog_init(); 76741da177e4SLinus Torvalds } 7675c4f135d6STetsuo Handa 7676025e1684STejun Heo /* 7677025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7678025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7679025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7680025e1684STejun Heo */ 7681025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7682025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7683025e1684STejun Heo { 7684025e1684STejun Heo int cur, pre, cpu, pod; 7685025e1684STejun Heo 7686025e1684STejun Heo pt->nr_pods = 0; 7687025e1684STejun Heo 7688025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7689025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7690025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7691025e1684STejun Heo 7692025e1684STejun Heo for_each_possible_cpu(cur) { 7693025e1684STejun Heo for_each_possible_cpu(pre) { 7694025e1684STejun Heo if (pre >= cur) { 7695025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7696025e1684STejun Heo break; 7697025e1684STejun Heo } 7698025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7699025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7700025e1684STejun Heo break; 7701025e1684STejun Heo } 7702025e1684STejun Heo } 7703025e1684STejun Heo } 7704025e1684STejun Heo 7705025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7706025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7707025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7708025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7709025e1684STejun Heo 7710025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7711025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7712025e1684STejun Heo 7713025e1684STejun Heo for_each_possible_cpu(cpu) { 7714025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7715025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7716025e1684STejun Heo } 7717025e1684STejun Heo } 7718025e1684STejun Heo 771963c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 772063c5484eSTejun Heo { 772163c5484eSTejun Heo return false; 772263c5484eSTejun Heo } 772363c5484eSTejun Heo 772463c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 772563c5484eSTejun Heo { 772663c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 772763c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 772863c5484eSTejun Heo #else 772963c5484eSTejun Heo return false; 773063c5484eSTejun Heo #endif 773163c5484eSTejun Heo } 773263c5484eSTejun Heo 7733025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7734025e1684STejun Heo { 7735025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7736025e1684STejun Heo } 7737025e1684STejun Heo 77382930155bSTejun Heo /** 77392930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 77402930155bSTejun Heo * 774196068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 77422930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 77432930155bSTejun Heo * initializes the unbound CPU pods accordingly. 77442930155bSTejun Heo */ 77452930155bSTejun Heo void __init workqueue_init_topology(void) 7746a86feae6STejun Heo { 77472930155bSTejun Heo struct workqueue_struct *wq; 7748025e1684STejun Heo int cpu; 7749a86feae6STejun Heo 775063c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 775163c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 775263c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7753025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7754a86feae6STejun Heo 7755c5f8cd6cSTejun Heo wq_topo_initialized = true; 7756c5f8cd6cSTejun Heo 77572930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7758a86feae6STejun Heo 7759a86feae6STejun Heo /* 77602930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 77612930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 77622930155bSTejun Heo * combinations to apply per-pod sharing. 77632930155bSTejun Heo */ 77642930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 77655797b1c1STejun Heo for_each_online_cpu(cpu) 77662930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 77675797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 77685797b1c1STejun Heo mutex_lock(&wq->mutex); 77695797b1c1STejun Heo wq_update_node_max_active(wq, -1); 77705797b1c1STejun Heo mutex_unlock(&wq->mutex); 77712930155bSTejun Heo } 77722930155bSTejun Heo } 77732930155bSTejun Heo 77742930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7775a86feae6STejun Heo } 7776a86feae6STejun Heo 777720bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 777820bdedafSTetsuo Handa { 777920bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 778020bdedafSTetsuo Handa dump_stack(); 778120bdedafSTetsuo Handa } 7782c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7783ace3c549Stiozhang 7784ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7785ace3c549Stiozhang { 7786ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7787ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7788ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7789ace3c549Stiozhang } 7790ace3c549Stiozhang 7791ace3c549Stiozhang return 1; 7792ace3c549Stiozhang } 7793ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7794