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 /* 250*e9a8e01fSTejun 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; 297*e9a8e01fSTejun 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 /* 488967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 489967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 490967b494eSTejun Heo * worker to avoid A-A deadlocks. 491967b494eSTejun Heo */ 49268279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 493967b494eSTejun Heo 49468279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 495ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 49668279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 4971aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 49868279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 499d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 50068279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 501f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 50268279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 50324d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 50468279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 5050668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 50668279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 5070668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 5084cb1ef64STejun Heo struct workqueue_struct *system_bh_wq; 5094cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq); 5104cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq; 5114cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq); 512d320c038STejun Heo 5137d19c5ceSTejun Heo static int worker_thread(void *__worker); 5146ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 515c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 51655df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 5177d19c5ceSTejun Heo 51897bd2347STejun Heo #define CREATE_TRACE_POINTS 51997bd2347STejun Heo #include <trace/events/workqueue.h> 52097bd2347STejun Heo 52168e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 522d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 523f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 52424acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 5255bcab335STejun Heo 5265b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 527d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 528f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 529f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53024acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5315b95e1afSLai Jiangshan 5324cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \ 5334cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ 5344cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5354cb1ef64STejun Heo (pool)++) 5364cb1ef64STejun Heo 537f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 538f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 539f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5407a62c2c8STejun Heo (pool)++) 5414ce62e9eSTejun Heo 54249e3cf44STejun Heo /** 54317116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 54417116969STejun Heo * @pool: iteration cursor 545611c92a0STejun Heo * @pi: integer used for iteration 546fa1b54e6STejun Heo * 54724acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 54868e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 54968e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 550fa1b54e6STejun Heo * 551fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 552fa1b54e6STejun Heo * ignored. 55317116969STejun Heo */ 554611c92a0STejun Heo #define for_each_pool(pool, pi) \ 555611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 55668e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 557fa1b54e6STejun Heo else 55817116969STejun Heo 55917116969STejun Heo /** 560822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 561822d8405STejun Heo * @worker: iteration cursor 562822d8405STejun Heo * @pool: worker_pool to iterate workers of 563822d8405STejun Heo * 5641258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 565822d8405STejun Heo * 566822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 567822d8405STejun Heo * ignored. 568822d8405STejun Heo */ 569da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 570da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5711258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 572822d8405STejun Heo else 573822d8405STejun Heo 574822d8405STejun Heo /** 57549e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 57649e3cf44STejun Heo * @pwq: iteration cursor 57749e3cf44STejun Heo * @wq: the target workqueue 57876af4d93STejun Heo * 57924acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 580794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 581794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 58276af4d93STejun Heo * 58376af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 58476af4d93STejun Heo * ignored. 58549e3cf44STejun Heo */ 58649e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 58749e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5885a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 589f3421797STejun Heo 590dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 591dc186ad7SThomas Gleixner 592f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 593dc186ad7SThomas Gleixner 59499777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 59599777288SStanislaw Gruszka { 59699777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 59799777288SStanislaw Gruszka } 59899777288SStanislaw Gruszka 599b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 600b9fdac7fSDu, Changbin { 601b9fdac7fSDu, Changbin struct work_struct *work = addr; 602b9fdac7fSDu, Changbin 603b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 604b9fdac7fSDu, Changbin } 605b9fdac7fSDu, Changbin 606dc186ad7SThomas Gleixner /* 607dc186ad7SThomas Gleixner * fixup_init is called when: 608dc186ad7SThomas Gleixner * - an active object is initialized 609dc186ad7SThomas Gleixner */ 61002a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 611dc186ad7SThomas Gleixner { 612dc186ad7SThomas Gleixner struct work_struct *work = addr; 613dc186ad7SThomas Gleixner 614dc186ad7SThomas Gleixner switch (state) { 615dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 616dc186ad7SThomas Gleixner cancel_work_sync(work); 617dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 61802a982a6SDu, Changbin return true; 619dc186ad7SThomas Gleixner default: 62002a982a6SDu, Changbin return false; 621dc186ad7SThomas Gleixner } 622dc186ad7SThomas Gleixner } 623dc186ad7SThomas Gleixner 624dc186ad7SThomas Gleixner /* 625dc186ad7SThomas Gleixner * fixup_free is called when: 626dc186ad7SThomas Gleixner * - an active object is freed 627dc186ad7SThomas Gleixner */ 62802a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 629dc186ad7SThomas Gleixner { 630dc186ad7SThomas Gleixner struct work_struct *work = addr; 631dc186ad7SThomas Gleixner 632dc186ad7SThomas Gleixner switch (state) { 633dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 634dc186ad7SThomas Gleixner cancel_work_sync(work); 635dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 63602a982a6SDu, Changbin return true; 637dc186ad7SThomas Gleixner default: 63802a982a6SDu, Changbin return false; 639dc186ad7SThomas Gleixner } 640dc186ad7SThomas Gleixner } 641dc186ad7SThomas Gleixner 642f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 643dc186ad7SThomas Gleixner .name = "work_struct", 64499777288SStanislaw Gruszka .debug_hint = work_debug_hint, 645b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 646dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 647dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 648dc186ad7SThomas Gleixner }; 649dc186ad7SThomas Gleixner 650dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 651dc186ad7SThomas Gleixner { 652dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 653dc186ad7SThomas Gleixner } 654dc186ad7SThomas Gleixner 655dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 656dc186ad7SThomas Gleixner { 657dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 658dc186ad7SThomas Gleixner } 659dc186ad7SThomas Gleixner 660dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 661dc186ad7SThomas Gleixner { 662dc186ad7SThomas Gleixner if (onstack) 663dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 664dc186ad7SThomas Gleixner else 665dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 666dc186ad7SThomas Gleixner } 667dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 668dc186ad7SThomas Gleixner 669dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 670dc186ad7SThomas Gleixner { 671dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 672dc186ad7SThomas Gleixner } 673dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 674dc186ad7SThomas Gleixner 675ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 676ea2e64f2SThomas Gleixner { 677ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 678ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 679ea2e64f2SThomas Gleixner } 680ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 681ea2e64f2SThomas Gleixner 682dc186ad7SThomas Gleixner #else 683dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 684dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 685dc186ad7SThomas Gleixner #endif 686dc186ad7SThomas Gleixner 6874e8b22bdSLi Bin /** 68867dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6894e8b22bdSLi Bin * @pool: the pool pointer of interest 6904e8b22bdSLi Bin * 6914e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6924e8b22bdSLi Bin * successfully, -errno on failure. 6934e8b22bdSLi Bin */ 6949daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 6959daf9e67STejun Heo { 6969daf9e67STejun Heo int ret; 6979daf9e67STejun Heo 69868e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6995bcab335STejun Heo 7004e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 7014e8b22bdSLi Bin GFP_KERNEL); 702229641a6STejun Heo if (ret >= 0) { 703e68035fbSTejun Heo pool->id = ret; 704229641a6STejun Heo return 0; 705229641a6STejun Heo } 7069daf9e67STejun Heo return ret; 7079daf9e67STejun Heo } 7089daf9e67STejun Heo 7099f66cff2STejun Heo static struct pool_workqueue __rcu ** 7109f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 7119f66cff2STejun Heo { 7129f66cff2STejun Heo if (cpu >= 0) 7139f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 7149f66cff2STejun Heo else 7159f66cff2STejun Heo return &wq->dfl_pwq; 7169f66cff2STejun Heo } 7179f66cff2STejun Heo 7189f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 7199f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 7209f66cff2STejun Heo { 7219f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 7229f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 7239f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 7249f66cff2STejun Heo } 7259f66cff2STejun Heo 7265797b1c1STejun Heo /** 7275797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 7285797b1c1STejun Heo * @wq: workqueue of interest 7295797b1c1STejun Heo * 7305797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 7315797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 7325797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 7335797b1c1STejun Heo */ 7345797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 7355797b1c1STejun Heo { 7365797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7375797b1c1STejun Heo } 7385797b1c1STejun Heo 73973f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 74073f53c4aSTejun Heo { 74173f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 74273f53c4aSTejun Heo } 74373f53c4aSTejun Heo 744c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 74573f53c4aSTejun Heo { 746c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 74773f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 74873f53c4aSTejun Heo } 74973f53c4aSTejun Heo 75073f53c4aSTejun Heo static int work_next_color(int color) 75173f53c4aSTejun Heo { 75273f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 753a848e3b6SOleg Nesterov } 754a848e3b6SOleg Nesterov 7554594bf15SDavid Howells /* 756112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 757112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7587c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7597a22ad75STejun Heo * 760112202d9STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending(), mark_work_canceling() 761112202d9STejun Heo * and clear_work_data() can be used to set the pwq, pool or clear 762bbb68dfaSTejun Heo * work->data. These functions should only be called while the work is 763bbb68dfaSTejun Heo * owned - ie. while the PENDING bit is set. 7647a22ad75STejun Heo * 765112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7667c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 767112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7687c3eed5cSTejun Heo * available only while the work item is queued. 769bbb68dfaSTejun Heo * 770bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 771bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 772bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 773bbb68dfaSTejun Heo * try to steal the PENDING bit. 7744594bf15SDavid Howells */ 7757a22ad75STejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data, 7767a22ad75STejun Heo unsigned long flags) 7777a22ad75STejun Heo { 7786183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 7797a22ad75STejun Heo atomic_long_set(&work->data, data | flags | work_static(work)); 7807a22ad75STejun Heo } 7817a22ad75STejun Heo 782112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 7834690c4abSTejun Heo unsigned long extra_flags) 784365970a1SDavid Howells { 785112202d9STejun Heo set_work_data(work, (unsigned long)pwq, 786112202d9STejun Heo WORK_STRUCT_PENDING | WORK_STRUCT_PWQ | extra_flags); 787365970a1SDavid Howells } 788365970a1SDavid Howells 7894468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 7904468a00fSLai Jiangshan int pool_id) 7914468a00fSLai Jiangshan { 7924468a00fSLai Jiangshan set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 7934468a00fSLai Jiangshan WORK_STRUCT_PENDING); 7944468a00fSLai Jiangshan } 7954468a00fSLai Jiangshan 7967c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 7977c3eed5cSTejun Heo int pool_id) 7984d707b9fSOleg Nesterov { 79923657bb1STejun Heo /* 80023657bb1STejun Heo * The following wmb is paired with the implied mb in 80123657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 80223657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 80323657bb1STejun Heo * owner. 80423657bb1STejun Heo */ 80523657bb1STejun Heo smp_wmb(); 8067c3eed5cSTejun Heo set_work_data(work, (unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT, 0); 807346c09f8SRoman Pen /* 808346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 809346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 810346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 8118bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 812346c09f8SRoman Pen * the same @work. E.g. consider this case: 813346c09f8SRoman Pen * 814346c09f8SRoman Pen * CPU#0 CPU#1 815346c09f8SRoman Pen * ---------------------------- -------------------------------- 816346c09f8SRoman Pen * 817346c09f8SRoman Pen * 1 STORE event_indicated 818346c09f8SRoman Pen * 2 queue_work_on() { 819346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 820346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 821346c09f8SRoman Pen * 5 set_work_data() # clear bit 822346c09f8SRoman Pen * 6 smp_mb() 823346c09f8SRoman Pen * 7 work->current_func() { 824346c09f8SRoman Pen * 8 LOAD event_indicated 825346c09f8SRoman Pen * } 826346c09f8SRoman Pen * 827346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 828346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 829346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 830346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 831346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 832346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 833346c09f8SRoman Pen * before actual STORE. 834346c09f8SRoman Pen */ 835346c09f8SRoman Pen smp_mb(); 8364d707b9fSOleg Nesterov } 8374d707b9fSOleg Nesterov 8387a22ad75STejun Heo static void clear_work_data(struct work_struct *work) 839365970a1SDavid Howells { 8407c3eed5cSTejun Heo smp_wmb(); /* see set_work_pool_and_clear_pending() */ 8417c3eed5cSTejun Heo set_work_data(work, WORK_STRUCT_NO_POOL, 0); 8427a22ad75STejun Heo } 8437a22ad75STejun Heo 844afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 845afa4bb77SLinus Torvalds { 846*e9a8e01fSTejun Heo return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK); 847afa4bb77SLinus Torvalds } 848afa4bb77SLinus Torvalds 849112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8507a22ad75STejun Heo { 851e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8527a22ad75STejun Heo 853112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 854afa4bb77SLinus Torvalds return work_struct_pwq(data); 855e120153dSTejun Heo else 856e120153dSTejun Heo return NULL; 8577a22ad75STejun Heo } 8587a22ad75STejun Heo 8597c3eed5cSTejun Heo /** 8607c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8617c3eed5cSTejun Heo * @work: the work item of interest 8627c3eed5cSTejun Heo * 86368e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 86424acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 86524acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 866fa1b54e6STejun Heo * 867fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 868fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 869fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 870fa1b54e6STejun Heo * returned pool is and stays online. 871d185af30SYacine Belkadi * 872d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8737c3eed5cSTejun Heo */ 8747c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8757a22ad75STejun Heo { 876e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8777c3eed5cSTejun Heo int pool_id; 8787a22ad75STejun Heo 87968e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 880fa1b54e6STejun Heo 881112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 882afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8837a22ad75STejun Heo 8847c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8857c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8867a22ad75STejun Heo return NULL; 8877a22ad75STejun Heo 888fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8897c3eed5cSTejun Heo } 8907c3eed5cSTejun Heo 8917c3eed5cSTejun Heo /** 8927c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8937c3eed5cSTejun Heo * @work: the work item of interest 8947c3eed5cSTejun Heo * 895d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8967c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8977c3eed5cSTejun Heo */ 8987c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8997c3eed5cSTejun Heo { 90054d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 9017c3eed5cSTejun Heo 902112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 903afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 90454d5b7d0SLai Jiangshan 90554d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 9067c3eed5cSTejun Heo } 9077c3eed5cSTejun Heo 908bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 909bbb68dfaSTejun Heo { 9107c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 911bbb68dfaSTejun Heo 9127c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 9137c3eed5cSTejun Heo set_work_data(work, pool_id | WORK_OFFQ_CANCELING, WORK_STRUCT_PENDING); 914bbb68dfaSTejun Heo } 915bbb68dfaSTejun Heo 916bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 917bbb68dfaSTejun Heo { 918bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 919bbb68dfaSTejun Heo 920112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 921bbb68dfaSTejun Heo } 922bbb68dfaSTejun Heo 923e22bee78STejun Heo /* 9243270476aSTejun Heo * Policy functions. These define the policies on how the global worker 9253270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 926d565ed63STejun Heo * they're being called with pool->lock held. 927e22bee78STejun Heo */ 928e22bee78STejun Heo 929e22bee78STejun Heo /* 930e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 931e22bee78STejun Heo * running workers. 932974271c4STejun Heo * 933974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 934706026c2STejun Heo * function will always return %true for unbound pools as long as the 935974271c4STejun Heo * worklist isn't empty. 936e22bee78STejun Heo */ 93763d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 938e22bee78STejun Heo { 9390219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 940e22bee78STejun Heo } 941e22bee78STejun Heo 942e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 94363d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 944e22bee78STejun Heo { 94563d95a91STejun Heo return pool->nr_idle; 946e22bee78STejun Heo } 947e22bee78STejun Heo 948e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 94963d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 950e22bee78STejun Heo { 951bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 952e22bee78STejun Heo } 953e22bee78STejun Heo 954e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 95563d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 956e22bee78STejun Heo { 95763d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 958e22bee78STejun Heo } 959e22bee78STejun Heo 960e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 96163d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 962e22bee78STejun Heo { 963692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 96463d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 96563d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 966e22bee78STejun Heo 967e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 968e22bee78STejun Heo } 969e22bee78STejun Heo 9704690c4abSTejun Heo /** 971e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 972cb444766STejun Heo * @worker: self 973d302f017STejun Heo * @flags: flags to set 974d302f017STejun Heo * 975228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 976d302f017STejun Heo */ 977228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 978d302f017STejun Heo { 979bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 980e22bee78STejun Heo 981bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 982cb444766STejun Heo 983228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 984e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 985e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 986bc35f7efSLai Jiangshan pool->nr_running--; 987e22bee78STejun Heo } 988e22bee78STejun Heo 989d302f017STejun Heo worker->flags |= flags; 990d302f017STejun Heo } 991d302f017STejun Heo 992d302f017STejun Heo /** 993e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 994cb444766STejun Heo * @worker: self 995d302f017STejun Heo * @flags: flags to clear 996d302f017STejun Heo * 997e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 998d302f017STejun Heo */ 999d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 1000d302f017STejun Heo { 100163d95a91STejun Heo struct worker_pool *pool = worker->pool; 1002e22bee78STejun Heo unsigned int oflags = worker->flags; 1003e22bee78STejun Heo 1004bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 1005cb444766STejun Heo 1006d302f017STejun Heo worker->flags &= ~flags; 1007e22bee78STejun Heo 100842c025f3STejun Heo /* 100942c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 101042c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 101142c025f3STejun Heo * of multiple flags, not a single flag. 101242c025f3STejun Heo */ 1013e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1014e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1015bc35f7efSLai Jiangshan pool->nr_running++; 1016d302f017STejun Heo } 1017d302f017STejun Heo 1018797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 1019797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 1020797e8345STejun Heo { 1021797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 1022797e8345STejun Heo return NULL; 1023797e8345STejun Heo 1024797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 1025797e8345STejun Heo } 1026797e8345STejun Heo 1027797e8345STejun Heo /** 1028797e8345STejun Heo * worker_enter_idle - enter idle state 1029797e8345STejun Heo * @worker: worker which is entering idle state 1030797e8345STejun Heo * 1031797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 1032797e8345STejun Heo * necessary. 1033797e8345STejun Heo * 1034797e8345STejun Heo * LOCKING: 1035797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1036797e8345STejun Heo */ 1037797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1038797e8345STejun Heo { 1039797e8345STejun Heo struct worker_pool *pool = worker->pool; 1040797e8345STejun Heo 1041797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1042797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1043797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1044797e8345STejun Heo return; 1045797e8345STejun Heo 1046797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1047797e8345STejun Heo worker->flags |= WORKER_IDLE; 1048797e8345STejun Heo pool->nr_idle++; 1049797e8345STejun Heo worker->last_active = jiffies; 1050797e8345STejun Heo 1051797e8345STejun Heo /* idle_list is LIFO */ 1052797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1053797e8345STejun Heo 1054797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1055797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1056797e8345STejun Heo 1057797e8345STejun Heo /* Sanity check nr_running. */ 1058797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1059797e8345STejun Heo } 1060797e8345STejun Heo 1061797e8345STejun Heo /** 1062797e8345STejun Heo * worker_leave_idle - leave idle state 1063797e8345STejun Heo * @worker: worker which is leaving idle state 1064797e8345STejun Heo * 1065797e8345STejun Heo * @worker is leaving idle state. Update stats. 1066797e8345STejun Heo * 1067797e8345STejun Heo * LOCKING: 1068797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1069797e8345STejun Heo */ 1070797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1071797e8345STejun Heo { 1072797e8345STejun Heo struct worker_pool *pool = worker->pool; 1073797e8345STejun Heo 1074797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1075797e8345STejun Heo return; 1076797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1077797e8345STejun Heo pool->nr_idle--; 1078797e8345STejun Heo list_del_init(&worker->entry); 1079797e8345STejun Heo } 1080797e8345STejun Heo 1081797e8345STejun Heo /** 1082797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1083797e8345STejun Heo * @pool: pool of interest 1084797e8345STejun Heo * @work: work to find worker for 1085797e8345STejun Heo * 1086797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1087797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1088797e8345STejun Heo * to match, its current execution should match the address of @work and 1089797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1090797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1091797e8345STejun Heo * being executed. 1092797e8345STejun Heo * 1093797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1094797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1095797e8345STejun Heo * another work item. If the same work item address ends up being reused 1096797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1097797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1098797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1099797e8345STejun Heo * 1100797e8345STejun Heo * This function checks the work item address and work function to avoid 1101797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1102797e8345STejun Heo * work function which can introduce dependency onto itself through a 1103797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1104797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1105797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1106797e8345STejun Heo * 1107797e8345STejun Heo * CONTEXT: 1108797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1109797e8345STejun Heo * 1110797e8345STejun Heo * Return: 1111797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1112797e8345STejun Heo * otherwise. 1113797e8345STejun Heo */ 1114797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1115797e8345STejun Heo struct work_struct *work) 1116797e8345STejun Heo { 1117797e8345STejun Heo struct worker *worker; 1118797e8345STejun Heo 1119797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1120797e8345STejun Heo (unsigned long)work) 1121797e8345STejun Heo if (worker->current_work == work && 1122797e8345STejun Heo worker->current_func == work->func) 1123797e8345STejun Heo return worker; 1124797e8345STejun Heo 1125797e8345STejun Heo return NULL; 1126797e8345STejun Heo } 1127797e8345STejun Heo 1128797e8345STejun Heo /** 1129797e8345STejun Heo * move_linked_works - move linked works to a list 1130797e8345STejun Heo * @work: start of series of works to be scheduled 1131797e8345STejun Heo * @head: target list to append @work to 1132797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1133797e8345STejun Heo * 1134873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1135873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1136873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1137873eaca6STejun Heo * @nextp. 1138797e8345STejun Heo * 1139797e8345STejun Heo * CONTEXT: 1140797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1141797e8345STejun Heo */ 1142797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1143797e8345STejun Heo struct work_struct **nextp) 1144797e8345STejun Heo { 1145797e8345STejun Heo struct work_struct *n; 1146797e8345STejun Heo 1147797e8345STejun Heo /* 1148797e8345STejun Heo * Linked worklist will always end before the end of the list, 1149797e8345STejun Heo * use NULL for list head. 1150797e8345STejun Heo */ 1151797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1152797e8345STejun Heo list_move_tail(&work->entry, head); 1153797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1154797e8345STejun Heo break; 1155797e8345STejun Heo } 1156797e8345STejun Heo 1157797e8345STejun Heo /* 1158797e8345STejun Heo * If we're already inside safe list traversal and have moved 1159797e8345STejun Heo * multiple works to the scheduled queue, the next position 1160797e8345STejun Heo * needs to be updated. 1161797e8345STejun Heo */ 1162797e8345STejun Heo if (nextp) 1163797e8345STejun Heo *nextp = n; 1164797e8345STejun Heo } 1165797e8345STejun Heo 1166797e8345STejun Heo /** 1167873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1168873eaca6STejun Heo * @work: work to assign 1169873eaca6STejun Heo * @worker: worker to assign to 1170873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1171873eaca6STejun Heo * 1172873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1173873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1174873eaca6STejun Heo * 1175873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1176873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1177873eaca6STejun Heo * list_for_each_entry_safe(). 1178873eaca6STejun Heo * 1179873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1180873eaca6STejun Heo * was punted to another worker already executing it. 1181873eaca6STejun Heo */ 1182873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1183873eaca6STejun Heo struct work_struct **nextp) 1184873eaca6STejun Heo { 1185873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1186873eaca6STejun Heo struct worker *collision; 1187873eaca6STejun Heo 1188873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1189873eaca6STejun Heo 1190873eaca6STejun Heo /* 1191873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1192873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1193873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1194873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1195873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1196873eaca6STejun Heo * defer the work to the currently executing one. 1197873eaca6STejun Heo */ 1198873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1199873eaca6STejun Heo if (unlikely(collision)) { 1200873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1201873eaca6STejun Heo return false; 1202873eaca6STejun Heo } 1203873eaca6STejun Heo 1204873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1205873eaca6STejun Heo return true; 1206873eaca6STejun Heo } 1207873eaca6STejun Heo 12082f34d733STejun Heo static struct irq_work *bh_pool_irq_work(struct worker_pool *pool) 12092f34d733STejun Heo { 12102f34d733STejun Heo int high = pool->attrs->nice == HIGHPRI_NICE_LEVEL ? 1 : 0; 12112f34d733STejun Heo 12122f34d733STejun Heo return &per_cpu(bh_pool_irq_works, pool->cpu)[high]; 12132f34d733STejun Heo } 12142f34d733STejun Heo 1215fd0a68a2STejun Heo static void kick_bh_pool(struct worker_pool *pool) 1216fd0a68a2STejun Heo { 1217fd0a68a2STejun Heo #ifdef CONFIG_SMP 1218fd0a68a2STejun Heo if (unlikely(pool->cpu != smp_processor_id())) { 1219fd0a68a2STejun Heo irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu); 1220fd0a68a2STejun Heo return; 1221fd0a68a2STejun Heo } 1222fd0a68a2STejun Heo #endif 1223fd0a68a2STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 1224fd0a68a2STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 1225fd0a68a2STejun Heo else 1226fd0a68a2STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 1227fd0a68a2STejun Heo } 1228fd0a68a2STejun Heo 1229873eaca6STejun Heo /** 12300219a352STejun Heo * kick_pool - wake up an idle worker if necessary 12310219a352STejun Heo * @pool: pool to kick 1232797e8345STejun Heo * 12330219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 12340219a352STejun Heo * whether a worker was woken up. 1235797e8345STejun Heo */ 12360219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1237797e8345STejun Heo { 1238797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 12398639ecebSTejun Heo struct task_struct *p; 1240797e8345STejun Heo 12410219a352STejun Heo lockdep_assert_held(&pool->lock); 12420219a352STejun Heo 12430219a352STejun Heo if (!need_more_worker(pool) || !worker) 12440219a352STejun Heo return false; 12450219a352STejun Heo 12464cb1ef64STejun Heo if (pool->flags & POOL_BH) { 1247fd0a68a2STejun Heo kick_bh_pool(pool); 12484cb1ef64STejun Heo return true; 12494cb1ef64STejun Heo } 12504cb1ef64STejun Heo 12518639ecebSTejun Heo p = worker->task; 12528639ecebSTejun Heo 12538639ecebSTejun Heo #ifdef CONFIG_SMP 12548639ecebSTejun Heo /* 12558639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 12568639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 12578639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 12588639ecebSTejun Heo * execution locality. 12598639ecebSTejun Heo * 12608639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 12618639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12628639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12638639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12648639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12658639ecebSTejun Heo * still on cpu when picking an idle worker. 12668639ecebSTejun Heo * 12678639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12688639ecebSTejun Heo * its affinity scope. Repatriate. 12698639ecebSTejun Heo */ 12708639ecebSTejun Heo if (!pool->attrs->affn_strict && 12718639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12728639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12738639ecebSTejun Heo struct work_struct, entry); 12748639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12758639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12768639ecebSTejun Heo } 12778639ecebSTejun Heo #endif 12788639ecebSTejun Heo wake_up_process(p); 12790219a352STejun Heo return true; 1280797e8345STejun Heo } 1281797e8345STejun Heo 128263638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 128363638450STejun Heo 128463638450STejun Heo /* 128563638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 128663638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 128763638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 128863638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 128963638450STejun Heo * should be using an unbound workqueue instead. 129063638450STejun Heo * 129163638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 129263638450STejun Heo * and report them so that they can be examined and converted to use unbound 129363638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 129463638450STejun Heo * function is tracked and reported with exponential backoff. 129563638450STejun Heo */ 129663638450STejun Heo #define WCI_MAX_ENTS 128 129763638450STejun Heo 129863638450STejun Heo struct wci_ent { 129963638450STejun Heo work_func_t func; 130063638450STejun Heo atomic64_t cnt; 130163638450STejun Heo struct hlist_node hash_node; 130263638450STejun Heo }; 130363638450STejun Heo 130463638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 130563638450STejun Heo static int wci_nr_ents; 130663638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 130763638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 130863638450STejun Heo 130963638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 131063638450STejun Heo { 131163638450STejun Heo struct wci_ent *ent; 131263638450STejun Heo 131363638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 131463638450STejun Heo (unsigned long)func) { 131563638450STejun Heo if (ent->func == func) 131663638450STejun Heo return ent; 131763638450STejun Heo } 131863638450STejun Heo return NULL; 131963638450STejun Heo } 132063638450STejun Heo 132163638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 132263638450STejun Heo { 132363638450STejun Heo struct wci_ent *ent; 132463638450STejun Heo 132563638450STejun Heo restart: 132663638450STejun Heo ent = wci_find_ent(func); 132763638450STejun Heo if (ent) { 132863638450STejun Heo u64 cnt; 132963638450STejun Heo 133063638450STejun Heo /* 133163638450STejun Heo * Start reporting from the fourth time and back off 133263638450STejun Heo * exponentially. 133363638450STejun Heo */ 133463638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 133563638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 133663638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 133763638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 133863638450STejun Heo atomic64_read(&ent->cnt)); 133963638450STejun Heo return; 134063638450STejun Heo } 134163638450STejun Heo 134263638450STejun Heo /* 134363638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 134463638450STejun Heo * is exhausted, something went really wrong and we probably made enough 134563638450STejun Heo * noise already. 134663638450STejun Heo */ 134763638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 134863638450STejun Heo return; 134963638450STejun Heo 135063638450STejun Heo raw_spin_lock(&wci_lock); 135163638450STejun Heo 135263638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 135363638450STejun Heo raw_spin_unlock(&wci_lock); 135463638450STejun Heo return; 135563638450STejun Heo } 135663638450STejun Heo 135763638450STejun Heo if (wci_find_ent(func)) { 135863638450STejun Heo raw_spin_unlock(&wci_lock); 135963638450STejun Heo goto restart; 136063638450STejun Heo } 136163638450STejun Heo 136263638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 136363638450STejun Heo ent->func = func; 136463638450STejun Heo atomic64_set(&ent->cnt, 1); 136563638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 136663638450STejun Heo 136763638450STejun Heo raw_spin_unlock(&wci_lock); 136863638450STejun Heo } 136963638450STejun Heo 137063638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 137163638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 137263638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 137363638450STejun Heo 1374c54d5046STejun Heo /** 13751da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13761da177e4SLinus Torvalds * @task: task waking up 13771da177e4SLinus Torvalds * 13781da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13791da177e4SLinus Torvalds */ 13801da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13811da177e4SLinus Torvalds { 13821da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13831da177e4SLinus Torvalds 1384c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13851da177e4SLinus Torvalds return; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds /* 13881da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13891da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13901da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13911da177e4SLinus Torvalds * pool. Protect against such race. 13921da177e4SLinus Torvalds */ 13931da177e4SLinus Torvalds preempt_disable(); 13941da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13951da177e4SLinus Torvalds worker->pool->nr_running++; 13961da177e4SLinus Torvalds preempt_enable(); 1397616db877STejun Heo 1398616db877STejun Heo /* 1399616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1400616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1401616db877STejun Heo */ 1402616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1403616db877STejun Heo 1404c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds /** 14081da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 14091da177e4SLinus Torvalds * @task: task going to sleep 14101da177e4SLinus Torvalds * 14111da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 14121da177e4SLinus Torvalds * going to sleep. 14131da177e4SLinus Torvalds */ 14141da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 14151da177e4SLinus Torvalds { 14161da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 14171da177e4SLinus Torvalds struct worker_pool *pool; 14181da177e4SLinus Torvalds 14191da177e4SLinus Torvalds /* 14201da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 14211da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 14221da177e4SLinus Torvalds * checking NOT_RUNNING. 14231da177e4SLinus Torvalds */ 14241da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 14251da177e4SLinus Torvalds return; 14261da177e4SLinus Torvalds 14271da177e4SLinus Torvalds pool = worker->pool; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1430c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 14311da177e4SLinus Torvalds return; 14321da177e4SLinus Torvalds 1433c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 14341da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds /* 14371da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 14381da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 14391da177e4SLinus Torvalds * and nr_running has been reset. 14401da177e4SLinus Torvalds */ 14411da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 14421da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14431da177e4SLinus Torvalds return; 14441da177e4SLinus Torvalds } 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds pool->nr_running--; 14470219a352STejun Heo if (kick_pool(pool)) 1448725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 14490219a352STejun Heo 14501da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds /** 1454616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1455616db877STejun Heo * @task: task currently running 1456616db877STejun Heo * 1457616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1458616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1459616db877STejun Heo */ 1460616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1461616db877STejun Heo { 1462616db877STejun Heo struct worker *worker = kthread_data(task); 1463616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1464616db877STejun Heo struct worker_pool *pool = worker->pool; 1465616db877STejun Heo 1466616db877STejun Heo if (!pwq) 1467616db877STejun Heo return; 1468616db877STejun Heo 14698a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14708a1dd1e5STejun Heo 147118c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 147218c8ae81SZqiang return; 147318c8ae81SZqiang 1474616db877STejun Heo /* 1475616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1476616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1477616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1478c8f6219bSZqiang * 1479c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1480c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1481c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1482c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1483c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1484c8f6219bSZqiang * We probably want to make this prettier in the future. 1485616db877STejun Heo */ 1486c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1487616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1488616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1489616db877STejun Heo return; 1490616db877STejun Heo 1491616db877STejun Heo raw_spin_lock(&pool->lock); 1492616db877STejun Heo 1493616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 149463638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1495616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1496616db877STejun Heo 14970219a352STejun Heo if (kick_pool(pool)) 1498616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1499616db877STejun Heo 1500616db877STejun Heo raw_spin_unlock(&pool->lock); 1501616db877STejun Heo } 1502616db877STejun Heo 1503616db877STejun Heo /** 15041da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 15051da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 15061da177e4SLinus Torvalds * 15071da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 15081da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 15091da177e4SLinus Torvalds * 15101da177e4SLinus Torvalds * CONTEXT: 15119b41ea72SAndrew Morton * raw_spin_lock_irq(rq->lock) 15121da177e4SLinus Torvalds * 15131da177e4SLinus Torvalds * This function is called during schedule() when a kworker is going 1514f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 1515f756d5e2SNathan Lynch * dequeuing, to allow periodic aggregation to shut-off when that 15161da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 15171da177e4SLinus Torvalds * 15181da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 15191da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 15201da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 15211da177e4SLinus Torvalds * is guaranteed to not be processing any works. 1522365970a1SDavid Howells * 1523365970a1SDavid Howells * Return: 1524365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1525365970a1SDavid Howells * hasn't executed any work yet. 1526365970a1SDavid Howells */ 1527365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1528365970a1SDavid Howells { 1529365970a1SDavid Howells struct worker *worker = kthread_data(task); 1530365970a1SDavid Howells 1531365970a1SDavid Howells return worker->last_func; 1532365970a1SDavid Howells } 1533365970a1SDavid Howells 1534d302f017STejun Heo /** 153591ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 153691ccc6e7STejun Heo * @wq: workqueue of interest 153791ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 153891ccc6e7STejun Heo * 153991ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 154091ccc6e7STejun Heo * 154191ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 154291ccc6e7STejun Heo * 154391ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 154491ccc6e7STejun Heo * 154591ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 154691ccc6e7STejun Heo */ 154791ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 154891ccc6e7STejun Heo int node) 154991ccc6e7STejun Heo { 155091ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 155191ccc6e7STejun Heo return NULL; 155291ccc6e7STejun Heo 155391ccc6e7STejun Heo if (node == NUMA_NO_NODE) 155491ccc6e7STejun Heo node = nr_node_ids; 155591ccc6e7STejun Heo 155691ccc6e7STejun Heo return wq->node_nr_active[node]; 155791ccc6e7STejun Heo } 155891ccc6e7STejun Heo 155991ccc6e7STejun Heo /** 15605797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 15615797b1c1STejun Heo * @wq: workqueue to update 15625797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15635797b1c1STejun Heo * 15645797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15655797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15665797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15675797b1c1STejun Heo */ 15685797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15695797b1c1STejun Heo { 15705797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15715797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15725797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15735797b1c1STejun Heo int total_cpus, node; 15745797b1c1STejun Heo 15755797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15765797b1c1STejun Heo 1577c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1578c5f8cd6cSTejun Heo return; 1579c5f8cd6cSTejun Heo 158015930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15815797b1c1STejun Heo off_cpu = -1; 15825797b1c1STejun Heo 15835797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15845797b1c1STejun Heo if (off_cpu >= 0) 15855797b1c1STejun Heo total_cpus--; 15865797b1c1STejun Heo 15875797b1c1STejun Heo for_each_node(node) { 15885797b1c1STejun Heo int node_cpus; 15895797b1c1STejun Heo 15905797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15915797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15925797b1c1STejun Heo node_cpus--; 15935797b1c1STejun Heo 15945797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 15955797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 15965797b1c1STejun Heo min_active, max_active); 15975797b1c1STejun Heo } 15985797b1c1STejun Heo 15995797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 16005797b1c1STejun Heo } 16015797b1c1STejun Heo 16025797b1c1STejun Heo /** 16038864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 16048864b4e5STejun Heo * @pwq: pool_workqueue to get 16058864b4e5STejun Heo * 16068864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 16078864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 16088864b4e5STejun Heo */ 16098864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 16108864b4e5STejun Heo { 16118864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16128864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 16138864b4e5STejun Heo pwq->refcnt++; 16148864b4e5STejun Heo } 16158864b4e5STejun Heo 16168864b4e5STejun Heo /** 16178864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 16188864b4e5STejun Heo * @pwq: pool_workqueue to put 16198864b4e5STejun Heo * 16208864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 16218864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 16228864b4e5STejun Heo */ 16238864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 16248864b4e5STejun Heo { 16258864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16268864b4e5STejun Heo if (likely(--pwq->refcnt)) 16278864b4e5STejun Heo return; 16288864b4e5STejun Heo /* 1629967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1630967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16318864b4e5STejun Heo */ 1632687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16338864b4e5STejun Heo } 16348864b4e5STejun Heo 1635dce90d47STejun Heo /** 1636dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1637dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1638dce90d47STejun Heo * 1639dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1640dce90d47STejun Heo */ 1641dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1642dce90d47STejun Heo { 1643dce90d47STejun Heo if (pwq) { 1644dce90d47STejun Heo /* 164524acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1646dce90d47STejun Heo * following lock operations are safe. 1647dce90d47STejun Heo */ 1648a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1649dce90d47STejun Heo put_pwq(pwq); 1650a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1651dce90d47STejun Heo } 1652dce90d47STejun Heo } 1653dce90d47STejun Heo 1654afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1655afa87ce8STejun Heo { 1656afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1657afa87ce8STejun Heo } 1658afa87ce8STejun Heo 16594c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16604c638030STejun Heo struct work_struct *work) 1661bf4ede01STejun Heo { 16621c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16631c270b79STejun Heo 16641c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1665bf4ede01STejun Heo trace_workqueue_activate_work(work); 166682607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 166782607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1668112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16691c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16704c638030STejun Heo } 16714c638030STejun Heo 16724c638030STejun Heo /** 16734c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16744c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16754c638030STejun Heo * @work: work item to activate 16764c638030STejun Heo * 16774c638030STejun Heo * Returns %true if activated. %false if already active. 16784c638030STejun Heo */ 16794c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16804c638030STejun Heo struct work_struct *work) 16814c638030STejun Heo { 16824c638030STejun Heo struct worker_pool *pool = pwq->pool; 168391ccc6e7STejun Heo struct wq_node_nr_active *nna; 16844c638030STejun Heo 16854c638030STejun Heo lockdep_assert_held(&pool->lock); 16864c638030STejun Heo 16874c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16884c638030STejun Heo return false; 16894c638030STejun Heo 169091ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 169191ccc6e7STejun Heo if (nna) 169291ccc6e7STejun Heo atomic_inc(&nna->nr); 169391ccc6e7STejun Heo 1694112202d9STejun Heo pwq->nr_active++; 16954c638030STejun Heo __pwq_activate_work(pwq, work); 16964c638030STejun Heo return true; 1697bf4ede01STejun Heo } 1698bf4ede01STejun Heo 16995797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 17005797b1c1STejun Heo { 17015797b1c1STejun Heo int max = READ_ONCE(nna->max); 17025797b1c1STejun Heo 17035797b1c1STejun Heo while (true) { 17045797b1c1STejun Heo int old, tmp; 17055797b1c1STejun Heo 17065797b1c1STejun Heo old = atomic_read(&nna->nr); 17075797b1c1STejun Heo if (old >= max) 17085797b1c1STejun Heo return false; 17095797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 17105797b1c1STejun Heo if (tmp == old) 17115797b1c1STejun Heo return true; 17125797b1c1STejun Heo } 17135797b1c1STejun Heo } 17145797b1c1STejun Heo 17151c270b79STejun Heo /** 17161c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 17171c270b79STejun Heo * @pwq: pool_workqueue of interest 17185797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17191c270b79STejun Heo * 17201c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 17211c270b79STejun Heo * successfully obtained. %false otherwise. 17221c270b79STejun Heo */ 17235797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 17243aa62497SLai Jiangshan { 17251c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17261c270b79STejun Heo struct worker_pool *pool = pwq->pool; 172791ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17285797b1c1STejun Heo bool obtained = false; 17291c270b79STejun Heo 17301c270b79STejun Heo lockdep_assert_held(&pool->lock); 17311c270b79STejun Heo 17325797b1c1STejun Heo if (!nna) { 17334cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17341c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17355797b1c1STejun Heo goto out; 173691ccc6e7STejun Heo } 17375797b1c1STejun Heo 17384c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17394c065dbcSWaiman Long return false; 17404c065dbcSWaiman Long 17415797b1c1STejun Heo /* 17425797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17435797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17445797b1c1STejun Heo * concurrency level. Don't jump the line. 17455797b1c1STejun Heo * 17465797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17475797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17485797b1c1STejun Heo * increase it. This is indicated by @fill. 17495797b1c1STejun Heo */ 17505797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17515797b1c1STejun Heo goto out; 17525797b1c1STejun Heo 17535797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17545797b1c1STejun Heo if (obtained) 17555797b1c1STejun Heo goto out; 17565797b1c1STejun Heo 17575797b1c1STejun Heo /* 17585797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17595797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17605797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17615797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17625797b1c1STejun Heo * $nna->pending_pwqs. 17635797b1c1STejun Heo */ 17645797b1c1STejun Heo raw_spin_lock(&nna->lock); 17655797b1c1STejun Heo 17665797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17675797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17685797b1c1STejun Heo else if (likely(!fill)) 17695797b1c1STejun Heo goto out_unlock; 17705797b1c1STejun Heo 17715797b1c1STejun Heo smp_mb(); 17725797b1c1STejun Heo 17735797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17745797b1c1STejun Heo 17755797b1c1STejun Heo /* 17765797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17775797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17785797b1c1STejun Heo */ 17795797b1c1STejun Heo if (obtained && likely(!fill)) 17805797b1c1STejun Heo list_del_init(&pwq->pending_node); 17815797b1c1STejun Heo 17825797b1c1STejun Heo out_unlock: 17835797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17845797b1c1STejun Heo out: 17855797b1c1STejun Heo if (obtained) 17865797b1c1STejun Heo pwq->nr_active++; 17871c270b79STejun Heo return obtained; 17881c270b79STejun Heo } 17891c270b79STejun Heo 17901c270b79STejun Heo /** 17911c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17921c270b79STejun Heo * @pwq: pool_workqueue of interest 17935797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17941c270b79STejun Heo * 17951c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 17961c270b79STejun Heo * max_active limit. 17971c270b79STejun Heo * 17981c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 17991c270b79STejun Heo * inactive work item is found or max_active limit is reached. 18001c270b79STejun Heo */ 18015797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 18021c270b79STejun Heo { 18031c270b79STejun Heo struct work_struct *work = 18041c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 18053aa62497SLai Jiangshan struct work_struct, entry); 18063aa62497SLai Jiangshan 18075797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 18081c270b79STejun Heo __pwq_activate_work(pwq, work); 18091c270b79STejun Heo return true; 18101c270b79STejun Heo } else { 18111c270b79STejun Heo return false; 18121c270b79STejun Heo } 18131c270b79STejun Heo } 18141c270b79STejun Heo 18151c270b79STejun Heo /** 1816516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1817516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 18184c065dbcSWaiman Long * 1819516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1820516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1821516d3dc9SWaiman Long * ensure proper work item ordering:: 18224c065dbcSWaiman Long * 18234c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 18244c065dbcSWaiman Long * | 18254c065dbcSWaiman Long * v 18264c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18274c065dbcSWaiman Long * | | | 18284c065dbcSWaiman Long * 1 3 5 18294c065dbcSWaiman Long * | | | 18304c065dbcSWaiman Long * 2 4 6 1831516d3dc9SWaiman Long * 1832516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1833516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1834516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1835516d3dc9SWaiman Long * the list is the oldest. 18364c065dbcSWaiman Long */ 18374c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18384c065dbcSWaiman Long { 18394c065dbcSWaiman Long struct pool_workqueue *pwq; 18404c065dbcSWaiman Long 18414c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18424c065dbcSWaiman Long 18434c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18444c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18454c065dbcSWaiman Long pwqs_node); 18464c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18474c065dbcSWaiman Long if (pwq->plugged) { 18484c065dbcSWaiman Long pwq->plugged = false; 18494c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18504c065dbcSWaiman Long kick_pool(pwq->pool); 18514c065dbcSWaiman Long } 18524c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18534c065dbcSWaiman Long } 18544c065dbcSWaiman Long 18554c065dbcSWaiman Long /** 18565797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18575797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18585797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18595797b1c1STejun Heo * 18605797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18615797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18625797b1c1STejun Heo */ 18635797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18645797b1c1STejun Heo struct worker_pool *caller_pool) 18655797b1c1STejun Heo { 18665797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18675797b1c1STejun Heo struct pool_workqueue *pwq; 18685797b1c1STejun Heo struct work_struct *work; 18695797b1c1STejun Heo 18705797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18715797b1c1STejun Heo 18725797b1c1STejun Heo raw_spin_lock(&nna->lock); 18735797b1c1STejun Heo retry: 18745797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18755797b1c1STejun Heo struct pool_workqueue, pending_node); 18765797b1c1STejun Heo if (!pwq) 18775797b1c1STejun Heo goto out_unlock; 18785797b1c1STejun Heo 18795797b1c1STejun Heo /* 18805797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 18815797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 18825797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 18835797b1c1STejun Heo * nested inside pool locks. 18845797b1c1STejun Heo */ 18855797b1c1STejun Heo if (pwq->pool != locked_pool) { 18865797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18875797b1c1STejun Heo locked_pool = pwq->pool; 18885797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 18895797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18905797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 18915797b1c1STejun Heo raw_spin_lock(&nna->lock); 18925797b1c1STejun Heo goto retry; 18935797b1c1STejun Heo } 18945797b1c1STejun Heo } 18955797b1c1STejun Heo 18965797b1c1STejun Heo /* 18975797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 18985797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 18995797b1c1STejun Heo */ 19005797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 19015797b1c1STejun Heo struct work_struct, entry); 19025797b1c1STejun Heo if (!work) { 19035797b1c1STejun Heo list_del_init(&pwq->pending_node); 19045797b1c1STejun Heo goto retry; 19055797b1c1STejun Heo } 19065797b1c1STejun Heo 19075797b1c1STejun Heo /* 19085797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 19095797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 19105797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 19115797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 19125797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 19135797b1c1STejun Heo */ 19145797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 19155797b1c1STejun Heo pwq->nr_active++; 19165797b1c1STejun Heo __pwq_activate_work(pwq, work); 19175797b1c1STejun Heo 19185797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 19195797b1c1STejun Heo list_del_init(&pwq->pending_node); 19205797b1c1STejun Heo else 19215797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 19225797b1c1STejun Heo 19235797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 19245797b1c1STejun Heo if (pwq->pool != caller_pool) 19255797b1c1STejun Heo kick_pool(pwq->pool); 19265797b1c1STejun Heo } 19275797b1c1STejun Heo 19285797b1c1STejun Heo out_unlock: 19295797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19305797b1c1STejun Heo if (locked_pool != caller_pool) { 19315797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19325797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19335797b1c1STejun Heo } 19345797b1c1STejun Heo } 19355797b1c1STejun Heo 19365797b1c1STejun Heo /** 19371c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19381c270b79STejun Heo * @pwq: pool_workqueue of interest 19391c270b79STejun Heo * 19401c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19415797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19421c270b79STejun Heo */ 19431c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19441c270b79STejun Heo { 19451c270b79STejun Heo struct worker_pool *pool = pwq->pool; 194691ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19471c270b79STejun Heo 19481c270b79STejun Heo lockdep_assert_held(&pool->lock); 19491c270b79STejun Heo 195091ccc6e7STejun Heo /* 195191ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 195291ccc6e7STejun Heo * workqueues. 195391ccc6e7STejun Heo */ 19541c270b79STejun Heo pwq->nr_active--; 195591ccc6e7STejun Heo 195691ccc6e7STejun Heo /* 195791ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 195891ccc6e7STejun Heo * inactive work item on @pwq itself. 195991ccc6e7STejun Heo */ 196091ccc6e7STejun Heo if (!nna) { 19615797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 196291ccc6e7STejun Heo return; 196391ccc6e7STejun Heo } 196491ccc6e7STejun Heo 19655797b1c1STejun Heo /* 19665797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19675797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19685797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19695797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19705797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19715797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19725797b1c1STejun Heo * decremented $nna->nr. 19735797b1c1STejun Heo * 19745797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19755797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19765797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 19775797b1c1STejun Heo * This maintains the forward progress guarantee. 19785797b1c1STejun Heo */ 19795797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 19805797b1c1STejun Heo return; 19815797b1c1STejun Heo 19825797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 19835797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 19843aa62497SLai Jiangshan } 19853aa62497SLai Jiangshan 1986bf4ede01STejun Heo /** 1987112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1988112202d9STejun Heo * @pwq: pwq of interest 1989c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1990bf4ede01STejun Heo * 1991bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1992112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1993bf4ede01STejun Heo * 1994dd6c3c54STejun Heo * NOTE: 1995dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1996dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1997dd6c3c54STejun Heo * work item is complete. 1998dd6c3c54STejun Heo * 1999bf4ede01STejun Heo * CONTEXT: 2000a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2001bf4ede01STejun Heo */ 2002c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 2003bf4ede01STejun Heo { 2004c4560c2cSLai Jiangshan int color = get_work_color(work_data); 2005c4560c2cSLai Jiangshan 20061c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 20071c270b79STejun Heo pwq_dec_nr_active(pwq); 2008018f3a13SLai Jiangshan 2009018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 2010bf4ede01STejun Heo 2011bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 2012112202d9STejun Heo if (likely(pwq->flush_color != color)) 20138864b4e5STejun Heo goto out_put; 2014bf4ede01STejun Heo 2015bf4ede01STejun Heo /* are there still in-flight works? */ 2016112202d9STejun Heo if (pwq->nr_in_flight[color]) 20178864b4e5STejun Heo goto out_put; 2018bf4ede01STejun Heo 2019112202d9STejun Heo /* this pwq is done, clear flush_color */ 2020112202d9STejun Heo pwq->flush_color = -1; 2021bf4ede01STejun Heo 2022bf4ede01STejun Heo /* 2023112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 2024bf4ede01STejun Heo * will handle the rest. 2025bf4ede01STejun Heo */ 2026112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2027112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20288864b4e5STejun Heo out_put: 20298864b4e5STejun Heo put_pwq(pwq); 2030bf4ede01STejun Heo } 2031bf4ede01STejun Heo 203236e227d2STejun Heo /** 2033bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 203436e227d2STejun Heo * @work: work item to steal 2035c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags 2036c26e2f2eSTejun Heo * @irq_flags: place to store irq state 203736e227d2STejun Heo * 203836e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2039d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 204036e227d2STejun Heo * 2041d185af30SYacine Belkadi * Return: 20423eb6b31bSMauro Carvalho Chehab * 20433eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 204436e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 204536e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 204636e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 2047bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 2048bbb68dfaSTejun Heo * for arbitrarily long 20493eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 205036e227d2STejun Heo * 2051d185af30SYacine Belkadi * Note: 2052bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2053e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2054e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2055e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2056bbb68dfaSTejun Heo * 2057bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2058c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags). 2059bbb68dfaSTejun Heo * 2060e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2061bf4ede01STejun Heo */ 2062c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags, 2063c26e2f2eSTejun Heo unsigned long *irq_flags) 2064bf4ede01STejun Heo { 2065d565ed63STejun Heo struct worker_pool *pool; 2066112202d9STejun Heo struct pool_workqueue *pwq; 2067bf4ede01STejun Heo 2068c26e2f2eSTejun Heo local_irq_save(*irq_flags); 2069bbb68dfaSTejun Heo 207036e227d2STejun Heo /* try to steal the timer if it exists */ 2071c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) { 207236e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 207336e227d2STejun Heo 2074e0aecdd8STejun Heo /* 2075e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2076e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2077e0aecdd8STejun Heo * running on the local CPU. 2078e0aecdd8STejun Heo */ 207936e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 208036e227d2STejun Heo return 1; 208136e227d2STejun Heo } 208236e227d2STejun Heo 208336e227d2STejun Heo /* try to claim PENDING the normal way */ 2084bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2085bf4ede01STejun Heo return 0; 2086bf4ede01STejun Heo 208724acfb71SThomas Gleixner rcu_read_lock(); 2088bf4ede01STejun Heo /* 2089bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2090bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2091bf4ede01STejun Heo */ 2092d565ed63STejun Heo pool = get_work_pool(work); 2093d565ed63STejun Heo if (!pool) 2094bbb68dfaSTejun Heo goto fail; 2095bf4ede01STejun Heo 2096a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2097bf4ede01STejun Heo /* 2098112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2099112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2100112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2101112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2102112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 21030b3dae68SLai Jiangshan * item is currently queued on that pool. 2104bf4ede01STejun Heo */ 2105112202d9STejun Heo pwq = get_work_pwq(work); 2106112202d9STejun Heo if (pwq && pwq->pool == pool) { 2107c70e1779STejun Heo unsigned long work_data; 2108c70e1779STejun Heo 2109bf4ede01STejun Heo debug_work_deactivate(work); 21103aa62497SLai Jiangshan 21113aa62497SLai Jiangshan /* 2112018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2113018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2114018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2115018f3a13SLai Jiangshan * 2116f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2117d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2118f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 211916062836STejun Heo * management later on and cause stall. Make sure the work 212016062836STejun Heo * item is activated before grabbing. 21213aa62497SLai Jiangshan */ 21224c638030STejun Heo pwq_activate_work(pwq, work); 21233aa62497SLai Jiangshan 2124bf4ede01STejun Heo list_del_init(&work->entry); 212536e227d2STejun Heo 2126c70e1779STejun Heo /* 2127c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2128c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2129c70e1779STejun Heo */ 2130c70e1779STejun Heo work_data = *work_data_bits(work); 21314468a00fSLai Jiangshan set_work_pool_and_keep_pending(work, pool->id); 21324468a00fSLai Jiangshan 2133dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2134c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2135dd6c3c54STejun Heo 2136a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 213724acfb71SThomas Gleixner rcu_read_unlock(); 213836e227d2STejun Heo return 1; 2139bf4ede01STejun Heo } 2140a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2141bbb68dfaSTejun Heo fail: 214224acfb71SThomas Gleixner rcu_read_unlock(); 2143c26e2f2eSTejun Heo local_irq_restore(*irq_flags); 2144bbb68dfaSTejun Heo if (work_is_canceling(work)) 2145bbb68dfaSTejun Heo return -ENOENT; 2146bbb68dfaSTejun Heo cpu_relax(); 214736e227d2STejun Heo return -EAGAIN; 2148bf4ede01STejun Heo } 2149bf4ede01STejun Heo 2150bf4ede01STejun Heo /** 2151706026c2STejun Heo * insert_work - insert a work into a pool 2152112202d9STejun Heo * @pwq: pwq @work belongs to 21534690c4abSTejun Heo * @work: work to insert 21544690c4abSTejun Heo * @head: insertion point 21554690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 21564690c4abSTejun Heo * 2157112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2158706026c2STejun Heo * work_struct flags. 21594690c4abSTejun Heo * 21604690c4abSTejun Heo * CONTEXT: 2161a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2162365970a1SDavid Howells */ 2163112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2164112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2165b89deed3SOleg Nesterov { 2166fe089f87STejun Heo debug_work_activate(work); 2167e1d8aa9fSFrederic Weisbecker 2168e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2169f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2170e89a85d6SWalter Wu 21714690c4abSTejun Heo /* we own @work, set data and link */ 2172112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 21731a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 21748864b4e5STejun Heo get_pwq(pwq); 2175b89deed3SOleg Nesterov } 2176b89deed3SOleg Nesterov 2177c8efcc25STejun Heo /* 2178c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 21798d03ecfeSTejun Heo * same workqueue. 2180c8efcc25STejun Heo */ 2181c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2182c8efcc25STejun Heo { 2183c8efcc25STejun Heo struct worker *worker; 2184c8efcc25STejun Heo 21858d03ecfeSTejun Heo worker = current_wq_worker(); 2186c8efcc25STejun Heo /* 2187bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 21888d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2189c8efcc25STejun Heo */ 2190112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2191c8efcc25STejun Heo } 2192c8efcc25STejun Heo 2193ef557180SMike Galbraith /* 2194ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2195ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2196ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2197ef557180SMike Galbraith */ 2198ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2199ef557180SMike Galbraith { 2200ef557180SMike Galbraith int new_cpu; 2201ef557180SMike Galbraith 2202f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2203ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2204ef557180SMike Galbraith return cpu; 2205a8ec5880SAmmar Faizi } else { 2206a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2207f303fccbSTejun Heo } 2208f303fccbSTejun Heo 2209ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2210ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2211ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2212ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2213ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2214ef557180SMike Galbraith return cpu; 2215ef557180SMike Galbraith } 2216ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2217ef557180SMike Galbraith 2218ef557180SMike Galbraith return new_cpu; 2219ef557180SMike Galbraith } 2220ef557180SMike Galbraith 2221d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 22221da177e4SLinus Torvalds struct work_struct *work) 22231da177e4SLinus Torvalds { 2224112202d9STejun Heo struct pool_workqueue *pwq; 2225fe089f87STejun Heo struct worker_pool *last_pool, *pool; 22268a2e8e5dSTejun Heo unsigned int work_flags; 2227b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 22288930cabaSTejun Heo 22298930cabaSTejun Heo /* 22308930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 22318930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 22328930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 22338930cabaSTejun Heo * happen with IRQ disabled. 22348930cabaSTejun Heo */ 22358e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 22361da177e4SLinus Torvalds 223733e3f0a3SRichard Clark /* 223833e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 223933e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 224033e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 224133e3f0a3SRichard Clark */ 224233e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 224333e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2244e41e704bSTejun Heo return; 224524acfb71SThomas Gleixner rcu_read_lock(); 22469e8cd2f5STejun Heo retry: 2247aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2248636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2249636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2250ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2251636b927eSTejun Heo else 2252aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2253aa202f1fSHillf Danton } 2254f3421797STejun Heo 2255636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2256fe089f87STejun Heo pool = pwq->pool; 2257fe089f87STejun Heo 225818aa9effSTejun Heo /* 2259c9178087STejun Heo * If @work was previously on a different pool, it might still be 2260c9178087STejun Heo * running there, in which case the work needs to be queued on that 2261c9178087STejun Heo * pool to guarantee non-reentrancy. 226218aa9effSTejun Heo */ 2263c9e7cf27STejun Heo last_pool = get_work_pool(work); 2264fe089f87STejun Heo if (last_pool && last_pool != pool) { 226518aa9effSTejun Heo struct worker *worker; 226618aa9effSTejun Heo 2267a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 226818aa9effSTejun Heo 2269c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 227018aa9effSTejun Heo 2271112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2272c9178087STejun Heo pwq = worker->current_pwq; 2273fe089f87STejun Heo pool = pwq->pool; 2274fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 22758594fadeSLai Jiangshan } else { 227618aa9effSTejun Heo /* meh... not running there, queue here */ 2277a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2278fe089f87STejun Heo raw_spin_lock(&pool->lock); 227918aa9effSTejun Heo } 22808930cabaSTejun Heo } else { 2281fe089f87STejun Heo raw_spin_lock(&pool->lock); 22828930cabaSTejun Heo } 2283502ca9d8STejun Heo 22849e8cd2f5STejun Heo /* 2285636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2286636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2287636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2288636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2289636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 22909e8cd2f5STejun Heo */ 22919e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 22929e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2293fe089f87STejun Heo raw_spin_unlock(&pool->lock); 22949e8cd2f5STejun Heo cpu_relax(); 22959e8cd2f5STejun Heo goto retry; 22969e8cd2f5STejun Heo } 22979e8cd2f5STejun Heo /* oops */ 22989e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 22999e8cd2f5STejun Heo wq->name, cpu); 23009e8cd2f5STejun Heo } 23019e8cd2f5STejun Heo 2302112202d9STejun Heo /* pwq determined, queue */ 2303112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2304502ca9d8STejun Heo 230524acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 230624acfb71SThomas Gleixner goto out; 23071e19ffc6STejun Heo 2308112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2309112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 23101e19ffc6STejun Heo 2311a045a272STejun Heo /* 2312a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2313a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2314a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2315a045a272STejun Heo */ 23165797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2317fe089f87STejun Heo if (list_empty(&pool->worklist)) 2318fe089f87STejun Heo pool->watchdog_ts = jiffies; 2319fe089f87STejun Heo 2320cdadf009STejun Heo trace_workqueue_activate_work(work); 2321fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 23220219a352STejun Heo kick_pool(pool); 23238a2e8e5dSTejun Heo } else { 2324f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2325fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 23268a2e8e5dSTejun Heo } 23271e19ffc6STejun Heo 232824acfb71SThomas Gleixner out: 2329fe089f87STejun Heo raw_spin_unlock(&pool->lock); 233024acfb71SThomas Gleixner rcu_read_unlock(); 23311da177e4SLinus Torvalds } 23321da177e4SLinus Torvalds 23330fcb78c2SRolf Eike Beer /** 2334c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2335c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2336c1a220e7SZhang Rui * @wq: workqueue to use 2337c1a220e7SZhang Rui * @work: work to queue 2338c1a220e7SZhang Rui * 2339c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2340443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2341443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2342854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2343854f5cc5SPaul E. McKenney * online will get a splat. 2344d185af30SYacine Belkadi * 2345d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2346c1a220e7SZhang Rui */ 2347d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2348d4283e93STejun Heo struct work_struct *work) 2349c1a220e7SZhang Rui { 2350d4283e93STejun Heo bool ret = false; 2351c26e2f2eSTejun Heo unsigned long irq_flags; 23528930cabaSTejun Heo 2353c26e2f2eSTejun Heo local_irq_save(irq_flags); 2354c1a220e7SZhang Rui 235522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 23564690c4abSTejun Heo __queue_work(cpu, wq, work); 2357d4283e93STejun Heo ret = true; 2358c1a220e7SZhang Rui } 23598930cabaSTejun Heo 2360c26e2f2eSTejun Heo local_irq_restore(irq_flags); 2361c1a220e7SZhang Rui return ret; 2362c1a220e7SZhang Rui } 2363ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2364c1a220e7SZhang Rui 23658204e0c1SAlexander Duyck /** 2366fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 23678204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 23688204e0c1SAlexander Duyck * 23698204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 23708204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 23718204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 23728204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 23738204e0c1SAlexander Duyck */ 2374fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 23758204e0c1SAlexander Duyck { 23768204e0c1SAlexander Duyck int cpu; 23778204e0c1SAlexander Duyck 23788204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 23798204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 23808204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 23818204e0c1SAlexander Duyck 23828204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 23838204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 23848204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 23858204e0c1SAlexander Duyck return cpu; 23868204e0c1SAlexander Duyck 23878204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 23888204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 23898204e0c1SAlexander Duyck 23908204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 23918204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 23928204e0c1SAlexander Duyck } 23938204e0c1SAlexander Duyck 23948204e0c1SAlexander Duyck /** 23958204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 23968204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 23978204e0c1SAlexander Duyck * @wq: workqueue to use 23988204e0c1SAlexander Duyck * @work: work to queue 23998204e0c1SAlexander Duyck * 24008204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 24018204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 24028204e0c1SAlexander Duyck * NUMA node. 24038204e0c1SAlexander Duyck * 24048204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 24058204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 24068204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 24078204e0c1SAlexander Duyck * 24088204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 24098204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 24108204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 24118204e0c1SAlexander Duyck * 24128204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 24138204e0c1SAlexander Duyck */ 24148204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 24158204e0c1SAlexander Duyck struct work_struct *work) 24168204e0c1SAlexander Duyck { 2417c26e2f2eSTejun Heo unsigned long irq_flags; 24188204e0c1SAlexander Duyck bool ret = false; 24198204e0c1SAlexander Duyck 24208204e0c1SAlexander Duyck /* 24218204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 24228204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 24238204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 24248204e0c1SAlexander Duyck * 24258204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 24268204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 24278204e0c1SAlexander Duyck * some round robin type logic. 24288204e0c1SAlexander Duyck */ 24298204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 24308204e0c1SAlexander Duyck 2431c26e2f2eSTejun Heo local_irq_save(irq_flags); 24328204e0c1SAlexander Duyck 24338204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2434fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 24358204e0c1SAlexander Duyck 24368204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 24378204e0c1SAlexander Duyck ret = true; 24388204e0c1SAlexander Duyck } 24398204e0c1SAlexander Duyck 2440c26e2f2eSTejun Heo local_irq_restore(irq_flags); 24418204e0c1SAlexander Duyck return ret; 24428204e0c1SAlexander Duyck } 24438204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 24448204e0c1SAlexander Duyck 24458c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 24461da177e4SLinus Torvalds { 24478c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 24481da177e4SLinus Torvalds 2449e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 245060c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 24511da177e4SLinus Torvalds } 24521438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 24531da177e4SLinus Torvalds 24547beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 245552bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 24561da177e4SLinus Torvalds { 24577beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 24587beb2edfSTejun Heo struct work_struct *work = &dwork->work; 24591da177e4SLinus Torvalds 2460637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 24614b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2462fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2463fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 24647beb2edfSTejun Heo 24658852aac2STejun Heo /* 24668852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 24678852aac2STejun Heo * both optimization and correctness. The earliest @timer can 24688852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 24698852aac2STejun Heo * on that there's no such delay when @delay is 0. 24708852aac2STejun Heo */ 24718852aac2STejun Heo if (!delay) { 24728852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 24738852aac2STejun Heo return; 24748852aac2STejun Heo } 24758852aac2STejun Heo 247660c057bcSLai Jiangshan dwork->wq = wq; 24771265057fSTejun Heo dwork->cpu = cpu; 24787beb2edfSTejun Heo timer->expires = jiffies + delay; 24797beb2edfSTejun Heo 2480aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2481aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2482aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2483aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2484aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 24857beb2edfSTejun Heo add_timer_on(timer, cpu); 2486aae17ebbSLeonardo Bras } else { 2487aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2488041bd12eSTejun Heo add_timer(timer); 2489aae17ebbSLeonardo Bras else 2490aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2491aae17ebbSLeonardo Bras } 24927beb2edfSTejun Heo } 24931da177e4SLinus Torvalds 24940fcb78c2SRolf Eike Beer /** 24950fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 24960fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 24970fcb78c2SRolf Eike Beer * @wq: workqueue to use 2498af9997e4SRandy Dunlap * @dwork: work to queue 24990fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 25000fcb78c2SRolf Eike Beer * 2501d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2502715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2503715f1300STejun Heo * execution. 25040fcb78c2SRolf Eike Beer */ 2505d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 250652bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25077a6bc1cdSVenkatesh Pallipadi { 250852bad64dSDavid Howells struct work_struct *work = &dwork->work; 2509d4283e93STejun Heo bool ret = false; 2510c26e2f2eSTejun Heo unsigned long irq_flags; 25118930cabaSTejun Heo 25128930cabaSTejun Heo /* read the comment in __queue_work() */ 2513c26e2f2eSTejun Heo local_irq_save(irq_flags); 25147a6bc1cdSVenkatesh Pallipadi 251522df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 25167beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2517d4283e93STejun Heo ret = true; 25187a6bc1cdSVenkatesh Pallipadi } 25198930cabaSTejun Heo 2520c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25217a6bc1cdSVenkatesh Pallipadi return ret; 25227a6bc1cdSVenkatesh Pallipadi } 2523ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 25241da177e4SLinus Torvalds 2525c8e55f36STejun Heo /** 25268376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 25278376fe22STejun Heo * @cpu: CPU number to execute work on 25288376fe22STejun Heo * @wq: workqueue to use 25298376fe22STejun Heo * @dwork: work to queue 25308376fe22STejun Heo * @delay: number of jiffies to wait before queueing 25318376fe22STejun Heo * 25328376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 25338376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 25348376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 25358376fe22STejun Heo * current state. 25368376fe22STejun Heo * 2537d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 25388376fe22STejun Heo * pending and its timer was modified. 25398376fe22STejun Heo * 2540e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 25418376fe22STejun Heo * See try_to_grab_pending() for details. 25428376fe22STejun Heo */ 25438376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 25448376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 25458376fe22STejun Heo { 2546c26e2f2eSTejun Heo unsigned long irq_flags; 25478376fe22STejun Heo int ret; 25488376fe22STejun Heo 25498376fe22STejun Heo do { 2550c5f5b942STejun Heo ret = try_to_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, 2551c5f5b942STejun Heo &irq_flags); 25528376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 25538376fe22STejun Heo 25548376fe22STejun Heo if (likely(ret >= 0)) { 25558376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2556c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25578376fe22STejun Heo } 25588376fe22STejun Heo 25598376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 25608376fe22STejun Heo return ret; 25618376fe22STejun Heo } 25628376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 25638376fe22STejun Heo 256405f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 256505f0fe6bSTejun Heo { 256605f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 256705f0fe6bSTejun Heo 256805f0fe6bSTejun Heo /* read the comment in __queue_work() */ 256905f0fe6bSTejun Heo local_irq_disable(); 257005f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 257105f0fe6bSTejun Heo local_irq_enable(); 257205f0fe6bSTejun Heo } 257305f0fe6bSTejun Heo 257405f0fe6bSTejun Heo /** 257505f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 257605f0fe6bSTejun Heo * @wq: workqueue to use 257705f0fe6bSTejun Heo * @rwork: work to queue 257805f0fe6bSTejun Heo * 257905f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 258005f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2581bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 258205f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 258305f0fe6bSTejun Heo */ 258405f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 258505f0fe6bSTejun Heo { 258605f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 258705f0fe6bSTejun Heo 258805f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 258905f0fe6bSTejun Heo rwork->wq = wq; 2590a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 259105f0fe6bSTejun Heo return true; 259205f0fe6bSTejun Heo } 259305f0fe6bSTejun Heo 259405f0fe6bSTejun Heo return false; 259505f0fe6bSTejun Heo } 259605f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 259705f0fe6bSTejun Heo 2598f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2599c34056a3STejun Heo { 2600c34056a3STejun Heo struct worker *worker; 2601c34056a3STejun Heo 2602f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2603c8e55f36STejun Heo if (worker) { 2604c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2605affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2606da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2607e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2608e22bee78STejun Heo worker->flags = WORKER_PREP; 2609c8e55f36STejun Heo } 2610c34056a3STejun Heo return worker; 2611c34056a3STejun Heo } 2612c34056a3STejun Heo 26139546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 26149546b29eSTejun Heo { 26158639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 26169546b29eSTejun Heo return pool->attrs->__pod_cpumask; 26178639ecebSTejun Heo else 26188639ecebSTejun Heo return pool->attrs->cpumask; 26199546b29eSTejun Heo } 26209546b29eSTejun Heo 2621c34056a3STejun Heo /** 26224736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 26234736cbf7SLai Jiangshan * @worker: worker to be attached 26244736cbf7SLai Jiangshan * @pool: the target pool 26254736cbf7SLai Jiangshan * 26264736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 26274736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 26284736cbf7SLai Jiangshan * cpu-[un]hotplugs. 26294736cbf7SLai Jiangshan */ 26304736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 26314736cbf7SLai Jiangshan struct worker_pool *pool) 26324736cbf7SLai Jiangshan { 26331258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 26344736cbf7SLai Jiangshan 26354736cbf7SLai Jiangshan /* 26364cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 26374cb1ef64STejun Heo * across this function. See the comments above the flag definition for 26384cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 26394736cbf7SLai Jiangshan */ 26404cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 26414736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 26424cb1ef64STejun Heo } else { 26434cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 26445c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 26454cb1ef64STejun Heo } 26464736cbf7SLai Jiangshan 2647640f17c8SPeter Zijlstra if (worker->rescue_wq) 26489546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2649640f17c8SPeter Zijlstra 26504736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2651a2d812a2STejun Heo worker->pool = pool; 26524736cbf7SLai Jiangshan 26531258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 26544736cbf7SLai Jiangshan } 26554736cbf7SLai Jiangshan 26564736cbf7SLai Jiangshan /** 265760f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 265860f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 265960f5a4bcSLai Jiangshan * 26604736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 26614736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 26624736cbf7SLai Jiangshan * other reference to the pool. 266360f5a4bcSLai Jiangshan */ 2664a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 266560f5a4bcSLai Jiangshan { 2666a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 266760f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 266860f5a4bcSLai Jiangshan 26694cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 26704cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 26714cb1ef64STejun Heo 26721258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2673a2d812a2STejun Heo 26745c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2675da028469SLai Jiangshan list_del(&worker->node); 2676a2d812a2STejun Heo worker->pool = NULL; 2677a2d812a2STejun Heo 2678e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 267960f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 26801258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 268160f5a4bcSLai Jiangshan 2682b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2683b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2684b62c0751SLai Jiangshan 268560f5a4bcSLai Jiangshan if (detach_completion) 268660f5a4bcSLai Jiangshan complete(detach_completion); 268760f5a4bcSLai Jiangshan } 268860f5a4bcSLai Jiangshan 268960f5a4bcSLai Jiangshan /** 2690c34056a3STejun Heo * create_worker - create a new workqueue worker 269163d95a91STejun Heo * @pool: pool the new worker will belong to 2692c34056a3STejun Heo * 2693051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2694c34056a3STejun Heo * 2695c34056a3STejun Heo * CONTEXT: 2696c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2697c34056a3STejun Heo * 2698d185af30SYacine Belkadi * Return: 2699c34056a3STejun Heo * Pointer to the newly created worker. 2700c34056a3STejun Heo */ 2701bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2702c34056a3STejun Heo { 2703e441b56fSZhen Lei struct worker *worker; 2704e441b56fSZhen Lei int id; 27055d9c7a1eSLucy Mielke char id_buf[23]; 2706c34056a3STejun Heo 27077cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2708e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 27093f0ea0b8SPetr Mladek if (id < 0) { 27103f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 27113f0ea0b8SPetr Mladek ERR_PTR(id)); 2712e441b56fSZhen Lei return NULL; 27133f0ea0b8SPetr Mladek } 2714c34056a3STejun Heo 2715f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 27163f0ea0b8SPetr Mladek if (!worker) { 27173f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2718c34056a3STejun Heo goto fail; 27193f0ea0b8SPetr Mladek } 2720c34056a3STejun Heo 2721c34056a3STejun Heo worker->id = id; 2722c34056a3STejun Heo 27234cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 272429c91e99STejun Heo if (pool->cpu >= 0) 2725e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2726e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2727f3421797STejun Heo else 2728e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2729e3c916a4STejun Heo 27304cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 27314cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 27323f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 273360f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 273460f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 273560f54038SPetr Mladek id_buf); 273660f54038SPetr Mladek } else { 27373f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 27383f0ea0b8SPetr Mladek worker->task); 273960f54038SPetr Mladek } 2740c34056a3STejun Heo goto fail; 27413f0ea0b8SPetr Mladek } 2742c34056a3STejun Heo 274391151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 27449546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 27454cb1ef64STejun Heo } 274691151228SOleg Nesterov 2747da028469SLai Jiangshan /* successful, attach the worker to the pool */ 27484736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2749822d8405STejun Heo 2750051e1850SLai Jiangshan /* start the newly created worker */ 2751a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 27520219a352STejun Heo 2753051e1850SLai Jiangshan worker->pool->nr_workers++; 2754051e1850SLai Jiangshan worker_enter_idle(worker); 27550219a352STejun Heo 27560219a352STejun Heo /* 27570219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 27586a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 27596a229b0eSTejun Heo * wake it up explicitly. 27600219a352STejun Heo */ 27614cb1ef64STejun Heo if (worker->task) 2762051e1850SLai Jiangshan wake_up_process(worker->task); 27630219a352STejun Heo 2764a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2765051e1850SLai Jiangshan 2766c34056a3STejun Heo return worker; 2767822d8405STejun Heo 2768c34056a3STejun Heo fail: 2769e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2770c34056a3STejun Heo kfree(worker); 2771c34056a3STejun Heo return NULL; 2772c34056a3STejun Heo } 2773c34056a3STejun Heo 2774793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2775793777bcSValentin Schneider { 2776793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2777793777bcSValentin Schneider 2778793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2779793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2780793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2781793777bcSValentin Schneider else 2782793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2783793777bcSValentin Schneider } 2784793777bcSValentin Schneider 2785e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2786e02b9312SValentin Schneider { 2787e02b9312SValentin Schneider struct worker *worker, *tmp; 2788e02b9312SValentin Schneider 2789e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2790e02b9312SValentin Schneider list_del_init(&worker->entry); 2791e02b9312SValentin Schneider unbind_worker(worker); 2792e02b9312SValentin Schneider /* 2793e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2794e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2795e02b9312SValentin Schneider * wouldn't have gotten here. 2796c34056a3STejun Heo * 2797e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2798e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2799e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2800e02b9312SValentin Schneider * outside of pool->lock. 2801e02b9312SValentin Schneider */ 2802e02b9312SValentin Schneider wake_up_process(worker->task); 2803e02b9312SValentin Schneider } 2804e02b9312SValentin Schneider } 2805e02b9312SValentin Schneider 2806e02b9312SValentin Schneider /** 2807e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2808e02b9312SValentin Schneider * @worker: worker to be destroyed 2809e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2810e02b9312SValentin Schneider * 2811e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2812e02b9312SValentin Schneider * should be idle. 2813c8e55f36STejun Heo * 2814c8e55f36STejun Heo * CONTEXT: 2815a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2816c34056a3STejun Heo */ 2817e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2818c34056a3STejun Heo { 2819bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2820c34056a3STejun Heo 2821cd549687STejun Heo lockdep_assert_held(&pool->lock); 2822e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2823cd549687STejun Heo 2824c34056a3STejun Heo /* sanity check frenzy */ 28256183c009STejun Heo if (WARN_ON(worker->current_work) || 282673eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 282773eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 28286183c009STejun Heo return; 2829c34056a3STejun Heo 2830bd7bdd43STejun Heo pool->nr_workers--; 2831bd7bdd43STejun Heo pool->nr_idle--; 2832c8e55f36STejun Heo 2833cb444766STejun Heo worker->flags |= WORKER_DIE; 2834e02b9312SValentin Schneider 2835e02b9312SValentin Schneider list_move(&worker->entry, list); 2836e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2837c34056a3STejun Heo } 2838c34056a3STejun Heo 28393f959aa3SValentin Schneider /** 28403f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 28413f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 28423f959aa3SValentin Schneider * 28433f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 28443f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 28453f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 28463f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 28473f959aa3SValentin Schneider * it expire and re-evaluate things from there. 28483f959aa3SValentin Schneider */ 284932a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2850e22bee78STejun Heo { 285132a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 28523f959aa3SValentin Schneider bool do_cull = false; 28533f959aa3SValentin Schneider 28543f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 28553f959aa3SValentin Schneider return; 28563f959aa3SValentin Schneider 28573f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 28583f959aa3SValentin Schneider 28593f959aa3SValentin Schneider if (too_many_workers(pool)) { 28603f959aa3SValentin Schneider struct worker *worker; 28613f959aa3SValentin Schneider unsigned long expires; 28623f959aa3SValentin Schneider 28633f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 28643f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 28653f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 28663f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 28673f959aa3SValentin Schneider 28683f959aa3SValentin Schneider if (!do_cull) 28693f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 28703f959aa3SValentin Schneider } 28713f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 28723f959aa3SValentin Schneider 28733f959aa3SValentin Schneider if (do_cull) 28743f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 28753f959aa3SValentin Schneider } 28763f959aa3SValentin Schneider 28773f959aa3SValentin Schneider /** 28783f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 28793f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 28803f959aa3SValentin Schneider * 28813f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 28823f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2883e02b9312SValentin Schneider * 2884e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2885e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2886e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 28873f959aa3SValentin Schneider */ 28883f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 28893f959aa3SValentin Schneider { 28903f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 28919680540cSYang Yingliang LIST_HEAD(cull_list); 2892e22bee78STejun Heo 2893e02b9312SValentin Schneider /* 2894e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2895e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2896e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2897e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2898e02b9312SValentin Schneider */ 2899e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2900a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2901e22bee78STejun Heo 29023347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2903e22bee78STejun Heo struct worker *worker; 2904e22bee78STejun Heo unsigned long expires; 2905e22bee78STejun Heo 290663d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2907e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2908e22bee78STejun Heo 29093347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 291063d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 29113347fc9fSLai Jiangshan break; 2912e22bee78STejun Heo } 29133347fc9fSLai Jiangshan 2914e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2915e22bee78STejun Heo } 2916e22bee78STejun Heo 2917a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2918e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2919e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2920e22bee78STejun Heo } 2921e22bee78STejun Heo 2922493a1724STejun Heo static void send_mayday(struct work_struct *work) 2923e22bee78STejun Heo { 2924112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2925112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2926493a1724STejun Heo 29272e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2928e22bee78STejun Heo 2929493008a8STejun Heo if (!wq->rescuer) 2930493a1724STejun Heo return; 2931e22bee78STejun Heo 2932e22bee78STejun Heo /* mayday mayday mayday */ 2933493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 293477668c8bSLai Jiangshan /* 293577668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 293677668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 293777668c8bSLai Jiangshan * rescuer is done with it. 293877668c8bSLai Jiangshan */ 293977668c8bSLai Jiangshan get_pwq(pwq); 2940493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 2941e22bee78STejun Heo wake_up_process(wq->rescuer->task); 2942725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 2943493a1724STejun Heo } 2944e22bee78STejun Heo } 2945e22bee78STejun Heo 294632a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 2947e22bee78STejun Heo { 294832a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 2949e22bee78STejun Heo struct work_struct *work; 2950e22bee78STejun Heo 2951a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2952a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 2953e22bee78STejun Heo 295463d95a91STejun Heo if (need_to_create_worker(pool)) { 2955e22bee78STejun Heo /* 2956e22bee78STejun Heo * We've been trying to create a new worker but 2957e22bee78STejun Heo * haven't been successful. We might be hitting an 2958e22bee78STejun Heo * allocation deadlock. Send distress signals to 2959e22bee78STejun Heo * rescuers. 2960e22bee78STejun Heo */ 296163d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 2962e22bee78STejun Heo send_mayday(work); 2963e22bee78STejun Heo } 2964e22bee78STejun Heo 2965a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 2966a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2967e22bee78STejun Heo 296863d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 2969e22bee78STejun Heo } 2970e22bee78STejun Heo 2971e22bee78STejun Heo /** 2972e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 297363d95a91STejun Heo * @pool: pool to create a new worker for 2974e22bee78STejun Heo * 297563d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 2976e22bee78STejun Heo * have at least one idle worker on return from this function. If 2977e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 297863d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 2979e22bee78STejun Heo * possible allocation deadlock. 2980e22bee78STejun Heo * 2981c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 2982c5aa87bbSTejun Heo * may_start_working() %true. 2983e22bee78STejun Heo * 2984e22bee78STejun Heo * LOCKING: 2985a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 2986e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 2987e22bee78STejun Heo * manager. 2988e22bee78STejun Heo */ 298929187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 2990d565ed63STejun Heo __releases(&pool->lock) 2991d565ed63STejun Heo __acquires(&pool->lock) 2992e22bee78STejun Heo { 2993e22bee78STejun Heo restart: 2994a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 29959f9c2364STejun Heo 2996e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 299763d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 2998e22bee78STejun Heo 2999e22bee78STejun Heo while (true) { 3000051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 3001e22bee78STejun Heo break; 3002e22bee78STejun Heo 3003e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 30049f9c2364STejun Heo 300563d95a91STejun Heo if (!need_to_create_worker(pool)) 3006e22bee78STejun Heo break; 3007e22bee78STejun Heo } 3008e22bee78STejun Heo 300963d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 3010a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3011051e1850SLai Jiangshan /* 3012051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 3013051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 3014051e1850SLai Jiangshan * already become busy. 3015051e1850SLai Jiangshan */ 301663d95a91STejun Heo if (need_to_create_worker(pool)) 3017e22bee78STejun Heo goto restart; 3018e22bee78STejun Heo } 3019e22bee78STejun Heo 3020e22bee78STejun Heo /** 3021e22bee78STejun Heo * manage_workers - manage worker pool 3022e22bee78STejun Heo * @worker: self 3023e22bee78STejun Heo * 3024706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 3025e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3026706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3027e22bee78STejun Heo * 3028e22bee78STejun Heo * The caller can safely start processing works on false return. On 3029e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3030e22bee78STejun Heo * and may_start_working() is true. 3031e22bee78STejun Heo * 3032e22bee78STejun Heo * CONTEXT: 3033a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3034e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3035e22bee78STejun Heo * 3036d185af30SYacine Belkadi * Return: 303729187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 303829187a9eSTejun Heo * start processing works, %true if management function was performed and 303929187a9eSTejun Heo * the conditions that the caller verified before calling the function may 304029187a9eSTejun Heo * no longer be true. 3041e22bee78STejun Heo */ 3042e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3043e22bee78STejun Heo { 304463d95a91STejun Heo struct worker_pool *pool = worker->pool; 3045e22bee78STejun Heo 3046692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 304729187a9eSTejun Heo return false; 3048692b4825STejun Heo 3049692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 30502607d7a6STejun Heo pool->manager = worker; 3051e22bee78STejun Heo 305229187a9eSTejun Heo maybe_create_worker(pool); 3053e22bee78STejun Heo 30542607d7a6STejun Heo pool->manager = NULL; 3055692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3056d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 305729187a9eSTejun Heo return true; 3058e22bee78STejun Heo } 3059e22bee78STejun Heo 3060a62428c0STejun Heo /** 3061a62428c0STejun Heo * process_one_work - process single work 3062c34056a3STejun Heo * @worker: self 3063a62428c0STejun Heo * @work: work to process 3064a62428c0STejun Heo * 3065a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3066a62428c0STejun Heo * process a single work including synchronization against and 3067a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3068a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3069a62428c0STejun Heo * call this function to process a work. 3070a62428c0STejun Heo * 3071a62428c0STejun Heo * CONTEXT: 3072a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3073a62428c0STejun Heo */ 3074c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3075d565ed63STejun Heo __releases(&pool->lock) 3076d565ed63STejun Heo __acquires(&pool->lock) 30771da177e4SLinus Torvalds { 3078112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3079bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3080c4560c2cSLai Jiangshan unsigned long work_data; 3081c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 30824e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 30834e6045f1SJohannes Berg /* 3084a62428c0STejun Heo * It is permissible to free the struct work_struct from 3085a62428c0STejun Heo * inside the function that is called from it, this we need to 3086a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3087a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3088a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 30894e6045f1SJohannes Berg */ 30904d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 30914d82a1deSPeter Zijlstra 30924d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 30934e6045f1SJohannes Berg #endif 3094807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 309585327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3096ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 309725511a47STejun Heo 30988930cabaSTejun Heo /* claim and dequeue */ 3099dc186ad7SThomas Gleixner debug_work_deactivate(work); 3100c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3101c34056a3STejun Heo worker->current_work = work; 3102a2c1c57bSTejun Heo worker->current_func = work->func; 3103112202d9STejun Heo worker->current_pwq = pwq; 31044cb1ef64STejun Heo if (worker->task) 3105616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3106c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3107d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 31087a22ad75STejun Heo 31098bf89593STejun Heo /* 31108bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 31118bf89593STejun Heo * overridden through set_worker_desc(). 31128bf89593STejun Heo */ 31138bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 31148bf89593STejun Heo 3115a62428c0STejun Heo list_del_init(&work->entry); 3116a62428c0STejun Heo 3117649027d7STejun Heo /* 3118228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3119228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3120228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3121228f1d00SLai Jiangshan * execution of the pending work items. 3122fb0e7bebSTejun Heo */ 3123616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3124228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3125fb0e7bebSTejun Heo 3126974271c4STejun Heo /* 31270219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 31280219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 31290219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 31300219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3131974271c4STejun Heo */ 31320219a352STejun Heo kick_pool(pool); 3133974271c4STejun Heo 31348930cabaSTejun Heo /* 31357c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3136d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 313723657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 313823657bb1STejun Heo * disabled. 31398930cabaSTejun Heo */ 31407c3eed5cSTejun Heo set_work_pool_and_clear_pending(work, pool->id); 31411da177e4SLinus Torvalds 3142fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3143a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3144365970a1SDavid Howells 3145c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3146c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 3147a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 31483295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3149e6f3faa7SPeter Zijlstra /* 3150f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3151f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3152e6f3faa7SPeter Zijlstra * 3153e6f3faa7SPeter Zijlstra * However, that would result in: 3154e6f3faa7SPeter Zijlstra * 3155e6f3faa7SPeter Zijlstra * A(W1) 3156e6f3faa7SPeter Zijlstra * WFC(C) 3157e6f3faa7SPeter Zijlstra * A(W1) 3158e6f3faa7SPeter Zijlstra * C(C) 3159e6f3faa7SPeter Zijlstra * 3160e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3161e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3162e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3163f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3164e6f3faa7SPeter Zijlstra * these locks. 3165e6f3faa7SPeter Zijlstra * 3166e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3167e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3168e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3169e6f3faa7SPeter Zijlstra */ 3170f52be570SPeter Zijlstra lockdep_invariant_state(true); 3171e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3172a2c1c57bSTejun Heo worker->current_func(work); 3173e36c886aSArjan van de Ven /* 3174e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3175e36c886aSArjan van de Ven * point will only record its address. 3176e36c886aSArjan van de Ven */ 31771c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3178725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 31793295f0efSIngo Molnar lock_map_release(&lockdep_map); 3180112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 31811da177e4SLinus Torvalds 3182c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3183c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3184c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3185c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3186c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3187c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3188c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3189c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3190c35aea39STejun Heo worker->current_func); 3191d5abe669SPeter Zijlstra debug_show_held_locks(current); 3192d5abe669SPeter Zijlstra dump_stack(); 3193d5abe669SPeter Zijlstra } 3194d5abe669SPeter Zijlstra 3195b22ce278STejun Heo /* 3196025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3197b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3198b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3199b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3200789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3201789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3202b22ce278STejun Heo */ 32034cb1ef64STejun Heo if (worker->task) 3204a7e6425eSPaul E. McKenney cond_resched(); 3205b22ce278STejun Heo 3206a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3207a62428c0STejun Heo 3208616db877STejun Heo /* 3209616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3210616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3211616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3212616db877STejun Heo */ 3213fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3214fb0e7bebSTejun Heo 32151b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 32161b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 32171b69ac6bSJohannes Weiner 3218a62428c0STejun Heo /* we're done with it, release */ 321942f8570fSSasha Levin hash_del(&worker->hentry); 3220c34056a3STejun Heo worker->current_work = NULL; 3221a2c1c57bSTejun Heo worker->current_func = NULL; 3222112202d9STejun Heo worker->current_pwq = NULL; 3223d812796eSLai Jiangshan worker->current_color = INT_MAX; 3224dd6c3c54STejun Heo 3225dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3226c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 32271da177e4SLinus Torvalds } 32281da177e4SLinus Torvalds 3229affee4b2STejun Heo /** 3230affee4b2STejun Heo * process_scheduled_works - process scheduled works 3231affee4b2STejun Heo * @worker: self 3232affee4b2STejun Heo * 3233affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3234affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3235affee4b2STejun Heo * fetches a work from the top and executes it. 3236affee4b2STejun Heo * 3237affee4b2STejun Heo * CONTEXT: 3238a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3239affee4b2STejun Heo * multiple times. 3240affee4b2STejun Heo */ 3241affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 32421da177e4SLinus Torvalds { 3243c0ab017dSTejun Heo struct work_struct *work; 3244c0ab017dSTejun Heo bool first = true; 3245c0ab017dSTejun Heo 3246c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3247c0ab017dSTejun Heo struct work_struct, entry))) { 3248c0ab017dSTejun Heo if (first) { 3249c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3250c0ab017dSTejun Heo first = false; 3251c0ab017dSTejun Heo } 3252c34056a3STejun Heo process_one_work(worker, work); 3253a62428c0STejun Heo } 32541da177e4SLinus Torvalds } 32551da177e4SLinus Torvalds 3256197f6accSTejun Heo static void set_pf_worker(bool val) 3257197f6accSTejun Heo { 3258197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3259197f6accSTejun Heo if (val) 3260197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3261197f6accSTejun Heo else 3262197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3263197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3264197f6accSTejun Heo } 3265197f6accSTejun Heo 32664690c4abSTejun Heo /** 32674690c4abSTejun Heo * worker_thread - the worker thread function 3268c34056a3STejun Heo * @__worker: self 32694690c4abSTejun Heo * 3270c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3271c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3272c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3273c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3274c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3275d185af30SYacine Belkadi * 3276d185af30SYacine Belkadi * Return: 0 32774690c4abSTejun Heo */ 3278c34056a3STejun Heo static int worker_thread(void *__worker) 32791da177e4SLinus Torvalds { 3280c34056a3STejun Heo struct worker *worker = __worker; 3281bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 32821da177e4SLinus Torvalds 3283e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3284197f6accSTejun Heo set_pf_worker(true); 3285c8e55f36STejun Heo woke_up: 3286a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3287affee4b2STejun Heo 3288a9ab775bSTejun Heo /* am I supposed to die? */ 3289a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3290a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3291197f6accSTejun Heo set_pf_worker(false); 329260f5a4bcSLai Jiangshan 329360f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3294e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3295a2d812a2STejun Heo worker_detach_from_pool(worker); 3296e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 329760f5a4bcSLai Jiangshan kfree(worker); 3298c8e55f36STejun Heo return 0; 3299c8e55f36STejun Heo } 3300c8e55f36STejun Heo 3301c8e55f36STejun Heo worker_leave_idle(worker); 3302db7bccf4STejun Heo recheck: 3303e22bee78STejun Heo /* no more worker necessary? */ 330463d95a91STejun Heo if (!need_more_worker(pool)) 3305e22bee78STejun Heo goto sleep; 3306e22bee78STejun Heo 3307e22bee78STejun Heo /* do we need to manage? */ 330863d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3309e22bee78STejun Heo goto recheck; 3310e22bee78STejun Heo 3311c8e55f36STejun Heo /* 3312c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3313c8e55f36STejun Heo * preparing to process a work or actually processing it. 3314c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3315c8e55f36STejun Heo */ 33166183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3317c8e55f36STejun Heo 3318e22bee78STejun Heo /* 3319a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3320a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3321a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3322a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3323a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3324e22bee78STejun Heo */ 3325a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3326e22bee78STejun Heo 3327e22bee78STejun Heo do { 3328affee4b2STejun Heo struct work_struct *work = 3329bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3330affee4b2STejun Heo struct work_struct, entry); 3331affee4b2STejun Heo 3332873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3333affee4b2STejun Heo process_scheduled_works(worker); 333463d95a91STejun Heo } while (keep_working(pool)); 3335affee4b2STejun Heo 3336228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3337d313dd85STejun Heo sleep: 3338c8e55f36STejun Heo /* 3339d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3340d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3341d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3342d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3343d565ed63STejun Heo * event. 3344c8e55f36STejun Heo */ 3345c8e55f36STejun Heo worker_enter_idle(worker); 3346c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3347a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 33481da177e4SLinus Torvalds schedule(); 3349c8e55f36STejun Heo goto woke_up; 33501da177e4SLinus Torvalds } 33511da177e4SLinus Torvalds 3352e22bee78STejun Heo /** 3353e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3354111c225aSTejun Heo * @__rescuer: self 3355e22bee78STejun Heo * 3356e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3357493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3358e22bee78STejun Heo * 3359706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3360e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3361e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3362e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3363e22bee78STejun Heo * the problem rescuer solves. 3364e22bee78STejun Heo * 3365706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3366706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3367e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3368e22bee78STejun Heo * 3369e22bee78STejun Heo * This should happen rarely. 3370d185af30SYacine Belkadi * 3371d185af30SYacine Belkadi * Return: 0 3372e22bee78STejun Heo */ 3373111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3374e22bee78STejun Heo { 3375111c225aSTejun Heo struct worker *rescuer = __rescuer; 3376111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 33774d595b86SLai Jiangshan bool should_stop; 3378e22bee78STejun Heo 3379e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3380111c225aSTejun Heo 3381111c225aSTejun Heo /* 3382111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3383111c225aSTejun Heo * doesn't participate in concurrency management. 3384111c225aSTejun Heo */ 3385197f6accSTejun Heo set_pf_worker(true); 3386e22bee78STejun Heo repeat: 3387c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 33881da177e4SLinus Torvalds 33894d595b86SLai Jiangshan /* 33904d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 33914d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 33924d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 33934d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 33944d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 33954d595b86SLai Jiangshan * list is always empty on exit. 33964d595b86SLai Jiangshan */ 33974d595b86SLai Jiangshan should_stop = kthread_should_stop(); 33981da177e4SLinus Torvalds 3399493a1724STejun Heo /* see whether any pwq is asking for help */ 3400a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3401493a1724STejun Heo 3402493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3403493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3404493a1724STejun Heo struct pool_workqueue, mayday_node); 3405112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3406e22bee78STejun Heo struct work_struct *work, *n; 3407e22bee78STejun Heo 3408e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3409493a1724STejun Heo list_del_init(&pwq->mayday_node); 3410493a1724STejun Heo 3411a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3412e22bee78STejun Heo 341351697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 341451697d39SLai Jiangshan 3415a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3416e22bee78STejun Heo 3417e22bee78STejun Heo /* 3418e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3419e22bee78STejun Heo * process'em. 3420e22bee78STejun Heo */ 3421873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 342282607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3423873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3424873eaca6STejun Heo assign_work(work, rescuer, &n)) 3425725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 342682607adcSTejun Heo } 3427e22bee78STejun Heo 3428873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3429e22bee78STejun Heo process_scheduled_works(rescuer); 34307576958aSTejun Heo 34317576958aSTejun Heo /* 3432008847f6SNeilBrown * The above execution of rescued work items could 3433008847f6SNeilBrown * have created more to rescue through 3434f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3435008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3436008847f6SNeilBrown * that such back-to-back work items, which may be 3437008847f6SNeilBrown * being used to relieve memory pressure, don't 3438008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3439008847f6SNeilBrown */ 34404f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3441a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3442e66b39afSTejun Heo /* 3443e66b39afSTejun Heo * Queue iff we aren't racing destruction 3444e66b39afSTejun Heo * and somebody else hasn't queued it already. 3445e66b39afSTejun Heo */ 3446e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3447008847f6SNeilBrown get_pwq(pwq); 3448e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3449e66b39afSTejun Heo } 3450a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3451008847f6SNeilBrown } 3452008847f6SNeilBrown } 3453008847f6SNeilBrown 3454008847f6SNeilBrown /* 345577668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 345613b1d625SLai Jiangshan * go away while we're still attached to it. 345777668c8bSLai Jiangshan */ 345877668c8bSLai Jiangshan put_pwq(pwq); 345977668c8bSLai Jiangshan 346077668c8bSLai Jiangshan /* 34610219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 34620219a352STejun Heo * with 0 concurrency and stalling the execution. 34637576958aSTejun Heo */ 34640219a352STejun Heo kick_pool(pool); 34657576958aSTejun Heo 3466a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 346713b1d625SLai Jiangshan 3468a2d812a2STejun Heo worker_detach_from_pool(rescuer); 346913b1d625SLai Jiangshan 3470a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 34711da177e4SLinus Torvalds } 34721da177e4SLinus Torvalds 3473a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3474493a1724STejun Heo 34754d595b86SLai Jiangshan if (should_stop) { 34764d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3477197f6accSTejun Heo set_pf_worker(false); 34784d595b86SLai Jiangshan return 0; 34794d595b86SLai Jiangshan } 34804d595b86SLai Jiangshan 3481111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3482111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3483e22bee78STejun Heo schedule(); 3484e22bee78STejun Heo goto repeat; 34851da177e4SLinus Torvalds } 34861da177e4SLinus Torvalds 34874cb1ef64STejun Heo static void bh_worker(struct worker *worker) 34884cb1ef64STejun Heo { 34894cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 34904cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 34914cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 34924cb1ef64STejun Heo 34934cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 34944cb1ef64STejun Heo worker_leave_idle(worker); 34954cb1ef64STejun Heo 34964cb1ef64STejun Heo /* 34974cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 34984cb1ef64STejun Heo * explanations on each step. 34994cb1ef64STejun Heo */ 35004cb1ef64STejun Heo if (!need_more_worker(pool)) 35014cb1ef64STejun Heo goto done; 35024cb1ef64STejun Heo 35034cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 35044cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 35054cb1ef64STejun Heo 35064cb1ef64STejun Heo do { 35074cb1ef64STejun Heo struct work_struct *work = 35084cb1ef64STejun Heo list_first_entry(&pool->worklist, 35094cb1ef64STejun Heo struct work_struct, entry); 35104cb1ef64STejun Heo 35114cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 35124cb1ef64STejun Heo process_scheduled_works(worker); 35134cb1ef64STejun Heo } while (keep_working(pool) && 35144cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 35154cb1ef64STejun Heo 35164cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 35174cb1ef64STejun Heo done: 35184cb1ef64STejun Heo worker_enter_idle(worker); 35194cb1ef64STejun Heo kick_pool(pool); 35204cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 35214cb1ef64STejun Heo } 35224cb1ef64STejun Heo 35234cb1ef64STejun Heo /* 35244cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 35254cb1ef64STejun Heo * 35264cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 35274cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 35284cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 35294cb1ef64STejun Heo * can be dropped. 35304cb1ef64STejun Heo * 35314cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 35324cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 35334cb1ef64STejun Heo */ 35344cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 35354cb1ef64STejun Heo { 35364cb1ef64STejun Heo struct worker_pool *pool = 35374cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 35384cb1ef64STejun Heo if (need_more_worker(pool)) 35394cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 35404cb1ef64STejun Heo } 35414cb1ef64STejun Heo 3542fca839c0STejun Heo /** 3543fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3544fca839c0STejun Heo * @target_wq: workqueue being flushed 3545fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3546fca839c0STejun Heo * 3547fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3548fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3549fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3550fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3551fca839c0STejun Heo * a deadlock. 3552fca839c0STejun Heo */ 3553fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3554fca839c0STejun Heo struct work_struct *target_work) 3555fca839c0STejun Heo { 3556fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3557fca839c0STejun Heo struct worker *worker; 3558fca839c0STejun Heo 3559fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3560fca839c0STejun Heo return; 3561fca839c0STejun Heo 3562fca839c0STejun Heo worker = current_wq_worker(); 3563fca839c0STejun Heo 3564fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3565d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3566fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 356723d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 356823d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3569d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3570fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3571fca839c0STejun Heo target_wq->name, target_func); 3572fca839c0STejun Heo } 3573fca839c0STejun Heo 3574fc2e4d70SOleg Nesterov struct wq_barrier { 3575fc2e4d70SOleg Nesterov struct work_struct work; 3576fc2e4d70SOleg Nesterov struct completion done; 35772607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3578fc2e4d70SOleg Nesterov }; 3579fc2e4d70SOleg Nesterov 3580fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3581fc2e4d70SOleg Nesterov { 3582fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3583fc2e4d70SOleg Nesterov complete(&barr->done); 3584fc2e4d70SOleg Nesterov } 3585fc2e4d70SOleg Nesterov 35864690c4abSTejun Heo /** 35874690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3588112202d9STejun Heo * @pwq: pwq to insert barrier into 35894690c4abSTejun Heo * @barr: wq_barrier to insert 3590affee4b2STejun Heo * @target: target work to attach @barr to 3591affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 35924690c4abSTejun Heo * 3593affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3594affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3595affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3596affee4b2STejun Heo * cpu. 3597affee4b2STejun Heo * 3598affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3599affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3600affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3601affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3602affee4b2STejun Heo * after a work with LINKED flag set. 3603affee4b2STejun Heo * 3604affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3605112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 36064690c4abSTejun Heo * 36074690c4abSTejun Heo * CONTEXT: 3608a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 36094690c4abSTejun Heo */ 3610112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3611affee4b2STejun Heo struct wq_barrier *barr, 3612affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3613fc2e4d70SOleg Nesterov { 36144cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3615d812796eSLai Jiangshan unsigned int work_flags = 0; 3616d812796eSLai Jiangshan unsigned int work_color; 3617affee4b2STejun Heo struct list_head *head; 3618affee4b2STejun Heo 3619dc186ad7SThomas Gleixner /* 3620d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3621dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3622dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3623dc186ad7SThomas Gleixner * might deadlock. 36244cb1ef64STejun Heo * 36254cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 36264cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 36274cb1ef64STejun Heo * usage". 3628dc186ad7SThomas Gleixner */ 36294cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 36304cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 363122df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 363252fa5bc5SBoqun Feng 3633fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3634fd1a5b04SByungchul Park 36352607d7a6STejun Heo barr->task = current; 363683c22520SOleg Nesterov 36375797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3638018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3639018f3a13SLai Jiangshan 3640affee4b2STejun Heo /* 3641affee4b2STejun Heo * If @target is currently being executed, schedule the 3642affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3643affee4b2STejun Heo */ 3644d812796eSLai Jiangshan if (worker) { 3645affee4b2STejun Heo head = worker->scheduled.next; 3646d812796eSLai Jiangshan work_color = worker->current_color; 3647d812796eSLai Jiangshan } else { 3648affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3649affee4b2STejun Heo 3650affee4b2STejun Heo head = target->entry.next; 3651affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3652d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3653d812796eSLai Jiangshan work_color = get_work_color(*bits); 3654affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3655affee4b2STejun Heo } 3656affee4b2STejun Heo 3657d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3658d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3659d812796eSLai Jiangshan 3660d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3661fc2e4d70SOleg Nesterov } 3662fc2e4d70SOleg Nesterov 366373f53c4aSTejun Heo /** 3664112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 366573f53c4aSTejun Heo * @wq: workqueue being flushed 366673f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 366773f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 366873f53c4aSTejun Heo * 3669112202d9STejun Heo * Prepare pwqs for workqueue flushing. 367073f53c4aSTejun Heo * 3671112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3672112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3673112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3674112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3675112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 367673f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 367773f53c4aSTejun Heo * 367873f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 367973f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 368073f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 368173f53c4aSTejun Heo * is returned. 368273f53c4aSTejun Heo * 3683112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 368473f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 368573f53c4aSTejun Heo * advanced to @work_color. 368673f53c4aSTejun Heo * 368773f53c4aSTejun Heo * CONTEXT: 36883c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 368973f53c4aSTejun Heo * 3690d185af30SYacine Belkadi * Return: 369173f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 369273f53c4aSTejun Heo * otherwise. 369373f53c4aSTejun Heo */ 3694112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 369573f53c4aSTejun Heo int flush_color, int work_color) 36961da177e4SLinus Torvalds { 369773f53c4aSTejun Heo bool wait = false; 369849e3cf44STejun Heo struct pool_workqueue *pwq; 36991da177e4SLinus Torvalds 370073f53c4aSTejun Heo if (flush_color >= 0) { 37016183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3702112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3703dc186ad7SThomas Gleixner } 370414441960SOleg Nesterov 370549e3cf44STejun Heo for_each_pwq(pwq, wq) { 3706112202d9STejun Heo struct worker_pool *pool = pwq->pool; 37071da177e4SLinus Torvalds 3708a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 370973f53c4aSTejun Heo 371073f53c4aSTejun Heo if (flush_color >= 0) { 37116183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 371273f53c4aSTejun Heo 3713112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3714112202d9STejun Heo pwq->flush_color = flush_color; 3715112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 371673f53c4aSTejun Heo wait = true; 37171da177e4SLinus Torvalds } 371873f53c4aSTejun Heo } 371973f53c4aSTejun Heo 372073f53c4aSTejun Heo if (work_color >= 0) { 37216183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3722112202d9STejun Heo pwq->work_color = work_color; 372373f53c4aSTejun Heo } 372473f53c4aSTejun Heo 3725a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 37261da177e4SLinus Torvalds } 37271da177e4SLinus Torvalds 3728112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 372973f53c4aSTejun Heo complete(&wq->first_flusher->done); 373073f53c4aSTejun Heo 373173f53c4aSTejun Heo return wait; 373283c22520SOleg Nesterov } 37331da177e4SLinus Torvalds 3734c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3735c35aea39STejun Heo { 37364cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 37374cb1ef64STejun Heo if (wq->flags & WQ_BH) 37384cb1ef64STejun Heo local_bh_disable(); 37394cb1ef64STejun Heo 3740c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3741c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 37424cb1ef64STejun Heo 37434cb1ef64STejun Heo if (wq->flags & WQ_BH) 37444cb1ef64STejun Heo local_bh_enable(); 37454cb1ef64STejun Heo #endif 3746c35aea39STejun Heo } 3747c35aea39STejun Heo 3748c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3749c35aea39STejun Heo struct workqueue_struct *wq) 3750c35aea39STejun Heo { 37514cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 37524cb1ef64STejun Heo if (wq->flags & WQ_BH) 37534cb1ef64STejun Heo local_bh_disable(); 37544cb1ef64STejun Heo 3755c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3756c35aea39STejun Heo lock_map_release(&work->lockdep_map); 37574cb1ef64STejun Heo 37584cb1ef64STejun Heo if (wq->flags & WQ_BH) 37594cb1ef64STejun Heo local_bh_enable(); 37604cb1ef64STejun Heo #endif 3761c35aea39STejun Heo } 3762c35aea39STejun Heo 37630fcb78c2SRolf Eike Beer /** 3764c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 37650fcb78c2SRolf Eike Beer * @wq: workqueue to flush 37661da177e4SLinus Torvalds * 3767c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3768c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 37691da177e4SLinus Torvalds */ 3770c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 37711da177e4SLinus Torvalds { 377273f53c4aSTejun Heo struct wq_flusher this_flusher = { 377373f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 377473f53c4aSTejun Heo .flush_color = -1, 3775fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 377673f53c4aSTejun Heo }; 377773f53c4aSTejun Heo int next_color; 3778b1f4ec17SOleg Nesterov 37793347fa09STejun Heo if (WARN_ON(!wq_online)) 37803347fa09STejun Heo return; 37813347fa09STejun Heo 3782c35aea39STejun Heo touch_wq_lockdep_map(wq); 378387915adcSJohannes Berg 37843c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 378573f53c4aSTejun Heo 378673f53c4aSTejun Heo /* 378773f53c4aSTejun Heo * Start-to-wait phase 378873f53c4aSTejun Heo */ 378973f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 379073f53c4aSTejun Heo 379173f53c4aSTejun Heo if (next_color != wq->flush_color) { 379273f53c4aSTejun Heo /* 379373f53c4aSTejun Heo * Color space is not full. The current work_color 379473f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 379573f53c4aSTejun Heo * by one. 379673f53c4aSTejun Heo */ 37976183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 379873f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 379973f53c4aSTejun Heo wq->work_color = next_color; 380073f53c4aSTejun Heo 380173f53c4aSTejun Heo if (!wq->first_flusher) { 380273f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 38036183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 380473f53c4aSTejun Heo 380573f53c4aSTejun Heo wq->first_flusher = &this_flusher; 380673f53c4aSTejun Heo 3807112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 380873f53c4aSTejun Heo wq->work_color)) { 380973f53c4aSTejun Heo /* nothing to flush, done */ 381073f53c4aSTejun Heo wq->flush_color = next_color; 381173f53c4aSTejun Heo wq->first_flusher = NULL; 381273f53c4aSTejun Heo goto out_unlock; 381373f53c4aSTejun Heo } 381473f53c4aSTejun Heo } else { 381573f53c4aSTejun Heo /* wait in queue */ 38166183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 381773f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3818112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 381973f53c4aSTejun Heo } 382073f53c4aSTejun Heo } else { 382173f53c4aSTejun Heo /* 382273f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 382373f53c4aSTejun Heo * The next flush completion will assign us 382473f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 382573f53c4aSTejun Heo */ 382673f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 382773f53c4aSTejun Heo } 382873f53c4aSTejun Heo 3829fca839c0STejun Heo check_flush_dependency(wq, NULL); 3830fca839c0STejun Heo 38313c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 383273f53c4aSTejun Heo 383373f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 383473f53c4aSTejun Heo 383573f53c4aSTejun Heo /* 383673f53c4aSTejun Heo * Wake-up-and-cascade phase 383773f53c4aSTejun Heo * 383873f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 383973f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 384073f53c4aSTejun Heo */ 384100d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 384273f53c4aSTejun Heo return; 384373f53c4aSTejun Heo 38443c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 384573f53c4aSTejun Heo 38464ce48b37STejun Heo /* we might have raced, check again with mutex held */ 38474ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 38484ce48b37STejun Heo goto out_unlock; 38494ce48b37STejun Heo 385000d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 385173f53c4aSTejun Heo 38526183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 38536183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 385473f53c4aSTejun Heo 385573f53c4aSTejun Heo while (true) { 385673f53c4aSTejun Heo struct wq_flusher *next, *tmp; 385773f53c4aSTejun Heo 385873f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 385973f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 386073f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 386173f53c4aSTejun Heo break; 386273f53c4aSTejun Heo list_del_init(&next->list); 386373f53c4aSTejun Heo complete(&next->done); 386473f53c4aSTejun Heo } 386573f53c4aSTejun Heo 38666183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 386773f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 386873f53c4aSTejun Heo 386973f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 387073f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 387173f53c4aSTejun Heo 387273f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 387373f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 387473f53c4aSTejun Heo /* 387573f53c4aSTejun Heo * Assign the same color to all overflowed 387673f53c4aSTejun Heo * flushers, advance work_color and append to 387773f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 387873f53c4aSTejun Heo * phase for these overflowed flushers. 387973f53c4aSTejun Heo */ 388073f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 388173f53c4aSTejun Heo tmp->flush_color = wq->work_color; 388273f53c4aSTejun Heo 388373f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 388473f53c4aSTejun Heo 388573f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 388673f53c4aSTejun Heo &wq->flusher_queue); 3887112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 388873f53c4aSTejun Heo } 388973f53c4aSTejun Heo 389073f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 38916183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 389273f53c4aSTejun Heo break; 389373f53c4aSTejun Heo } 389473f53c4aSTejun Heo 389573f53c4aSTejun Heo /* 389673f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3897112202d9STejun Heo * the new first flusher and arm pwqs. 389873f53c4aSTejun Heo */ 38996183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 39006183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 390173f53c4aSTejun Heo 390273f53c4aSTejun Heo list_del_init(&next->list); 390373f53c4aSTejun Heo wq->first_flusher = next; 390473f53c4aSTejun Heo 3905112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 390673f53c4aSTejun Heo break; 390773f53c4aSTejun Heo 390873f53c4aSTejun Heo /* 390973f53c4aSTejun Heo * Meh... this color is already done, clear first 391073f53c4aSTejun Heo * flusher and repeat cascading. 391173f53c4aSTejun Heo */ 391273f53c4aSTejun Heo wq->first_flusher = NULL; 391373f53c4aSTejun Heo } 391473f53c4aSTejun Heo 391573f53c4aSTejun Heo out_unlock: 39163c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 39171da177e4SLinus Torvalds } 3918c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 39191da177e4SLinus Torvalds 39209c5a2ba7STejun Heo /** 39219c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 39229c5a2ba7STejun Heo * @wq: workqueue to drain 39239c5a2ba7STejun Heo * 39249c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 39259c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 39269c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3927b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 39289c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 39299c5a2ba7STejun Heo * takes too long. 39309c5a2ba7STejun Heo */ 39319c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 39329c5a2ba7STejun Heo { 39339c5a2ba7STejun Heo unsigned int flush_cnt = 0; 393449e3cf44STejun Heo struct pool_workqueue *pwq; 39359c5a2ba7STejun Heo 39369c5a2ba7STejun Heo /* 39379c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 39389c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 3939618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 39409c5a2ba7STejun Heo */ 394187fc741eSLai Jiangshan mutex_lock(&wq->mutex); 39429c5a2ba7STejun Heo if (!wq->nr_drainers++) 3943618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 394487fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 39459c5a2ba7STejun Heo reflush: 3946c4f135d6STetsuo Handa __flush_workqueue(wq); 39479c5a2ba7STejun Heo 3948b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 394976af4d93STejun Heo 395049e3cf44STejun Heo for_each_pwq(pwq, wq) { 3951fa2563e4SThomas Tuttle bool drained; 39529c5a2ba7STejun Heo 3953a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 3954afa87ce8STejun Heo drained = pwq_is_empty(pwq); 3955a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 3956fa2563e4SThomas Tuttle 3957fa2563e4SThomas Tuttle if (drained) 39589c5a2ba7STejun Heo continue; 39599c5a2ba7STejun Heo 39609c5a2ba7STejun Heo if (++flush_cnt == 10 || 39619c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 3962e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 3963e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 396476af4d93STejun Heo 3965b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 39669c5a2ba7STejun Heo goto reflush; 39679c5a2ba7STejun Heo } 39689c5a2ba7STejun Heo 39699c5a2ba7STejun Heo if (!--wq->nr_drainers) 3970618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 397187fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 39729c5a2ba7STejun Heo } 39739c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 39749c5a2ba7STejun Heo 3975d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 3976d6e89786SJohannes Berg bool from_cancel) 3977baf59022STejun Heo { 3978baf59022STejun Heo struct worker *worker = NULL; 3979c9e7cf27STejun Heo struct worker_pool *pool; 3980112202d9STejun Heo struct pool_workqueue *pwq; 3981c35aea39STejun Heo struct workqueue_struct *wq; 3982baf59022STejun Heo 3983baf59022STejun Heo might_sleep(); 3984baf59022STejun Heo 398524acfb71SThomas Gleixner rcu_read_lock(); 3986fa1b54e6STejun Heo pool = get_work_pool(work); 3987fa1b54e6STejun Heo if (!pool) { 398824acfb71SThomas Gleixner rcu_read_unlock(); 3989fa1b54e6STejun Heo return false; 3990fa1b54e6STejun Heo } 3991fa1b54e6STejun Heo 3992a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 39930b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 3994112202d9STejun Heo pwq = get_work_pwq(work); 3995112202d9STejun Heo if (pwq) { 3996112202d9STejun Heo if (unlikely(pwq->pool != pool)) 3997baf59022STejun Heo goto already_gone; 3998606a5020STejun Heo } else { 3999c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 4000baf59022STejun Heo if (!worker) 4001baf59022STejun Heo goto already_gone; 4002112202d9STejun Heo pwq = worker->current_pwq; 4003606a5020STejun Heo } 4004baf59022STejun Heo 4005c35aea39STejun Heo wq = pwq->wq; 4006c35aea39STejun Heo check_flush_dependency(wq, work); 4007fca839c0STejun Heo 4008112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 4009a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4010baf59022STejun Heo 4011c35aea39STejun Heo touch_work_lockdep_map(work, wq); 4012c35aea39STejun Heo 4013e159489bSTejun Heo /* 4014a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 4015a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 4016a1d14934SPeter Zijlstra * 4017a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 4018a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 4019a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 4020a1d14934SPeter Zijlstra * forward progress. 4021e159489bSTejun Heo */ 4022c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 4023c35aea39STejun Heo touch_wq_lockdep_map(wq); 4024c35aea39STejun Heo 402524acfb71SThomas Gleixner rcu_read_unlock(); 4026baf59022STejun Heo return true; 4027baf59022STejun Heo already_gone: 4028a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 402924acfb71SThomas Gleixner rcu_read_unlock(); 4030baf59022STejun Heo return false; 4031baf59022STejun Heo } 4032baf59022STejun Heo 4033d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4034d6e89786SJohannes Berg { 4035d6e89786SJohannes Berg struct wq_barrier barr; 4036d6e89786SJohannes Berg 4037d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4038d6e89786SJohannes Berg return false; 4039d6e89786SJohannes Berg 40404d43d395STetsuo Handa if (WARN_ON(!work->func)) 40414d43d395STetsuo Handa return false; 40424d43d395STetsuo Handa 4043d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4044d6e89786SJohannes Berg wait_for_completion(&barr.done); 4045d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4046d6e89786SJohannes Berg return true; 4047d6e89786SJohannes Berg } else { 4048d6e89786SJohannes Berg return false; 4049d6e89786SJohannes Berg } 4050d6e89786SJohannes Berg } 4051d6e89786SJohannes Berg 4052db700897SOleg Nesterov /** 4053401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4054401a8d04STejun Heo * @work: the work to flush 4055db700897SOleg Nesterov * 4056606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4057606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4058401a8d04STejun Heo * 4059d185af30SYacine Belkadi * Return: 4060401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4061401a8d04STejun Heo * %false if it was already idle. 4062db700897SOleg Nesterov */ 4063401a8d04STejun Heo bool flush_work(struct work_struct *work) 4064db700897SOleg Nesterov { 4065d6e89786SJohannes Berg return __flush_work(work, false); 4066606a5020STejun Heo } 4067db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4068db700897SOleg Nesterov 4069cdc6e4b3STejun Heo /** 4070cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4071cdc6e4b3STejun Heo * @dwork: the delayed work to flush 4072cdc6e4b3STejun Heo * 4073cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for 4074cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only 4075cdc6e4b3STejun Heo * considers the last queueing instance of @dwork. 4076cdc6e4b3STejun Heo * 4077cdc6e4b3STejun Heo * Return: 4078cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution, 4079cdc6e4b3STejun Heo * %false if it was already idle. 4080cdc6e4b3STejun Heo */ 4081cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4082cdc6e4b3STejun Heo { 4083cdc6e4b3STejun Heo local_irq_disable(); 4084cdc6e4b3STejun Heo if (del_timer_sync(&dwork->timer)) 4085cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work); 4086cdc6e4b3STejun Heo local_irq_enable(); 4087cdc6e4b3STejun Heo return flush_work(&dwork->work); 4088cdc6e4b3STejun Heo } 4089cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4090cdc6e4b3STejun Heo 4091cdc6e4b3STejun Heo /** 4092cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 4093cdc6e4b3STejun Heo * @rwork: the rcu work to flush 4094cdc6e4b3STejun Heo * 4095cdc6e4b3STejun Heo * Return: 4096cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 4097cdc6e4b3STejun Heo * %false if it was already idle. 4098cdc6e4b3STejun Heo */ 4099cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork) 4100cdc6e4b3STejun Heo { 4101cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 4102cdc6e4b3STejun Heo rcu_barrier(); 4103cdc6e4b3STejun Heo flush_work(&rwork->work); 4104cdc6e4b3STejun Heo return true; 4105cdc6e4b3STejun Heo } else { 4106cdc6e4b3STejun Heo return flush_work(&rwork->work); 4107cdc6e4b3STejun Heo } 4108cdc6e4b3STejun Heo } 4109cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work); 4110cdc6e4b3STejun Heo 4111c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags) 4112cdc6e4b3STejun Heo { 4113c26e2f2eSTejun Heo unsigned long irq_flags; 4114cdc6e4b3STejun Heo int ret; 4115cdc6e4b3STejun Heo 4116cdc6e4b3STejun Heo do { 4117c5f5b942STejun Heo ret = try_to_grab_pending(work, cflags, &irq_flags); 4118cdc6e4b3STejun Heo } while (unlikely(ret == -EAGAIN)); 4119cdc6e4b3STejun Heo 4120cdc6e4b3STejun Heo if (unlikely(ret < 0)) 4121cdc6e4b3STejun Heo return false; 4122cdc6e4b3STejun Heo 4123cdc6e4b3STejun Heo set_work_pool_and_clear_pending(work, get_work_pool_id(work)); 4124c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4125cdc6e4b3STejun Heo return ret; 4126cdc6e4b3STejun Heo } 4127cdc6e4b3STejun Heo 41288603e1b3STejun Heo struct cwt_wait { 4129ac6424b9SIngo Molnar wait_queue_entry_t wait; 41308603e1b3STejun Heo struct work_struct *work; 41318603e1b3STejun Heo }; 41328603e1b3STejun Heo 4133ac6424b9SIngo Molnar static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 41348603e1b3STejun Heo { 41358603e1b3STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 41368603e1b3STejun Heo 41378603e1b3STejun Heo if (cwait->work != key) 41388603e1b3STejun Heo return 0; 41398603e1b3STejun Heo return autoremove_wake_function(wait, mode, sync, key); 41408603e1b3STejun Heo } 41418603e1b3STejun Heo 4142c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags) 4143401a8d04STejun Heo { 41448603e1b3STejun Heo static DECLARE_WAIT_QUEUE_HEAD(cancel_waitq); 4145c26e2f2eSTejun Heo unsigned long irq_flags; 41461f1f642eSOleg Nesterov int ret; 41471f1f642eSOleg Nesterov 41481f1f642eSOleg Nesterov do { 4149c5f5b942STejun Heo ret = try_to_grab_pending(work, cflags, &irq_flags); 4150bbb68dfaSTejun Heo /* 41518603e1b3STejun Heo * If someone else is already canceling, wait for it to 41528603e1b3STejun Heo * finish. flush_work() doesn't work for PREEMPT_NONE 41538603e1b3STejun Heo * because we may get scheduled between @work's completion 41548603e1b3STejun Heo * and the other canceling task resuming and clearing 41558603e1b3STejun Heo * CANCELING - flush_work() will return false immediately 41568603e1b3STejun Heo * as @work is no longer busy, try_to_grab_pending() will 41578603e1b3STejun Heo * return -ENOENT as @work is still being canceled and the 41588603e1b3STejun Heo * other canceling task won't be able to clear CANCELING as 41598603e1b3STejun Heo * we're hogging the CPU. 41608603e1b3STejun Heo * 41618603e1b3STejun Heo * Let's wait for completion using a waitqueue. As this 41628603e1b3STejun Heo * may lead to the thundering herd problem, use a custom 41638603e1b3STejun Heo * wake function which matches @work along with exclusive 41648603e1b3STejun Heo * wait and wakeup. 4165bbb68dfaSTejun Heo */ 41668603e1b3STejun Heo if (unlikely(ret == -ENOENT)) { 41678603e1b3STejun Heo struct cwt_wait cwait; 41688603e1b3STejun Heo 41698603e1b3STejun Heo init_wait(&cwait.wait); 41708603e1b3STejun Heo cwait.wait.func = cwt_wakefn; 41718603e1b3STejun Heo cwait.work = work; 41728603e1b3STejun Heo 41738603e1b3STejun Heo prepare_to_wait_exclusive(&cancel_waitq, &cwait.wait, 41748603e1b3STejun Heo TASK_UNINTERRUPTIBLE); 41758603e1b3STejun Heo if (work_is_canceling(work)) 41768603e1b3STejun Heo schedule(); 41778603e1b3STejun Heo finish_wait(&cancel_waitq, &cwait.wait); 41788603e1b3STejun Heo } 41791f1f642eSOleg Nesterov } while (unlikely(ret < 0)); 41801f1f642eSOleg Nesterov 4181bbb68dfaSTejun Heo /* tell other tasks trying to grab @work to back off */ 4182bbb68dfaSTejun Heo mark_work_canceling(work); 4183c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4184bbb68dfaSTejun Heo 41853347fa09STejun Heo /* 4186c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't 4187c7a40c49STejun Heo * executing. This allows canceling during early boot. 41883347fa09STejun Heo */ 41893347fa09STejun Heo if (wq_online) 4190d6e89786SJohannes Berg __flush_work(work, true); 41913347fa09STejun Heo 41927a22ad75STejun Heo clear_work_data(work); 41938603e1b3STejun Heo 41948603e1b3STejun Heo /* 41958603e1b3STejun Heo * Paired with prepare_to_wait() above so that either 41968603e1b3STejun Heo * waitqueue_active() is visible here or !work_is_canceling() is 41978603e1b3STejun Heo * visible there. 41988603e1b3STejun Heo */ 41998603e1b3STejun Heo smp_mb(); 42008603e1b3STejun Heo if (waitqueue_active(&cancel_waitq)) 42018603e1b3STejun Heo __wake_up(&cancel_waitq, TASK_NORMAL, 1, work); 42028603e1b3STejun Heo 42031f1f642eSOleg Nesterov return ret; 42041f1f642eSOleg Nesterov } 42051f1f642eSOleg Nesterov 4206cdc6e4b3STejun Heo /* 4207cdc6e4b3STejun Heo * See cancel_delayed_work() 4208cdc6e4b3STejun Heo */ 4209cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work) 4210cdc6e4b3STejun Heo { 4211c5f5b942STejun Heo return __cancel_work(work, 0); 4212cdc6e4b3STejun Heo } 4213cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work); 4214cdc6e4b3STejun Heo 42156e84d644SOleg Nesterov /** 4216401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4217401a8d04STejun Heo * @work: the work to cancel 42186e84d644SOleg Nesterov * 4219401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4220401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4221401a8d04STejun Heo * another workqueue. On return from this function, @work is 4222401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 42231f1f642eSOleg Nesterov * 4224401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4225401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 42266e84d644SOleg Nesterov * 4227401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 42286e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4229401a8d04STejun Heo * 4230d185af30SYacine Belkadi * Return: 4231401a8d04STejun Heo * %true if @work was pending, %false otherwise. 42326e84d644SOleg Nesterov */ 4233401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 42346e84d644SOleg Nesterov { 4235c5f5b942STejun Heo return __cancel_work_sync(work, 0); 4236b89deed3SOleg Nesterov } 423728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4238b89deed3SOleg Nesterov 42396e84d644SOleg Nesterov /** 424057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 424157b30ae7STejun Heo * @dwork: delayed_work to cancel 424209383498STejun Heo * 4243d185af30SYacine Belkadi * Kill off a pending delayed_work. 4244d185af30SYacine Belkadi * 4245d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4246d185af30SYacine Belkadi * pending. 4247d185af30SYacine Belkadi * 4248d185af30SYacine Belkadi * Note: 4249d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4250d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4251d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 425209383498STejun Heo * 425357b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 425409383498STejun Heo */ 425557b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 425609383498STejun Heo { 4257c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED); 425809383498STejun Heo } 425957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 426009383498STejun Heo 426109383498STejun Heo /** 4262401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4263401a8d04STejun Heo * @dwork: the delayed work cancel 4264401a8d04STejun Heo * 4265401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4266401a8d04STejun Heo * 4267d185af30SYacine Belkadi * Return: 4268401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4269401a8d04STejun Heo */ 4270401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 42716e84d644SOleg Nesterov { 4272c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED); 42736e84d644SOleg Nesterov } 4274f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 42751da177e4SLinus Torvalds 42760fcb78c2SRolf Eike Beer /** 427731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4278b6136773SAndrew Morton * @func: the function to call 4279b6136773SAndrew Morton * 428031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 428131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4282b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 428331ddd871STejun Heo * 4284d185af30SYacine Belkadi * Return: 428531ddd871STejun Heo * 0 on success, -errno on failure. 4286b6136773SAndrew Morton */ 428765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 428815316ba8SChristoph Lameter { 428915316ba8SChristoph Lameter int cpu; 429038f51568SNamhyung Kim struct work_struct __percpu *works; 429115316ba8SChristoph Lameter 4292b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4293b6136773SAndrew Morton if (!works) 429415316ba8SChristoph Lameter return -ENOMEM; 4295b6136773SAndrew Morton 4296ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 429793981800STejun Heo 429815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 42999bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 43009bfb1839SIngo Molnar 43019bfb1839SIngo Molnar INIT_WORK(work, func); 43028de6d308SOleg Nesterov schedule_work_on(cpu, work); 430315316ba8SChristoph Lameter } 430493981800STejun Heo 430593981800STejun Heo for_each_online_cpu(cpu) 43068616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 430793981800STejun Heo 4308ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4309b6136773SAndrew Morton free_percpu(works); 431015316ba8SChristoph Lameter return 0; 431115316ba8SChristoph Lameter } 431215316ba8SChristoph Lameter 4313eef6a7d5SAlan Stern /** 43141fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 43151fa44ecaSJames Bottomley * @fn: the function to execute 43161fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 43171fa44ecaSJames Bottomley * be available when the work executes) 43181fa44ecaSJames Bottomley * 43191fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 43201fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 43211fa44ecaSJames Bottomley * 4322d185af30SYacine Belkadi * Return: 0 - function was executed 43231fa44ecaSJames Bottomley * 1 - function was scheduled for execution 43241fa44ecaSJames Bottomley */ 432565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 43261fa44ecaSJames Bottomley { 43271fa44ecaSJames Bottomley if (!in_interrupt()) { 432865f27f38SDavid Howells fn(&ew->work); 43291fa44ecaSJames Bottomley return 0; 43301fa44ecaSJames Bottomley } 43311fa44ecaSJames Bottomley 433265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 43331fa44ecaSJames Bottomley schedule_work(&ew->work); 43341fa44ecaSJames Bottomley 43351fa44ecaSJames Bottomley return 1; 43361fa44ecaSJames Bottomley } 43371fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 43381fa44ecaSJames Bottomley 43397a4e344cSTejun Heo /** 43407a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 43417a4e344cSTejun Heo * @attrs: workqueue_attrs to free 43427a4e344cSTejun Heo * 43437a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 43447a4e344cSTejun Heo */ 4345513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 43467a4e344cSTejun Heo { 43477a4e344cSTejun Heo if (attrs) { 43487a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 43499546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 43507a4e344cSTejun Heo kfree(attrs); 43517a4e344cSTejun Heo } 43527a4e344cSTejun Heo } 43537a4e344cSTejun Heo 43547a4e344cSTejun Heo /** 43557a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 43567a4e344cSTejun Heo * 43577a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4358d185af30SYacine Belkadi * return it. 4359d185af30SYacine Belkadi * 4360d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 43617a4e344cSTejun Heo */ 4362513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 43637a4e344cSTejun Heo { 43647a4e344cSTejun Heo struct workqueue_attrs *attrs; 43657a4e344cSTejun Heo 4366be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 43677a4e344cSTejun Heo if (!attrs) 43687a4e344cSTejun Heo goto fail; 4369be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 43707a4e344cSTejun Heo goto fail; 43719546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 43729546b29eSTejun Heo goto fail; 43737a4e344cSTejun Heo 437413e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4375523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 43767a4e344cSTejun Heo return attrs; 43777a4e344cSTejun Heo fail: 43787a4e344cSTejun Heo free_workqueue_attrs(attrs); 43797a4e344cSTejun Heo return NULL; 43807a4e344cSTejun Heo } 43817a4e344cSTejun Heo 438229c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 438329c91e99STejun Heo const struct workqueue_attrs *from) 438429c91e99STejun Heo { 438529c91e99STejun Heo to->nice = from->nice; 438629c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 43879546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 43888639ecebSTejun Heo to->affn_strict = from->affn_strict; 438984193c07STejun Heo 43902865a8fbSShaohua Li /* 439184193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 439284193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 439384193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 43942865a8fbSShaohua Li */ 439584193c07STejun Heo to->affn_scope = from->affn_scope; 4396af73f5c9STejun Heo to->ordered = from->ordered; 439729c91e99STejun Heo } 439829c91e99STejun Heo 43995de7a03cSTejun Heo /* 44005de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 44015de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 44025de7a03cSTejun Heo */ 44035de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 44045de7a03cSTejun Heo { 440584193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 44065de7a03cSTejun Heo attrs->ordered = false; 44075de7a03cSTejun Heo } 44085de7a03cSTejun Heo 440929c91e99STejun Heo /* hash value of the content of @attr */ 441029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 441129c91e99STejun Heo { 441229c91e99STejun Heo u32 hash = 0; 441329c91e99STejun Heo 441429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 441513e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 441613e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 44179546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 44189546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 44198639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 442029c91e99STejun Heo return hash; 442129c91e99STejun Heo } 442229c91e99STejun Heo 442329c91e99STejun Heo /* content equality test */ 442429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 442529c91e99STejun Heo const struct workqueue_attrs *b) 442629c91e99STejun Heo { 442729c91e99STejun Heo if (a->nice != b->nice) 442829c91e99STejun Heo return false; 442929c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 443029c91e99STejun Heo return false; 44319546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 44329546b29eSTejun Heo return false; 44338639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 44348639ecebSTejun Heo return false; 443529c91e99STejun Heo return true; 443629c91e99STejun Heo } 443729c91e99STejun Heo 44380f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 44390f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 44400f36ee24STejun Heo const cpumask_t *unbound_cpumask) 44410f36ee24STejun Heo { 44420f36ee24STejun Heo /* 44430f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 44440f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 44450f36ee24STejun Heo * @unbound_cpumask. 44460f36ee24STejun Heo */ 44470f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 44480f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 44490f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 44500f36ee24STejun Heo } 44510f36ee24STejun Heo 445284193c07STejun Heo /* find wq_pod_type to use for @attrs */ 445384193c07STejun Heo static const struct wq_pod_type * 445484193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 445584193c07STejun Heo { 4456523a301eSTejun Heo enum wq_affn_scope scope; 4457523a301eSTejun Heo struct wq_pod_type *pt; 4458523a301eSTejun Heo 4459523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4460523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4461523a301eSTejun Heo 4462523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4463523a301eSTejun Heo scope = wq_affn_dfl; 4464523a301eSTejun Heo else 4465523a301eSTejun Heo scope = attrs->affn_scope; 4466523a301eSTejun Heo 4467523a301eSTejun Heo pt = &wq_pod_types[scope]; 446884193c07STejun Heo 446984193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 447084193c07STejun Heo likely(pt->nr_pods)) 447184193c07STejun Heo return pt; 447284193c07STejun Heo 447384193c07STejun Heo /* 447484193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 447584193c07STejun Heo * initialized in workqueue_init_early(). 447684193c07STejun Heo */ 447784193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 447884193c07STejun Heo BUG_ON(!pt->nr_pods); 447984193c07STejun Heo return pt; 448084193c07STejun Heo } 448184193c07STejun Heo 44827a4e344cSTejun Heo /** 44837a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 44847a4e344cSTejun Heo * @pool: worker_pool to initialize 44857a4e344cSTejun Heo * 4486402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4487d185af30SYacine Belkadi * 4488d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 448929c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 449029c91e99STejun Heo * on @pool safely to release it. 44917a4e344cSTejun Heo */ 44927a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 44934e1a1f9aSTejun Heo { 4494a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 449529c91e99STejun Heo pool->id = -1; 449629c91e99STejun Heo pool->cpu = -1; 4497f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 44984e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 449982607adcSTejun Heo pool->watchdog_ts = jiffies; 45004e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 45014e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 45024e1a1f9aSTejun Heo hash_init(pool->busy_hash); 45034e1a1f9aSTejun Heo 450432a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 45053f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 45064e1a1f9aSTejun Heo 450732a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 45084e1a1f9aSTejun Heo 4509da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4510e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 45117a4e344cSTejun Heo 45127cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 451329c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 451429c91e99STejun Heo pool->refcnt = 1; 451529c91e99STejun Heo 451629c91e99STejun Heo /* shouldn't fail above this point */ 4517be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 45187a4e344cSTejun Heo if (!pool->attrs) 45197a4e344cSTejun Heo return -ENOMEM; 45205de7a03cSTejun Heo 45215de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 45225de7a03cSTejun Heo 45237a4e344cSTejun Heo return 0; 45244e1a1f9aSTejun Heo } 45254e1a1f9aSTejun Heo 4526669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4527669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4528669de8bdSBart Van Assche { 4529669de8bdSBart Van Assche char *lock_name; 4530669de8bdSBart Van Assche 4531669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4532669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4533669de8bdSBart Van Assche if (!lock_name) 4534669de8bdSBart Van Assche lock_name = wq->name; 453569a106c0SQian Cai 453669a106c0SQian Cai wq->lock_name = lock_name; 4537669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4538669de8bdSBart Van Assche } 4539669de8bdSBart Van Assche 4540669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4541669de8bdSBart Van Assche { 4542669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4543669de8bdSBart Van Assche } 4544669de8bdSBart Van Assche 4545669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4546669de8bdSBart Van Assche { 4547669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4548669de8bdSBart Van Assche kfree(wq->lock_name); 4549669de8bdSBart Van Assche } 4550669de8bdSBart Van Assche #else 4551669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4552669de8bdSBart Van Assche { 4553669de8bdSBart Van Assche } 4554669de8bdSBart Van Assche 4555669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4556669de8bdSBart Van Assche { 4557669de8bdSBart Van Assche } 4558669de8bdSBart Van Assche 4559669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4560669de8bdSBart Van Assche { 4561669de8bdSBart Van Assche } 4562669de8bdSBart Van Assche #endif 4563669de8bdSBart Van Assche 456491ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 456591ccc6e7STejun Heo { 456691ccc6e7STejun Heo int node; 456791ccc6e7STejun Heo 456891ccc6e7STejun Heo for_each_node(node) { 456991ccc6e7STejun Heo kfree(nna_ar[node]); 457091ccc6e7STejun Heo nna_ar[node] = NULL; 457191ccc6e7STejun Heo } 457291ccc6e7STejun Heo 457391ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 457491ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 457591ccc6e7STejun Heo } 457691ccc6e7STejun Heo 457791ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 457891ccc6e7STejun Heo { 4579c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 458091ccc6e7STejun Heo atomic_set(&nna->nr, 0); 45815797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 45825797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 458391ccc6e7STejun Heo } 458491ccc6e7STejun Heo 458591ccc6e7STejun Heo /* 458691ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 458791ccc6e7STejun Heo * should be allocated in the node. 458891ccc6e7STejun Heo */ 458991ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 459091ccc6e7STejun Heo { 459191ccc6e7STejun Heo struct wq_node_nr_active *nna; 459291ccc6e7STejun Heo int node; 459391ccc6e7STejun Heo 459491ccc6e7STejun Heo for_each_node(node) { 459591ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 459691ccc6e7STejun Heo if (!nna) 459791ccc6e7STejun Heo goto err_free; 459891ccc6e7STejun Heo init_node_nr_active(nna); 459991ccc6e7STejun Heo nna_ar[node] = nna; 460091ccc6e7STejun Heo } 460191ccc6e7STejun Heo 460291ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 460391ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 460491ccc6e7STejun Heo if (!nna) 460591ccc6e7STejun Heo goto err_free; 460691ccc6e7STejun Heo init_node_nr_active(nna); 460791ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 460891ccc6e7STejun Heo 460991ccc6e7STejun Heo return 0; 461091ccc6e7STejun Heo 461191ccc6e7STejun Heo err_free: 461291ccc6e7STejun Heo free_node_nr_active(nna_ar); 461391ccc6e7STejun Heo return -ENOMEM; 461491ccc6e7STejun Heo } 461591ccc6e7STejun Heo 4616e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4617e2dca7adSTejun Heo { 4618e2dca7adSTejun Heo struct workqueue_struct *wq = 4619e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4620e2dca7adSTejun Heo 462191ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 462291ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 462391ccc6e7STejun Heo 4624669de8bdSBart Van Assche wq_free_lockdep(wq); 4625ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4626e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4627e2dca7adSTejun Heo kfree(wq); 4628e2dca7adSTejun Heo } 4629e2dca7adSTejun Heo 463029c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 463129c91e99STejun Heo { 463229c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 463329c91e99STejun Heo 46347cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 463529c91e99STejun Heo free_workqueue_attrs(pool->attrs); 463629c91e99STejun Heo kfree(pool); 463729c91e99STejun Heo } 463829c91e99STejun Heo 463929c91e99STejun Heo /** 464029c91e99STejun Heo * put_unbound_pool - put a worker_pool 464129c91e99STejun Heo * @pool: worker_pool to put 464229c91e99STejun Heo * 464324acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4644c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4645c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4646c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4647a892caccSTejun Heo * 4648a892caccSTejun Heo * Should be called with wq_pool_mutex held. 464929c91e99STejun Heo */ 465029c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 465129c91e99STejun Heo { 465260f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 465329c91e99STejun Heo struct worker *worker; 46549680540cSYang Yingliang LIST_HEAD(cull_list); 4655e02b9312SValentin Schneider 4656a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4657a892caccSTejun Heo 4658a892caccSTejun Heo if (--pool->refcnt) 465929c91e99STejun Heo return; 466029c91e99STejun Heo 466129c91e99STejun Heo /* sanity checks */ 466261d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4663a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 466429c91e99STejun Heo return; 466529c91e99STejun Heo 466629c91e99STejun Heo /* release id and unhash */ 466729c91e99STejun Heo if (pool->id >= 0) 466829c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 466929c91e99STejun Heo hash_del(&pool->hash_node); 467029c91e99STejun Heo 4671c5aa87bbSTejun Heo /* 4672692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4673692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4674692b4825STejun Heo * manager and @pool gets freed with the flag set. 46759ab03be4SValentin Schneider * 46769ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 46779ab03be4SValentin Schneider * only get here with 46789ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 46799ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 46809ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 46819ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 46829ab03be4SValentin Schneider * drops pool->lock 4683c5aa87bbSTejun Heo */ 46849ab03be4SValentin Schneider while (true) { 46859ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 46869ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4687d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4688e02b9312SValentin Schneider 4689e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 46909ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 46919ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4692692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 46939ab03be4SValentin Schneider break; 46949ab03be4SValentin Schneider } 46959ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4696e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 46979ab03be4SValentin Schneider } 4698692b4825STejun Heo 46991037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4700e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 470129c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4702a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 470360f5a4bcSLai Jiangshan 4704e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4705e02b9312SValentin Schneider 4706e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 470760f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 47081258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 470960f5a4bcSLai Jiangshan 471060f5a4bcSLai Jiangshan if (pool->detach_completion) 471160f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 471260f5a4bcSLai Jiangshan 471329c91e99STejun Heo /* shut down the timers */ 471429c91e99STejun Heo del_timer_sync(&pool->idle_timer); 47153f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 471629c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 471729c91e99STejun Heo 471824acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 471925b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 472029c91e99STejun Heo } 472129c91e99STejun Heo 472229c91e99STejun Heo /** 472329c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 472429c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 472529c91e99STejun Heo * 472629c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 472729c91e99STejun Heo * reference count and return it. If there already is a matching 472829c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4729d185af30SYacine Belkadi * create a new one. 4730a892caccSTejun Heo * 4731a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4732d185af30SYacine Belkadi * 4733d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4734d185af30SYacine Belkadi * On failure, %NULL. 473529c91e99STejun Heo */ 473629c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 473729c91e99STejun Heo { 473884193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 473929c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 474029c91e99STejun Heo struct worker_pool *pool; 474184193c07STejun Heo int pod, node = NUMA_NO_NODE; 474229c91e99STejun Heo 4743a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 474429c91e99STejun Heo 474529c91e99STejun Heo /* do we already have a matching pool? */ 474629c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 474729c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 474829c91e99STejun Heo pool->refcnt++; 47493fb1823cSLai Jiangshan return pool; 475029c91e99STejun Heo } 475129c91e99STejun Heo } 475229c91e99STejun Heo 47539546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 475484193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 47559546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 475684193c07STejun Heo node = pt->pod_node[pod]; 4757e2273584SXunlei Pang break; 4758e2273584SXunlei Pang } 4759e2273584SXunlei Pang } 4760e2273584SXunlei Pang 476129c91e99STejun Heo /* nope, create a new one */ 476284193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 476329c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 476429c91e99STejun Heo goto fail; 476529c91e99STejun Heo 476684193c07STejun Heo pool->node = node; 47675de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 47685de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 47692865a8fbSShaohua Li 477029c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 477129c91e99STejun Heo goto fail; 477229c91e99STejun Heo 477329c91e99STejun Heo /* create and start the initial worker */ 47743347fa09STejun Heo if (wq_online && !create_worker(pool)) 477529c91e99STejun Heo goto fail; 477629c91e99STejun Heo 477729c91e99STejun Heo /* install */ 477829c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 47793fb1823cSLai Jiangshan 478029c91e99STejun Heo return pool; 478129c91e99STejun Heo fail: 478229c91e99STejun Heo if (pool) 478329c91e99STejun Heo put_unbound_pool(pool); 478429c91e99STejun Heo return NULL; 478529c91e99STejun Heo } 478629c91e99STejun Heo 47878864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 47888864b4e5STejun Heo { 47898864b4e5STejun Heo kmem_cache_free(pwq_cache, 47908864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 47918864b4e5STejun Heo } 47928864b4e5STejun Heo 47938864b4e5STejun Heo /* 4794967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4795967b494eSTejun Heo * refcnt and needs to be destroyed. 47968864b4e5STejun Heo */ 4797687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 47988864b4e5STejun Heo { 47998864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4800687a9aa5STejun Heo release_work); 48018864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 48028864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4803b42b0bddSYang Yingliang bool is_last = false; 48048864b4e5STejun Heo 4805b42b0bddSYang Yingliang /* 4806687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4807b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4808b42b0bddSYang Yingliang */ 4809b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 48103c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 48118864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4812bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 48134c065dbcSWaiman Long 48144c065dbcSWaiman Long /* 48154c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 48164c065dbcSWaiman Long */ 48174c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 48184c065dbcSWaiman Long unplug_oldest_pwq(wq); 48194c065dbcSWaiman Long 48203c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4821b42b0bddSYang Yingliang } 48228864b4e5STejun Heo 4823687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4824a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 48258864b4e5STejun Heo put_unbound_pool(pool); 4826a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4827687a9aa5STejun Heo } 4828a892caccSTejun Heo 48295797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 48305797b1c1STejun Heo struct wq_node_nr_active *nna = 48315797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 48325797b1c1STejun Heo 48335797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 48345797b1c1STejun Heo list_del_init(&pwq->pending_node); 48355797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 48365797b1c1STejun Heo } 48375797b1c1STejun Heo 483825b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 48398864b4e5STejun Heo 48408864b4e5STejun Heo /* 48418864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4842e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 48438864b4e5STejun Heo */ 4844669de8bdSBart Van Assche if (is_last) { 4845669de8bdSBart Van Assche wq_unregister_lockdep(wq); 484625b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 48476029a918STejun Heo } 4848669de8bdSBart Van Assche } 48498864b4e5STejun Heo 485067dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4851f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4852f147f29eSTejun Heo struct worker_pool *pool) 4853d2c1d404STejun Heo { 4854*e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK); 4855d2c1d404STejun Heo 4856e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4857e50aba9aSTejun Heo 4858d2c1d404STejun Heo pwq->pool = pool; 4859d2c1d404STejun Heo pwq->wq = wq; 4860d2c1d404STejun Heo pwq->flush_color = -1; 48618864b4e5STejun Heo pwq->refcnt = 1; 4862f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 48635797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 48641befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4865d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4866687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4867f147f29eSTejun Heo } 4868d2c1d404STejun Heo 4869f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 48701befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4871f147f29eSTejun Heo { 4872f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4873f147f29eSTejun Heo 4874f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 487575ccf595STejun Heo 48761befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 48771befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 48781befcf30STejun Heo return; 48791befcf30STejun Heo 488029b1cb41SLai Jiangshan /* set the matching work_color */ 488175ccf595STejun Heo pwq->work_color = wq->work_color; 4882983ca25eSTejun Heo 4883983ca25eSTejun Heo /* link in @pwq */ 488426fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 4885df2d5ae4STejun Heo } 48866029a918STejun Heo 4887f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4888f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4889f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4890f147f29eSTejun Heo { 4891f147f29eSTejun Heo struct worker_pool *pool; 4892f147f29eSTejun Heo struct pool_workqueue *pwq; 4893f147f29eSTejun Heo 4894f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4895f147f29eSTejun Heo 4896f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4897f147f29eSTejun Heo if (!pool) 4898f147f29eSTejun Heo return NULL; 4899f147f29eSTejun Heo 4900e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4901f147f29eSTejun Heo if (!pwq) { 4902f147f29eSTejun Heo put_unbound_pool(pool); 4903f147f29eSTejun Heo return NULL; 4904f147f29eSTejun Heo } 4905f147f29eSTejun Heo 4906f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4907f147f29eSTejun Heo return pwq; 4908d2c1d404STejun Heo } 4909d2c1d404STejun Heo 49104c16bd32STejun Heo /** 4911fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4912042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 491384193c07STejun Heo * @cpu: the target CPU 49144c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 49154c16bd32STejun Heo * 4916fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4917fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 49189546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 49194c16bd32STejun Heo * 4920fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4921fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4922fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 49234c16bd32STejun Heo * 4924fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 49254c16bd32STejun Heo */ 49269546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 49279546b29eSTejun Heo int cpu_going_down) 49284c16bd32STejun Heo { 492984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 493084193c07STejun Heo int pod = pt->cpu_pod[cpu]; 49314c16bd32STejun Heo 4932fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 49339546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 49349546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 49354c16bd32STejun Heo if (cpu_going_down >= 0) 49369546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 49374c16bd32STejun Heo 49389546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 49399546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 494084193c07STejun Heo return; 494184193c07STejun Heo } 49424c16bd32STejun Heo 4943fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 49449546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 49451ad0f0a7SMichael Bringmann 49469546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 49471ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 49481ad0f0a7SMichael Bringmann "possible intersect\n"); 49494c16bd32STejun Heo } 49504c16bd32STejun Heo 49519f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4952636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4953636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 49541befcf30STejun Heo { 49559f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 49561befcf30STejun Heo struct pool_workqueue *old_pwq; 49571befcf30STejun Heo 49585b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 49591befcf30STejun Heo lockdep_assert_held(&wq->mutex); 49601befcf30STejun Heo 49611befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 49621befcf30STejun Heo link_pwq(pwq); 49631befcf30STejun Heo 49649f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 49659f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 49661befcf30STejun Heo return old_pwq; 49671befcf30STejun Heo } 49681befcf30STejun Heo 49692d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 49702d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 49712d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 49722d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4973042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 49742d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 49752d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 49762d5f0764SLai Jiangshan }; 49772d5f0764SLai Jiangshan 49782d5f0764SLai Jiangshan /* free the resources after success or abort */ 49792d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 49802d5f0764SLai Jiangshan { 49812d5f0764SLai Jiangshan if (ctx) { 4982636b927eSTejun Heo int cpu; 49832d5f0764SLai Jiangshan 4984636b927eSTejun Heo for_each_possible_cpu(cpu) 4985636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 49862d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 49872d5f0764SLai Jiangshan 49882d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 49892d5f0764SLai Jiangshan 49902d5f0764SLai Jiangshan kfree(ctx); 49912d5f0764SLai Jiangshan } 49922d5f0764SLai Jiangshan } 49932d5f0764SLai Jiangshan 49942d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 49952d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 49962d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 499799c621efSLai Jiangshan const struct workqueue_attrs *attrs, 499899c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 49992d5f0764SLai Jiangshan { 50002d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 50019546b29eSTejun Heo struct workqueue_attrs *new_attrs; 5002636b927eSTejun Heo int cpu; 50032d5f0764SLai Jiangshan 50042d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 50052d5f0764SLai Jiangshan 500684193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 500784193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 500884193c07STejun Heo return ERR_PTR(-EINVAL); 500984193c07STejun Heo 5010636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 50112d5f0764SLai Jiangshan 5012be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 50139546b29eSTejun Heo if (!ctx || !new_attrs) 50142d5f0764SLai Jiangshan goto out_free; 50152d5f0764SLai Jiangshan 5016042f7df1SLai Jiangshan /* 50172d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 50182d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 50192d5f0764SLai Jiangshan * it even if we don't use it immediately. 50202d5f0764SLai Jiangshan */ 50210f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 50220f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 50239546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50242d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 50252d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 50262d5f0764SLai Jiangshan goto out_free; 50272d5f0764SLai Jiangshan 5028636b927eSTejun Heo for_each_possible_cpu(cpu) { 5029af73f5c9STejun Heo if (new_attrs->ordered) { 50302d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5031636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5032636b927eSTejun Heo } else { 50339546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 50349546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5035636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5036636b927eSTejun Heo goto out_free; 50372d5f0764SLai Jiangshan } 50382d5f0764SLai Jiangshan } 50392d5f0764SLai Jiangshan 5040042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5041042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5042042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 50439546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50442d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5045042f7df1SLai Jiangshan 50464c065dbcSWaiman Long /* 50474c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 50484c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 50494c065dbcSWaiman Long * of newly queued work items until execution of older work items in 50504c065dbcSWaiman Long * the old pwq's have completed. 50514c065dbcSWaiman Long */ 50524c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 50534c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 50544c065dbcSWaiman Long 50552d5f0764SLai Jiangshan ctx->wq = wq; 50562d5f0764SLai Jiangshan return ctx; 50572d5f0764SLai Jiangshan 50582d5f0764SLai Jiangshan out_free: 50592d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 50602d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 506184193c07STejun Heo return ERR_PTR(-ENOMEM); 50622d5f0764SLai Jiangshan } 50632d5f0764SLai Jiangshan 50642d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 50652d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 50662d5f0764SLai Jiangshan { 5067636b927eSTejun Heo int cpu; 50682d5f0764SLai Jiangshan 50692d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 50702d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 50712d5f0764SLai Jiangshan 50722d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 50732d5f0764SLai Jiangshan 50749f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5075636b927eSTejun Heo for_each_possible_cpu(cpu) 5076636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5077636b927eSTejun Heo ctx->pwq_tbl[cpu]); 50789f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 50792d5f0764SLai Jiangshan 50805797b1c1STejun Heo /* update node_nr_active->max */ 50815797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 50825797b1c1STejun Heo 5083d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5084d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5085d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5086d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5087d64f2fa0SJuri Lelli 50882d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 50892d5f0764SLai Jiangshan } 50902d5f0764SLai Jiangshan 5091a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5092a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5093a0111cf6SLai Jiangshan { 5094a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5095a0111cf6SLai Jiangshan 5096a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5097a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5098a0111cf6SLai Jiangshan return -EINVAL; 5099a0111cf6SLai Jiangshan 510099c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 510184193c07STejun Heo if (IS_ERR(ctx)) 510284193c07STejun Heo return PTR_ERR(ctx); 5103a0111cf6SLai Jiangshan 5104a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5105a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5106a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5107a0111cf6SLai Jiangshan 51086201171eSwanghaibin return 0; 5109a0111cf6SLai Jiangshan } 5110a0111cf6SLai Jiangshan 51119e8cd2f5STejun Heo /** 51129e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 51139e8cd2f5STejun Heo * @wq: the target workqueue 51149e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 51159e8cd2f5STejun Heo * 5116fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5117fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5118fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5119fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5120fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 51219e8cd2f5STejun Heo * 5122d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5123d185af30SYacine Belkadi * 5124ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5125509b3204SDaniel Jordan * 5126d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 51279e8cd2f5STejun Heo */ 5128513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 51299e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 51309e8cd2f5STejun Heo { 5131a0111cf6SLai Jiangshan int ret; 51329e8cd2f5STejun Heo 5133509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5134509b3204SDaniel Jordan 5135509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5136a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5137509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 51382d5f0764SLai Jiangshan 51392d5f0764SLai Jiangshan return ret; 51409e8cd2f5STejun Heo } 51419e8cd2f5STejun Heo 51424c16bd32STejun Heo /** 5143fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 51444c16bd32STejun Heo * @wq: the target workqueue 51454cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 51464cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 51474c16bd32STejun Heo * @online: whether @cpu is coming up or going down 51484c16bd32STejun Heo * 51494c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5150fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 51514c16bd32STejun Heo * @wq accordingly. 51524c16bd32STejun Heo * 51534c16bd32STejun Heo * 5154fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5155fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5156fef59c9cSTejun Heo * 5157fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5158fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5159fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5160fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5161fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5162fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 51634c16bd32STejun Heo */ 5164fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 51654cbfd3deSTejun Heo int hotplug_cpu, bool online) 51664c16bd32STejun Heo { 51674cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 51684c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 51694c16bd32STejun Heo struct workqueue_attrs *target_attrs; 51704c16bd32STejun Heo 51714c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 51724c16bd32STejun Heo 517384193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 51744c16bd32STejun Heo return; 51754c16bd32STejun Heo 51764c16bd32STejun Heo /* 51774c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 51784c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 51794c16bd32STejun Heo * CPU hotplug exclusion. 51804c16bd32STejun Heo */ 5181fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 51824c16bd32STejun Heo 51834c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 51840f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 51854c16bd32STejun Heo 5186636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 51879546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 51889f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5189f7142ed4SLai Jiangshan return; 51904c16bd32STejun Heo 51914c16bd32STejun Heo /* create a new pwq */ 51924c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 51934c16bd32STejun Heo if (!pwq) { 5194fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 51954c16bd32STejun Heo wq->name); 519677f300b1SDaeseok Youn goto use_dfl_pwq; 51974c16bd32STejun Heo } 51984c16bd32STejun Heo 5199f7142ed4SLai Jiangshan /* Install the new pwq. */ 52004c16bd32STejun Heo mutex_lock(&wq->mutex); 5201636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 52024c16bd32STejun Heo goto out_unlock; 52034c16bd32STejun Heo 52044c16bd32STejun Heo use_dfl_pwq: 5205f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 52069f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 52079f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 52089f66cff2STejun Heo get_pwq(pwq); 52099f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 52109f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 52114c16bd32STejun Heo out_unlock: 52124c16bd32STejun Heo mutex_unlock(&wq->mutex); 52134c16bd32STejun Heo put_pwq_unlocked(old_pwq); 52144c16bd32STejun Heo } 52154c16bd32STejun Heo 521630cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 52171da177e4SLinus Torvalds { 521849e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 52198a2b7538STejun Heo int cpu, ret; 5220e1d8aa9fSFrederic Weisbecker 5221687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5222ee1ceef7STejun Heo if (!wq->cpu_pwq) 5223687a9aa5STejun Heo goto enomem; 522430cdf249STejun Heo 5225636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 522630cdf249STejun Heo for_each_possible_cpu(cpu) { 52274cb1ef64STejun Heo struct pool_workqueue **pwq_p; 52284cb1ef64STejun Heo struct worker_pool __percpu *pools; 52294cb1ef64STejun Heo struct worker_pool *pool; 52304cb1ef64STejun Heo 52314cb1ef64STejun Heo if (wq->flags & WQ_BH) 52324cb1ef64STejun Heo pools = bh_worker_pools; 52334cb1ef64STejun Heo else 52344cb1ef64STejun Heo pools = cpu_worker_pools; 52354cb1ef64STejun Heo 52364cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 52374cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 523830cdf249STejun Heo 5239687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5240687a9aa5STejun Heo pool->node); 5241687a9aa5STejun Heo if (!*pwq_p) 5242687a9aa5STejun Heo goto enomem; 5243687a9aa5STejun Heo 5244687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5245f147f29eSTejun Heo 5246f147f29eSTejun Heo mutex_lock(&wq->mutex); 5247687a9aa5STejun Heo link_pwq(*pwq_p); 5248f147f29eSTejun Heo mutex_unlock(&wq->mutex); 524930cdf249STejun Heo } 525030cdf249STejun Heo return 0; 5251509b3204SDaniel Jordan } 5252509b3204SDaniel Jordan 5253ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5254509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 52559f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 52569f66cff2STejun Heo 52578a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 52588a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 52599f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 52609f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 52619f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 52628a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 52639e8cd2f5STejun Heo } else { 5264509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 52659e8cd2f5STejun Heo } 5266ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5267509b3204SDaniel Jordan 526864344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 526964344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 527064344553SZqiang */ 527164344553SZqiang if (ret) 527264344553SZqiang kthread_flush_worker(pwq_release_worker); 527364344553SZqiang 5274509b3204SDaniel Jordan return ret; 5275687a9aa5STejun Heo 5276687a9aa5STejun Heo enomem: 5277687a9aa5STejun Heo if (wq->cpu_pwq) { 52787b42f401SZqiang for_each_possible_cpu(cpu) { 52797b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 52807b42f401SZqiang 52817b42f401SZqiang if (pwq) 52827b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 52837b42f401SZqiang } 5284687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5285687a9aa5STejun Heo wq->cpu_pwq = NULL; 5286687a9aa5STejun Heo } 5287687a9aa5STejun Heo return -ENOMEM; 52880f900049STejun Heo } 52890f900049STejun Heo 5290f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5291f3421797STejun Heo const char *name) 5292b71ab8c2STejun Heo { 5293636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5294044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5295636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5296b71ab8c2STejun Heo 5297636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5298b71ab8c2STejun Heo } 5299b71ab8c2STejun Heo 5300983c7515STejun Heo /* 5301983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5302983c7515STejun Heo * to guarantee forward progress. 5303983c7515STejun Heo */ 5304983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5305983c7515STejun Heo { 5306983c7515STejun Heo struct worker *rescuer; 5307b92b36eaSDan Carpenter int ret; 5308983c7515STejun Heo 5309983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5310983c7515STejun Heo return 0; 5311983c7515STejun Heo 5312983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 53134c0736a7SPetr Mladek if (!rescuer) { 53144c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 53154c0736a7SPetr Mladek wq->name); 5316983c7515STejun Heo return -ENOMEM; 53174c0736a7SPetr Mladek } 5318983c7515STejun Heo 5319983c7515STejun Heo rescuer->rescue_wq = wq; 5320b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5321f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5322b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 53234c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 53244c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5325983c7515STejun Heo kfree(rescuer); 5326b92b36eaSDan Carpenter return ret; 5327983c7515STejun Heo } 5328983c7515STejun Heo 5329983c7515STejun Heo wq->rescuer = rescuer; 533085f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 533149584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 533285f0ab43SJuri Lelli else 5333983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5334983c7515STejun Heo wake_up_process(rescuer->task); 5335983c7515STejun Heo 5336983c7515STejun Heo return 0; 5337983c7515STejun Heo } 5338983c7515STejun Heo 5339a045a272STejun Heo /** 5340a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5341a045a272STejun Heo * @wq: target workqueue 5342a045a272STejun Heo * 5343a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5344a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5345a045a272STejun Heo * @wq->max_active to zero. 5346a045a272STejun Heo */ 5347a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5348a045a272STejun Heo { 5349c5404d4eSTejun Heo bool activated; 53505797b1c1STejun Heo int new_max, new_min; 5351a045a272STejun Heo 5352a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5353a045a272STejun Heo 5354a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 53555797b1c1STejun Heo new_max = 0; 53565797b1c1STejun Heo new_min = 0; 53575797b1c1STejun Heo } else { 53585797b1c1STejun Heo new_max = wq->saved_max_active; 53595797b1c1STejun Heo new_min = wq->saved_min_active; 5360a045a272STejun Heo } 5361a045a272STejun Heo 53625797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5363a045a272STejun Heo return; 5364a045a272STejun Heo 5365a045a272STejun Heo /* 53665797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5367a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5368a045a272STejun Heo * because new work items are always queued behind existing inactive 5369a045a272STejun Heo * work items if there are any. 5370a045a272STejun Heo */ 53715797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 53725797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 53735797b1c1STejun Heo 53745797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 53755797b1c1STejun Heo wq_update_node_max_active(wq, -1); 53765797b1c1STejun Heo 53775797b1c1STejun Heo if (new_max == 0) 53785797b1c1STejun Heo return; 5379a045a272STejun Heo 5380c5404d4eSTejun Heo /* 5381c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5382c5404d4eSTejun Heo * until max_active is filled. 5383c5404d4eSTejun Heo */ 5384c5404d4eSTejun Heo do { 5385c5404d4eSTejun Heo struct pool_workqueue *pwq; 5386c5404d4eSTejun Heo 5387c5404d4eSTejun Heo activated = false; 5388a045a272STejun Heo for_each_pwq(pwq, wq) { 5389c26e2f2eSTejun Heo unsigned long irq_flags; 5390a045a272STejun Heo 5391c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5392c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 53935797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5394c5404d4eSTejun Heo activated = true; 5395a045a272STejun Heo kick_pool(pwq->pool); 5396c5404d4eSTejun Heo } 5397c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 5398a045a272STejun Heo } 5399c5404d4eSTejun Heo } while (activated); 5400a045a272STejun Heo } 5401a045a272STejun Heo 5402a2775bbcSMathieu Malaterre __printf(1, 4) 5403669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 540497e37d7bSTejun Heo unsigned int flags, 5405669de8bdSBart Van Assche int max_active, ...) 54063af24433SOleg Nesterov { 5407ecf6881fSTejun Heo va_list args; 54083af24433SOleg Nesterov struct workqueue_struct *wq; 540991ccc6e7STejun Heo size_t wq_size; 541091ccc6e7STejun Heo int name_len; 5411b196be89STejun Heo 54124cb1ef64STejun Heo if (flags & WQ_BH) { 54134cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 54144cb1ef64STejun Heo return NULL; 54154cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 54164cb1ef64STejun Heo return NULL; 54174cb1ef64STejun Heo } 54184cb1ef64STejun Heo 5419cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5420cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5421cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5422cee22a15SViresh Kumar 5423ecf6881fSTejun Heo /* allocate wq and format name */ 542491ccc6e7STejun Heo if (flags & WQ_UNBOUND) 542591ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 542691ccc6e7STejun Heo else 542791ccc6e7STejun Heo wq_size = sizeof(*wq); 542891ccc6e7STejun Heo 542991ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5430b196be89STejun Heo if (!wq) 5431d2c1d404STejun Heo return NULL; 5432b196be89STejun Heo 54336029a918STejun Heo if (flags & WQ_UNBOUND) { 5434be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 54356029a918STejun Heo if (!wq->unbound_attrs) 54366029a918STejun Heo goto err_free_wq; 54376029a918STejun Heo } 54386029a918STejun Heo 5439669de8bdSBart Van Assche va_start(args, max_active); 544091ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5441b196be89STejun Heo va_end(args); 54423af24433SOleg Nesterov 544391ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 544491ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 544591ccc6e7STejun Heo wq->name); 544631c89007SAudra Mitchell 54474cb1ef64STejun Heo if (flags & WQ_BH) { 54484cb1ef64STejun Heo /* 54494cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 54504cb1ef64STejun Heo * and don't impose any max_active limit. 54514cb1ef64STejun Heo */ 54524cb1ef64STejun Heo max_active = INT_MAX; 54534cb1ef64STejun Heo } else { 5454d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5455b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 54564cb1ef64STejun Heo } 54573af24433SOleg Nesterov 5458b196be89STejun Heo /* init wq */ 545997e37d7bSTejun Heo wq->flags = flags; 5460a045a272STejun Heo wq->max_active = max_active; 54615797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 54625797b1c1STejun Heo wq->saved_max_active = wq->max_active; 54635797b1c1STejun Heo wq->saved_min_active = wq->min_active; 54643c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5465112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 546630cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 546773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 546873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5469493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 54703af24433SOleg Nesterov 5471669de8bdSBart Van Assche wq_init_lockdep(wq); 5472cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 54733af24433SOleg Nesterov 547491ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 547591ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 547682efcab3SBart Van Assche goto err_unreg_lockdep; 547791ccc6e7STejun Heo } 547891ccc6e7STejun Heo 547991ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 548091ccc6e7STejun Heo goto err_free_node_nr_active; 54811537663fSTejun Heo 548240c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5483d2c1d404STejun Heo goto err_destroy; 5484e22bee78STejun Heo 5485226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5486226223abSTejun Heo goto err_destroy; 5487226223abSTejun Heo 54886af8bf3dSOleg Nesterov /* 548968e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 549068e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 549168e13a67SLai Jiangshan * list. 54926af8bf3dSOleg Nesterov */ 549368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5494a0a1a5fdSTejun Heo 5495a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5496a045a272STejun Heo wq_adjust_max_active(wq); 5497a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5498a0a1a5fdSTejun Heo 5499e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5500a0a1a5fdSTejun Heo 550168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55023af24433SOleg Nesterov 55033af24433SOleg Nesterov return wq; 5504d2c1d404STejun Heo 550591ccc6e7STejun Heo err_free_node_nr_active: 550691ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 550791ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 550882efcab3SBart Van Assche err_unreg_lockdep: 5509009bb421SBart Van Assche wq_unregister_lockdep(wq); 5510009bb421SBart Van Assche wq_free_lockdep(wq); 551182efcab3SBart Van Assche err_free_wq: 55126029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 55134690c4abSTejun Heo kfree(wq); 5514d2c1d404STejun Heo return NULL; 5515d2c1d404STejun Heo err_destroy: 5516d2c1d404STejun Heo destroy_workqueue(wq); 55174690c4abSTejun Heo return NULL; 55181da177e4SLinus Torvalds } 5519669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 55201da177e4SLinus Torvalds 5521c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5522c29eb853STejun Heo { 5523c29eb853STejun Heo int i; 5524c29eb853STejun Heo 5525c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5526c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5527c29eb853STejun Heo return true; 5528c29eb853STejun Heo 55299f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5530c29eb853STejun Heo return true; 5531afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5532c29eb853STejun Heo return true; 5533c29eb853STejun Heo 5534c29eb853STejun Heo return false; 5535c29eb853STejun Heo } 5536c29eb853STejun Heo 55373af24433SOleg Nesterov /** 55383af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 55393af24433SOleg Nesterov * @wq: target workqueue 55403af24433SOleg Nesterov * 55413af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 55423af24433SOleg Nesterov */ 55433af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 55443af24433SOleg Nesterov { 554549e3cf44STejun Heo struct pool_workqueue *pwq; 5546636b927eSTejun Heo int cpu; 55473af24433SOleg Nesterov 5548def98c84STejun Heo /* 5549def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5550def98c84STejun Heo * lead to sysfs name conflicts. 5551def98c84STejun Heo */ 5552def98c84STejun Heo workqueue_sysfs_unregister(wq); 5553def98c84STejun Heo 555433e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 555533e3f0a3SRichard Clark mutex_lock(&wq->mutex); 555633e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 555733e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 555833e3f0a3SRichard Clark 55599c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 55609c5a2ba7STejun Heo drain_workqueue(wq); 5561c8efcc25STejun Heo 5562def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5563def98c84STejun Heo if (wq->rescuer) { 5564def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5565def98c84STejun Heo 5566def98c84STejun Heo /* this prevents new queueing */ 5567a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5568def98c84STejun Heo wq->rescuer = NULL; 5569a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5570def98c84STejun Heo 5571def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5572def98c84STejun Heo kthread_stop(rescuer->task); 55738efe1223STejun Heo kfree(rescuer); 5574def98c84STejun Heo } 5575def98c84STejun Heo 5576c29eb853STejun Heo /* 5577c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5578c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5579c29eb853STejun Heo */ 5580c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5581b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 558249e3cf44STejun Heo for_each_pwq(pwq, wq) { 5583a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5584c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 55851d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5586e66b39afSTejun Heo __func__, wq->name); 5587c29eb853STejun Heo show_pwq(pwq); 5588a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5589b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5590c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 559155df0933SImran Khan show_one_workqueue(wq); 55926183c009STejun Heo return; 559376af4d93STejun Heo } 5594a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 559576af4d93STejun Heo } 5596b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 55976183c009STejun Heo 5598a0a1a5fdSTejun Heo /* 5599a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5600a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5601a0a1a5fdSTejun Heo */ 5602e2dca7adSTejun Heo list_del_rcu(&wq->list); 560368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 56043af24433SOleg Nesterov 56058864b4e5STejun Heo /* 5606636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5607636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5608636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 56098864b4e5STejun Heo */ 5610636b927eSTejun Heo rcu_read_lock(); 5611636b927eSTejun Heo 5612636b927eSTejun Heo for_each_possible_cpu(cpu) { 56139f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 56149f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 56154c16bd32STejun Heo } 56164c16bd32STejun Heo 56179f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 56189f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5619636b927eSTejun Heo 5620636b927eSTejun Heo rcu_read_unlock(); 56213af24433SOleg Nesterov } 56223af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 56233af24433SOleg Nesterov 5624dcd989cbSTejun Heo /** 5625dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5626dcd989cbSTejun Heo * @wq: target workqueue 5627dcd989cbSTejun Heo * @max_active: new max_active value. 5628dcd989cbSTejun Heo * 56295797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 56305797b1c1STejun Heo * comment. 5631dcd989cbSTejun Heo * 5632dcd989cbSTejun Heo * CONTEXT: 5633dcd989cbSTejun Heo * Don't call from IRQ context. 5634dcd989cbSTejun Heo */ 5635dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5636dcd989cbSTejun Heo { 56374cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 56384cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 56394cb1ef64STejun Heo return; 56408719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 56413bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 56428719dceaSTejun Heo return; 56438719dceaSTejun Heo 5644f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5645dcd989cbSTejun Heo 5646a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5647dcd989cbSTejun Heo 5648dcd989cbSTejun Heo wq->saved_max_active = max_active; 56495797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 56505797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 56515797b1c1STejun Heo 5652a045a272STejun Heo wq_adjust_max_active(wq); 5653dcd989cbSTejun Heo 5654a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5655dcd989cbSTejun Heo } 5656dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5657dcd989cbSTejun Heo 5658dcd989cbSTejun Heo /** 56598f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue 56608f172181STejun Heo * @wq: target unbound workqueue 56618f172181STejun Heo * @min_active: new min_active value 56628f172181STejun Heo * 56638f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an 56648f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active 56658f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be 56668f172181STejun Heo * able to process min_active number of interdependent work items which is 56678f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default. 56688f172181STejun Heo * 56698f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current 56708f172181STejun Heo * max_active. 56718f172181STejun Heo */ 56728f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) 56738f172181STejun Heo { 56748f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */ 56758f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != 56768f172181STejun Heo WQ_UNBOUND)) 56778f172181STejun Heo return; 56788f172181STejun Heo 56798f172181STejun Heo mutex_lock(&wq->mutex); 56808f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); 56818f172181STejun Heo wq_adjust_max_active(wq); 56828f172181STejun Heo mutex_unlock(&wq->mutex); 56838f172181STejun Heo } 56848f172181STejun Heo 56858f172181STejun Heo /** 568627d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 568727d4ee03SLukas Wunner * 568827d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 568927d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 569027d4ee03SLukas Wunner * 569127d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 569227d4ee03SLukas Wunner */ 569327d4ee03SLukas Wunner struct work_struct *current_work(void) 569427d4ee03SLukas Wunner { 569527d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 569627d4ee03SLukas Wunner 569727d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 569827d4ee03SLukas Wunner } 569927d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 570027d4ee03SLukas Wunner 570127d4ee03SLukas Wunner /** 5702e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5703e6267616STejun Heo * 5704e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5705e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5706d185af30SYacine Belkadi * 5707d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5708e6267616STejun Heo */ 5709e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5710e6267616STejun Heo { 5711e6267616STejun Heo struct worker *worker = current_wq_worker(); 5712e6267616STejun Heo 57136a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5714e6267616STejun Heo } 5715e6267616STejun Heo 5716e6267616STejun Heo /** 5717dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5718dcd989cbSTejun Heo * @cpu: CPU in question 5719dcd989cbSTejun Heo * @wq: target workqueue 5720dcd989cbSTejun Heo * 5721dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5722dcd989cbSTejun Heo * no synchronization around this function and the test result is 5723dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5724dcd989cbSTejun Heo * 5725d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5726636b927eSTejun Heo * 5727636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5728636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5729636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5730636b927eSTejun Heo * other CPUs. 5731d3251859STejun Heo * 5732d185af30SYacine Belkadi * Return: 5733dcd989cbSTejun Heo * %true if congested, %false otherwise. 5734dcd989cbSTejun Heo */ 5735d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5736dcd989cbSTejun Heo { 57377fb98ea7STejun Heo struct pool_workqueue *pwq; 573876af4d93STejun Heo bool ret; 573976af4d93STejun Heo 574024acfb71SThomas Gleixner rcu_read_lock(); 574124acfb71SThomas Gleixner preempt_disable(); 57427fb98ea7STejun Heo 5743d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5744d3251859STejun Heo cpu = smp_processor_id(); 5745d3251859STejun Heo 5746687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5747f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5748636b927eSTejun Heo 574924acfb71SThomas Gleixner preempt_enable(); 575024acfb71SThomas Gleixner rcu_read_unlock(); 575176af4d93STejun Heo 575276af4d93STejun Heo return ret; 5753dcd989cbSTejun Heo } 5754dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5755dcd989cbSTejun Heo 5756dcd989cbSTejun Heo /** 5757dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5758dcd989cbSTejun Heo * @work: the work to be tested 5759dcd989cbSTejun Heo * 5760dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5761dcd989cbSTejun Heo * synchronization around this function and the test result is 5762dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5763dcd989cbSTejun Heo * 5764d185af30SYacine Belkadi * Return: 5765dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5766dcd989cbSTejun Heo */ 5767dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5768dcd989cbSTejun Heo { 5769fa1b54e6STejun Heo struct worker_pool *pool; 5770c26e2f2eSTejun Heo unsigned long irq_flags; 5771dcd989cbSTejun Heo unsigned int ret = 0; 5772dcd989cbSTejun Heo 5773dcd989cbSTejun Heo if (work_pending(work)) 5774dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5775038366c5SLai Jiangshan 577624acfb71SThomas Gleixner rcu_read_lock(); 5777fa1b54e6STejun Heo pool = get_work_pool(work); 5778038366c5SLai Jiangshan if (pool) { 5779c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 5780c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5781dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5782c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 5783038366c5SLai Jiangshan } 578424acfb71SThomas Gleixner rcu_read_unlock(); 5785dcd989cbSTejun Heo 5786dcd989cbSTejun Heo return ret; 5787dcd989cbSTejun Heo } 5788dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5789dcd989cbSTejun Heo 57903d1cb205STejun Heo /** 57913d1cb205STejun Heo * set_worker_desc - set description for the current work item 57923d1cb205STejun Heo * @fmt: printf-style format string 57933d1cb205STejun Heo * @...: arguments for the format string 57943d1cb205STejun Heo * 57953d1cb205STejun Heo * This function can be called by a running work function to describe what 57963d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 57973d1cb205STejun Heo * information will be printed out together to help debugging. The 57983d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 57993d1cb205STejun Heo */ 58003d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 58013d1cb205STejun Heo { 58023d1cb205STejun Heo struct worker *worker = current_wq_worker(); 58033d1cb205STejun Heo va_list args; 58043d1cb205STejun Heo 58053d1cb205STejun Heo if (worker) { 58063d1cb205STejun Heo va_start(args, fmt); 58073d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 58083d1cb205STejun Heo va_end(args); 58093d1cb205STejun Heo } 58103d1cb205STejun Heo } 58115c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 58123d1cb205STejun Heo 58133d1cb205STejun Heo /** 58143d1cb205STejun Heo * print_worker_info - print out worker information and description 58153d1cb205STejun Heo * @log_lvl: the log level to use when printing 58163d1cb205STejun Heo * @task: target task 58173d1cb205STejun Heo * 58183d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 58193d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 58203d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 58213d1cb205STejun Heo * 58223d1cb205STejun Heo * This function can be safely called on any task as long as the 58233d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 58243d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 58253d1cb205STejun Heo */ 58263d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 58273d1cb205STejun Heo { 58283d1cb205STejun Heo work_func_t *fn = NULL; 58293d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 58303d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 58313d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 58323d1cb205STejun Heo struct workqueue_struct *wq = NULL; 58333d1cb205STejun Heo struct worker *worker; 58343d1cb205STejun Heo 58353d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 58363d1cb205STejun Heo return; 58373d1cb205STejun Heo 58383d1cb205STejun Heo /* 58393d1cb205STejun Heo * This function is called without any synchronization and @task 58403d1cb205STejun Heo * could be in any state. Be careful with dereferences. 58413d1cb205STejun Heo */ 5842e700591aSPetr Mladek worker = kthread_probe_data(task); 58433d1cb205STejun Heo 58443d1cb205STejun Heo /* 58458bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 58468bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 58473d1cb205STejun Heo */ 5848fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5849fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5850fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5851fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5852fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 58533d1cb205STejun Heo 58543d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5855d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 58568bf89593STejun Heo if (strcmp(name, desc)) 58573d1cb205STejun Heo pr_cont(" (%s)", desc); 58583d1cb205STejun Heo pr_cont("\n"); 58593d1cb205STejun Heo } 58603d1cb205STejun Heo } 58613d1cb205STejun Heo 58623494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 58633494fc30STejun Heo { 58643494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 58653494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 58663494fc30STejun Heo pr_cont(" node=%d", pool->node); 58674cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 58684cb1ef64STejun Heo if (pool->flags & POOL_BH) 58694cb1ef64STejun Heo pr_cont(" bh%s", 58704cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 58714cb1ef64STejun Heo else 58724cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 58734cb1ef64STejun Heo } 58744cb1ef64STejun Heo 58754cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 58764cb1ef64STejun Heo { 58774cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 58784cb1ef64STejun Heo 58794cb1ef64STejun Heo if (pool->flags & WQ_BH) 58804cb1ef64STejun Heo pr_cont("bh%s", 58814cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 58824cb1ef64STejun Heo else 58834cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 58844cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 58853494fc30STejun Heo } 58863494fc30STejun Heo 5887c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5888c76feb0dSPaul E. McKenney bool comma; 5889c76feb0dSPaul E. McKenney work_func_t func; 5890c76feb0dSPaul E. McKenney long ctr; 5891c76feb0dSPaul E. McKenney }; 5892c76feb0dSPaul E. McKenney 5893c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5894c76feb0dSPaul E. McKenney { 5895c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5896c76feb0dSPaul E. McKenney goto out_record; 5897c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5898c76feb0dSPaul E. McKenney pcwsp->ctr++; 5899c76feb0dSPaul E. McKenney return; 5900c76feb0dSPaul E. McKenney } 5901c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5902c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5903c76feb0dSPaul E. McKenney else 5904c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5905c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5906c76feb0dSPaul E. McKenney out_record: 5907c76feb0dSPaul E. McKenney if ((long)func == -1L) 5908c76feb0dSPaul E. McKenney return; 5909c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5910c76feb0dSPaul E. McKenney pcwsp->func = func; 5911c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5912c76feb0dSPaul E. McKenney } 5913c76feb0dSPaul E. McKenney 5914c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 59153494fc30STejun Heo { 59163494fc30STejun Heo if (work->func == wq_barrier_func) { 59173494fc30STejun Heo struct wq_barrier *barr; 59183494fc30STejun Heo 59193494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 59203494fc30STejun Heo 5921c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 59223494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 59233494fc30STejun Heo task_pid_nr(barr->task)); 59243494fc30STejun Heo } else { 5925c76feb0dSPaul E. McKenney if (!comma) 5926c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5927c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 59283494fc30STejun Heo } 59293494fc30STejun Heo } 59303494fc30STejun Heo 59313494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 59323494fc30STejun Heo { 5933c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 59343494fc30STejun Heo struct worker_pool *pool = pwq->pool; 59353494fc30STejun Heo struct work_struct *work; 59363494fc30STejun Heo struct worker *worker; 59373494fc30STejun Heo bool has_in_flight = false, has_pending = false; 59383494fc30STejun Heo int bkt; 59393494fc30STejun Heo 59403494fc30STejun Heo pr_info(" pwq %d:", pool->id); 59413494fc30STejun Heo pr_cont_pool_info(pool); 59423494fc30STejun Heo 5943a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5944a045a272STejun Heo pwq->nr_active, pwq->refcnt, 59453494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 59463494fc30STejun Heo 59473494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59483494fc30STejun Heo if (worker->current_pwq == pwq) { 59493494fc30STejun Heo has_in_flight = true; 59503494fc30STejun Heo break; 59513494fc30STejun Heo } 59523494fc30STejun Heo } 59533494fc30STejun Heo if (has_in_flight) { 59543494fc30STejun Heo bool comma = false; 59553494fc30STejun Heo 59563494fc30STejun Heo pr_info(" in-flight:"); 59573494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59583494fc30STejun Heo if (worker->current_pwq != pwq) 59593494fc30STejun Heo continue; 59603494fc30STejun Heo 59614cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 59624cb1ef64STejun Heo pr_cont_worker_id(worker); 59634cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 59643494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5965c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5966c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59673494fc30STejun Heo comma = true; 59683494fc30STejun Heo } 59693494fc30STejun Heo pr_cont("\n"); 59703494fc30STejun Heo } 59713494fc30STejun Heo 59723494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 59733494fc30STejun Heo if (get_work_pwq(work) == pwq) { 59743494fc30STejun Heo has_pending = true; 59753494fc30STejun Heo break; 59763494fc30STejun Heo } 59773494fc30STejun Heo } 59783494fc30STejun Heo if (has_pending) { 59793494fc30STejun Heo bool comma = false; 59803494fc30STejun Heo 59813494fc30STejun Heo pr_info(" pending:"); 59823494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 59833494fc30STejun Heo if (get_work_pwq(work) != pwq) 59843494fc30STejun Heo continue; 59853494fc30STejun Heo 5986c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 59873494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 59883494fc30STejun Heo } 5989c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59903494fc30STejun Heo pr_cont("\n"); 59913494fc30STejun Heo } 59923494fc30STejun Heo 5993f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 59943494fc30STejun Heo bool comma = false; 59953494fc30STejun Heo 5996f97a4a1aSLai Jiangshan pr_info(" inactive:"); 5997f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 5998c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 59993494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 60003494fc30STejun Heo } 6001c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60023494fc30STejun Heo pr_cont("\n"); 60033494fc30STejun Heo } 60043494fc30STejun Heo } 60053494fc30STejun Heo 60063494fc30STejun Heo /** 600755df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 600855df0933SImran Khan * @wq: workqueue whose state will be printed 60093494fc30STejun Heo */ 601055df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 60113494fc30STejun Heo { 60123494fc30STejun Heo struct pool_workqueue *pwq; 60133494fc30STejun Heo bool idle = true; 6014c26e2f2eSTejun Heo unsigned long irq_flags; 60153494fc30STejun Heo 60163494fc30STejun Heo for_each_pwq(pwq, wq) { 6017afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 60183494fc30STejun Heo idle = false; 60193494fc30STejun Heo break; 60203494fc30STejun Heo } 60213494fc30STejun Heo } 602255df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 602355df0933SImran Khan return; 60243494fc30STejun Heo 60253494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 60263494fc30STejun Heo 60273494fc30STejun Heo for_each_pwq(pwq, wq) { 6028c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 6029afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 603057116ce1SJohan Hovold /* 603157116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 603257116ce1SJohan Hovold * drivers that queue work while holding locks 603357116ce1SJohan Hovold * also taken in their write paths. 603457116ce1SJohan Hovold */ 603557116ce1SJohan Hovold printk_deferred_enter(); 60363494fc30STejun Heo show_pwq(pwq); 603757116ce1SJohan Hovold printk_deferred_exit(); 603857116ce1SJohan Hovold } 6039c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 604062635ea8SSergey Senozhatsky /* 604162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 604255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 604362635ea8SSergey Senozhatsky * hard lockup. 604462635ea8SSergey Senozhatsky */ 604562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 60463494fc30STejun Heo } 604755df0933SImran Khan 60483494fc30STejun Heo } 60493494fc30STejun Heo 605055df0933SImran Khan /** 605155df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 605255df0933SImran Khan * @pool: worker pool whose state will be printed 605355df0933SImran Khan */ 605455df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 605555df0933SImran Khan { 60563494fc30STejun Heo struct worker *worker; 60573494fc30STejun Heo bool first = true; 6058c26e2f2eSTejun Heo unsigned long irq_flags; 6059335a42ebSPetr Mladek unsigned long hung = 0; 60603494fc30STejun Heo 6061c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 60623494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 60633494fc30STejun Heo goto next_pool; 6064335a42ebSPetr Mladek 6065335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6066335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6067335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6068335a42ebSPetr Mladek 606957116ce1SJohan Hovold /* 607057116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 607157116ce1SJohan Hovold * queue work while holding locks also taken in their write 607257116ce1SJohan Hovold * paths. 607357116ce1SJohan Hovold */ 607457116ce1SJohan Hovold printk_deferred_enter(); 60753494fc30STejun Heo pr_info("pool %d:", pool->id); 60763494fc30STejun Heo pr_cont_pool_info(pool); 6077335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 60783494fc30STejun Heo if (pool->manager) 60793494fc30STejun Heo pr_cont(" manager: %d", 60803494fc30STejun Heo task_pid_nr(pool->manager->task)); 60813494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 60824cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 60834cb1ef64STejun Heo pr_cont_worker_id(worker); 60843494fc30STejun Heo first = false; 60853494fc30STejun Heo } 60863494fc30STejun Heo pr_cont("\n"); 608757116ce1SJohan Hovold printk_deferred_exit(); 60883494fc30STejun Heo next_pool: 6089c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 609062635ea8SSergey Senozhatsky /* 609162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 609255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 609362635ea8SSergey Senozhatsky * hard lockup. 609462635ea8SSergey Senozhatsky */ 609562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 609655df0933SImran Khan 60973494fc30STejun Heo } 60983494fc30STejun Heo 609955df0933SImran Khan /** 610055df0933SImran Khan * show_all_workqueues - dump workqueue state 610155df0933SImran Khan * 6102704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 610355df0933SImran Khan */ 610455df0933SImran Khan void show_all_workqueues(void) 610555df0933SImran Khan { 610655df0933SImran Khan struct workqueue_struct *wq; 610755df0933SImran Khan struct worker_pool *pool; 610855df0933SImran Khan int pi; 610955df0933SImran Khan 611055df0933SImran Khan rcu_read_lock(); 611155df0933SImran Khan 611255df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 611355df0933SImran Khan 611455df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 611555df0933SImran Khan show_one_workqueue(wq); 611655df0933SImran Khan 611755df0933SImran Khan for_each_pool(pool, pi) 611855df0933SImran Khan show_one_worker_pool(pool); 611955df0933SImran Khan 612024acfb71SThomas Gleixner rcu_read_unlock(); 61213494fc30STejun Heo } 61223494fc30STejun Heo 6123704bc669SJungseung Lee /** 6124704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6125704bc669SJungseung Lee * 6126704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6127704bc669SJungseung Lee * still busy. 6128704bc669SJungseung Lee */ 6129704bc669SJungseung Lee void show_freezable_workqueues(void) 6130704bc669SJungseung Lee { 6131704bc669SJungseung Lee struct workqueue_struct *wq; 6132704bc669SJungseung Lee 6133704bc669SJungseung Lee rcu_read_lock(); 6134704bc669SJungseung Lee 6135704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6136704bc669SJungseung Lee 6137704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6138704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6139704bc669SJungseung Lee continue; 6140704bc669SJungseung Lee show_one_workqueue(wq); 6141704bc669SJungseung Lee } 6142704bc669SJungseung Lee 6143704bc669SJungseung Lee rcu_read_unlock(); 6144704bc669SJungseung Lee } 6145704bc669SJungseung Lee 61466b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 61476b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 61486b59808bSTejun Heo { 61496b59808bSTejun Heo int off; 61506b59808bSTejun Heo 61516b59808bSTejun Heo /* always show the actual comm */ 61526b59808bSTejun Heo off = strscpy(buf, task->comm, size); 61536b59808bSTejun Heo if (off < 0) 61546b59808bSTejun Heo return; 61556b59808bSTejun Heo 6156197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 61576b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 61586b59808bSTejun Heo 6159197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6160197f6accSTejun Heo struct worker *worker = kthread_data(task); 6161197f6accSTejun Heo struct worker_pool *pool = worker->pool; 61626b59808bSTejun Heo 61636b59808bSTejun Heo if (pool) { 6164a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 61656b59808bSTejun Heo /* 6166197f6accSTejun Heo * ->desc tracks information (wq name or 6167197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6168197f6accSTejun Heo * current, prepend '+', otherwise '-'. 61696b59808bSTejun Heo */ 61706b59808bSTejun Heo if (worker->desc[0] != '\0') { 61716b59808bSTejun Heo if (worker->current_work) 61726b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 61736b59808bSTejun Heo worker->desc); 61746b59808bSTejun Heo else 61756b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 61766b59808bSTejun Heo worker->desc); 61776b59808bSTejun Heo } 6178a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 61796b59808bSTejun Heo } 6180197f6accSTejun Heo } 61816b59808bSTejun Heo 61826b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 61836b59808bSTejun Heo } 61846b59808bSTejun Heo 618566448bc2SMathieu Malaterre #ifdef CONFIG_SMP 618666448bc2SMathieu Malaterre 6187db7bccf4STejun Heo /* 6188db7bccf4STejun Heo * CPU hotplug. 6189db7bccf4STejun Heo * 6190e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6191112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6192706026c2STejun Heo * pool which make migrating pending and scheduled works very 6193e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 619494cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6195e22bee78STejun Heo * blocked draining impractical. 6196e22bee78STejun Heo * 619724647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6198628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6199628c78e7STejun Heo * cpu comes back online. 6200db7bccf4STejun Heo */ 6201db7bccf4STejun Heo 6202e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6203db7bccf4STejun Heo { 62044ce62e9eSTejun Heo struct worker_pool *pool; 6205db7bccf4STejun Heo struct worker *worker; 6206db7bccf4STejun Heo 6207f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 62081258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6209a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6210e22bee78STejun Heo 6211f2d5a0eeSTejun Heo /* 621292f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 621394cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 621411b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6215b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6216b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6217b4ac9384SLai Jiangshan * is on the same cpu. 6218f2d5a0eeSTejun Heo */ 6219da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6220403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6221db7bccf4STejun Heo 622224647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6223f2d5a0eeSTejun Heo 6224e22bee78STejun Heo /* 6225989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6226989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6227989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6228989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6229989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6230eb283428SLai Jiangshan * are served by workers tied to the pool. 6231e22bee78STejun Heo */ 6232bc35f7efSLai Jiangshan pool->nr_running = 0; 6233eb283428SLai Jiangshan 6234eb283428SLai Jiangshan /* 6235eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6236eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6237eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6238eb283428SLai Jiangshan */ 62390219a352STejun Heo kick_pool(pool); 6240989442d7SLai Jiangshan 6241a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6242989442d7SLai Jiangshan 6243793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6244793777bcSValentin Schneider unbind_worker(worker); 6245989442d7SLai Jiangshan 6246989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6247eb283428SLai Jiangshan } 6248db7bccf4STejun Heo } 6249db7bccf4STejun Heo 6250bd7c089eSTejun Heo /** 6251bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6252bd7c089eSTejun Heo * @pool: pool of interest 6253bd7c089eSTejun Heo * 6254a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6255bd7c089eSTejun Heo */ 6256bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6257bd7c089eSTejun Heo { 6258a9ab775bSTejun Heo struct worker *worker; 6259bd7c089eSTejun Heo 62601258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6261bd7c089eSTejun Heo 6262bd7c089eSTejun Heo /* 6263a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6264a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6265402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6266a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6267a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6268bd7c089eSTejun Heo */ 6269c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6270c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6271c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 62729546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6273c63a2e52SValentin Schneider } 6274a9ab775bSTejun Heo 6275a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6276f7c17d26SWanpeng Li 62773de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6278a9ab775bSTejun Heo 6279da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6280a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6281a9ab775bSTejun Heo 6282a9ab775bSTejun Heo /* 6283a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6284a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6285a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6286a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6287a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6288a9ab775bSTejun Heo * concurrency management. Note that when or whether 6289a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6290a9ab775bSTejun Heo * 6291c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6292a9ab775bSTejun Heo * tested without holding any lock in 62936d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6294a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6295a9ab775bSTejun Heo * management operations. 6296bd7c089eSTejun Heo */ 6297a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6298a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6299a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6300c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6301bd7c089eSTejun Heo } 6302a9ab775bSTejun Heo 6303a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6304bd7c089eSTejun Heo } 6305bd7c089eSTejun Heo 63067dbc725eSTejun Heo /** 63077dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 63087dbc725eSTejun Heo * @pool: unbound pool of interest 63097dbc725eSTejun Heo * @cpu: the CPU which is coming up 63107dbc725eSTejun Heo * 63117dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 63127dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 63137dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 63147dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 63157dbc725eSTejun Heo */ 63167dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 63177dbc725eSTejun Heo { 63187dbc725eSTejun Heo static cpumask_t cpumask; 63197dbc725eSTejun Heo struct worker *worker; 63207dbc725eSTejun Heo 63211258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 63227dbc725eSTejun Heo 63237dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 63247dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 63257dbc725eSTejun Heo return; 63267dbc725eSTejun Heo 63277dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 63287dbc725eSTejun Heo 63297dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6330da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6331d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 63327dbc725eSTejun Heo } 63337dbc725eSTejun Heo 63347ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 63351da177e4SLinus Torvalds { 63364ce62e9eSTejun Heo struct worker_pool *pool; 63371da177e4SLinus Torvalds 6338f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63393ce63377STejun Heo if (pool->nr_workers) 63403ce63377STejun Heo continue; 6341051e1850SLai Jiangshan if (!create_worker(pool)) 63427ee681b2SThomas Gleixner return -ENOMEM; 63433af24433SOleg Nesterov } 63447ee681b2SThomas Gleixner return 0; 63457ee681b2SThomas Gleixner } 63461da177e4SLinus Torvalds 63477ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 63487ee681b2SThomas Gleixner { 63497ee681b2SThomas Gleixner struct worker_pool *pool; 63507ee681b2SThomas Gleixner struct workqueue_struct *wq; 63517ee681b2SThomas Gleixner int pi; 63527ee681b2SThomas Gleixner 635368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63547dbc725eSTejun Heo 63557dbc725eSTejun Heo for_each_pool(pool, pi) { 63564cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 63574cb1ef64STejun Heo if (pool->flags & POOL_BH) 63584cb1ef64STejun Heo continue; 635994cf58bbSTejun Heo 63604cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6361f05b558dSLai Jiangshan if (pool->cpu == cpu) 636294cf58bbSTejun Heo rebind_workers(pool); 6363f05b558dSLai Jiangshan else if (pool->cpu < 0) 63647dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 63651258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 636694cf58bbSTejun Heo } 63677dbc725eSTejun Heo 6368fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 63694cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 637084193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 637184193c07STejun Heo 637284193c07STejun Heo if (attrs) { 637384193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 63744cbfd3deSTejun Heo int tcpu; 63754cbfd3deSTejun Heo 637684193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6377fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 63785797b1c1STejun Heo 63795797b1c1STejun Heo mutex_lock(&wq->mutex); 63805797b1c1STejun Heo wq_update_node_max_active(wq, -1); 63815797b1c1STejun Heo mutex_unlock(&wq->mutex); 63824cbfd3deSTejun Heo } 63834cbfd3deSTejun Heo } 63844c16bd32STejun Heo 638568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 63867ee681b2SThomas Gleixner return 0; 638765758202STejun Heo } 638865758202STejun Heo 63897ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 639065758202STejun Heo { 63914c16bd32STejun Heo struct workqueue_struct *wq; 63928db25e78STejun Heo 63934c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6394e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6395e8b3f8dbSLai Jiangshan return -1; 6396e8b3f8dbSLai Jiangshan 6397e8b3f8dbSLai Jiangshan unbind_workers(cpu); 63984c16bd32STejun Heo 6399fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 64004c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 64014cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 640284193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 640384193c07STejun Heo 640484193c07STejun Heo if (attrs) { 640584193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 64064cbfd3deSTejun Heo int tcpu; 64074cbfd3deSTejun Heo 640884193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6409fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 64105797b1c1STejun Heo 64115797b1c1STejun Heo mutex_lock(&wq->mutex); 64125797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 64135797b1c1STejun Heo mutex_unlock(&wq->mutex); 64144cbfd3deSTejun Heo } 64154cbfd3deSTejun Heo } 64164c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 64174c16bd32STejun Heo 64187ee681b2SThomas Gleixner return 0; 641965758202STejun Heo } 642065758202STejun Heo 64212d3854a3SRusty Russell struct work_for_cpu { 6422ed48ece2STejun Heo struct work_struct work; 64232d3854a3SRusty Russell long (*fn)(void *); 64242d3854a3SRusty Russell void *arg; 64252d3854a3SRusty Russell long ret; 64262d3854a3SRusty Russell }; 64272d3854a3SRusty Russell 6428ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 64292d3854a3SRusty Russell { 6430ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6431ed48ece2STejun Heo 64322d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 64332d3854a3SRusty Russell } 64342d3854a3SRusty Russell 64352d3854a3SRusty Russell /** 6436265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 64372d3854a3SRusty Russell * @cpu: the cpu to run on 64382d3854a3SRusty Russell * @fn: the function to run 64392d3854a3SRusty Russell * @arg: the function arg 6440265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64412d3854a3SRusty Russell * 644231ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 64436b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6444d185af30SYacine Belkadi * 6445d185af30SYacine Belkadi * Return: The value @fn returns. 64462d3854a3SRusty Russell */ 6447265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6448265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 64492d3854a3SRusty Russell { 6450ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 64512d3854a3SRusty Russell 6452265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6453ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 645412997d1aSBjorn Helgaas flush_work(&wfc.work); 6455440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 64562d3854a3SRusty Russell return wfc.ret; 64572d3854a3SRusty Russell } 6458265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 64590e8d6a93SThomas Gleixner 64600e8d6a93SThomas Gleixner /** 6461265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 64620e8d6a93SThomas Gleixner * @cpu: the cpu to run on 64630e8d6a93SThomas Gleixner * @fn: the function to run 64640e8d6a93SThomas Gleixner * @arg: the function argument 6465265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64660e8d6a93SThomas Gleixner * 64670e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 64680e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 64690e8d6a93SThomas Gleixner * 64700e8d6a93SThomas Gleixner * Return: The value @fn returns. 64710e8d6a93SThomas Gleixner */ 6472265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6473265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 64740e8d6a93SThomas Gleixner { 64750e8d6a93SThomas Gleixner long ret = -ENODEV; 64760e8d6a93SThomas Gleixner 6477ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 64780e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6479265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6480ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 64810e8d6a93SThomas Gleixner return ret; 64820e8d6a93SThomas Gleixner } 6483265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 64842d3854a3SRusty Russell #endif /* CONFIG_SMP */ 64852d3854a3SRusty Russell 6486a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6487e7577c50SRusty Russell 6488a0a1a5fdSTejun Heo /** 6489a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6490a0a1a5fdSTejun Heo * 649158a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6492f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6493706026c2STejun Heo * pool->worklist. 6494a0a1a5fdSTejun Heo * 6495a0a1a5fdSTejun Heo * CONTEXT: 6496a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6497a0a1a5fdSTejun Heo */ 6498a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6499a0a1a5fdSTejun Heo { 650024b8a847STejun Heo struct workqueue_struct *wq; 6501a0a1a5fdSTejun Heo 650268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6503a0a1a5fdSTejun Heo 65046183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6505a0a1a5fdSTejun Heo workqueue_freezing = true; 6506a0a1a5fdSTejun Heo 650724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6508a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6509a045a272STejun Heo wq_adjust_max_active(wq); 6510a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6511a1056305STejun Heo } 65125bcab335STejun Heo 651368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6514a0a1a5fdSTejun Heo } 6515a0a1a5fdSTejun Heo 6516a0a1a5fdSTejun Heo /** 651758a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6518a0a1a5fdSTejun Heo * 6519a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6520a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6521a0a1a5fdSTejun Heo * 6522a0a1a5fdSTejun Heo * CONTEXT: 652368e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6524a0a1a5fdSTejun Heo * 6525d185af30SYacine Belkadi * Return: 652658a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 652758a69cb4STejun Heo * is complete. 6528a0a1a5fdSTejun Heo */ 6529a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6530a0a1a5fdSTejun Heo { 6531a0a1a5fdSTejun Heo bool busy = false; 653224b8a847STejun Heo struct workqueue_struct *wq; 653324b8a847STejun Heo struct pool_workqueue *pwq; 6534a0a1a5fdSTejun Heo 653568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6536a0a1a5fdSTejun Heo 65376183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6538a0a1a5fdSTejun Heo 653924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 654024b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 654124b8a847STejun Heo continue; 6542a0a1a5fdSTejun Heo /* 6543a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6544a0a1a5fdSTejun Heo * to peek without lock. 6545a0a1a5fdSTejun Heo */ 654624acfb71SThomas Gleixner rcu_read_lock(); 654724b8a847STejun Heo for_each_pwq(pwq, wq) { 65486183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6549112202d9STejun Heo if (pwq->nr_active) { 6550a0a1a5fdSTejun Heo busy = true; 655124acfb71SThomas Gleixner rcu_read_unlock(); 6552a0a1a5fdSTejun Heo goto out_unlock; 6553a0a1a5fdSTejun Heo } 6554a0a1a5fdSTejun Heo } 655524acfb71SThomas Gleixner rcu_read_unlock(); 6556a0a1a5fdSTejun Heo } 6557a0a1a5fdSTejun Heo out_unlock: 655868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6559a0a1a5fdSTejun Heo return busy; 6560a0a1a5fdSTejun Heo } 6561a0a1a5fdSTejun Heo 6562a0a1a5fdSTejun Heo /** 6563a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6564a0a1a5fdSTejun Heo * 6565a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6566706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6567a0a1a5fdSTejun Heo * 6568a0a1a5fdSTejun Heo * CONTEXT: 6569a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6570a0a1a5fdSTejun Heo */ 6571a0a1a5fdSTejun Heo void thaw_workqueues(void) 6572a0a1a5fdSTejun Heo { 657324b8a847STejun Heo struct workqueue_struct *wq; 6574a0a1a5fdSTejun Heo 657568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6576a0a1a5fdSTejun Heo 6577a0a1a5fdSTejun Heo if (!workqueue_freezing) 6578a0a1a5fdSTejun Heo goto out_unlock; 6579a0a1a5fdSTejun Heo 658074b414eaSLai Jiangshan workqueue_freezing = false; 658124b8a847STejun Heo 658224b8a847STejun Heo /* restore max_active and repopulate worklist */ 658324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6584a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6585a045a272STejun Heo wq_adjust_max_active(wq); 6586a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 658724b8a847STejun Heo } 658824b8a847STejun Heo 6589a0a1a5fdSTejun Heo out_unlock: 659068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6591a0a1a5fdSTejun Heo } 6592a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6593a0a1a5fdSTejun Heo 659499c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6595042f7df1SLai Jiangshan { 6596042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6597042f7df1SLai Jiangshan int ret = 0; 6598042f7df1SLai Jiangshan struct workqueue_struct *wq; 6599042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6600042f7df1SLai Jiangshan 6601042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6602042f7df1SLai Jiangshan 6603042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 66048eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6605042f7df1SLai Jiangshan continue; 6606042f7df1SLai Jiangshan 660799c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 660884193c07STejun Heo if (IS_ERR(ctx)) { 660984193c07STejun Heo ret = PTR_ERR(ctx); 6610042f7df1SLai Jiangshan break; 6611042f7df1SLai Jiangshan } 6612042f7df1SLai Jiangshan 6613042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6614042f7df1SLai Jiangshan } 6615042f7df1SLai Jiangshan 6616042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6617042f7df1SLai Jiangshan if (!ret) 6618042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6619042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6620042f7df1SLai Jiangshan } 6621042f7df1SLai Jiangshan 662299c621efSLai Jiangshan if (!ret) { 662399c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 662499c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 662599c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 662699c621efSLai Jiangshan } 6627042f7df1SLai Jiangshan return ret; 6628042f7df1SLai Jiangshan } 6629042f7df1SLai Jiangshan 6630042f7df1SLai Jiangshan /** 6631fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6632fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6633fe28f631SWaiman Long * 6634fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6635fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6636fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6637fe28f631SWaiman Long */ 6638fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6639fe28f631SWaiman Long { 6640fe28f631SWaiman Long cpumask_var_t cpumask; 6641fe28f631SWaiman Long int ret = 0; 6642fe28f631SWaiman Long 6643fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6644fe28f631SWaiman Long return -ENOMEM; 6645fe28f631SWaiman Long 6646fe28f631SWaiman Long lockdep_assert_cpus_held(); 6647fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6648fe28f631SWaiman Long 6649fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6650fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6651fe28f631SWaiman Long 6652fe28f631SWaiman Long /* 6653fe28f631SWaiman Long * If the operation fails, it will fall back to 6654fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6655fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6656fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6657fe28f631SWaiman Long */ 6658fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6659fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6660fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6661fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6662fe28f631SWaiman Long 6663fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6664fe28f631SWaiman Long free_cpumask_var(cpumask); 6665fe28f631SWaiman Long return ret; 6666fe28f631SWaiman Long } 6667fe28f631SWaiman Long 666863c5484eSTejun Heo static int parse_affn_scope(const char *val) 666963c5484eSTejun Heo { 667063c5484eSTejun Heo int i; 667163c5484eSTejun Heo 667263c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 667363c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 667463c5484eSTejun Heo return i; 667563c5484eSTejun Heo } 667663c5484eSTejun Heo return -EINVAL; 667763c5484eSTejun Heo } 667863c5484eSTejun Heo 667963c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 668063c5484eSTejun Heo { 6681523a301eSTejun Heo struct workqueue_struct *wq; 6682523a301eSTejun Heo int affn, cpu; 668363c5484eSTejun Heo 668463c5484eSTejun Heo affn = parse_affn_scope(val); 668563c5484eSTejun Heo if (affn < 0) 668663c5484eSTejun Heo return affn; 6687523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6688523a301eSTejun Heo return -EINVAL; 6689523a301eSTejun Heo 6690523a301eSTejun Heo cpus_read_lock(); 6691523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 669263c5484eSTejun Heo 669363c5484eSTejun Heo wq_affn_dfl = affn; 6694523a301eSTejun Heo 6695523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6696523a301eSTejun Heo for_each_online_cpu(cpu) { 6697523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6698523a301eSTejun Heo } 6699523a301eSTejun Heo } 6700523a301eSTejun Heo 6701523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6702523a301eSTejun Heo cpus_read_unlock(); 6703523a301eSTejun Heo 670463c5484eSTejun Heo return 0; 670563c5484eSTejun Heo } 670663c5484eSTejun Heo 670763c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 670863c5484eSTejun Heo { 670963c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 671063c5484eSTejun Heo } 671163c5484eSTejun Heo 671263c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 671363c5484eSTejun Heo .set = wq_affn_dfl_set, 671463c5484eSTejun Heo .get = wq_affn_dfl_get, 671563c5484eSTejun Heo }; 671663c5484eSTejun Heo 671763c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 671863c5484eSTejun Heo 67196ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 67206ba94429SFrederic Weisbecker /* 67216ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 67226ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 67236ba94429SFrederic Weisbecker * following attributes. 67246ba94429SFrederic Weisbecker * 67256ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 67266ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 67276ba94429SFrederic Weisbecker * 67286ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 67296ba94429SFrederic Weisbecker * 67306ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 67316ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 673263c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 67338639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 67346ba94429SFrederic Weisbecker */ 67356ba94429SFrederic Weisbecker struct wq_device { 67366ba94429SFrederic Weisbecker struct workqueue_struct *wq; 67376ba94429SFrederic Weisbecker struct device dev; 67386ba94429SFrederic Weisbecker }; 67396ba94429SFrederic Weisbecker 67406ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 67416ba94429SFrederic Weisbecker { 67426ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 67436ba94429SFrederic Weisbecker 67446ba94429SFrederic Weisbecker return wq_dev->wq; 67456ba94429SFrederic Weisbecker } 67466ba94429SFrederic Weisbecker 67476ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 67486ba94429SFrederic Weisbecker char *buf) 67496ba94429SFrederic Weisbecker { 67506ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67516ba94429SFrederic Weisbecker 67526ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 67536ba94429SFrederic Weisbecker } 67546ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 67556ba94429SFrederic Weisbecker 67566ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 67576ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 67586ba94429SFrederic Weisbecker { 67596ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67606ba94429SFrederic Weisbecker 67616ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 67626ba94429SFrederic Weisbecker } 67636ba94429SFrederic Weisbecker 67646ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 67656ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 67666ba94429SFrederic Weisbecker size_t count) 67676ba94429SFrederic Weisbecker { 67686ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67696ba94429SFrederic Weisbecker int val; 67706ba94429SFrederic Weisbecker 67716ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 67726ba94429SFrederic Weisbecker return -EINVAL; 67736ba94429SFrederic Weisbecker 67746ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 67756ba94429SFrederic Weisbecker return count; 67766ba94429SFrederic Weisbecker } 67776ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 67786ba94429SFrederic Weisbecker 67796ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 67806ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 67816ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 67826ba94429SFrederic Weisbecker NULL, 67836ba94429SFrederic Weisbecker }; 67846ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 67856ba94429SFrederic Weisbecker 678649277a5bSWaiman Long static void apply_wqattrs_lock(void) 678749277a5bSWaiman Long { 678849277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 678949277a5bSWaiman Long cpus_read_lock(); 679049277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 679149277a5bSWaiman Long } 679249277a5bSWaiman Long 679349277a5bSWaiman Long static void apply_wqattrs_unlock(void) 679449277a5bSWaiman Long { 679549277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 679649277a5bSWaiman Long cpus_read_unlock(); 679749277a5bSWaiman Long } 679849277a5bSWaiman Long 67996ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 68006ba94429SFrederic Weisbecker char *buf) 68016ba94429SFrederic Weisbecker { 68026ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68036ba94429SFrederic Weisbecker int written; 68046ba94429SFrederic Weisbecker 68056ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68066ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 68076ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68086ba94429SFrederic Weisbecker 68096ba94429SFrederic Weisbecker return written; 68106ba94429SFrederic Weisbecker } 68116ba94429SFrederic Weisbecker 68126ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 68136ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 68146ba94429SFrederic Weisbecker { 68156ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 68166ba94429SFrederic Weisbecker 6817899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6818899a94feSLai Jiangshan 6819be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 68206ba94429SFrederic Weisbecker if (!attrs) 68216ba94429SFrederic Weisbecker return NULL; 68226ba94429SFrederic Weisbecker 68236ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 68246ba94429SFrederic Weisbecker return attrs; 68256ba94429SFrederic Weisbecker } 68266ba94429SFrederic Weisbecker 68276ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 68286ba94429SFrederic Weisbecker const char *buf, size_t count) 68296ba94429SFrederic Weisbecker { 68306ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68316ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6832d4d3e257SLai Jiangshan int ret = -ENOMEM; 6833d4d3e257SLai Jiangshan 6834d4d3e257SLai Jiangshan apply_wqattrs_lock(); 68356ba94429SFrederic Weisbecker 68366ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 68376ba94429SFrederic Weisbecker if (!attrs) 6838d4d3e257SLai Jiangshan goto out_unlock; 68396ba94429SFrederic Weisbecker 68406ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 68416ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6842d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 68436ba94429SFrederic Weisbecker else 68446ba94429SFrederic Weisbecker ret = -EINVAL; 68456ba94429SFrederic Weisbecker 6846d4d3e257SLai Jiangshan out_unlock: 6847d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 68486ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 68496ba94429SFrederic Weisbecker return ret ?: count; 68506ba94429SFrederic Weisbecker } 68516ba94429SFrederic Weisbecker 68526ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 68536ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 68546ba94429SFrederic Weisbecker { 68556ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68566ba94429SFrederic Weisbecker int written; 68576ba94429SFrederic Weisbecker 68586ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68596ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 68606ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 68616ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68626ba94429SFrederic Weisbecker return written; 68636ba94429SFrederic Weisbecker } 68646ba94429SFrederic Weisbecker 68656ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 68666ba94429SFrederic Weisbecker struct device_attribute *attr, 68676ba94429SFrederic Weisbecker const char *buf, size_t count) 68686ba94429SFrederic Weisbecker { 68696ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68706ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6871d4d3e257SLai Jiangshan int ret = -ENOMEM; 6872d4d3e257SLai Jiangshan 6873d4d3e257SLai Jiangshan apply_wqattrs_lock(); 68746ba94429SFrederic Weisbecker 68756ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 68766ba94429SFrederic Weisbecker if (!attrs) 6877d4d3e257SLai Jiangshan goto out_unlock; 68786ba94429SFrederic Weisbecker 68796ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 68806ba94429SFrederic Weisbecker if (!ret) 6881d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 68826ba94429SFrederic Weisbecker 6883d4d3e257SLai Jiangshan out_unlock: 6884d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 68856ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 68866ba94429SFrederic Weisbecker return ret ?: count; 68876ba94429SFrederic Weisbecker } 68886ba94429SFrederic Weisbecker 688963c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 689063c5484eSTejun Heo struct device_attribute *attr, char *buf) 689163c5484eSTejun Heo { 689263c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 689363c5484eSTejun Heo int written; 689463c5484eSTejun Heo 689563c5484eSTejun Heo mutex_lock(&wq->mutex); 6896523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6897523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6898523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6899523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6900523a301eSTejun Heo else 690163c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 690263c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 690363c5484eSTejun Heo mutex_unlock(&wq->mutex); 690463c5484eSTejun Heo 690563c5484eSTejun Heo return written; 690663c5484eSTejun Heo } 690763c5484eSTejun Heo 690863c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 690963c5484eSTejun Heo struct device_attribute *attr, 691063c5484eSTejun Heo const char *buf, size_t count) 691163c5484eSTejun Heo { 691263c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 691363c5484eSTejun Heo struct workqueue_attrs *attrs; 691463c5484eSTejun Heo int affn, ret = -ENOMEM; 691563c5484eSTejun Heo 691663c5484eSTejun Heo affn = parse_affn_scope(buf); 691763c5484eSTejun Heo if (affn < 0) 691863c5484eSTejun Heo return affn; 691963c5484eSTejun Heo 692063c5484eSTejun Heo apply_wqattrs_lock(); 692163c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 692263c5484eSTejun Heo if (attrs) { 692363c5484eSTejun Heo attrs->affn_scope = affn; 692463c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 692563c5484eSTejun Heo } 692663c5484eSTejun Heo apply_wqattrs_unlock(); 692763c5484eSTejun Heo free_workqueue_attrs(attrs); 692863c5484eSTejun Heo return ret ?: count; 692963c5484eSTejun Heo } 693063c5484eSTejun Heo 69318639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 69328639ecebSTejun Heo struct device_attribute *attr, char *buf) 69338639ecebSTejun Heo { 69348639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 69358639ecebSTejun Heo 69368639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 69378639ecebSTejun Heo wq->unbound_attrs->affn_strict); 69388639ecebSTejun Heo } 69398639ecebSTejun Heo 69408639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 69418639ecebSTejun Heo struct device_attribute *attr, 69428639ecebSTejun Heo const char *buf, size_t count) 69438639ecebSTejun Heo { 69448639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 69458639ecebSTejun Heo struct workqueue_attrs *attrs; 69468639ecebSTejun Heo int v, ret = -ENOMEM; 69478639ecebSTejun Heo 69488639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 69498639ecebSTejun Heo return -EINVAL; 69508639ecebSTejun Heo 69518639ecebSTejun Heo apply_wqattrs_lock(); 69528639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 69538639ecebSTejun Heo if (attrs) { 69548639ecebSTejun Heo attrs->affn_strict = (bool)v; 69558639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 69568639ecebSTejun Heo } 69578639ecebSTejun Heo apply_wqattrs_unlock(); 69588639ecebSTejun Heo free_workqueue_attrs(attrs); 69598639ecebSTejun Heo return ret ?: count; 69608639ecebSTejun Heo } 69618639ecebSTejun Heo 69626ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 69636ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 69646ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 696563c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 69668639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 69676ba94429SFrederic Weisbecker __ATTR_NULL, 69686ba94429SFrederic Weisbecker }; 69696ba94429SFrederic Weisbecker 69704f19b8e0STejun Heo static struct bus_type wq_subsys = { 69716ba94429SFrederic Weisbecker .name = "workqueue", 69726ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 69736ba94429SFrederic Weisbecker }; 69746ba94429SFrederic Weisbecker 697549277a5bSWaiman Long /** 697649277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 697749277a5bSWaiman Long * @cpumask: the cpumask to set 697849277a5bSWaiman Long * 697949277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 698049277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 698149277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 698249277a5bSWaiman Long * 698349277a5bSWaiman Long * Return: 0 - Success 698449277a5bSWaiman Long * -EINVAL - Invalid @cpumask 698549277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 698649277a5bSWaiman Long */ 698749277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 698849277a5bSWaiman Long { 698949277a5bSWaiman Long int ret = -EINVAL; 699049277a5bSWaiman Long 699149277a5bSWaiman Long /* 699249277a5bSWaiman Long * Not excluding isolated cpus on purpose. 699349277a5bSWaiman Long * If the user wishes to include them, we allow that. 699449277a5bSWaiman Long */ 699549277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 699649277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 699749277a5bSWaiman Long apply_wqattrs_lock(); 699849277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 699949277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 700049277a5bSWaiman Long ret = 0; 700149277a5bSWaiman Long goto out_unlock; 700249277a5bSWaiman Long } 700349277a5bSWaiman Long 700449277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 700549277a5bSWaiman Long 700649277a5bSWaiman Long out_unlock: 700749277a5bSWaiman Long apply_wqattrs_unlock(); 700849277a5bSWaiman Long } 700949277a5bSWaiman Long 701049277a5bSWaiman Long return ret; 701149277a5bSWaiman Long } 701249277a5bSWaiman Long 7013fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 7014fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 7015b05a7928SFrederic Weisbecker { 7016b05a7928SFrederic Weisbecker int written; 7017b05a7928SFrederic Weisbecker 7018042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 7019fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 7020042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 7021b05a7928SFrederic Weisbecker 7022b05a7928SFrederic Weisbecker return written; 7023b05a7928SFrederic Weisbecker } 7024b05a7928SFrederic Weisbecker 7025fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 7026fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7027fe28f631SWaiman Long { 7028fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 7029fe28f631SWaiman Long } 7030fe28f631SWaiman Long 7031fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 7032fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7033fe28f631SWaiman Long { 7034fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 7035fe28f631SWaiman Long } 7036fe28f631SWaiman Long 7037fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 7038fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7039fe28f631SWaiman Long { 7040fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 7041fe28f631SWaiman Long } 7042fe28f631SWaiman Long 7043042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 7044042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 7045042f7df1SLai Jiangshan { 7046042f7df1SLai Jiangshan cpumask_var_t cpumask; 7047042f7df1SLai Jiangshan int ret; 7048042f7df1SLai Jiangshan 7049042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 7050042f7df1SLai Jiangshan return -ENOMEM; 7051042f7df1SLai Jiangshan 7052042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7053042f7df1SLai Jiangshan if (!ret) 7054042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7055042f7df1SLai Jiangshan 7056042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7057042f7df1SLai Jiangshan return ret ? ret : count; 7058042f7df1SLai Jiangshan } 7059042f7df1SLai Jiangshan 7060fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7061042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7062fe28f631SWaiman Long wq_unbound_cpumask_store), 7063fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7064fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7065fe28f631SWaiman Long __ATTR_NULL, 7066fe28f631SWaiman Long }; 7067b05a7928SFrederic Weisbecker 70686ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 70696ba94429SFrederic Weisbecker { 7070686f6697SGreg Kroah-Hartman struct device *dev_root; 7071b05a7928SFrederic Weisbecker int err; 7072b05a7928SFrederic Weisbecker 7073b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7074b05a7928SFrederic Weisbecker if (err) 7075b05a7928SFrederic Weisbecker return err; 7076b05a7928SFrederic Weisbecker 7077686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7078686f6697SGreg Kroah-Hartman if (dev_root) { 7079fe28f631SWaiman Long struct device_attribute *attr; 7080fe28f631SWaiman Long 7081fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7082fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7083fe28f631SWaiman Long if (err) 7084fe28f631SWaiman Long break; 7085fe28f631SWaiman Long } 7086686f6697SGreg Kroah-Hartman put_device(dev_root); 7087686f6697SGreg Kroah-Hartman } 7088686f6697SGreg Kroah-Hartman return err; 70896ba94429SFrederic Weisbecker } 70906ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 70916ba94429SFrederic Weisbecker 70926ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 70936ba94429SFrederic Weisbecker { 70946ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 70956ba94429SFrederic Weisbecker 70966ba94429SFrederic Weisbecker kfree(wq_dev); 70976ba94429SFrederic Weisbecker } 70986ba94429SFrederic Weisbecker 70996ba94429SFrederic Weisbecker /** 71006ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 71016ba94429SFrederic Weisbecker * @wq: the workqueue to register 71026ba94429SFrederic Weisbecker * 71036ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 71046ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 71056ba94429SFrederic Weisbecker * which is the preferred method. 71066ba94429SFrederic Weisbecker * 71076ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 71086ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 71096ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 71106ba94429SFrederic Weisbecker * attributes. 71116ba94429SFrederic Weisbecker * 71126ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 71136ba94429SFrederic Weisbecker */ 71146ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 71156ba94429SFrederic Weisbecker { 71166ba94429SFrederic Weisbecker struct wq_device *wq_dev; 71176ba94429SFrederic Weisbecker int ret; 71186ba94429SFrederic Weisbecker 71196ba94429SFrederic Weisbecker /* 71204c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 71214c065dbcSWaiman Long * ordered workqueues. 71226ba94429SFrederic Weisbecker */ 71233bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 71246ba94429SFrederic Weisbecker return -EINVAL; 71256ba94429SFrederic Weisbecker 71266ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 71276ba94429SFrederic Weisbecker if (!wq_dev) 71286ba94429SFrederic Weisbecker return -ENOMEM; 71296ba94429SFrederic Weisbecker 71306ba94429SFrederic Weisbecker wq_dev->wq = wq; 71316ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 71326ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 713323217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 71346ba94429SFrederic Weisbecker 71356ba94429SFrederic Weisbecker /* 71366ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 71376ba94429SFrederic Weisbecker * everything is ready. 71386ba94429SFrederic Weisbecker */ 71396ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 71406ba94429SFrederic Weisbecker 71416ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 71426ba94429SFrederic Weisbecker if (ret) { 7143537f4146SArvind Yadav put_device(&wq_dev->dev); 71446ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71456ba94429SFrederic Weisbecker return ret; 71466ba94429SFrederic Weisbecker } 71476ba94429SFrederic Weisbecker 71486ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 71496ba94429SFrederic Weisbecker struct device_attribute *attr; 71506ba94429SFrederic Weisbecker 71516ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 71526ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 71536ba94429SFrederic Weisbecker if (ret) { 71546ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 71556ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71566ba94429SFrederic Weisbecker return ret; 71576ba94429SFrederic Weisbecker } 71586ba94429SFrederic Weisbecker } 71596ba94429SFrederic Weisbecker } 71606ba94429SFrederic Weisbecker 71616ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 71626ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 71636ba94429SFrederic Weisbecker return 0; 71646ba94429SFrederic Weisbecker } 71656ba94429SFrederic Weisbecker 71666ba94429SFrederic Weisbecker /** 71676ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 71686ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 71696ba94429SFrederic Weisbecker * 71706ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 71716ba94429SFrederic Weisbecker */ 71726ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 71736ba94429SFrederic Weisbecker { 71746ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 71756ba94429SFrederic Weisbecker 71766ba94429SFrederic Weisbecker if (!wq->wq_dev) 71776ba94429SFrederic Weisbecker return; 71786ba94429SFrederic Weisbecker 71796ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71806ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 71816ba94429SFrederic Weisbecker } 71826ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 71836ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 71846ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 71856ba94429SFrederic Weisbecker 718682607adcSTejun Heo /* 718782607adcSTejun Heo * Workqueue watchdog. 718882607adcSTejun Heo * 718982607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 719082607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 719182607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 719282607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 719382607adcSTejun Heo * largely opaque. 719482607adcSTejun Heo * 719582607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 719682607adcSTejun Heo * state if some pools failed to make forward progress for a while where 719782607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 719882607adcSTejun Heo * 719982607adcSTejun Heo * This mechanism is controlled through the kernel parameter 720082607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 720182607adcSTejun Heo * corresponding sysfs parameter file. 720282607adcSTejun Heo */ 720382607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 720482607adcSTejun Heo 720582607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 72065cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 720782607adcSTejun Heo 720882607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 720982607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 721082607adcSTejun Heo 7211cd2440d6SPetr Mladek /* 7212cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7213cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7214cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7215cd2440d6SPetr Mladek * in all other situations. 7216cd2440d6SPetr Mladek */ 7217cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7218cd2440d6SPetr Mladek { 7219cd2440d6SPetr Mladek struct worker *worker; 7220c26e2f2eSTejun Heo unsigned long irq_flags; 7221cd2440d6SPetr Mladek int bkt; 7222cd2440d6SPetr Mladek 7223c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 7224cd2440d6SPetr Mladek 7225cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7226cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7227cd2440d6SPetr Mladek /* 7228cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7229cd2440d6SPetr Mladek * drivers that queue work while holding locks 7230cd2440d6SPetr Mladek * also taken in their write paths. 7231cd2440d6SPetr Mladek */ 7232cd2440d6SPetr Mladek printk_deferred_enter(); 7233cd2440d6SPetr Mladek 7234cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7235cd2440d6SPetr Mladek sched_show_task(worker->task); 7236cd2440d6SPetr Mladek 7237cd2440d6SPetr Mladek printk_deferred_exit(); 7238cd2440d6SPetr Mladek } 7239cd2440d6SPetr Mladek } 7240cd2440d6SPetr Mladek 7241c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 7242cd2440d6SPetr Mladek } 7243cd2440d6SPetr Mladek 7244cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7245cd2440d6SPetr Mladek { 7246cd2440d6SPetr Mladek struct worker_pool *pool; 7247cd2440d6SPetr Mladek int pi; 7248cd2440d6SPetr Mladek 7249cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7250cd2440d6SPetr Mladek 7251cd2440d6SPetr Mladek rcu_read_lock(); 7252cd2440d6SPetr Mladek 7253cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7254cd2440d6SPetr Mladek if (pool->cpu_stall) 7255cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7256cd2440d6SPetr Mladek 7257cd2440d6SPetr Mladek } 7258cd2440d6SPetr Mladek 7259cd2440d6SPetr Mladek rcu_read_unlock(); 7260cd2440d6SPetr Mladek } 7261cd2440d6SPetr Mladek 726282607adcSTejun Heo static void wq_watchdog_reset_touched(void) 726382607adcSTejun Heo { 726482607adcSTejun Heo int cpu; 726582607adcSTejun Heo 726682607adcSTejun Heo wq_watchdog_touched = jiffies; 726782607adcSTejun Heo for_each_possible_cpu(cpu) 726882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 726982607adcSTejun Heo } 727082607adcSTejun Heo 72715cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 727282607adcSTejun Heo { 727382607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 727482607adcSTejun Heo bool lockup_detected = false; 7275cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7276940d71c6SSergey Senozhatsky unsigned long now = jiffies; 727782607adcSTejun Heo struct worker_pool *pool; 727882607adcSTejun Heo int pi; 727982607adcSTejun Heo 728082607adcSTejun Heo if (!thresh) 728182607adcSTejun Heo return; 728282607adcSTejun Heo 728382607adcSTejun Heo rcu_read_lock(); 728482607adcSTejun Heo 728582607adcSTejun Heo for_each_pool(pool, pi) { 728682607adcSTejun Heo unsigned long pool_ts, touched, ts; 728782607adcSTejun Heo 7288cd2440d6SPetr Mladek pool->cpu_stall = false; 728982607adcSTejun Heo if (list_empty(&pool->worklist)) 729082607adcSTejun Heo continue; 729182607adcSTejun Heo 7292940d71c6SSergey Senozhatsky /* 7293940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7294940d71c6SSergey Senozhatsky * the watchdog like a stall. 7295940d71c6SSergey Senozhatsky */ 7296940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7297940d71c6SSergey Senozhatsky 729882607adcSTejun Heo /* get the latest of pool and touched timestamps */ 729989e28ce6SWang Qing if (pool->cpu >= 0) 730089e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 730189e28ce6SWang Qing else 730282607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 730389e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 730482607adcSTejun Heo 730582607adcSTejun Heo if (time_after(pool_ts, touched)) 730682607adcSTejun Heo ts = pool_ts; 730782607adcSTejun Heo else 730882607adcSTejun Heo ts = touched; 730982607adcSTejun Heo 731082607adcSTejun Heo /* did we stall? */ 7311940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 731282607adcSTejun Heo lockup_detected = true; 73134cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7314cd2440d6SPetr Mladek pool->cpu_stall = true; 7315cd2440d6SPetr Mladek cpu_pool_stall = true; 7316cd2440d6SPetr Mladek } 731782607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 731882607adcSTejun Heo pr_cont_pool_info(pool); 731982607adcSTejun Heo pr_cont(" stuck for %us!\n", 7320940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 732182607adcSTejun Heo } 7322cd2440d6SPetr Mladek 7323cd2440d6SPetr Mladek 732482607adcSTejun Heo } 732582607adcSTejun Heo 732682607adcSTejun Heo rcu_read_unlock(); 732782607adcSTejun Heo 732882607adcSTejun Heo if (lockup_detected) 732955df0933SImran Khan show_all_workqueues(); 733082607adcSTejun Heo 7331cd2440d6SPetr Mladek if (cpu_pool_stall) 7332cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7333cd2440d6SPetr Mladek 733482607adcSTejun Heo wq_watchdog_reset_touched(); 733582607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 733682607adcSTejun Heo } 733782607adcSTejun Heo 7338cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 733982607adcSTejun Heo { 734082607adcSTejun Heo if (cpu >= 0) 734182607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 734289e28ce6SWang Qing 734382607adcSTejun Heo wq_watchdog_touched = jiffies; 734482607adcSTejun Heo } 734582607adcSTejun Heo 734682607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 734782607adcSTejun Heo { 734882607adcSTejun Heo wq_watchdog_thresh = 0; 734982607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 735082607adcSTejun Heo 735182607adcSTejun Heo if (thresh) { 735282607adcSTejun Heo wq_watchdog_thresh = thresh; 735382607adcSTejun Heo wq_watchdog_reset_touched(); 735482607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 735582607adcSTejun Heo } 735682607adcSTejun Heo } 735782607adcSTejun Heo 735882607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 735982607adcSTejun Heo const struct kernel_param *kp) 736082607adcSTejun Heo { 736182607adcSTejun Heo unsigned long thresh; 736282607adcSTejun Heo int ret; 736382607adcSTejun Heo 736482607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 736582607adcSTejun Heo if (ret) 736682607adcSTejun Heo return ret; 736782607adcSTejun Heo 736882607adcSTejun Heo if (system_wq) 736982607adcSTejun Heo wq_watchdog_set_thresh(thresh); 737082607adcSTejun Heo else 737182607adcSTejun Heo wq_watchdog_thresh = thresh; 737282607adcSTejun Heo 737382607adcSTejun Heo return 0; 737482607adcSTejun Heo } 737582607adcSTejun Heo 737682607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 737782607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 737882607adcSTejun Heo .get = param_get_ulong, 737982607adcSTejun Heo }; 738082607adcSTejun Heo 738182607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 738282607adcSTejun Heo 0644); 738382607adcSTejun Heo 738482607adcSTejun Heo static void wq_watchdog_init(void) 738582607adcSTejun Heo { 73865cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 738782607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 738882607adcSTejun Heo } 738982607adcSTejun Heo 739082607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 739182607adcSTejun Heo 739282607adcSTejun Heo static inline void wq_watchdog_init(void) { } 739382607adcSTejun Heo 739482607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 739582607adcSTejun Heo 73962f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work) 73972f34d733STejun Heo { 73982f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 73992f34d733STejun Heo } 74002f34d733STejun Heo 74012f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work) 74022f34d733STejun Heo { 74032f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 74042f34d733STejun Heo } 74052f34d733STejun Heo 74064a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 74074a6c5607STejun Heo { 74084a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 74094a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 74104a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 74114a6c5607STejun Heo return; 74124a6c5607STejun Heo } 74134a6c5607STejun Heo 74144a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 74154a6c5607STejun Heo } 74164a6c5607STejun Heo 74172fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 74182fcdb1b4STejun Heo { 74192fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 74202fcdb1b4STejun Heo pool->cpu = cpu; 74212fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 74222fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 74232fcdb1b4STejun Heo pool->attrs->nice = nice; 74242fcdb1b4STejun Heo pool->attrs->affn_strict = true; 74252fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 74262fcdb1b4STejun Heo 74272fcdb1b4STejun Heo /* alloc pool ID */ 74282fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 74292fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 74302fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 74312fcdb1b4STejun Heo } 74322fcdb1b4STejun Heo 74333347fa09STejun Heo /** 74343347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 74353347fa09STejun Heo * 74362930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 74372930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 74382930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 74392930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 74402930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 74412930155bSTejun Heo * before early initcalls. 74423347fa09STejun Heo */ 74432333e829SYu Chen void __init workqueue_init_early(void) 74441da177e4SLinus Torvalds { 744584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 74467a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 74472f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal, 74482f34d733STejun Heo bh_pool_kick_highpri }; 74497a4e344cSTejun Heo int i, cpu; 7450c34056a3STejun Heo 745110cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7452e904e6c2STejun Heo 7453b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7454fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7455fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7456b05a7928SFrederic Weisbecker 74574a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 74584a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 74594a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7460ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 74614a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7462ace3c549Stiozhang 7463fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 74648b03ae3cSTejun Heo 74658b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7466e22bee78STejun Heo 74672930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 74682930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 74692930155bSTejun Heo 74707bd20b6bSMarcelo Tosatti /* 74717bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 74727bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 74737bd20b6bSMarcelo Tosatti */ 74747bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 74757bd20b6bSMarcelo Tosatti wq_power_efficient = true; 74767bd20b6bSMarcelo Tosatti 747784193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 747884193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 747984193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 748084193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 748184193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 748284193c07STejun Heo 748384193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 748484193c07STejun Heo 748584193c07STejun Heo pt->nr_pods = 1; 748684193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 748784193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 748884193c07STejun Heo pt->cpu_pod[0] = 0; 748984193c07STejun Heo 74904cb1ef64STejun Heo /* initialize BH and CPU pools */ 74911da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 74921da177e4SLinus Torvalds struct worker_pool *pool; 74931da177e4SLinus Torvalds 74941da177e4SLinus Torvalds i = 0; 74954cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 74962f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]); 74974cb1ef64STejun Heo pool->flags |= POOL_BH; 74982f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]); 74992f34d733STejun Heo i++; 75004cb1ef64STejun Heo } 75014cb1ef64STejun Heo 75024cb1ef64STejun Heo i = 0; 75032fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 75042fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 75051da177e4SLinus Torvalds } 75061da177e4SLinus Torvalds 75078a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 750829c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 750929c91e99STejun Heo struct workqueue_attrs *attrs; 751029c91e99STejun Heo 7511be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 751229c91e99STejun Heo attrs->nice = std_nice[i]; 751329c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 75148a2b7538STejun Heo 75158a2b7538STejun Heo /* 75168a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 75178a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 75188a2b7538STejun Heo */ 7519be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 75208a2b7538STejun Heo attrs->nice = std_nice[i]; 7521af73f5c9STejun Heo attrs->ordered = true; 75228a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 752329c91e99STejun Heo } 752429c91e99STejun Heo 7525d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 75261aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7527d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7528f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7529636b927eSTejun Heo WQ_MAX_ACTIVE); 753024d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 753124d51addSTejun Heo WQ_FREEZABLE, 0); 75320668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 75330668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 75348318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 75350668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 75360668106cSViresh Kumar 0); 75374cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 75384cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 75394cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 75401aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 75410668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 75420668106cSViresh Kumar !system_power_efficient_wq || 75434cb1ef64STejun Heo !system_freezable_power_efficient_wq || 75444cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 75453347fa09STejun Heo } 75463347fa09STejun Heo 7547aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7548aa6fde93STejun Heo { 7549aa6fde93STejun Heo unsigned long thresh; 7550aa6fde93STejun Heo unsigned long bogo; 7551aa6fde93STejun Heo 7552dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7553dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7554dd64c873SZqiang 7555aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7556aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7557aa6fde93STejun Heo return; 7558aa6fde93STejun Heo 7559aa6fde93STejun Heo /* 7560aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7561aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7562aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7563aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7564aa6fde93STejun Heo * too low. 7565aa6fde93STejun Heo * 7566aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7567aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7568aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7569aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7570aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7571aa6fde93STejun Heo * usefulness. 7572aa6fde93STejun Heo */ 7573aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7574aa6fde93STejun Heo 7575aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7576aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7577aa6fde93STejun Heo if (bogo < 4000) 7578aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7579aa6fde93STejun Heo 7580aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7581aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7582aa6fde93STejun Heo 7583aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7584aa6fde93STejun Heo } 7585aa6fde93STejun Heo 75863347fa09STejun Heo /** 75873347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 75883347fa09STejun Heo * 75892930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 75902930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 75912930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 75922930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 75932930155bSTejun Heo * workers and enable future kworker creations. 75943347fa09STejun Heo */ 75952333e829SYu Chen void __init workqueue_init(void) 75963347fa09STejun Heo { 75972186d9f9STejun Heo struct workqueue_struct *wq; 75983347fa09STejun Heo struct worker_pool *pool; 75993347fa09STejun Heo int cpu, bkt; 76003347fa09STejun Heo 7601aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7602aa6fde93STejun Heo 76032186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 76042186d9f9STejun Heo 76052930155bSTejun Heo /* 76062930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 76072930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 76082930155bSTejun Heo */ 76092186d9f9STejun Heo for_each_possible_cpu(cpu) { 76104cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 76112186d9f9STejun Heo pool->node = cpu_to_node(cpu); 76124cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 76134cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 76142186d9f9STejun Heo } 76152186d9f9STejun Heo 761640c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 761740c17f75STejun Heo WARN(init_rescuer(wq), 761840c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 761940c17f75STejun Heo wq->name); 762040c17f75STejun Heo } 76212186d9f9STejun Heo 76222186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 76232186d9f9STejun Heo 76244cb1ef64STejun Heo /* 76254cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 76264cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 76274cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 76284cb1ef64STejun Heo * possible CPUs here. 76294cb1ef64STejun Heo */ 76304cb1ef64STejun Heo for_each_possible_cpu(cpu) 76314cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 76324cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 76334cb1ef64STejun Heo 76343347fa09STejun Heo for_each_online_cpu(cpu) { 76353347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 76363347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 76373347fa09STejun Heo BUG_ON(!create_worker(pool)); 76383347fa09STejun Heo } 76393347fa09STejun Heo } 76403347fa09STejun Heo 76413347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 76423347fa09STejun Heo BUG_ON(!create_worker(pool)); 76433347fa09STejun Heo 76443347fa09STejun Heo wq_online = true; 764582607adcSTejun Heo wq_watchdog_init(); 76461da177e4SLinus Torvalds } 7647c4f135d6STetsuo Handa 7648025e1684STejun Heo /* 7649025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7650025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7651025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7652025e1684STejun Heo */ 7653025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7654025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7655025e1684STejun Heo { 7656025e1684STejun Heo int cur, pre, cpu, pod; 7657025e1684STejun Heo 7658025e1684STejun Heo pt->nr_pods = 0; 7659025e1684STejun Heo 7660025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7661025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7662025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7663025e1684STejun Heo 7664025e1684STejun Heo for_each_possible_cpu(cur) { 7665025e1684STejun Heo for_each_possible_cpu(pre) { 7666025e1684STejun Heo if (pre >= cur) { 7667025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7668025e1684STejun Heo break; 7669025e1684STejun Heo } 7670025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7671025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7672025e1684STejun Heo break; 7673025e1684STejun Heo } 7674025e1684STejun Heo } 7675025e1684STejun Heo } 7676025e1684STejun Heo 7677025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7678025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7679025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7680025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7681025e1684STejun Heo 7682025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7683025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7684025e1684STejun Heo 7685025e1684STejun Heo for_each_possible_cpu(cpu) { 7686025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7687025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7688025e1684STejun Heo } 7689025e1684STejun Heo } 7690025e1684STejun Heo 769163c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 769263c5484eSTejun Heo { 769363c5484eSTejun Heo return false; 769463c5484eSTejun Heo } 769563c5484eSTejun Heo 769663c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 769763c5484eSTejun Heo { 769863c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 769963c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 770063c5484eSTejun Heo #else 770163c5484eSTejun Heo return false; 770263c5484eSTejun Heo #endif 770363c5484eSTejun Heo } 770463c5484eSTejun Heo 7705025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7706025e1684STejun Heo { 7707025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7708025e1684STejun Heo } 7709025e1684STejun Heo 77102930155bSTejun Heo /** 77112930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 77122930155bSTejun Heo * 771396068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 77142930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 77152930155bSTejun Heo * initializes the unbound CPU pods accordingly. 77162930155bSTejun Heo */ 77172930155bSTejun Heo void __init workqueue_init_topology(void) 7718a86feae6STejun Heo { 77192930155bSTejun Heo struct workqueue_struct *wq; 7720025e1684STejun Heo int cpu; 7721a86feae6STejun Heo 772263c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 772363c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 772463c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7725025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7726a86feae6STejun Heo 7727c5f8cd6cSTejun Heo wq_topo_initialized = true; 7728c5f8cd6cSTejun Heo 77292930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7730a86feae6STejun Heo 7731a86feae6STejun Heo /* 77322930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 77332930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 77342930155bSTejun Heo * combinations to apply per-pod sharing. 77352930155bSTejun Heo */ 77362930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 77375797b1c1STejun Heo for_each_online_cpu(cpu) 77382930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 77395797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 77405797b1c1STejun Heo mutex_lock(&wq->mutex); 77415797b1c1STejun Heo wq_update_node_max_active(wq, -1); 77425797b1c1STejun Heo mutex_unlock(&wq->mutex); 77432930155bSTejun Heo } 77442930155bSTejun Heo } 77452930155bSTejun Heo 77462930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7747a86feae6STejun Heo } 7748a86feae6STejun Heo 774920bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 775020bdedafSTetsuo Handa { 775120bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 775220bdedafSTetsuo Handa dump_stack(); 775320bdedafSTetsuo Handa } 7754c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7755ace3c549Stiozhang 7756ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7757ace3c549Stiozhang { 7758ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7759ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7760ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7761ace3c549Stiozhang } 7762ace3c549Stiozhang 7763ace3c549Stiozhang return 1; 7764ace3c549Stiozhang } 7765ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7766