1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 3c54fce6eSTejun Heo * kernel/workqueue.c - generic async execution with shared worker pool 41da177e4SLinus Torvalds * 5c54fce6eSTejun Heo * Copyright (C) 2002 Ingo Molnar 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Derived from the taskqueue/keventd code by: 81da177e4SLinus Torvalds * David Woodhouse <[email protected]> 9e1f8e874SFrancois Cami * Andrew Morton 101da177e4SLinus Torvalds * Kai Petzke <[email protected]> 111da177e4SLinus Torvalds * Theodore Ts'o <[email protected]> 1289ada679SChristoph Lameter * 13cde53535SChristoph Lameter * Made to use alloc_percpu by Christoph Lameter. 14c54fce6eSTejun Heo * 15c54fce6eSTejun Heo * Copyright (C) 2010 SUSE Linux Products GmbH 16c54fce6eSTejun Heo * Copyright (C) 2010 Tejun Heo <[email protected]> 17c54fce6eSTejun Heo * 18c54fce6eSTejun Heo * This is the generic async execution mechanism. Work items as are 19c54fce6eSTejun Heo * executed in process context. The worker pool is shared and 20b11895c4SLibin * automatically managed. There are two worker pools for each CPU (one for 21b11895c4SLibin * normal work items and the other for high priority ones) and some extra 22b11895c4SLibin * pools for workqueues which are not bound to any specific CPU - the 23b11895c4SLibin * number of these backing pools is dynamic. 24c54fce6eSTejun Heo * 259a261491SBenjamin Peterson * Please read Documentation/core-api/workqueue.rst for details. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 289984de1aSPaul Gortmaker #include <linux/export.h> 291da177e4SLinus Torvalds #include <linux/kernel.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/init.h> 324cb1ef64STejun Heo #include <linux/interrupt.h> 331da177e4SLinus Torvalds #include <linux/signal.h> 341da177e4SLinus Torvalds #include <linux/completion.h> 351da177e4SLinus Torvalds #include <linux/workqueue.h> 361da177e4SLinus Torvalds #include <linux/slab.h> 371da177e4SLinus Torvalds #include <linux/cpu.h> 381da177e4SLinus Torvalds #include <linux/notifier.h> 391da177e4SLinus Torvalds #include <linux/kthread.h> 401fa44ecaSJames Bottomley #include <linux/hardirq.h> 4146934023SChristoph Lameter #include <linux/mempolicy.h> 42341a5958SRafael J. Wysocki #include <linux/freezer.h> 43d5abe669SPeter Zijlstra #include <linux/debug_locks.h> 444e6045f1SJohannes Berg #include <linux/lockdep.h> 45c34056a3STejun Heo #include <linux/idr.h> 4629c91e99STejun Heo #include <linux/jhash.h> 4742f8570fSSasha Levin #include <linux/hashtable.h> 4876af4d93STejun Heo #include <linux/rculist.h> 49bce90380STejun Heo #include <linux/nodemask.h> 504c16bd32STejun Heo #include <linux/moduleparam.h> 513d1cb205STejun Heo #include <linux/uaccess.h> 52c98a9805STal Shorer #include <linux/sched/isolation.h> 53cd2440d6SPetr Mladek #include <linux/sched/debug.h> 5462635ea8SSergey Senozhatsky #include <linux/nmi.h> 55940d71c6SSergey Senozhatsky #include <linux/kvm_para.h> 56aa6fde93STejun Heo #include <linux/delay.h> 572f34d733STejun Heo #include <linux/irq_work.h> 58e22bee78STejun Heo 59ea138446STejun Heo #include "workqueue_internal.h" 601da177e4SLinus Torvalds 61e563d0a7STejun Heo enum worker_pool_flags { 62bc2ae0f5STejun Heo /* 6324647570STejun Heo * worker_pool flags 64bc2ae0f5STejun Heo * 6524647570STejun Heo * A bound pool is either associated or disassociated with its CPU. 66bc2ae0f5STejun Heo * While associated (!DISASSOCIATED), all workers are bound to the 67bc2ae0f5STejun Heo * CPU and none has %WORKER_UNBOUND set and concurrency management 68bc2ae0f5STejun Heo * is in effect. 69bc2ae0f5STejun Heo * 70bc2ae0f5STejun Heo * While DISASSOCIATED, the cpu may be offline and all workers have 71bc2ae0f5STejun Heo * %WORKER_UNBOUND set and concurrency management disabled, and may 7224647570STejun Heo * be executing on any CPU. The pool behaves as an unbound one. 73bc2ae0f5STejun Heo * 74bc3a1afcSTejun Heo * Note that DISASSOCIATED should be flipped only while holding 751258fae7STejun Heo * wq_pool_attach_mutex to avoid changing binding state while 764736cbf7SLai Jiangshan * worker_attach_to_pool() is in progress. 774cb1ef64STejun Heo * 784cb1ef64STejun Heo * As there can only be one concurrent BH execution context per CPU, a 794cb1ef64STejun Heo * BH pool is per-CPU and always DISASSOCIATED. 80bc2ae0f5STejun Heo */ 814cb1ef64STejun Heo POOL_BH = 1 << 0, /* is a BH pool */ 824cb1ef64STejun Heo POOL_MANAGER_ACTIVE = 1 << 1, /* being managed */ 8324647570STejun Heo POOL_DISASSOCIATED = 1 << 2, /* cpu can't serve workers */ 84e563d0a7STejun Heo }; 85db7bccf4STejun Heo 86e563d0a7STejun Heo enum worker_flags { 87c8e55f36STejun Heo /* worker flags */ 88c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 89c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 90e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 91fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 92f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 93a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 94e22bee78STejun Heo 95a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 96a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 97e563d0a7STejun Heo }; 98db7bccf4STejun Heo 99c5f5b942STejun Heo enum work_cancel_flags { 100c5f5b942STejun Heo WORK_CANCEL_DELAYED = 1 << 0, /* canceling a delayed_work */ 101c5f5b942STejun Heo }; 102c5f5b942STejun Heo 103e563d0a7STejun Heo enum wq_internal_consts { 104e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 1054ce62e9eSTejun Heo 10629c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 107c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 108db7bccf4STejun Heo 109e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 110e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 111e22bee78STejun Heo 1123233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1133233cdbdSTejun Heo /* call for help after 10ms 1143233cdbdSTejun Heo (min two ticks) */ 115e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 116e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* 119e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1208698a745SDongsheng Yang * all cpus. Give MIN_NICE. 121e22bee78STejun Heo */ 1228698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1238698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 124ecf6881fSTejun Heo 12531c89007SAudra Mitchell WQ_NAME_LEN = 32, 126c8e55f36STejun Heo }; 127c8e55f36STejun Heo 1281da177e4SLinus Torvalds /* 1294cb1ef64STejun Heo * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and 1304cb1ef64STejun Heo * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because 1314cb1ef64STejun Heo * msecs_to_jiffies() can't be an initializer. 1324cb1ef64STejun Heo */ 1334cb1ef64STejun Heo #define BH_WORKER_JIFFIES msecs_to_jiffies(2) 1344cb1ef64STejun Heo #define BH_WORKER_RESTARTS 10 1354cb1ef64STejun Heo 1364cb1ef64STejun Heo /* 1374690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1384690c4abSTejun Heo * 139e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 140e41e704bSTejun Heo * everyone else. 1414690c4abSTejun Heo * 142e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 143e22bee78STejun Heo * only be modified and accessed from the local cpu. 144e22bee78STejun Heo * 145d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1464690c4abSTejun Heo * 1475797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for 1485797b1c1STejun Heo * reads. 1495797b1c1STejun Heo * 150bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 151bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 152bdf8b9bfSTejun Heo * kworker. 153bdf8b9bfSTejun Heo * 154bdf8b9bfSTejun Heo * S: Only modified by worker self. 155bdf8b9bfSTejun Heo * 1561258fae7STejun Heo * A: wq_pool_attach_mutex protected. 157822d8405STejun Heo * 15868e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 15976af4d93STejun Heo * 16024acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1615bcab335STejun Heo * 1625b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1635b95e1afSLai Jiangshan * 1645b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 16524acfb71SThomas Gleixner * RCU for reads. 1665b95e1afSLai Jiangshan * 1673c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1683c25a55dSLai Jiangshan * 16924acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1702e109a28STejun Heo * 171a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 172a045a272STejun Heo * with READ_ONCE() without locking. 173a045a272STejun Heo * 1742e109a28STejun Heo * MD: wq_mayday_lock protected. 175cd2440d6SPetr Mladek * 176cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1774690c4abSTejun Heo */ 1784690c4abSTejun Heo 1792eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 180c34056a3STejun Heo 181bd7bdd43STejun Heo struct worker_pool { 182a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 183d84ff051STejun Heo int cpu; /* I: the associated cpu */ 184f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1859daf9e67STejun Heo int id; /* I: pool ID */ 186bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 187bd7bdd43STejun Heo 18882607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 189cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 19082607adcSTejun Heo 191bc35f7efSLai Jiangshan /* 192bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 193bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 194bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 195bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 196bc35f7efSLai Jiangshan */ 197bc35f7efSLai Jiangshan int nr_running; 19884f91c62SLai Jiangshan 199bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 200ea1abd61SLai Jiangshan 2015826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 2025826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 203bd7bdd43STejun Heo 2042c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 205bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 2063f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 2073f959aa3SValentin Schneider 208bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 209bd7bdd43STejun Heo 210c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 211c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 212c9e7cf27STejun Heo /* L: hash of busy workers */ 213c9e7cf27STejun Heo 2142607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 21592f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 216e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 21760f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 218e19e397aSTejun Heo 2197cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 220e19e397aSTejun Heo 2217a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 22268e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 22368e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2247a4e344cSTejun Heo 225e19e397aSTejun Heo /* 22624acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 22729c91e99STejun Heo * from get_work_pool(). 22829c91e99STejun Heo */ 22929c91e99STejun Heo struct rcu_head rcu; 23084f91c62SLai Jiangshan }; 2318b03ae3cSTejun Heo 2328b03ae3cSTejun Heo /* 233725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 234725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 235725e8ec5STejun Heo */ 236725e8ec5STejun Heo enum pool_workqueue_stats { 237725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 238725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2398a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 240616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 241725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2428639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 243725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 244725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 245725e8ec5STejun Heo 246725e8ec5STejun Heo PWQ_NR_STATS, 247725e8ec5STejun Heo }; 248725e8ec5STejun Heo 249725e8ec5STejun Heo /* 250e9a8e01fSTejun Heo * The per-pool workqueue. While queued, bits below WORK_PWQ_SHIFT 251112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 252112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 253112202d9STejun Heo * number of flag bits. 2541da177e4SLinus Torvalds */ 255112202d9STejun Heo struct pool_workqueue { 256bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2574690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 25873f53c4aSTejun Heo int work_color; /* L: current color */ 25973f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2608864b4e5STejun Heo int refcnt; /* L: reference count */ 26173f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 26273f53c4aSTejun Heo /* L: nr of in_flight works */ 2634c065dbcSWaiman Long bool plugged; /* L: execution suspended */ 264018f3a13SLai Jiangshan 265018f3a13SLai Jiangshan /* 266018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 267018f3a13SLai Jiangshan * 268018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 269018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 270018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 271018f3a13SLai Jiangshan * 2725797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in 2735797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with 2745797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are 2755797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in 2765797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct 2775797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate 2785797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with 2795797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 280018f3a13SLai Jiangshan */ 2811e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 282f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2835797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ 2843c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2852e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2868864b4e5STejun Heo 287725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 288725e8ec5STejun Heo 2898864b4e5STejun Heo /* 290967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 291687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 292687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 293967b494eSTejun Heo * grabbing wq->mutex. 2948864b4e5STejun Heo */ 295687a9aa5STejun Heo struct kthread_work release_work; 2968864b4e5STejun Heo struct rcu_head rcu; 297e9a8e01fSTejun Heo } __aligned(1 << WORK_STRUCT_PWQ_SHIFT); 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds /* 30073f53c4aSTejun Heo * Structure used to wait for workqueue flush. 30173f53c4aSTejun Heo */ 30273f53c4aSTejun Heo struct wq_flusher { 3033c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 3043c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 30573f53c4aSTejun Heo struct completion done; /* flush completion */ 30673f53c4aSTejun Heo }; 3071da177e4SLinus Torvalds 308226223abSTejun Heo struct wq_device; 309226223abSTejun Heo 31073f53c4aSTejun Heo /* 31191ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 31291ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 31391ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 31491ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 3155797b1c1STejun Heo * 3165797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants 3175797b1c1STejun Heo * to start executing a work item, it should increment ->nr using 3185797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over 3195797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish 3205797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in 3215797b1c1STejun Heo * round-robin order. 32291ccc6e7STejun Heo */ 32391ccc6e7STejun Heo struct wq_node_nr_active { 3245797b1c1STejun Heo int max; /* per-node max_active */ 3255797b1c1STejun Heo atomic_t nr; /* per-node nr_active */ 3265797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */ 3275797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */ 32891ccc6e7STejun Heo }; 32991ccc6e7STejun Heo 33091ccc6e7STejun Heo /* 331c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 332c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds struct workqueue_struct { 3353c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 336e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 33773f53c4aSTejun Heo 3383c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3393c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3403c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 341112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3423c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3433c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3443c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 34573f53c4aSTejun Heo 3462e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 34730ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 348e22bee78STejun Heo 34987fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 3505797b1c1STejun Heo 3515797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */ 352a045a272STejun Heo int max_active; /* WO: max active works */ 3535797b1c1STejun Heo int min_active; /* WO: min active works */ 354a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 3555797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */ 356226223abSTejun Heo 3575b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3589f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3596029a918STejun Heo 360226223abSTejun Heo #ifdef CONFIG_SYSFS 361226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 362226223abSTejun Heo #endif 3634e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 364669de8bdSBart Van Assche char *lock_name; 365669de8bdSBart Van Assche struct lock_class_key key; 3664e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3674e6045f1SJohannes Berg #endif 368ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3692728fd2fSTejun Heo 370e2dca7adSTejun Heo /* 37124acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 37224acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 373e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 374e2dca7adSTejun Heo */ 375e2dca7adSTejun Heo struct rcu_head rcu; 376e2dca7adSTejun Heo 3772728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3782728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 379636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 38091ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3811da177e4SLinus Torvalds }; 3821da177e4SLinus Torvalds 38384193c07STejun Heo /* 38484193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 38584193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 38684193c07STejun Heo */ 38784193c07STejun Heo struct wq_pod_type { 38884193c07STejun Heo int nr_pods; /* number of pods */ 38984193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 39084193c07STejun Heo int *pod_node; /* pod -> node */ 39184193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 39284193c07STejun Heo }; 39384193c07STejun Heo 39463c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 395523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 39663c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 39763c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 39863c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 39963c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 40063c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 40163c5484eSTejun Heo }; 402bce90380STejun Heo 403616db877STejun Heo /* 404616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 405616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 406616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 407aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 408aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 409616db877STejun Heo */ 410aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 411616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 412616db877STejun Heo 413cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 414552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 415cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 416cee22a15SViresh Kumar 417863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 418c7a40c49STejun Heo static bool wq_topo_initialized __read_mostly = false; 419c7a40c49STejun Heo 420c7a40c49STejun Heo static struct kmem_cache *pwq_cache; 421c7a40c49STejun Heo 422c7a40c49STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 423c7a40c49STejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 4243347fa09STejun Heo 425fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 426fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4274c16bd32STejun Heo 42868e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4291258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 430a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 431d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 432d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4335bcab335STejun Heo 434e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 43568e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4367d19c5ceSTejun Heo 43799c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 438ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 439ef557180SMike Galbraith 440fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 441fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 442fe28f631SWaiman Long 443fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 444fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 445fe28f631SWaiman Long 446ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 447ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 448ace3c549Stiozhang 449ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 450ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 451b05a7928SFrederic Weisbecker 452f303fccbSTejun Heo /* 453f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 454f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 455f303fccbSTejun Heo * to uncover usages which depend on it. 456f303fccbSTejun Heo */ 457f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 458f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 459f303fccbSTejun Heo #else 460f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 461f303fccbSTejun Heo #endif 462f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 463f303fccbSTejun Heo 4642f34d733STejun Heo /* to raise softirq for the BH worker pools on other CPUs */ 4652f34d733STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct irq_work [NR_STD_WORKER_POOLS], 4662f34d733STejun Heo bh_pool_irq_works); 4672f34d733STejun Heo 4684cb1ef64STejun Heo /* the BH worker pools */ 4694cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4704cb1ef64STejun Heo bh_worker_pools); 4714cb1ef64STejun Heo 4727d19c5ceSTejun Heo /* the per-cpu worker pools */ 4734cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4744cb1ef64STejun Heo cpu_worker_pools); 4757d19c5ceSTejun Heo 47668e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4777d19c5ceSTejun Heo 47868e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 47929c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 48029c91e99STejun Heo 481c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 48229c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 48329c91e99STejun Heo 4848a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4858a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4868a2b7538STejun Heo 487967b494eSTejun Heo /* 488978b8409STejun Heo * Used to synchronize multiple cancel_sync attempts on the same work item. See 489978b8409STejun Heo * work_grab_pending() and __cancel_work_sync(). 490978b8409STejun Heo */ 491978b8409STejun Heo static DECLARE_WAIT_QUEUE_HEAD(wq_cancel_waitq); 492978b8409STejun Heo 493978b8409STejun Heo /* 494967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 495967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 496967b494eSTejun Heo * worker to avoid A-A deadlocks. 497967b494eSTejun Heo */ 49868279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 499967b494eSTejun Heo 50068279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 501ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 50268279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 5031aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 50468279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 505d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 50668279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 507f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 50868279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 50924d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 51068279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 5110668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 51268279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 5130668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 5144cb1ef64STejun Heo struct workqueue_struct *system_bh_wq; 5154cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq); 5164cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq; 5174cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq); 518d320c038STejun Heo 5197d19c5ceSTejun Heo static int worker_thread(void *__worker); 5206ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 521c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 52255df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 5237d19c5ceSTejun Heo 52497bd2347STejun Heo #define CREATE_TRACE_POINTS 52597bd2347STejun Heo #include <trace/events/workqueue.h> 52697bd2347STejun Heo 52768e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 528d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 529f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53024acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 5315bcab335STejun Heo 5325b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 533d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 534f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 535f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53624acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5375b95e1afSLai Jiangshan 5384cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \ 5394cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ 5404cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5414cb1ef64STejun Heo (pool)++) 5424cb1ef64STejun Heo 543f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 544f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 545f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5467a62c2c8STejun Heo (pool)++) 5474ce62e9eSTejun Heo 54849e3cf44STejun Heo /** 54917116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 55017116969STejun Heo * @pool: iteration cursor 551611c92a0STejun Heo * @pi: integer used for iteration 552fa1b54e6STejun Heo * 55324acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 55468e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 55568e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 556fa1b54e6STejun Heo * 557fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 558fa1b54e6STejun Heo * ignored. 55917116969STejun Heo */ 560611c92a0STejun Heo #define for_each_pool(pool, pi) \ 561611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 56268e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 563fa1b54e6STejun Heo else 56417116969STejun Heo 56517116969STejun Heo /** 566822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 567822d8405STejun Heo * @worker: iteration cursor 568822d8405STejun Heo * @pool: worker_pool to iterate workers of 569822d8405STejun Heo * 5701258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 571822d8405STejun Heo * 572822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 573822d8405STejun Heo * ignored. 574822d8405STejun Heo */ 575da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 576da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5771258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 578822d8405STejun Heo else 579822d8405STejun Heo 580822d8405STejun Heo /** 58149e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 58249e3cf44STejun Heo * @pwq: iteration cursor 58349e3cf44STejun Heo * @wq: the target workqueue 58476af4d93STejun Heo * 58524acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 586794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 587794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 58876af4d93STejun Heo * 58976af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 59076af4d93STejun Heo * ignored. 59149e3cf44STejun Heo */ 59249e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 59349e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5945a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 595f3421797STejun Heo 596dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 597dc186ad7SThomas Gleixner 598f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 599dc186ad7SThomas Gleixner 60099777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 60199777288SStanislaw Gruszka { 60299777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 60399777288SStanislaw Gruszka } 60499777288SStanislaw Gruszka 605b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 606b9fdac7fSDu, Changbin { 607b9fdac7fSDu, Changbin struct work_struct *work = addr; 608b9fdac7fSDu, Changbin 609b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 610b9fdac7fSDu, Changbin } 611b9fdac7fSDu, Changbin 612dc186ad7SThomas Gleixner /* 613dc186ad7SThomas Gleixner * fixup_init is called when: 614dc186ad7SThomas Gleixner * - an active object is initialized 615dc186ad7SThomas Gleixner */ 61602a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 617dc186ad7SThomas Gleixner { 618dc186ad7SThomas Gleixner struct work_struct *work = addr; 619dc186ad7SThomas Gleixner 620dc186ad7SThomas Gleixner switch (state) { 621dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 622dc186ad7SThomas Gleixner cancel_work_sync(work); 623dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 62402a982a6SDu, Changbin return true; 625dc186ad7SThomas Gleixner default: 62602a982a6SDu, Changbin return false; 627dc186ad7SThomas Gleixner } 628dc186ad7SThomas Gleixner } 629dc186ad7SThomas Gleixner 630dc186ad7SThomas Gleixner /* 631dc186ad7SThomas Gleixner * fixup_free is called when: 632dc186ad7SThomas Gleixner * - an active object is freed 633dc186ad7SThomas Gleixner */ 63402a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 635dc186ad7SThomas Gleixner { 636dc186ad7SThomas Gleixner struct work_struct *work = addr; 637dc186ad7SThomas Gleixner 638dc186ad7SThomas Gleixner switch (state) { 639dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 640dc186ad7SThomas Gleixner cancel_work_sync(work); 641dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 64202a982a6SDu, Changbin return true; 643dc186ad7SThomas Gleixner default: 64402a982a6SDu, Changbin return false; 645dc186ad7SThomas Gleixner } 646dc186ad7SThomas Gleixner } 647dc186ad7SThomas Gleixner 648f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 649dc186ad7SThomas Gleixner .name = "work_struct", 65099777288SStanislaw Gruszka .debug_hint = work_debug_hint, 651b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 652dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 653dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 654dc186ad7SThomas Gleixner }; 655dc186ad7SThomas Gleixner 656dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 657dc186ad7SThomas Gleixner { 658dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 659dc186ad7SThomas Gleixner } 660dc186ad7SThomas Gleixner 661dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 662dc186ad7SThomas Gleixner { 663dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 664dc186ad7SThomas Gleixner } 665dc186ad7SThomas Gleixner 666dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 667dc186ad7SThomas Gleixner { 668dc186ad7SThomas Gleixner if (onstack) 669dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 670dc186ad7SThomas Gleixner else 671dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 672dc186ad7SThomas Gleixner } 673dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 674dc186ad7SThomas Gleixner 675dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 676dc186ad7SThomas Gleixner { 677dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 678dc186ad7SThomas Gleixner } 679dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 680dc186ad7SThomas Gleixner 681ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 682ea2e64f2SThomas Gleixner { 683ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 684ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 685ea2e64f2SThomas Gleixner } 686ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 687ea2e64f2SThomas Gleixner 688dc186ad7SThomas Gleixner #else 689dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 690dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 691dc186ad7SThomas Gleixner #endif 692dc186ad7SThomas Gleixner 6934e8b22bdSLi Bin /** 69467dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 6954e8b22bdSLi Bin * @pool: the pool pointer of interest 6964e8b22bdSLi Bin * 6974e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 6984e8b22bdSLi Bin * successfully, -errno on failure. 6994e8b22bdSLi Bin */ 7009daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 7019daf9e67STejun Heo { 7029daf9e67STejun Heo int ret; 7039daf9e67STejun Heo 70468e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 7055bcab335STejun Heo 7064e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 7074e8b22bdSLi Bin GFP_KERNEL); 708229641a6STejun Heo if (ret >= 0) { 709e68035fbSTejun Heo pool->id = ret; 710229641a6STejun Heo return 0; 711229641a6STejun Heo } 7129daf9e67STejun Heo return ret; 7139daf9e67STejun Heo } 7149daf9e67STejun Heo 7159f66cff2STejun Heo static struct pool_workqueue __rcu ** 7169f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 7179f66cff2STejun Heo { 7189f66cff2STejun Heo if (cpu >= 0) 7199f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 7209f66cff2STejun Heo else 7219f66cff2STejun Heo return &wq->dfl_pwq; 7229f66cff2STejun Heo } 7239f66cff2STejun Heo 7249f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 7259f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 7269f66cff2STejun Heo { 7279f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 7289f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 7299f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 7309f66cff2STejun Heo } 7319f66cff2STejun Heo 7325797b1c1STejun Heo /** 7335797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 7345797b1c1STejun Heo * @wq: workqueue of interest 7355797b1c1STejun Heo * 7365797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 7375797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 7385797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 7395797b1c1STejun Heo */ 7405797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 7415797b1c1STejun Heo { 7425797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7435797b1c1STejun Heo } 7445797b1c1STejun Heo 74573f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 74673f53c4aSTejun Heo { 74773f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 74873f53c4aSTejun Heo } 74973f53c4aSTejun Heo 750c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 75173f53c4aSTejun Heo { 752c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 75373f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 75473f53c4aSTejun Heo } 75573f53c4aSTejun Heo 75673f53c4aSTejun Heo static int work_next_color(int color) 75773f53c4aSTejun Heo { 75873f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 759a848e3b6SOleg Nesterov } 760a848e3b6SOleg Nesterov 7614594bf15SDavid Howells /* 762112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 763112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7647c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7657a22ad75STejun Heo * 766afe928c1STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending() and mark_work_canceling() 767afe928c1STejun Heo * can be used to set the pwq, pool or clear work->data. These functions should 768afe928c1STejun Heo * only be called while the work is owned - ie. while the PENDING bit is set. 7697a22ad75STejun Heo * 770112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7717c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 772112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7737c3eed5cSTejun Heo * available only while the work item is queued. 774bbb68dfaSTejun Heo * 775bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 776bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 777bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 778bbb68dfaSTejun Heo * try to steal the PENDING bit. 7794594bf15SDavid Howells */ 780*bccdc1faSTejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data) 7817a22ad75STejun Heo { 7826183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 783*bccdc1faSTejun Heo atomic_long_set(&work->data, data | work_static(work)); 7847a22ad75STejun Heo } 7857a22ad75STejun Heo 786112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 787*bccdc1faSTejun Heo unsigned long flags) 788365970a1SDavid Howells { 789*bccdc1faSTejun Heo set_work_data(work, (unsigned long)pwq | WORK_STRUCT_PENDING | 790*bccdc1faSTejun Heo WORK_STRUCT_PWQ | flags); 791365970a1SDavid Howells } 792365970a1SDavid Howells 7934468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 794*bccdc1faSTejun Heo int pool_id, unsigned long flags) 7954468a00fSLai Jiangshan { 796*bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) | 797*bccdc1faSTejun Heo WORK_STRUCT_PENDING | flags); 7984468a00fSLai Jiangshan } 7994468a00fSLai Jiangshan 8007c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 801*bccdc1faSTejun Heo int pool_id, unsigned long flags) 8024d707b9fSOleg Nesterov { 80323657bb1STejun Heo /* 80423657bb1STejun Heo * The following wmb is paired with the implied mb in 80523657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 80623657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 80723657bb1STejun Heo * owner. 80823657bb1STejun Heo */ 80923657bb1STejun Heo smp_wmb(); 810*bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) | 811*bccdc1faSTejun Heo flags); 812346c09f8SRoman Pen /* 813346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 814346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 815346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 8168bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 817346c09f8SRoman Pen * the same @work. E.g. consider this case: 818346c09f8SRoman Pen * 819346c09f8SRoman Pen * CPU#0 CPU#1 820346c09f8SRoman Pen * ---------------------------- -------------------------------- 821346c09f8SRoman Pen * 822346c09f8SRoman Pen * 1 STORE event_indicated 823346c09f8SRoman Pen * 2 queue_work_on() { 824346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 825346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 826346c09f8SRoman Pen * 5 set_work_data() # clear bit 827346c09f8SRoman Pen * 6 smp_mb() 828346c09f8SRoman Pen * 7 work->current_func() { 829346c09f8SRoman Pen * 8 LOAD event_indicated 830346c09f8SRoman Pen * } 831346c09f8SRoman Pen * 832346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 833346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 834346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 835346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 836346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 837346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 838346c09f8SRoman Pen * before actual STORE. 839346c09f8SRoman Pen */ 840346c09f8SRoman Pen smp_mb(); 8414d707b9fSOleg Nesterov } 8424d707b9fSOleg Nesterov 843afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 844afa4bb77SLinus Torvalds { 845e9a8e01fSTejun Heo return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK); 846afa4bb77SLinus Torvalds } 847afa4bb77SLinus Torvalds 848112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8497a22ad75STejun Heo { 850e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8517a22ad75STejun Heo 852112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 853afa4bb77SLinus Torvalds return work_struct_pwq(data); 854e120153dSTejun Heo else 855e120153dSTejun Heo return NULL; 8567a22ad75STejun Heo } 8577a22ad75STejun Heo 8587c3eed5cSTejun Heo /** 8597c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8607c3eed5cSTejun Heo * @work: the work item of interest 8617c3eed5cSTejun Heo * 86268e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 86324acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 86424acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 865fa1b54e6STejun Heo * 866fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 867fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 868fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 869fa1b54e6STejun Heo * returned pool is and stays online. 870d185af30SYacine Belkadi * 871d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8727c3eed5cSTejun Heo */ 8737c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8747a22ad75STejun Heo { 875e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8767c3eed5cSTejun Heo int pool_id; 8777a22ad75STejun Heo 87868e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 879fa1b54e6STejun Heo 880112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 881afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8827a22ad75STejun Heo 8837c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8847c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8857a22ad75STejun Heo return NULL; 8867a22ad75STejun Heo 887fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8887c3eed5cSTejun Heo } 8897c3eed5cSTejun Heo 8907c3eed5cSTejun Heo /** 8917c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8927c3eed5cSTejun Heo * @work: the work item of interest 8937c3eed5cSTejun Heo * 894d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 8957c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 8967c3eed5cSTejun Heo */ 8977c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 8987c3eed5cSTejun Heo { 89954d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 9007c3eed5cSTejun Heo 901112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 902afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 90354d5b7d0SLai Jiangshan 90454d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 9057c3eed5cSTejun Heo } 9067c3eed5cSTejun Heo 907bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 908bbb68dfaSTejun Heo { 9097c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 910bbb68dfaSTejun Heo 9117c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 912*bccdc1faSTejun Heo set_work_data(work, pool_id | WORK_STRUCT_PENDING | WORK_OFFQ_CANCELING); 913bbb68dfaSTejun Heo } 914bbb68dfaSTejun Heo 915bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 916bbb68dfaSTejun Heo { 917bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 918bbb68dfaSTejun Heo 919112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 920bbb68dfaSTejun Heo } 921bbb68dfaSTejun Heo 922e22bee78STejun Heo /* 9233270476aSTejun Heo * Policy functions. These define the policies on how the global worker 9243270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 925d565ed63STejun Heo * they're being called with pool->lock held. 926e22bee78STejun Heo */ 927e22bee78STejun Heo 928e22bee78STejun Heo /* 929e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 930e22bee78STejun Heo * running workers. 931974271c4STejun Heo * 932974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 933706026c2STejun Heo * function will always return %true for unbound pools as long as the 934974271c4STejun Heo * worklist isn't empty. 935e22bee78STejun Heo */ 93663d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 937e22bee78STejun Heo { 9380219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 939e22bee78STejun Heo } 940e22bee78STejun Heo 941e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 94263d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 943e22bee78STejun Heo { 94463d95a91STejun Heo return pool->nr_idle; 945e22bee78STejun Heo } 946e22bee78STejun Heo 947e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 94863d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 949e22bee78STejun Heo { 950bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 951e22bee78STejun Heo } 952e22bee78STejun Heo 953e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 95463d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 955e22bee78STejun Heo { 95663d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 957e22bee78STejun Heo } 958e22bee78STejun Heo 959e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 96063d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 961e22bee78STejun Heo { 962692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 96363d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 96463d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 965e22bee78STejun Heo 966e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 967e22bee78STejun Heo } 968e22bee78STejun Heo 9694690c4abSTejun Heo /** 970e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 971cb444766STejun Heo * @worker: self 972d302f017STejun Heo * @flags: flags to set 973d302f017STejun Heo * 974228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 975d302f017STejun Heo */ 976228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 977d302f017STejun Heo { 978bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 979e22bee78STejun Heo 980bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 981cb444766STejun Heo 982228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 983e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 984e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 985bc35f7efSLai Jiangshan pool->nr_running--; 986e22bee78STejun Heo } 987e22bee78STejun Heo 988d302f017STejun Heo worker->flags |= flags; 989d302f017STejun Heo } 990d302f017STejun Heo 991d302f017STejun Heo /** 992e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 993cb444766STejun Heo * @worker: self 994d302f017STejun Heo * @flags: flags to clear 995d302f017STejun Heo * 996e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 997d302f017STejun Heo */ 998d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 999d302f017STejun Heo { 100063d95a91STejun Heo struct worker_pool *pool = worker->pool; 1001e22bee78STejun Heo unsigned int oflags = worker->flags; 1002e22bee78STejun Heo 1003bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 1004cb444766STejun Heo 1005d302f017STejun Heo worker->flags &= ~flags; 1006e22bee78STejun Heo 100742c025f3STejun Heo /* 100842c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 100942c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 101042c025f3STejun Heo * of multiple flags, not a single flag. 101142c025f3STejun Heo */ 1012e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1013e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1014bc35f7efSLai Jiangshan pool->nr_running++; 1015d302f017STejun Heo } 1016d302f017STejun Heo 1017797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 1018797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 1019797e8345STejun Heo { 1020797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 1021797e8345STejun Heo return NULL; 1022797e8345STejun Heo 1023797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 1024797e8345STejun Heo } 1025797e8345STejun Heo 1026797e8345STejun Heo /** 1027797e8345STejun Heo * worker_enter_idle - enter idle state 1028797e8345STejun Heo * @worker: worker which is entering idle state 1029797e8345STejun Heo * 1030797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 1031797e8345STejun Heo * necessary. 1032797e8345STejun Heo * 1033797e8345STejun Heo * LOCKING: 1034797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1035797e8345STejun Heo */ 1036797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1037797e8345STejun Heo { 1038797e8345STejun Heo struct worker_pool *pool = worker->pool; 1039797e8345STejun Heo 1040797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1041797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1042797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1043797e8345STejun Heo return; 1044797e8345STejun Heo 1045797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1046797e8345STejun Heo worker->flags |= WORKER_IDLE; 1047797e8345STejun Heo pool->nr_idle++; 1048797e8345STejun Heo worker->last_active = jiffies; 1049797e8345STejun Heo 1050797e8345STejun Heo /* idle_list is LIFO */ 1051797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1052797e8345STejun Heo 1053797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1054797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1055797e8345STejun Heo 1056797e8345STejun Heo /* Sanity check nr_running. */ 1057797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1058797e8345STejun Heo } 1059797e8345STejun Heo 1060797e8345STejun Heo /** 1061797e8345STejun Heo * worker_leave_idle - leave idle state 1062797e8345STejun Heo * @worker: worker which is leaving idle state 1063797e8345STejun Heo * 1064797e8345STejun Heo * @worker is leaving idle state. Update stats. 1065797e8345STejun Heo * 1066797e8345STejun Heo * LOCKING: 1067797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1068797e8345STejun Heo */ 1069797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1070797e8345STejun Heo { 1071797e8345STejun Heo struct worker_pool *pool = worker->pool; 1072797e8345STejun Heo 1073797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1074797e8345STejun Heo return; 1075797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1076797e8345STejun Heo pool->nr_idle--; 1077797e8345STejun Heo list_del_init(&worker->entry); 1078797e8345STejun Heo } 1079797e8345STejun Heo 1080797e8345STejun Heo /** 1081797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1082797e8345STejun Heo * @pool: pool of interest 1083797e8345STejun Heo * @work: work to find worker for 1084797e8345STejun Heo * 1085797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1086797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1087797e8345STejun Heo * to match, its current execution should match the address of @work and 1088797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1089797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1090797e8345STejun Heo * being executed. 1091797e8345STejun Heo * 1092797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1093797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1094797e8345STejun Heo * another work item. If the same work item address ends up being reused 1095797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1096797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1097797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1098797e8345STejun Heo * 1099797e8345STejun Heo * This function checks the work item address and work function to avoid 1100797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1101797e8345STejun Heo * work function which can introduce dependency onto itself through a 1102797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1103797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1104797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1105797e8345STejun Heo * 1106797e8345STejun Heo * CONTEXT: 1107797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1108797e8345STejun Heo * 1109797e8345STejun Heo * Return: 1110797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1111797e8345STejun Heo * otherwise. 1112797e8345STejun Heo */ 1113797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1114797e8345STejun Heo struct work_struct *work) 1115797e8345STejun Heo { 1116797e8345STejun Heo struct worker *worker; 1117797e8345STejun Heo 1118797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1119797e8345STejun Heo (unsigned long)work) 1120797e8345STejun Heo if (worker->current_work == work && 1121797e8345STejun Heo worker->current_func == work->func) 1122797e8345STejun Heo return worker; 1123797e8345STejun Heo 1124797e8345STejun Heo return NULL; 1125797e8345STejun Heo } 1126797e8345STejun Heo 1127797e8345STejun Heo /** 1128797e8345STejun Heo * move_linked_works - move linked works to a list 1129797e8345STejun Heo * @work: start of series of works to be scheduled 1130797e8345STejun Heo * @head: target list to append @work to 1131797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1132797e8345STejun Heo * 1133873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1134873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1135873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1136873eaca6STejun Heo * @nextp. 1137797e8345STejun Heo * 1138797e8345STejun Heo * CONTEXT: 1139797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1140797e8345STejun Heo */ 1141797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1142797e8345STejun Heo struct work_struct **nextp) 1143797e8345STejun Heo { 1144797e8345STejun Heo struct work_struct *n; 1145797e8345STejun Heo 1146797e8345STejun Heo /* 1147797e8345STejun Heo * Linked worklist will always end before the end of the list, 1148797e8345STejun Heo * use NULL for list head. 1149797e8345STejun Heo */ 1150797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1151797e8345STejun Heo list_move_tail(&work->entry, head); 1152797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1153797e8345STejun Heo break; 1154797e8345STejun Heo } 1155797e8345STejun Heo 1156797e8345STejun Heo /* 1157797e8345STejun Heo * If we're already inside safe list traversal and have moved 1158797e8345STejun Heo * multiple works to the scheduled queue, the next position 1159797e8345STejun Heo * needs to be updated. 1160797e8345STejun Heo */ 1161797e8345STejun Heo if (nextp) 1162797e8345STejun Heo *nextp = n; 1163797e8345STejun Heo } 1164797e8345STejun Heo 1165797e8345STejun Heo /** 1166873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1167873eaca6STejun Heo * @work: work to assign 1168873eaca6STejun Heo * @worker: worker to assign to 1169873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1170873eaca6STejun Heo * 1171873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1172873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1173873eaca6STejun Heo * 1174873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1175873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1176873eaca6STejun Heo * list_for_each_entry_safe(). 1177873eaca6STejun Heo * 1178873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1179873eaca6STejun Heo * was punted to another worker already executing it. 1180873eaca6STejun Heo */ 1181873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1182873eaca6STejun Heo struct work_struct **nextp) 1183873eaca6STejun Heo { 1184873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1185873eaca6STejun Heo struct worker *collision; 1186873eaca6STejun Heo 1187873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1188873eaca6STejun Heo 1189873eaca6STejun Heo /* 1190873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1191873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1192873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1193873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1194873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1195873eaca6STejun Heo * defer the work to the currently executing one. 1196873eaca6STejun Heo */ 1197873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1198873eaca6STejun Heo if (unlikely(collision)) { 1199873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1200873eaca6STejun Heo return false; 1201873eaca6STejun Heo } 1202873eaca6STejun Heo 1203873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1204873eaca6STejun Heo return true; 1205873eaca6STejun Heo } 1206873eaca6STejun Heo 12072f34d733STejun Heo static struct irq_work *bh_pool_irq_work(struct worker_pool *pool) 12082f34d733STejun Heo { 12092f34d733STejun Heo int high = pool->attrs->nice == HIGHPRI_NICE_LEVEL ? 1 : 0; 12102f34d733STejun Heo 12112f34d733STejun Heo return &per_cpu(bh_pool_irq_works, pool->cpu)[high]; 12122f34d733STejun Heo } 12132f34d733STejun Heo 1214fd0a68a2STejun Heo static void kick_bh_pool(struct worker_pool *pool) 1215fd0a68a2STejun Heo { 1216fd0a68a2STejun Heo #ifdef CONFIG_SMP 1217fd0a68a2STejun Heo if (unlikely(pool->cpu != smp_processor_id())) { 1218fd0a68a2STejun Heo irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu); 1219fd0a68a2STejun Heo return; 1220fd0a68a2STejun Heo } 1221fd0a68a2STejun Heo #endif 1222fd0a68a2STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 1223fd0a68a2STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 1224fd0a68a2STejun Heo else 1225fd0a68a2STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 1226fd0a68a2STejun Heo } 1227fd0a68a2STejun Heo 1228873eaca6STejun Heo /** 12290219a352STejun Heo * kick_pool - wake up an idle worker if necessary 12300219a352STejun Heo * @pool: pool to kick 1231797e8345STejun Heo * 12320219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 12330219a352STejun Heo * whether a worker was woken up. 1234797e8345STejun Heo */ 12350219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1236797e8345STejun Heo { 1237797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 12388639ecebSTejun Heo struct task_struct *p; 1239797e8345STejun Heo 12400219a352STejun Heo lockdep_assert_held(&pool->lock); 12410219a352STejun Heo 12420219a352STejun Heo if (!need_more_worker(pool) || !worker) 12430219a352STejun Heo return false; 12440219a352STejun Heo 12454cb1ef64STejun Heo if (pool->flags & POOL_BH) { 1246fd0a68a2STejun Heo kick_bh_pool(pool); 12474cb1ef64STejun Heo return true; 12484cb1ef64STejun Heo } 12494cb1ef64STejun Heo 12508639ecebSTejun Heo p = worker->task; 12518639ecebSTejun Heo 12528639ecebSTejun Heo #ifdef CONFIG_SMP 12538639ecebSTejun Heo /* 12548639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 12558639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 12568639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 12578639ecebSTejun Heo * execution locality. 12588639ecebSTejun Heo * 12598639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 12608639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12618639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12628639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12638639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12648639ecebSTejun Heo * still on cpu when picking an idle worker. 12658639ecebSTejun Heo * 12668639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12678639ecebSTejun Heo * its affinity scope. Repatriate. 12688639ecebSTejun Heo */ 12698639ecebSTejun Heo if (!pool->attrs->affn_strict && 12708639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12718639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12728639ecebSTejun Heo struct work_struct, entry); 12738639ecebSTejun Heo p->wake_cpu = cpumask_any_distribute(pool->attrs->__pod_cpumask); 12748639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12758639ecebSTejun Heo } 12768639ecebSTejun Heo #endif 12778639ecebSTejun Heo wake_up_process(p); 12780219a352STejun Heo return true; 1279797e8345STejun Heo } 1280797e8345STejun Heo 128163638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 128263638450STejun Heo 128363638450STejun Heo /* 128463638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 128563638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 128663638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 128763638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 128863638450STejun Heo * should be using an unbound workqueue instead. 128963638450STejun Heo * 129063638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 129163638450STejun Heo * and report them so that they can be examined and converted to use unbound 129263638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 129363638450STejun Heo * function is tracked and reported with exponential backoff. 129463638450STejun Heo */ 129563638450STejun Heo #define WCI_MAX_ENTS 128 129663638450STejun Heo 129763638450STejun Heo struct wci_ent { 129863638450STejun Heo work_func_t func; 129963638450STejun Heo atomic64_t cnt; 130063638450STejun Heo struct hlist_node hash_node; 130163638450STejun Heo }; 130263638450STejun Heo 130363638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 130463638450STejun Heo static int wci_nr_ents; 130563638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 130663638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 130763638450STejun Heo 130863638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 130963638450STejun Heo { 131063638450STejun Heo struct wci_ent *ent; 131163638450STejun Heo 131263638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 131363638450STejun Heo (unsigned long)func) { 131463638450STejun Heo if (ent->func == func) 131563638450STejun Heo return ent; 131663638450STejun Heo } 131763638450STejun Heo return NULL; 131863638450STejun Heo } 131963638450STejun Heo 132063638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 132163638450STejun Heo { 132263638450STejun Heo struct wci_ent *ent; 132363638450STejun Heo 132463638450STejun Heo restart: 132563638450STejun Heo ent = wci_find_ent(func); 132663638450STejun Heo if (ent) { 132763638450STejun Heo u64 cnt; 132863638450STejun Heo 132963638450STejun Heo /* 133063638450STejun Heo * Start reporting from the fourth time and back off 133163638450STejun Heo * exponentially. 133263638450STejun Heo */ 133363638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 133463638450STejun Heo if (cnt >= 4 && is_power_of_2(cnt)) 133563638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 133663638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 133763638450STejun Heo atomic64_read(&ent->cnt)); 133863638450STejun Heo return; 133963638450STejun Heo } 134063638450STejun Heo 134163638450STejun Heo /* 134263638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 134363638450STejun Heo * is exhausted, something went really wrong and we probably made enough 134463638450STejun Heo * noise already. 134563638450STejun Heo */ 134663638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 134763638450STejun Heo return; 134863638450STejun Heo 134963638450STejun Heo raw_spin_lock(&wci_lock); 135063638450STejun Heo 135163638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 135263638450STejun Heo raw_spin_unlock(&wci_lock); 135363638450STejun Heo return; 135463638450STejun Heo } 135563638450STejun Heo 135663638450STejun Heo if (wci_find_ent(func)) { 135763638450STejun Heo raw_spin_unlock(&wci_lock); 135863638450STejun Heo goto restart; 135963638450STejun Heo } 136063638450STejun Heo 136163638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 136263638450STejun Heo ent->func = func; 136363638450STejun Heo atomic64_set(&ent->cnt, 1); 136463638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 136563638450STejun Heo 136663638450STejun Heo raw_spin_unlock(&wci_lock); 136763638450STejun Heo } 136863638450STejun Heo 136963638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 137063638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 137163638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 137263638450STejun Heo 1373c54d5046STejun Heo /** 13741da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13751da177e4SLinus Torvalds * @task: task waking up 13761da177e4SLinus Torvalds * 13771da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13801da177e4SLinus Torvalds { 13811da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13821da177e4SLinus Torvalds 1383c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13841da177e4SLinus Torvalds return; 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds /* 13871da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 13881da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 13891da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 13901da177e4SLinus Torvalds * pool. Protect against such race. 13911da177e4SLinus Torvalds */ 13921da177e4SLinus Torvalds preempt_disable(); 13931da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 13941da177e4SLinus Torvalds worker->pool->nr_running++; 13951da177e4SLinus Torvalds preempt_enable(); 1396616db877STejun Heo 1397616db877STejun Heo /* 1398616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1399616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1400616db877STejun Heo */ 1401616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1402616db877STejun Heo 1403c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds /** 14071da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 14081da177e4SLinus Torvalds * @task: task going to sleep 14091da177e4SLinus Torvalds * 14101da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 14111da177e4SLinus Torvalds * going to sleep. 14121da177e4SLinus Torvalds */ 14131da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 14141da177e4SLinus Torvalds { 14151da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 14161da177e4SLinus Torvalds struct worker_pool *pool; 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds /* 14191da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 14201da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 14211da177e4SLinus Torvalds * checking NOT_RUNNING. 14221da177e4SLinus Torvalds */ 14231da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 14241da177e4SLinus Torvalds return; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds pool = worker->pool; 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1429c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 14301da177e4SLinus Torvalds return; 14311da177e4SLinus Torvalds 1432c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 14331da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds /* 14361da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 14371da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 14381da177e4SLinus Torvalds * and nr_running has been reset. 14391da177e4SLinus Torvalds */ 14401da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 14411da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14421da177e4SLinus Torvalds return; 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds pool->nr_running--; 14460219a352STejun Heo if (kick_pool(pool)) 1447725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 14480219a352STejun Heo 14491da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds /** 1453616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1454616db877STejun Heo * @task: task currently running 1455616db877STejun Heo * 1456616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1457616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1458616db877STejun Heo */ 1459616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1460616db877STejun Heo { 1461616db877STejun Heo struct worker *worker = kthread_data(task); 1462616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1463616db877STejun Heo struct worker_pool *pool = worker->pool; 1464616db877STejun Heo 1465616db877STejun Heo if (!pwq) 1466616db877STejun Heo return; 1467616db877STejun Heo 14688a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14698a1dd1e5STejun Heo 147018c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 147118c8ae81SZqiang return; 147218c8ae81SZqiang 1473616db877STejun Heo /* 1474616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1475616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1476616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1477c8f6219bSZqiang * 1478c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1479c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1480c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1481c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1482c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1483c8f6219bSZqiang * We probably want to make this prettier in the future. 1484616db877STejun Heo */ 1485c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1486616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1487616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1488616db877STejun Heo return; 1489616db877STejun Heo 1490616db877STejun Heo raw_spin_lock(&pool->lock); 1491616db877STejun Heo 1492616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 149363638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1494616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1495616db877STejun Heo 14960219a352STejun Heo if (kick_pool(pool)) 1497616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1498616db877STejun Heo 1499616db877STejun Heo raw_spin_unlock(&pool->lock); 1500616db877STejun Heo } 1501616db877STejun Heo 1502616db877STejun Heo /** 15031da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 15041da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 15051da177e4SLinus Torvalds * 15061da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 15071da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 15081da177e4SLinus Torvalds * 15099b41ea72SAndrew Morton * CONTEXT: 15101da177e4SLinus Torvalds * raw_spin_lock_irq(rq->lock) 15111da177e4SLinus Torvalds * 1512f756d5e2SNathan Lynch * This function is called during schedule() when a kworker is going 1513f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 15141da177e4SLinus Torvalds * dequeuing, to allow periodic aggregation to shut-off when that 15151da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 15161da177e4SLinus Torvalds * 15171da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 15181da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 15191da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 1520365970a1SDavid Howells * is guaranteed to not be processing any works. 1521365970a1SDavid Howells * 1522365970a1SDavid Howells * Return: 1523365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1524365970a1SDavid Howells * hasn't executed any work yet. 1525365970a1SDavid Howells */ 1526365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1527365970a1SDavid Howells { 1528365970a1SDavid Howells struct worker *worker = kthread_data(task); 1529365970a1SDavid Howells 1530365970a1SDavid Howells return worker->last_func; 1531365970a1SDavid Howells } 1532365970a1SDavid Howells 1533d302f017STejun Heo /** 153491ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 153591ccc6e7STejun Heo * @wq: workqueue of interest 153691ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 153791ccc6e7STejun Heo * 153891ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 153991ccc6e7STejun Heo * 154091ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 154191ccc6e7STejun Heo * 154291ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 154391ccc6e7STejun Heo * 154491ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 154591ccc6e7STejun Heo */ 154691ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 154791ccc6e7STejun Heo int node) 154891ccc6e7STejun Heo { 154991ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 155091ccc6e7STejun Heo return NULL; 155191ccc6e7STejun Heo 155291ccc6e7STejun Heo if (node == NUMA_NO_NODE) 155391ccc6e7STejun Heo node = nr_node_ids; 155491ccc6e7STejun Heo 155591ccc6e7STejun Heo return wq->node_nr_active[node]; 155691ccc6e7STejun Heo } 155791ccc6e7STejun Heo 155891ccc6e7STejun Heo /** 15595797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 15605797b1c1STejun Heo * @wq: workqueue to update 15615797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15625797b1c1STejun Heo * 15635797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15645797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15655797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15665797b1c1STejun Heo */ 15675797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15685797b1c1STejun Heo { 15695797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15705797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15715797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15725797b1c1STejun Heo int total_cpus, node; 15735797b1c1STejun Heo 15745797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15755797b1c1STejun Heo 1576c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1577c5f8cd6cSTejun Heo return; 1578c5f8cd6cSTejun Heo 157915930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15805797b1c1STejun Heo off_cpu = -1; 15815797b1c1STejun Heo 15825797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15835797b1c1STejun Heo if (off_cpu >= 0) 15845797b1c1STejun Heo total_cpus--; 15855797b1c1STejun Heo 15865797b1c1STejun Heo for_each_node(node) { 15875797b1c1STejun Heo int node_cpus; 15885797b1c1STejun Heo 15895797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 15905797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 15915797b1c1STejun Heo node_cpus--; 15925797b1c1STejun Heo 15935797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 15945797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 15955797b1c1STejun Heo min_active, max_active); 15965797b1c1STejun Heo } 15975797b1c1STejun Heo 15985797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 15995797b1c1STejun Heo } 16005797b1c1STejun Heo 16015797b1c1STejun Heo /** 16028864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 16038864b4e5STejun Heo * @pwq: pool_workqueue to get 16048864b4e5STejun Heo * 16058864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 16068864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 16078864b4e5STejun Heo */ 16088864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 16098864b4e5STejun Heo { 16108864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16118864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 16128864b4e5STejun Heo pwq->refcnt++; 16138864b4e5STejun Heo } 16148864b4e5STejun Heo 16158864b4e5STejun Heo /** 16168864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 16178864b4e5STejun Heo * @pwq: pool_workqueue to put 16188864b4e5STejun Heo * 16198864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 16208864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 16218864b4e5STejun Heo */ 16228864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 16238864b4e5STejun Heo { 16248864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16258864b4e5STejun Heo if (likely(--pwq->refcnt)) 16268864b4e5STejun Heo return; 16278864b4e5STejun Heo /* 1628967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1629967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16308864b4e5STejun Heo */ 1631687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16328864b4e5STejun Heo } 16338864b4e5STejun Heo 1634dce90d47STejun Heo /** 1635dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1636dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1637dce90d47STejun Heo * 1638dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1639dce90d47STejun Heo */ 1640dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1641dce90d47STejun Heo { 1642dce90d47STejun Heo if (pwq) { 1643dce90d47STejun Heo /* 164424acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1645dce90d47STejun Heo * following lock operations are safe. 1646dce90d47STejun Heo */ 1647a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1648dce90d47STejun Heo put_pwq(pwq); 1649a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1650dce90d47STejun Heo } 1651dce90d47STejun Heo } 1652dce90d47STejun Heo 1653afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1654afa87ce8STejun Heo { 1655afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1656afa87ce8STejun Heo } 1657afa87ce8STejun Heo 16584c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16594c638030STejun Heo struct work_struct *work) 1660bf4ede01STejun Heo { 16611c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16621c270b79STejun Heo 16631c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1664bf4ede01STejun Heo trace_workqueue_activate_work(work); 166582607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 166682607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1667112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16681c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16694c638030STejun Heo } 16704c638030STejun Heo 16714c638030STejun Heo /** 16724c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16734c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16744c638030STejun Heo * @work: work item to activate 16754c638030STejun Heo * 16764c638030STejun Heo * Returns %true if activated. %false if already active. 16774c638030STejun Heo */ 16784c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16794c638030STejun Heo struct work_struct *work) 16804c638030STejun Heo { 16814c638030STejun Heo struct worker_pool *pool = pwq->pool; 168291ccc6e7STejun Heo struct wq_node_nr_active *nna; 16834c638030STejun Heo 16844c638030STejun Heo lockdep_assert_held(&pool->lock); 16854c638030STejun Heo 16864c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 16874c638030STejun Heo return false; 16884c638030STejun Heo 168991ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 169091ccc6e7STejun Heo if (nna) 169191ccc6e7STejun Heo atomic_inc(&nna->nr); 169291ccc6e7STejun Heo 1693112202d9STejun Heo pwq->nr_active++; 16944c638030STejun Heo __pwq_activate_work(pwq, work); 16954c638030STejun Heo return true; 1696bf4ede01STejun Heo } 1697bf4ede01STejun Heo 16985797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 16995797b1c1STejun Heo { 17005797b1c1STejun Heo int max = READ_ONCE(nna->max); 17015797b1c1STejun Heo 17025797b1c1STejun Heo while (true) { 17035797b1c1STejun Heo int old, tmp; 17045797b1c1STejun Heo 17055797b1c1STejun Heo old = atomic_read(&nna->nr); 17065797b1c1STejun Heo if (old >= max) 17075797b1c1STejun Heo return false; 17085797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 17095797b1c1STejun Heo if (tmp == old) 17105797b1c1STejun Heo return true; 17115797b1c1STejun Heo } 17125797b1c1STejun Heo } 17135797b1c1STejun Heo 17141c270b79STejun Heo /** 17151c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 17161c270b79STejun Heo * @pwq: pool_workqueue of interest 17175797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17181c270b79STejun Heo * 17191c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 17201c270b79STejun Heo * successfully obtained. %false otherwise. 17211c270b79STejun Heo */ 17225797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 17233aa62497SLai Jiangshan { 17241c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17251c270b79STejun Heo struct worker_pool *pool = pwq->pool; 172691ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17275797b1c1STejun Heo bool obtained = false; 17281c270b79STejun Heo 17291c270b79STejun Heo lockdep_assert_held(&pool->lock); 17301c270b79STejun Heo 17315797b1c1STejun Heo if (!nna) { 17324cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17331c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17345797b1c1STejun Heo goto out; 173591ccc6e7STejun Heo } 17365797b1c1STejun Heo 17374c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17384c065dbcSWaiman Long return false; 17394c065dbcSWaiman Long 17405797b1c1STejun Heo /* 17415797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17425797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17435797b1c1STejun Heo * concurrency level. Don't jump the line. 17445797b1c1STejun Heo * 17455797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17465797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17475797b1c1STejun Heo * increase it. This is indicated by @fill. 17485797b1c1STejun Heo */ 17495797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17505797b1c1STejun Heo goto out; 17515797b1c1STejun Heo 17525797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17535797b1c1STejun Heo if (obtained) 17545797b1c1STejun Heo goto out; 17555797b1c1STejun Heo 17565797b1c1STejun Heo /* 17575797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17585797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17595797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17605797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17615797b1c1STejun Heo * $nna->pending_pwqs. 17625797b1c1STejun Heo */ 17635797b1c1STejun Heo raw_spin_lock(&nna->lock); 17645797b1c1STejun Heo 17655797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17665797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17675797b1c1STejun Heo else if (likely(!fill)) 17685797b1c1STejun Heo goto out_unlock; 17695797b1c1STejun Heo 17705797b1c1STejun Heo smp_mb(); 17715797b1c1STejun Heo 17725797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17735797b1c1STejun Heo 17745797b1c1STejun Heo /* 17755797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17765797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17775797b1c1STejun Heo */ 17785797b1c1STejun Heo if (obtained && likely(!fill)) 17795797b1c1STejun Heo list_del_init(&pwq->pending_node); 17805797b1c1STejun Heo 17815797b1c1STejun Heo out_unlock: 17825797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17835797b1c1STejun Heo out: 17845797b1c1STejun Heo if (obtained) 17855797b1c1STejun Heo pwq->nr_active++; 17861c270b79STejun Heo return obtained; 17871c270b79STejun Heo } 17881c270b79STejun Heo 17891c270b79STejun Heo /** 17901c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 17911c270b79STejun Heo * @pwq: pool_workqueue of interest 17925797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17931c270b79STejun Heo * 17941c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 17951c270b79STejun Heo * max_active limit. 17961c270b79STejun Heo * 17971c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 17981c270b79STejun Heo * inactive work item is found or max_active limit is reached. 17991c270b79STejun Heo */ 18005797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 18011c270b79STejun Heo { 18021c270b79STejun Heo struct work_struct *work = 18031c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 18043aa62497SLai Jiangshan struct work_struct, entry); 18053aa62497SLai Jiangshan 18065797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 18071c270b79STejun Heo __pwq_activate_work(pwq, work); 18081c270b79STejun Heo return true; 18091c270b79STejun Heo } else { 18101c270b79STejun Heo return false; 18111c270b79STejun Heo } 18121c270b79STejun Heo } 18131c270b79STejun Heo 18141c270b79STejun Heo /** 1815516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1816516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 18174c065dbcSWaiman Long * 1818516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1819516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1820516d3dc9SWaiman Long * ensure proper work item ordering:: 18214c065dbcSWaiman Long * 18224c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 18234c065dbcSWaiman Long * | 18244c065dbcSWaiman Long * v 18254c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18264c065dbcSWaiman Long * | | | 18274c065dbcSWaiman Long * 1 3 5 18284c065dbcSWaiman Long * | | | 18294c065dbcSWaiman Long * 2 4 6 1830516d3dc9SWaiman Long * 1831516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1832516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1833516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1834516d3dc9SWaiman Long * the list is the oldest. 18354c065dbcSWaiman Long */ 18364c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18374c065dbcSWaiman Long { 18384c065dbcSWaiman Long struct pool_workqueue *pwq; 18394c065dbcSWaiman Long 18404c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18414c065dbcSWaiman Long 18424c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18434c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18444c065dbcSWaiman Long pwqs_node); 18454c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18464c065dbcSWaiman Long if (pwq->plugged) { 18474c065dbcSWaiman Long pwq->plugged = false; 18484c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18494c065dbcSWaiman Long kick_pool(pwq->pool); 18504c065dbcSWaiman Long } 18514c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18524c065dbcSWaiman Long } 18534c065dbcSWaiman Long 18544c065dbcSWaiman Long /** 18555797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18565797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18575797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18585797b1c1STejun Heo * 18595797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18605797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18615797b1c1STejun Heo */ 18625797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18635797b1c1STejun Heo struct worker_pool *caller_pool) 18645797b1c1STejun Heo { 18655797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18665797b1c1STejun Heo struct pool_workqueue *pwq; 18675797b1c1STejun Heo struct work_struct *work; 18685797b1c1STejun Heo 18695797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18705797b1c1STejun Heo 18715797b1c1STejun Heo raw_spin_lock(&nna->lock); 18725797b1c1STejun Heo retry: 18735797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18745797b1c1STejun Heo struct pool_workqueue, pending_node); 18755797b1c1STejun Heo if (!pwq) 18765797b1c1STejun Heo goto out_unlock; 18775797b1c1STejun Heo 18785797b1c1STejun Heo /* 18795797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 18805797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 18815797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 18825797b1c1STejun Heo * nested inside pool locks. 18835797b1c1STejun Heo */ 18845797b1c1STejun Heo if (pwq->pool != locked_pool) { 18855797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 18865797b1c1STejun Heo locked_pool = pwq->pool; 18875797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 18885797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18895797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 18905797b1c1STejun Heo raw_spin_lock(&nna->lock); 18915797b1c1STejun Heo goto retry; 18925797b1c1STejun Heo } 18935797b1c1STejun Heo } 18945797b1c1STejun Heo 18955797b1c1STejun Heo /* 18965797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 18975797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 18985797b1c1STejun Heo */ 18995797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 19005797b1c1STejun Heo struct work_struct, entry); 19015797b1c1STejun Heo if (!work) { 19025797b1c1STejun Heo list_del_init(&pwq->pending_node); 19035797b1c1STejun Heo goto retry; 19045797b1c1STejun Heo } 19055797b1c1STejun Heo 19065797b1c1STejun Heo /* 19075797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 19085797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 19095797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 19105797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 19115797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 19125797b1c1STejun Heo */ 19135797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 19145797b1c1STejun Heo pwq->nr_active++; 19155797b1c1STejun Heo __pwq_activate_work(pwq, work); 19165797b1c1STejun Heo 19175797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 19185797b1c1STejun Heo list_del_init(&pwq->pending_node); 19195797b1c1STejun Heo else 19205797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 19215797b1c1STejun Heo 19225797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 19235797b1c1STejun Heo if (pwq->pool != caller_pool) 19245797b1c1STejun Heo kick_pool(pwq->pool); 19255797b1c1STejun Heo } 19265797b1c1STejun Heo 19275797b1c1STejun Heo out_unlock: 19285797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19295797b1c1STejun Heo if (locked_pool != caller_pool) { 19305797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19315797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19325797b1c1STejun Heo } 19335797b1c1STejun Heo } 19345797b1c1STejun Heo 19355797b1c1STejun Heo /** 19361c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19371c270b79STejun Heo * @pwq: pool_workqueue of interest 19381c270b79STejun Heo * 19391c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19405797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19411c270b79STejun Heo */ 19421c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19431c270b79STejun Heo { 19441c270b79STejun Heo struct worker_pool *pool = pwq->pool; 194591ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19461c270b79STejun Heo 19471c270b79STejun Heo lockdep_assert_held(&pool->lock); 19481c270b79STejun Heo 194991ccc6e7STejun Heo /* 195091ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 195191ccc6e7STejun Heo * workqueues. 195291ccc6e7STejun Heo */ 19531c270b79STejun Heo pwq->nr_active--; 195491ccc6e7STejun Heo 195591ccc6e7STejun Heo /* 195691ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 195791ccc6e7STejun Heo * inactive work item on @pwq itself. 195891ccc6e7STejun Heo */ 195991ccc6e7STejun Heo if (!nna) { 19605797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 196191ccc6e7STejun Heo return; 196291ccc6e7STejun Heo } 196391ccc6e7STejun Heo 19645797b1c1STejun Heo /* 19655797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19665797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19675797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19685797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19695797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19705797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19715797b1c1STejun Heo * decremented $nna->nr. 19725797b1c1STejun Heo * 19735797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19745797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19755797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 19765797b1c1STejun Heo * This maintains the forward progress guarantee. 19775797b1c1STejun Heo */ 19785797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 19795797b1c1STejun Heo return; 19805797b1c1STejun Heo 19815797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 19825797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 19833aa62497SLai Jiangshan } 19843aa62497SLai Jiangshan 1985bf4ede01STejun Heo /** 1986112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 1987112202d9STejun Heo * @pwq: pwq of interest 1988c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 1989bf4ede01STejun Heo * 1990bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 1991112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 1992bf4ede01STejun Heo * 1993dd6c3c54STejun Heo * NOTE: 1994dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 1995dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 1996dd6c3c54STejun Heo * work item is complete. 1997dd6c3c54STejun Heo * 1998bf4ede01STejun Heo * CONTEXT: 1999a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2000bf4ede01STejun Heo */ 2001c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 2002bf4ede01STejun Heo { 2003c4560c2cSLai Jiangshan int color = get_work_color(work_data); 2004c4560c2cSLai Jiangshan 20051c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 20061c270b79STejun Heo pwq_dec_nr_active(pwq); 2007018f3a13SLai Jiangshan 2008018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 2009bf4ede01STejun Heo 2010bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 2011112202d9STejun Heo if (likely(pwq->flush_color != color)) 20128864b4e5STejun Heo goto out_put; 2013bf4ede01STejun Heo 2014bf4ede01STejun Heo /* are there still in-flight works? */ 2015112202d9STejun Heo if (pwq->nr_in_flight[color]) 20168864b4e5STejun Heo goto out_put; 2017bf4ede01STejun Heo 2018112202d9STejun Heo /* this pwq is done, clear flush_color */ 2019112202d9STejun Heo pwq->flush_color = -1; 2020bf4ede01STejun Heo 2021bf4ede01STejun Heo /* 2022112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 2023bf4ede01STejun Heo * will handle the rest. 2024bf4ede01STejun Heo */ 2025112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2026112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20278864b4e5STejun Heo out_put: 20288864b4e5STejun Heo put_pwq(pwq); 2029bf4ede01STejun Heo } 2030bf4ede01STejun Heo 203136e227d2STejun Heo /** 2032bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 203336e227d2STejun Heo * @work: work item to steal 2034c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags 2035c26e2f2eSTejun Heo * @irq_flags: place to store irq state 203636e227d2STejun Heo * 203736e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2038d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 203936e227d2STejun Heo * 2040d185af30SYacine Belkadi * Return: 20413eb6b31bSMauro Carvalho Chehab * 20423eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 204336e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 204436e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 204536e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 2046bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 2047bbb68dfaSTejun Heo * for arbitrarily long 20483eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 204936e227d2STejun Heo * 2050d185af30SYacine Belkadi * Note: 2051bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2052e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2053e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2054e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2055bbb68dfaSTejun Heo * 2056bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2057c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags). 2058bbb68dfaSTejun Heo * 2059e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2060bf4ede01STejun Heo */ 2061c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags, 2062c26e2f2eSTejun Heo unsigned long *irq_flags) 2063bf4ede01STejun Heo { 2064d565ed63STejun Heo struct worker_pool *pool; 2065112202d9STejun Heo struct pool_workqueue *pwq; 2066bf4ede01STejun Heo 2067c26e2f2eSTejun Heo local_irq_save(*irq_flags); 2068bbb68dfaSTejun Heo 206936e227d2STejun Heo /* try to steal the timer if it exists */ 2070c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) { 207136e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 207236e227d2STejun Heo 2073e0aecdd8STejun Heo /* 2074e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2075e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2076e0aecdd8STejun Heo * running on the local CPU. 2077e0aecdd8STejun Heo */ 207836e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 207936e227d2STejun Heo return 1; 208036e227d2STejun Heo } 208136e227d2STejun Heo 208236e227d2STejun Heo /* try to claim PENDING the normal way */ 2083bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2084bf4ede01STejun Heo return 0; 2085bf4ede01STejun Heo 208624acfb71SThomas Gleixner rcu_read_lock(); 2087bf4ede01STejun Heo /* 2088bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2089bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2090bf4ede01STejun Heo */ 2091d565ed63STejun Heo pool = get_work_pool(work); 2092d565ed63STejun Heo if (!pool) 2093bbb68dfaSTejun Heo goto fail; 2094bf4ede01STejun Heo 2095a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2096bf4ede01STejun Heo /* 2097112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2098112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2099112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2100112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2101112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 21020b3dae68SLai Jiangshan * item is currently queued on that pool. 2103bf4ede01STejun Heo */ 2104112202d9STejun Heo pwq = get_work_pwq(work); 2105112202d9STejun Heo if (pwq && pwq->pool == pool) { 2106c70e1779STejun Heo unsigned long work_data; 2107c70e1779STejun Heo 2108bf4ede01STejun Heo debug_work_deactivate(work); 21093aa62497SLai Jiangshan 21103aa62497SLai Jiangshan /* 2111018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2112018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2113018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2114018f3a13SLai Jiangshan * 2115f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2116d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2117f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 211816062836STejun Heo * management later on and cause stall. Make sure the work 211916062836STejun Heo * item is activated before grabbing. 21203aa62497SLai Jiangshan */ 21214c638030STejun Heo pwq_activate_work(pwq, work); 21223aa62497SLai Jiangshan 2123bf4ede01STejun Heo list_del_init(&work->entry); 212436e227d2STejun Heo 2125c70e1779STejun Heo /* 2126c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2127c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2128c70e1779STejun Heo */ 2129c70e1779STejun Heo work_data = *work_data_bits(work); 2130*bccdc1faSTejun Heo set_work_pool_and_keep_pending(work, pool->id, 0); 21314468a00fSLai Jiangshan 2132dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2133c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2134dd6c3c54STejun Heo 2135a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 213624acfb71SThomas Gleixner rcu_read_unlock(); 213736e227d2STejun Heo return 1; 2138bf4ede01STejun Heo } 2139a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2140bbb68dfaSTejun Heo fail: 214124acfb71SThomas Gleixner rcu_read_unlock(); 2142c26e2f2eSTejun Heo local_irq_restore(*irq_flags); 2143bbb68dfaSTejun Heo if (work_is_canceling(work)) 2144bbb68dfaSTejun Heo return -ENOENT; 2145bbb68dfaSTejun Heo cpu_relax(); 214636e227d2STejun Heo return -EAGAIN; 2147bf4ede01STejun Heo } 2148bf4ede01STejun Heo 2149978b8409STejun Heo struct cwt_wait { 2150978b8409STejun Heo wait_queue_entry_t wait; 2151978b8409STejun Heo struct work_struct *work; 2152978b8409STejun Heo }; 2153978b8409STejun Heo 2154978b8409STejun Heo static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 2155978b8409STejun Heo { 2156978b8409STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 2157978b8409STejun Heo 2158978b8409STejun Heo if (cwait->work != key) 2159978b8409STejun Heo return 0; 2160978b8409STejun Heo return autoremove_wake_function(wait, mode, sync, key); 2161978b8409STejun Heo } 2162978b8409STejun Heo 2163978b8409STejun Heo /** 2164978b8409STejun Heo * work_grab_pending - steal work item from worklist and disable irq 2165978b8409STejun Heo * @work: work item to steal 2166978b8409STejun Heo * @cflags: %WORK_CANCEL_ flags 2167978b8409STejun Heo * @irq_flags: place to store IRQ state 2168978b8409STejun Heo * 2169978b8409STejun Heo * Grab PENDING bit of @work. @work can be in any stable state - idle, on timer 2170978b8409STejun Heo * or on worklist. 2171978b8409STejun Heo * 2172978b8409STejun Heo * Must be called in process context. IRQ is disabled on return with IRQ state 2173978b8409STejun Heo * stored in *@irq_flags. The caller is responsible for re-enabling it using 2174978b8409STejun Heo * local_irq_restore(). 2175978b8409STejun Heo * 2176978b8409STejun Heo * Returns %true if @work was pending. %false if idle. 2177978b8409STejun Heo */ 2178978b8409STejun Heo static bool work_grab_pending(struct work_struct *work, u32 cflags, 2179978b8409STejun Heo unsigned long *irq_flags) 2180978b8409STejun Heo { 2181978b8409STejun Heo struct cwt_wait cwait; 2182978b8409STejun Heo int ret; 2183978b8409STejun Heo 2184978b8409STejun Heo might_sleep(); 2185978b8409STejun Heo repeat: 2186978b8409STejun Heo ret = try_to_grab_pending(work, cflags, irq_flags); 2187978b8409STejun Heo if (likely(ret >= 0)) 2188978b8409STejun Heo return ret; 2189978b8409STejun Heo if (ret != -ENOENT) 2190978b8409STejun Heo goto repeat; 2191978b8409STejun Heo 2192978b8409STejun Heo /* 2193978b8409STejun Heo * Someone is already canceling. Wait for it to finish. flush_work() 2194978b8409STejun Heo * doesn't work for PREEMPT_NONE because we may get woken up between 2195978b8409STejun Heo * @work's completion and the other canceling task resuming and clearing 2196978b8409STejun Heo * CANCELING - flush_work() will return false immediately as @work is no 2197978b8409STejun Heo * longer busy, try_to_grab_pending() will return -ENOENT as @work is 2198978b8409STejun Heo * still being canceled and the other canceling task won't be able to 2199978b8409STejun Heo * clear CANCELING as we're hogging the CPU. 2200978b8409STejun Heo * 2201978b8409STejun Heo * Let's wait for completion using a waitqueue. As this may lead to the 2202978b8409STejun Heo * thundering herd problem, use a custom wake function which matches 2203978b8409STejun Heo * @work along with exclusive wait and wakeup. 2204978b8409STejun Heo */ 2205978b8409STejun Heo init_wait(&cwait.wait); 2206978b8409STejun Heo cwait.wait.func = cwt_wakefn; 2207978b8409STejun Heo cwait.work = work; 2208978b8409STejun Heo 2209978b8409STejun Heo prepare_to_wait_exclusive(&wq_cancel_waitq, &cwait.wait, 2210978b8409STejun Heo TASK_UNINTERRUPTIBLE); 2211978b8409STejun Heo if (work_is_canceling(work)) 2212978b8409STejun Heo schedule(); 2213978b8409STejun Heo finish_wait(&wq_cancel_waitq, &cwait.wait); 2214978b8409STejun Heo 2215978b8409STejun Heo goto repeat; 2216978b8409STejun Heo } 2217978b8409STejun Heo 2218bf4ede01STejun Heo /** 2219706026c2STejun Heo * insert_work - insert a work into a pool 2220112202d9STejun Heo * @pwq: pwq @work belongs to 22214690c4abSTejun Heo * @work: work to insert 22224690c4abSTejun Heo * @head: insertion point 22234690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 22244690c4abSTejun Heo * 2225112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2226706026c2STejun Heo * work_struct flags. 22274690c4abSTejun Heo * 22284690c4abSTejun Heo * CONTEXT: 2229a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2230365970a1SDavid Howells */ 2231112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2232112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2233b89deed3SOleg Nesterov { 2234fe089f87STejun Heo debug_work_activate(work); 2235e1d8aa9fSFrederic Weisbecker 2236e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2237f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2238e89a85d6SWalter Wu 22394690c4abSTejun Heo /* we own @work, set data and link */ 2240112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 22411a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 22428864b4e5STejun Heo get_pwq(pwq); 2243b89deed3SOleg Nesterov } 2244b89deed3SOleg Nesterov 2245c8efcc25STejun Heo /* 2246c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 22478d03ecfeSTejun Heo * same workqueue. 2248c8efcc25STejun Heo */ 2249c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2250c8efcc25STejun Heo { 2251c8efcc25STejun Heo struct worker *worker; 2252c8efcc25STejun Heo 22538d03ecfeSTejun Heo worker = current_wq_worker(); 2254c8efcc25STejun Heo /* 2255bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 22568d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2257c8efcc25STejun Heo */ 2258112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2259c8efcc25STejun Heo } 2260c8efcc25STejun Heo 2261ef557180SMike Galbraith /* 2262ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2263ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2264ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2265ef557180SMike Galbraith */ 2266ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2267ef557180SMike Galbraith { 2268ef557180SMike Galbraith int new_cpu; 2269ef557180SMike Galbraith 2270f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2271ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2272ef557180SMike Galbraith return cpu; 2273a8ec5880SAmmar Faizi } else { 2274a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2275f303fccbSTejun Heo } 2276f303fccbSTejun Heo 2277ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2278ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2279ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2280ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2281ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2282ef557180SMike Galbraith return cpu; 2283ef557180SMike Galbraith } 2284ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2285ef557180SMike Galbraith 2286ef557180SMike Galbraith return new_cpu; 2287ef557180SMike Galbraith } 2288ef557180SMike Galbraith 2289d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 22901da177e4SLinus Torvalds struct work_struct *work) 22911da177e4SLinus Torvalds { 2292112202d9STejun Heo struct pool_workqueue *pwq; 2293fe089f87STejun Heo struct worker_pool *last_pool, *pool; 22948a2e8e5dSTejun Heo unsigned int work_flags; 2295b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 22968930cabaSTejun Heo 22978930cabaSTejun Heo /* 22988930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 22998930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 23008930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 23018930cabaSTejun Heo * happen with IRQ disabled. 23028930cabaSTejun Heo */ 23038e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 23041da177e4SLinus Torvalds 230533e3f0a3SRichard Clark /* 230633e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 230733e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 230833e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 230933e3f0a3SRichard Clark */ 231033e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 231133e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2312e41e704bSTejun Heo return; 231324acfb71SThomas Gleixner rcu_read_lock(); 23149e8cd2f5STejun Heo retry: 2315aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2316636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2317636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2318ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2319636b927eSTejun Heo else 2320aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2321aa202f1fSHillf Danton } 2322f3421797STejun Heo 2323636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2324fe089f87STejun Heo pool = pwq->pool; 2325fe089f87STejun Heo 232618aa9effSTejun Heo /* 2327c9178087STejun Heo * If @work was previously on a different pool, it might still be 2328c9178087STejun Heo * running there, in which case the work needs to be queued on that 2329c9178087STejun Heo * pool to guarantee non-reentrancy. 233018aa9effSTejun Heo */ 2331c9e7cf27STejun Heo last_pool = get_work_pool(work); 2332fe089f87STejun Heo if (last_pool && last_pool != pool) { 233318aa9effSTejun Heo struct worker *worker; 233418aa9effSTejun Heo 2335a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 233618aa9effSTejun Heo 2337c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 233818aa9effSTejun Heo 2339112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2340c9178087STejun Heo pwq = worker->current_pwq; 2341fe089f87STejun Heo pool = pwq->pool; 2342fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 23438594fadeSLai Jiangshan } else { 234418aa9effSTejun Heo /* meh... not running there, queue here */ 2345a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2346fe089f87STejun Heo raw_spin_lock(&pool->lock); 234718aa9effSTejun Heo } 23488930cabaSTejun Heo } else { 2349fe089f87STejun Heo raw_spin_lock(&pool->lock); 23508930cabaSTejun Heo } 2351502ca9d8STejun Heo 23529e8cd2f5STejun Heo /* 2353636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2354636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2355636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2356636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2357636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 23589e8cd2f5STejun Heo */ 23599e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 23609e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2361fe089f87STejun Heo raw_spin_unlock(&pool->lock); 23629e8cd2f5STejun Heo cpu_relax(); 23639e8cd2f5STejun Heo goto retry; 23649e8cd2f5STejun Heo } 23659e8cd2f5STejun Heo /* oops */ 23669e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 23679e8cd2f5STejun Heo wq->name, cpu); 23689e8cd2f5STejun Heo } 23699e8cd2f5STejun Heo 2370112202d9STejun Heo /* pwq determined, queue */ 2371112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2372502ca9d8STejun Heo 237324acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 237424acfb71SThomas Gleixner goto out; 23751e19ffc6STejun Heo 2376112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2377112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 23781e19ffc6STejun Heo 2379a045a272STejun Heo /* 2380a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2381a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2382a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2383a045a272STejun Heo */ 23845797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2385fe089f87STejun Heo if (list_empty(&pool->worklist)) 2386fe089f87STejun Heo pool->watchdog_ts = jiffies; 2387fe089f87STejun Heo 2388cdadf009STejun Heo trace_workqueue_activate_work(work); 2389fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 23900219a352STejun Heo kick_pool(pool); 23918a2e8e5dSTejun Heo } else { 2392f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2393fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 23948a2e8e5dSTejun Heo } 23951e19ffc6STejun Heo 239624acfb71SThomas Gleixner out: 2397fe089f87STejun Heo raw_spin_unlock(&pool->lock); 239824acfb71SThomas Gleixner rcu_read_unlock(); 23991da177e4SLinus Torvalds } 24001da177e4SLinus Torvalds 24010fcb78c2SRolf Eike Beer /** 2402c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2403c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2404c1a220e7SZhang Rui * @wq: workqueue to use 2405c1a220e7SZhang Rui * @work: work to queue 2406c1a220e7SZhang Rui * 2407c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2408443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2409443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2410854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2411854f5cc5SPaul E. McKenney * online will get a splat. 2412d185af30SYacine Belkadi * 2413d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2414c1a220e7SZhang Rui */ 2415d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2416d4283e93STejun Heo struct work_struct *work) 2417c1a220e7SZhang Rui { 2418d4283e93STejun Heo bool ret = false; 2419c26e2f2eSTejun Heo unsigned long irq_flags; 24208930cabaSTejun Heo 2421c26e2f2eSTejun Heo local_irq_save(irq_flags); 2422c1a220e7SZhang Rui 242322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24244690c4abSTejun Heo __queue_work(cpu, wq, work); 2425d4283e93STejun Heo ret = true; 2426c1a220e7SZhang Rui } 24278930cabaSTejun Heo 2428c26e2f2eSTejun Heo local_irq_restore(irq_flags); 2429c1a220e7SZhang Rui return ret; 2430c1a220e7SZhang Rui } 2431ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2432c1a220e7SZhang Rui 24338204e0c1SAlexander Duyck /** 2434fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 24358204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 24368204e0c1SAlexander Duyck * 24378204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 24388204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 24398204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 24408204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 24418204e0c1SAlexander Duyck */ 2442fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 24438204e0c1SAlexander Duyck { 24448204e0c1SAlexander Duyck int cpu; 24458204e0c1SAlexander Duyck 24468204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 24478204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 24488204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 24498204e0c1SAlexander Duyck 24508204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 24518204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 24528204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 24538204e0c1SAlexander Duyck return cpu; 24548204e0c1SAlexander Duyck 24558204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 24568204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 24578204e0c1SAlexander Duyck 24588204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 24598204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 24608204e0c1SAlexander Duyck } 24618204e0c1SAlexander Duyck 24628204e0c1SAlexander Duyck /** 24638204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 24648204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 24658204e0c1SAlexander Duyck * @wq: workqueue to use 24668204e0c1SAlexander Duyck * @work: work to queue 24678204e0c1SAlexander Duyck * 24688204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 24698204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 24708204e0c1SAlexander Duyck * NUMA node. 24718204e0c1SAlexander Duyck * 24728204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 24738204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 24748204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 24758204e0c1SAlexander Duyck * 24768204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 24778204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 24788204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 24798204e0c1SAlexander Duyck * 24808204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 24818204e0c1SAlexander Duyck */ 24828204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 24838204e0c1SAlexander Duyck struct work_struct *work) 24848204e0c1SAlexander Duyck { 2485c26e2f2eSTejun Heo unsigned long irq_flags; 24868204e0c1SAlexander Duyck bool ret = false; 24878204e0c1SAlexander Duyck 24888204e0c1SAlexander Duyck /* 24898204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 24908204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 24918204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 24928204e0c1SAlexander Duyck * 24938204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 24948204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 24958204e0c1SAlexander Duyck * some round robin type logic. 24968204e0c1SAlexander Duyck */ 24978204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 24988204e0c1SAlexander Duyck 2499c26e2f2eSTejun Heo local_irq_save(irq_flags); 25008204e0c1SAlexander Duyck 25018204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2502fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 25038204e0c1SAlexander Duyck 25048204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 25058204e0c1SAlexander Duyck ret = true; 25068204e0c1SAlexander Duyck } 25078204e0c1SAlexander Duyck 2508c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25098204e0c1SAlexander Duyck return ret; 25108204e0c1SAlexander Duyck } 25118204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 25128204e0c1SAlexander Duyck 25138c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 25141da177e4SLinus Torvalds { 25158c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 25161da177e4SLinus Torvalds 2517e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 251860c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 25191da177e4SLinus Torvalds } 25201438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 25211da177e4SLinus Torvalds 25227beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 252352bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25241da177e4SLinus Torvalds { 25257beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 25267beb2edfSTejun Heo struct work_struct *work = &dwork->work; 25271da177e4SLinus Torvalds 2528637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 25294b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2530fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2531fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 25327beb2edfSTejun Heo 25338852aac2STejun Heo /* 25348852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 25358852aac2STejun Heo * both optimization and correctness. The earliest @timer can 25368852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 25378852aac2STejun Heo * on that there's no such delay when @delay is 0. 25388852aac2STejun Heo */ 25398852aac2STejun Heo if (!delay) { 25408852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 25418852aac2STejun Heo return; 25428852aac2STejun Heo } 25438852aac2STejun Heo 254460c057bcSLai Jiangshan dwork->wq = wq; 25451265057fSTejun Heo dwork->cpu = cpu; 25467beb2edfSTejun Heo timer->expires = jiffies + delay; 25477beb2edfSTejun Heo 2548aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2549aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2550aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2551aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2552aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 25537beb2edfSTejun Heo add_timer_on(timer, cpu); 2554aae17ebbSLeonardo Bras } else { 2555aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2556041bd12eSTejun Heo add_timer(timer); 2557aae17ebbSLeonardo Bras else 2558aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2559aae17ebbSLeonardo Bras } 25607beb2edfSTejun Heo } 25611da177e4SLinus Torvalds 25620fcb78c2SRolf Eike Beer /** 25630fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 25640fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 25650fcb78c2SRolf Eike Beer * @wq: workqueue to use 2566af9997e4SRandy Dunlap * @dwork: work to queue 25670fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 25680fcb78c2SRolf Eike Beer * 2569d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2570715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2571715f1300STejun Heo * execution. 25720fcb78c2SRolf Eike Beer */ 2573d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 257452bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25757a6bc1cdSVenkatesh Pallipadi { 257652bad64dSDavid Howells struct work_struct *work = &dwork->work; 2577d4283e93STejun Heo bool ret = false; 2578c26e2f2eSTejun Heo unsigned long irq_flags; 25798930cabaSTejun Heo 25808930cabaSTejun Heo /* read the comment in __queue_work() */ 2581c26e2f2eSTejun Heo local_irq_save(irq_flags); 25827a6bc1cdSVenkatesh Pallipadi 258322df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 25847beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2585d4283e93STejun Heo ret = true; 25867a6bc1cdSVenkatesh Pallipadi } 25878930cabaSTejun Heo 2588c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25897a6bc1cdSVenkatesh Pallipadi return ret; 25907a6bc1cdSVenkatesh Pallipadi } 2591ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 25921da177e4SLinus Torvalds 2593c8e55f36STejun Heo /** 25948376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 25958376fe22STejun Heo * @cpu: CPU number to execute work on 25968376fe22STejun Heo * @wq: workqueue to use 25978376fe22STejun Heo * @dwork: work to queue 25988376fe22STejun Heo * @delay: number of jiffies to wait before queueing 25998376fe22STejun Heo * 26008376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 26018376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 26028376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 26038376fe22STejun Heo * current state. 26048376fe22STejun Heo * 2605d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 26068376fe22STejun Heo * pending and its timer was modified. 26078376fe22STejun Heo * 2608e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 26098376fe22STejun Heo * See try_to_grab_pending() for details. 26108376fe22STejun Heo */ 26118376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 26128376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 26138376fe22STejun Heo { 2614c26e2f2eSTejun Heo unsigned long irq_flags; 26158376fe22STejun Heo int ret; 26168376fe22STejun Heo 26178376fe22STejun Heo do { 2618c5f5b942STejun Heo ret = try_to_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, 2619c5f5b942STejun Heo &irq_flags); 26208376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 26218376fe22STejun Heo 26228376fe22STejun Heo if (likely(ret >= 0)) { 26238376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2624c26e2f2eSTejun Heo local_irq_restore(irq_flags); 26258376fe22STejun Heo } 26268376fe22STejun Heo 26278376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 26288376fe22STejun Heo return ret; 26298376fe22STejun Heo } 26308376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 26318376fe22STejun Heo 263205f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 263305f0fe6bSTejun Heo { 263405f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 263505f0fe6bSTejun Heo 263605f0fe6bSTejun Heo /* read the comment in __queue_work() */ 263705f0fe6bSTejun Heo local_irq_disable(); 263805f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 263905f0fe6bSTejun Heo local_irq_enable(); 264005f0fe6bSTejun Heo } 264105f0fe6bSTejun Heo 264205f0fe6bSTejun Heo /** 264305f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 264405f0fe6bSTejun Heo * @wq: workqueue to use 264505f0fe6bSTejun Heo * @rwork: work to queue 264605f0fe6bSTejun Heo * 264705f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 264805f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2649bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 265005f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 265105f0fe6bSTejun Heo */ 265205f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 265305f0fe6bSTejun Heo { 265405f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 265505f0fe6bSTejun Heo 265605f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 265705f0fe6bSTejun Heo rwork->wq = wq; 2658a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 265905f0fe6bSTejun Heo return true; 266005f0fe6bSTejun Heo } 266105f0fe6bSTejun Heo 266205f0fe6bSTejun Heo return false; 266305f0fe6bSTejun Heo } 266405f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 266505f0fe6bSTejun Heo 2666f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2667c34056a3STejun Heo { 2668c34056a3STejun Heo struct worker *worker; 2669c34056a3STejun Heo 2670f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2671c8e55f36STejun Heo if (worker) { 2672c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2673affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2674da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2675e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2676e22bee78STejun Heo worker->flags = WORKER_PREP; 2677c8e55f36STejun Heo } 2678c34056a3STejun Heo return worker; 2679c34056a3STejun Heo } 2680c34056a3STejun Heo 26819546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 26829546b29eSTejun Heo { 26838639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 26849546b29eSTejun Heo return pool->attrs->__pod_cpumask; 26858639ecebSTejun Heo else 26868639ecebSTejun Heo return pool->attrs->cpumask; 26879546b29eSTejun Heo } 26889546b29eSTejun Heo 2689c34056a3STejun Heo /** 26904736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 26914736cbf7SLai Jiangshan * @worker: worker to be attached 26924736cbf7SLai Jiangshan * @pool: the target pool 26934736cbf7SLai Jiangshan * 26944736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 26954736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 26964736cbf7SLai Jiangshan * cpu-[un]hotplugs. 26974736cbf7SLai Jiangshan */ 26984736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 26994736cbf7SLai Jiangshan struct worker_pool *pool) 27004736cbf7SLai Jiangshan { 27011258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 27024736cbf7SLai Jiangshan 27034736cbf7SLai Jiangshan /* 27044cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 27054cb1ef64STejun Heo * across this function. See the comments above the flag definition for 27064cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 27074736cbf7SLai Jiangshan */ 27084cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 27094736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 27104cb1ef64STejun Heo } else { 27114cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27125c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 27134cb1ef64STejun Heo } 27144736cbf7SLai Jiangshan 2715640f17c8SPeter Zijlstra if (worker->rescue_wq) 27169546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2717640f17c8SPeter Zijlstra 27184736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2719a2d812a2STejun Heo worker->pool = pool; 27204736cbf7SLai Jiangshan 27211258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 27224736cbf7SLai Jiangshan } 27234736cbf7SLai Jiangshan 27244736cbf7SLai Jiangshan /** 272560f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 272660f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 272760f5a4bcSLai Jiangshan * 27284736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 27294736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 27304736cbf7SLai Jiangshan * other reference to the pool. 273160f5a4bcSLai Jiangshan */ 2732a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 273360f5a4bcSLai Jiangshan { 2734a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 273560f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 273660f5a4bcSLai Jiangshan 27374cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 27384cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27394cb1ef64STejun Heo 27401258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2741a2d812a2STejun Heo 27425c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2743da028469SLai Jiangshan list_del(&worker->node); 2744a2d812a2STejun Heo worker->pool = NULL; 2745a2d812a2STejun Heo 2746e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 274760f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 27481258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 274960f5a4bcSLai Jiangshan 2750b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2751b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2752b62c0751SLai Jiangshan 275360f5a4bcSLai Jiangshan if (detach_completion) 275460f5a4bcSLai Jiangshan complete(detach_completion); 275560f5a4bcSLai Jiangshan } 275660f5a4bcSLai Jiangshan 275760f5a4bcSLai Jiangshan /** 2758c34056a3STejun Heo * create_worker - create a new workqueue worker 275963d95a91STejun Heo * @pool: pool the new worker will belong to 2760c34056a3STejun Heo * 2761051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2762c34056a3STejun Heo * 2763c34056a3STejun Heo * CONTEXT: 2764c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2765c34056a3STejun Heo * 2766d185af30SYacine Belkadi * Return: 2767c34056a3STejun Heo * Pointer to the newly created worker. 2768c34056a3STejun Heo */ 2769bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2770c34056a3STejun Heo { 2771e441b56fSZhen Lei struct worker *worker; 2772e441b56fSZhen Lei int id; 27735d9c7a1eSLucy Mielke char id_buf[23]; 2774c34056a3STejun Heo 27757cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2776e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 27773f0ea0b8SPetr Mladek if (id < 0) { 27783f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 27793f0ea0b8SPetr Mladek ERR_PTR(id)); 2780e441b56fSZhen Lei return NULL; 27813f0ea0b8SPetr Mladek } 2782c34056a3STejun Heo 2783f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 27843f0ea0b8SPetr Mladek if (!worker) { 27853f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2786c34056a3STejun Heo goto fail; 27873f0ea0b8SPetr Mladek } 2788c34056a3STejun Heo 2789c34056a3STejun Heo worker->id = id; 2790c34056a3STejun Heo 27914cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 279229c91e99STejun Heo if (pool->cpu >= 0) 2793e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2794e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2795f3421797STejun Heo else 2796e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2797e3c916a4STejun Heo 27984cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 27994cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 28003f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 280160f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 280260f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 280360f54038SPetr Mladek id_buf); 280460f54038SPetr Mladek } else { 28053f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 28063f0ea0b8SPetr Mladek worker->task); 280760f54038SPetr Mladek } 2808c34056a3STejun Heo goto fail; 28093f0ea0b8SPetr Mladek } 2810c34056a3STejun Heo 281191151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 28129546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 28134cb1ef64STejun Heo } 281491151228SOleg Nesterov 2815da028469SLai Jiangshan /* successful, attach the worker to the pool */ 28164736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2817822d8405STejun Heo 2818051e1850SLai Jiangshan /* start the newly created worker */ 2819a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 28200219a352STejun Heo 2821051e1850SLai Jiangshan worker->pool->nr_workers++; 2822051e1850SLai Jiangshan worker_enter_idle(worker); 28230219a352STejun Heo 28240219a352STejun Heo /* 28250219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 28266a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 28276a229b0eSTejun Heo * wake it up explicitly. 28280219a352STejun Heo */ 28294cb1ef64STejun Heo if (worker->task) 2830051e1850SLai Jiangshan wake_up_process(worker->task); 28310219a352STejun Heo 2832a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2833051e1850SLai Jiangshan 2834c34056a3STejun Heo return worker; 2835822d8405STejun Heo 2836c34056a3STejun Heo fail: 2837e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2838c34056a3STejun Heo kfree(worker); 2839c34056a3STejun Heo return NULL; 2840c34056a3STejun Heo } 2841c34056a3STejun Heo 2842793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2843793777bcSValentin Schneider { 2844793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2845793777bcSValentin Schneider 2846793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2847793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2848793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2849793777bcSValentin Schneider else 2850793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2851793777bcSValentin Schneider } 2852793777bcSValentin Schneider 2853e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2854e02b9312SValentin Schneider { 2855e02b9312SValentin Schneider struct worker *worker, *tmp; 2856e02b9312SValentin Schneider 2857e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2858e02b9312SValentin Schneider list_del_init(&worker->entry); 2859e02b9312SValentin Schneider unbind_worker(worker); 2860e02b9312SValentin Schneider /* 2861e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2862e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2863e02b9312SValentin Schneider * wouldn't have gotten here. 2864c34056a3STejun Heo * 2865e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2866e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2867e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2868e02b9312SValentin Schneider * outside of pool->lock. 2869e02b9312SValentin Schneider */ 2870e02b9312SValentin Schneider wake_up_process(worker->task); 2871e02b9312SValentin Schneider } 2872e02b9312SValentin Schneider } 2873e02b9312SValentin Schneider 2874e02b9312SValentin Schneider /** 2875e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2876e02b9312SValentin Schneider * @worker: worker to be destroyed 2877e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2878e02b9312SValentin Schneider * 2879e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2880e02b9312SValentin Schneider * should be idle. 2881c8e55f36STejun Heo * 2882c8e55f36STejun Heo * CONTEXT: 2883a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2884c34056a3STejun Heo */ 2885e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2886c34056a3STejun Heo { 2887bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2888c34056a3STejun Heo 2889cd549687STejun Heo lockdep_assert_held(&pool->lock); 2890e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2891cd549687STejun Heo 2892c34056a3STejun Heo /* sanity check frenzy */ 28936183c009STejun Heo if (WARN_ON(worker->current_work) || 289473eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 289573eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 28966183c009STejun Heo return; 2897c34056a3STejun Heo 2898bd7bdd43STejun Heo pool->nr_workers--; 2899bd7bdd43STejun Heo pool->nr_idle--; 2900c8e55f36STejun Heo 2901cb444766STejun Heo worker->flags |= WORKER_DIE; 2902e02b9312SValentin Schneider 2903e02b9312SValentin Schneider list_move(&worker->entry, list); 2904e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2905c34056a3STejun Heo } 2906c34056a3STejun Heo 29073f959aa3SValentin Schneider /** 29083f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 29093f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 29103f959aa3SValentin Schneider * 29113f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 29123f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 29133f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 29143f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 29153f959aa3SValentin Schneider * it expire and re-evaluate things from there. 29163f959aa3SValentin Schneider */ 291732a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2918e22bee78STejun Heo { 291932a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 29203f959aa3SValentin Schneider bool do_cull = false; 29213f959aa3SValentin Schneider 29223f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 29233f959aa3SValentin Schneider return; 29243f959aa3SValentin Schneider 29253f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 29263f959aa3SValentin Schneider 29273f959aa3SValentin Schneider if (too_many_workers(pool)) { 29283f959aa3SValentin Schneider struct worker *worker; 29293f959aa3SValentin Schneider unsigned long expires; 29303f959aa3SValentin Schneider 29313f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 29323f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 29333f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 29343f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 29353f959aa3SValentin Schneider 29363f959aa3SValentin Schneider if (!do_cull) 29373f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 29383f959aa3SValentin Schneider } 29393f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 29403f959aa3SValentin Schneider 29413f959aa3SValentin Schneider if (do_cull) 29423f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 29433f959aa3SValentin Schneider } 29443f959aa3SValentin Schneider 29453f959aa3SValentin Schneider /** 29463f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 29473f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 29483f959aa3SValentin Schneider * 29493f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 29503f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2951e02b9312SValentin Schneider * 2952e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2953e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2954e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 29553f959aa3SValentin Schneider */ 29563f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 29573f959aa3SValentin Schneider { 29583f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 29599680540cSYang Yingliang LIST_HEAD(cull_list); 2960e22bee78STejun Heo 2961e02b9312SValentin Schneider /* 2962e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2963e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2964e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2965e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2966e02b9312SValentin Schneider */ 2967e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2968a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2969e22bee78STejun Heo 29703347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2971e22bee78STejun Heo struct worker *worker; 2972e22bee78STejun Heo unsigned long expires; 2973e22bee78STejun Heo 297463d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2975e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2976e22bee78STejun Heo 29773347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 297863d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 29793347fc9fSLai Jiangshan break; 2980e22bee78STejun Heo } 29813347fc9fSLai Jiangshan 2982e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2983e22bee78STejun Heo } 2984e22bee78STejun Heo 2985a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2986e02b9312SValentin Schneider wake_dying_workers(&cull_list); 2987e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 2988e22bee78STejun Heo } 2989e22bee78STejun Heo 2990493a1724STejun Heo static void send_mayday(struct work_struct *work) 2991e22bee78STejun Heo { 2992112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 2993112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 2994493a1724STejun Heo 29952e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 2996e22bee78STejun Heo 2997493008a8STejun Heo if (!wq->rescuer) 2998493a1724STejun Heo return; 2999e22bee78STejun Heo 3000e22bee78STejun Heo /* mayday mayday mayday */ 3001493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 300277668c8bSLai Jiangshan /* 300377668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 300477668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 300577668c8bSLai Jiangshan * rescuer is done with it. 300677668c8bSLai Jiangshan */ 300777668c8bSLai Jiangshan get_pwq(pwq); 3008493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3009e22bee78STejun Heo wake_up_process(wq->rescuer->task); 3010725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 3011493a1724STejun Heo } 3012e22bee78STejun Heo } 3013e22bee78STejun Heo 301432a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 3015e22bee78STejun Heo { 301632a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 3017e22bee78STejun Heo struct work_struct *work; 3018e22bee78STejun Heo 3019a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3020a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 3021e22bee78STejun Heo 302263d95a91STejun Heo if (need_to_create_worker(pool)) { 3023e22bee78STejun Heo /* 3024e22bee78STejun Heo * We've been trying to create a new worker but 3025e22bee78STejun Heo * haven't been successful. We might be hitting an 3026e22bee78STejun Heo * allocation deadlock. Send distress signals to 3027e22bee78STejun Heo * rescuers. 3028e22bee78STejun Heo */ 302963d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 3030e22bee78STejun Heo send_mayday(work); 3031e22bee78STejun Heo } 3032e22bee78STejun Heo 3033a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3034a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3035e22bee78STejun Heo 303663d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 3037e22bee78STejun Heo } 3038e22bee78STejun Heo 3039e22bee78STejun Heo /** 3040e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 304163d95a91STejun Heo * @pool: pool to create a new worker for 3042e22bee78STejun Heo * 304363d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 3044e22bee78STejun Heo * have at least one idle worker on return from this function. If 3045e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 304663d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 3047e22bee78STejun Heo * possible allocation deadlock. 3048e22bee78STejun Heo * 3049c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 3050c5aa87bbSTejun Heo * may_start_working() %true. 3051e22bee78STejun Heo * 3052e22bee78STejun Heo * LOCKING: 3053a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3054e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 3055e22bee78STejun Heo * manager. 3056e22bee78STejun Heo */ 305729187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 3058d565ed63STejun Heo __releases(&pool->lock) 3059d565ed63STejun Heo __acquires(&pool->lock) 3060e22bee78STejun Heo { 3061e22bee78STejun Heo restart: 3062a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30639f9c2364STejun Heo 3064e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 306563d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 3066e22bee78STejun Heo 3067e22bee78STejun Heo while (true) { 3068051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 3069e22bee78STejun Heo break; 3070e22bee78STejun Heo 3071e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 30729f9c2364STejun Heo 307363d95a91STejun Heo if (!need_to_create_worker(pool)) 3074e22bee78STejun Heo break; 3075e22bee78STejun Heo } 3076e22bee78STejun Heo 307763d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 3078a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3079051e1850SLai Jiangshan /* 3080051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 3081051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 3082051e1850SLai Jiangshan * already become busy. 3083051e1850SLai Jiangshan */ 308463d95a91STejun Heo if (need_to_create_worker(pool)) 3085e22bee78STejun Heo goto restart; 3086e22bee78STejun Heo } 3087e22bee78STejun Heo 3088e22bee78STejun Heo /** 3089e22bee78STejun Heo * manage_workers - manage worker pool 3090e22bee78STejun Heo * @worker: self 3091e22bee78STejun Heo * 3092706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 3093e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3094706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3095e22bee78STejun Heo * 3096e22bee78STejun Heo * The caller can safely start processing works on false return. On 3097e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3098e22bee78STejun Heo * and may_start_working() is true. 3099e22bee78STejun Heo * 3100e22bee78STejun Heo * CONTEXT: 3101a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3102e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3103e22bee78STejun Heo * 3104d185af30SYacine Belkadi * Return: 310529187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 310629187a9eSTejun Heo * start processing works, %true if management function was performed and 310729187a9eSTejun Heo * the conditions that the caller verified before calling the function may 310829187a9eSTejun Heo * no longer be true. 3109e22bee78STejun Heo */ 3110e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3111e22bee78STejun Heo { 311263d95a91STejun Heo struct worker_pool *pool = worker->pool; 3113e22bee78STejun Heo 3114692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 311529187a9eSTejun Heo return false; 3116692b4825STejun Heo 3117692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 31182607d7a6STejun Heo pool->manager = worker; 3119e22bee78STejun Heo 312029187a9eSTejun Heo maybe_create_worker(pool); 3121e22bee78STejun Heo 31222607d7a6STejun Heo pool->manager = NULL; 3123692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3124d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 312529187a9eSTejun Heo return true; 3126e22bee78STejun Heo } 3127e22bee78STejun Heo 3128a62428c0STejun Heo /** 3129a62428c0STejun Heo * process_one_work - process single work 3130c34056a3STejun Heo * @worker: self 3131a62428c0STejun Heo * @work: work to process 3132a62428c0STejun Heo * 3133a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3134a62428c0STejun Heo * process a single work including synchronization against and 3135a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3136a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3137a62428c0STejun Heo * call this function to process a work. 3138a62428c0STejun Heo * 3139a62428c0STejun Heo * CONTEXT: 3140a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3141a62428c0STejun Heo */ 3142c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3143d565ed63STejun Heo __releases(&pool->lock) 3144d565ed63STejun Heo __acquires(&pool->lock) 31451da177e4SLinus Torvalds { 3146112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3147bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3148c4560c2cSLai Jiangshan unsigned long work_data; 3149c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 31504e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 31514e6045f1SJohannes Berg /* 3152a62428c0STejun Heo * It is permissible to free the struct work_struct from 3153a62428c0STejun Heo * inside the function that is called from it, this we need to 3154a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3155a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3156a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 31574e6045f1SJohannes Berg */ 31584d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 31594d82a1deSPeter Zijlstra 31604d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 31614e6045f1SJohannes Berg #endif 3162807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 316385327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3164ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 316525511a47STejun Heo 31668930cabaSTejun Heo /* claim and dequeue */ 3167dc186ad7SThomas Gleixner debug_work_deactivate(work); 3168c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3169c34056a3STejun Heo worker->current_work = work; 3170a2c1c57bSTejun Heo worker->current_func = work->func; 3171112202d9STejun Heo worker->current_pwq = pwq; 31724cb1ef64STejun Heo if (worker->task) 3173616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3174c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3175d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 31767a22ad75STejun Heo 31778bf89593STejun Heo /* 31788bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 31798bf89593STejun Heo * overridden through set_worker_desc(). 31808bf89593STejun Heo */ 31818bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 31828bf89593STejun Heo 3183a62428c0STejun Heo list_del_init(&work->entry); 3184a62428c0STejun Heo 3185649027d7STejun Heo /* 3186228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3187228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3188228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3189228f1d00SLai Jiangshan * execution of the pending work items. 3190fb0e7bebSTejun Heo */ 3191616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3192228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3193fb0e7bebSTejun Heo 3194974271c4STejun Heo /* 31950219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 31960219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 31970219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 31980219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3199974271c4STejun Heo */ 32000219a352STejun Heo kick_pool(pool); 3201974271c4STejun Heo 32028930cabaSTejun Heo /* 32037c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3204d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 320523657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 320623657bb1STejun Heo * disabled. 32078930cabaSTejun Heo */ 3208*bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, pool->id, 0); 32091da177e4SLinus Torvalds 3210fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3211a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3212365970a1SDavid Howells 3213c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3214c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 3215a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 32163295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3217e6f3faa7SPeter Zijlstra /* 3218f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3219f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3220e6f3faa7SPeter Zijlstra * 3221e6f3faa7SPeter Zijlstra * However, that would result in: 3222e6f3faa7SPeter Zijlstra * 3223e6f3faa7SPeter Zijlstra * A(W1) 3224e6f3faa7SPeter Zijlstra * WFC(C) 3225e6f3faa7SPeter Zijlstra * A(W1) 3226e6f3faa7SPeter Zijlstra * C(C) 3227e6f3faa7SPeter Zijlstra * 3228e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3229e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3230e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3231f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3232e6f3faa7SPeter Zijlstra * these locks. 3233e6f3faa7SPeter Zijlstra * 3234e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3235e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3236e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3237e6f3faa7SPeter Zijlstra */ 3238f52be570SPeter Zijlstra lockdep_invariant_state(true); 3239e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3240a2c1c57bSTejun Heo worker->current_func(work); 3241e36c886aSArjan van de Ven /* 3242e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3243e36c886aSArjan van de Ven * point will only record its address. 3244e36c886aSArjan van de Ven */ 32451c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3246725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 32473295f0efSIngo Molnar lock_map_release(&lockdep_map); 3248112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 32491da177e4SLinus Torvalds 3250c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3251c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3252c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3253c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3254c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3255c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3256c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3257c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3258c35aea39STejun Heo worker->current_func); 3259d5abe669SPeter Zijlstra debug_show_held_locks(current); 3260d5abe669SPeter Zijlstra dump_stack(); 3261d5abe669SPeter Zijlstra } 3262d5abe669SPeter Zijlstra 3263b22ce278STejun Heo /* 3264025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3265b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3266b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3267b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3268789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3269789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3270b22ce278STejun Heo */ 32714cb1ef64STejun Heo if (worker->task) 3272a7e6425eSPaul E. McKenney cond_resched(); 3273b22ce278STejun Heo 3274a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3275a62428c0STejun Heo 3276616db877STejun Heo /* 3277616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3278616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3279616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3280616db877STejun Heo */ 3281fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3282fb0e7bebSTejun Heo 32831b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 32841b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 32851b69ac6bSJohannes Weiner 3286a62428c0STejun Heo /* we're done with it, release */ 328742f8570fSSasha Levin hash_del(&worker->hentry); 3288c34056a3STejun Heo worker->current_work = NULL; 3289a2c1c57bSTejun Heo worker->current_func = NULL; 3290112202d9STejun Heo worker->current_pwq = NULL; 3291d812796eSLai Jiangshan worker->current_color = INT_MAX; 3292dd6c3c54STejun Heo 3293dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3294c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 32951da177e4SLinus Torvalds } 32961da177e4SLinus Torvalds 3297affee4b2STejun Heo /** 3298affee4b2STejun Heo * process_scheduled_works - process scheduled works 3299affee4b2STejun Heo * @worker: self 3300affee4b2STejun Heo * 3301affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3302affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3303affee4b2STejun Heo * fetches a work from the top and executes it. 3304affee4b2STejun Heo * 3305affee4b2STejun Heo * CONTEXT: 3306a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3307affee4b2STejun Heo * multiple times. 3308affee4b2STejun Heo */ 3309affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 33101da177e4SLinus Torvalds { 3311c0ab017dSTejun Heo struct work_struct *work; 3312c0ab017dSTejun Heo bool first = true; 3313c0ab017dSTejun Heo 3314c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3315c0ab017dSTejun Heo struct work_struct, entry))) { 3316c0ab017dSTejun Heo if (first) { 3317c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3318c0ab017dSTejun Heo first = false; 3319c0ab017dSTejun Heo } 3320c34056a3STejun Heo process_one_work(worker, work); 3321a62428c0STejun Heo } 33221da177e4SLinus Torvalds } 33231da177e4SLinus Torvalds 3324197f6accSTejun Heo static void set_pf_worker(bool val) 3325197f6accSTejun Heo { 3326197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3327197f6accSTejun Heo if (val) 3328197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3329197f6accSTejun Heo else 3330197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3331197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3332197f6accSTejun Heo } 3333197f6accSTejun Heo 33344690c4abSTejun Heo /** 33354690c4abSTejun Heo * worker_thread - the worker thread function 3336c34056a3STejun Heo * @__worker: self 33374690c4abSTejun Heo * 3338c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3339c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3340c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3341c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3342c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3343d185af30SYacine Belkadi * 3344d185af30SYacine Belkadi * Return: 0 33454690c4abSTejun Heo */ 3346c34056a3STejun Heo static int worker_thread(void *__worker) 33471da177e4SLinus Torvalds { 3348c34056a3STejun Heo struct worker *worker = __worker; 3349bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 33501da177e4SLinus Torvalds 3351e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3352197f6accSTejun Heo set_pf_worker(true); 3353c8e55f36STejun Heo woke_up: 3354a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3355affee4b2STejun Heo 3356a9ab775bSTejun Heo /* am I supposed to die? */ 3357a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3358a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3359197f6accSTejun Heo set_pf_worker(false); 336060f5a4bcSLai Jiangshan 336160f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3362e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3363a2d812a2STejun Heo worker_detach_from_pool(worker); 3364e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 336560f5a4bcSLai Jiangshan kfree(worker); 3366c8e55f36STejun Heo return 0; 3367c8e55f36STejun Heo } 3368c8e55f36STejun Heo 3369c8e55f36STejun Heo worker_leave_idle(worker); 3370db7bccf4STejun Heo recheck: 3371e22bee78STejun Heo /* no more worker necessary? */ 337263d95a91STejun Heo if (!need_more_worker(pool)) 3373e22bee78STejun Heo goto sleep; 3374e22bee78STejun Heo 3375e22bee78STejun Heo /* do we need to manage? */ 337663d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3377e22bee78STejun Heo goto recheck; 3378e22bee78STejun Heo 3379c8e55f36STejun Heo /* 3380c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3381c8e55f36STejun Heo * preparing to process a work or actually processing it. 3382c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3383c8e55f36STejun Heo */ 33846183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3385c8e55f36STejun Heo 3386e22bee78STejun Heo /* 3387a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3388a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3389a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3390a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3391a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3392e22bee78STejun Heo */ 3393a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3394e22bee78STejun Heo 3395e22bee78STejun Heo do { 3396affee4b2STejun Heo struct work_struct *work = 3397bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3398affee4b2STejun Heo struct work_struct, entry); 3399affee4b2STejun Heo 3400873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3401affee4b2STejun Heo process_scheduled_works(worker); 340263d95a91STejun Heo } while (keep_working(pool)); 3403affee4b2STejun Heo 3404228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3405d313dd85STejun Heo sleep: 3406c8e55f36STejun Heo /* 3407d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3408d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3409d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3410d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3411d565ed63STejun Heo * event. 3412c8e55f36STejun Heo */ 3413c8e55f36STejun Heo worker_enter_idle(worker); 3414c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3415a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 34161da177e4SLinus Torvalds schedule(); 3417c8e55f36STejun Heo goto woke_up; 34181da177e4SLinus Torvalds } 34191da177e4SLinus Torvalds 3420e22bee78STejun Heo /** 3421e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3422111c225aSTejun Heo * @__rescuer: self 3423e22bee78STejun Heo * 3424e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3425493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3426e22bee78STejun Heo * 3427706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3428e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3429e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3430e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3431e22bee78STejun Heo * the problem rescuer solves. 3432e22bee78STejun Heo * 3433706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3434706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3435e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3436e22bee78STejun Heo * 3437e22bee78STejun Heo * This should happen rarely. 3438d185af30SYacine Belkadi * 3439d185af30SYacine Belkadi * Return: 0 3440e22bee78STejun Heo */ 3441111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3442e22bee78STejun Heo { 3443111c225aSTejun Heo struct worker *rescuer = __rescuer; 3444111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 34454d595b86SLai Jiangshan bool should_stop; 3446e22bee78STejun Heo 3447e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3448111c225aSTejun Heo 3449111c225aSTejun Heo /* 3450111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3451111c225aSTejun Heo * doesn't participate in concurrency management. 3452111c225aSTejun Heo */ 3453197f6accSTejun Heo set_pf_worker(true); 3454e22bee78STejun Heo repeat: 3455c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 34561da177e4SLinus Torvalds 34574d595b86SLai Jiangshan /* 34584d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 34594d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 34604d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 34614d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 34624d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 34634d595b86SLai Jiangshan * list is always empty on exit. 34644d595b86SLai Jiangshan */ 34654d595b86SLai Jiangshan should_stop = kthread_should_stop(); 34661da177e4SLinus Torvalds 3467493a1724STejun Heo /* see whether any pwq is asking for help */ 3468a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3469493a1724STejun Heo 3470493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3471493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3472493a1724STejun Heo struct pool_workqueue, mayday_node); 3473112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3474e22bee78STejun Heo struct work_struct *work, *n; 3475e22bee78STejun Heo 3476e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3477493a1724STejun Heo list_del_init(&pwq->mayday_node); 3478493a1724STejun Heo 3479a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3480e22bee78STejun Heo 348151697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 348251697d39SLai Jiangshan 3483a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3484e22bee78STejun Heo 3485e22bee78STejun Heo /* 3486e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3487e22bee78STejun Heo * process'em. 3488e22bee78STejun Heo */ 3489873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 349082607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3491873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3492873eaca6STejun Heo assign_work(work, rescuer, &n)) 3493725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 349482607adcSTejun Heo } 3495e22bee78STejun Heo 3496873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3497e22bee78STejun Heo process_scheduled_works(rescuer); 34987576958aSTejun Heo 34997576958aSTejun Heo /* 3500008847f6SNeilBrown * The above execution of rescued work items could 3501008847f6SNeilBrown * have created more to rescue through 3502f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3503008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3504008847f6SNeilBrown * that such back-to-back work items, which may be 3505008847f6SNeilBrown * being used to relieve memory pressure, don't 3506008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3507008847f6SNeilBrown */ 35084f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3509a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3510e66b39afSTejun Heo /* 3511e66b39afSTejun Heo * Queue iff we aren't racing destruction 3512e66b39afSTejun Heo * and somebody else hasn't queued it already. 3513e66b39afSTejun Heo */ 3514e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3515008847f6SNeilBrown get_pwq(pwq); 3516e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3517e66b39afSTejun Heo } 3518a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3519008847f6SNeilBrown } 3520008847f6SNeilBrown } 3521008847f6SNeilBrown 3522008847f6SNeilBrown /* 352377668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 352413b1d625SLai Jiangshan * go away while we're still attached to it. 352577668c8bSLai Jiangshan */ 352677668c8bSLai Jiangshan put_pwq(pwq); 352777668c8bSLai Jiangshan 352877668c8bSLai Jiangshan /* 35290219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 35300219a352STejun Heo * with 0 concurrency and stalling the execution. 35317576958aSTejun Heo */ 35320219a352STejun Heo kick_pool(pool); 35337576958aSTejun Heo 3534a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 353513b1d625SLai Jiangshan 3536a2d812a2STejun Heo worker_detach_from_pool(rescuer); 353713b1d625SLai Jiangshan 3538a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 35391da177e4SLinus Torvalds } 35401da177e4SLinus Torvalds 3541a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3542493a1724STejun Heo 35434d595b86SLai Jiangshan if (should_stop) { 35444d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3545197f6accSTejun Heo set_pf_worker(false); 35464d595b86SLai Jiangshan return 0; 35474d595b86SLai Jiangshan } 35484d595b86SLai Jiangshan 3549111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3550111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3551e22bee78STejun Heo schedule(); 3552e22bee78STejun Heo goto repeat; 35531da177e4SLinus Torvalds } 35541da177e4SLinus Torvalds 35554cb1ef64STejun Heo static void bh_worker(struct worker *worker) 35564cb1ef64STejun Heo { 35574cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 35584cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 35594cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 35604cb1ef64STejun Heo 35614cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 35624cb1ef64STejun Heo worker_leave_idle(worker); 35634cb1ef64STejun Heo 35644cb1ef64STejun Heo /* 35654cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 35664cb1ef64STejun Heo * explanations on each step. 35674cb1ef64STejun Heo */ 35684cb1ef64STejun Heo if (!need_more_worker(pool)) 35694cb1ef64STejun Heo goto done; 35704cb1ef64STejun Heo 35714cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 35724cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 35734cb1ef64STejun Heo 35744cb1ef64STejun Heo do { 35754cb1ef64STejun Heo struct work_struct *work = 35764cb1ef64STejun Heo list_first_entry(&pool->worklist, 35774cb1ef64STejun Heo struct work_struct, entry); 35784cb1ef64STejun Heo 35794cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 35804cb1ef64STejun Heo process_scheduled_works(worker); 35814cb1ef64STejun Heo } while (keep_working(pool) && 35824cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 35834cb1ef64STejun Heo 35844cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 35854cb1ef64STejun Heo done: 35864cb1ef64STejun Heo worker_enter_idle(worker); 35874cb1ef64STejun Heo kick_pool(pool); 35884cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 35894cb1ef64STejun Heo } 35904cb1ef64STejun Heo 35914cb1ef64STejun Heo /* 35924cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 35934cb1ef64STejun Heo * 35944cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 35954cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 35964cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 35974cb1ef64STejun Heo * can be dropped. 35984cb1ef64STejun Heo * 35994cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 36004cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 36014cb1ef64STejun Heo */ 36024cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 36034cb1ef64STejun Heo { 36044cb1ef64STejun Heo struct worker_pool *pool = 36054cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 36064cb1ef64STejun Heo if (need_more_worker(pool)) 36074cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36084cb1ef64STejun Heo } 36094cb1ef64STejun Heo 3610fca839c0STejun Heo /** 3611fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3612fca839c0STejun Heo * @target_wq: workqueue being flushed 3613fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3614fca839c0STejun Heo * 3615fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3616fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3617fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3618fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3619fca839c0STejun Heo * a deadlock. 3620fca839c0STejun Heo */ 3621fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3622fca839c0STejun Heo struct work_struct *target_work) 3623fca839c0STejun Heo { 3624fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3625fca839c0STejun Heo struct worker *worker; 3626fca839c0STejun Heo 3627fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3628fca839c0STejun Heo return; 3629fca839c0STejun Heo 3630fca839c0STejun Heo worker = current_wq_worker(); 3631fca839c0STejun Heo 3632fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3633d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3634fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 363523d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 363623d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3637d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3638fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3639fca839c0STejun Heo target_wq->name, target_func); 3640fca839c0STejun Heo } 3641fca839c0STejun Heo 3642fc2e4d70SOleg Nesterov struct wq_barrier { 3643fc2e4d70SOleg Nesterov struct work_struct work; 3644fc2e4d70SOleg Nesterov struct completion done; 36452607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3646fc2e4d70SOleg Nesterov }; 3647fc2e4d70SOleg Nesterov 3648fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3649fc2e4d70SOleg Nesterov { 3650fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3651fc2e4d70SOleg Nesterov complete(&barr->done); 3652fc2e4d70SOleg Nesterov } 3653fc2e4d70SOleg Nesterov 36544690c4abSTejun Heo /** 36554690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3656112202d9STejun Heo * @pwq: pwq to insert barrier into 36574690c4abSTejun Heo * @barr: wq_barrier to insert 3658affee4b2STejun Heo * @target: target work to attach @barr to 3659affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 36604690c4abSTejun Heo * 3661affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3662affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3663affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3664affee4b2STejun Heo * cpu. 3665affee4b2STejun Heo * 3666affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3667affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3668affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3669affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3670affee4b2STejun Heo * after a work with LINKED flag set. 3671affee4b2STejun Heo * 3672affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3673112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 36744690c4abSTejun Heo * 36754690c4abSTejun Heo * CONTEXT: 3676a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 36774690c4abSTejun Heo */ 3678112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3679affee4b2STejun Heo struct wq_barrier *barr, 3680affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3681fc2e4d70SOleg Nesterov { 36824cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3683d812796eSLai Jiangshan unsigned int work_flags = 0; 3684d812796eSLai Jiangshan unsigned int work_color; 3685affee4b2STejun Heo struct list_head *head; 3686affee4b2STejun Heo 3687dc186ad7SThomas Gleixner /* 3688d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3689dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3690dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3691dc186ad7SThomas Gleixner * might deadlock. 36924cb1ef64STejun Heo * 36934cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 36944cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 36954cb1ef64STejun Heo * usage". 3696dc186ad7SThomas Gleixner */ 36974cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 36984cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 369922df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 370052fa5bc5SBoqun Feng 3701fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3702fd1a5b04SByungchul Park 37032607d7a6STejun Heo barr->task = current; 370483c22520SOleg Nesterov 37055797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3706018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3707018f3a13SLai Jiangshan 3708affee4b2STejun Heo /* 3709affee4b2STejun Heo * If @target is currently being executed, schedule the 3710affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3711affee4b2STejun Heo */ 3712d812796eSLai Jiangshan if (worker) { 3713affee4b2STejun Heo head = worker->scheduled.next; 3714d812796eSLai Jiangshan work_color = worker->current_color; 3715d812796eSLai Jiangshan } else { 3716affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3717affee4b2STejun Heo 3718affee4b2STejun Heo head = target->entry.next; 3719affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3720d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3721d812796eSLai Jiangshan work_color = get_work_color(*bits); 3722affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3723affee4b2STejun Heo } 3724affee4b2STejun Heo 3725d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3726d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3727d812796eSLai Jiangshan 3728d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3729fc2e4d70SOleg Nesterov } 3730fc2e4d70SOleg Nesterov 373173f53c4aSTejun Heo /** 3732112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 373373f53c4aSTejun Heo * @wq: workqueue being flushed 373473f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 373573f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 373673f53c4aSTejun Heo * 3737112202d9STejun Heo * Prepare pwqs for workqueue flushing. 373873f53c4aSTejun Heo * 3739112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3740112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3741112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3742112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3743112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 374473f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 374573f53c4aSTejun Heo * 374673f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 374773f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 374873f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 374973f53c4aSTejun Heo * is returned. 375073f53c4aSTejun Heo * 3751112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 375273f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 375373f53c4aSTejun Heo * advanced to @work_color. 375473f53c4aSTejun Heo * 375573f53c4aSTejun Heo * CONTEXT: 37563c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 375773f53c4aSTejun Heo * 3758d185af30SYacine Belkadi * Return: 375973f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 376073f53c4aSTejun Heo * otherwise. 376173f53c4aSTejun Heo */ 3762112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 376373f53c4aSTejun Heo int flush_color, int work_color) 37641da177e4SLinus Torvalds { 376573f53c4aSTejun Heo bool wait = false; 376649e3cf44STejun Heo struct pool_workqueue *pwq; 37671da177e4SLinus Torvalds 376873f53c4aSTejun Heo if (flush_color >= 0) { 37696183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3770112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3771dc186ad7SThomas Gleixner } 377214441960SOleg Nesterov 377349e3cf44STejun Heo for_each_pwq(pwq, wq) { 3774112202d9STejun Heo struct worker_pool *pool = pwq->pool; 37751da177e4SLinus Torvalds 3776a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 377773f53c4aSTejun Heo 377873f53c4aSTejun Heo if (flush_color >= 0) { 37796183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 378073f53c4aSTejun Heo 3781112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3782112202d9STejun Heo pwq->flush_color = flush_color; 3783112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 378473f53c4aSTejun Heo wait = true; 37851da177e4SLinus Torvalds } 378673f53c4aSTejun Heo } 378773f53c4aSTejun Heo 378873f53c4aSTejun Heo if (work_color >= 0) { 37896183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3790112202d9STejun Heo pwq->work_color = work_color; 379173f53c4aSTejun Heo } 379273f53c4aSTejun Heo 3793a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 37941da177e4SLinus Torvalds } 37951da177e4SLinus Torvalds 3796112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 379773f53c4aSTejun Heo complete(&wq->first_flusher->done); 379873f53c4aSTejun Heo 379973f53c4aSTejun Heo return wait; 380083c22520SOleg Nesterov } 38011da177e4SLinus Torvalds 3802c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3803c35aea39STejun Heo { 38044cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 38054cb1ef64STejun Heo if (wq->flags & WQ_BH) 38064cb1ef64STejun Heo local_bh_disable(); 38074cb1ef64STejun Heo 3808c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3809c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 38104cb1ef64STejun Heo 38114cb1ef64STejun Heo if (wq->flags & WQ_BH) 38124cb1ef64STejun Heo local_bh_enable(); 38134cb1ef64STejun Heo #endif 3814c35aea39STejun Heo } 3815c35aea39STejun Heo 3816c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3817c35aea39STejun Heo struct workqueue_struct *wq) 3818c35aea39STejun Heo { 38194cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 38204cb1ef64STejun Heo if (wq->flags & WQ_BH) 38214cb1ef64STejun Heo local_bh_disable(); 38224cb1ef64STejun Heo 3823c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3824c35aea39STejun Heo lock_map_release(&work->lockdep_map); 38254cb1ef64STejun Heo 38264cb1ef64STejun Heo if (wq->flags & WQ_BH) 38274cb1ef64STejun Heo local_bh_enable(); 38284cb1ef64STejun Heo #endif 3829c35aea39STejun Heo } 3830c35aea39STejun Heo 38310fcb78c2SRolf Eike Beer /** 3832c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 38330fcb78c2SRolf Eike Beer * @wq: workqueue to flush 38341da177e4SLinus Torvalds * 3835c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3836c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 38371da177e4SLinus Torvalds */ 3838c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 38391da177e4SLinus Torvalds { 384073f53c4aSTejun Heo struct wq_flusher this_flusher = { 384173f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 384273f53c4aSTejun Heo .flush_color = -1, 3843fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 384473f53c4aSTejun Heo }; 384573f53c4aSTejun Heo int next_color; 3846b1f4ec17SOleg Nesterov 38473347fa09STejun Heo if (WARN_ON(!wq_online)) 38483347fa09STejun Heo return; 38493347fa09STejun Heo 3850c35aea39STejun Heo touch_wq_lockdep_map(wq); 385187915adcSJohannes Berg 38523c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 385373f53c4aSTejun Heo 385473f53c4aSTejun Heo /* 385573f53c4aSTejun Heo * Start-to-wait phase 385673f53c4aSTejun Heo */ 385773f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 385873f53c4aSTejun Heo 385973f53c4aSTejun Heo if (next_color != wq->flush_color) { 386073f53c4aSTejun Heo /* 386173f53c4aSTejun Heo * Color space is not full. The current work_color 386273f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 386373f53c4aSTejun Heo * by one. 386473f53c4aSTejun Heo */ 38656183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 386673f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 386773f53c4aSTejun Heo wq->work_color = next_color; 386873f53c4aSTejun Heo 386973f53c4aSTejun Heo if (!wq->first_flusher) { 387073f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 38716183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 387273f53c4aSTejun Heo 387373f53c4aSTejun Heo wq->first_flusher = &this_flusher; 387473f53c4aSTejun Heo 3875112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 387673f53c4aSTejun Heo wq->work_color)) { 387773f53c4aSTejun Heo /* nothing to flush, done */ 387873f53c4aSTejun Heo wq->flush_color = next_color; 387973f53c4aSTejun Heo wq->first_flusher = NULL; 388073f53c4aSTejun Heo goto out_unlock; 388173f53c4aSTejun Heo } 388273f53c4aSTejun Heo } else { 388373f53c4aSTejun Heo /* wait in queue */ 38846183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 388573f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3886112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 388773f53c4aSTejun Heo } 388873f53c4aSTejun Heo } else { 388973f53c4aSTejun Heo /* 389073f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 389173f53c4aSTejun Heo * The next flush completion will assign us 389273f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 389373f53c4aSTejun Heo */ 389473f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 389573f53c4aSTejun Heo } 389673f53c4aSTejun Heo 3897fca839c0STejun Heo check_flush_dependency(wq, NULL); 3898fca839c0STejun Heo 38993c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 390073f53c4aSTejun Heo 390173f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 390273f53c4aSTejun Heo 390373f53c4aSTejun Heo /* 390473f53c4aSTejun Heo * Wake-up-and-cascade phase 390573f53c4aSTejun Heo * 390673f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 390773f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 390873f53c4aSTejun Heo */ 390900d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 391073f53c4aSTejun Heo return; 391173f53c4aSTejun Heo 39123c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 391373f53c4aSTejun Heo 39144ce48b37STejun Heo /* we might have raced, check again with mutex held */ 39154ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 39164ce48b37STejun Heo goto out_unlock; 39174ce48b37STejun Heo 391800d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 391973f53c4aSTejun Heo 39206183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 39216183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 392273f53c4aSTejun Heo 392373f53c4aSTejun Heo while (true) { 392473f53c4aSTejun Heo struct wq_flusher *next, *tmp; 392573f53c4aSTejun Heo 392673f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 392773f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 392873f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 392973f53c4aSTejun Heo break; 393073f53c4aSTejun Heo list_del_init(&next->list); 393173f53c4aSTejun Heo complete(&next->done); 393273f53c4aSTejun Heo } 393373f53c4aSTejun Heo 39346183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 393573f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 393673f53c4aSTejun Heo 393773f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 393873f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 393973f53c4aSTejun Heo 394073f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 394173f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 394273f53c4aSTejun Heo /* 394373f53c4aSTejun Heo * Assign the same color to all overflowed 394473f53c4aSTejun Heo * flushers, advance work_color and append to 394573f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 394673f53c4aSTejun Heo * phase for these overflowed flushers. 394773f53c4aSTejun Heo */ 394873f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 394973f53c4aSTejun Heo tmp->flush_color = wq->work_color; 395073f53c4aSTejun Heo 395173f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 395273f53c4aSTejun Heo 395373f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 395473f53c4aSTejun Heo &wq->flusher_queue); 3955112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 395673f53c4aSTejun Heo } 395773f53c4aSTejun Heo 395873f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 39596183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 396073f53c4aSTejun Heo break; 396173f53c4aSTejun Heo } 396273f53c4aSTejun Heo 396373f53c4aSTejun Heo /* 396473f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 3965112202d9STejun Heo * the new first flusher and arm pwqs. 396673f53c4aSTejun Heo */ 39676183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 39686183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 396973f53c4aSTejun Heo 397073f53c4aSTejun Heo list_del_init(&next->list); 397173f53c4aSTejun Heo wq->first_flusher = next; 397273f53c4aSTejun Heo 3973112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 397473f53c4aSTejun Heo break; 397573f53c4aSTejun Heo 397673f53c4aSTejun Heo /* 397773f53c4aSTejun Heo * Meh... this color is already done, clear first 397873f53c4aSTejun Heo * flusher and repeat cascading. 397973f53c4aSTejun Heo */ 398073f53c4aSTejun Heo wq->first_flusher = NULL; 398173f53c4aSTejun Heo } 398273f53c4aSTejun Heo 398373f53c4aSTejun Heo out_unlock: 39843c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 39851da177e4SLinus Torvalds } 3986c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 39871da177e4SLinus Torvalds 39889c5a2ba7STejun Heo /** 39899c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 39909c5a2ba7STejun Heo * @wq: workqueue to drain 39919c5a2ba7STejun Heo * 39929c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 39939c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 39949c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 3995b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 39969c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 39979c5a2ba7STejun Heo * takes too long. 39989c5a2ba7STejun Heo */ 39999c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 40009c5a2ba7STejun Heo { 40019c5a2ba7STejun Heo unsigned int flush_cnt = 0; 400249e3cf44STejun Heo struct pool_workqueue *pwq; 40039c5a2ba7STejun Heo 40049c5a2ba7STejun Heo /* 40059c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 40069c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 4007618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 40089c5a2ba7STejun Heo */ 400987fc741eSLai Jiangshan mutex_lock(&wq->mutex); 40109c5a2ba7STejun Heo if (!wq->nr_drainers++) 4011618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 401287fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 40139c5a2ba7STejun Heo reflush: 4014c4f135d6STetsuo Handa __flush_workqueue(wq); 40159c5a2ba7STejun Heo 4016b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 401776af4d93STejun Heo 401849e3cf44STejun Heo for_each_pwq(pwq, wq) { 4019fa2563e4SThomas Tuttle bool drained; 40209c5a2ba7STejun Heo 4021a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4022afa87ce8STejun Heo drained = pwq_is_empty(pwq); 4023a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4024fa2563e4SThomas Tuttle 4025fa2563e4SThomas Tuttle if (drained) 40269c5a2ba7STejun Heo continue; 40279c5a2ba7STejun Heo 40289c5a2ba7STejun Heo if (++flush_cnt == 10 || 40299c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 4030e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 4031e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 403276af4d93STejun Heo 4033b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 40349c5a2ba7STejun Heo goto reflush; 40359c5a2ba7STejun Heo } 40369c5a2ba7STejun Heo 40379c5a2ba7STejun Heo if (!--wq->nr_drainers) 4038618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 403987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 40409c5a2ba7STejun Heo } 40419c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 40429c5a2ba7STejun Heo 4043d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 4044d6e89786SJohannes Berg bool from_cancel) 4045baf59022STejun Heo { 4046baf59022STejun Heo struct worker *worker = NULL; 4047c9e7cf27STejun Heo struct worker_pool *pool; 4048112202d9STejun Heo struct pool_workqueue *pwq; 4049c35aea39STejun Heo struct workqueue_struct *wq; 4050baf59022STejun Heo 4051baf59022STejun Heo might_sleep(); 4052baf59022STejun Heo 405324acfb71SThomas Gleixner rcu_read_lock(); 4054fa1b54e6STejun Heo pool = get_work_pool(work); 4055fa1b54e6STejun Heo if (!pool) { 405624acfb71SThomas Gleixner rcu_read_unlock(); 4057fa1b54e6STejun Heo return false; 4058fa1b54e6STejun Heo } 4059fa1b54e6STejun Heo 4060a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 40610b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 4062112202d9STejun Heo pwq = get_work_pwq(work); 4063112202d9STejun Heo if (pwq) { 4064112202d9STejun Heo if (unlikely(pwq->pool != pool)) 4065baf59022STejun Heo goto already_gone; 4066606a5020STejun Heo } else { 4067c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 4068baf59022STejun Heo if (!worker) 4069baf59022STejun Heo goto already_gone; 4070112202d9STejun Heo pwq = worker->current_pwq; 4071606a5020STejun Heo } 4072baf59022STejun Heo 4073c35aea39STejun Heo wq = pwq->wq; 4074c35aea39STejun Heo check_flush_dependency(wq, work); 4075fca839c0STejun Heo 4076112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 4077a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4078baf59022STejun Heo 4079c35aea39STejun Heo touch_work_lockdep_map(work, wq); 4080c35aea39STejun Heo 4081e159489bSTejun Heo /* 4082a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 4083a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 4084a1d14934SPeter Zijlstra * 4085a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 4086a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 4087a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 4088a1d14934SPeter Zijlstra * forward progress. 4089e159489bSTejun Heo */ 4090c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 4091c35aea39STejun Heo touch_wq_lockdep_map(wq); 4092c35aea39STejun Heo 409324acfb71SThomas Gleixner rcu_read_unlock(); 4094baf59022STejun Heo return true; 4095baf59022STejun Heo already_gone: 4096a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 409724acfb71SThomas Gleixner rcu_read_unlock(); 4098baf59022STejun Heo return false; 4099baf59022STejun Heo } 4100baf59022STejun Heo 4101d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4102d6e89786SJohannes Berg { 4103d6e89786SJohannes Berg struct wq_barrier barr; 4104d6e89786SJohannes Berg 4105d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4106d6e89786SJohannes Berg return false; 4107d6e89786SJohannes Berg 41084d43d395STetsuo Handa if (WARN_ON(!work->func)) 41094d43d395STetsuo Handa return false; 41104d43d395STetsuo Handa 4111d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4112d6e89786SJohannes Berg wait_for_completion(&barr.done); 4113d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4114d6e89786SJohannes Berg return true; 4115d6e89786SJohannes Berg } else { 4116d6e89786SJohannes Berg return false; 4117d6e89786SJohannes Berg } 4118d6e89786SJohannes Berg } 4119d6e89786SJohannes Berg 4120db700897SOleg Nesterov /** 4121401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4122401a8d04STejun Heo * @work: the work to flush 4123db700897SOleg Nesterov * 4124606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4125606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4126401a8d04STejun Heo * 4127d185af30SYacine Belkadi * Return: 4128401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4129401a8d04STejun Heo * %false if it was already idle. 4130db700897SOleg Nesterov */ 4131401a8d04STejun Heo bool flush_work(struct work_struct *work) 4132db700897SOleg Nesterov { 4133d6e89786SJohannes Berg return __flush_work(work, false); 4134606a5020STejun Heo } 4135db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4136db700897SOleg Nesterov 4137cdc6e4b3STejun Heo /** 4138cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4139cdc6e4b3STejun Heo * @dwork: the delayed work to flush 4140cdc6e4b3STejun Heo * 4141cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for 4142cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only 4143cdc6e4b3STejun Heo * considers the last queueing instance of @dwork. 4144cdc6e4b3STejun Heo * 4145cdc6e4b3STejun Heo * Return: 4146cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution, 4147cdc6e4b3STejun Heo * %false if it was already idle. 4148cdc6e4b3STejun Heo */ 4149cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4150cdc6e4b3STejun Heo { 4151cdc6e4b3STejun Heo local_irq_disable(); 4152cdc6e4b3STejun Heo if (del_timer_sync(&dwork->timer)) 4153cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work); 4154cdc6e4b3STejun Heo local_irq_enable(); 4155cdc6e4b3STejun Heo return flush_work(&dwork->work); 4156cdc6e4b3STejun Heo } 4157cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4158cdc6e4b3STejun Heo 4159cdc6e4b3STejun Heo /** 4160cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 4161cdc6e4b3STejun Heo * @rwork: the rcu work to flush 4162cdc6e4b3STejun Heo * 4163cdc6e4b3STejun Heo * Return: 4164cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 4165cdc6e4b3STejun Heo * %false if it was already idle. 4166cdc6e4b3STejun Heo */ 4167cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork) 4168cdc6e4b3STejun Heo { 4169cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 4170cdc6e4b3STejun Heo rcu_barrier(); 4171cdc6e4b3STejun Heo flush_work(&rwork->work); 4172cdc6e4b3STejun Heo return true; 4173cdc6e4b3STejun Heo } else { 4174cdc6e4b3STejun Heo return flush_work(&rwork->work); 4175cdc6e4b3STejun Heo } 4176cdc6e4b3STejun Heo } 4177cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work); 4178cdc6e4b3STejun Heo 4179c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags) 4180cdc6e4b3STejun Heo { 4181c26e2f2eSTejun Heo unsigned long irq_flags; 4182cdc6e4b3STejun Heo int ret; 4183cdc6e4b3STejun Heo 4184cdc6e4b3STejun Heo do { 4185c5f5b942STejun Heo ret = try_to_grab_pending(work, cflags, &irq_flags); 4186cdc6e4b3STejun Heo } while (unlikely(ret == -EAGAIN)); 4187cdc6e4b3STejun Heo 4188cdc6e4b3STejun Heo if (unlikely(ret < 0)) 4189cdc6e4b3STejun Heo return false; 4190cdc6e4b3STejun Heo 4191*bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, get_work_pool_id(work), 0); 4192c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4193cdc6e4b3STejun Heo return ret; 4194cdc6e4b3STejun Heo } 4195cdc6e4b3STejun Heo 4196c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags) 4197401a8d04STejun Heo { 4198c26e2f2eSTejun Heo unsigned long irq_flags; 4199978b8409STejun Heo bool ret; 42001f1f642eSOleg Nesterov 4201978b8409STejun Heo /* claim @work and tell other tasks trying to grab @work to back off */ 4202978b8409STejun Heo ret = work_grab_pending(work, cflags, &irq_flags); 4203bbb68dfaSTejun Heo mark_work_canceling(work); 4204c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4205bbb68dfaSTejun Heo 42063347fa09STejun Heo /* 4207c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't 4208c7a40c49STejun Heo * executing. This allows canceling during early boot. 42093347fa09STejun Heo */ 42103347fa09STejun Heo if (wq_online) 4211d6e89786SJohannes Berg __flush_work(work, true); 42123347fa09STejun Heo 42138603e1b3STejun Heo /* 4214afe928c1STejun Heo * smp_mb() at the end of set_work_pool_and_clear_pending() is paired 4215afe928c1STejun Heo * with prepare_to_wait() above so that either waitqueue_active() is 4216afe928c1STejun Heo * visible here or !work_is_canceling() is visible there. 42178603e1b3STejun Heo */ 4218*bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, WORK_OFFQ_POOL_NONE, 0); 4219afe928c1STejun Heo 4220978b8409STejun Heo if (waitqueue_active(&wq_cancel_waitq)) 4221978b8409STejun Heo __wake_up(&wq_cancel_waitq, TASK_NORMAL, 1, work); 42228603e1b3STejun Heo 42231f1f642eSOleg Nesterov return ret; 42241f1f642eSOleg Nesterov } 42251f1f642eSOleg Nesterov 4226cdc6e4b3STejun Heo /* 4227cdc6e4b3STejun Heo * See cancel_delayed_work() 4228cdc6e4b3STejun Heo */ 4229cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work) 4230cdc6e4b3STejun Heo { 4231c5f5b942STejun Heo return __cancel_work(work, 0); 4232cdc6e4b3STejun Heo } 4233cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work); 4234cdc6e4b3STejun Heo 42356e84d644SOleg Nesterov /** 4236401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4237401a8d04STejun Heo * @work: the work to cancel 42386e84d644SOleg Nesterov * 4239401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4240401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4241401a8d04STejun Heo * another workqueue. On return from this function, @work is 4242401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 42431f1f642eSOleg Nesterov * 4244401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4245401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 42466e84d644SOleg Nesterov * 4247401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 42486e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4249401a8d04STejun Heo * 4250d185af30SYacine Belkadi * Return: 4251401a8d04STejun Heo * %true if @work was pending, %false otherwise. 42526e84d644SOleg Nesterov */ 4253401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 42546e84d644SOleg Nesterov { 4255c5f5b942STejun Heo return __cancel_work_sync(work, 0); 4256b89deed3SOleg Nesterov } 425728e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4258b89deed3SOleg Nesterov 42596e84d644SOleg Nesterov /** 426057b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 426157b30ae7STejun Heo * @dwork: delayed_work to cancel 426209383498STejun Heo * 4263d185af30SYacine Belkadi * Kill off a pending delayed_work. 4264d185af30SYacine Belkadi * 4265d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4266d185af30SYacine Belkadi * pending. 4267d185af30SYacine Belkadi * 4268d185af30SYacine Belkadi * Note: 4269d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4270d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4271d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 427209383498STejun Heo * 427357b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 427409383498STejun Heo */ 427557b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 427609383498STejun Heo { 4277c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED); 427809383498STejun Heo } 427957b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 428009383498STejun Heo 428109383498STejun Heo /** 4282401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4283401a8d04STejun Heo * @dwork: the delayed work cancel 4284401a8d04STejun Heo * 4285401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4286401a8d04STejun Heo * 4287d185af30SYacine Belkadi * Return: 4288401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4289401a8d04STejun Heo */ 4290401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 42916e84d644SOleg Nesterov { 4292c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED); 42936e84d644SOleg Nesterov } 4294f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 42951da177e4SLinus Torvalds 42960fcb78c2SRolf Eike Beer /** 429731ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4298b6136773SAndrew Morton * @func: the function to call 4299b6136773SAndrew Morton * 430031ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 430131ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4302b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 430331ddd871STejun Heo * 4304d185af30SYacine Belkadi * Return: 430531ddd871STejun Heo * 0 on success, -errno on failure. 4306b6136773SAndrew Morton */ 430765f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 430815316ba8SChristoph Lameter { 430915316ba8SChristoph Lameter int cpu; 431038f51568SNamhyung Kim struct work_struct __percpu *works; 431115316ba8SChristoph Lameter 4312b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4313b6136773SAndrew Morton if (!works) 431415316ba8SChristoph Lameter return -ENOMEM; 4315b6136773SAndrew Morton 4316ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 431793981800STejun Heo 431815316ba8SChristoph Lameter for_each_online_cpu(cpu) { 43199bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 43209bfb1839SIngo Molnar 43219bfb1839SIngo Molnar INIT_WORK(work, func); 43228de6d308SOleg Nesterov schedule_work_on(cpu, work); 432315316ba8SChristoph Lameter } 432493981800STejun Heo 432593981800STejun Heo for_each_online_cpu(cpu) 43268616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 432793981800STejun Heo 4328ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4329b6136773SAndrew Morton free_percpu(works); 433015316ba8SChristoph Lameter return 0; 433115316ba8SChristoph Lameter } 433215316ba8SChristoph Lameter 4333eef6a7d5SAlan Stern /** 43341fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 43351fa44ecaSJames Bottomley * @fn: the function to execute 43361fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 43371fa44ecaSJames Bottomley * be available when the work executes) 43381fa44ecaSJames Bottomley * 43391fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 43401fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 43411fa44ecaSJames Bottomley * 4342d185af30SYacine Belkadi * Return: 0 - function was executed 43431fa44ecaSJames Bottomley * 1 - function was scheduled for execution 43441fa44ecaSJames Bottomley */ 434565f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 43461fa44ecaSJames Bottomley { 43471fa44ecaSJames Bottomley if (!in_interrupt()) { 434865f27f38SDavid Howells fn(&ew->work); 43491fa44ecaSJames Bottomley return 0; 43501fa44ecaSJames Bottomley } 43511fa44ecaSJames Bottomley 435265f27f38SDavid Howells INIT_WORK(&ew->work, fn); 43531fa44ecaSJames Bottomley schedule_work(&ew->work); 43541fa44ecaSJames Bottomley 43551fa44ecaSJames Bottomley return 1; 43561fa44ecaSJames Bottomley } 43571fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 43581fa44ecaSJames Bottomley 43597a4e344cSTejun Heo /** 43607a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 43617a4e344cSTejun Heo * @attrs: workqueue_attrs to free 43627a4e344cSTejun Heo * 43637a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 43647a4e344cSTejun Heo */ 4365513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 43667a4e344cSTejun Heo { 43677a4e344cSTejun Heo if (attrs) { 43687a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 43699546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 43707a4e344cSTejun Heo kfree(attrs); 43717a4e344cSTejun Heo } 43727a4e344cSTejun Heo } 43737a4e344cSTejun Heo 43747a4e344cSTejun Heo /** 43757a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 43767a4e344cSTejun Heo * 43777a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4378d185af30SYacine Belkadi * return it. 4379d185af30SYacine Belkadi * 4380d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 43817a4e344cSTejun Heo */ 4382513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 43837a4e344cSTejun Heo { 43847a4e344cSTejun Heo struct workqueue_attrs *attrs; 43857a4e344cSTejun Heo 4386be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 43877a4e344cSTejun Heo if (!attrs) 43887a4e344cSTejun Heo goto fail; 4389be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 43907a4e344cSTejun Heo goto fail; 43919546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 43929546b29eSTejun Heo goto fail; 43937a4e344cSTejun Heo 439413e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4395523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 43967a4e344cSTejun Heo return attrs; 43977a4e344cSTejun Heo fail: 43987a4e344cSTejun Heo free_workqueue_attrs(attrs); 43997a4e344cSTejun Heo return NULL; 44007a4e344cSTejun Heo } 44017a4e344cSTejun Heo 440229c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 440329c91e99STejun Heo const struct workqueue_attrs *from) 440429c91e99STejun Heo { 440529c91e99STejun Heo to->nice = from->nice; 440629c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 44079546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 44088639ecebSTejun Heo to->affn_strict = from->affn_strict; 440984193c07STejun Heo 44102865a8fbSShaohua Li /* 441184193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 441284193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 441384193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 44142865a8fbSShaohua Li */ 441584193c07STejun Heo to->affn_scope = from->affn_scope; 4416af73f5c9STejun Heo to->ordered = from->ordered; 441729c91e99STejun Heo } 441829c91e99STejun Heo 44195de7a03cSTejun Heo /* 44205de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 44215de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 44225de7a03cSTejun Heo */ 44235de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 44245de7a03cSTejun Heo { 442584193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 44265de7a03cSTejun Heo attrs->ordered = false; 44275de7a03cSTejun Heo } 44285de7a03cSTejun Heo 442929c91e99STejun Heo /* hash value of the content of @attr */ 443029c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 443129c91e99STejun Heo { 443229c91e99STejun Heo u32 hash = 0; 443329c91e99STejun Heo 443429c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 443513e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 443613e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 44379546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 44389546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 44398639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 444029c91e99STejun Heo return hash; 444129c91e99STejun Heo } 444229c91e99STejun Heo 444329c91e99STejun Heo /* content equality test */ 444429c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 444529c91e99STejun Heo const struct workqueue_attrs *b) 444629c91e99STejun Heo { 444729c91e99STejun Heo if (a->nice != b->nice) 444829c91e99STejun Heo return false; 444929c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 445029c91e99STejun Heo return false; 44519546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 44529546b29eSTejun Heo return false; 44538639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 44548639ecebSTejun Heo return false; 445529c91e99STejun Heo return true; 445629c91e99STejun Heo } 445729c91e99STejun Heo 44580f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 44590f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 44600f36ee24STejun Heo const cpumask_t *unbound_cpumask) 44610f36ee24STejun Heo { 44620f36ee24STejun Heo /* 44630f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 44640f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 44650f36ee24STejun Heo * @unbound_cpumask. 44660f36ee24STejun Heo */ 44670f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 44680f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 44690f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 44700f36ee24STejun Heo } 44710f36ee24STejun Heo 447284193c07STejun Heo /* find wq_pod_type to use for @attrs */ 447384193c07STejun Heo static const struct wq_pod_type * 447484193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 447584193c07STejun Heo { 4476523a301eSTejun Heo enum wq_affn_scope scope; 4477523a301eSTejun Heo struct wq_pod_type *pt; 4478523a301eSTejun Heo 4479523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4480523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4481523a301eSTejun Heo 4482523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4483523a301eSTejun Heo scope = wq_affn_dfl; 4484523a301eSTejun Heo else 4485523a301eSTejun Heo scope = attrs->affn_scope; 4486523a301eSTejun Heo 4487523a301eSTejun Heo pt = &wq_pod_types[scope]; 448884193c07STejun Heo 448984193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 449084193c07STejun Heo likely(pt->nr_pods)) 449184193c07STejun Heo return pt; 449284193c07STejun Heo 449384193c07STejun Heo /* 449484193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 449584193c07STejun Heo * initialized in workqueue_init_early(). 449684193c07STejun Heo */ 449784193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 449884193c07STejun Heo BUG_ON(!pt->nr_pods); 449984193c07STejun Heo return pt; 450084193c07STejun Heo } 450184193c07STejun Heo 45027a4e344cSTejun Heo /** 45037a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 45047a4e344cSTejun Heo * @pool: worker_pool to initialize 45057a4e344cSTejun Heo * 4506402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4507d185af30SYacine Belkadi * 4508d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 450929c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 451029c91e99STejun Heo * on @pool safely to release it. 45117a4e344cSTejun Heo */ 45127a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 45134e1a1f9aSTejun Heo { 4514a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 451529c91e99STejun Heo pool->id = -1; 451629c91e99STejun Heo pool->cpu = -1; 4517f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 45184e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 451982607adcSTejun Heo pool->watchdog_ts = jiffies; 45204e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 45214e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 45224e1a1f9aSTejun Heo hash_init(pool->busy_hash); 45234e1a1f9aSTejun Heo 452432a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 45253f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 45264e1a1f9aSTejun Heo 452732a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 45284e1a1f9aSTejun Heo 4529da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4530e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 45317a4e344cSTejun Heo 45327cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 453329c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 453429c91e99STejun Heo pool->refcnt = 1; 453529c91e99STejun Heo 453629c91e99STejun Heo /* shouldn't fail above this point */ 4537be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 45387a4e344cSTejun Heo if (!pool->attrs) 45397a4e344cSTejun Heo return -ENOMEM; 45405de7a03cSTejun Heo 45415de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 45425de7a03cSTejun Heo 45437a4e344cSTejun Heo return 0; 45444e1a1f9aSTejun Heo } 45454e1a1f9aSTejun Heo 4546669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4547669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4548669de8bdSBart Van Assche { 4549669de8bdSBart Van Assche char *lock_name; 4550669de8bdSBart Van Assche 4551669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4552669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4553669de8bdSBart Van Assche if (!lock_name) 4554669de8bdSBart Van Assche lock_name = wq->name; 455569a106c0SQian Cai 455669a106c0SQian Cai wq->lock_name = lock_name; 4557669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4558669de8bdSBart Van Assche } 4559669de8bdSBart Van Assche 4560669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4561669de8bdSBart Van Assche { 4562669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4563669de8bdSBart Van Assche } 4564669de8bdSBart Van Assche 4565669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4566669de8bdSBart Van Assche { 4567669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4568669de8bdSBart Van Assche kfree(wq->lock_name); 4569669de8bdSBart Van Assche } 4570669de8bdSBart Van Assche #else 4571669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4572669de8bdSBart Van Assche { 4573669de8bdSBart Van Assche } 4574669de8bdSBart Van Assche 4575669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4576669de8bdSBart Van Assche { 4577669de8bdSBart Van Assche } 4578669de8bdSBart Van Assche 4579669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4580669de8bdSBart Van Assche { 4581669de8bdSBart Van Assche } 4582669de8bdSBart Van Assche #endif 4583669de8bdSBart Van Assche 458491ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 458591ccc6e7STejun Heo { 458691ccc6e7STejun Heo int node; 458791ccc6e7STejun Heo 458891ccc6e7STejun Heo for_each_node(node) { 458991ccc6e7STejun Heo kfree(nna_ar[node]); 459091ccc6e7STejun Heo nna_ar[node] = NULL; 459191ccc6e7STejun Heo } 459291ccc6e7STejun Heo 459391ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 459491ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 459591ccc6e7STejun Heo } 459691ccc6e7STejun Heo 459791ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 459891ccc6e7STejun Heo { 4599c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 460091ccc6e7STejun Heo atomic_set(&nna->nr, 0); 46015797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 46025797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 460391ccc6e7STejun Heo } 460491ccc6e7STejun Heo 460591ccc6e7STejun Heo /* 460691ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 460791ccc6e7STejun Heo * should be allocated in the node. 460891ccc6e7STejun Heo */ 460991ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 461091ccc6e7STejun Heo { 461191ccc6e7STejun Heo struct wq_node_nr_active *nna; 461291ccc6e7STejun Heo int node; 461391ccc6e7STejun Heo 461491ccc6e7STejun Heo for_each_node(node) { 461591ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 461691ccc6e7STejun Heo if (!nna) 461791ccc6e7STejun Heo goto err_free; 461891ccc6e7STejun Heo init_node_nr_active(nna); 461991ccc6e7STejun Heo nna_ar[node] = nna; 462091ccc6e7STejun Heo } 462191ccc6e7STejun Heo 462291ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 462391ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 462491ccc6e7STejun Heo if (!nna) 462591ccc6e7STejun Heo goto err_free; 462691ccc6e7STejun Heo init_node_nr_active(nna); 462791ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 462891ccc6e7STejun Heo 462991ccc6e7STejun Heo return 0; 463091ccc6e7STejun Heo 463191ccc6e7STejun Heo err_free: 463291ccc6e7STejun Heo free_node_nr_active(nna_ar); 463391ccc6e7STejun Heo return -ENOMEM; 463491ccc6e7STejun Heo } 463591ccc6e7STejun Heo 4636e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4637e2dca7adSTejun Heo { 4638e2dca7adSTejun Heo struct workqueue_struct *wq = 4639e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4640e2dca7adSTejun Heo 464191ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 464291ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 464391ccc6e7STejun Heo 4644669de8bdSBart Van Assche wq_free_lockdep(wq); 4645ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4646e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4647e2dca7adSTejun Heo kfree(wq); 4648e2dca7adSTejun Heo } 4649e2dca7adSTejun Heo 465029c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 465129c91e99STejun Heo { 465229c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 465329c91e99STejun Heo 46547cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 465529c91e99STejun Heo free_workqueue_attrs(pool->attrs); 465629c91e99STejun Heo kfree(pool); 465729c91e99STejun Heo } 465829c91e99STejun Heo 465929c91e99STejun Heo /** 466029c91e99STejun Heo * put_unbound_pool - put a worker_pool 466129c91e99STejun Heo * @pool: worker_pool to put 466229c91e99STejun Heo * 466324acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4664c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4665c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4666c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4667a892caccSTejun Heo * 4668a892caccSTejun Heo * Should be called with wq_pool_mutex held. 466929c91e99STejun Heo */ 467029c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 467129c91e99STejun Heo { 467260f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 467329c91e99STejun Heo struct worker *worker; 46749680540cSYang Yingliang LIST_HEAD(cull_list); 4675e02b9312SValentin Schneider 4676a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4677a892caccSTejun Heo 4678a892caccSTejun Heo if (--pool->refcnt) 467929c91e99STejun Heo return; 468029c91e99STejun Heo 468129c91e99STejun Heo /* sanity checks */ 468261d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4683a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 468429c91e99STejun Heo return; 468529c91e99STejun Heo 468629c91e99STejun Heo /* release id and unhash */ 468729c91e99STejun Heo if (pool->id >= 0) 468829c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 468929c91e99STejun Heo hash_del(&pool->hash_node); 469029c91e99STejun Heo 4691c5aa87bbSTejun Heo /* 4692692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4693692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4694692b4825STejun Heo * manager and @pool gets freed with the flag set. 46959ab03be4SValentin Schneider * 46969ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 46979ab03be4SValentin Schneider * only get here with 46989ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 46999ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 47009ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 47019ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 47029ab03be4SValentin Schneider * drops pool->lock 4703c5aa87bbSTejun Heo */ 47049ab03be4SValentin Schneider while (true) { 47059ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 47069ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4707d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4708e02b9312SValentin Schneider 4709e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 47109ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 47119ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4712692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 47139ab03be4SValentin Schneider break; 47149ab03be4SValentin Schneider } 47159ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4716e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 47179ab03be4SValentin Schneider } 4718692b4825STejun Heo 47191037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4720e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 472129c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4722a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 472360f5a4bcSLai Jiangshan 4724e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4725e02b9312SValentin Schneider 4726e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 472760f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 47281258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 472960f5a4bcSLai Jiangshan 473060f5a4bcSLai Jiangshan if (pool->detach_completion) 473160f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 473260f5a4bcSLai Jiangshan 473329c91e99STejun Heo /* shut down the timers */ 473429c91e99STejun Heo del_timer_sync(&pool->idle_timer); 47353f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 473629c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 473729c91e99STejun Heo 473824acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 473925b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 474029c91e99STejun Heo } 474129c91e99STejun Heo 474229c91e99STejun Heo /** 474329c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 474429c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 474529c91e99STejun Heo * 474629c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 474729c91e99STejun Heo * reference count and return it. If there already is a matching 474829c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4749d185af30SYacine Belkadi * create a new one. 4750a892caccSTejun Heo * 4751a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4752d185af30SYacine Belkadi * 4753d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4754d185af30SYacine Belkadi * On failure, %NULL. 475529c91e99STejun Heo */ 475629c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 475729c91e99STejun Heo { 475884193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 475929c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 476029c91e99STejun Heo struct worker_pool *pool; 476184193c07STejun Heo int pod, node = NUMA_NO_NODE; 476229c91e99STejun Heo 4763a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 476429c91e99STejun Heo 476529c91e99STejun Heo /* do we already have a matching pool? */ 476629c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 476729c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 476829c91e99STejun Heo pool->refcnt++; 47693fb1823cSLai Jiangshan return pool; 477029c91e99STejun Heo } 477129c91e99STejun Heo } 477229c91e99STejun Heo 47739546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 477484193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 47759546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 477684193c07STejun Heo node = pt->pod_node[pod]; 4777e2273584SXunlei Pang break; 4778e2273584SXunlei Pang } 4779e2273584SXunlei Pang } 4780e2273584SXunlei Pang 478129c91e99STejun Heo /* nope, create a new one */ 478284193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 478329c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 478429c91e99STejun Heo goto fail; 478529c91e99STejun Heo 478684193c07STejun Heo pool->node = node; 47875de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 47885de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 47892865a8fbSShaohua Li 479029c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 479129c91e99STejun Heo goto fail; 479229c91e99STejun Heo 479329c91e99STejun Heo /* create and start the initial worker */ 47943347fa09STejun Heo if (wq_online && !create_worker(pool)) 479529c91e99STejun Heo goto fail; 479629c91e99STejun Heo 479729c91e99STejun Heo /* install */ 479829c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 47993fb1823cSLai Jiangshan 480029c91e99STejun Heo return pool; 480129c91e99STejun Heo fail: 480229c91e99STejun Heo if (pool) 480329c91e99STejun Heo put_unbound_pool(pool); 480429c91e99STejun Heo return NULL; 480529c91e99STejun Heo } 480629c91e99STejun Heo 48078864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 48088864b4e5STejun Heo { 48098864b4e5STejun Heo kmem_cache_free(pwq_cache, 48108864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 48118864b4e5STejun Heo } 48128864b4e5STejun Heo 48138864b4e5STejun Heo /* 4814967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4815967b494eSTejun Heo * refcnt and needs to be destroyed. 48168864b4e5STejun Heo */ 4817687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 48188864b4e5STejun Heo { 48198864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4820687a9aa5STejun Heo release_work); 48218864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 48228864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4823b42b0bddSYang Yingliang bool is_last = false; 48248864b4e5STejun Heo 4825b42b0bddSYang Yingliang /* 4826687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4827b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4828b42b0bddSYang Yingliang */ 4829b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 48303c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 48318864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4832bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 48334c065dbcSWaiman Long 48344c065dbcSWaiman Long /* 48354c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 48364c065dbcSWaiman Long */ 48374c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 48384c065dbcSWaiman Long unplug_oldest_pwq(wq); 48394c065dbcSWaiman Long 48403c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4841b42b0bddSYang Yingliang } 48428864b4e5STejun Heo 4843687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4844a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 48458864b4e5STejun Heo put_unbound_pool(pool); 4846a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4847687a9aa5STejun Heo } 4848a892caccSTejun Heo 48495797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 48505797b1c1STejun Heo struct wq_node_nr_active *nna = 48515797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 48525797b1c1STejun Heo 48535797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 48545797b1c1STejun Heo list_del_init(&pwq->pending_node); 48555797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 48565797b1c1STejun Heo } 48575797b1c1STejun Heo 485825b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 48598864b4e5STejun Heo 48608864b4e5STejun Heo /* 48618864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4862e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 48638864b4e5STejun Heo */ 4864669de8bdSBart Van Assche if (is_last) { 4865669de8bdSBart Van Assche wq_unregister_lockdep(wq); 486625b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 48676029a918STejun Heo } 4868669de8bdSBart Van Assche } 48698864b4e5STejun Heo 487067dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4871f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4872f147f29eSTejun Heo struct worker_pool *pool) 4873d2c1d404STejun Heo { 4874e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK); 4875d2c1d404STejun Heo 4876e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4877e50aba9aSTejun Heo 4878d2c1d404STejun Heo pwq->pool = pool; 4879d2c1d404STejun Heo pwq->wq = wq; 4880d2c1d404STejun Heo pwq->flush_color = -1; 48818864b4e5STejun Heo pwq->refcnt = 1; 4882f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 48835797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 48841befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4885d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4886687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4887f147f29eSTejun Heo } 4888d2c1d404STejun Heo 4889f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 48901befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4891f147f29eSTejun Heo { 4892f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4893f147f29eSTejun Heo 4894f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 489575ccf595STejun Heo 48961befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 48971befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 48981befcf30STejun Heo return; 48991befcf30STejun Heo 490029b1cb41SLai Jiangshan /* set the matching work_color */ 490175ccf595STejun Heo pwq->work_color = wq->work_color; 4902983ca25eSTejun Heo 4903983ca25eSTejun Heo /* link in @pwq */ 490426fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 4905df2d5ae4STejun Heo } 49066029a918STejun Heo 4907f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 4908f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 4909f147f29eSTejun Heo const struct workqueue_attrs *attrs) 4910f147f29eSTejun Heo { 4911f147f29eSTejun Heo struct worker_pool *pool; 4912f147f29eSTejun Heo struct pool_workqueue *pwq; 4913f147f29eSTejun Heo 4914f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4915f147f29eSTejun Heo 4916f147f29eSTejun Heo pool = get_unbound_pool(attrs); 4917f147f29eSTejun Heo if (!pool) 4918f147f29eSTejun Heo return NULL; 4919f147f29eSTejun Heo 4920e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 4921f147f29eSTejun Heo if (!pwq) { 4922f147f29eSTejun Heo put_unbound_pool(pool); 4923f147f29eSTejun Heo return NULL; 4924f147f29eSTejun Heo } 4925f147f29eSTejun Heo 4926f147f29eSTejun Heo init_pwq(pwq, wq, pool); 4927f147f29eSTejun Heo return pwq; 4928d2c1d404STejun Heo } 4929d2c1d404STejun Heo 49304c16bd32STejun Heo /** 4931fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 4932042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 493384193c07STejun Heo * @cpu: the target CPU 49344c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 49354c16bd32STejun Heo * 4936fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 4937fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 49389546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 49394c16bd32STejun Heo * 4940fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 4941fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 4942fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 49434c16bd32STejun Heo * 4944fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 49454c16bd32STejun Heo */ 49469546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 49479546b29eSTejun Heo int cpu_going_down) 49484c16bd32STejun Heo { 494984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 495084193c07STejun Heo int pod = pt->cpu_pod[cpu]; 49514c16bd32STejun Heo 4952fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 49539546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 49549546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 49554c16bd32STejun Heo if (cpu_going_down >= 0) 49569546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 49574c16bd32STejun Heo 49589546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 49599546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 496084193c07STejun Heo return; 496184193c07STejun Heo } 49624c16bd32STejun Heo 4963fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 49649546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 49651ad0f0a7SMichael Bringmann 49669546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 49671ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 49681ad0f0a7SMichael Bringmann "possible intersect\n"); 49694c16bd32STejun Heo } 49704c16bd32STejun Heo 49719f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 4972636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 4973636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 49741befcf30STejun Heo { 49759f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 49761befcf30STejun Heo struct pool_workqueue *old_pwq; 49771befcf30STejun Heo 49785b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 49791befcf30STejun Heo lockdep_assert_held(&wq->mutex); 49801befcf30STejun Heo 49811befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 49821befcf30STejun Heo link_pwq(pwq); 49831befcf30STejun Heo 49849f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 49859f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 49861befcf30STejun Heo return old_pwq; 49871befcf30STejun Heo } 49881befcf30STejun Heo 49892d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 49902d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 49912d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 49922d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 4993042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 49942d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 49952d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 49962d5f0764SLai Jiangshan }; 49972d5f0764SLai Jiangshan 49982d5f0764SLai Jiangshan /* free the resources after success or abort */ 49992d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 50002d5f0764SLai Jiangshan { 50012d5f0764SLai Jiangshan if (ctx) { 5002636b927eSTejun Heo int cpu; 50032d5f0764SLai Jiangshan 5004636b927eSTejun Heo for_each_possible_cpu(cpu) 5005636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 50062d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 50072d5f0764SLai Jiangshan 50082d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 50092d5f0764SLai Jiangshan 50102d5f0764SLai Jiangshan kfree(ctx); 50112d5f0764SLai Jiangshan } 50122d5f0764SLai Jiangshan } 50132d5f0764SLai Jiangshan 50142d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 50152d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 50162d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 501799c621efSLai Jiangshan const struct workqueue_attrs *attrs, 501899c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 50192d5f0764SLai Jiangshan { 50202d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 50219546b29eSTejun Heo struct workqueue_attrs *new_attrs; 5022636b927eSTejun Heo int cpu; 50232d5f0764SLai Jiangshan 50242d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 50252d5f0764SLai Jiangshan 502684193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 502784193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 502884193c07STejun Heo return ERR_PTR(-EINVAL); 502984193c07STejun Heo 5030636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 50312d5f0764SLai Jiangshan 5032be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 50339546b29eSTejun Heo if (!ctx || !new_attrs) 50342d5f0764SLai Jiangshan goto out_free; 50352d5f0764SLai Jiangshan 5036042f7df1SLai Jiangshan /* 50372d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 50382d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 50392d5f0764SLai Jiangshan * it even if we don't use it immediately. 50402d5f0764SLai Jiangshan */ 50410f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 50420f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 50439546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50442d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 50452d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 50462d5f0764SLai Jiangshan goto out_free; 50472d5f0764SLai Jiangshan 5048636b927eSTejun Heo for_each_possible_cpu(cpu) { 5049af73f5c9STejun Heo if (new_attrs->ordered) { 50502d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5051636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5052636b927eSTejun Heo } else { 50539546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 50549546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5055636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5056636b927eSTejun Heo goto out_free; 50572d5f0764SLai Jiangshan } 50582d5f0764SLai Jiangshan } 50592d5f0764SLai Jiangshan 5060042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5061042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5062042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 50639546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 50642d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5065042f7df1SLai Jiangshan 50664c065dbcSWaiman Long /* 50674c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 50684c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 50694c065dbcSWaiman Long * of newly queued work items until execution of older work items in 50704c065dbcSWaiman Long * the old pwq's have completed. 50714c065dbcSWaiman Long */ 50724c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 50734c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 50744c065dbcSWaiman Long 50752d5f0764SLai Jiangshan ctx->wq = wq; 50762d5f0764SLai Jiangshan return ctx; 50772d5f0764SLai Jiangshan 50782d5f0764SLai Jiangshan out_free: 50792d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 50802d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 508184193c07STejun Heo return ERR_PTR(-ENOMEM); 50822d5f0764SLai Jiangshan } 50832d5f0764SLai Jiangshan 50842d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 50852d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 50862d5f0764SLai Jiangshan { 5087636b927eSTejun Heo int cpu; 50882d5f0764SLai Jiangshan 50892d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 50902d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 50912d5f0764SLai Jiangshan 50922d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 50932d5f0764SLai Jiangshan 50949f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5095636b927eSTejun Heo for_each_possible_cpu(cpu) 5096636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5097636b927eSTejun Heo ctx->pwq_tbl[cpu]); 50989f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 50992d5f0764SLai Jiangshan 51005797b1c1STejun Heo /* update node_nr_active->max */ 51015797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 51025797b1c1STejun Heo 5103d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5104d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5105d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5106d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5107d64f2fa0SJuri Lelli 51082d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 51092d5f0764SLai Jiangshan } 51102d5f0764SLai Jiangshan 5111a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5112a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5113a0111cf6SLai Jiangshan { 5114a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5115a0111cf6SLai Jiangshan 5116a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5117a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5118a0111cf6SLai Jiangshan return -EINVAL; 5119a0111cf6SLai Jiangshan 512099c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 512184193c07STejun Heo if (IS_ERR(ctx)) 512284193c07STejun Heo return PTR_ERR(ctx); 5123a0111cf6SLai Jiangshan 5124a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5125a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5126a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5127a0111cf6SLai Jiangshan 51286201171eSwanghaibin return 0; 5129a0111cf6SLai Jiangshan } 5130a0111cf6SLai Jiangshan 51319e8cd2f5STejun Heo /** 51329e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 51339e8cd2f5STejun Heo * @wq: the target workqueue 51349e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 51359e8cd2f5STejun Heo * 5136fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5137fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5138fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5139fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5140fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 51419e8cd2f5STejun Heo * 5142d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5143d185af30SYacine Belkadi * 5144ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5145509b3204SDaniel Jordan * 5146d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 51479e8cd2f5STejun Heo */ 5148513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 51499e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 51509e8cd2f5STejun Heo { 5151a0111cf6SLai Jiangshan int ret; 51529e8cd2f5STejun Heo 5153509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5154509b3204SDaniel Jordan 5155509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5156a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5157509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 51582d5f0764SLai Jiangshan 51592d5f0764SLai Jiangshan return ret; 51609e8cd2f5STejun Heo } 51619e8cd2f5STejun Heo 51624c16bd32STejun Heo /** 5163fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 51644c16bd32STejun Heo * @wq: the target workqueue 51654cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 51664cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 51674c16bd32STejun Heo * @online: whether @cpu is coming up or going down 51684c16bd32STejun Heo * 51694c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5170fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 51714c16bd32STejun Heo * @wq accordingly. 51724c16bd32STejun Heo * 51734c16bd32STejun Heo * 5174fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5175fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5176fef59c9cSTejun Heo * 5177fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5178fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5179fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5180fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5181fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5182fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 51834c16bd32STejun Heo */ 5184fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 51854cbfd3deSTejun Heo int hotplug_cpu, bool online) 51864c16bd32STejun Heo { 51874cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 51884c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 51894c16bd32STejun Heo struct workqueue_attrs *target_attrs; 51904c16bd32STejun Heo 51914c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 51924c16bd32STejun Heo 519384193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 51944c16bd32STejun Heo return; 51954c16bd32STejun Heo 51964c16bd32STejun Heo /* 51974c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 51984c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 51994c16bd32STejun Heo * CPU hotplug exclusion. 52004c16bd32STejun Heo */ 5201fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 52024c16bd32STejun Heo 52034c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 52040f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 52054c16bd32STejun Heo 5206636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 52079546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 52089f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5209f7142ed4SLai Jiangshan return; 52104c16bd32STejun Heo 52114c16bd32STejun Heo /* create a new pwq */ 52124c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 52134c16bd32STejun Heo if (!pwq) { 5214fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 52154c16bd32STejun Heo wq->name); 521677f300b1SDaeseok Youn goto use_dfl_pwq; 52174c16bd32STejun Heo } 52184c16bd32STejun Heo 5219f7142ed4SLai Jiangshan /* Install the new pwq. */ 52204c16bd32STejun Heo mutex_lock(&wq->mutex); 5221636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 52224c16bd32STejun Heo goto out_unlock; 52234c16bd32STejun Heo 52244c16bd32STejun Heo use_dfl_pwq: 5225f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 52269f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 52279f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 52289f66cff2STejun Heo get_pwq(pwq); 52299f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 52309f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 52314c16bd32STejun Heo out_unlock: 52324c16bd32STejun Heo mutex_unlock(&wq->mutex); 52334c16bd32STejun Heo put_pwq_unlocked(old_pwq); 52344c16bd32STejun Heo } 52354c16bd32STejun Heo 523630cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 52371da177e4SLinus Torvalds { 523849e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 52398a2b7538STejun Heo int cpu, ret; 5240e1d8aa9fSFrederic Weisbecker 5241687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5242ee1ceef7STejun Heo if (!wq->cpu_pwq) 5243687a9aa5STejun Heo goto enomem; 524430cdf249STejun Heo 5245636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 524630cdf249STejun Heo for_each_possible_cpu(cpu) { 52474cb1ef64STejun Heo struct pool_workqueue **pwq_p; 52484cb1ef64STejun Heo struct worker_pool __percpu *pools; 52494cb1ef64STejun Heo struct worker_pool *pool; 52504cb1ef64STejun Heo 52514cb1ef64STejun Heo if (wq->flags & WQ_BH) 52524cb1ef64STejun Heo pools = bh_worker_pools; 52534cb1ef64STejun Heo else 52544cb1ef64STejun Heo pools = cpu_worker_pools; 52554cb1ef64STejun Heo 52564cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 52574cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 525830cdf249STejun Heo 5259687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5260687a9aa5STejun Heo pool->node); 5261687a9aa5STejun Heo if (!*pwq_p) 5262687a9aa5STejun Heo goto enomem; 5263687a9aa5STejun Heo 5264687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5265f147f29eSTejun Heo 5266f147f29eSTejun Heo mutex_lock(&wq->mutex); 5267687a9aa5STejun Heo link_pwq(*pwq_p); 5268f147f29eSTejun Heo mutex_unlock(&wq->mutex); 526930cdf249STejun Heo } 527030cdf249STejun Heo return 0; 5271509b3204SDaniel Jordan } 5272509b3204SDaniel Jordan 5273ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5274509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 52759f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 52769f66cff2STejun Heo 52778a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 52788a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 52799f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 52809f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 52819f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 52828a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 52839e8cd2f5STejun Heo } else { 5284509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 52859e8cd2f5STejun Heo } 5286ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5287509b3204SDaniel Jordan 528864344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 528964344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 529064344553SZqiang */ 529164344553SZqiang if (ret) 529264344553SZqiang kthread_flush_worker(pwq_release_worker); 529364344553SZqiang 5294509b3204SDaniel Jordan return ret; 5295687a9aa5STejun Heo 5296687a9aa5STejun Heo enomem: 5297687a9aa5STejun Heo if (wq->cpu_pwq) { 52987b42f401SZqiang for_each_possible_cpu(cpu) { 52997b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 53007b42f401SZqiang 53017b42f401SZqiang if (pwq) 53027b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 53037b42f401SZqiang } 5304687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5305687a9aa5STejun Heo wq->cpu_pwq = NULL; 5306687a9aa5STejun Heo } 5307687a9aa5STejun Heo return -ENOMEM; 53080f900049STejun Heo } 53090f900049STejun Heo 5310f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5311f3421797STejun Heo const char *name) 5312b71ab8c2STejun Heo { 5313636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5314044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5315636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5316b71ab8c2STejun Heo 5317636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5318b71ab8c2STejun Heo } 5319b71ab8c2STejun Heo 5320983c7515STejun Heo /* 5321983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5322983c7515STejun Heo * to guarantee forward progress. 5323983c7515STejun Heo */ 5324983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5325983c7515STejun Heo { 5326983c7515STejun Heo struct worker *rescuer; 5327b92b36eaSDan Carpenter int ret; 5328983c7515STejun Heo 5329983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5330983c7515STejun Heo return 0; 5331983c7515STejun Heo 5332983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 53334c0736a7SPetr Mladek if (!rescuer) { 53344c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 53354c0736a7SPetr Mladek wq->name); 5336983c7515STejun Heo return -ENOMEM; 53374c0736a7SPetr Mladek } 5338983c7515STejun Heo 5339983c7515STejun Heo rescuer->rescue_wq = wq; 5340b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5341f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5342b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 53434c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 53444c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5345983c7515STejun Heo kfree(rescuer); 5346b92b36eaSDan Carpenter return ret; 5347983c7515STejun Heo } 5348983c7515STejun Heo 5349983c7515STejun Heo wq->rescuer = rescuer; 535085f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 535149584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 535285f0ab43SJuri Lelli else 5353983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5354983c7515STejun Heo wake_up_process(rescuer->task); 5355983c7515STejun Heo 5356983c7515STejun Heo return 0; 5357983c7515STejun Heo } 5358983c7515STejun Heo 5359a045a272STejun Heo /** 5360a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5361a045a272STejun Heo * @wq: target workqueue 5362a045a272STejun Heo * 5363a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5364a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5365a045a272STejun Heo * @wq->max_active to zero. 5366a045a272STejun Heo */ 5367a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5368a045a272STejun Heo { 5369c5404d4eSTejun Heo bool activated; 53705797b1c1STejun Heo int new_max, new_min; 5371a045a272STejun Heo 5372a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5373a045a272STejun Heo 5374a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 53755797b1c1STejun Heo new_max = 0; 53765797b1c1STejun Heo new_min = 0; 53775797b1c1STejun Heo } else { 53785797b1c1STejun Heo new_max = wq->saved_max_active; 53795797b1c1STejun Heo new_min = wq->saved_min_active; 5380a045a272STejun Heo } 5381a045a272STejun Heo 53825797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5383a045a272STejun Heo return; 5384a045a272STejun Heo 5385a045a272STejun Heo /* 53865797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5387a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5388a045a272STejun Heo * because new work items are always queued behind existing inactive 5389a045a272STejun Heo * work items if there are any. 5390a045a272STejun Heo */ 53915797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 53925797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 53935797b1c1STejun Heo 53945797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 53955797b1c1STejun Heo wq_update_node_max_active(wq, -1); 53965797b1c1STejun Heo 53975797b1c1STejun Heo if (new_max == 0) 53985797b1c1STejun Heo return; 5399a045a272STejun Heo 5400c5404d4eSTejun Heo /* 5401c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5402c5404d4eSTejun Heo * until max_active is filled. 5403c5404d4eSTejun Heo */ 5404c5404d4eSTejun Heo do { 5405c5404d4eSTejun Heo struct pool_workqueue *pwq; 5406c5404d4eSTejun Heo 5407c5404d4eSTejun Heo activated = false; 5408a045a272STejun Heo for_each_pwq(pwq, wq) { 5409c26e2f2eSTejun Heo unsigned long irq_flags; 5410a045a272STejun Heo 5411c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5412c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 54135797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5414c5404d4eSTejun Heo activated = true; 5415a045a272STejun Heo kick_pool(pwq->pool); 5416c5404d4eSTejun Heo } 5417c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 5418a045a272STejun Heo } 5419c5404d4eSTejun Heo } while (activated); 5420a045a272STejun Heo } 5421a045a272STejun Heo 5422a2775bbcSMathieu Malaterre __printf(1, 4) 5423669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 542497e37d7bSTejun Heo unsigned int flags, 5425669de8bdSBart Van Assche int max_active, ...) 54263af24433SOleg Nesterov { 5427ecf6881fSTejun Heo va_list args; 54283af24433SOleg Nesterov struct workqueue_struct *wq; 542991ccc6e7STejun Heo size_t wq_size; 543091ccc6e7STejun Heo int name_len; 5431b196be89STejun Heo 54324cb1ef64STejun Heo if (flags & WQ_BH) { 54334cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 54344cb1ef64STejun Heo return NULL; 54354cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 54364cb1ef64STejun Heo return NULL; 54374cb1ef64STejun Heo } 54384cb1ef64STejun Heo 5439cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5440cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5441cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5442cee22a15SViresh Kumar 5443ecf6881fSTejun Heo /* allocate wq and format name */ 544491ccc6e7STejun Heo if (flags & WQ_UNBOUND) 544591ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 544691ccc6e7STejun Heo else 544791ccc6e7STejun Heo wq_size = sizeof(*wq); 544891ccc6e7STejun Heo 544991ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5450b196be89STejun Heo if (!wq) 5451d2c1d404STejun Heo return NULL; 5452b196be89STejun Heo 54536029a918STejun Heo if (flags & WQ_UNBOUND) { 5454be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 54556029a918STejun Heo if (!wq->unbound_attrs) 54566029a918STejun Heo goto err_free_wq; 54576029a918STejun Heo } 54586029a918STejun Heo 5459669de8bdSBart Van Assche va_start(args, max_active); 546091ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5461b196be89STejun Heo va_end(args); 54623af24433SOleg Nesterov 546391ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 546491ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 546591ccc6e7STejun Heo wq->name); 546631c89007SAudra Mitchell 54674cb1ef64STejun Heo if (flags & WQ_BH) { 54684cb1ef64STejun Heo /* 54694cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 54704cb1ef64STejun Heo * and don't impose any max_active limit. 54714cb1ef64STejun Heo */ 54724cb1ef64STejun Heo max_active = INT_MAX; 54734cb1ef64STejun Heo } else { 5474d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5475b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 54764cb1ef64STejun Heo } 54773af24433SOleg Nesterov 5478b196be89STejun Heo /* init wq */ 547997e37d7bSTejun Heo wq->flags = flags; 5480a045a272STejun Heo wq->max_active = max_active; 54815797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 54825797b1c1STejun Heo wq->saved_max_active = wq->max_active; 54835797b1c1STejun Heo wq->saved_min_active = wq->min_active; 54843c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5485112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 548630cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 548773f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 548873f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5489493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 54903af24433SOleg Nesterov 5491669de8bdSBart Van Assche wq_init_lockdep(wq); 5492cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 54933af24433SOleg Nesterov 549491ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 549591ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 549682efcab3SBart Van Assche goto err_unreg_lockdep; 549791ccc6e7STejun Heo } 549891ccc6e7STejun Heo 549991ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 550091ccc6e7STejun Heo goto err_free_node_nr_active; 55011537663fSTejun Heo 550240c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5503d2c1d404STejun Heo goto err_destroy; 5504e22bee78STejun Heo 5505226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5506226223abSTejun Heo goto err_destroy; 5507226223abSTejun Heo 55086af8bf3dSOleg Nesterov /* 550968e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 551068e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 551168e13a67SLai Jiangshan * list. 55126af8bf3dSOleg Nesterov */ 551368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5514a0a1a5fdSTejun Heo 5515a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5516a045a272STejun Heo wq_adjust_max_active(wq); 5517a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5518a0a1a5fdSTejun Heo 5519e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5520a0a1a5fdSTejun Heo 552168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 55223af24433SOleg Nesterov 55233af24433SOleg Nesterov return wq; 5524d2c1d404STejun Heo 552591ccc6e7STejun Heo err_free_node_nr_active: 552691ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 552791ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 552882efcab3SBart Van Assche err_unreg_lockdep: 5529009bb421SBart Van Assche wq_unregister_lockdep(wq); 5530009bb421SBart Van Assche wq_free_lockdep(wq); 553182efcab3SBart Van Assche err_free_wq: 55326029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 55334690c4abSTejun Heo kfree(wq); 5534d2c1d404STejun Heo return NULL; 5535d2c1d404STejun Heo err_destroy: 5536d2c1d404STejun Heo destroy_workqueue(wq); 55374690c4abSTejun Heo return NULL; 55381da177e4SLinus Torvalds } 5539669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 55401da177e4SLinus Torvalds 5541c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5542c29eb853STejun Heo { 5543c29eb853STejun Heo int i; 5544c29eb853STejun Heo 5545c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5546c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5547c29eb853STejun Heo return true; 5548c29eb853STejun Heo 55499f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5550c29eb853STejun Heo return true; 5551afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5552c29eb853STejun Heo return true; 5553c29eb853STejun Heo 5554c29eb853STejun Heo return false; 5555c29eb853STejun Heo } 5556c29eb853STejun Heo 55573af24433SOleg Nesterov /** 55583af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 55593af24433SOleg Nesterov * @wq: target workqueue 55603af24433SOleg Nesterov * 55613af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 55623af24433SOleg Nesterov */ 55633af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 55643af24433SOleg Nesterov { 556549e3cf44STejun Heo struct pool_workqueue *pwq; 5566636b927eSTejun Heo int cpu; 55673af24433SOleg Nesterov 5568def98c84STejun Heo /* 5569def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5570def98c84STejun Heo * lead to sysfs name conflicts. 5571def98c84STejun Heo */ 5572def98c84STejun Heo workqueue_sysfs_unregister(wq); 5573def98c84STejun Heo 557433e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 557533e3f0a3SRichard Clark mutex_lock(&wq->mutex); 557633e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 557733e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 557833e3f0a3SRichard Clark 55799c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 55809c5a2ba7STejun Heo drain_workqueue(wq); 5581c8efcc25STejun Heo 5582def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5583def98c84STejun Heo if (wq->rescuer) { 5584def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5585def98c84STejun Heo 5586def98c84STejun Heo /* this prevents new queueing */ 5587a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5588def98c84STejun Heo wq->rescuer = NULL; 5589a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5590def98c84STejun Heo 5591def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5592def98c84STejun Heo kthread_stop(rescuer->task); 55938efe1223STejun Heo kfree(rescuer); 5594def98c84STejun Heo } 5595def98c84STejun Heo 5596c29eb853STejun Heo /* 5597c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5598c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5599c29eb853STejun Heo */ 5600c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5601b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 560249e3cf44STejun Heo for_each_pwq(pwq, wq) { 5603a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5604c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 56051d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5606e66b39afSTejun Heo __func__, wq->name); 5607c29eb853STejun Heo show_pwq(pwq); 5608a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5609b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5610c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 561155df0933SImran Khan show_one_workqueue(wq); 56126183c009STejun Heo return; 561376af4d93STejun Heo } 5614a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 561576af4d93STejun Heo } 5616b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 56176183c009STejun Heo 5618a0a1a5fdSTejun Heo /* 5619a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5620a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5621a0a1a5fdSTejun Heo */ 5622e2dca7adSTejun Heo list_del_rcu(&wq->list); 562368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 56243af24433SOleg Nesterov 56258864b4e5STejun Heo /* 5626636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5627636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5628636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 56298864b4e5STejun Heo */ 5630636b927eSTejun Heo rcu_read_lock(); 5631636b927eSTejun Heo 5632636b927eSTejun Heo for_each_possible_cpu(cpu) { 56339f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 56349f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 56354c16bd32STejun Heo } 56364c16bd32STejun Heo 56379f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 56389f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5639636b927eSTejun Heo 5640636b927eSTejun Heo rcu_read_unlock(); 56413af24433SOleg Nesterov } 56423af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 56433af24433SOleg Nesterov 5644dcd989cbSTejun Heo /** 5645dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5646dcd989cbSTejun Heo * @wq: target workqueue 5647dcd989cbSTejun Heo * @max_active: new max_active value. 5648dcd989cbSTejun Heo * 56495797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 56505797b1c1STejun Heo * comment. 5651dcd989cbSTejun Heo * 5652dcd989cbSTejun Heo * CONTEXT: 5653dcd989cbSTejun Heo * Don't call from IRQ context. 5654dcd989cbSTejun Heo */ 5655dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5656dcd989cbSTejun Heo { 56574cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 56584cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 56594cb1ef64STejun Heo return; 56608719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 56613bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 56628719dceaSTejun Heo return; 56638719dceaSTejun Heo 5664f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5665dcd989cbSTejun Heo 5666a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5667dcd989cbSTejun Heo 5668dcd989cbSTejun Heo wq->saved_max_active = max_active; 56695797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 56705797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 56715797b1c1STejun Heo 5672a045a272STejun Heo wq_adjust_max_active(wq); 5673dcd989cbSTejun Heo 5674a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5675dcd989cbSTejun Heo } 5676dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5677dcd989cbSTejun Heo 5678dcd989cbSTejun Heo /** 56798f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue 56808f172181STejun Heo * @wq: target unbound workqueue 56818f172181STejun Heo * @min_active: new min_active value 56828f172181STejun Heo * 56838f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an 56848f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active 56858f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be 56868f172181STejun Heo * able to process min_active number of interdependent work items which is 56878f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default. 56888f172181STejun Heo * 56898f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current 56908f172181STejun Heo * max_active. 56918f172181STejun Heo */ 56928f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) 56938f172181STejun Heo { 56948f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */ 56958f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != 56968f172181STejun Heo WQ_UNBOUND)) 56978f172181STejun Heo return; 56988f172181STejun Heo 56998f172181STejun Heo mutex_lock(&wq->mutex); 57008f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); 57018f172181STejun Heo wq_adjust_max_active(wq); 57028f172181STejun Heo mutex_unlock(&wq->mutex); 57038f172181STejun Heo } 57048f172181STejun Heo 57058f172181STejun Heo /** 570627d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 570727d4ee03SLukas Wunner * 570827d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 570927d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 571027d4ee03SLukas Wunner * 571127d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 571227d4ee03SLukas Wunner */ 571327d4ee03SLukas Wunner struct work_struct *current_work(void) 571427d4ee03SLukas Wunner { 571527d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 571627d4ee03SLukas Wunner 571727d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 571827d4ee03SLukas Wunner } 571927d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 572027d4ee03SLukas Wunner 572127d4ee03SLukas Wunner /** 5722e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5723e6267616STejun Heo * 5724e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5725e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5726d185af30SYacine Belkadi * 5727d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5728e6267616STejun Heo */ 5729e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5730e6267616STejun Heo { 5731e6267616STejun Heo struct worker *worker = current_wq_worker(); 5732e6267616STejun Heo 57336a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5734e6267616STejun Heo } 5735e6267616STejun Heo 5736e6267616STejun Heo /** 5737dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5738dcd989cbSTejun Heo * @cpu: CPU in question 5739dcd989cbSTejun Heo * @wq: target workqueue 5740dcd989cbSTejun Heo * 5741dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5742dcd989cbSTejun Heo * no synchronization around this function and the test result is 5743dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5744dcd989cbSTejun Heo * 5745d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5746636b927eSTejun Heo * 5747636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5748636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5749636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5750636b927eSTejun Heo * other CPUs. 5751d3251859STejun Heo * 5752d185af30SYacine Belkadi * Return: 5753dcd989cbSTejun Heo * %true if congested, %false otherwise. 5754dcd989cbSTejun Heo */ 5755d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5756dcd989cbSTejun Heo { 57577fb98ea7STejun Heo struct pool_workqueue *pwq; 575876af4d93STejun Heo bool ret; 575976af4d93STejun Heo 576024acfb71SThomas Gleixner rcu_read_lock(); 576124acfb71SThomas Gleixner preempt_disable(); 57627fb98ea7STejun Heo 5763d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5764d3251859STejun Heo cpu = smp_processor_id(); 5765d3251859STejun Heo 5766687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5767f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5768636b927eSTejun Heo 576924acfb71SThomas Gleixner preempt_enable(); 577024acfb71SThomas Gleixner rcu_read_unlock(); 577176af4d93STejun Heo 577276af4d93STejun Heo return ret; 5773dcd989cbSTejun Heo } 5774dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5775dcd989cbSTejun Heo 5776dcd989cbSTejun Heo /** 5777dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5778dcd989cbSTejun Heo * @work: the work to be tested 5779dcd989cbSTejun Heo * 5780dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5781dcd989cbSTejun Heo * synchronization around this function and the test result is 5782dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5783dcd989cbSTejun Heo * 5784d185af30SYacine Belkadi * Return: 5785dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5786dcd989cbSTejun Heo */ 5787dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5788dcd989cbSTejun Heo { 5789fa1b54e6STejun Heo struct worker_pool *pool; 5790c26e2f2eSTejun Heo unsigned long irq_flags; 5791dcd989cbSTejun Heo unsigned int ret = 0; 5792dcd989cbSTejun Heo 5793dcd989cbSTejun Heo if (work_pending(work)) 5794dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5795038366c5SLai Jiangshan 579624acfb71SThomas Gleixner rcu_read_lock(); 5797fa1b54e6STejun Heo pool = get_work_pool(work); 5798038366c5SLai Jiangshan if (pool) { 5799c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 5800c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5801dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5802c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 5803038366c5SLai Jiangshan } 580424acfb71SThomas Gleixner rcu_read_unlock(); 5805dcd989cbSTejun Heo 5806dcd989cbSTejun Heo return ret; 5807dcd989cbSTejun Heo } 5808dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5809dcd989cbSTejun Heo 58103d1cb205STejun Heo /** 58113d1cb205STejun Heo * set_worker_desc - set description for the current work item 58123d1cb205STejun Heo * @fmt: printf-style format string 58133d1cb205STejun Heo * @...: arguments for the format string 58143d1cb205STejun Heo * 58153d1cb205STejun Heo * This function can be called by a running work function to describe what 58163d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 58173d1cb205STejun Heo * information will be printed out together to help debugging. The 58183d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 58193d1cb205STejun Heo */ 58203d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 58213d1cb205STejun Heo { 58223d1cb205STejun Heo struct worker *worker = current_wq_worker(); 58233d1cb205STejun Heo va_list args; 58243d1cb205STejun Heo 58253d1cb205STejun Heo if (worker) { 58263d1cb205STejun Heo va_start(args, fmt); 58273d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 58283d1cb205STejun Heo va_end(args); 58293d1cb205STejun Heo } 58303d1cb205STejun Heo } 58315c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 58323d1cb205STejun Heo 58333d1cb205STejun Heo /** 58343d1cb205STejun Heo * print_worker_info - print out worker information and description 58353d1cb205STejun Heo * @log_lvl: the log level to use when printing 58363d1cb205STejun Heo * @task: target task 58373d1cb205STejun Heo * 58383d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 58393d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 58403d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 58413d1cb205STejun Heo * 58423d1cb205STejun Heo * This function can be safely called on any task as long as the 58433d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 58443d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 58453d1cb205STejun Heo */ 58463d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 58473d1cb205STejun Heo { 58483d1cb205STejun Heo work_func_t *fn = NULL; 58493d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 58503d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 58513d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 58523d1cb205STejun Heo struct workqueue_struct *wq = NULL; 58533d1cb205STejun Heo struct worker *worker; 58543d1cb205STejun Heo 58553d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 58563d1cb205STejun Heo return; 58573d1cb205STejun Heo 58583d1cb205STejun Heo /* 58593d1cb205STejun Heo * This function is called without any synchronization and @task 58603d1cb205STejun Heo * could be in any state. Be careful with dereferences. 58613d1cb205STejun Heo */ 5862e700591aSPetr Mladek worker = kthread_probe_data(task); 58633d1cb205STejun Heo 58643d1cb205STejun Heo /* 58658bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 58668bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 58673d1cb205STejun Heo */ 5868fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5869fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5870fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5871fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5872fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 58733d1cb205STejun Heo 58743d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5875d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 58768bf89593STejun Heo if (strcmp(name, desc)) 58773d1cb205STejun Heo pr_cont(" (%s)", desc); 58783d1cb205STejun Heo pr_cont("\n"); 58793d1cb205STejun Heo } 58803d1cb205STejun Heo } 58813d1cb205STejun Heo 58823494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 58833494fc30STejun Heo { 58843494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 58853494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 58863494fc30STejun Heo pr_cont(" node=%d", pool->node); 58874cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 58884cb1ef64STejun Heo if (pool->flags & POOL_BH) 58894cb1ef64STejun Heo pr_cont(" bh%s", 58904cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 58914cb1ef64STejun Heo else 58924cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 58934cb1ef64STejun Heo } 58944cb1ef64STejun Heo 58954cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 58964cb1ef64STejun Heo { 58974cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 58984cb1ef64STejun Heo 58994cb1ef64STejun Heo if (pool->flags & WQ_BH) 59004cb1ef64STejun Heo pr_cont("bh%s", 59014cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 59024cb1ef64STejun Heo else 59034cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 59044cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 59053494fc30STejun Heo } 59063494fc30STejun Heo 5907c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 5908c76feb0dSPaul E. McKenney bool comma; 5909c76feb0dSPaul E. McKenney work_func_t func; 5910c76feb0dSPaul E. McKenney long ctr; 5911c76feb0dSPaul E. McKenney }; 5912c76feb0dSPaul E. McKenney 5913c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 5914c76feb0dSPaul E. McKenney { 5915c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 5916c76feb0dSPaul E. McKenney goto out_record; 5917c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 5918c76feb0dSPaul E. McKenney pcwsp->ctr++; 5919c76feb0dSPaul E. McKenney return; 5920c76feb0dSPaul E. McKenney } 5921c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 5922c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 5923c76feb0dSPaul E. McKenney else 5924c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 5925c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 5926c76feb0dSPaul E. McKenney out_record: 5927c76feb0dSPaul E. McKenney if ((long)func == -1L) 5928c76feb0dSPaul E. McKenney return; 5929c76feb0dSPaul E. McKenney pcwsp->comma = comma; 5930c76feb0dSPaul E. McKenney pcwsp->func = func; 5931c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 5932c76feb0dSPaul E. McKenney } 5933c76feb0dSPaul E. McKenney 5934c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 59353494fc30STejun Heo { 59363494fc30STejun Heo if (work->func == wq_barrier_func) { 59373494fc30STejun Heo struct wq_barrier *barr; 59383494fc30STejun Heo 59393494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 59403494fc30STejun Heo 5941c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 59423494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 59433494fc30STejun Heo task_pid_nr(barr->task)); 59443494fc30STejun Heo } else { 5945c76feb0dSPaul E. McKenney if (!comma) 5946c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 5947c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 59483494fc30STejun Heo } 59493494fc30STejun Heo } 59503494fc30STejun Heo 59513494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 59523494fc30STejun Heo { 5953c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 59543494fc30STejun Heo struct worker_pool *pool = pwq->pool; 59553494fc30STejun Heo struct work_struct *work; 59563494fc30STejun Heo struct worker *worker; 59573494fc30STejun Heo bool has_in_flight = false, has_pending = false; 59583494fc30STejun Heo int bkt; 59593494fc30STejun Heo 59603494fc30STejun Heo pr_info(" pwq %d:", pool->id); 59613494fc30STejun Heo pr_cont_pool_info(pool); 59623494fc30STejun Heo 5963a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 5964a045a272STejun Heo pwq->nr_active, pwq->refcnt, 59653494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 59663494fc30STejun Heo 59673494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59683494fc30STejun Heo if (worker->current_pwq == pwq) { 59693494fc30STejun Heo has_in_flight = true; 59703494fc30STejun Heo break; 59713494fc30STejun Heo } 59723494fc30STejun Heo } 59733494fc30STejun Heo if (has_in_flight) { 59743494fc30STejun Heo bool comma = false; 59753494fc30STejun Heo 59763494fc30STejun Heo pr_info(" in-flight:"); 59773494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 59783494fc30STejun Heo if (worker->current_pwq != pwq) 59793494fc30STejun Heo continue; 59803494fc30STejun Heo 59814cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 59824cb1ef64STejun Heo pr_cont_worker_id(worker); 59834cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 59843494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 5985c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 5986c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 59873494fc30STejun Heo comma = true; 59883494fc30STejun Heo } 59893494fc30STejun Heo pr_cont("\n"); 59903494fc30STejun Heo } 59913494fc30STejun Heo 59923494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 59933494fc30STejun Heo if (get_work_pwq(work) == pwq) { 59943494fc30STejun Heo has_pending = true; 59953494fc30STejun Heo break; 59963494fc30STejun Heo } 59973494fc30STejun Heo } 59983494fc30STejun Heo if (has_pending) { 59993494fc30STejun Heo bool comma = false; 60003494fc30STejun Heo 60013494fc30STejun Heo pr_info(" pending:"); 60023494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 60033494fc30STejun Heo if (get_work_pwq(work) != pwq) 60043494fc30STejun Heo continue; 60053494fc30STejun Heo 6006c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 60073494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 60083494fc30STejun Heo } 6009c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60103494fc30STejun Heo pr_cont("\n"); 60113494fc30STejun Heo } 60123494fc30STejun Heo 6013f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 60143494fc30STejun Heo bool comma = false; 60153494fc30STejun Heo 6016f97a4a1aSLai Jiangshan pr_info(" inactive:"); 6017f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 6018c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 60193494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 60203494fc30STejun Heo } 6021c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60223494fc30STejun Heo pr_cont("\n"); 60233494fc30STejun Heo } 60243494fc30STejun Heo } 60253494fc30STejun Heo 60263494fc30STejun Heo /** 602755df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 602855df0933SImran Khan * @wq: workqueue whose state will be printed 60293494fc30STejun Heo */ 603055df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 60313494fc30STejun Heo { 60323494fc30STejun Heo struct pool_workqueue *pwq; 60333494fc30STejun Heo bool idle = true; 6034c26e2f2eSTejun Heo unsigned long irq_flags; 60353494fc30STejun Heo 60363494fc30STejun Heo for_each_pwq(pwq, wq) { 6037afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 60383494fc30STejun Heo idle = false; 60393494fc30STejun Heo break; 60403494fc30STejun Heo } 60413494fc30STejun Heo } 604255df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 604355df0933SImran Khan return; 60443494fc30STejun Heo 60453494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 60463494fc30STejun Heo 60473494fc30STejun Heo for_each_pwq(pwq, wq) { 6048c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 6049afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 605057116ce1SJohan Hovold /* 605157116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 605257116ce1SJohan Hovold * drivers that queue work while holding locks 605357116ce1SJohan Hovold * also taken in their write paths. 605457116ce1SJohan Hovold */ 605557116ce1SJohan Hovold printk_deferred_enter(); 60563494fc30STejun Heo show_pwq(pwq); 605757116ce1SJohan Hovold printk_deferred_exit(); 605857116ce1SJohan Hovold } 6059c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 606062635ea8SSergey Senozhatsky /* 606162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 606255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 606362635ea8SSergey Senozhatsky * hard lockup. 606462635ea8SSergey Senozhatsky */ 606562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 60663494fc30STejun Heo } 606755df0933SImran Khan 60683494fc30STejun Heo } 60693494fc30STejun Heo 607055df0933SImran Khan /** 607155df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 607255df0933SImran Khan * @pool: worker pool whose state will be printed 607355df0933SImran Khan */ 607455df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 607555df0933SImran Khan { 60763494fc30STejun Heo struct worker *worker; 60773494fc30STejun Heo bool first = true; 6078c26e2f2eSTejun Heo unsigned long irq_flags; 6079335a42ebSPetr Mladek unsigned long hung = 0; 60803494fc30STejun Heo 6081c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 60823494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 60833494fc30STejun Heo goto next_pool; 6084335a42ebSPetr Mladek 6085335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6086335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6087335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6088335a42ebSPetr Mladek 608957116ce1SJohan Hovold /* 609057116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 609157116ce1SJohan Hovold * queue work while holding locks also taken in their write 609257116ce1SJohan Hovold * paths. 609357116ce1SJohan Hovold */ 609457116ce1SJohan Hovold printk_deferred_enter(); 60953494fc30STejun Heo pr_info("pool %d:", pool->id); 60963494fc30STejun Heo pr_cont_pool_info(pool); 6097335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 60983494fc30STejun Heo if (pool->manager) 60993494fc30STejun Heo pr_cont(" manager: %d", 61003494fc30STejun Heo task_pid_nr(pool->manager->task)); 61013494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 61024cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 61034cb1ef64STejun Heo pr_cont_worker_id(worker); 61043494fc30STejun Heo first = false; 61053494fc30STejun Heo } 61063494fc30STejun Heo pr_cont("\n"); 610757116ce1SJohan Hovold printk_deferred_exit(); 61083494fc30STejun Heo next_pool: 6109c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 611062635ea8SSergey Senozhatsky /* 611162635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 611255df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 611362635ea8SSergey Senozhatsky * hard lockup. 611462635ea8SSergey Senozhatsky */ 611562635ea8SSergey Senozhatsky touch_nmi_watchdog(); 611655df0933SImran Khan 61173494fc30STejun Heo } 61183494fc30STejun Heo 611955df0933SImran Khan /** 612055df0933SImran Khan * show_all_workqueues - dump workqueue state 612155df0933SImran Khan * 6122704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 612355df0933SImran Khan */ 612455df0933SImran Khan void show_all_workqueues(void) 612555df0933SImran Khan { 612655df0933SImran Khan struct workqueue_struct *wq; 612755df0933SImran Khan struct worker_pool *pool; 612855df0933SImran Khan int pi; 612955df0933SImran Khan 613055df0933SImran Khan rcu_read_lock(); 613155df0933SImran Khan 613255df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 613355df0933SImran Khan 613455df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 613555df0933SImran Khan show_one_workqueue(wq); 613655df0933SImran Khan 613755df0933SImran Khan for_each_pool(pool, pi) 613855df0933SImran Khan show_one_worker_pool(pool); 613955df0933SImran Khan 614024acfb71SThomas Gleixner rcu_read_unlock(); 61413494fc30STejun Heo } 61423494fc30STejun Heo 6143704bc669SJungseung Lee /** 6144704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6145704bc669SJungseung Lee * 6146704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6147704bc669SJungseung Lee * still busy. 6148704bc669SJungseung Lee */ 6149704bc669SJungseung Lee void show_freezable_workqueues(void) 6150704bc669SJungseung Lee { 6151704bc669SJungseung Lee struct workqueue_struct *wq; 6152704bc669SJungseung Lee 6153704bc669SJungseung Lee rcu_read_lock(); 6154704bc669SJungseung Lee 6155704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6156704bc669SJungseung Lee 6157704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6158704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6159704bc669SJungseung Lee continue; 6160704bc669SJungseung Lee show_one_workqueue(wq); 6161704bc669SJungseung Lee } 6162704bc669SJungseung Lee 6163704bc669SJungseung Lee rcu_read_unlock(); 6164704bc669SJungseung Lee } 6165704bc669SJungseung Lee 61666b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 61676b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 61686b59808bSTejun Heo { 61696b59808bSTejun Heo int off; 61706b59808bSTejun Heo 61716b59808bSTejun Heo /* always show the actual comm */ 61726b59808bSTejun Heo off = strscpy(buf, task->comm, size); 61736b59808bSTejun Heo if (off < 0) 61746b59808bSTejun Heo return; 61756b59808bSTejun Heo 6176197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 61776b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 61786b59808bSTejun Heo 6179197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6180197f6accSTejun Heo struct worker *worker = kthread_data(task); 6181197f6accSTejun Heo struct worker_pool *pool = worker->pool; 61826b59808bSTejun Heo 61836b59808bSTejun Heo if (pool) { 6184a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 61856b59808bSTejun Heo /* 6186197f6accSTejun Heo * ->desc tracks information (wq name or 6187197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6188197f6accSTejun Heo * current, prepend '+', otherwise '-'. 61896b59808bSTejun Heo */ 61906b59808bSTejun Heo if (worker->desc[0] != '\0') { 61916b59808bSTejun Heo if (worker->current_work) 61926b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 61936b59808bSTejun Heo worker->desc); 61946b59808bSTejun Heo else 61956b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 61966b59808bSTejun Heo worker->desc); 61976b59808bSTejun Heo } 6198a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 61996b59808bSTejun Heo } 6200197f6accSTejun Heo } 62016b59808bSTejun Heo 62026b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 62036b59808bSTejun Heo } 62046b59808bSTejun Heo 620566448bc2SMathieu Malaterre #ifdef CONFIG_SMP 620666448bc2SMathieu Malaterre 6207db7bccf4STejun Heo /* 6208db7bccf4STejun Heo * CPU hotplug. 6209db7bccf4STejun Heo * 6210e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6211112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6212706026c2STejun Heo * pool which make migrating pending and scheduled works very 6213e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 621494cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6215e22bee78STejun Heo * blocked draining impractical. 6216e22bee78STejun Heo * 621724647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6218628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6219628c78e7STejun Heo * cpu comes back online. 6220db7bccf4STejun Heo */ 6221db7bccf4STejun Heo 6222e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6223db7bccf4STejun Heo { 62244ce62e9eSTejun Heo struct worker_pool *pool; 6225db7bccf4STejun Heo struct worker *worker; 6226db7bccf4STejun Heo 6227f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 62281258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6229a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6230e22bee78STejun Heo 6231f2d5a0eeSTejun Heo /* 623292f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 623394cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 623411b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6235b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6236b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6237b4ac9384SLai Jiangshan * is on the same cpu. 6238f2d5a0eeSTejun Heo */ 6239da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6240403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6241db7bccf4STejun Heo 624224647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6243f2d5a0eeSTejun Heo 6244e22bee78STejun Heo /* 6245989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6246989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6247989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6248989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6249989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6250eb283428SLai Jiangshan * are served by workers tied to the pool. 6251e22bee78STejun Heo */ 6252bc35f7efSLai Jiangshan pool->nr_running = 0; 6253eb283428SLai Jiangshan 6254eb283428SLai Jiangshan /* 6255eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6256eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6257eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6258eb283428SLai Jiangshan */ 62590219a352STejun Heo kick_pool(pool); 6260989442d7SLai Jiangshan 6261a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6262989442d7SLai Jiangshan 6263793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6264793777bcSValentin Schneider unbind_worker(worker); 6265989442d7SLai Jiangshan 6266989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6267eb283428SLai Jiangshan } 6268db7bccf4STejun Heo } 6269db7bccf4STejun Heo 6270bd7c089eSTejun Heo /** 6271bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6272bd7c089eSTejun Heo * @pool: pool of interest 6273bd7c089eSTejun Heo * 6274a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6275bd7c089eSTejun Heo */ 6276bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6277bd7c089eSTejun Heo { 6278a9ab775bSTejun Heo struct worker *worker; 6279bd7c089eSTejun Heo 62801258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6281bd7c089eSTejun Heo 6282bd7c089eSTejun Heo /* 6283a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6284a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6285402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6286a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6287a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6288bd7c089eSTejun Heo */ 6289c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6290c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6291c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 62929546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6293c63a2e52SValentin Schneider } 6294a9ab775bSTejun Heo 6295a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6296f7c17d26SWanpeng Li 62973de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6298a9ab775bSTejun Heo 6299da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6300a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6301a9ab775bSTejun Heo 6302a9ab775bSTejun Heo /* 6303a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6304a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6305a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6306a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6307a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6308a9ab775bSTejun Heo * concurrency management. Note that when or whether 6309a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6310a9ab775bSTejun Heo * 6311c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6312a9ab775bSTejun Heo * tested without holding any lock in 63136d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6314a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6315a9ab775bSTejun Heo * management operations. 6316bd7c089eSTejun Heo */ 6317a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6318a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6319a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6320c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6321bd7c089eSTejun Heo } 6322a9ab775bSTejun Heo 6323a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6324bd7c089eSTejun Heo } 6325bd7c089eSTejun Heo 63267dbc725eSTejun Heo /** 63277dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 63287dbc725eSTejun Heo * @pool: unbound pool of interest 63297dbc725eSTejun Heo * @cpu: the CPU which is coming up 63307dbc725eSTejun Heo * 63317dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 63327dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 63337dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 63347dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 63357dbc725eSTejun Heo */ 63367dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 63377dbc725eSTejun Heo { 63387dbc725eSTejun Heo static cpumask_t cpumask; 63397dbc725eSTejun Heo struct worker *worker; 63407dbc725eSTejun Heo 63411258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 63427dbc725eSTejun Heo 63437dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 63447dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 63457dbc725eSTejun Heo return; 63467dbc725eSTejun Heo 63477dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 63487dbc725eSTejun Heo 63497dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6350da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6351d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 63527dbc725eSTejun Heo } 63537dbc725eSTejun Heo 63547ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 63551da177e4SLinus Torvalds { 63564ce62e9eSTejun Heo struct worker_pool *pool; 63571da177e4SLinus Torvalds 6358f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63593ce63377STejun Heo if (pool->nr_workers) 63603ce63377STejun Heo continue; 6361051e1850SLai Jiangshan if (!create_worker(pool)) 63627ee681b2SThomas Gleixner return -ENOMEM; 63633af24433SOleg Nesterov } 63647ee681b2SThomas Gleixner return 0; 63657ee681b2SThomas Gleixner } 63661da177e4SLinus Torvalds 63677ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 63687ee681b2SThomas Gleixner { 63697ee681b2SThomas Gleixner struct worker_pool *pool; 63707ee681b2SThomas Gleixner struct workqueue_struct *wq; 63717ee681b2SThomas Gleixner int pi; 63727ee681b2SThomas Gleixner 637368e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 63747dbc725eSTejun Heo 63757dbc725eSTejun Heo for_each_pool(pool, pi) { 63764cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 63774cb1ef64STejun Heo if (pool->flags & POOL_BH) 63784cb1ef64STejun Heo continue; 637994cf58bbSTejun Heo 63804cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6381f05b558dSLai Jiangshan if (pool->cpu == cpu) 638294cf58bbSTejun Heo rebind_workers(pool); 6383f05b558dSLai Jiangshan else if (pool->cpu < 0) 63847dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 63851258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 638694cf58bbSTejun Heo } 63877dbc725eSTejun Heo 6388fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 63894cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 639084193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 639184193c07STejun Heo 639284193c07STejun Heo if (attrs) { 639384193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 63944cbfd3deSTejun Heo int tcpu; 63954cbfd3deSTejun Heo 639684193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6397fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 63985797b1c1STejun Heo 63995797b1c1STejun Heo mutex_lock(&wq->mutex); 64005797b1c1STejun Heo wq_update_node_max_active(wq, -1); 64015797b1c1STejun Heo mutex_unlock(&wq->mutex); 64024cbfd3deSTejun Heo } 64034cbfd3deSTejun Heo } 64044c16bd32STejun Heo 640568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 64067ee681b2SThomas Gleixner return 0; 640765758202STejun Heo } 640865758202STejun Heo 64097ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 641065758202STejun Heo { 64114c16bd32STejun Heo struct workqueue_struct *wq; 64128db25e78STejun Heo 64134c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6414e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6415e8b3f8dbSLai Jiangshan return -1; 6416e8b3f8dbSLai Jiangshan 6417e8b3f8dbSLai Jiangshan unbind_workers(cpu); 64184c16bd32STejun Heo 6419fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 64204c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 64214cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 642284193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 642384193c07STejun Heo 642484193c07STejun Heo if (attrs) { 642584193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 64264cbfd3deSTejun Heo int tcpu; 64274cbfd3deSTejun Heo 642884193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6429fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 64305797b1c1STejun Heo 64315797b1c1STejun Heo mutex_lock(&wq->mutex); 64325797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 64335797b1c1STejun Heo mutex_unlock(&wq->mutex); 64344cbfd3deSTejun Heo } 64354cbfd3deSTejun Heo } 64364c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 64374c16bd32STejun Heo 64387ee681b2SThomas Gleixner return 0; 643965758202STejun Heo } 644065758202STejun Heo 64412d3854a3SRusty Russell struct work_for_cpu { 6442ed48ece2STejun Heo struct work_struct work; 64432d3854a3SRusty Russell long (*fn)(void *); 64442d3854a3SRusty Russell void *arg; 64452d3854a3SRusty Russell long ret; 64462d3854a3SRusty Russell }; 64472d3854a3SRusty Russell 6448ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 64492d3854a3SRusty Russell { 6450ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6451ed48ece2STejun Heo 64522d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 64532d3854a3SRusty Russell } 64542d3854a3SRusty Russell 64552d3854a3SRusty Russell /** 6456265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 64572d3854a3SRusty Russell * @cpu: the cpu to run on 64582d3854a3SRusty Russell * @fn: the function to run 64592d3854a3SRusty Russell * @arg: the function arg 6460265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64612d3854a3SRusty Russell * 646231ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 64636b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6464d185af30SYacine Belkadi * 6465d185af30SYacine Belkadi * Return: The value @fn returns. 64662d3854a3SRusty Russell */ 6467265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6468265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 64692d3854a3SRusty Russell { 6470ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 64712d3854a3SRusty Russell 6472265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6473ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 647412997d1aSBjorn Helgaas flush_work(&wfc.work); 6475440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 64762d3854a3SRusty Russell return wfc.ret; 64772d3854a3SRusty Russell } 6478265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 64790e8d6a93SThomas Gleixner 64800e8d6a93SThomas Gleixner /** 6481265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 64820e8d6a93SThomas Gleixner * @cpu: the cpu to run on 64830e8d6a93SThomas Gleixner * @fn: the function to run 64840e8d6a93SThomas Gleixner * @arg: the function argument 6485265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 64860e8d6a93SThomas Gleixner * 64870e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 64880e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 64890e8d6a93SThomas Gleixner * 64900e8d6a93SThomas Gleixner * Return: The value @fn returns. 64910e8d6a93SThomas Gleixner */ 6492265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6493265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 64940e8d6a93SThomas Gleixner { 64950e8d6a93SThomas Gleixner long ret = -ENODEV; 64960e8d6a93SThomas Gleixner 6497ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 64980e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6499265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6500ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 65010e8d6a93SThomas Gleixner return ret; 65020e8d6a93SThomas Gleixner } 6503265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 65042d3854a3SRusty Russell #endif /* CONFIG_SMP */ 65052d3854a3SRusty Russell 6506a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6507e7577c50SRusty Russell 6508a0a1a5fdSTejun Heo /** 6509a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6510a0a1a5fdSTejun Heo * 651158a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6512f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6513706026c2STejun Heo * pool->worklist. 6514a0a1a5fdSTejun Heo * 6515a0a1a5fdSTejun Heo * CONTEXT: 6516a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6517a0a1a5fdSTejun Heo */ 6518a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6519a0a1a5fdSTejun Heo { 652024b8a847STejun Heo struct workqueue_struct *wq; 6521a0a1a5fdSTejun Heo 652268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6523a0a1a5fdSTejun Heo 65246183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6525a0a1a5fdSTejun Heo workqueue_freezing = true; 6526a0a1a5fdSTejun Heo 652724b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6528a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6529a045a272STejun Heo wq_adjust_max_active(wq); 6530a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6531a1056305STejun Heo } 65325bcab335STejun Heo 653368e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6534a0a1a5fdSTejun Heo } 6535a0a1a5fdSTejun Heo 6536a0a1a5fdSTejun Heo /** 653758a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6538a0a1a5fdSTejun Heo * 6539a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6540a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6541a0a1a5fdSTejun Heo * 6542a0a1a5fdSTejun Heo * CONTEXT: 654368e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6544a0a1a5fdSTejun Heo * 6545d185af30SYacine Belkadi * Return: 654658a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 654758a69cb4STejun Heo * is complete. 6548a0a1a5fdSTejun Heo */ 6549a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6550a0a1a5fdSTejun Heo { 6551a0a1a5fdSTejun Heo bool busy = false; 655224b8a847STejun Heo struct workqueue_struct *wq; 655324b8a847STejun Heo struct pool_workqueue *pwq; 6554a0a1a5fdSTejun Heo 655568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6556a0a1a5fdSTejun Heo 65576183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6558a0a1a5fdSTejun Heo 655924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 656024b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 656124b8a847STejun Heo continue; 6562a0a1a5fdSTejun Heo /* 6563a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6564a0a1a5fdSTejun Heo * to peek without lock. 6565a0a1a5fdSTejun Heo */ 656624acfb71SThomas Gleixner rcu_read_lock(); 656724b8a847STejun Heo for_each_pwq(pwq, wq) { 65686183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6569112202d9STejun Heo if (pwq->nr_active) { 6570a0a1a5fdSTejun Heo busy = true; 657124acfb71SThomas Gleixner rcu_read_unlock(); 6572a0a1a5fdSTejun Heo goto out_unlock; 6573a0a1a5fdSTejun Heo } 6574a0a1a5fdSTejun Heo } 657524acfb71SThomas Gleixner rcu_read_unlock(); 6576a0a1a5fdSTejun Heo } 6577a0a1a5fdSTejun Heo out_unlock: 657868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6579a0a1a5fdSTejun Heo return busy; 6580a0a1a5fdSTejun Heo } 6581a0a1a5fdSTejun Heo 6582a0a1a5fdSTejun Heo /** 6583a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6584a0a1a5fdSTejun Heo * 6585a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6586706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6587a0a1a5fdSTejun Heo * 6588a0a1a5fdSTejun Heo * CONTEXT: 6589a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6590a0a1a5fdSTejun Heo */ 6591a0a1a5fdSTejun Heo void thaw_workqueues(void) 6592a0a1a5fdSTejun Heo { 659324b8a847STejun Heo struct workqueue_struct *wq; 6594a0a1a5fdSTejun Heo 659568e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6596a0a1a5fdSTejun Heo 6597a0a1a5fdSTejun Heo if (!workqueue_freezing) 6598a0a1a5fdSTejun Heo goto out_unlock; 6599a0a1a5fdSTejun Heo 660074b414eaSLai Jiangshan workqueue_freezing = false; 660124b8a847STejun Heo 660224b8a847STejun Heo /* restore max_active and repopulate worklist */ 660324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6604a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6605a045a272STejun Heo wq_adjust_max_active(wq); 6606a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 660724b8a847STejun Heo } 660824b8a847STejun Heo 6609a0a1a5fdSTejun Heo out_unlock: 661068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6611a0a1a5fdSTejun Heo } 6612a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6613a0a1a5fdSTejun Heo 661499c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6615042f7df1SLai Jiangshan { 6616042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6617042f7df1SLai Jiangshan int ret = 0; 6618042f7df1SLai Jiangshan struct workqueue_struct *wq; 6619042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6620042f7df1SLai Jiangshan 6621042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6622042f7df1SLai Jiangshan 6623042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 66248eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6625042f7df1SLai Jiangshan continue; 6626042f7df1SLai Jiangshan 662799c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 662884193c07STejun Heo if (IS_ERR(ctx)) { 662984193c07STejun Heo ret = PTR_ERR(ctx); 6630042f7df1SLai Jiangshan break; 6631042f7df1SLai Jiangshan } 6632042f7df1SLai Jiangshan 6633042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6634042f7df1SLai Jiangshan } 6635042f7df1SLai Jiangshan 6636042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6637042f7df1SLai Jiangshan if (!ret) 6638042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6639042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6640042f7df1SLai Jiangshan } 6641042f7df1SLai Jiangshan 664299c621efSLai Jiangshan if (!ret) { 664399c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 664499c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 664599c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 664699c621efSLai Jiangshan } 6647042f7df1SLai Jiangshan return ret; 6648042f7df1SLai Jiangshan } 6649042f7df1SLai Jiangshan 6650042f7df1SLai Jiangshan /** 6651fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6652fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6653fe28f631SWaiman Long * 6654fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6655fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6656fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6657fe28f631SWaiman Long */ 6658fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6659fe28f631SWaiman Long { 6660fe28f631SWaiman Long cpumask_var_t cpumask; 6661fe28f631SWaiman Long int ret = 0; 6662fe28f631SWaiman Long 6663fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6664fe28f631SWaiman Long return -ENOMEM; 6665fe28f631SWaiman Long 6666fe28f631SWaiman Long lockdep_assert_cpus_held(); 6667fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6668fe28f631SWaiman Long 6669fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6670fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6671fe28f631SWaiman Long 6672fe28f631SWaiman Long /* 6673fe28f631SWaiman Long * If the operation fails, it will fall back to 6674fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6675fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6676fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6677fe28f631SWaiman Long */ 6678fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6679fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6680fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6681fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6682fe28f631SWaiman Long 6683fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6684fe28f631SWaiman Long free_cpumask_var(cpumask); 6685fe28f631SWaiman Long return ret; 6686fe28f631SWaiman Long } 6687fe28f631SWaiman Long 668863c5484eSTejun Heo static int parse_affn_scope(const char *val) 668963c5484eSTejun Heo { 669063c5484eSTejun Heo int i; 669163c5484eSTejun Heo 669263c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 669363c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 669463c5484eSTejun Heo return i; 669563c5484eSTejun Heo } 669663c5484eSTejun Heo return -EINVAL; 669763c5484eSTejun Heo } 669863c5484eSTejun Heo 669963c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 670063c5484eSTejun Heo { 6701523a301eSTejun Heo struct workqueue_struct *wq; 6702523a301eSTejun Heo int affn, cpu; 670363c5484eSTejun Heo 670463c5484eSTejun Heo affn = parse_affn_scope(val); 670563c5484eSTejun Heo if (affn < 0) 670663c5484eSTejun Heo return affn; 6707523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6708523a301eSTejun Heo return -EINVAL; 6709523a301eSTejun Heo 6710523a301eSTejun Heo cpus_read_lock(); 6711523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 671263c5484eSTejun Heo 671363c5484eSTejun Heo wq_affn_dfl = affn; 6714523a301eSTejun Heo 6715523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6716523a301eSTejun Heo for_each_online_cpu(cpu) { 6717523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6718523a301eSTejun Heo } 6719523a301eSTejun Heo } 6720523a301eSTejun Heo 6721523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6722523a301eSTejun Heo cpus_read_unlock(); 6723523a301eSTejun Heo 672463c5484eSTejun Heo return 0; 672563c5484eSTejun Heo } 672663c5484eSTejun Heo 672763c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 672863c5484eSTejun Heo { 672963c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 673063c5484eSTejun Heo } 673163c5484eSTejun Heo 673263c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 673363c5484eSTejun Heo .set = wq_affn_dfl_set, 673463c5484eSTejun Heo .get = wq_affn_dfl_get, 673563c5484eSTejun Heo }; 673663c5484eSTejun Heo 673763c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 673863c5484eSTejun Heo 67396ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 67406ba94429SFrederic Weisbecker /* 67416ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 67426ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 67436ba94429SFrederic Weisbecker * following attributes. 67446ba94429SFrederic Weisbecker * 67456ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 67466ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 67476ba94429SFrederic Weisbecker * 67486ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 67496ba94429SFrederic Weisbecker * 67506ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 67516ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 675263c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 67538639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 67546ba94429SFrederic Weisbecker */ 67556ba94429SFrederic Weisbecker struct wq_device { 67566ba94429SFrederic Weisbecker struct workqueue_struct *wq; 67576ba94429SFrederic Weisbecker struct device dev; 67586ba94429SFrederic Weisbecker }; 67596ba94429SFrederic Weisbecker 67606ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 67616ba94429SFrederic Weisbecker { 67626ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 67636ba94429SFrederic Weisbecker 67646ba94429SFrederic Weisbecker return wq_dev->wq; 67656ba94429SFrederic Weisbecker } 67666ba94429SFrederic Weisbecker 67676ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 67686ba94429SFrederic Weisbecker char *buf) 67696ba94429SFrederic Weisbecker { 67706ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67716ba94429SFrederic Weisbecker 67726ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 67736ba94429SFrederic Weisbecker } 67746ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 67756ba94429SFrederic Weisbecker 67766ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 67776ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 67786ba94429SFrederic Weisbecker { 67796ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67806ba94429SFrederic Weisbecker 67816ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 67826ba94429SFrederic Weisbecker } 67836ba94429SFrederic Weisbecker 67846ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 67856ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 67866ba94429SFrederic Weisbecker size_t count) 67876ba94429SFrederic Weisbecker { 67886ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 67896ba94429SFrederic Weisbecker int val; 67906ba94429SFrederic Weisbecker 67916ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 67926ba94429SFrederic Weisbecker return -EINVAL; 67936ba94429SFrederic Weisbecker 67946ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 67956ba94429SFrederic Weisbecker return count; 67966ba94429SFrederic Weisbecker } 67976ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 67986ba94429SFrederic Weisbecker 67996ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 68006ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 68016ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 68026ba94429SFrederic Weisbecker NULL, 68036ba94429SFrederic Weisbecker }; 68046ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 68056ba94429SFrederic Weisbecker 680649277a5bSWaiman Long static void apply_wqattrs_lock(void) 680749277a5bSWaiman Long { 680849277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 680949277a5bSWaiman Long cpus_read_lock(); 681049277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 681149277a5bSWaiman Long } 681249277a5bSWaiman Long 681349277a5bSWaiman Long static void apply_wqattrs_unlock(void) 681449277a5bSWaiman Long { 681549277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 681649277a5bSWaiman Long cpus_read_unlock(); 681749277a5bSWaiman Long } 681849277a5bSWaiman Long 68196ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 68206ba94429SFrederic Weisbecker char *buf) 68216ba94429SFrederic Weisbecker { 68226ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68236ba94429SFrederic Weisbecker int written; 68246ba94429SFrederic Weisbecker 68256ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68266ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 68276ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68286ba94429SFrederic Weisbecker 68296ba94429SFrederic Weisbecker return written; 68306ba94429SFrederic Weisbecker } 68316ba94429SFrederic Weisbecker 68326ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 68336ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 68346ba94429SFrederic Weisbecker { 68356ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 68366ba94429SFrederic Weisbecker 6837899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6838899a94feSLai Jiangshan 6839be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 68406ba94429SFrederic Weisbecker if (!attrs) 68416ba94429SFrederic Weisbecker return NULL; 68426ba94429SFrederic Weisbecker 68436ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 68446ba94429SFrederic Weisbecker return attrs; 68456ba94429SFrederic Weisbecker } 68466ba94429SFrederic Weisbecker 68476ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 68486ba94429SFrederic Weisbecker const char *buf, size_t count) 68496ba94429SFrederic Weisbecker { 68506ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68516ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6852d4d3e257SLai Jiangshan int ret = -ENOMEM; 6853d4d3e257SLai Jiangshan 6854d4d3e257SLai Jiangshan apply_wqattrs_lock(); 68556ba94429SFrederic Weisbecker 68566ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 68576ba94429SFrederic Weisbecker if (!attrs) 6858d4d3e257SLai Jiangshan goto out_unlock; 68596ba94429SFrederic Weisbecker 68606ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 68616ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6862d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 68636ba94429SFrederic Weisbecker else 68646ba94429SFrederic Weisbecker ret = -EINVAL; 68656ba94429SFrederic Weisbecker 6866d4d3e257SLai Jiangshan out_unlock: 6867d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 68686ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 68696ba94429SFrederic Weisbecker return ret ?: count; 68706ba94429SFrederic Weisbecker } 68716ba94429SFrederic Weisbecker 68726ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 68736ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 68746ba94429SFrederic Weisbecker { 68756ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68766ba94429SFrederic Weisbecker int written; 68776ba94429SFrederic Weisbecker 68786ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 68796ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 68806ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 68816ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 68826ba94429SFrederic Weisbecker return written; 68836ba94429SFrederic Weisbecker } 68846ba94429SFrederic Weisbecker 68856ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 68866ba94429SFrederic Weisbecker struct device_attribute *attr, 68876ba94429SFrederic Weisbecker const char *buf, size_t count) 68886ba94429SFrederic Weisbecker { 68896ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68906ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6891d4d3e257SLai Jiangshan int ret = -ENOMEM; 6892d4d3e257SLai Jiangshan 6893d4d3e257SLai Jiangshan apply_wqattrs_lock(); 68946ba94429SFrederic Weisbecker 68956ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 68966ba94429SFrederic Weisbecker if (!attrs) 6897d4d3e257SLai Jiangshan goto out_unlock; 68986ba94429SFrederic Weisbecker 68996ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 69006ba94429SFrederic Weisbecker if (!ret) 6901d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 69026ba94429SFrederic Weisbecker 6903d4d3e257SLai Jiangshan out_unlock: 6904d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 69056ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 69066ba94429SFrederic Weisbecker return ret ?: count; 69076ba94429SFrederic Weisbecker } 69086ba94429SFrederic Weisbecker 690963c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 691063c5484eSTejun Heo struct device_attribute *attr, char *buf) 691163c5484eSTejun Heo { 691263c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 691363c5484eSTejun Heo int written; 691463c5484eSTejun Heo 691563c5484eSTejun Heo mutex_lock(&wq->mutex); 6916523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 6917523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 6918523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 6919523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 6920523a301eSTejun Heo else 692163c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 692263c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 692363c5484eSTejun Heo mutex_unlock(&wq->mutex); 692463c5484eSTejun Heo 692563c5484eSTejun Heo return written; 692663c5484eSTejun Heo } 692763c5484eSTejun Heo 692863c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 692963c5484eSTejun Heo struct device_attribute *attr, 693063c5484eSTejun Heo const char *buf, size_t count) 693163c5484eSTejun Heo { 693263c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 693363c5484eSTejun Heo struct workqueue_attrs *attrs; 693463c5484eSTejun Heo int affn, ret = -ENOMEM; 693563c5484eSTejun Heo 693663c5484eSTejun Heo affn = parse_affn_scope(buf); 693763c5484eSTejun Heo if (affn < 0) 693863c5484eSTejun Heo return affn; 693963c5484eSTejun Heo 694063c5484eSTejun Heo apply_wqattrs_lock(); 694163c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 694263c5484eSTejun Heo if (attrs) { 694363c5484eSTejun Heo attrs->affn_scope = affn; 694463c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 694563c5484eSTejun Heo } 694663c5484eSTejun Heo apply_wqattrs_unlock(); 694763c5484eSTejun Heo free_workqueue_attrs(attrs); 694863c5484eSTejun Heo return ret ?: count; 694963c5484eSTejun Heo } 695063c5484eSTejun Heo 69518639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 69528639ecebSTejun Heo struct device_attribute *attr, char *buf) 69538639ecebSTejun Heo { 69548639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 69558639ecebSTejun Heo 69568639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 69578639ecebSTejun Heo wq->unbound_attrs->affn_strict); 69588639ecebSTejun Heo } 69598639ecebSTejun Heo 69608639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 69618639ecebSTejun Heo struct device_attribute *attr, 69628639ecebSTejun Heo const char *buf, size_t count) 69638639ecebSTejun Heo { 69648639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 69658639ecebSTejun Heo struct workqueue_attrs *attrs; 69668639ecebSTejun Heo int v, ret = -ENOMEM; 69678639ecebSTejun Heo 69688639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 69698639ecebSTejun Heo return -EINVAL; 69708639ecebSTejun Heo 69718639ecebSTejun Heo apply_wqattrs_lock(); 69728639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 69738639ecebSTejun Heo if (attrs) { 69748639ecebSTejun Heo attrs->affn_strict = (bool)v; 69758639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 69768639ecebSTejun Heo } 69778639ecebSTejun Heo apply_wqattrs_unlock(); 69788639ecebSTejun Heo free_workqueue_attrs(attrs); 69798639ecebSTejun Heo return ret ?: count; 69808639ecebSTejun Heo } 69818639ecebSTejun Heo 69826ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 69836ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 69846ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 698563c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 69868639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 69876ba94429SFrederic Weisbecker __ATTR_NULL, 69886ba94429SFrederic Weisbecker }; 69896ba94429SFrederic Weisbecker 69904f19b8e0STejun Heo static struct bus_type wq_subsys = { 69916ba94429SFrederic Weisbecker .name = "workqueue", 69926ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 69936ba94429SFrederic Weisbecker }; 69946ba94429SFrederic Weisbecker 699549277a5bSWaiman Long /** 699649277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 699749277a5bSWaiman Long * @cpumask: the cpumask to set 699849277a5bSWaiman Long * 699949277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 700049277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 700149277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 700249277a5bSWaiman Long * 700349277a5bSWaiman Long * Return: 0 - Success 700449277a5bSWaiman Long * -EINVAL - Invalid @cpumask 700549277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 700649277a5bSWaiman Long */ 700749277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 700849277a5bSWaiman Long { 700949277a5bSWaiman Long int ret = -EINVAL; 701049277a5bSWaiman Long 701149277a5bSWaiman Long /* 701249277a5bSWaiman Long * Not excluding isolated cpus on purpose. 701349277a5bSWaiman Long * If the user wishes to include them, we allow that. 701449277a5bSWaiman Long */ 701549277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 701649277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 701749277a5bSWaiman Long apply_wqattrs_lock(); 701849277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 701949277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 702049277a5bSWaiman Long ret = 0; 702149277a5bSWaiman Long goto out_unlock; 702249277a5bSWaiman Long } 702349277a5bSWaiman Long 702449277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 702549277a5bSWaiman Long 702649277a5bSWaiman Long out_unlock: 702749277a5bSWaiman Long apply_wqattrs_unlock(); 702849277a5bSWaiman Long } 702949277a5bSWaiman Long 703049277a5bSWaiman Long return ret; 703149277a5bSWaiman Long } 703249277a5bSWaiman Long 7033fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 7034fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 7035b05a7928SFrederic Weisbecker { 7036b05a7928SFrederic Weisbecker int written; 7037b05a7928SFrederic Weisbecker 7038042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 7039fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 7040042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 7041b05a7928SFrederic Weisbecker 7042b05a7928SFrederic Weisbecker return written; 7043b05a7928SFrederic Weisbecker } 7044b05a7928SFrederic Weisbecker 7045fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 7046fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7047fe28f631SWaiman Long { 7048fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 7049fe28f631SWaiman Long } 7050fe28f631SWaiman Long 7051fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 7052fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7053fe28f631SWaiman Long { 7054fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 7055fe28f631SWaiman Long } 7056fe28f631SWaiman Long 7057fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 7058fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7059fe28f631SWaiman Long { 7060fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 7061fe28f631SWaiman Long } 7062fe28f631SWaiman Long 7063042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 7064042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 7065042f7df1SLai Jiangshan { 7066042f7df1SLai Jiangshan cpumask_var_t cpumask; 7067042f7df1SLai Jiangshan int ret; 7068042f7df1SLai Jiangshan 7069042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 7070042f7df1SLai Jiangshan return -ENOMEM; 7071042f7df1SLai Jiangshan 7072042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7073042f7df1SLai Jiangshan if (!ret) 7074042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7075042f7df1SLai Jiangshan 7076042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7077042f7df1SLai Jiangshan return ret ? ret : count; 7078042f7df1SLai Jiangshan } 7079042f7df1SLai Jiangshan 7080fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7081042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7082fe28f631SWaiman Long wq_unbound_cpumask_store), 7083fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7084fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7085fe28f631SWaiman Long __ATTR_NULL, 7086fe28f631SWaiman Long }; 7087b05a7928SFrederic Weisbecker 70886ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 70896ba94429SFrederic Weisbecker { 7090686f6697SGreg Kroah-Hartman struct device *dev_root; 7091b05a7928SFrederic Weisbecker int err; 7092b05a7928SFrederic Weisbecker 7093b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7094b05a7928SFrederic Weisbecker if (err) 7095b05a7928SFrederic Weisbecker return err; 7096b05a7928SFrederic Weisbecker 7097686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7098686f6697SGreg Kroah-Hartman if (dev_root) { 7099fe28f631SWaiman Long struct device_attribute *attr; 7100fe28f631SWaiman Long 7101fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7102fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7103fe28f631SWaiman Long if (err) 7104fe28f631SWaiman Long break; 7105fe28f631SWaiman Long } 7106686f6697SGreg Kroah-Hartman put_device(dev_root); 7107686f6697SGreg Kroah-Hartman } 7108686f6697SGreg Kroah-Hartman return err; 71096ba94429SFrederic Weisbecker } 71106ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 71116ba94429SFrederic Weisbecker 71126ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 71136ba94429SFrederic Weisbecker { 71146ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 71156ba94429SFrederic Weisbecker 71166ba94429SFrederic Weisbecker kfree(wq_dev); 71176ba94429SFrederic Weisbecker } 71186ba94429SFrederic Weisbecker 71196ba94429SFrederic Weisbecker /** 71206ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 71216ba94429SFrederic Weisbecker * @wq: the workqueue to register 71226ba94429SFrederic Weisbecker * 71236ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 71246ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 71256ba94429SFrederic Weisbecker * which is the preferred method. 71266ba94429SFrederic Weisbecker * 71276ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 71286ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 71296ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 71306ba94429SFrederic Weisbecker * attributes. 71316ba94429SFrederic Weisbecker * 71326ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 71336ba94429SFrederic Weisbecker */ 71346ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 71356ba94429SFrederic Weisbecker { 71366ba94429SFrederic Weisbecker struct wq_device *wq_dev; 71376ba94429SFrederic Weisbecker int ret; 71386ba94429SFrederic Weisbecker 71396ba94429SFrederic Weisbecker /* 71404c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 71414c065dbcSWaiman Long * ordered workqueues. 71426ba94429SFrederic Weisbecker */ 71433bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 71446ba94429SFrederic Weisbecker return -EINVAL; 71456ba94429SFrederic Weisbecker 71466ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 71476ba94429SFrederic Weisbecker if (!wq_dev) 71486ba94429SFrederic Weisbecker return -ENOMEM; 71496ba94429SFrederic Weisbecker 71506ba94429SFrederic Weisbecker wq_dev->wq = wq; 71516ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 71526ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 715323217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 71546ba94429SFrederic Weisbecker 71556ba94429SFrederic Weisbecker /* 71566ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 71576ba94429SFrederic Weisbecker * everything is ready. 71586ba94429SFrederic Weisbecker */ 71596ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 71606ba94429SFrederic Weisbecker 71616ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 71626ba94429SFrederic Weisbecker if (ret) { 7163537f4146SArvind Yadav put_device(&wq_dev->dev); 71646ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71656ba94429SFrederic Weisbecker return ret; 71666ba94429SFrederic Weisbecker } 71676ba94429SFrederic Weisbecker 71686ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 71696ba94429SFrederic Weisbecker struct device_attribute *attr; 71706ba94429SFrederic Weisbecker 71716ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 71726ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 71736ba94429SFrederic Weisbecker if (ret) { 71746ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 71756ba94429SFrederic Weisbecker wq->wq_dev = NULL; 71766ba94429SFrederic Weisbecker return ret; 71776ba94429SFrederic Weisbecker } 71786ba94429SFrederic Weisbecker } 71796ba94429SFrederic Weisbecker } 71806ba94429SFrederic Weisbecker 71816ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 71826ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 71836ba94429SFrederic Weisbecker return 0; 71846ba94429SFrederic Weisbecker } 71856ba94429SFrederic Weisbecker 71866ba94429SFrederic Weisbecker /** 71876ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 71886ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 71896ba94429SFrederic Weisbecker * 71906ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 71916ba94429SFrederic Weisbecker */ 71926ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 71936ba94429SFrederic Weisbecker { 71946ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 71956ba94429SFrederic Weisbecker 71966ba94429SFrederic Weisbecker if (!wq->wq_dev) 71976ba94429SFrederic Weisbecker return; 71986ba94429SFrederic Weisbecker 71996ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72006ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 72016ba94429SFrederic Weisbecker } 72026ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 72036ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 72046ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 72056ba94429SFrederic Weisbecker 720682607adcSTejun Heo /* 720782607adcSTejun Heo * Workqueue watchdog. 720882607adcSTejun Heo * 720982607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 721082607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 721182607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 721282607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 721382607adcSTejun Heo * largely opaque. 721482607adcSTejun Heo * 721582607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 721682607adcSTejun Heo * state if some pools failed to make forward progress for a while where 721782607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 721882607adcSTejun Heo * 721982607adcSTejun Heo * This mechanism is controlled through the kernel parameter 722082607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 722182607adcSTejun Heo * corresponding sysfs parameter file. 722282607adcSTejun Heo */ 722382607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 722482607adcSTejun Heo 722582607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 72265cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 722782607adcSTejun Heo 722882607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 722982607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 723082607adcSTejun Heo 7231cd2440d6SPetr Mladek /* 7232cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7233cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7234cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7235cd2440d6SPetr Mladek * in all other situations. 7236cd2440d6SPetr Mladek */ 7237cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7238cd2440d6SPetr Mladek { 7239cd2440d6SPetr Mladek struct worker *worker; 7240c26e2f2eSTejun Heo unsigned long irq_flags; 7241cd2440d6SPetr Mladek int bkt; 7242cd2440d6SPetr Mladek 7243c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 7244cd2440d6SPetr Mladek 7245cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7246cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7247cd2440d6SPetr Mladek /* 7248cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7249cd2440d6SPetr Mladek * drivers that queue work while holding locks 7250cd2440d6SPetr Mladek * also taken in their write paths. 7251cd2440d6SPetr Mladek */ 7252cd2440d6SPetr Mladek printk_deferred_enter(); 7253cd2440d6SPetr Mladek 7254cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7255cd2440d6SPetr Mladek sched_show_task(worker->task); 7256cd2440d6SPetr Mladek 7257cd2440d6SPetr Mladek printk_deferred_exit(); 7258cd2440d6SPetr Mladek } 7259cd2440d6SPetr Mladek } 7260cd2440d6SPetr Mladek 7261c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 7262cd2440d6SPetr Mladek } 7263cd2440d6SPetr Mladek 7264cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7265cd2440d6SPetr Mladek { 7266cd2440d6SPetr Mladek struct worker_pool *pool; 7267cd2440d6SPetr Mladek int pi; 7268cd2440d6SPetr Mladek 7269cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7270cd2440d6SPetr Mladek 7271cd2440d6SPetr Mladek rcu_read_lock(); 7272cd2440d6SPetr Mladek 7273cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7274cd2440d6SPetr Mladek if (pool->cpu_stall) 7275cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7276cd2440d6SPetr Mladek 7277cd2440d6SPetr Mladek } 7278cd2440d6SPetr Mladek 7279cd2440d6SPetr Mladek rcu_read_unlock(); 7280cd2440d6SPetr Mladek } 7281cd2440d6SPetr Mladek 728282607adcSTejun Heo static void wq_watchdog_reset_touched(void) 728382607adcSTejun Heo { 728482607adcSTejun Heo int cpu; 728582607adcSTejun Heo 728682607adcSTejun Heo wq_watchdog_touched = jiffies; 728782607adcSTejun Heo for_each_possible_cpu(cpu) 728882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 728982607adcSTejun Heo } 729082607adcSTejun Heo 72915cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 729282607adcSTejun Heo { 729382607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 729482607adcSTejun Heo bool lockup_detected = false; 7295cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7296940d71c6SSergey Senozhatsky unsigned long now = jiffies; 729782607adcSTejun Heo struct worker_pool *pool; 729882607adcSTejun Heo int pi; 729982607adcSTejun Heo 730082607adcSTejun Heo if (!thresh) 730182607adcSTejun Heo return; 730282607adcSTejun Heo 730382607adcSTejun Heo rcu_read_lock(); 730482607adcSTejun Heo 730582607adcSTejun Heo for_each_pool(pool, pi) { 730682607adcSTejun Heo unsigned long pool_ts, touched, ts; 730782607adcSTejun Heo 7308cd2440d6SPetr Mladek pool->cpu_stall = false; 730982607adcSTejun Heo if (list_empty(&pool->worklist)) 731082607adcSTejun Heo continue; 731182607adcSTejun Heo 7312940d71c6SSergey Senozhatsky /* 7313940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7314940d71c6SSergey Senozhatsky * the watchdog like a stall. 7315940d71c6SSergey Senozhatsky */ 7316940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7317940d71c6SSergey Senozhatsky 731882607adcSTejun Heo /* get the latest of pool and touched timestamps */ 731989e28ce6SWang Qing if (pool->cpu >= 0) 732089e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 732189e28ce6SWang Qing else 732282607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 732389e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 732482607adcSTejun Heo 732582607adcSTejun Heo if (time_after(pool_ts, touched)) 732682607adcSTejun Heo ts = pool_ts; 732782607adcSTejun Heo else 732882607adcSTejun Heo ts = touched; 732982607adcSTejun Heo 733082607adcSTejun Heo /* did we stall? */ 7331940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 733282607adcSTejun Heo lockup_detected = true; 73334cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7334cd2440d6SPetr Mladek pool->cpu_stall = true; 7335cd2440d6SPetr Mladek cpu_pool_stall = true; 7336cd2440d6SPetr Mladek } 733782607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 733882607adcSTejun Heo pr_cont_pool_info(pool); 733982607adcSTejun Heo pr_cont(" stuck for %us!\n", 7340940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 734182607adcSTejun Heo } 7342cd2440d6SPetr Mladek 7343cd2440d6SPetr Mladek 734482607adcSTejun Heo } 734582607adcSTejun Heo 734682607adcSTejun Heo rcu_read_unlock(); 734782607adcSTejun Heo 734882607adcSTejun Heo if (lockup_detected) 734955df0933SImran Khan show_all_workqueues(); 735082607adcSTejun Heo 7351cd2440d6SPetr Mladek if (cpu_pool_stall) 7352cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7353cd2440d6SPetr Mladek 735482607adcSTejun Heo wq_watchdog_reset_touched(); 735582607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 735682607adcSTejun Heo } 735782607adcSTejun Heo 7358cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 735982607adcSTejun Heo { 736082607adcSTejun Heo if (cpu >= 0) 736182607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 736289e28ce6SWang Qing 736382607adcSTejun Heo wq_watchdog_touched = jiffies; 736482607adcSTejun Heo } 736582607adcSTejun Heo 736682607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 736782607adcSTejun Heo { 736882607adcSTejun Heo wq_watchdog_thresh = 0; 736982607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 737082607adcSTejun Heo 737182607adcSTejun Heo if (thresh) { 737282607adcSTejun Heo wq_watchdog_thresh = thresh; 737382607adcSTejun Heo wq_watchdog_reset_touched(); 737482607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 737582607adcSTejun Heo } 737682607adcSTejun Heo } 737782607adcSTejun Heo 737882607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 737982607adcSTejun Heo const struct kernel_param *kp) 738082607adcSTejun Heo { 738182607adcSTejun Heo unsigned long thresh; 738282607adcSTejun Heo int ret; 738382607adcSTejun Heo 738482607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 738582607adcSTejun Heo if (ret) 738682607adcSTejun Heo return ret; 738782607adcSTejun Heo 738882607adcSTejun Heo if (system_wq) 738982607adcSTejun Heo wq_watchdog_set_thresh(thresh); 739082607adcSTejun Heo else 739182607adcSTejun Heo wq_watchdog_thresh = thresh; 739282607adcSTejun Heo 739382607adcSTejun Heo return 0; 739482607adcSTejun Heo } 739582607adcSTejun Heo 739682607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 739782607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 739882607adcSTejun Heo .get = param_get_ulong, 739982607adcSTejun Heo }; 740082607adcSTejun Heo 740182607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 740282607adcSTejun Heo 0644); 740382607adcSTejun Heo 740482607adcSTejun Heo static void wq_watchdog_init(void) 740582607adcSTejun Heo { 74065cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 740782607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 740882607adcSTejun Heo } 740982607adcSTejun Heo 741082607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 741182607adcSTejun Heo 741282607adcSTejun Heo static inline void wq_watchdog_init(void) { } 741382607adcSTejun Heo 741482607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 741582607adcSTejun Heo 74162f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work) 74172f34d733STejun Heo { 74182f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 74192f34d733STejun Heo } 74202f34d733STejun Heo 74212f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work) 74222f34d733STejun Heo { 74232f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 74242f34d733STejun Heo } 74252f34d733STejun Heo 74264a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 74274a6c5607STejun Heo { 74284a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 74294a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 74304a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 74314a6c5607STejun Heo return; 74324a6c5607STejun Heo } 74334a6c5607STejun Heo 74344a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 74354a6c5607STejun Heo } 74364a6c5607STejun Heo 74372fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 74382fcdb1b4STejun Heo { 74392fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 74402fcdb1b4STejun Heo pool->cpu = cpu; 74412fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 74422fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 74432fcdb1b4STejun Heo pool->attrs->nice = nice; 74442fcdb1b4STejun Heo pool->attrs->affn_strict = true; 74452fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 74462fcdb1b4STejun Heo 74472fcdb1b4STejun Heo /* alloc pool ID */ 74482fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 74492fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 74502fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 74512fcdb1b4STejun Heo } 74522fcdb1b4STejun Heo 74533347fa09STejun Heo /** 74543347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 74553347fa09STejun Heo * 74562930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 74572930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 74582930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 74592930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 74602930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 74612930155bSTejun Heo * before early initcalls. 74623347fa09STejun Heo */ 74632333e829SYu Chen void __init workqueue_init_early(void) 74641da177e4SLinus Torvalds { 746584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 74667a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 74672f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal, 74682f34d733STejun Heo bh_pool_kick_highpri }; 74697a4e344cSTejun Heo int i, cpu; 7470c34056a3STejun Heo 747110cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7472e904e6c2STejun Heo 7473b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7474fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7475fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7476b05a7928SFrederic Weisbecker 74774a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 74784a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 74794a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7480ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 74814a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7482ace3c549Stiozhang 7483fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 74848b03ae3cSTejun Heo 74858b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7486e22bee78STejun Heo 74872930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 74882930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 74892930155bSTejun Heo 74907bd20b6bSMarcelo Tosatti /* 74917bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 74927bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 74937bd20b6bSMarcelo Tosatti */ 74947bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 74957bd20b6bSMarcelo Tosatti wq_power_efficient = true; 74967bd20b6bSMarcelo Tosatti 749784193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 749884193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 749984193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 750084193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 750184193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 750284193c07STejun Heo 750384193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 750484193c07STejun Heo 750584193c07STejun Heo pt->nr_pods = 1; 750684193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 750784193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 750884193c07STejun Heo pt->cpu_pod[0] = 0; 750984193c07STejun Heo 75104cb1ef64STejun Heo /* initialize BH and CPU pools */ 75111da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 75121da177e4SLinus Torvalds struct worker_pool *pool; 75131da177e4SLinus Torvalds 75141da177e4SLinus Torvalds i = 0; 75154cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 75162f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]); 75174cb1ef64STejun Heo pool->flags |= POOL_BH; 75182f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]); 75192f34d733STejun Heo i++; 75204cb1ef64STejun Heo } 75214cb1ef64STejun Heo 75224cb1ef64STejun Heo i = 0; 75232fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 75242fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 75251da177e4SLinus Torvalds } 75261da177e4SLinus Torvalds 75278a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 752829c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 752929c91e99STejun Heo struct workqueue_attrs *attrs; 753029c91e99STejun Heo 7531be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 753229c91e99STejun Heo attrs->nice = std_nice[i]; 753329c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 75348a2b7538STejun Heo 75358a2b7538STejun Heo /* 75368a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 75378a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 75388a2b7538STejun Heo */ 7539be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 75408a2b7538STejun Heo attrs->nice = std_nice[i]; 7541af73f5c9STejun Heo attrs->ordered = true; 75428a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 754329c91e99STejun Heo } 754429c91e99STejun Heo 7545d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 75461aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7547d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7548f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7549636b927eSTejun Heo WQ_MAX_ACTIVE); 755024d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 755124d51addSTejun Heo WQ_FREEZABLE, 0); 75520668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 75530668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 75548318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 75550668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 75560668106cSViresh Kumar 0); 75574cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 75584cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 75594cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 75601aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 75610668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 75620668106cSViresh Kumar !system_power_efficient_wq || 75634cb1ef64STejun Heo !system_freezable_power_efficient_wq || 75644cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 75653347fa09STejun Heo } 75663347fa09STejun Heo 7567aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7568aa6fde93STejun Heo { 7569aa6fde93STejun Heo unsigned long thresh; 7570aa6fde93STejun Heo unsigned long bogo; 7571aa6fde93STejun Heo 7572dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7573dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7574dd64c873SZqiang 7575aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7576aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7577aa6fde93STejun Heo return; 7578aa6fde93STejun Heo 7579aa6fde93STejun Heo /* 7580aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7581aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7582aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7583aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7584aa6fde93STejun Heo * too low. 7585aa6fde93STejun Heo * 7586aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7587aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7588aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7589aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7590aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7591aa6fde93STejun Heo * usefulness. 7592aa6fde93STejun Heo */ 7593aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7594aa6fde93STejun Heo 7595aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7596aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7597aa6fde93STejun Heo if (bogo < 4000) 7598aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7599aa6fde93STejun Heo 7600aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7601aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7602aa6fde93STejun Heo 7603aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7604aa6fde93STejun Heo } 7605aa6fde93STejun Heo 76063347fa09STejun Heo /** 76073347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 76083347fa09STejun Heo * 76092930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 76102930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 76112930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 76122930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 76132930155bSTejun Heo * workers and enable future kworker creations. 76143347fa09STejun Heo */ 76152333e829SYu Chen void __init workqueue_init(void) 76163347fa09STejun Heo { 76172186d9f9STejun Heo struct workqueue_struct *wq; 76183347fa09STejun Heo struct worker_pool *pool; 76193347fa09STejun Heo int cpu, bkt; 76203347fa09STejun Heo 7621aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7622aa6fde93STejun Heo 76232186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 76242186d9f9STejun Heo 76252930155bSTejun Heo /* 76262930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 76272930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 76282930155bSTejun Heo */ 76292186d9f9STejun Heo for_each_possible_cpu(cpu) { 76304cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 76312186d9f9STejun Heo pool->node = cpu_to_node(cpu); 76324cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 76334cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 76342186d9f9STejun Heo } 76352186d9f9STejun Heo 763640c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 763740c17f75STejun Heo WARN(init_rescuer(wq), 763840c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 763940c17f75STejun Heo wq->name); 764040c17f75STejun Heo } 76412186d9f9STejun Heo 76422186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 76432186d9f9STejun Heo 76444cb1ef64STejun Heo /* 76454cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 76464cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 76474cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 76484cb1ef64STejun Heo * possible CPUs here. 76494cb1ef64STejun Heo */ 76504cb1ef64STejun Heo for_each_possible_cpu(cpu) 76514cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 76524cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 76534cb1ef64STejun Heo 76543347fa09STejun Heo for_each_online_cpu(cpu) { 76553347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 76563347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 76573347fa09STejun Heo BUG_ON(!create_worker(pool)); 76583347fa09STejun Heo } 76593347fa09STejun Heo } 76603347fa09STejun Heo 76613347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 76623347fa09STejun Heo BUG_ON(!create_worker(pool)); 76633347fa09STejun Heo 76643347fa09STejun Heo wq_online = true; 766582607adcSTejun Heo wq_watchdog_init(); 76661da177e4SLinus Torvalds } 7667c4f135d6STetsuo Handa 7668025e1684STejun Heo /* 7669025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7670025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7671025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7672025e1684STejun Heo */ 7673025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7674025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7675025e1684STejun Heo { 7676025e1684STejun Heo int cur, pre, cpu, pod; 7677025e1684STejun Heo 7678025e1684STejun Heo pt->nr_pods = 0; 7679025e1684STejun Heo 7680025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7681025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7682025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7683025e1684STejun Heo 7684025e1684STejun Heo for_each_possible_cpu(cur) { 7685025e1684STejun Heo for_each_possible_cpu(pre) { 7686025e1684STejun Heo if (pre >= cur) { 7687025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7688025e1684STejun Heo break; 7689025e1684STejun Heo } 7690025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7691025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7692025e1684STejun Heo break; 7693025e1684STejun Heo } 7694025e1684STejun Heo } 7695025e1684STejun Heo } 7696025e1684STejun Heo 7697025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7698025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7699025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7700025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7701025e1684STejun Heo 7702025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7703025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7704025e1684STejun Heo 7705025e1684STejun Heo for_each_possible_cpu(cpu) { 7706025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7707025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7708025e1684STejun Heo } 7709025e1684STejun Heo } 7710025e1684STejun Heo 771163c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 771263c5484eSTejun Heo { 771363c5484eSTejun Heo return false; 771463c5484eSTejun Heo } 771563c5484eSTejun Heo 771663c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 771763c5484eSTejun Heo { 771863c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 771963c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 772063c5484eSTejun Heo #else 772163c5484eSTejun Heo return false; 772263c5484eSTejun Heo #endif 772363c5484eSTejun Heo } 772463c5484eSTejun Heo 7725025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7726025e1684STejun Heo { 7727025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7728025e1684STejun Heo } 7729025e1684STejun Heo 77302930155bSTejun Heo /** 77312930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 77322930155bSTejun Heo * 773396068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 77342930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 77352930155bSTejun Heo * initializes the unbound CPU pods accordingly. 77362930155bSTejun Heo */ 77372930155bSTejun Heo void __init workqueue_init_topology(void) 7738a86feae6STejun Heo { 77392930155bSTejun Heo struct workqueue_struct *wq; 7740025e1684STejun Heo int cpu; 7741a86feae6STejun Heo 774263c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 774363c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 774463c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7745025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7746a86feae6STejun Heo 7747c5f8cd6cSTejun Heo wq_topo_initialized = true; 7748c5f8cd6cSTejun Heo 77492930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7750a86feae6STejun Heo 7751a86feae6STejun Heo /* 77522930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 77532930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 77542930155bSTejun Heo * combinations to apply per-pod sharing. 77552930155bSTejun Heo */ 77562930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 77575797b1c1STejun Heo for_each_online_cpu(cpu) 77582930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 77595797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 77605797b1c1STejun Heo mutex_lock(&wq->mutex); 77615797b1c1STejun Heo wq_update_node_max_active(wq, -1); 77625797b1c1STejun Heo mutex_unlock(&wq->mutex); 77632930155bSTejun Heo } 77642930155bSTejun Heo } 77652930155bSTejun Heo 77662930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7767a86feae6STejun Heo } 7768a86feae6STejun Heo 776920bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 777020bdedafSTetsuo Handa { 777120bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 777220bdedafSTetsuo Handa dump_stack(); 777320bdedafSTetsuo Handa } 7774c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7775ace3c549Stiozhang 7776ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7777ace3c549Stiozhang { 7778ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7779ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7780ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7781ace3c549Stiozhang } 7782ace3c549Stiozhang 7783ace3c549Stiozhang return 1; 7784ace3c549Stiozhang } 7785ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7786