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 */ 841acd92d9STejun Heo POOL_BH_DRAINING = 1 << 3, /* draining after CPU offline */ 85e563d0a7STejun Heo }; 86db7bccf4STejun Heo 87e563d0a7STejun Heo enum worker_flags { 88c8e55f36STejun Heo /* worker flags */ 89c8e55f36STejun Heo WORKER_DIE = 1 << 1, /* die die die */ 90c8e55f36STejun Heo WORKER_IDLE = 1 << 2, /* is idle */ 91e22bee78STejun Heo WORKER_PREP = 1 << 3, /* preparing to run works */ 92fb0e7bebSTejun Heo WORKER_CPU_INTENSIVE = 1 << 6, /* cpu intensive */ 93f3421797STejun Heo WORKER_UNBOUND = 1 << 7, /* worker is unbound */ 94a9ab775bSTejun Heo WORKER_REBOUND = 1 << 8, /* worker was rebound */ 95e22bee78STejun Heo 96a9ab775bSTejun Heo WORKER_NOT_RUNNING = WORKER_PREP | WORKER_CPU_INTENSIVE | 97a9ab775bSTejun Heo WORKER_UNBOUND | WORKER_REBOUND, 98e563d0a7STejun Heo }; 99db7bccf4STejun Heo 100c5f5b942STejun Heo enum work_cancel_flags { 101c5f5b942STejun Heo WORK_CANCEL_DELAYED = 1 << 0, /* canceling a delayed_work */ 102c5f5b942STejun Heo }; 103c5f5b942STejun Heo 104e563d0a7STejun Heo enum wq_internal_consts { 105e34cdddbSTejun Heo NR_STD_WORKER_POOLS = 2, /* # standard pools per cpu */ 1064ce62e9eSTejun Heo 10729c91e99STejun Heo UNBOUND_POOL_HASH_ORDER = 6, /* hashed by pool->attrs */ 108c8e55f36STejun Heo BUSY_WORKER_HASH_ORDER = 6, /* 64 pointers */ 109db7bccf4STejun Heo 110e22bee78STejun Heo MAX_IDLE_WORKERS_RATIO = 4, /* 1/4 of busy can be idle */ 111e22bee78STejun Heo IDLE_WORKER_TIMEOUT = 300 * HZ, /* keep idle ones for 5 mins */ 112e22bee78STejun Heo 1133233cdbdSTejun Heo MAYDAY_INITIAL_TIMEOUT = HZ / 100 >= 2 ? HZ / 100 : 2, 1143233cdbdSTejun Heo /* call for help after 10ms 1153233cdbdSTejun Heo (min two ticks) */ 116e22bee78STejun Heo MAYDAY_INTERVAL = HZ / 10, /* and then every 100ms */ 117e22bee78STejun Heo CREATE_COOLDOWN = HZ, /* time to breath after fail */ 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* 120e22bee78STejun Heo * Rescue workers are used only on emergencies and shared by 1218698a745SDongsheng Yang * all cpus. Give MIN_NICE. 122e22bee78STejun Heo */ 1238698a745SDongsheng Yang RESCUER_NICE_LEVEL = MIN_NICE, 1248698a745SDongsheng Yang HIGHPRI_NICE_LEVEL = MIN_NICE, 125ecf6881fSTejun Heo 12631c89007SAudra Mitchell WQ_NAME_LEN = 32, 127c8e55f36STejun Heo }; 128c8e55f36STejun Heo 1291da177e4SLinus Torvalds /* 1304cb1ef64STejun Heo * We don't want to trap softirq for too long. See MAX_SOFTIRQ_TIME and 1314cb1ef64STejun Heo * MAX_SOFTIRQ_RESTART in kernel/softirq.c. These are macros because 1324cb1ef64STejun Heo * msecs_to_jiffies() can't be an initializer. 1334cb1ef64STejun Heo */ 1344cb1ef64STejun Heo #define BH_WORKER_JIFFIES msecs_to_jiffies(2) 1354cb1ef64STejun Heo #define BH_WORKER_RESTARTS 10 1364cb1ef64STejun Heo 1374cb1ef64STejun Heo /* 1384690c4abSTejun Heo * Structure fields follow one of the following exclusion rules. 1394690c4abSTejun Heo * 140e41e704bSTejun Heo * I: Modifiable by initialization/destruction paths and read-only for 141e41e704bSTejun Heo * everyone else. 1424690c4abSTejun Heo * 143e22bee78STejun Heo * P: Preemption protected. Disabling preemption is enough and should 144e22bee78STejun Heo * only be modified and accessed from the local cpu. 145e22bee78STejun Heo * 146d565ed63STejun Heo * L: pool->lock protected. Access with pool->lock held. 1474690c4abSTejun Heo * 1485797b1c1STejun Heo * LN: pool->lock and wq_node_nr_active->lock protected for writes. Either for 1495797b1c1STejun Heo * reads. 1505797b1c1STejun Heo * 151bdf8b9bfSTejun Heo * K: Only modified by worker while holding pool->lock. Can be safely read by 152bdf8b9bfSTejun Heo * self, while holding pool->lock or from IRQ context if %current is the 153bdf8b9bfSTejun Heo * kworker. 154bdf8b9bfSTejun Heo * 155bdf8b9bfSTejun Heo * S: Only modified by worker self. 156bdf8b9bfSTejun Heo * 1571258fae7STejun Heo * A: wq_pool_attach_mutex protected. 158822d8405STejun Heo * 15968e13a67SLai Jiangshan * PL: wq_pool_mutex protected. 16076af4d93STejun Heo * 16124acfb71SThomas Gleixner * PR: wq_pool_mutex protected for writes. RCU protected for reads. 1625bcab335STejun Heo * 1635b95e1afSLai Jiangshan * PW: wq_pool_mutex and wq->mutex protected for writes. Either for reads. 1645b95e1afSLai Jiangshan * 1655b95e1afSLai Jiangshan * PWR: wq_pool_mutex and wq->mutex protected for writes. Either or 16624acfb71SThomas Gleixner * RCU for reads. 1675b95e1afSLai Jiangshan * 1683c25a55dSLai Jiangshan * WQ: wq->mutex protected. 1693c25a55dSLai Jiangshan * 17024acfb71SThomas Gleixner * WR: wq->mutex protected for writes. RCU protected for reads. 1712e109a28STejun Heo * 172a045a272STejun Heo * WO: wq->mutex protected for writes. Updated with WRITE_ONCE() and can be read 173a045a272STejun Heo * with READ_ONCE() without locking. 174a045a272STejun Heo * 1752e109a28STejun Heo * MD: wq_mayday_lock protected. 176cd2440d6SPetr Mladek * 177cd2440d6SPetr Mladek * WD: Used internally by the watchdog. 1784690c4abSTejun Heo */ 1794690c4abSTejun Heo 1802eaebdb3STejun Heo /* struct worker is defined in workqueue_internal.h */ 181c34056a3STejun Heo 182bd7bdd43STejun Heo struct worker_pool { 183a9b8a985SSebastian Andrzej Siewior raw_spinlock_t lock; /* the pool lock */ 184d84ff051STejun Heo int cpu; /* I: the associated cpu */ 185f3f90ad4STejun Heo int node; /* I: the associated node ID */ 1869daf9e67STejun Heo int id; /* I: pool ID */ 187bc8b50c2STejun Heo unsigned int flags; /* L: flags */ 188bd7bdd43STejun Heo 18982607adcSTejun Heo unsigned long watchdog_ts; /* L: watchdog timestamp */ 190cd2440d6SPetr Mladek bool cpu_stall; /* WD: stalled cpu bound pool */ 19182607adcSTejun Heo 192bc35f7efSLai Jiangshan /* 193bc35f7efSLai Jiangshan * The counter is incremented in a process context on the associated CPU 194bc35f7efSLai Jiangshan * w/ preemption disabled, and decremented or reset in the same context 195bc35f7efSLai Jiangshan * but w/ pool->lock held. The readers grab pool->lock and are 196bc35f7efSLai Jiangshan * guaranteed to see if the counter reached zero. 197bc35f7efSLai Jiangshan */ 198bc35f7efSLai Jiangshan int nr_running; 19984f91c62SLai Jiangshan 200bd7bdd43STejun Heo struct list_head worklist; /* L: list of pending works */ 201ea1abd61SLai Jiangshan 2025826cc8fSLai Jiangshan int nr_workers; /* L: total number of workers */ 2035826cc8fSLai Jiangshan int nr_idle; /* L: currently idle workers */ 204bd7bdd43STejun Heo 2052c1f1a91SLai Jiangshan struct list_head idle_list; /* L: list of idle workers */ 206bd7bdd43STejun Heo struct timer_list idle_timer; /* L: worker idle timeout */ 2073f959aa3SValentin Schneider struct work_struct idle_cull_work; /* L: worker idle cleanup */ 2083f959aa3SValentin Schneider 209bd7bdd43STejun Heo struct timer_list mayday_timer; /* L: SOS timer for workers */ 210bd7bdd43STejun Heo 211c5aa87bbSTejun Heo /* a workers is either on busy_hash or idle_list, or the manager */ 212c9e7cf27STejun Heo DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); 213c9e7cf27STejun Heo /* L: hash of busy workers */ 214c9e7cf27STejun Heo 2152607d7a6STejun Heo struct worker *manager; /* L: purely informational */ 21692f9c5c4SLai Jiangshan struct list_head workers; /* A: attached workers */ 217e02b9312SValentin Schneider struct list_head dying_workers; /* A: workers about to die */ 21860f5a4bcSLai Jiangshan struct completion *detach_completion; /* all workers detached */ 219e19e397aSTejun Heo 2207cda9aaeSLai Jiangshan struct ida worker_ida; /* worker IDs for task name */ 221e19e397aSTejun Heo 2227a4e344cSTejun Heo struct workqueue_attrs *attrs; /* I: worker attributes */ 22368e13a67SLai Jiangshan struct hlist_node hash_node; /* PL: unbound_pool_hash node */ 22468e13a67SLai Jiangshan int refcnt; /* PL: refcnt for unbound pools */ 2257a4e344cSTejun Heo 226e19e397aSTejun Heo /* 22724acfb71SThomas Gleixner * Destruction of pool is RCU protected to allow dereferences 22829c91e99STejun Heo * from get_work_pool(). 22929c91e99STejun Heo */ 23029c91e99STejun Heo struct rcu_head rcu; 23184f91c62SLai Jiangshan }; 2328b03ae3cSTejun Heo 2338b03ae3cSTejun Heo /* 234725e8ec5STejun Heo * Per-pool_workqueue statistics. These can be monitored using 235725e8ec5STejun Heo * tools/workqueue/wq_monitor.py. 236725e8ec5STejun Heo */ 237725e8ec5STejun Heo enum pool_workqueue_stats { 238725e8ec5STejun Heo PWQ_STAT_STARTED, /* work items started execution */ 239725e8ec5STejun Heo PWQ_STAT_COMPLETED, /* work items completed execution */ 2408a1dd1e5STejun Heo PWQ_STAT_CPU_TIME, /* total CPU time consumed */ 241616db877STejun Heo PWQ_STAT_CPU_INTENSIVE, /* wq_cpu_intensive_thresh_us violations */ 242725e8ec5STejun Heo PWQ_STAT_CM_WAKEUP, /* concurrency-management worker wakeups */ 2438639ecebSTejun Heo PWQ_STAT_REPATRIATED, /* unbound workers brought back into scope */ 244725e8ec5STejun Heo PWQ_STAT_MAYDAY, /* maydays to rescuer */ 245725e8ec5STejun Heo PWQ_STAT_RESCUED, /* linked work items executed by rescuer */ 246725e8ec5STejun Heo 247725e8ec5STejun Heo PWQ_NR_STATS, 248725e8ec5STejun Heo }; 249725e8ec5STejun Heo 250725e8ec5STejun Heo /* 251e9a8e01fSTejun Heo * The per-pool workqueue. While queued, bits below WORK_PWQ_SHIFT 252112202d9STejun Heo * of work_struct->data are used for flags and the remaining high bits 253112202d9STejun Heo * point to the pwq; thus, pwqs need to be aligned at two's power of the 254112202d9STejun Heo * number of flag bits. 2551da177e4SLinus Torvalds */ 256112202d9STejun Heo struct pool_workqueue { 257bd7bdd43STejun Heo struct worker_pool *pool; /* I: the associated pool */ 2584690c4abSTejun Heo struct workqueue_struct *wq; /* I: the owning workqueue */ 25973f53c4aSTejun Heo int work_color; /* L: current color */ 26073f53c4aSTejun Heo int flush_color; /* L: flushing color */ 2618864b4e5STejun Heo int refcnt; /* L: reference count */ 26273f53c4aSTejun Heo int nr_in_flight[WORK_NR_COLORS]; 26373f53c4aSTejun Heo /* L: nr of in_flight works */ 2644c065dbcSWaiman Long bool plugged; /* L: execution suspended */ 265018f3a13SLai Jiangshan 266018f3a13SLai Jiangshan /* 267018f3a13SLai Jiangshan * nr_active management and WORK_STRUCT_INACTIVE: 268018f3a13SLai Jiangshan * 269018f3a13SLai Jiangshan * When pwq->nr_active >= max_active, new work item is queued to 270018f3a13SLai Jiangshan * pwq->inactive_works instead of pool->worklist and marked with 271018f3a13SLai Jiangshan * WORK_STRUCT_INACTIVE. 272018f3a13SLai Jiangshan * 2735797b1c1STejun Heo * All work items marked with WORK_STRUCT_INACTIVE do not participate in 2745797b1c1STejun Heo * nr_active and all work items in pwq->inactive_works are marked with 2755797b1c1STejun Heo * WORK_STRUCT_INACTIVE. But not all WORK_STRUCT_INACTIVE work items are 2765797b1c1STejun Heo * in pwq->inactive_works. Some of them are ready to run in 2775797b1c1STejun Heo * pool->worklist or worker->scheduled. Those work itmes are only struct 2785797b1c1STejun Heo * wq_barrier which is used for flush_work() and should not participate 2795797b1c1STejun Heo * in nr_active. For non-barrier work item, it is marked with 2805797b1c1STejun Heo * WORK_STRUCT_INACTIVE iff it is in pwq->inactive_works. 281018f3a13SLai Jiangshan */ 2821e19ffc6STejun Heo int nr_active; /* L: nr of active works */ 283f97a4a1aSLai Jiangshan struct list_head inactive_works; /* L: inactive works */ 2845797b1c1STejun Heo struct list_head pending_node; /* LN: node on wq_node_nr_active->pending_pwqs */ 2853c25a55dSLai Jiangshan struct list_head pwqs_node; /* WR: node on wq->pwqs */ 2862e109a28STejun Heo struct list_head mayday_node; /* MD: node on wq->maydays */ 2878864b4e5STejun Heo 288725e8ec5STejun Heo u64 stats[PWQ_NR_STATS]; 289725e8ec5STejun Heo 2908864b4e5STejun Heo /* 291967b494eSTejun Heo * Release of unbound pwq is punted to a kthread_worker. See put_pwq() 292687a9aa5STejun Heo * and pwq_release_workfn() for details. pool_workqueue itself is also 293687a9aa5STejun Heo * RCU protected so that the first pwq can be determined without 294967b494eSTejun Heo * grabbing wq->mutex. 2958864b4e5STejun Heo */ 296687a9aa5STejun Heo struct kthread_work release_work; 2978864b4e5STejun Heo struct rcu_head rcu; 298e9a8e01fSTejun Heo } __aligned(1 << WORK_STRUCT_PWQ_SHIFT); 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* 30173f53c4aSTejun Heo * Structure used to wait for workqueue flush. 30273f53c4aSTejun Heo */ 30373f53c4aSTejun Heo struct wq_flusher { 3043c25a55dSLai Jiangshan struct list_head list; /* WQ: list of flushers */ 3053c25a55dSLai Jiangshan int flush_color; /* WQ: flush color waiting for */ 30673f53c4aSTejun Heo struct completion done; /* flush completion */ 30773f53c4aSTejun Heo }; 3081da177e4SLinus Torvalds 309226223abSTejun Heo struct wq_device; 310226223abSTejun Heo 31173f53c4aSTejun Heo /* 31291ccc6e7STejun Heo * Unlike in a per-cpu workqueue where max_active limits its concurrency level 31391ccc6e7STejun Heo * on each CPU, in an unbound workqueue, max_active applies to the whole system. 31491ccc6e7STejun Heo * As sharing a single nr_active across multiple sockets can be very expensive, 31591ccc6e7STejun Heo * the counting and enforcement is per NUMA node. 3165797b1c1STejun Heo * 3175797b1c1STejun Heo * The following struct is used to enforce per-node max_active. When a pwq wants 3185797b1c1STejun Heo * to start executing a work item, it should increment ->nr using 3195797b1c1STejun Heo * tryinc_node_nr_active(). If acquisition fails due to ->nr already being over 3205797b1c1STejun Heo * ->max, the pwq is queued on ->pending_pwqs. As in-flight work items finish 3215797b1c1STejun Heo * and decrement ->nr, node_activate_pending_pwq() activates the pending pwqs in 3225797b1c1STejun Heo * round-robin order. 32391ccc6e7STejun Heo */ 32491ccc6e7STejun Heo struct wq_node_nr_active { 3255797b1c1STejun Heo int max; /* per-node max_active */ 3265797b1c1STejun Heo atomic_t nr; /* per-node nr_active */ 3275797b1c1STejun Heo raw_spinlock_t lock; /* nests inside pool locks */ 3285797b1c1STejun Heo struct list_head pending_pwqs; /* LN: pwqs with inactive works */ 32991ccc6e7STejun Heo }; 33091ccc6e7STejun Heo 33191ccc6e7STejun Heo /* 332c5aa87bbSTejun Heo * The externally visible workqueue. It relays the issued work items to 333c5aa87bbSTejun Heo * the appropriate worker_pool through its pool_workqueues. 3341da177e4SLinus Torvalds */ 3351da177e4SLinus Torvalds struct workqueue_struct { 3363c25a55dSLai Jiangshan struct list_head pwqs; /* WR: all pwqs of this wq */ 337e2dca7adSTejun Heo struct list_head list; /* PR: list of all workqueues */ 33873f53c4aSTejun Heo 3393c25a55dSLai Jiangshan struct mutex mutex; /* protects this wq */ 3403c25a55dSLai Jiangshan int work_color; /* WQ: current work color */ 3413c25a55dSLai Jiangshan int flush_color; /* WQ: current flush color */ 342112202d9STejun Heo atomic_t nr_pwqs_to_flush; /* flush in progress */ 3433c25a55dSLai Jiangshan struct wq_flusher *first_flusher; /* WQ: first flusher */ 3443c25a55dSLai Jiangshan struct list_head flusher_queue; /* WQ: flush waiters */ 3453c25a55dSLai Jiangshan struct list_head flusher_overflow; /* WQ: flush overflow list */ 34673f53c4aSTejun Heo 3472e109a28STejun Heo struct list_head maydays; /* MD: pwqs requesting rescue */ 34830ae2fc0STejun Heo struct worker *rescuer; /* MD: rescue worker */ 349e22bee78STejun Heo 35087fc741eSLai Jiangshan int nr_drainers; /* WQ: drain in progress */ 3515797b1c1STejun Heo 3525797b1c1STejun Heo /* See alloc_workqueue() function comment for info on min/max_active */ 353a045a272STejun Heo int max_active; /* WO: max active works */ 3545797b1c1STejun Heo int min_active; /* WO: min active works */ 355a045a272STejun Heo int saved_max_active; /* WQ: saved max_active */ 3565797b1c1STejun Heo int saved_min_active; /* WQ: saved min_active */ 357226223abSTejun Heo 3585b95e1afSLai Jiangshan struct workqueue_attrs *unbound_attrs; /* PW: only for unbound wqs */ 3599f66cff2STejun Heo struct pool_workqueue __rcu *dfl_pwq; /* PW: only for unbound wqs */ 3606029a918STejun Heo 361226223abSTejun Heo #ifdef CONFIG_SYSFS 362226223abSTejun Heo struct wq_device *wq_dev; /* I: for sysfs interface */ 363226223abSTejun Heo #endif 3644e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 365669de8bdSBart Van Assche char *lock_name; 366669de8bdSBart Van Assche struct lock_class_key key; 3674e6045f1SJohannes Berg struct lockdep_map lockdep_map; 3684e6045f1SJohannes Berg #endif 369ecf6881fSTejun Heo char name[WQ_NAME_LEN]; /* I: workqueue name */ 3702728fd2fSTejun Heo 371e2dca7adSTejun Heo /* 37224acfb71SThomas Gleixner * Destruction of workqueue_struct is RCU protected to allow walking 37324acfb71SThomas Gleixner * the workqueues list without grabbing wq_pool_mutex. 374e2dca7adSTejun Heo * This is used to dump all workqueues from sysrq. 375e2dca7adSTejun Heo */ 376e2dca7adSTejun Heo struct rcu_head rcu; 377e2dca7adSTejun Heo 3782728fd2fSTejun Heo /* hot fields used during command issue, aligned to cacheline */ 3792728fd2fSTejun Heo unsigned int flags ____cacheline_aligned; /* WQ: WQ_* flags */ 380636b927eSTejun Heo struct pool_workqueue __percpu __rcu **cpu_pwq; /* I: per-cpu pwqs */ 38191ccc6e7STejun Heo struct wq_node_nr_active *node_nr_active[]; /* I: per-node nr_active */ 3821da177e4SLinus Torvalds }; 3831da177e4SLinus Torvalds 38484193c07STejun Heo /* 38584193c07STejun Heo * Each pod type describes how CPUs should be grouped for unbound workqueues. 38684193c07STejun Heo * See the comment above workqueue_attrs->affn_scope. 38784193c07STejun Heo */ 38884193c07STejun Heo struct wq_pod_type { 38984193c07STejun Heo int nr_pods; /* number of pods */ 39084193c07STejun Heo cpumask_var_t *pod_cpus; /* pod -> cpus */ 39184193c07STejun Heo int *pod_node; /* pod -> node */ 39284193c07STejun Heo int *cpu_pod; /* cpu -> pod */ 39384193c07STejun Heo }; 39484193c07STejun Heo 39563c5484eSTejun Heo static const char *wq_affn_names[WQ_AFFN_NR_TYPES] = { 396523a301eSTejun Heo [WQ_AFFN_DFL] = "default", 39763c5484eSTejun Heo [WQ_AFFN_CPU] = "cpu", 39863c5484eSTejun Heo [WQ_AFFN_SMT] = "smt", 39963c5484eSTejun Heo [WQ_AFFN_CACHE] = "cache", 40063c5484eSTejun Heo [WQ_AFFN_NUMA] = "numa", 40163c5484eSTejun Heo [WQ_AFFN_SYSTEM] = "system", 40263c5484eSTejun Heo }; 403bce90380STejun Heo 404616db877STejun Heo /* 405616db877STejun Heo * Per-cpu work items which run for longer than the following threshold are 406616db877STejun Heo * automatically considered CPU intensive and excluded from concurrency 407616db877STejun Heo * management to prevent them from noticeably delaying other per-cpu work items. 408aa6fde93STejun Heo * ULONG_MAX indicates that the user hasn't overridden it with a boot parameter. 409aa6fde93STejun Heo * The actual value is initialized in wq_cpu_intensive_thresh_init(). 410616db877STejun Heo */ 411aa6fde93STejun Heo static unsigned long wq_cpu_intensive_thresh_us = ULONG_MAX; 412616db877STejun Heo module_param_named(cpu_intensive_thresh_us, wq_cpu_intensive_thresh_us, ulong, 0644); 413ccdec921SXuewen Yan #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 414ccdec921SXuewen Yan static unsigned int wq_cpu_intensive_warning_thresh = 4; 415ccdec921SXuewen Yan module_param_named(cpu_intensive_warning_thresh, wq_cpu_intensive_warning_thresh, uint, 0644); 416ccdec921SXuewen Yan #endif 417616db877STejun Heo 418cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 419552f530cSLuis R. Rodriguez static bool wq_power_efficient = IS_ENABLED(CONFIG_WQ_POWER_EFFICIENT_DEFAULT); 420cee22a15SViresh Kumar module_param_named(power_efficient, wq_power_efficient, bool, 0444); 421cee22a15SViresh Kumar 422863b710bSTejun Heo static bool wq_online; /* can kworkers be created yet? */ 423c7a40c49STejun Heo static bool wq_topo_initialized __read_mostly = false; 424c7a40c49STejun Heo 425c7a40c49STejun Heo static struct kmem_cache *pwq_cache; 426c7a40c49STejun Heo 427c7a40c49STejun Heo static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES]; 428c7a40c49STejun Heo static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE; 4293347fa09STejun Heo 430fef59c9cSTejun Heo /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */ 431fef59c9cSTejun Heo static struct workqueue_attrs *wq_update_pod_attrs_buf; 4324c16bd32STejun Heo 43368e13a67SLai Jiangshan static DEFINE_MUTEX(wq_pool_mutex); /* protects pools and workqueues list */ 4341258fae7STejun Heo static DEFINE_MUTEX(wq_pool_attach_mutex); /* protects worker attach/detach */ 435a9b8a985SSebastian Andrzej Siewior static DEFINE_RAW_SPINLOCK(wq_mayday_lock); /* protects wq->maydays list */ 436d8bb65abSSebastian Andrzej Siewior /* wait for manager to go away */ 437d8bb65abSSebastian Andrzej Siewior static struct rcuwait manager_wait = __RCUWAIT_INITIALIZER(manager_wait); 4385bcab335STejun Heo 439e2dca7adSTejun Heo static LIST_HEAD(workqueues); /* PR: list of all workqueues */ 44068e13a67SLai Jiangshan static bool workqueue_freezing; /* PL: have wqs started freezing? */ 4417d19c5ceSTejun Heo 44299c621efSLai Jiangshan /* PL&A: allowable cpus for unbound wqs and work items */ 443ef557180SMike Galbraith static cpumask_var_t wq_unbound_cpumask; 444ef557180SMike Galbraith 445fe28f631SWaiman Long /* PL: user requested unbound cpumask via sysfs */ 446fe28f631SWaiman Long static cpumask_var_t wq_requested_unbound_cpumask; 447fe28f631SWaiman Long 448fe28f631SWaiman Long /* PL: isolated cpumask to be excluded from unbound cpumask */ 449fe28f631SWaiman Long static cpumask_var_t wq_isolated_cpumask; 450fe28f631SWaiman Long 451ace3c549Stiozhang /* for further constrain wq_unbound_cpumask by cmdline parameter*/ 452ace3c549Stiozhang static struct cpumask wq_cmdline_cpumask __initdata; 453ace3c549Stiozhang 454ef557180SMike Galbraith /* CPU where unbound work was last round robin scheduled from this CPU */ 455ef557180SMike Galbraith static DEFINE_PER_CPU(int, wq_rr_cpu_last); 456b05a7928SFrederic Weisbecker 457f303fccbSTejun Heo /* 458f303fccbSTejun Heo * Local execution of unbound work items is no longer guaranteed. The 459f303fccbSTejun Heo * following always forces round-robin CPU selection on unbound work items 460f303fccbSTejun Heo * to uncover usages which depend on it. 461f303fccbSTejun Heo */ 462f303fccbSTejun Heo #ifdef CONFIG_DEBUG_WQ_FORCE_RR_CPU 463f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = true; 464f303fccbSTejun Heo #else 465f303fccbSTejun Heo static bool wq_debug_force_rr_cpu = false; 466f303fccbSTejun Heo #endif 467f303fccbSTejun Heo module_param_named(debug_force_rr_cpu, wq_debug_force_rr_cpu, bool, 0644); 468f303fccbSTejun Heo 4692f34d733STejun Heo /* to raise softirq for the BH worker pools on other CPUs */ 4702f34d733STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct irq_work [NR_STD_WORKER_POOLS], 4712f34d733STejun Heo bh_pool_irq_works); 4722f34d733STejun Heo 4734cb1ef64STejun Heo /* the BH worker pools */ 4744cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4754cb1ef64STejun Heo bh_worker_pools); 4764cb1ef64STejun Heo 4777d19c5ceSTejun Heo /* the per-cpu worker pools */ 4784cb1ef64STejun Heo static DEFINE_PER_CPU_SHARED_ALIGNED(struct worker_pool [NR_STD_WORKER_POOLS], 4794cb1ef64STejun Heo cpu_worker_pools); 4807d19c5ceSTejun Heo 48168e13a67SLai Jiangshan static DEFINE_IDR(worker_pool_idr); /* PR: idr of all pools */ 4827d19c5ceSTejun Heo 48368e13a67SLai Jiangshan /* PL: hash of all unbound pools keyed by pool->attrs */ 48429c91e99STejun Heo static DEFINE_HASHTABLE(unbound_pool_hash, UNBOUND_POOL_HASH_ORDER); 48529c91e99STejun Heo 486c5aa87bbSTejun Heo /* I: attributes used when instantiating standard unbound pools on demand */ 48729c91e99STejun Heo static struct workqueue_attrs *unbound_std_wq_attrs[NR_STD_WORKER_POOLS]; 48829c91e99STejun Heo 4898a2b7538STejun Heo /* I: attributes used when instantiating ordered pools on demand */ 4908a2b7538STejun Heo static struct workqueue_attrs *ordered_wq_attrs[NR_STD_WORKER_POOLS]; 4918a2b7538STejun Heo 492967b494eSTejun Heo /* 493978b8409STejun Heo * Used to synchronize multiple cancel_sync attempts on the same work item. See 494978b8409STejun Heo * work_grab_pending() and __cancel_work_sync(). 495978b8409STejun Heo */ 496978b8409STejun Heo static DECLARE_WAIT_QUEUE_HEAD(wq_cancel_waitq); 497978b8409STejun Heo 498978b8409STejun Heo /* 499967b494eSTejun Heo * I: kthread_worker to release pwq's. pwq release needs to be bounced to a 500967b494eSTejun Heo * process context while holding a pool lock. Bounce to a dedicated kthread 501967b494eSTejun Heo * worker to avoid A-A deadlocks. 502967b494eSTejun Heo */ 50368279f9cSAlexey Dobriyan static struct kthread_worker *pwq_release_worker __ro_after_init; 504967b494eSTejun Heo 50568279f9cSAlexey Dobriyan struct workqueue_struct *system_wq __ro_after_init; 506ad7b1f84SMarc Dionne EXPORT_SYMBOL(system_wq); 50768279f9cSAlexey Dobriyan struct workqueue_struct *system_highpri_wq __ro_after_init; 5081aabe902SJoonsoo Kim EXPORT_SYMBOL_GPL(system_highpri_wq); 50968279f9cSAlexey Dobriyan struct workqueue_struct *system_long_wq __ro_after_init; 510d320c038STejun Heo EXPORT_SYMBOL_GPL(system_long_wq); 51168279f9cSAlexey Dobriyan struct workqueue_struct *system_unbound_wq __ro_after_init; 512f3421797STejun Heo EXPORT_SYMBOL_GPL(system_unbound_wq); 51368279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_wq __ro_after_init; 51424d51addSTejun Heo EXPORT_SYMBOL_GPL(system_freezable_wq); 51568279f9cSAlexey Dobriyan struct workqueue_struct *system_power_efficient_wq __ro_after_init; 5160668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_power_efficient_wq); 51768279f9cSAlexey Dobriyan struct workqueue_struct *system_freezable_power_efficient_wq __ro_after_init; 5180668106cSViresh Kumar EXPORT_SYMBOL_GPL(system_freezable_power_efficient_wq); 5194cb1ef64STejun Heo struct workqueue_struct *system_bh_wq; 5204cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_wq); 5214cb1ef64STejun Heo struct workqueue_struct *system_bh_highpri_wq; 5224cb1ef64STejun Heo EXPORT_SYMBOL_GPL(system_bh_highpri_wq); 523d320c038STejun Heo 5247d19c5ceSTejun Heo static int worker_thread(void *__worker); 5256ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq); 526c29eb853STejun Heo static void show_pwq(struct pool_workqueue *pwq); 52755df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool); 5287d19c5ceSTejun Heo 52997bd2347STejun Heo #define CREATE_TRACE_POINTS 53097bd2347STejun Heo #include <trace/events/workqueue.h> 53197bd2347STejun Heo 53268e13a67SLai Jiangshan #define assert_rcu_or_pool_mutex() \ 533d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 534f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 53524acfb71SThomas Gleixner "RCU or wq_pool_mutex should be held") 5365bcab335STejun Heo 5375b95e1afSLai Jiangshan #define assert_rcu_or_wq_mutex_or_pool_mutex(wq) \ 538d355001fSTejun Heo RCU_LOCKDEP_WARN(!rcu_read_lock_any_held() && \ 539f78f5b90SPaul E. McKenney !lockdep_is_held(&wq->mutex) && \ 540f78f5b90SPaul E. McKenney !lockdep_is_held(&wq_pool_mutex), \ 54124acfb71SThomas Gleixner "RCU, wq->mutex or wq_pool_mutex should be held") 5425b95e1afSLai Jiangshan 5434cb1ef64STejun Heo #define for_each_bh_worker_pool(pool, cpu) \ 5444cb1ef64STejun Heo for ((pool) = &per_cpu(bh_worker_pools, cpu)[0]; \ 5454cb1ef64STejun Heo (pool) < &per_cpu(bh_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5464cb1ef64STejun Heo (pool)++) 5474cb1ef64STejun Heo 548f02ae73aSTejun Heo #define for_each_cpu_worker_pool(pool, cpu) \ 549f02ae73aSTejun Heo for ((pool) = &per_cpu(cpu_worker_pools, cpu)[0]; \ 550f02ae73aSTejun Heo (pool) < &per_cpu(cpu_worker_pools, cpu)[NR_STD_WORKER_POOLS]; \ 5517a62c2c8STejun Heo (pool)++) 5524ce62e9eSTejun Heo 55349e3cf44STejun Heo /** 55417116969STejun Heo * for_each_pool - iterate through all worker_pools in the system 55517116969STejun Heo * @pool: iteration cursor 556611c92a0STejun Heo * @pi: integer used for iteration 557fa1b54e6STejun Heo * 55824acfb71SThomas Gleixner * This must be called either with wq_pool_mutex held or RCU read 55968e13a67SLai Jiangshan * locked. If the pool needs to be used beyond the locking in effect, the 56068e13a67SLai Jiangshan * caller is responsible for guaranteeing that the pool stays online. 561fa1b54e6STejun Heo * 562fa1b54e6STejun Heo * The if/else clause exists only for the lockdep assertion and can be 563fa1b54e6STejun Heo * ignored. 56417116969STejun Heo */ 565611c92a0STejun Heo #define for_each_pool(pool, pi) \ 566611c92a0STejun Heo idr_for_each_entry(&worker_pool_idr, pool, pi) \ 56768e13a67SLai Jiangshan if (({ assert_rcu_or_pool_mutex(); false; })) { } \ 568fa1b54e6STejun Heo else 56917116969STejun Heo 57017116969STejun Heo /** 571822d8405STejun Heo * for_each_pool_worker - iterate through all workers of a worker_pool 572822d8405STejun Heo * @worker: iteration cursor 573822d8405STejun Heo * @pool: worker_pool to iterate workers of 574822d8405STejun Heo * 5751258fae7STejun Heo * This must be called with wq_pool_attach_mutex. 576822d8405STejun Heo * 577822d8405STejun Heo * The if/else clause exists only for the lockdep assertion and can be 578822d8405STejun Heo * ignored. 579822d8405STejun Heo */ 580da028469SLai Jiangshan #define for_each_pool_worker(worker, pool) \ 581da028469SLai Jiangshan list_for_each_entry((worker), &(pool)->workers, node) \ 5821258fae7STejun Heo if (({ lockdep_assert_held(&wq_pool_attach_mutex); false; })) { } \ 583822d8405STejun Heo else 584822d8405STejun Heo 585822d8405STejun Heo /** 58649e3cf44STejun Heo * for_each_pwq - iterate through all pool_workqueues of the specified workqueue 58749e3cf44STejun Heo * @pwq: iteration cursor 58849e3cf44STejun Heo * @wq: the target workqueue 58976af4d93STejun Heo * 59024acfb71SThomas Gleixner * This must be called either with wq->mutex held or RCU read locked. 591794b18bcSTejun Heo * If the pwq needs to be used beyond the locking in effect, the caller is 592794b18bcSTejun Heo * responsible for guaranteeing that the pwq stays online. 59376af4d93STejun Heo * 59476af4d93STejun Heo * The if/else clause exists only for the lockdep assertion and can be 59576af4d93STejun Heo * ignored. 59649e3cf44STejun Heo */ 59749e3cf44STejun Heo #define for_each_pwq(pwq, wq) \ 59849e9d1a9SSebastian Andrzej Siewior list_for_each_entry_rcu((pwq), &(wq)->pwqs, pwqs_node, \ 5995a644662SJoel Fernandes (Google) lockdep_is_held(&(wq->mutex))) 600f3421797STejun Heo 601dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK 602dc186ad7SThomas Gleixner 603f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr; 604dc186ad7SThomas Gleixner 60599777288SStanislaw Gruszka static void *work_debug_hint(void *addr) 60699777288SStanislaw Gruszka { 60799777288SStanislaw Gruszka return ((struct work_struct *) addr)->func; 60899777288SStanislaw Gruszka } 60999777288SStanislaw Gruszka 610b9fdac7fSDu, Changbin static bool work_is_static_object(void *addr) 611b9fdac7fSDu, Changbin { 612b9fdac7fSDu, Changbin struct work_struct *work = addr; 613b9fdac7fSDu, Changbin 614b9fdac7fSDu, Changbin return test_bit(WORK_STRUCT_STATIC_BIT, work_data_bits(work)); 615b9fdac7fSDu, Changbin } 616b9fdac7fSDu, Changbin 617dc186ad7SThomas Gleixner /* 618dc186ad7SThomas Gleixner * fixup_init is called when: 619dc186ad7SThomas Gleixner * - an active object is initialized 620dc186ad7SThomas Gleixner */ 62102a982a6SDu, Changbin static bool work_fixup_init(void *addr, enum debug_obj_state state) 622dc186ad7SThomas Gleixner { 623dc186ad7SThomas Gleixner struct work_struct *work = addr; 624dc186ad7SThomas Gleixner 625dc186ad7SThomas Gleixner switch (state) { 626dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 627dc186ad7SThomas Gleixner cancel_work_sync(work); 628dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 62902a982a6SDu, Changbin return true; 630dc186ad7SThomas Gleixner default: 63102a982a6SDu, Changbin return false; 632dc186ad7SThomas Gleixner } 633dc186ad7SThomas Gleixner } 634dc186ad7SThomas Gleixner 635dc186ad7SThomas Gleixner /* 636dc186ad7SThomas Gleixner * fixup_free is called when: 637dc186ad7SThomas Gleixner * - an active object is freed 638dc186ad7SThomas Gleixner */ 63902a982a6SDu, Changbin static bool work_fixup_free(void *addr, enum debug_obj_state state) 640dc186ad7SThomas Gleixner { 641dc186ad7SThomas Gleixner struct work_struct *work = addr; 642dc186ad7SThomas Gleixner 643dc186ad7SThomas Gleixner switch (state) { 644dc186ad7SThomas Gleixner case ODEBUG_STATE_ACTIVE: 645dc186ad7SThomas Gleixner cancel_work_sync(work); 646dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 64702a982a6SDu, Changbin return true; 648dc186ad7SThomas Gleixner default: 64902a982a6SDu, Changbin return false; 650dc186ad7SThomas Gleixner } 651dc186ad7SThomas Gleixner } 652dc186ad7SThomas Gleixner 653f9e62f31SStephen Boyd static const struct debug_obj_descr work_debug_descr = { 654dc186ad7SThomas Gleixner .name = "work_struct", 65599777288SStanislaw Gruszka .debug_hint = work_debug_hint, 656b9fdac7fSDu, Changbin .is_static_object = work_is_static_object, 657dc186ad7SThomas Gleixner .fixup_init = work_fixup_init, 658dc186ad7SThomas Gleixner .fixup_free = work_fixup_free, 659dc186ad7SThomas Gleixner }; 660dc186ad7SThomas Gleixner 661dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) 662dc186ad7SThomas Gleixner { 663dc186ad7SThomas Gleixner debug_object_activate(work, &work_debug_descr); 664dc186ad7SThomas Gleixner } 665dc186ad7SThomas Gleixner 666dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) 667dc186ad7SThomas Gleixner { 668dc186ad7SThomas Gleixner debug_object_deactivate(work, &work_debug_descr); 669dc186ad7SThomas Gleixner } 670dc186ad7SThomas Gleixner 671dc186ad7SThomas Gleixner void __init_work(struct work_struct *work, int onstack) 672dc186ad7SThomas Gleixner { 673dc186ad7SThomas Gleixner if (onstack) 674dc186ad7SThomas Gleixner debug_object_init_on_stack(work, &work_debug_descr); 675dc186ad7SThomas Gleixner else 676dc186ad7SThomas Gleixner debug_object_init(work, &work_debug_descr); 677dc186ad7SThomas Gleixner } 678dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(__init_work); 679dc186ad7SThomas Gleixner 680dc186ad7SThomas Gleixner void destroy_work_on_stack(struct work_struct *work) 681dc186ad7SThomas Gleixner { 682dc186ad7SThomas Gleixner debug_object_free(work, &work_debug_descr); 683dc186ad7SThomas Gleixner } 684dc186ad7SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_work_on_stack); 685dc186ad7SThomas Gleixner 686ea2e64f2SThomas Gleixner void destroy_delayed_work_on_stack(struct delayed_work *work) 687ea2e64f2SThomas Gleixner { 688ea2e64f2SThomas Gleixner destroy_timer_on_stack(&work->timer); 689ea2e64f2SThomas Gleixner debug_object_free(&work->work, &work_debug_descr); 690ea2e64f2SThomas Gleixner } 691ea2e64f2SThomas Gleixner EXPORT_SYMBOL_GPL(destroy_delayed_work_on_stack); 692ea2e64f2SThomas Gleixner 693dc186ad7SThomas Gleixner #else 694dc186ad7SThomas Gleixner static inline void debug_work_activate(struct work_struct *work) { } 695dc186ad7SThomas Gleixner static inline void debug_work_deactivate(struct work_struct *work) { } 696dc186ad7SThomas Gleixner #endif 697dc186ad7SThomas Gleixner 6984e8b22bdSLi Bin /** 69967dc8325SCai Huoqing * worker_pool_assign_id - allocate ID and assign it to @pool 7004e8b22bdSLi Bin * @pool: the pool pointer of interest 7014e8b22bdSLi Bin * 7024e8b22bdSLi Bin * Returns 0 if ID in [0, WORK_OFFQ_POOL_NONE) is allocated and assigned 7034e8b22bdSLi Bin * successfully, -errno on failure. 7044e8b22bdSLi Bin */ 7059daf9e67STejun Heo static int worker_pool_assign_id(struct worker_pool *pool) 7069daf9e67STejun Heo { 7079daf9e67STejun Heo int ret; 7089daf9e67STejun Heo 70968e13a67SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 7105bcab335STejun Heo 7114e8b22bdSLi Bin ret = idr_alloc(&worker_pool_idr, pool, 0, WORK_OFFQ_POOL_NONE, 7124e8b22bdSLi Bin GFP_KERNEL); 713229641a6STejun Heo if (ret >= 0) { 714e68035fbSTejun Heo pool->id = ret; 715229641a6STejun Heo return 0; 716229641a6STejun Heo } 7179daf9e67STejun Heo return ret; 7189daf9e67STejun Heo } 7199daf9e67STejun Heo 7209f66cff2STejun Heo static struct pool_workqueue __rcu ** 7219f66cff2STejun Heo unbound_pwq_slot(struct workqueue_struct *wq, int cpu) 7229f66cff2STejun Heo { 7239f66cff2STejun Heo if (cpu >= 0) 7249f66cff2STejun Heo return per_cpu_ptr(wq->cpu_pwq, cpu); 7259f66cff2STejun Heo else 7269f66cff2STejun Heo return &wq->dfl_pwq; 7279f66cff2STejun Heo } 7289f66cff2STejun Heo 7299f66cff2STejun Heo /* @cpu < 0 for dfl_pwq */ 7309f66cff2STejun Heo static struct pool_workqueue *unbound_pwq(struct workqueue_struct *wq, int cpu) 7319f66cff2STejun Heo { 7329f66cff2STejun Heo return rcu_dereference_check(*unbound_pwq_slot(wq, cpu), 7339f66cff2STejun Heo lockdep_is_held(&wq_pool_mutex) || 7349f66cff2STejun Heo lockdep_is_held(&wq->mutex)); 7359f66cff2STejun Heo } 7369f66cff2STejun Heo 7375797b1c1STejun Heo /** 7385797b1c1STejun Heo * unbound_effective_cpumask - effective cpumask of an unbound workqueue 7395797b1c1STejun Heo * @wq: workqueue of interest 7405797b1c1STejun Heo * 7415797b1c1STejun Heo * @wq->unbound_attrs->cpumask contains the cpumask requested by the user which 7425797b1c1STejun Heo * is masked with wq_unbound_cpumask to determine the effective cpumask. The 7435797b1c1STejun Heo * default pwq is always mapped to the pool with the current effective cpumask. 7445797b1c1STejun Heo */ 7455797b1c1STejun Heo static struct cpumask *unbound_effective_cpumask(struct workqueue_struct *wq) 7465797b1c1STejun Heo { 7475797b1c1STejun Heo return unbound_pwq(wq, -1)->pool->attrs->__pod_cpumask; 7485797b1c1STejun Heo } 7495797b1c1STejun Heo 75073f53c4aSTejun Heo static unsigned int work_color_to_flags(int color) 75173f53c4aSTejun Heo { 75273f53c4aSTejun Heo return color << WORK_STRUCT_COLOR_SHIFT; 75373f53c4aSTejun Heo } 75473f53c4aSTejun Heo 755c4560c2cSLai Jiangshan static int get_work_color(unsigned long work_data) 75673f53c4aSTejun Heo { 757c4560c2cSLai Jiangshan return (work_data >> WORK_STRUCT_COLOR_SHIFT) & 75873f53c4aSTejun Heo ((1 << WORK_STRUCT_COLOR_BITS) - 1); 75973f53c4aSTejun Heo } 76073f53c4aSTejun Heo 76173f53c4aSTejun Heo static int work_next_color(int color) 76273f53c4aSTejun Heo { 76373f53c4aSTejun Heo return (color + 1) % WORK_NR_COLORS; 764a848e3b6SOleg Nesterov } 765a848e3b6SOleg Nesterov 7664594bf15SDavid Howells /* 767112202d9STejun Heo * While queued, %WORK_STRUCT_PWQ is set and non flag bits of a work's data 768112202d9STejun Heo * contain the pointer to the queued pwq. Once execution starts, the flag 7697c3eed5cSTejun Heo * is cleared and the high bits contain OFFQ flags and pool ID. 7707a22ad75STejun Heo * 771afe928c1STejun Heo * set_work_pwq(), set_work_pool_and_clear_pending() and mark_work_canceling() 772afe928c1STejun Heo * can be used to set the pwq, pool or clear work->data. These functions should 773afe928c1STejun Heo * only be called while the work is owned - ie. while the PENDING bit is set. 7747a22ad75STejun Heo * 775112202d9STejun Heo * get_work_pool() and get_work_pwq() can be used to obtain the pool or pwq 7767c3eed5cSTejun Heo * corresponding to a work. Pool is available once the work has been 777112202d9STejun Heo * queued anywhere after initialization until it is sync canceled. pwq is 7787c3eed5cSTejun Heo * available only while the work item is queued. 779bbb68dfaSTejun Heo * 780bbb68dfaSTejun Heo * %WORK_OFFQ_CANCELING is used to mark a work item which is being 781bbb68dfaSTejun Heo * canceled. While being canceled, a work item may have its PENDING set 782bbb68dfaSTejun Heo * but stay off timer and worklist for arbitrarily long and nobody should 783bbb68dfaSTejun Heo * try to steal the PENDING bit. 7844594bf15SDavid Howells */ 785bccdc1faSTejun Heo static inline void set_work_data(struct work_struct *work, unsigned long data) 7867a22ad75STejun Heo { 7876183c009STejun Heo WARN_ON_ONCE(!work_pending(work)); 788bccdc1faSTejun Heo atomic_long_set(&work->data, data | work_static(work)); 7897a22ad75STejun Heo } 7907a22ad75STejun Heo 791112202d9STejun Heo static void set_work_pwq(struct work_struct *work, struct pool_workqueue *pwq, 792bccdc1faSTejun Heo unsigned long flags) 793365970a1SDavid Howells { 794bccdc1faSTejun Heo set_work_data(work, (unsigned long)pwq | WORK_STRUCT_PENDING | 795bccdc1faSTejun Heo WORK_STRUCT_PWQ | flags); 796365970a1SDavid Howells } 797365970a1SDavid Howells 7984468a00fSLai Jiangshan static void set_work_pool_and_keep_pending(struct work_struct *work, 799bccdc1faSTejun Heo int pool_id, unsigned long flags) 8004468a00fSLai Jiangshan { 801bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) | 802bccdc1faSTejun Heo WORK_STRUCT_PENDING | flags); 8034468a00fSLai Jiangshan } 8044468a00fSLai Jiangshan 8057c3eed5cSTejun Heo static void set_work_pool_and_clear_pending(struct work_struct *work, 806bccdc1faSTejun Heo int pool_id, unsigned long flags) 8074d707b9fSOleg Nesterov { 80823657bb1STejun Heo /* 80923657bb1STejun Heo * The following wmb is paired with the implied mb in 81023657bb1STejun Heo * test_and_set_bit(PENDING) and ensures all updates to @work made 81123657bb1STejun Heo * here are visible to and precede any updates by the next PENDING 81223657bb1STejun Heo * owner. 81323657bb1STejun Heo */ 81423657bb1STejun Heo smp_wmb(); 815bccdc1faSTejun Heo set_work_data(work, ((unsigned long)pool_id << WORK_OFFQ_POOL_SHIFT) | 816bccdc1faSTejun Heo flags); 817346c09f8SRoman Pen /* 818346c09f8SRoman Pen * The following mb guarantees that previous clear of a PENDING bit 819346c09f8SRoman Pen * will not be reordered with any speculative LOADS or STORES from 820346c09f8SRoman Pen * work->current_func, which is executed afterwards. This possible 8218bdc6201SLiu Song * reordering can lead to a missed execution on attempt to queue 822346c09f8SRoman Pen * the same @work. E.g. consider this case: 823346c09f8SRoman Pen * 824346c09f8SRoman Pen * CPU#0 CPU#1 825346c09f8SRoman Pen * ---------------------------- -------------------------------- 826346c09f8SRoman Pen * 827346c09f8SRoman Pen * 1 STORE event_indicated 828346c09f8SRoman Pen * 2 queue_work_on() { 829346c09f8SRoman Pen * 3 test_and_set_bit(PENDING) 830346c09f8SRoman Pen * 4 } set_..._and_clear_pending() { 831346c09f8SRoman Pen * 5 set_work_data() # clear bit 832346c09f8SRoman Pen * 6 smp_mb() 833346c09f8SRoman Pen * 7 work->current_func() { 834346c09f8SRoman Pen * 8 LOAD event_indicated 835346c09f8SRoman Pen * } 836346c09f8SRoman Pen * 837346c09f8SRoman Pen * Without an explicit full barrier speculative LOAD on line 8 can 838346c09f8SRoman Pen * be executed before CPU#0 does STORE on line 1. If that happens, 839346c09f8SRoman Pen * CPU#0 observes the PENDING bit is still set and new execution of 840346c09f8SRoman Pen * a @work is not queued in a hope, that CPU#1 will eventually 841346c09f8SRoman Pen * finish the queued @work. Meanwhile CPU#1 does not see 842346c09f8SRoman Pen * event_indicated is set, because speculative LOAD was executed 843346c09f8SRoman Pen * before actual STORE. 844346c09f8SRoman Pen */ 845346c09f8SRoman Pen smp_mb(); 8464d707b9fSOleg Nesterov } 8474d707b9fSOleg Nesterov 848afa4bb77SLinus Torvalds static inline struct pool_workqueue *work_struct_pwq(unsigned long data) 849afa4bb77SLinus Torvalds { 850e9a8e01fSTejun Heo return (struct pool_workqueue *)(data & WORK_STRUCT_PWQ_MASK); 851afa4bb77SLinus Torvalds } 852afa4bb77SLinus Torvalds 853112202d9STejun Heo static struct pool_workqueue *get_work_pwq(struct work_struct *work) 8547a22ad75STejun Heo { 855e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8567a22ad75STejun Heo 857112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 858afa4bb77SLinus Torvalds return work_struct_pwq(data); 859e120153dSTejun Heo else 860e120153dSTejun Heo return NULL; 8617a22ad75STejun Heo } 8627a22ad75STejun Heo 8637c3eed5cSTejun Heo /** 8647c3eed5cSTejun Heo * get_work_pool - return the worker_pool a given work was associated with 8657c3eed5cSTejun Heo * @work: the work item of interest 8667c3eed5cSTejun Heo * 86768e13a67SLai Jiangshan * Pools are created and destroyed under wq_pool_mutex, and allows read 86824acfb71SThomas Gleixner * access under RCU read lock. As such, this function should be 86924acfb71SThomas Gleixner * called under wq_pool_mutex or inside of a rcu_read_lock() region. 870fa1b54e6STejun Heo * 871fa1b54e6STejun Heo * All fields of the returned pool are accessible as long as the above 872fa1b54e6STejun Heo * mentioned locking is in effect. If the returned pool needs to be used 873fa1b54e6STejun Heo * beyond the critical section, the caller is responsible for ensuring the 874fa1b54e6STejun Heo * returned pool is and stays online. 875d185af30SYacine Belkadi * 876d185af30SYacine Belkadi * Return: The worker_pool @work was last associated with. %NULL if none. 8777c3eed5cSTejun Heo */ 8787c3eed5cSTejun Heo static struct worker_pool *get_work_pool(struct work_struct *work) 8797a22ad75STejun Heo { 880e120153dSTejun Heo unsigned long data = atomic_long_read(&work->data); 8817c3eed5cSTejun Heo int pool_id; 8827a22ad75STejun Heo 88368e13a67SLai Jiangshan assert_rcu_or_pool_mutex(); 884fa1b54e6STejun Heo 885112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 886afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool; 8877a22ad75STejun Heo 8887c3eed5cSTejun Heo pool_id = data >> WORK_OFFQ_POOL_SHIFT; 8897c3eed5cSTejun Heo if (pool_id == WORK_OFFQ_POOL_NONE) 8907a22ad75STejun Heo return NULL; 8917a22ad75STejun Heo 892fa1b54e6STejun Heo return idr_find(&worker_pool_idr, pool_id); 8937c3eed5cSTejun Heo } 8947c3eed5cSTejun Heo 8957c3eed5cSTejun Heo /** 8967c3eed5cSTejun Heo * get_work_pool_id - return the worker pool ID a given work is associated with 8977c3eed5cSTejun Heo * @work: the work item of interest 8987c3eed5cSTejun Heo * 899d185af30SYacine Belkadi * Return: The worker_pool ID @work was last associated with. 9007c3eed5cSTejun Heo * %WORK_OFFQ_POOL_NONE if none. 9017c3eed5cSTejun Heo */ 9027c3eed5cSTejun Heo static int get_work_pool_id(struct work_struct *work) 9037c3eed5cSTejun Heo { 90454d5b7d0SLai Jiangshan unsigned long data = atomic_long_read(&work->data); 9057c3eed5cSTejun Heo 906112202d9STejun Heo if (data & WORK_STRUCT_PWQ) 907afa4bb77SLinus Torvalds return work_struct_pwq(data)->pool->id; 90854d5b7d0SLai Jiangshan 90954d5b7d0SLai Jiangshan return data >> WORK_OFFQ_POOL_SHIFT; 9107c3eed5cSTejun Heo } 9117c3eed5cSTejun Heo 912bbb68dfaSTejun Heo static void mark_work_canceling(struct work_struct *work) 913bbb68dfaSTejun Heo { 9147c3eed5cSTejun Heo unsigned long pool_id = get_work_pool_id(work); 915bbb68dfaSTejun Heo 9167c3eed5cSTejun Heo pool_id <<= WORK_OFFQ_POOL_SHIFT; 917bccdc1faSTejun Heo set_work_data(work, pool_id | WORK_STRUCT_PENDING | WORK_OFFQ_CANCELING); 918bbb68dfaSTejun Heo } 919bbb68dfaSTejun Heo 920bbb68dfaSTejun Heo static bool work_is_canceling(struct work_struct *work) 921bbb68dfaSTejun Heo { 922bbb68dfaSTejun Heo unsigned long data = atomic_long_read(&work->data); 923bbb68dfaSTejun Heo 924112202d9STejun Heo return !(data & WORK_STRUCT_PWQ) && (data & WORK_OFFQ_CANCELING); 925bbb68dfaSTejun Heo } 926bbb68dfaSTejun Heo 927e22bee78STejun Heo /* 9283270476aSTejun Heo * Policy functions. These define the policies on how the global worker 9293270476aSTejun Heo * pools are managed. Unless noted otherwise, these functions assume that 930d565ed63STejun Heo * they're being called with pool->lock held. 931e22bee78STejun Heo */ 932e22bee78STejun Heo 933e22bee78STejun Heo /* 934e22bee78STejun Heo * Need to wake up a worker? Called from anything but currently 935e22bee78STejun Heo * running workers. 936974271c4STejun Heo * 937974271c4STejun Heo * Note that, because unbound workers never contribute to nr_running, this 938706026c2STejun Heo * function will always return %true for unbound pools as long as the 939974271c4STejun Heo * worklist isn't empty. 940e22bee78STejun Heo */ 94163d95a91STejun Heo static bool need_more_worker(struct worker_pool *pool) 942e22bee78STejun Heo { 9430219a352STejun Heo return !list_empty(&pool->worklist) && !pool->nr_running; 944e22bee78STejun Heo } 945e22bee78STejun Heo 946e22bee78STejun Heo /* Can I start working? Called from busy but !running workers. */ 94763d95a91STejun Heo static bool may_start_working(struct worker_pool *pool) 948e22bee78STejun Heo { 94963d95a91STejun Heo return pool->nr_idle; 950e22bee78STejun Heo } 951e22bee78STejun Heo 952e22bee78STejun Heo /* Do I need to keep working? Called from currently running workers. */ 95363d95a91STejun Heo static bool keep_working(struct worker_pool *pool) 954e22bee78STejun Heo { 955bc35f7efSLai Jiangshan return !list_empty(&pool->worklist) && (pool->nr_running <= 1); 956e22bee78STejun Heo } 957e22bee78STejun Heo 958e22bee78STejun Heo /* Do we need a new worker? Called from manager. */ 95963d95a91STejun Heo static bool need_to_create_worker(struct worker_pool *pool) 960e22bee78STejun Heo { 96163d95a91STejun Heo return need_more_worker(pool) && !may_start_working(pool); 962e22bee78STejun Heo } 963e22bee78STejun Heo 964e22bee78STejun Heo /* Do we have too many workers and should some go away? */ 96563d95a91STejun Heo static bool too_many_workers(struct worker_pool *pool) 966e22bee78STejun Heo { 967692b4825STejun Heo bool managing = pool->flags & POOL_MANAGER_ACTIVE; 96863d95a91STejun Heo int nr_idle = pool->nr_idle + managing; /* manager is considered idle */ 96963d95a91STejun Heo int nr_busy = pool->nr_workers - nr_idle; 970e22bee78STejun Heo 971e22bee78STejun Heo return nr_idle > 2 && (nr_idle - 2) * MAX_IDLE_WORKERS_RATIO >= nr_busy; 972e22bee78STejun Heo } 973e22bee78STejun Heo 9744690c4abSTejun Heo /** 975e22bee78STejun Heo * worker_set_flags - set worker flags and adjust nr_running accordingly 976cb444766STejun Heo * @worker: self 977d302f017STejun Heo * @flags: flags to set 978d302f017STejun Heo * 979228f1d00SLai Jiangshan * Set @flags in @worker->flags and adjust nr_running accordingly. 980d302f017STejun Heo */ 981228f1d00SLai Jiangshan static inline void worker_set_flags(struct worker *worker, unsigned int flags) 982d302f017STejun Heo { 983bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 984e22bee78STejun Heo 985bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 986cb444766STejun Heo 987228f1d00SLai Jiangshan /* If transitioning into NOT_RUNNING, adjust nr_running. */ 988e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && 989e22bee78STejun Heo !(worker->flags & WORKER_NOT_RUNNING)) { 990bc35f7efSLai Jiangshan pool->nr_running--; 991e22bee78STejun Heo } 992e22bee78STejun Heo 993d302f017STejun Heo worker->flags |= flags; 994d302f017STejun Heo } 995d302f017STejun Heo 996d302f017STejun Heo /** 997e22bee78STejun Heo * worker_clr_flags - clear worker flags and adjust nr_running accordingly 998cb444766STejun Heo * @worker: self 999d302f017STejun Heo * @flags: flags to clear 1000d302f017STejun Heo * 1001e22bee78STejun Heo * Clear @flags in @worker->flags and adjust nr_running accordingly. 1002d302f017STejun Heo */ 1003d302f017STejun Heo static inline void worker_clr_flags(struct worker *worker, unsigned int flags) 1004d302f017STejun Heo { 100563d95a91STejun Heo struct worker_pool *pool = worker->pool; 1006e22bee78STejun Heo unsigned int oflags = worker->flags; 1007e22bee78STejun Heo 1008bc8b50c2STejun Heo lockdep_assert_held(&pool->lock); 1009cb444766STejun Heo 1010d302f017STejun Heo worker->flags &= ~flags; 1011e22bee78STejun Heo 101242c025f3STejun Heo /* 101342c025f3STejun Heo * If transitioning out of NOT_RUNNING, increment nr_running. Note 101442c025f3STejun Heo * that the nested NOT_RUNNING is not a noop. NOT_RUNNING is mask 101542c025f3STejun Heo * of multiple flags, not a single flag. 101642c025f3STejun Heo */ 1017e22bee78STejun Heo if ((flags & WORKER_NOT_RUNNING) && (oflags & WORKER_NOT_RUNNING)) 1018e22bee78STejun Heo if (!(worker->flags & WORKER_NOT_RUNNING)) 1019bc35f7efSLai Jiangshan pool->nr_running++; 1020d302f017STejun Heo } 1021d302f017STejun Heo 1022797e8345STejun Heo /* Return the first idle worker. Called with pool->lock held. */ 1023797e8345STejun Heo static struct worker *first_idle_worker(struct worker_pool *pool) 1024797e8345STejun Heo { 1025797e8345STejun Heo if (unlikely(list_empty(&pool->idle_list))) 1026797e8345STejun Heo return NULL; 1027797e8345STejun Heo 1028797e8345STejun Heo return list_first_entry(&pool->idle_list, struct worker, entry); 1029797e8345STejun Heo } 1030797e8345STejun Heo 1031797e8345STejun Heo /** 1032797e8345STejun Heo * worker_enter_idle - enter idle state 1033797e8345STejun Heo * @worker: worker which is entering idle state 1034797e8345STejun Heo * 1035797e8345STejun Heo * @worker is entering idle state. Update stats and idle timer if 1036797e8345STejun Heo * necessary. 1037797e8345STejun Heo * 1038797e8345STejun Heo * LOCKING: 1039797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1040797e8345STejun Heo */ 1041797e8345STejun Heo static void worker_enter_idle(struct worker *worker) 1042797e8345STejun Heo { 1043797e8345STejun Heo struct worker_pool *pool = worker->pool; 1044797e8345STejun Heo 1045797e8345STejun Heo if (WARN_ON_ONCE(worker->flags & WORKER_IDLE) || 1046797e8345STejun Heo WARN_ON_ONCE(!list_empty(&worker->entry) && 1047797e8345STejun Heo (worker->hentry.next || worker->hentry.pprev))) 1048797e8345STejun Heo return; 1049797e8345STejun Heo 1050797e8345STejun Heo /* can't use worker_set_flags(), also called from create_worker() */ 1051797e8345STejun Heo worker->flags |= WORKER_IDLE; 1052797e8345STejun Heo pool->nr_idle++; 1053797e8345STejun Heo worker->last_active = jiffies; 1054797e8345STejun Heo 1055797e8345STejun Heo /* idle_list is LIFO */ 1056797e8345STejun Heo list_add(&worker->entry, &pool->idle_list); 1057797e8345STejun Heo 1058797e8345STejun Heo if (too_many_workers(pool) && !timer_pending(&pool->idle_timer)) 1059797e8345STejun Heo mod_timer(&pool->idle_timer, jiffies + IDLE_WORKER_TIMEOUT); 1060797e8345STejun Heo 1061797e8345STejun Heo /* Sanity check nr_running. */ 1062797e8345STejun Heo WARN_ON_ONCE(pool->nr_workers == pool->nr_idle && pool->nr_running); 1063797e8345STejun Heo } 1064797e8345STejun Heo 1065797e8345STejun Heo /** 1066797e8345STejun Heo * worker_leave_idle - leave idle state 1067797e8345STejun Heo * @worker: worker which is leaving idle state 1068797e8345STejun Heo * 1069797e8345STejun Heo * @worker is leaving idle state. Update stats. 1070797e8345STejun Heo * 1071797e8345STejun Heo * LOCKING: 1072797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1073797e8345STejun Heo */ 1074797e8345STejun Heo static void worker_leave_idle(struct worker *worker) 1075797e8345STejun Heo { 1076797e8345STejun Heo struct worker_pool *pool = worker->pool; 1077797e8345STejun Heo 1078797e8345STejun Heo if (WARN_ON_ONCE(!(worker->flags & WORKER_IDLE))) 1079797e8345STejun Heo return; 1080797e8345STejun Heo worker_clr_flags(worker, WORKER_IDLE); 1081797e8345STejun Heo pool->nr_idle--; 1082797e8345STejun Heo list_del_init(&worker->entry); 1083797e8345STejun Heo } 1084797e8345STejun Heo 1085797e8345STejun Heo /** 1086797e8345STejun Heo * find_worker_executing_work - find worker which is executing a work 1087797e8345STejun Heo * @pool: pool of interest 1088797e8345STejun Heo * @work: work to find worker for 1089797e8345STejun Heo * 1090797e8345STejun Heo * Find a worker which is executing @work on @pool by searching 1091797e8345STejun Heo * @pool->busy_hash which is keyed by the address of @work. For a worker 1092797e8345STejun Heo * to match, its current execution should match the address of @work and 1093797e8345STejun Heo * its work function. This is to avoid unwanted dependency between 1094797e8345STejun Heo * unrelated work executions through a work item being recycled while still 1095797e8345STejun Heo * being executed. 1096797e8345STejun Heo * 1097797e8345STejun Heo * This is a bit tricky. A work item may be freed once its execution 1098797e8345STejun Heo * starts and nothing prevents the freed area from being recycled for 1099797e8345STejun Heo * another work item. If the same work item address ends up being reused 1100797e8345STejun Heo * before the original execution finishes, workqueue will identify the 1101797e8345STejun Heo * recycled work item as currently executing and make it wait until the 1102797e8345STejun Heo * current execution finishes, introducing an unwanted dependency. 1103797e8345STejun Heo * 1104797e8345STejun Heo * This function checks the work item address and work function to avoid 1105797e8345STejun Heo * false positives. Note that this isn't complete as one may construct a 1106797e8345STejun Heo * work function which can introduce dependency onto itself through a 1107797e8345STejun Heo * recycled work item. Well, if somebody wants to shoot oneself in the 1108797e8345STejun Heo * foot that badly, there's only so much we can do, and if such deadlock 1109797e8345STejun Heo * actually occurs, it should be easy to locate the culprit work function. 1110797e8345STejun Heo * 1111797e8345STejun Heo * CONTEXT: 1112797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1113797e8345STejun Heo * 1114797e8345STejun Heo * Return: 1115797e8345STejun Heo * Pointer to worker which is executing @work if found, %NULL 1116797e8345STejun Heo * otherwise. 1117797e8345STejun Heo */ 1118797e8345STejun Heo static struct worker *find_worker_executing_work(struct worker_pool *pool, 1119797e8345STejun Heo struct work_struct *work) 1120797e8345STejun Heo { 1121797e8345STejun Heo struct worker *worker; 1122797e8345STejun Heo 1123797e8345STejun Heo hash_for_each_possible(pool->busy_hash, worker, hentry, 1124797e8345STejun Heo (unsigned long)work) 1125797e8345STejun Heo if (worker->current_work == work && 1126797e8345STejun Heo worker->current_func == work->func) 1127797e8345STejun Heo return worker; 1128797e8345STejun Heo 1129797e8345STejun Heo return NULL; 1130797e8345STejun Heo } 1131797e8345STejun Heo 1132797e8345STejun Heo /** 1133797e8345STejun Heo * move_linked_works - move linked works to a list 1134797e8345STejun Heo * @work: start of series of works to be scheduled 1135797e8345STejun Heo * @head: target list to append @work to 1136797e8345STejun Heo * @nextp: out parameter for nested worklist walking 1137797e8345STejun Heo * 1138873eaca6STejun Heo * Schedule linked works starting from @work to @head. Work series to be 1139873eaca6STejun Heo * scheduled starts at @work and includes any consecutive work with 1140873eaca6STejun Heo * WORK_STRUCT_LINKED set in its predecessor. See assign_work() for details on 1141873eaca6STejun Heo * @nextp. 1142797e8345STejun Heo * 1143797e8345STejun Heo * CONTEXT: 1144797e8345STejun Heo * raw_spin_lock_irq(pool->lock). 1145797e8345STejun Heo */ 1146797e8345STejun Heo static void move_linked_works(struct work_struct *work, struct list_head *head, 1147797e8345STejun Heo struct work_struct **nextp) 1148797e8345STejun Heo { 1149797e8345STejun Heo struct work_struct *n; 1150797e8345STejun Heo 1151797e8345STejun Heo /* 1152797e8345STejun Heo * Linked worklist will always end before the end of the list, 1153797e8345STejun Heo * use NULL for list head. 1154797e8345STejun Heo */ 1155797e8345STejun Heo list_for_each_entry_safe_from(work, n, NULL, entry) { 1156797e8345STejun Heo list_move_tail(&work->entry, head); 1157797e8345STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_LINKED)) 1158797e8345STejun Heo break; 1159797e8345STejun Heo } 1160797e8345STejun Heo 1161797e8345STejun Heo /* 1162797e8345STejun Heo * If we're already inside safe list traversal and have moved 1163797e8345STejun Heo * multiple works to the scheduled queue, the next position 1164797e8345STejun Heo * needs to be updated. 1165797e8345STejun Heo */ 1166797e8345STejun Heo if (nextp) 1167797e8345STejun Heo *nextp = n; 1168797e8345STejun Heo } 1169797e8345STejun Heo 1170797e8345STejun Heo /** 1171873eaca6STejun Heo * assign_work - assign a work item and its linked work items to a worker 1172873eaca6STejun Heo * @work: work to assign 1173873eaca6STejun Heo * @worker: worker to assign to 1174873eaca6STejun Heo * @nextp: out parameter for nested worklist walking 1175873eaca6STejun Heo * 1176873eaca6STejun Heo * Assign @work and its linked work items to @worker. If @work is already being 1177873eaca6STejun Heo * executed by another worker in the same pool, it'll be punted there. 1178873eaca6STejun Heo * 1179873eaca6STejun Heo * If @nextp is not NULL, it's updated to point to the next work of the last 1180873eaca6STejun Heo * scheduled work. This allows assign_work() to be nested inside 1181873eaca6STejun Heo * list_for_each_entry_safe(). 1182873eaca6STejun Heo * 1183873eaca6STejun Heo * Returns %true if @work was successfully assigned to @worker. %false if @work 1184873eaca6STejun Heo * was punted to another worker already executing it. 1185873eaca6STejun Heo */ 1186873eaca6STejun Heo static bool assign_work(struct work_struct *work, struct worker *worker, 1187873eaca6STejun Heo struct work_struct **nextp) 1188873eaca6STejun Heo { 1189873eaca6STejun Heo struct worker_pool *pool = worker->pool; 1190873eaca6STejun Heo struct worker *collision; 1191873eaca6STejun Heo 1192873eaca6STejun Heo lockdep_assert_held(&pool->lock); 1193873eaca6STejun Heo 1194873eaca6STejun Heo /* 1195873eaca6STejun Heo * A single work shouldn't be executed concurrently by multiple workers. 1196873eaca6STejun Heo * __queue_work() ensures that @work doesn't jump to a different pool 1197873eaca6STejun Heo * while still running in the previous pool. Here, we should ensure that 1198873eaca6STejun Heo * @work is not executed concurrently by multiple workers from the same 1199873eaca6STejun Heo * pool. Check whether anyone is already processing the work. If so, 1200873eaca6STejun Heo * defer the work to the currently executing one. 1201873eaca6STejun Heo */ 1202873eaca6STejun Heo collision = find_worker_executing_work(pool, work); 1203873eaca6STejun Heo if (unlikely(collision)) { 1204873eaca6STejun Heo move_linked_works(work, &collision->scheduled, nextp); 1205873eaca6STejun Heo return false; 1206873eaca6STejun Heo } 1207873eaca6STejun Heo 1208873eaca6STejun Heo move_linked_works(work, &worker->scheduled, nextp); 1209873eaca6STejun Heo return true; 1210873eaca6STejun Heo } 1211873eaca6STejun Heo 12122f34d733STejun Heo static struct irq_work *bh_pool_irq_work(struct worker_pool *pool) 12132f34d733STejun Heo { 12142f34d733STejun Heo int high = pool->attrs->nice == HIGHPRI_NICE_LEVEL ? 1 : 0; 12152f34d733STejun Heo 12162f34d733STejun Heo return &per_cpu(bh_pool_irq_works, pool->cpu)[high]; 12172f34d733STejun Heo } 12182f34d733STejun Heo 1219fd0a68a2STejun Heo static void kick_bh_pool(struct worker_pool *pool) 1220fd0a68a2STejun Heo { 1221fd0a68a2STejun Heo #ifdef CONFIG_SMP 12221acd92d9STejun Heo /* see drain_dead_softirq_workfn() for BH_DRAINING */ 12231acd92d9STejun Heo if (unlikely(pool->cpu != smp_processor_id() && 12241acd92d9STejun Heo !(pool->flags & POOL_BH_DRAINING))) { 1225fd0a68a2STejun Heo irq_work_queue_on(bh_pool_irq_work(pool), pool->cpu); 1226fd0a68a2STejun Heo return; 1227fd0a68a2STejun Heo } 1228fd0a68a2STejun Heo #endif 1229fd0a68a2STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 1230fd0a68a2STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 1231fd0a68a2STejun Heo else 1232fd0a68a2STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 1233fd0a68a2STejun Heo } 1234fd0a68a2STejun Heo 1235873eaca6STejun Heo /** 12360219a352STejun Heo * kick_pool - wake up an idle worker if necessary 12370219a352STejun Heo * @pool: pool to kick 1238797e8345STejun Heo * 12390219a352STejun Heo * @pool may have pending work items. Wake up worker if necessary. Returns 12400219a352STejun Heo * whether a worker was woken up. 1241797e8345STejun Heo */ 12420219a352STejun Heo static bool kick_pool(struct worker_pool *pool) 1243797e8345STejun Heo { 1244797e8345STejun Heo struct worker *worker = first_idle_worker(pool); 12458639ecebSTejun Heo struct task_struct *p; 1246797e8345STejun Heo 12470219a352STejun Heo lockdep_assert_held(&pool->lock); 12480219a352STejun Heo 12490219a352STejun Heo if (!need_more_worker(pool) || !worker) 12500219a352STejun Heo return false; 12510219a352STejun Heo 12524cb1ef64STejun Heo if (pool->flags & POOL_BH) { 1253fd0a68a2STejun Heo kick_bh_pool(pool); 12544cb1ef64STejun Heo return true; 12554cb1ef64STejun Heo } 12564cb1ef64STejun Heo 12578639ecebSTejun Heo p = worker->task; 12588639ecebSTejun Heo 12598639ecebSTejun Heo #ifdef CONFIG_SMP 12608639ecebSTejun Heo /* 12618639ecebSTejun Heo * Idle @worker is about to execute @work and waking up provides an 12628639ecebSTejun Heo * opportunity to migrate @worker at a lower cost by setting the task's 12638639ecebSTejun Heo * wake_cpu field. Let's see if we want to move @worker to improve 12648639ecebSTejun Heo * execution locality. 12658639ecebSTejun Heo * 12668639ecebSTejun Heo * We're waking the worker that went idle the latest and there's some 12678639ecebSTejun Heo * chance that @worker is marked idle but hasn't gone off CPU yet. If 12688639ecebSTejun Heo * so, setting the wake_cpu won't do anything. As this is a best-effort 12698639ecebSTejun Heo * optimization and the race window is narrow, let's leave as-is for 12708639ecebSTejun Heo * now. If this becomes pronounced, we can skip over workers which are 12718639ecebSTejun Heo * still on cpu when picking an idle worker. 12728639ecebSTejun Heo * 12738639ecebSTejun Heo * If @pool has non-strict affinity, @worker might have ended up outside 12748639ecebSTejun Heo * its affinity scope. Repatriate. 12758639ecebSTejun Heo */ 12768639ecebSTejun Heo if (!pool->attrs->affn_strict && 12778639ecebSTejun Heo !cpumask_test_cpu(p->wake_cpu, pool->attrs->__pod_cpumask)) { 12788639ecebSTejun Heo struct work_struct *work = list_first_entry(&pool->worklist, 12798639ecebSTejun Heo struct work_struct, entry); 1280*57a01eafSSven Schnelle int wake_cpu = cpumask_any_and_distribute(pool->attrs->__pod_cpumask, 1281*57a01eafSSven Schnelle cpu_online_mask); 1282*57a01eafSSven Schnelle if (wake_cpu < nr_cpu_ids) { 1283*57a01eafSSven Schnelle p->wake_cpu = wake_cpu; 12848639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12858639ecebSTejun Heo } 1286*57a01eafSSven Schnelle } 12878639ecebSTejun Heo #endif 12888639ecebSTejun Heo wake_up_process(p); 12890219a352STejun Heo return true; 1290797e8345STejun Heo } 1291797e8345STejun Heo 129263638450STejun Heo #ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT 129363638450STejun Heo 129463638450STejun Heo /* 129563638450STejun Heo * Concurrency-managed per-cpu work items that hog CPU for longer than 129663638450STejun Heo * wq_cpu_intensive_thresh_us trigger the automatic CPU_INTENSIVE mechanism, 129763638450STejun Heo * which prevents them from stalling other concurrency-managed work items. If a 129863638450STejun Heo * work function keeps triggering this mechanism, it's likely that the work item 129963638450STejun Heo * should be using an unbound workqueue instead. 130063638450STejun Heo * 130163638450STejun Heo * wq_cpu_intensive_report() tracks work functions which trigger such conditions 130263638450STejun Heo * and report them so that they can be examined and converted to use unbound 130363638450STejun Heo * workqueues as appropriate. To avoid flooding the console, each violating work 130463638450STejun Heo * function is tracked and reported with exponential backoff. 130563638450STejun Heo */ 130663638450STejun Heo #define WCI_MAX_ENTS 128 130763638450STejun Heo 130863638450STejun Heo struct wci_ent { 130963638450STejun Heo work_func_t func; 131063638450STejun Heo atomic64_t cnt; 131163638450STejun Heo struct hlist_node hash_node; 131263638450STejun Heo }; 131363638450STejun Heo 131463638450STejun Heo static struct wci_ent wci_ents[WCI_MAX_ENTS]; 131563638450STejun Heo static int wci_nr_ents; 131663638450STejun Heo static DEFINE_RAW_SPINLOCK(wci_lock); 131763638450STejun Heo static DEFINE_HASHTABLE(wci_hash, ilog2(WCI_MAX_ENTS)); 131863638450STejun Heo 131963638450STejun Heo static struct wci_ent *wci_find_ent(work_func_t func) 132063638450STejun Heo { 132163638450STejun Heo struct wci_ent *ent; 132263638450STejun Heo 132363638450STejun Heo hash_for_each_possible_rcu(wci_hash, ent, hash_node, 132463638450STejun Heo (unsigned long)func) { 132563638450STejun Heo if (ent->func == func) 132663638450STejun Heo return ent; 132763638450STejun Heo } 132863638450STejun Heo return NULL; 132963638450STejun Heo } 133063638450STejun Heo 133163638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) 133263638450STejun Heo { 133363638450STejun Heo struct wci_ent *ent; 133463638450STejun Heo 133563638450STejun Heo restart: 133663638450STejun Heo ent = wci_find_ent(func); 133763638450STejun Heo if (ent) { 133863638450STejun Heo u64 cnt; 133963638450STejun Heo 134063638450STejun Heo /* 1341ccdec921SXuewen Yan * Start reporting from the warning_thresh and back off 134263638450STejun Heo * exponentially. 134363638450STejun Heo */ 134463638450STejun Heo cnt = atomic64_inc_return_relaxed(&ent->cnt); 1345ccdec921SXuewen Yan if (wq_cpu_intensive_warning_thresh && 1346ccdec921SXuewen Yan cnt >= wq_cpu_intensive_warning_thresh && 1347ccdec921SXuewen Yan is_power_of_2(cnt + 1 - wq_cpu_intensive_warning_thresh)) 134863638450STejun Heo printk_deferred(KERN_WARNING "workqueue: %ps hogged CPU for >%luus %llu times, consider switching to WQ_UNBOUND\n", 134963638450STejun Heo ent->func, wq_cpu_intensive_thresh_us, 135063638450STejun Heo atomic64_read(&ent->cnt)); 135163638450STejun Heo return; 135263638450STejun Heo } 135363638450STejun Heo 135463638450STejun Heo /* 135563638450STejun Heo * @func is a new violation. Allocate a new entry for it. If wcn_ents[] 135663638450STejun Heo * is exhausted, something went really wrong and we probably made enough 135763638450STejun Heo * noise already. 135863638450STejun Heo */ 135963638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) 136063638450STejun Heo return; 136163638450STejun Heo 136263638450STejun Heo raw_spin_lock(&wci_lock); 136363638450STejun Heo 136463638450STejun Heo if (wci_nr_ents >= WCI_MAX_ENTS) { 136563638450STejun Heo raw_spin_unlock(&wci_lock); 136663638450STejun Heo return; 136763638450STejun Heo } 136863638450STejun Heo 136963638450STejun Heo if (wci_find_ent(func)) { 137063638450STejun Heo raw_spin_unlock(&wci_lock); 137163638450STejun Heo goto restart; 137263638450STejun Heo } 137363638450STejun Heo 137463638450STejun Heo ent = &wci_ents[wci_nr_ents++]; 137563638450STejun Heo ent->func = func; 1376ccdec921SXuewen Yan atomic64_set(&ent->cnt, 0); 137763638450STejun Heo hash_add_rcu(wci_hash, &ent->hash_node, (unsigned long)func); 137863638450STejun Heo 137963638450STejun Heo raw_spin_unlock(&wci_lock); 1380ccdec921SXuewen Yan 1381ccdec921SXuewen Yan goto restart; 138263638450STejun Heo } 138363638450STejun Heo 138463638450STejun Heo #else /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 138563638450STejun Heo static void wq_cpu_intensive_report(work_func_t func) {} 138663638450STejun Heo #endif /* CONFIG_WQ_CPU_INTENSIVE_REPORT */ 138763638450STejun Heo 1388c54d5046STejun Heo /** 13891da177e4SLinus Torvalds * wq_worker_running - a worker is running again 13901da177e4SLinus Torvalds * @task: task waking up 13911da177e4SLinus Torvalds * 13921da177e4SLinus Torvalds * This function is called when a worker returns from schedule() 13931da177e4SLinus Torvalds */ 13941da177e4SLinus Torvalds void wq_worker_running(struct task_struct *task) 13951da177e4SLinus Torvalds { 13961da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 13971da177e4SLinus Torvalds 1398c8f6219bSZqiang if (!READ_ONCE(worker->sleeping)) 13991da177e4SLinus Torvalds return; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /* 14021da177e4SLinus Torvalds * If preempted by unbind_workers() between the WORKER_NOT_RUNNING check 14031da177e4SLinus Torvalds * and the nr_running increment below, we may ruin the nr_running reset 14041da177e4SLinus Torvalds * and leave with an unexpected pool->nr_running == 1 on the newly unbound 14051da177e4SLinus Torvalds * pool. Protect against such race. 14061da177e4SLinus Torvalds */ 14071da177e4SLinus Torvalds preempt_disable(); 14081da177e4SLinus Torvalds if (!(worker->flags & WORKER_NOT_RUNNING)) 14091da177e4SLinus Torvalds worker->pool->nr_running++; 14101da177e4SLinus Torvalds preempt_enable(); 1411616db877STejun Heo 1412616db877STejun Heo /* 1413616db877STejun Heo * CPU intensive auto-detection cares about how long a work item hogged 1414616db877STejun Heo * CPU without sleeping. Reset the starting timestamp on wakeup. 1415616db877STejun Heo */ 1416616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 1417616db877STejun Heo 1418c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 0); 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14211da177e4SLinus Torvalds /** 14221da177e4SLinus Torvalds * wq_worker_sleeping - a worker is going to sleep 14231da177e4SLinus Torvalds * @task: task going to sleep 14241da177e4SLinus Torvalds * 14251da177e4SLinus Torvalds * This function is called from schedule() when a busy worker is 14261da177e4SLinus Torvalds * going to sleep. 14271da177e4SLinus Torvalds */ 14281da177e4SLinus Torvalds void wq_worker_sleeping(struct task_struct *task) 14291da177e4SLinus Torvalds { 14301da177e4SLinus Torvalds struct worker *worker = kthread_data(task); 14311da177e4SLinus Torvalds struct worker_pool *pool; 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds /* 14341da177e4SLinus Torvalds * Rescuers, which may not have all the fields set up like normal 14351da177e4SLinus Torvalds * workers, also reach here, let's not access anything before 14361da177e4SLinus Torvalds * checking NOT_RUNNING. 14371da177e4SLinus Torvalds */ 14381da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) 14391da177e4SLinus Torvalds return; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds pool = worker->pool; 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /* Return if preempted before wq_worker_running() was reached */ 1444c8f6219bSZqiang if (READ_ONCE(worker->sleeping)) 14451da177e4SLinus Torvalds return; 14461da177e4SLinus Torvalds 1447c8f6219bSZqiang WRITE_ONCE(worker->sleeping, 1); 14481da177e4SLinus Torvalds raw_spin_lock_irq(&pool->lock); 14491da177e4SLinus Torvalds 14501da177e4SLinus Torvalds /* 14511da177e4SLinus Torvalds * Recheck in case unbind_workers() preempted us. We don't 14521da177e4SLinus Torvalds * want to decrement nr_running after the worker is unbound 14531da177e4SLinus Torvalds * and nr_running has been reset. 14541da177e4SLinus Torvalds */ 14551da177e4SLinus Torvalds if (worker->flags & WORKER_NOT_RUNNING) { 14561da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14571da177e4SLinus Torvalds return; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds pool->nr_running--; 14610219a352STejun Heo if (kick_pool(pool)) 1462725e8ec5STejun Heo worker->current_pwq->stats[PWQ_STAT_CM_WAKEUP]++; 14630219a352STejun Heo 14641da177e4SLinus Torvalds raw_spin_unlock_irq(&pool->lock); 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds /** 1468616db877STejun Heo * wq_worker_tick - a scheduler tick occurred while a kworker is running 1469616db877STejun Heo * @task: task currently running 1470616db877STejun Heo * 1471616db877STejun Heo * Called from scheduler_tick(). We're in the IRQ context and the current 1472616db877STejun Heo * worker's fields which follow the 'K' locking rule can be accessed safely. 1473616db877STejun Heo */ 1474616db877STejun Heo void wq_worker_tick(struct task_struct *task) 1475616db877STejun Heo { 1476616db877STejun Heo struct worker *worker = kthread_data(task); 1477616db877STejun Heo struct pool_workqueue *pwq = worker->current_pwq; 1478616db877STejun Heo struct worker_pool *pool = worker->pool; 1479616db877STejun Heo 1480616db877STejun Heo if (!pwq) 1481616db877STejun Heo return; 1482616db877STejun Heo 14838a1dd1e5STejun Heo pwq->stats[PWQ_STAT_CPU_TIME] += TICK_USEC; 14848a1dd1e5STejun Heo 148518c8ae81SZqiang if (!wq_cpu_intensive_thresh_us) 148618c8ae81SZqiang return; 148718c8ae81SZqiang 1488616db877STejun Heo /* 1489616db877STejun Heo * If the current worker is concurrency managed and hogged the CPU for 1490616db877STejun Heo * longer than wq_cpu_intensive_thresh_us, it's automatically marked 1491616db877STejun Heo * CPU_INTENSIVE to avoid stalling other concurrency-managed work items. 1492c8f6219bSZqiang * 1493c8f6219bSZqiang * Set @worker->sleeping means that @worker is in the process of 1494c8f6219bSZqiang * switching out voluntarily and won't be contributing to 1495c8f6219bSZqiang * @pool->nr_running until it wakes up. As wq_worker_sleeping() also 1496c8f6219bSZqiang * decrements ->nr_running, setting CPU_INTENSIVE here can lead to 1497c8f6219bSZqiang * double decrements. The task is releasing the CPU anyway. Let's skip. 1498c8f6219bSZqiang * We probably want to make this prettier in the future. 1499616db877STejun Heo */ 1500c8f6219bSZqiang if ((worker->flags & WORKER_NOT_RUNNING) || READ_ONCE(worker->sleeping) || 1501616db877STejun Heo worker->task->se.sum_exec_runtime - worker->current_at < 1502616db877STejun Heo wq_cpu_intensive_thresh_us * NSEC_PER_USEC) 1503616db877STejun Heo return; 1504616db877STejun Heo 1505616db877STejun Heo raw_spin_lock(&pool->lock); 1506616db877STejun Heo 1507616db877STejun Heo worker_set_flags(worker, WORKER_CPU_INTENSIVE); 150863638450STejun Heo wq_cpu_intensive_report(worker->current_func); 1509616db877STejun Heo pwq->stats[PWQ_STAT_CPU_INTENSIVE]++; 1510616db877STejun Heo 15110219a352STejun Heo if (kick_pool(pool)) 1512616db877STejun Heo pwq->stats[PWQ_STAT_CM_WAKEUP]++; 1513616db877STejun Heo 1514616db877STejun Heo raw_spin_unlock(&pool->lock); 1515616db877STejun Heo } 1516616db877STejun Heo 1517616db877STejun Heo /** 15181da177e4SLinus Torvalds * wq_worker_last_func - retrieve worker's last work function 15191da177e4SLinus Torvalds * @task: Task to retrieve last work function of. 15201da177e4SLinus Torvalds * 15211da177e4SLinus Torvalds * Determine the last function a worker executed. This is called from 15221da177e4SLinus Torvalds * the scheduler to get a worker's last known identity. 15231da177e4SLinus Torvalds * 15249b41ea72SAndrew Morton * CONTEXT: 15251da177e4SLinus Torvalds * raw_spin_lock_irq(rq->lock) 15261da177e4SLinus Torvalds * 1527f756d5e2SNathan Lynch * This function is called during schedule() when a kworker is going 1528f756d5e2SNathan Lynch * to sleep. It's used by psi to identify aggregation workers during 15291da177e4SLinus Torvalds * dequeuing, to allow periodic aggregation to shut-off when that 15301da177e4SLinus Torvalds * worker is the last task in the system or cgroup to go to sleep. 15311da177e4SLinus Torvalds * 15321da177e4SLinus Torvalds * As this function doesn't involve any workqueue-related locking, it 15331da177e4SLinus Torvalds * only returns stable values when called from inside the scheduler's 15341da177e4SLinus Torvalds * queuing and dequeuing paths, when @task, which must be a kworker, 1535365970a1SDavid Howells * is guaranteed to not be processing any works. 1536365970a1SDavid Howells * 1537365970a1SDavid Howells * Return: 1538365970a1SDavid Howells * The last work function %current executed as a worker, NULL if it 1539365970a1SDavid Howells * hasn't executed any work yet. 1540365970a1SDavid Howells */ 1541365970a1SDavid Howells work_func_t wq_worker_last_func(struct task_struct *task) 1542365970a1SDavid Howells { 1543365970a1SDavid Howells struct worker *worker = kthread_data(task); 1544365970a1SDavid Howells 1545365970a1SDavid Howells return worker->last_func; 1546365970a1SDavid Howells } 1547365970a1SDavid Howells 1548d302f017STejun Heo /** 154991ccc6e7STejun Heo * wq_node_nr_active - Determine wq_node_nr_active to use 155091ccc6e7STejun Heo * @wq: workqueue of interest 155191ccc6e7STejun Heo * @node: NUMA node, can be %NUMA_NO_NODE 155291ccc6e7STejun Heo * 155391ccc6e7STejun Heo * Determine wq_node_nr_active to use for @wq on @node. Returns: 155491ccc6e7STejun Heo * 155591ccc6e7STejun Heo * - %NULL for per-cpu workqueues as they don't need to use shared nr_active. 155691ccc6e7STejun Heo * 155791ccc6e7STejun Heo * - node_nr_active[nr_node_ids] if @node is %NUMA_NO_NODE. 155891ccc6e7STejun Heo * 155991ccc6e7STejun Heo * - Otherwise, node_nr_active[@node]. 156091ccc6e7STejun Heo */ 156191ccc6e7STejun Heo static struct wq_node_nr_active *wq_node_nr_active(struct workqueue_struct *wq, 156291ccc6e7STejun Heo int node) 156391ccc6e7STejun Heo { 156491ccc6e7STejun Heo if (!(wq->flags & WQ_UNBOUND)) 156591ccc6e7STejun Heo return NULL; 156691ccc6e7STejun Heo 156791ccc6e7STejun Heo if (node == NUMA_NO_NODE) 156891ccc6e7STejun Heo node = nr_node_ids; 156991ccc6e7STejun Heo 157091ccc6e7STejun Heo return wq->node_nr_active[node]; 157191ccc6e7STejun Heo } 157291ccc6e7STejun Heo 157391ccc6e7STejun Heo /** 15745797b1c1STejun Heo * wq_update_node_max_active - Update per-node max_actives to use 15755797b1c1STejun Heo * @wq: workqueue to update 15765797b1c1STejun Heo * @off_cpu: CPU that's going down, -1 if a CPU is not going down 15775797b1c1STejun Heo * 15785797b1c1STejun Heo * Update @wq->node_nr_active[]->max. @wq must be unbound. max_active is 15795797b1c1STejun Heo * distributed among nodes according to the proportions of numbers of online 15805797b1c1STejun Heo * cpus. The result is always between @wq->min_active and max_active. 15815797b1c1STejun Heo */ 15825797b1c1STejun Heo static void wq_update_node_max_active(struct workqueue_struct *wq, int off_cpu) 15835797b1c1STejun Heo { 15845797b1c1STejun Heo struct cpumask *effective = unbound_effective_cpumask(wq); 15855797b1c1STejun Heo int min_active = READ_ONCE(wq->min_active); 15865797b1c1STejun Heo int max_active = READ_ONCE(wq->max_active); 15875797b1c1STejun Heo int total_cpus, node; 15885797b1c1STejun Heo 15895797b1c1STejun Heo lockdep_assert_held(&wq->mutex); 15905797b1c1STejun Heo 1591c5f8cd6cSTejun Heo if (!wq_topo_initialized) 1592c5f8cd6cSTejun Heo return; 1593c5f8cd6cSTejun Heo 159415930da4STejun Heo if (off_cpu >= 0 && !cpumask_test_cpu(off_cpu, effective)) 15955797b1c1STejun Heo off_cpu = -1; 15965797b1c1STejun Heo 15975797b1c1STejun Heo total_cpus = cpumask_weight_and(effective, cpu_online_mask); 15985797b1c1STejun Heo if (off_cpu >= 0) 15995797b1c1STejun Heo total_cpus--; 16005797b1c1STejun Heo 16015797b1c1STejun Heo for_each_node(node) { 16025797b1c1STejun Heo int node_cpus; 16035797b1c1STejun Heo 16045797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 16055797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 16065797b1c1STejun Heo node_cpus--; 16075797b1c1STejun Heo 16085797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 16095797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 16105797b1c1STejun Heo min_active, max_active); 16115797b1c1STejun Heo } 16125797b1c1STejun Heo 16135797b1c1STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = min_active; 16145797b1c1STejun Heo } 16155797b1c1STejun Heo 16165797b1c1STejun Heo /** 16178864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 16188864b4e5STejun Heo * @pwq: pool_workqueue to get 16198864b4e5STejun Heo * 16208864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 16218864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 16228864b4e5STejun Heo */ 16238864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 16248864b4e5STejun Heo { 16258864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16268864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 16278864b4e5STejun Heo pwq->refcnt++; 16288864b4e5STejun Heo } 16298864b4e5STejun Heo 16308864b4e5STejun Heo /** 16318864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 16328864b4e5STejun Heo * @pwq: pool_workqueue to put 16338864b4e5STejun Heo * 16348864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 16358864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 16368864b4e5STejun Heo */ 16378864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 16388864b4e5STejun Heo { 16398864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16408864b4e5STejun Heo if (likely(--pwq->refcnt)) 16418864b4e5STejun Heo return; 16428864b4e5STejun Heo /* 1643967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1644967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16458864b4e5STejun Heo */ 1646687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16478864b4e5STejun Heo } 16488864b4e5STejun Heo 1649dce90d47STejun Heo /** 1650dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1651dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1652dce90d47STejun Heo * 1653dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1654dce90d47STejun Heo */ 1655dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1656dce90d47STejun Heo { 1657dce90d47STejun Heo if (pwq) { 1658dce90d47STejun Heo /* 165924acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1660dce90d47STejun Heo * following lock operations are safe. 1661dce90d47STejun Heo */ 1662a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1663dce90d47STejun Heo put_pwq(pwq); 1664a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1665dce90d47STejun Heo } 1666dce90d47STejun Heo } 1667dce90d47STejun Heo 1668afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1669afa87ce8STejun Heo { 1670afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1671afa87ce8STejun Heo } 1672afa87ce8STejun Heo 16734c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16744c638030STejun Heo struct work_struct *work) 1675bf4ede01STejun Heo { 16761c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16771c270b79STejun Heo 16781c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1679bf4ede01STejun Heo trace_workqueue_activate_work(work); 168082607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 168182607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1682112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16831c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16844c638030STejun Heo } 16854c638030STejun Heo 16864c638030STejun Heo /** 16874c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16884c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16894c638030STejun Heo * @work: work item to activate 16904c638030STejun Heo * 16914c638030STejun Heo * Returns %true if activated. %false if already active. 16924c638030STejun Heo */ 16934c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 16944c638030STejun Heo struct work_struct *work) 16954c638030STejun Heo { 16964c638030STejun Heo struct worker_pool *pool = pwq->pool; 169791ccc6e7STejun Heo struct wq_node_nr_active *nna; 16984c638030STejun Heo 16994c638030STejun Heo lockdep_assert_held(&pool->lock); 17004c638030STejun Heo 17014c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 17024c638030STejun Heo return false; 17034c638030STejun Heo 170491ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 170591ccc6e7STejun Heo if (nna) 170691ccc6e7STejun Heo atomic_inc(&nna->nr); 170791ccc6e7STejun Heo 1708112202d9STejun Heo pwq->nr_active++; 17094c638030STejun Heo __pwq_activate_work(pwq, work); 17104c638030STejun Heo return true; 1711bf4ede01STejun Heo } 1712bf4ede01STejun Heo 17135797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 17145797b1c1STejun Heo { 17155797b1c1STejun Heo int max = READ_ONCE(nna->max); 17165797b1c1STejun Heo 17175797b1c1STejun Heo while (true) { 17185797b1c1STejun Heo int old, tmp; 17195797b1c1STejun Heo 17205797b1c1STejun Heo old = atomic_read(&nna->nr); 17215797b1c1STejun Heo if (old >= max) 17225797b1c1STejun Heo return false; 17235797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 17245797b1c1STejun Heo if (tmp == old) 17255797b1c1STejun Heo return true; 17265797b1c1STejun Heo } 17275797b1c1STejun Heo } 17285797b1c1STejun Heo 17291c270b79STejun Heo /** 17301c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 17311c270b79STejun Heo * @pwq: pool_workqueue of interest 17325797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17331c270b79STejun Heo * 17341c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 17351c270b79STejun Heo * successfully obtained. %false otherwise. 17361c270b79STejun Heo */ 17375797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 17383aa62497SLai Jiangshan { 17391c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17401c270b79STejun Heo struct worker_pool *pool = pwq->pool; 174191ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17425797b1c1STejun Heo bool obtained = false; 17431c270b79STejun Heo 17441c270b79STejun Heo lockdep_assert_held(&pool->lock); 17451c270b79STejun Heo 17465797b1c1STejun Heo if (!nna) { 17474cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17481c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17495797b1c1STejun Heo goto out; 175091ccc6e7STejun Heo } 17515797b1c1STejun Heo 17524c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17534c065dbcSWaiman Long return false; 17544c065dbcSWaiman Long 17555797b1c1STejun Heo /* 17565797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17575797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17585797b1c1STejun Heo * concurrency level. Don't jump the line. 17595797b1c1STejun Heo * 17605797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17615797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17625797b1c1STejun Heo * increase it. This is indicated by @fill. 17635797b1c1STejun Heo */ 17645797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17655797b1c1STejun Heo goto out; 17665797b1c1STejun Heo 17675797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17685797b1c1STejun Heo if (obtained) 17695797b1c1STejun Heo goto out; 17705797b1c1STejun Heo 17715797b1c1STejun Heo /* 17725797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17735797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17745797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17755797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17765797b1c1STejun Heo * $nna->pending_pwqs. 17775797b1c1STejun Heo */ 17785797b1c1STejun Heo raw_spin_lock(&nna->lock); 17795797b1c1STejun Heo 17805797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17815797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17825797b1c1STejun Heo else if (likely(!fill)) 17835797b1c1STejun Heo goto out_unlock; 17845797b1c1STejun Heo 17855797b1c1STejun Heo smp_mb(); 17865797b1c1STejun Heo 17875797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17885797b1c1STejun Heo 17895797b1c1STejun Heo /* 17905797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 17915797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 17925797b1c1STejun Heo */ 17935797b1c1STejun Heo if (obtained && likely(!fill)) 17945797b1c1STejun Heo list_del_init(&pwq->pending_node); 17955797b1c1STejun Heo 17965797b1c1STejun Heo out_unlock: 17975797b1c1STejun Heo raw_spin_unlock(&nna->lock); 17985797b1c1STejun Heo out: 17995797b1c1STejun Heo if (obtained) 18005797b1c1STejun Heo pwq->nr_active++; 18011c270b79STejun Heo return obtained; 18021c270b79STejun Heo } 18031c270b79STejun Heo 18041c270b79STejun Heo /** 18051c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 18061c270b79STejun Heo * @pwq: pool_workqueue of interest 18075797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 18081c270b79STejun Heo * 18091c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 18101c270b79STejun Heo * max_active limit. 18111c270b79STejun Heo * 18121c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 18131c270b79STejun Heo * inactive work item is found or max_active limit is reached. 18141c270b79STejun Heo */ 18155797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 18161c270b79STejun Heo { 18171c270b79STejun Heo struct work_struct *work = 18181c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 18193aa62497SLai Jiangshan struct work_struct, entry); 18203aa62497SLai Jiangshan 18215797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 18221c270b79STejun Heo __pwq_activate_work(pwq, work); 18231c270b79STejun Heo return true; 18241c270b79STejun Heo } else { 18251c270b79STejun Heo return false; 18261c270b79STejun Heo } 18271c270b79STejun Heo } 18281c270b79STejun Heo 18291c270b79STejun Heo /** 1830516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1831516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 18324c065dbcSWaiman Long * 1833516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1834516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1835516d3dc9SWaiman Long * ensure proper work item ordering:: 18364c065dbcSWaiman Long * 18374c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 18384c065dbcSWaiman Long * | 18394c065dbcSWaiman Long * v 18404c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18414c065dbcSWaiman Long * | | | 18424c065dbcSWaiman Long * 1 3 5 18434c065dbcSWaiman Long * | | | 18444c065dbcSWaiman Long * 2 4 6 1845516d3dc9SWaiman Long * 1846516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1847516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1848516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1849516d3dc9SWaiman Long * the list is the oldest. 18504c065dbcSWaiman Long */ 18514c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18524c065dbcSWaiman Long { 18534c065dbcSWaiman Long struct pool_workqueue *pwq; 18544c065dbcSWaiman Long 18554c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18564c065dbcSWaiman Long 18574c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18584c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18594c065dbcSWaiman Long pwqs_node); 18604c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18614c065dbcSWaiman Long if (pwq->plugged) { 18624c065dbcSWaiman Long pwq->plugged = false; 18634c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18644c065dbcSWaiman Long kick_pool(pwq->pool); 18654c065dbcSWaiman Long } 18664c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18674c065dbcSWaiman Long } 18684c065dbcSWaiman Long 18694c065dbcSWaiman Long /** 18705797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18715797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18725797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18735797b1c1STejun Heo * 18745797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18755797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18765797b1c1STejun Heo */ 18775797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18785797b1c1STejun Heo struct worker_pool *caller_pool) 18795797b1c1STejun Heo { 18805797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18815797b1c1STejun Heo struct pool_workqueue *pwq; 18825797b1c1STejun Heo struct work_struct *work; 18835797b1c1STejun Heo 18845797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18855797b1c1STejun Heo 18865797b1c1STejun Heo raw_spin_lock(&nna->lock); 18875797b1c1STejun Heo retry: 18885797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18895797b1c1STejun Heo struct pool_workqueue, pending_node); 18905797b1c1STejun Heo if (!pwq) 18915797b1c1STejun Heo goto out_unlock; 18925797b1c1STejun Heo 18935797b1c1STejun Heo /* 18945797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 18955797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 18965797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 18975797b1c1STejun Heo * nested inside pool locks. 18985797b1c1STejun Heo */ 18995797b1c1STejun Heo if (pwq->pool != locked_pool) { 19005797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19015797b1c1STejun Heo locked_pool = pwq->pool; 19025797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 19035797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19045797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 19055797b1c1STejun Heo raw_spin_lock(&nna->lock); 19065797b1c1STejun Heo goto retry; 19075797b1c1STejun Heo } 19085797b1c1STejun Heo } 19095797b1c1STejun Heo 19105797b1c1STejun Heo /* 19115797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 19125797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 19135797b1c1STejun Heo */ 19145797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 19155797b1c1STejun Heo struct work_struct, entry); 19165797b1c1STejun Heo if (!work) { 19175797b1c1STejun Heo list_del_init(&pwq->pending_node); 19185797b1c1STejun Heo goto retry; 19195797b1c1STejun Heo } 19205797b1c1STejun Heo 19215797b1c1STejun Heo /* 19225797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 19235797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 19245797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 19255797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 19265797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 19275797b1c1STejun Heo */ 19285797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 19295797b1c1STejun Heo pwq->nr_active++; 19305797b1c1STejun Heo __pwq_activate_work(pwq, work); 19315797b1c1STejun Heo 19325797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 19335797b1c1STejun Heo list_del_init(&pwq->pending_node); 19345797b1c1STejun Heo else 19355797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 19365797b1c1STejun Heo 19375797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 19385797b1c1STejun Heo if (pwq->pool != caller_pool) 19395797b1c1STejun Heo kick_pool(pwq->pool); 19405797b1c1STejun Heo } 19415797b1c1STejun Heo 19425797b1c1STejun Heo out_unlock: 19435797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19445797b1c1STejun Heo if (locked_pool != caller_pool) { 19455797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19465797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19475797b1c1STejun Heo } 19485797b1c1STejun Heo } 19495797b1c1STejun Heo 19505797b1c1STejun Heo /** 19511c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19521c270b79STejun Heo * @pwq: pool_workqueue of interest 19531c270b79STejun Heo * 19541c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19555797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19561c270b79STejun Heo */ 19571c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19581c270b79STejun Heo { 19591c270b79STejun Heo struct worker_pool *pool = pwq->pool; 196091ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19611c270b79STejun Heo 19621c270b79STejun Heo lockdep_assert_held(&pool->lock); 19631c270b79STejun Heo 196491ccc6e7STejun Heo /* 196591ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 196691ccc6e7STejun Heo * workqueues. 196791ccc6e7STejun Heo */ 19681c270b79STejun Heo pwq->nr_active--; 196991ccc6e7STejun Heo 197091ccc6e7STejun Heo /* 197191ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 197291ccc6e7STejun Heo * inactive work item on @pwq itself. 197391ccc6e7STejun Heo */ 197491ccc6e7STejun Heo if (!nna) { 19755797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 197691ccc6e7STejun Heo return; 197791ccc6e7STejun Heo } 197891ccc6e7STejun Heo 19795797b1c1STejun Heo /* 19805797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19815797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19825797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19835797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19845797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19855797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19865797b1c1STejun Heo * decremented $nna->nr. 19875797b1c1STejun Heo * 19885797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19895797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19905797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 19915797b1c1STejun Heo * This maintains the forward progress guarantee. 19925797b1c1STejun Heo */ 19935797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 19945797b1c1STejun Heo return; 19955797b1c1STejun Heo 19965797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 19975797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 19983aa62497SLai Jiangshan } 19993aa62497SLai Jiangshan 2000bf4ede01STejun Heo /** 2001112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 2002112202d9STejun Heo * @pwq: pwq of interest 2003c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 2004bf4ede01STejun Heo * 2005bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 2006112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 2007bf4ede01STejun Heo * 2008dd6c3c54STejun Heo * NOTE: 2009dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 2010dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 2011dd6c3c54STejun Heo * work item is complete. 2012dd6c3c54STejun Heo * 2013bf4ede01STejun Heo * CONTEXT: 2014a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2015bf4ede01STejun Heo */ 2016c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 2017bf4ede01STejun Heo { 2018c4560c2cSLai Jiangshan int color = get_work_color(work_data); 2019c4560c2cSLai Jiangshan 20201c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 20211c270b79STejun Heo pwq_dec_nr_active(pwq); 2022018f3a13SLai Jiangshan 2023018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 2024bf4ede01STejun Heo 2025bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 2026112202d9STejun Heo if (likely(pwq->flush_color != color)) 20278864b4e5STejun Heo goto out_put; 2028bf4ede01STejun Heo 2029bf4ede01STejun Heo /* are there still in-flight works? */ 2030112202d9STejun Heo if (pwq->nr_in_flight[color]) 20318864b4e5STejun Heo goto out_put; 2032bf4ede01STejun Heo 2033112202d9STejun Heo /* this pwq is done, clear flush_color */ 2034112202d9STejun Heo pwq->flush_color = -1; 2035bf4ede01STejun Heo 2036bf4ede01STejun Heo /* 2037112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 2038bf4ede01STejun Heo * will handle the rest. 2039bf4ede01STejun Heo */ 2040112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2041112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20428864b4e5STejun Heo out_put: 20438864b4e5STejun Heo put_pwq(pwq); 2044bf4ede01STejun Heo } 2045bf4ede01STejun Heo 204636e227d2STejun Heo /** 2047bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 204836e227d2STejun Heo * @work: work item to steal 2049c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags 2050c26e2f2eSTejun Heo * @irq_flags: place to store irq state 205136e227d2STejun Heo * 205236e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2053d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 205436e227d2STejun Heo * 2055d185af30SYacine Belkadi * Return: 20563eb6b31bSMauro Carvalho Chehab * 20573eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 205836e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 205936e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 206036e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 2061bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 2062bbb68dfaSTejun Heo * for arbitrarily long 20633eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 206436e227d2STejun Heo * 2065d185af30SYacine Belkadi * Note: 2066bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2067e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2068e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2069e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2070bbb68dfaSTejun Heo * 2071bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2072c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags). 2073bbb68dfaSTejun Heo * 2074e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2075bf4ede01STejun Heo */ 2076c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags, 2077c26e2f2eSTejun Heo unsigned long *irq_flags) 2078bf4ede01STejun Heo { 2079d565ed63STejun Heo struct worker_pool *pool; 2080112202d9STejun Heo struct pool_workqueue *pwq; 2081bf4ede01STejun Heo 2082c26e2f2eSTejun Heo local_irq_save(*irq_flags); 2083bbb68dfaSTejun Heo 208436e227d2STejun Heo /* try to steal the timer if it exists */ 2085c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) { 208636e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 208736e227d2STejun Heo 2088e0aecdd8STejun Heo /* 2089e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2090e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2091e0aecdd8STejun Heo * running on the local CPU. 2092e0aecdd8STejun Heo */ 209336e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 209436e227d2STejun Heo return 1; 209536e227d2STejun Heo } 209636e227d2STejun Heo 209736e227d2STejun Heo /* try to claim PENDING the normal way */ 2098bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2099bf4ede01STejun Heo return 0; 2100bf4ede01STejun Heo 210124acfb71SThomas Gleixner rcu_read_lock(); 2102bf4ede01STejun Heo /* 2103bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2104bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2105bf4ede01STejun Heo */ 2106d565ed63STejun Heo pool = get_work_pool(work); 2107d565ed63STejun Heo if (!pool) 2108bbb68dfaSTejun Heo goto fail; 2109bf4ede01STejun Heo 2110a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2111bf4ede01STejun Heo /* 2112112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2113112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2114112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2115112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2116112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 21170b3dae68SLai Jiangshan * item is currently queued on that pool. 2118bf4ede01STejun Heo */ 2119112202d9STejun Heo pwq = get_work_pwq(work); 2120112202d9STejun Heo if (pwq && pwq->pool == pool) { 2121c70e1779STejun Heo unsigned long work_data; 2122c70e1779STejun Heo 2123bf4ede01STejun Heo debug_work_deactivate(work); 21243aa62497SLai Jiangshan 21253aa62497SLai Jiangshan /* 2126018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2127018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2128018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2129018f3a13SLai Jiangshan * 2130f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2131d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2132f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 213316062836STejun Heo * management later on and cause stall. Make sure the work 213416062836STejun Heo * item is activated before grabbing. 21353aa62497SLai Jiangshan */ 21364c638030STejun Heo pwq_activate_work(pwq, work); 21373aa62497SLai Jiangshan 2138bf4ede01STejun Heo list_del_init(&work->entry); 213936e227d2STejun Heo 2140c70e1779STejun Heo /* 2141c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2142c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2143c70e1779STejun Heo */ 2144c70e1779STejun Heo work_data = *work_data_bits(work); 2145bccdc1faSTejun Heo set_work_pool_and_keep_pending(work, pool->id, 0); 21464468a00fSLai Jiangshan 2147dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2148c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2149dd6c3c54STejun Heo 2150a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 215124acfb71SThomas Gleixner rcu_read_unlock(); 215236e227d2STejun Heo return 1; 2153bf4ede01STejun Heo } 2154a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2155bbb68dfaSTejun Heo fail: 215624acfb71SThomas Gleixner rcu_read_unlock(); 2157c26e2f2eSTejun Heo local_irq_restore(*irq_flags); 2158bbb68dfaSTejun Heo if (work_is_canceling(work)) 2159bbb68dfaSTejun Heo return -ENOENT; 2160bbb68dfaSTejun Heo cpu_relax(); 216136e227d2STejun Heo return -EAGAIN; 2162bf4ede01STejun Heo } 2163bf4ede01STejun Heo 2164978b8409STejun Heo struct cwt_wait { 2165978b8409STejun Heo wait_queue_entry_t wait; 2166978b8409STejun Heo struct work_struct *work; 2167978b8409STejun Heo }; 2168978b8409STejun Heo 2169978b8409STejun Heo static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 2170978b8409STejun Heo { 2171978b8409STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 2172978b8409STejun Heo 2173978b8409STejun Heo if (cwait->work != key) 2174978b8409STejun Heo return 0; 2175978b8409STejun Heo return autoremove_wake_function(wait, mode, sync, key); 2176978b8409STejun Heo } 2177978b8409STejun Heo 2178978b8409STejun Heo /** 2179978b8409STejun Heo * work_grab_pending - steal work item from worklist and disable irq 2180978b8409STejun Heo * @work: work item to steal 2181978b8409STejun Heo * @cflags: %WORK_CANCEL_ flags 2182978b8409STejun Heo * @irq_flags: place to store IRQ state 2183978b8409STejun Heo * 2184978b8409STejun Heo * Grab PENDING bit of @work. @work can be in any stable state - idle, on timer 2185978b8409STejun Heo * or on worklist. 2186978b8409STejun Heo * 2187978b8409STejun Heo * Must be called in process context. IRQ is disabled on return with IRQ state 2188978b8409STejun Heo * stored in *@irq_flags. The caller is responsible for re-enabling it using 2189978b8409STejun Heo * local_irq_restore(). 2190978b8409STejun Heo * 2191978b8409STejun Heo * Returns %true if @work was pending. %false if idle. 2192978b8409STejun Heo */ 2193978b8409STejun Heo static bool work_grab_pending(struct work_struct *work, u32 cflags, 2194978b8409STejun Heo unsigned long *irq_flags) 2195978b8409STejun Heo { 2196978b8409STejun Heo struct cwt_wait cwait; 2197978b8409STejun Heo int ret; 2198978b8409STejun Heo 2199978b8409STejun Heo might_sleep(); 2200978b8409STejun Heo repeat: 2201978b8409STejun Heo ret = try_to_grab_pending(work, cflags, irq_flags); 2202978b8409STejun Heo if (likely(ret >= 0)) 2203978b8409STejun Heo return ret; 2204978b8409STejun Heo if (ret != -ENOENT) 2205978b8409STejun Heo goto repeat; 2206978b8409STejun Heo 2207978b8409STejun Heo /* 2208978b8409STejun Heo * Someone is already canceling. Wait for it to finish. flush_work() 2209978b8409STejun Heo * doesn't work for PREEMPT_NONE because we may get woken up between 2210978b8409STejun Heo * @work's completion and the other canceling task resuming and clearing 2211978b8409STejun Heo * CANCELING - flush_work() will return false immediately as @work is no 2212978b8409STejun Heo * longer busy, try_to_grab_pending() will return -ENOENT as @work is 2213978b8409STejun Heo * still being canceled and the other canceling task won't be able to 2214978b8409STejun Heo * clear CANCELING as we're hogging the CPU. 2215978b8409STejun Heo * 2216978b8409STejun Heo * Let's wait for completion using a waitqueue. As this may lead to the 2217978b8409STejun Heo * thundering herd problem, use a custom wake function which matches 2218978b8409STejun Heo * @work along with exclusive wait and wakeup. 2219978b8409STejun Heo */ 2220978b8409STejun Heo init_wait(&cwait.wait); 2221978b8409STejun Heo cwait.wait.func = cwt_wakefn; 2222978b8409STejun Heo cwait.work = work; 2223978b8409STejun Heo 2224978b8409STejun Heo prepare_to_wait_exclusive(&wq_cancel_waitq, &cwait.wait, 2225978b8409STejun Heo TASK_UNINTERRUPTIBLE); 2226978b8409STejun Heo if (work_is_canceling(work)) 2227978b8409STejun Heo schedule(); 2228978b8409STejun Heo finish_wait(&wq_cancel_waitq, &cwait.wait); 2229978b8409STejun Heo 2230978b8409STejun Heo goto repeat; 2231978b8409STejun Heo } 2232978b8409STejun Heo 2233bf4ede01STejun Heo /** 2234706026c2STejun Heo * insert_work - insert a work into a pool 2235112202d9STejun Heo * @pwq: pwq @work belongs to 22364690c4abSTejun Heo * @work: work to insert 22374690c4abSTejun Heo * @head: insertion point 22384690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 22394690c4abSTejun Heo * 2240112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2241706026c2STejun Heo * work_struct flags. 22424690c4abSTejun Heo * 22434690c4abSTejun Heo * CONTEXT: 2244a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2245365970a1SDavid Howells */ 2246112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2247112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2248b89deed3SOleg Nesterov { 2249fe089f87STejun Heo debug_work_activate(work); 2250e1d8aa9fSFrederic Weisbecker 2251e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2252f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2253e89a85d6SWalter Wu 22544690c4abSTejun Heo /* we own @work, set data and link */ 2255112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 22561a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 22578864b4e5STejun Heo get_pwq(pwq); 2258b89deed3SOleg Nesterov } 2259b89deed3SOleg Nesterov 2260c8efcc25STejun Heo /* 2261c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 22628d03ecfeSTejun Heo * same workqueue. 2263c8efcc25STejun Heo */ 2264c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2265c8efcc25STejun Heo { 2266c8efcc25STejun Heo struct worker *worker; 2267c8efcc25STejun Heo 22688d03ecfeSTejun Heo worker = current_wq_worker(); 2269c8efcc25STejun Heo /* 2270bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 22718d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2272c8efcc25STejun Heo */ 2273112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2274c8efcc25STejun Heo } 2275c8efcc25STejun Heo 2276ef557180SMike Galbraith /* 2277ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2278ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2279ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2280ef557180SMike Galbraith */ 2281ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2282ef557180SMike Galbraith { 2283ef557180SMike Galbraith int new_cpu; 2284ef557180SMike Galbraith 2285f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2286ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2287ef557180SMike Galbraith return cpu; 2288a8ec5880SAmmar Faizi } else { 2289a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2290f303fccbSTejun Heo } 2291f303fccbSTejun Heo 2292ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2293ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2294ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2295ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2296ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2297ef557180SMike Galbraith return cpu; 2298ef557180SMike Galbraith } 2299ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2300ef557180SMike Galbraith 2301ef557180SMike Galbraith return new_cpu; 2302ef557180SMike Galbraith } 2303ef557180SMike Galbraith 2304d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 23051da177e4SLinus Torvalds struct work_struct *work) 23061da177e4SLinus Torvalds { 2307112202d9STejun Heo struct pool_workqueue *pwq; 2308fe089f87STejun Heo struct worker_pool *last_pool, *pool; 23098a2e8e5dSTejun Heo unsigned int work_flags; 2310b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 23118930cabaSTejun Heo 23128930cabaSTejun Heo /* 23138930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 23148930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 23158930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 23168930cabaSTejun Heo * happen with IRQ disabled. 23178930cabaSTejun Heo */ 23188e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 23191da177e4SLinus Torvalds 232033e3f0a3SRichard Clark /* 232133e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 232233e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 232333e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 232433e3f0a3SRichard Clark */ 232533e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 232633e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2327e41e704bSTejun Heo return; 232824acfb71SThomas Gleixner rcu_read_lock(); 23299e8cd2f5STejun Heo retry: 2330aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2331636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2332636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2333ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2334636b927eSTejun Heo else 2335aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2336aa202f1fSHillf Danton } 2337f3421797STejun Heo 2338636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2339fe089f87STejun Heo pool = pwq->pool; 2340fe089f87STejun Heo 234118aa9effSTejun Heo /* 2342c9178087STejun Heo * If @work was previously on a different pool, it might still be 2343c9178087STejun Heo * running there, in which case the work needs to be queued on that 2344c9178087STejun Heo * pool to guarantee non-reentrancy. 234518aa9effSTejun Heo */ 2346c9e7cf27STejun Heo last_pool = get_work_pool(work); 2347fe089f87STejun Heo if (last_pool && last_pool != pool) { 234818aa9effSTejun Heo struct worker *worker; 234918aa9effSTejun Heo 2350a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 235118aa9effSTejun Heo 2352c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 235318aa9effSTejun Heo 2354112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2355c9178087STejun Heo pwq = worker->current_pwq; 2356fe089f87STejun Heo pool = pwq->pool; 2357fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 23588594fadeSLai Jiangshan } else { 235918aa9effSTejun Heo /* meh... not running there, queue here */ 2360a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2361fe089f87STejun Heo raw_spin_lock(&pool->lock); 236218aa9effSTejun Heo } 23638930cabaSTejun Heo } else { 2364fe089f87STejun Heo raw_spin_lock(&pool->lock); 23658930cabaSTejun Heo } 2366502ca9d8STejun Heo 23679e8cd2f5STejun Heo /* 2368636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2369636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2370636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2371636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2372636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 23739e8cd2f5STejun Heo */ 23749e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 23759e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2376fe089f87STejun Heo raw_spin_unlock(&pool->lock); 23779e8cd2f5STejun Heo cpu_relax(); 23789e8cd2f5STejun Heo goto retry; 23799e8cd2f5STejun Heo } 23809e8cd2f5STejun Heo /* oops */ 23819e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 23829e8cd2f5STejun Heo wq->name, cpu); 23839e8cd2f5STejun Heo } 23849e8cd2f5STejun Heo 2385112202d9STejun Heo /* pwq determined, queue */ 2386112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2387502ca9d8STejun Heo 238824acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 238924acfb71SThomas Gleixner goto out; 23901e19ffc6STejun Heo 2391112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2392112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 23931e19ffc6STejun Heo 2394a045a272STejun Heo /* 2395a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2396a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2397a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2398a045a272STejun Heo */ 23995797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2400fe089f87STejun Heo if (list_empty(&pool->worklist)) 2401fe089f87STejun Heo pool->watchdog_ts = jiffies; 2402fe089f87STejun Heo 2403cdadf009STejun Heo trace_workqueue_activate_work(work); 2404fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 24050219a352STejun Heo kick_pool(pool); 24068a2e8e5dSTejun Heo } else { 2407f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2408fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 24098a2e8e5dSTejun Heo } 24101e19ffc6STejun Heo 241124acfb71SThomas Gleixner out: 2412fe089f87STejun Heo raw_spin_unlock(&pool->lock); 241324acfb71SThomas Gleixner rcu_read_unlock(); 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 24160fcb78c2SRolf Eike Beer /** 2417c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2418c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2419c1a220e7SZhang Rui * @wq: workqueue to use 2420c1a220e7SZhang Rui * @work: work to queue 2421c1a220e7SZhang Rui * 2422c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2423443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2424443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2425854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2426854f5cc5SPaul E. McKenney * online will get a splat. 2427d185af30SYacine Belkadi * 2428d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2429c1a220e7SZhang Rui */ 2430d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2431d4283e93STejun Heo struct work_struct *work) 2432c1a220e7SZhang Rui { 2433d4283e93STejun Heo bool ret = false; 2434c26e2f2eSTejun Heo unsigned long irq_flags; 24358930cabaSTejun Heo 2436c26e2f2eSTejun Heo local_irq_save(irq_flags); 2437c1a220e7SZhang Rui 243822df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24394690c4abSTejun Heo __queue_work(cpu, wq, work); 2440d4283e93STejun Heo ret = true; 2441c1a220e7SZhang Rui } 24428930cabaSTejun Heo 2443c26e2f2eSTejun Heo local_irq_restore(irq_flags); 2444c1a220e7SZhang Rui return ret; 2445c1a220e7SZhang Rui } 2446ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2447c1a220e7SZhang Rui 24488204e0c1SAlexander Duyck /** 2449fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 24508204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 24518204e0c1SAlexander Duyck * 24528204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 24538204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 24548204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 24558204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 24568204e0c1SAlexander Duyck */ 2457fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 24588204e0c1SAlexander Duyck { 24598204e0c1SAlexander Duyck int cpu; 24608204e0c1SAlexander Duyck 24618204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 24628204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 24638204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 24648204e0c1SAlexander Duyck 24658204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 24668204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 24678204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 24688204e0c1SAlexander Duyck return cpu; 24698204e0c1SAlexander Duyck 24708204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 24718204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 24728204e0c1SAlexander Duyck 24738204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 24748204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 24758204e0c1SAlexander Duyck } 24768204e0c1SAlexander Duyck 24778204e0c1SAlexander Duyck /** 24788204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 24798204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 24808204e0c1SAlexander Duyck * @wq: workqueue to use 24818204e0c1SAlexander Duyck * @work: work to queue 24828204e0c1SAlexander Duyck * 24838204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 24848204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 24858204e0c1SAlexander Duyck * NUMA node. 24868204e0c1SAlexander Duyck * 24878204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 24888204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 24898204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 24908204e0c1SAlexander Duyck * 24918204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 24928204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 24938204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 24948204e0c1SAlexander Duyck * 24958204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 24968204e0c1SAlexander Duyck */ 24978204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 24988204e0c1SAlexander Duyck struct work_struct *work) 24998204e0c1SAlexander Duyck { 2500c26e2f2eSTejun Heo unsigned long irq_flags; 25018204e0c1SAlexander Duyck bool ret = false; 25028204e0c1SAlexander Duyck 25038204e0c1SAlexander Duyck /* 25048204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 25058204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 25068204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 25078204e0c1SAlexander Duyck * 25088204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 25098204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 25108204e0c1SAlexander Duyck * some round robin type logic. 25118204e0c1SAlexander Duyck */ 25128204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 25138204e0c1SAlexander Duyck 2514c26e2f2eSTejun Heo local_irq_save(irq_flags); 25158204e0c1SAlexander Duyck 25168204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2517fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 25188204e0c1SAlexander Duyck 25198204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 25208204e0c1SAlexander Duyck ret = true; 25218204e0c1SAlexander Duyck } 25228204e0c1SAlexander Duyck 2523c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25248204e0c1SAlexander Duyck return ret; 25258204e0c1SAlexander Duyck } 25268204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 25278204e0c1SAlexander Duyck 25288c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 25291da177e4SLinus Torvalds { 25308c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 25311da177e4SLinus Torvalds 2532e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 253360c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 25341da177e4SLinus Torvalds } 25351438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 25361da177e4SLinus Torvalds 25377beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 253852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25391da177e4SLinus Torvalds { 25407beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 25417beb2edfSTejun Heo struct work_struct *work = &dwork->work; 25421da177e4SLinus Torvalds 2543637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 25444b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2545fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2546fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 25477beb2edfSTejun Heo 25488852aac2STejun Heo /* 25498852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 25508852aac2STejun Heo * both optimization and correctness. The earliest @timer can 25518852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 25528852aac2STejun Heo * on that there's no such delay when @delay is 0. 25538852aac2STejun Heo */ 25548852aac2STejun Heo if (!delay) { 25558852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 25568852aac2STejun Heo return; 25578852aac2STejun Heo } 25588852aac2STejun Heo 255960c057bcSLai Jiangshan dwork->wq = wq; 25601265057fSTejun Heo dwork->cpu = cpu; 25617beb2edfSTejun Heo timer->expires = jiffies + delay; 25627beb2edfSTejun Heo 2563aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2564aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2565aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2566aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2567aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 25687beb2edfSTejun Heo add_timer_on(timer, cpu); 2569aae17ebbSLeonardo Bras } else { 2570aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2571c0e8c5b5SAnna-Maria Behnsen add_timer_global(timer); 2572aae17ebbSLeonardo Bras else 2573aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2574aae17ebbSLeonardo Bras } 25757beb2edfSTejun Heo } 25761da177e4SLinus Torvalds 25770fcb78c2SRolf Eike Beer /** 25780fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 25790fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 25800fcb78c2SRolf Eike Beer * @wq: workqueue to use 2581af9997e4SRandy Dunlap * @dwork: work to queue 25820fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 25830fcb78c2SRolf Eike Beer * 2584d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2585715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2586715f1300STejun Heo * execution. 25870fcb78c2SRolf Eike Beer */ 2588d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 258952bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25907a6bc1cdSVenkatesh Pallipadi { 259152bad64dSDavid Howells struct work_struct *work = &dwork->work; 2592d4283e93STejun Heo bool ret = false; 2593c26e2f2eSTejun Heo unsigned long irq_flags; 25948930cabaSTejun Heo 25958930cabaSTejun Heo /* read the comment in __queue_work() */ 2596c26e2f2eSTejun Heo local_irq_save(irq_flags); 25977a6bc1cdSVenkatesh Pallipadi 259822df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 25997beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2600d4283e93STejun Heo ret = true; 26017a6bc1cdSVenkatesh Pallipadi } 26028930cabaSTejun Heo 2603c26e2f2eSTejun Heo local_irq_restore(irq_flags); 26047a6bc1cdSVenkatesh Pallipadi return ret; 26057a6bc1cdSVenkatesh Pallipadi } 2606ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 26071da177e4SLinus Torvalds 2608c8e55f36STejun Heo /** 26098376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 26108376fe22STejun Heo * @cpu: CPU number to execute work on 26118376fe22STejun Heo * @wq: workqueue to use 26128376fe22STejun Heo * @dwork: work to queue 26138376fe22STejun Heo * @delay: number of jiffies to wait before queueing 26148376fe22STejun Heo * 26158376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 26168376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 26178376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 26188376fe22STejun Heo * current state. 26198376fe22STejun Heo * 2620d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 26218376fe22STejun Heo * pending and its timer was modified. 26228376fe22STejun Heo * 2623e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 26248376fe22STejun Heo * See try_to_grab_pending() for details. 26258376fe22STejun Heo */ 26268376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 26278376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 26288376fe22STejun Heo { 2629c26e2f2eSTejun Heo unsigned long irq_flags; 26308376fe22STejun Heo int ret; 26318376fe22STejun Heo 26328376fe22STejun Heo do { 2633c5f5b942STejun Heo ret = try_to_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, 2634c5f5b942STejun Heo &irq_flags); 26358376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 26368376fe22STejun Heo 26378376fe22STejun Heo if (likely(ret >= 0)) { 26388376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2639c26e2f2eSTejun Heo local_irq_restore(irq_flags); 26408376fe22STejun Heo } 26418376fe22STejun Heo 26428376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 26438376fe22STejun Heo return ret; 26448376fe22STejun Heo } 26458376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 26468376fe22STejun Heo 264705f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 264805f0fe6bSTejun Heo { 264905f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 265005f0fe6bSTejun Heo 265105f0fe6bSTejun Heo /* read the comment in __queue_work() */ 265205f0fe6bSTejun Heo local_irq_disable(); 265305f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 265405f0fe6bSTejun Heo local_irq_enable(); 265505f0fe6bSTejun Heo } 265605f0fe6bSTejun Heo 265705f0fe6bSTejun Heo /** 265805f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 265905f0fe6bSTejun Heo * @wq: workqueue to use 266005f0fe6bSTejun Heo * @rwork: work to queue 266105f0fe6bSTejun Heo * 266205f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 266305f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2664bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 266505f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 266605f0fe6bSTejun Heo */ 266705f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 266805f0fe6bSTejun Heo { 266905f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 267005f0fe6bSTejun Heo 267105f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 267205f0fe6bSTejun Heo rwork->wq = wq; 2673a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 267405f0fe6bSTejun Heo return true; 267505f0fe6bSTejun Heo } 267605f0fe6bSTejun Heo 267705f0fe6bSTejun Heo return false; 267805f0fe6bSTejun Heo } 267905f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 268005f0fe6bSTejun Heo 2681f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2682c34056a3STejun Heo { 2683c34056a3STejun Heo struct worker *worker; 2684c34056a3STejun Heo 2685f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2686c8e55f36STejun Heo if (worker) { 2687c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2688affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2689da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2690e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2691e22bee78STejun Heo worker->flags = WORKER_PREP; 2692c8e55f36STejun Heo } 2693c34056a3STejun Heo return worker; 2694c34056a3STejun Heo } 2695c34056a3STejun Heo 26969546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 26979546b29eSTejun Heo { 26988639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 26999546b29eSTejun Heo return pool->attrs->__pod_cpumask; 27008639ecebSTejun Heo else 27018639ecebSTejun Heo return pool->attrs->cpumask; 27029546b29eSTejun Heo } 27039546b29eSTejun Heo 2704c34056a3STejun Heo /** 27054736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 27064736cbf7SLai Jiangshan * @worker: worker to be attached 27074736cbf7SLai Jiangshan * @pool: the target pool 27084736cbf7SLai Jiangshan * 27094736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 27104736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 27114736cbf7SLai Jiangshan * cpu-[un]hotplugs. 27124736cbf7SLai Jiangshan */ 27134736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 27144736cbf7SLai Jiangshan struct worker_pool *pool) 27154736cbf7SLai Jiangshan { 27161258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 27174736cbf7SLai Jiangshan 27184736cbf7SLai Jiangshan /* 27194cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 27204cb1ef64STejun Heo * across this function. See the comments above the flag definition for 27214cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 27224736cbf7SLai Jiangshan */ 27234cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 27244736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 27254cb1ef64STejun Heo } else { 27264cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27275c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 27284cb1ef64STejun Heo } 27294736cbf7SLai Jiangshan 2730640f17c8SPeter Zijlstra if (worker->rescue_wq) 27319546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2732640f17c8SPeter Zijlstra 27334736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2734a2d812a2STejun Heo worker->pool = pool; 27354736cbf7SLai Jiangshan 27361258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 27374736cbf7SLai Jiangshan } 27384736cbf7SLai Jiangshan 27394736cbf7SLai Jiangshan /** 274060f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 274160f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 274260f5a4bcSLai Jiangshan * 27434736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 27444736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 27454736cbf7SLai Jiangshan * other reference to the pool. 274660f5a4bcSLai Jiangshan */ 2747a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 274860f5a4bcSLai Jiangshan { 2749a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 275060f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 275160f5a4bcSLai Jiangshan 27524cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 27534cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27544cb1ef64STejun Heo 27551258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2756a2d812a2STejun Heo 27575c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2758da028469SLai Jiangshan list_del(&worker->node); 2759a2d812a2STejun Heo worker->pool = NULL; 2760a2d812a2STejun Heo 2761e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 276260f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 27631258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 276460f5a4bcSLai Jiangshan 2765b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2766b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2767b62c0751SLai Jiangshan 276860f5a4bcSLai Jiangshan if (detach_completion) 276960f5a4bcSLai Jiangshan complete(detach_completion); 277060f5a4bcSLai Jiangshan } 277160f5a4bcSLai Jiangshan 277260f5a4bcSLai Jiangshan /** 2773c34056a3STejun Heo * create_worker - create a new workqueue worker 277463d95a91STejun Heo * @pool: pool the new worker will belong to 2775c34056a3STejun Heo * 2776051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2777c34056a3STejun Heo * 2778c34056a3STejun Heo * CONTEXT: 2779c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2780c34056a3STejun Heo * 2781d185af30SYacine Belkadi * Return: 2782c34056a3STejun Heo * Pointer to the newly created worker. 2783c34056a3STejun Heo */ 2784bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2785c34056a3STejun Heo { 2786e441b56fSZhen Lei struct worker *worker; 2787e441b56fSZhen Lei int id; 27885d9c7a1eSLucy Mielke char id_buf[23]; 2789c34056a3STejun Heo 27907cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2791e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 27923f0ea0b8SPetr Mladek if (id < 0) { 27933f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 27943f0ea0b8SPetr Mladek ERR_PTR(id)); 2795e441b56fSZhen Lei return NULL; 27963f0ea0b8SPetr Mladek } 2797c34056a3STejun Heo 2798f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 27993f0ea0b8SPetr Mladek if (!worker) { 28003f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2801c34056a3STejun Heo goto fail; 28023f0ea0b8SPetr Mladek } 2803c34056a3STejun Heo 2804c34056a3STejun Heo worker->id = id; 2805c34056a3STejun Heo 28064cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 280729c91e99STejun Heo if (pool->cpu >= 0) 2808e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2809e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2810f3421797STejun Heo else 2811e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2812e3c916a4STejun Heo 28134cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 28144cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 28153f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 281660f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 281760f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 281860f54038SPetr Mladek id_buf); 281960f54038SPetr Mladek } else { 28203f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 28213f0ea0b8SPetr Mladek worker->task); 282260f54038SPetr Mladek } 2823c34056a3STejun Heo goto fail; 28243f0ea0b8SPetr Mladek } 2825c34056a3STejun Heo 282691151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 28279546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 28284cb1ef64STejun Heo } 282991151228SOleg Nesterov 2830da028469SLai Jiangshan /* successful, attach the worker to the pool */ 28314736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2832822d8405STejun Heo 2833051e1850SLai Jiangshan /* start the newly created worker */ 2834a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 28350219a352STejun Heo 2836051e1850SLai Jiangshan worker->pool->nr_workers++; 2837051e1850SLai Jiangshan worker_enter_idle(worker); 28380219a352STejun Heo 28390219a352STejun Heo /* 28400219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 28416a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 28426a229b0eSTejun Heo * wake it up explicitly. 28430219a352STejun Heo */ 28444cb1ef64STejun Heo if (worker->task) 2845051e1850SLai Jiangshan wake_up_process(worker->task); 28460219a352STejun Heo 2847a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2848051e1850SLai Jiangshan 2849c34056a3STejun Heo return worker; 2850822d8405STejun Heo 2851c34056a3STejun Heo fail: 2852e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2853c34056a3STejun Heo kfree(worker); 2854c34056a3STejun Heo return NULL; 2855c34056a3STejun Heo } 2856c34056a3STejun Heo 2857793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2858793777bcSValentin Schneider { 2859793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2860793777bcSValentin Schneider 2861793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2862793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2863793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2864793777bcSValentin Schneider else 2865793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2866793777bcSValentin Schneider } 2867793777bcSValentin Schneider 2868e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2869e02b9312SValentin Schneider { 2870e02b9312SValentin Schneider struct worker *worker, *tmp; 2871e02b9312SValentin Schneider 2872e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2873e02b9312SValentin Schneider list_del_init(&worker->entry); 2874e02b9312SValentin Schneider unbind_worker(worker); 2875e02b9312SValentin Schneider /* 2876e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2877e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2878e02b9312SValentin Schneider * wouldn't have gotten here. 2879c34056a3STejun Heo * 2880e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2881e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2882e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2883e02b9312SValentin Schneider * outside of pool->lock. 2884e02b9312SValentin Schneider */ 2885e02b9312SValentin Schneider wake_up_process(worker->task); 2886e02b9312SValentin Schneider } 2887e02b9312SValentin Schneider } 2888e02b9312SValentin Schneider 2889e02b9312SValentin Schneider /** 2890e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2891e02b9312SValentin Schneider * @worker: worker to be destroyed 2892e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2893e02b9312SValentin Schneider * 2894e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2895e02b9312SValentin Schneider * should be idle. 2896c8e55f36STejun Heo * 2897c8e55f36STejun Heo * CONTEXT: 2898a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2899c34056a3STejun Heo */ 2900e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2901c34056a3STejun Heo { 2902bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2903c34056a3STejun Heo 2904cd549687STejun Heo lockdep_assert_held(&pool->lock); 2905e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2906cd549687STejun Heo 2907c34056a3STejun Heo /* sanity check frenzy */ 29086183c009STejun Heo if (WARN_ON(worker->current_work) || 290973eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 291073eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 29116183c009STejun Heo return; 2912c34056a3STejun Heo 2913bd7bdd43STejun Heo pool->nr_workers--; 2914bd7bdd43STejun Heo pool->nr_idle--; 2915c8e55f36STejun Heo 2916cb444766STejun Heo worker->flags |= WORKER_DIE; 2917e02b9312SValentin Schneider 2918e02b9312SValentin Schneider list_move(&worker->entry, list); 2919e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2920c34056a3STejun Heo } 2921c34056a3STejun Heo 29223f959aa3SValentin Schneider /** 29233f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 29243f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 29253f959aa3SValentin Schneider * 29263f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 29273f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 29283f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 29293f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 29303f959aa3SValentin Schneider * it expire and re-evaluate things from there. 29313f959aa3SValentin Schneider */ 293232a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2933e22bee78STejun Heo { 293432a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 29353f959aa3SValentin Schneider bool do_cull = false; 29363f959aa3SValentin Schneider 29373f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 29383f959aa3SValentin Schneider return; 29393f959aa3SValentin Schneider 29403f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 29413f959aa3SValentin Schneider 29423f959aa3SValentin Schneider if (too_many_workers(pool)) { 29433f959aa3SValentin Schneider struct worker *worker; 29443f959aa3SValentin Schneider unsigned long expires; 29453f959aa3SValentin Schneider 29463f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 29473f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 29483f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 29493f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 29503f959aa3SValentin Schneider 29513f959aa3SValentin Schneider if (!do_cull) 29523f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 29533f959aa3SValentin Schneider } 29543f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 29553f959aa3SValentin Schneider 29563f959aa3SValentin Schneider if (do_cull) 29573f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 29583f959aa3SValentin Schneider } 29593f959aa3SValentin Schneider 29603f959aa3SValentin Schneider /** 29613f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 29623f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 29633f959aa3SValentin Schneider * 29643f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 29653f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2966e02b9312SValentin Schneider * 2967e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2968e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2969e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 29703f959aa3SValentin Schneider */ 29713f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 29723f959aa3SValentin Schneider { 29733f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 29749680540cSYang Yingliang LIST_HEAD(cull_list); 2975e22bee78STejun Heo 2976e02b9312SValentin Schneider /* 2977e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2978e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2979e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2980e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2981e02b9312SValentin Schneider */ 2982e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2983a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2984e22bee78STejun Heo 29853347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2986e22bee78STejun Heo struct worker *worker; 2987e22bee78STejun Heo unsigned long expires; 2988e22bee78STejun Heo 298963d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2990e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 2991e22bee78STejun Heo 29923347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 299363d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 29943347fc9fSLai Jiangshan break; 2995e22bee78STejun Heo } 29963347fc9fSLai Jiangshan 2997e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 2998e22bee78STejun Heo } 2999e22bee78STejun Heo 3000a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3001e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3002e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 3003e22bee78STejun Heo } 3004e22bee78STejun Heo 3005493a1724STejun Heo static void send_mayday(struct work_struct *work) 3006e22bee78STejun Heo { 3007112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3008112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 3009493a1724STejun Heo 30102e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 3011e22bee78STejun Heo 3012493008a8STejun Heo if (!wq->rescuer) 3013493a1724STejun Heo return; 3014e22bee78STejun Heo 3015e22bee78STejun Heo /* mayday mayday mayday */ 3016493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 301777668c8bSLai Jiangshan /* 301877668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 301977668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 302077668c8bSLai Jiangshan * rescuer is done with it. 302177668c8bSLai Jiangshan */ 302277668c8bSLai Jiangshan get_pwq(pwq); 3023493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3024e22bee78STejun Heo wake_up_process(wq->rescuer->task); 3025725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 3026493a1724STejun Heo } 3027e22bee78STejun Heo } 3028e22bee78STejun Heo 302932a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 3030e22bee78STejun Heo { 303132a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 3032e22bee78STejun Heo struct work_struct *work; 3033e22bee78STejun Heo 3034a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3035a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 3036e22bee78STejun Heo 303763d95a91STejun Heo if (need_to_create_worker(pool)) { 3038e22bee78STejun Heo /* 3039e22bee78STejun Heo * We've been trying to create a new worker but 3040e22bee78STejun Heo * haven't been successful. We might be hitting an 3041e22bee78STejun Heo * allocation deadlock. Send distress signals to 3042e22bee78STejun Heo * rescuers. 3043e22bee78STejun Heo */ 304463d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 3045e22bee78STejun Heo send_mayday(work); 3046e22bee78STejun Heo } 3047e22bee78STejun Heo 3048a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3049a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3050e22bee78STejun Heo 305163d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 3052e22bee78STejun Heo } 3053e22bee78STejun Heo 3054e22bee78STejun Heo /** 3055e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 305663d95a91STejun Heo * @pool: pool to create a new worker for 3057e22bee78STejun Heo * 305863d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 3059e22bee78STejun Heo * have at least one idle worker on return from this function. If 3060e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 306163d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 3062e22bee78STejun Heo * possible allocation deadlock. 3063e22bee78STejun Heo * 3064c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 3065c5aa87bbSTejun Heo * may_start_working() %true. 3066e22bee78STejun Heo * 3067e22bee78STejun Heo * LOCKING: 3068a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3069e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 3070e22bee78STejun Heo * manager. 3071e22bee78STejun Heo */ 307229187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 3073d565ed63STejun Heo __releases(&pool->lock) 3074d565ed63STejun Heo __acquires(&pool->lock) 3075e22bee78STejun Heo { 3076e22bee78STejun Heo restart: 3077a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30789f9c2364STejun Heo 3079e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 308063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 3081e22bee78STejun Heo 3082e22bee78STejun Heo while (true) { 3083051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 3084e22bee78STejun Heo break; 3085e22bee78STejun Heo 3086e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 30879f9c2364STejun Heo 308863d95a91STejun Heo if (!need_to_create_worker(pool)) 3089e22bee78STejun Heo break; 3090e22bee78STejun Heo } 3091e22bee78STejun Heo 309263d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 3093a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3094051e1850SLai Jiangshan /* 3095051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 3096051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 3097051e1850SLai Jiangshan * already become busy. 3098051e1850SLai Jiangshan */ 309963d95a91STejun Heo if (need_to_create_worker(pool)) 3100e22bee78STejun Heo goto restart; 3101e22bee78STejun Heo } 3102e22bee78STejun Heo 3103e22bee78STejun Heo /** 3104e22bee78STejun Heo * manage_workers - manage worker pool 3105e22bee78STejun Heo * @worker: self 3106e22bee78STejun Heo * 3107706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 3108e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3109706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3110e22bee78STejun Heo * 3111e22bee78STejun Heo * The caller can safely start processing works on false return. On 3112e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3113e22bee78STejun Heo * and may_start_working() is true. 3114e22bee78STejun Heo * 3115e22bee78STejun Heo * CONTEXT: 3116a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3117e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3118e22bee78STejun Heo * 3119d185af30SYacine Belkadi * Return: 312029187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 312129187a9eSTejun Heo * start processing works, %true if management function was performed and 312229187a9eSTejun Heo * the conditions that the caller verified before calling the function may 312329187a9eSTejun Heo * no longer be true. 3124e22bee78STejun Heo */ 3125e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3126e22bee78STejun Heo { 312763d95a91STejun Heo struct worker_pool *pool = worker->pool; 3128e22bee78STejun Heo 3129692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 313029187a9eSTejun Heo return false; 3131692b4825STejun Heo 3132692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 31332607d7a6STejun Heo pool->manager = worker; 3134e22bee78STejun Heo 313529187a9eSTejun Heo maybe_create_worker(pool); 3136e22bee78STejun Heo 31372607d7a6STejun Heo pool->manager = NULL; 3138692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3139d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 314029187a9eSTejun Heo return true; 3141e22bee78STejun Heo } 3142e22bee78STejun Heo 3143a62428c0STejun Heo /** 3144a62428c0STejun Heo * process_one_work - process single work 3145c34056a3STejun Heo * @worker: self 3146a62428c0STejun Heo * @work: work to process 3147a62428c0STejun Heo * 3148a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3149a62428c0STejun Heo * process a single work including synchronization against and 3150a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3151a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3152a62428c0STejun Heo * call this function to process a work. 3153a62428c0STejun Heo * 3154a62428c0STejun Heo * CONTEXT: 3155a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3156a62428c0STejun Heo */ 3157c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3158d565ed63STejun Heo __releases(&pool->lock) 3159d565ed63STejun Heo __acquires(&pool->lock) 31601da177e4SLinus Torvalds { 3161112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3162bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3163c4560c2cSLai Jiangshan unsigned long work_data; 3164c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 31651acd92d9STejun Heo bool bh_draining = pool->flags & POOL_BH_DRAINING; 31664e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 31674e6045f1SJohannes Berg /* 3168a62428c0STejun Heo * It is permissible to free the struct work_struct from 3169a62428c0STejun Heo * inside the function that is called from it, this we need to 3170a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3171a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3172a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 31734e6045f1SJohannes Berg */ 31744d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 31754d82a1deSPeter Zijlstra 31764d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 31774e6045f1SJohannes Berg #endif 3178807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 317985327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3180ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 318125511a47STejun Heo 31828930cabaSTejun Heo /* claim and dequeue */ 3183dc186ad7SThomas Gleixner debug_work_deactivate(work); 3184c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3185c34056a3STejun Heo worker->current_work = work; 3186a2c1c57bSTejun Heo worker->current_func = work->func; 3187112202d9STejun Heo worker->current_pwq = pwq; 31884cb1ef64STejun Heo if (worker->task) 3189616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3190c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3191d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 31927a22ad75STejun Heo 31938bf89593STejun Heo /* 31948bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 31958bf89593STejun Heo * overridden through set_worker_desc(). 31968bf89593STejun Heo */ 31978bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 31988bf89593STejun Heo 3199a62428c0STejun Heo list_del_init(&work->entry); 3200a62428c0STejun Heo 3201649027d7STejun Heo /* 3202228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3203228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3204228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3205228f1d00SLai Jiangshan * execution of the pending work items. 3206fb0e7bebSTejun Heo */ 3207616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3208228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3209fb0e7bebSTejun Heo 3210974271c4STejun Heo /* 32110219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 32120219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 32130219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 32140219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3215974271c4STejun Heo */ 32160219a352STejun Heo kick_pool(pool); 3217974271c4STejun Heo 32188930cabaSTejun Heo /* 32197c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3220d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 322123657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 322223657bb1STejun Heo * disabled. 32238930cabaSTejun Heo */ 3224bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, pool->id, 0); 32251da177e4SLinus Torvalds 3226fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3227a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3228365970a1SDavid Howells 3229c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3230c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 32311acd92d9STejun Heo /* see drain_dead_softirq_workfn() */ 32321acd92d9STejun Heo if (!bh_draining) 3233a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 32343295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3235e6f3faa7SPeter Zijlstra /* 3236f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3237f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3238e6f3faa7SPeter Zijlstra * 3239e6f3faa7SPeter Zijlstra * However, that would result in: 3240e6f3faa7SPeter Zijlstra * 3241e6f3faa7SPeter Zijlstra * A(W1) 3242e6f3faa7SPeter Zijlstra * WFC(C) 3243e6f3faa7SPeter Zijlstra * A(W1) 3244e6f3faa7SPeter Zijlstra * C(C) 3245e6f3faa7SPeter Zijlstra * 3246e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3247e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3248e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3249f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3250e6f3faa7SPeter Zijlstra * these locks. 3251e6f3faa7SPeter Zijlstra * 3252e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3253e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3254e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3255e6f3faa7SPeter Zijlstra */ 3256f52be570SPeter Zijlstra lockdep_invariant_state(true); 3257e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3258a2c1c57bSTejun Heo worker->current_func(work); 3259e36c886aSArjan van de Ven /* 3260e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3261e36c886aSArjan van de Ven * point will only record its address. 3262e36c886aSArjan van de Ven */ 32631c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3264725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 32653295f0efSIngo Molnar lock_map_release(&lockdep_map); 32661acd92d9STejun Heo if (!bh_draining) 3267112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 32681da177e4SLinus Torvalds 3269c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3270c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3271c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3272c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3273c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3274c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3275c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3276c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3277c35aea39STejun Heo worker->current_func); 3278d5abe669SPeter Zijlstra debug_show_held_locks(current); 3279d5abe669SPeter Zijlstra dump_stack(); 3280d5abe669SPeter Zijlstra } 3281d5abe669SPeter Zijlstra 3282b22ce278STejun Heo /* 3283025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3284b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3285b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3286b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3287789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3288789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3289b22ce278STejun Heo */ 32904cb1ef64STejun Heo if (worker->task) 3291a7e6425eSPaul E. McKenney cond_resched(); 3292b22ce278STejun Heo 3293a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3294a62428c0STejun Heo 3295616db877STejun Heo /* 3296616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3297616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3298616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3299616db877STejun Heo */ 3300fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3301fb0e7bebSTejun Heo 33021b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 33031b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 33041b69ac6bSJohannes Weiner 3305a62428c0STejun Heo /* we're done with it, release */ 330642f8570fSSasha Levin hash_del(&worker->hentry); 3307c34056a3STejun Heo worker->current_work = NULL; 3308a2c1c57bSTejun Heo worker->current_func = NULL; 3309112202d9STejun Heo worker->current_pwq = NULL; 3310d812796eSLai Jiangshan worker->current_color = INT_MAX; 3311dd6c3c54STejun Heo 3312dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3313c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 33141da177e4SLinus Torvalds } 33151da177e4SLinus Torvalds 3316affee4b2STejun Heo /** 3317affee4b2STejun Heo * process_scheduled_works - process scheduled works 3318affee4b2STejun Heo * @worker: self 3319affee4b2STejun Heo * 3320affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3321affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3322affee4b2STejun Heo * fetches a work from the top and executes it. 3323affee4b2STejun Heo * 3324affee4b2STejun Heo * CONTEXT: 3325a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3326affee4b2STejun Heo * multiple times. 3327affee4b2STejun Heo */ 3328affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 33291da177e4SLinus Torvalds { 3330c0ab017dSTejun Heo struct work_struct *work; 3331c0ab017dSTejun Heo bool first = true; 3332c0ab017dSTejun Heo 3333c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3334c0ab017dSTejun Heo struct work_struct, entry))) { 3335c0ab017dSTejun Heo if (first) { 3336c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3337c0ab017dSTejun Heo first = false; 3338c0ab017dSTejun Heo } 3339c34056a3STejun Heo process_one_work(worker, work); 3340a62428c0STejun Heo } 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 3343197f6accSTejun Heo static void set_pf_worker(bool val) 3344197f6accSTejun Heo { 3345197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3346197f6accSTejun Heo if (val) 3347197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3348197f6accSTejun Heo else 3349197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3350197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3351197f6accSTejun Heo } 3352197f6accSTejun Heo 33534690c4abSTejun Heo /** 33544690c4abSTejun Heo * worker_thread - the worker thread function 3355c34056a3STejun Heo * @__worker: self 33564690c4abSTejun Heo * 3357c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3358c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3359c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3360c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3361c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3362d185af30SYacine Belkadi * 3363d185af30SYacine Belkadi * Return: 0 33644690c4abSTejun Heo */ 3365c34056a3STejun Heo static int worker_thread(void *__worker) 33661da177e4SLinus Torvalds { 3367c34056a3STejun Heo struct worker *worker = __worker; 3368bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 33691da177e4SLinus Torvalds 3370e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3371197f6accSTejun Heo set_pf_worker(true); 3372c8e55f36STejun Heo woke_up: 3373a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3374affee4b2STejun Heo 3375a9ab775bSTejun Heo /* am I supposed to die? */ 3376a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3377a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3378197f6accSTejun Heo set_pf_worker(false); 337960f5a4bcSLai Jiangshan 338060f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3381e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3382a2d812a2STejun Heo worker_detach_from_pool(worker); 3383e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 338460f5a4bcSLai Jiangshan kfree(worker); 3385c8e55f36STejun Heo return 0; 3386c8e55f36STejun Heo } 3387c8e55f36STejun Heo 3388c8e55f36STejun Heo worker_leave_idle(worker); 3389db7bccf4STejun Heo recheck: 3390e22bee78STejun Heo /* no more worker necessary? */ 339163d95a91STejun Heo if (!need_more_worker(pool)) 3392e22bee78STejun Heo goto sleep; 3393e22bee78STejun Heo 3394e22bee78STejun Heo /* do we need to manage? */ 339563d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3396e22bee78STejun Heo goto recheck; 3397e22bee78STejun Heo 3398c8e55f36STejun Heo /* 3399c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3400c8e55f36STejun Heo * preparing to process a work or actually processing it. 3401c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3402c8e55f36STejun Heo */ 34036183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3404c8e55f36STejun Heo 3405e22bee78STejun Heo /* 3406a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3407a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3408a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3409a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3410a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3411e22bee78STejun Heo */ 3412a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3413e22bee78STejun Heo 3414e22bee78STejun Heo do { 3415affee4b2STejun Heo struct work_struct *work = 3416bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3417affee4b2STejun Heo struct work_struct, entry); 3418affee4b2STejun Heo 3419873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3420affee4b2STejun Heo process_scheduled_works(worker); 342163d95a91STejun Heo } while (keep_working(pool)); 3422affee4b2STejun Heo 3423228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3424d313dd85STejun Heo sleep: 3425c8e55f36STejun Heo /* 3426d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3427d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3428d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3429d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3430d565ed63STejun Heo * event. 3431c8e55f36STejun Heo */ 3432c8e55f36STejun Heo worker_enter_idle(worker); 3433c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3434a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 34351da177e4SLinus Torvalds schedule(); 3436c8e55f36STejun Heo goto woke_up; 34371da177e4SLinus Torvalds } 34381da177e4SLinus Torvalds 3439e22bee78STejun Heo /** 3440e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3441111c225aSTejun Heo * @__rescuer: self 3442e22bee78STejun Heo * 3443e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3444493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3445e22bee78STejun Heo * 3446706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3447e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3448e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3449e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3450e22bee78STejun Heo * the problem rescuer solves. 3451e22bee78STejun Heo * 3452706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3453706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3454e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3455e22bee78STejun Heo * 3456e22bee78STejun Heo * This should happen rarely. 3457d185af30SYacine Belkadi * 3458d185af30SYacine Belkadi * Return: 0 3459e22bee78STejun Heo */ 3460111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3461e22bee78STejun Heo { 3462111c225aSTejun Heo struct worker *rescuer = __rescuer; 3463111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 34644d595b86SLai Jiangshan bool should_stop; 3465e22bee78STejun Heo 3466e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3467111c225aSTejun Heo 3468111c225aSTejun Heo /* 3469111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3470111c225aSTejun Heo * doesn't participate in concurrency management. 3471111c225aSTejun Heo */ 3472197f6accSTejun Heo set_pf_worker(true); 3473e22bee78STejun Heo repeat: 3474c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 34751da177e4SLinus Torvalds 34764d595b86SLai Jiangshan /* 34774d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 34784d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 34794d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 34804d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 34814d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 34824d595b86SLai Jiangshan * list is always empty on exit. 34834d595b86SLai Jiangshan */ 34844d595b86SLai Jiangshan should_stop = kthread_should_stop(); 34851da177e4SLinus Torvalds 3486493a1724STejun Heo /* see whether any pwq is asking for help */ 3487a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3488493a1724STejun Heo 3489493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3490493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3491493a1724STejun Heo struct pool_workqueue, mayday_node); 3492112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3493e22bee78STejun Heo struct work_struct *work, *n; 3494e22bee78STejun Heo 3495e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3496493a1724STejun Heo list_del_init(&pwq->mayday_node); 3497493a1724STejun Heo 3498a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3499e22bee78STejun Heo 350051697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 350151697d39SLai Jiangshan 3502a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3503e22bee78STejun Heo 3504e22bee78STejun Heo /* 3505e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3506e22bee78STejun Heo * process'em. 3507e22bee78STejun Heo */ 3508873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 350982607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3510873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3511873eaca6STejun Heo assign_work(work, rescuer, &n)) 3512725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 351382607adcSTejun Heo } 3514e22bee78STejun Heo 3515873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3516e22bee78STejun Heo process_scheduled_works(rescuer); 35177576958aSTejun Heo 35187576958aSTejun Heo /* 3519008847f6SNeilBrown * The above execution of rescued work items could 3520008847f6SNeilBrown * have created more to rescue through 3521f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3522008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3523008847f6SNeilBrown * that such back-to-back work items, which may be 3524008847f6SNeilBrown * being used to relieve memory pressure, don't 3525008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3526008847f6SNeilBrown */ 35274f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3528a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3529e66b39afSTejun Heo /* 3530e66b39afSTejun Heo * Queue iff we aren't racing destruction 3531e66b39afSTejun Heo * and somebody else hasn't queued it already. 3532e66b39afSTejun Heo */ 3533e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3534008847f6SNeilBrown get_pwq(pwq); 3535e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3536e66b39afSTejun Heo } 3537a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3538008847f6SNeilBrown } 3539008847f6SNeilBrown } 3540008847f6SNeilBrown 3541008847f6SNeilBrown /* 354277668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 354313b1d625SLai Jiangshan * go away while we're still attached to it. 354477668c8bSLai Jiangshan */ 354577668c8bSLai Jiangshan put_pwq(pwq); 354677668c8bSLai Jiangshan 354777668c8bSLai Jiangshan /* 35480219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 35490219a352STejun Heo * with 0 concurrency and stalling the execution. 35507576958aSTejun Heo */ 35510219a352STejun Heo kick_pool(pool); 35527576958aSTejun Heo 3553a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 355413b1d625SLai Jiangshan 3555a2d812a2STejun Heo worker_detach_from_pool(rescuer); 355613b1d625SLai Jiangshan 3557a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 35581da177e4SLinus Torvalds } 35591da177e4SLinus Torvalds 3560a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3561493a1724STejun Heo 35624d595b86SLai Jiangshan if (should_stop) { 35634d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3564197f6accSTejun Heo set_pf_worker(false); 35654d595b86SLai Jiangshan return 0; 35664d595b86SLai Jiangshan } 35674d595b86SLai Jiangshan 3568111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3569111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3570e22bee78STejun Heo schedule(); 3571e22bee78STejun Heo goto repeat; 35721da177e4SLinus Torvalds } 35731da177e4SLinus Torvalds 35744cb1ef64STejun Heo static void bh_worker(struct worker *worker) 35754cb1ef64STejun Heo { 35764cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 35774cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 35784cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 35794cb1ef64STejun Heo 35804cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 35814cb1ef64STejun Heo worker_leave_idle(worker); 35824cb1ef64STejun Heo 35834cb1ef64STejun Heo /* 35844cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 35854cb1ef64STejun Heo * explanations on each step. 35864cb1ef64STejun Heo */ 35874cb1ef64STejun Heo if (!need_more_worker(pool)) 35884cb1ef64STejun Heo goto done; 35894cb1ef64STejun Heo 35904cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 35914cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 35924cb1ef64STejun Heo 35934cb1ef64STejun Heo do { 35944cb1ef64STejun Heo struct work_struct *work = 35954cb1ef64STejun Heo list_first_entry(&pool->worklist, 35964cb1ef64STejun Heo struct work_struct, entry); 35974cb1ef64STejun Heo 35984cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 35994cb1ef64STejun Heo process_scheduled_works(worker); 36004cb1ef64STejun Heo } while (keep_working(pool) && 36014cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 36024cb1ef64STejun Heo 36034cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 36044cb1ef64STejun Heo done: 36054cb1ef64STejun Heo worker_enter_idle(worker); 36064cb1ef64STejun Heo kick_pool(pool); 36074cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 36084cb1ef64STejun Heo } 36094cb1ef64STejun Heo 36104cb1ef64STejun Heo /* 36114cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 36124cb1ef64STejun Heo * 36134cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 36144cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 36154cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 36164cb1ef64STejun Heo * can be dropped. 36174cb1ef64STejun Heo * 36184cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 36194cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 36204cb1ef64STejun Heo */ 36214cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 36224cb1ef64STejun Heo { 36234cb1ef64STejun Heo struct worker_pool *pool = 36244cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 36254cb1ef64STejun Heo if (need_more_worker(pool)) 36264cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36274cb1ef64STejun Heo } 36284cb1ef64STejun Heo 36291acd92d9STejun Heo struct wq_drain_dead_softirq_work { 36301acd92d9STejun Heo struct work_struct work; 36311acd92d9STejun Heo struct worker_pool *pool; 36321acd92d9STejun Heo struct completion done; 36331acd92d9STejun Heo }; 36341acd92d9STejun Heo 36351acd92d9STejun Heo static void drain_dead_softirq_workfn(struct work_struct *work) 36361acd92d9STejun Heo { 36371acd92d9STejun Heo struct wq_drain_dead_softirq_work *dead_work = 36381acd92d9STejun Heo container_of(work, struct wq_drain_dead_softirq_work, work); 36391acd92d9STejun Heo struct worker_pool *pool = dead_work->pool; 36401acd92d9STejun Heo bool repeat; 36411acd92d9STejun Heo 36421acd92d9STejun Heo /* 36431acd92d9STejun Heo * @pool's CPU is dead and we want to execute its still pending work 36441acd92d9STejun Heo * items from this BH work item which is running on a different CPU. As 36451acd92d9STejun Heo * its CPU is dead, @pool can't be kicked and, as work execution path 36461acd92d9STejun Heo * will be nested, a lockdep annotation needs to be suppressed. Mark 36471acd92d9STejun Heo * @pool with %POOL_BH_DRAINING for the special treatments. 36481acd92d9STejun Heo */ 36491acd92d9STejun Heo raw_spin_lock_irq(&pool->lock); 36501acd92d9STejun Heo pool->flags |= POOL_BH_DRAINING; 36511acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock); 36521acd92d9STejun Heo 36531acd92d9STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36541acd92d9STejun Heo 36551acd92d9STejun Heo raw_spin_lock_irq(&pool->lock); 36561acd92d9STejun Heo pool->flags &= ~POOL_BH_DRAINING; 36571acd92d9STejun Heo repeat = need_more_worker(pool); 36581acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock); 36591acd92d9STejun Heo 36601acd92d9STejun Heo /* 36611acd92d9STejun Heo * bh_worker() might hit consecutive execution limit and bail. If there 36621acd92d9STejun Heo * still are pending work items, reschedule self and return so that we 36631acd92d9STejun Heo * don't hog this CPU's BH. 36641acd92d9STejun Heo */ 36651acd92d9STejun Heo if (repeat) { 36661acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 36671acd92d9STejun Heo queue_work(system_bh_highpri_wq, work); 36681acd92d9STejun Heo else 36691acd92d9STejun Heo queue_work(system_bh_wq, work); 36701acd92d9STejun Heo } else { 36711acd92d9STejun Heo complete(&dead_work->done); 36721acd92d9STejun Heo } 36731acd92d9STejun Heo } 36741acd92d9STejun Heo 36751acd92d9STejun Heo /* 36761acd92d9STejun Heo * @cpu is dead. Drain the remaining BH work items on the current CPU. It's 36771acd92d9STejun Heo * possible to allocate dead_work per CPU and avoid flushing. However, then we 36781acd92d9STejun Heo * have to worry about draining overlapping with CPU coming back online or 36791acd92d9STejun Heo * nesting (one CPU's dead_work queued on another CPU which is also dead and so 36801acd92d9STejun Heo * on). Let's keep it simple and drain them synchronously. These are BH work 36811acd92d9STejun Heo * items which shouldn't be requeued on the same pool. Shouldn't take long. 36821acd92d9STejun Heo */ 36831acd92d9STejun Heo void workqueue_softirq_dead(unsigned int cpu) 36841acd92d9STejun Heo { 36851acd92d9STejun Heo int i; 36861acd92d9STejun Heo 36871acd92d9STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 36881acd92d9STejun Heo struct worker_pool *pool = &per_cpu(bh_worker_pools, cpu)[i]; 36891acd92d9STejun Heo struct wq_drain_dead_softirq_work dead_work; 36901acd92d9STejun Heo 36911acd92d9STejun Heo if (!need_more_worker(pool)) 36921acd92d9STejun Heo continue; 36931acd92d9STejun Heo 36941acd92d9STejun Heo INIT_WORK(&dead_work.work, drain_dead_softirq_workfn); 36951acd92d9STejun Heo dead_work.pool = pool; 36961acd92d9STejun Heo init_completion(&dead_work.done); 36971acd92d9STejun Heo 36981acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 36991acd92d9STejun Heo queue_work(system_bh_highpri_wq, &dead_work.work); 37001acd92d9STejun Heo else 37011acd92d9STejun Heo queue_work(system_bh_wq, &dead_work.work); 37021acd92d9STejun Heo 37031acd92d9STejun Heo wait_for_completion(&dead_work.done); 37041acd92d9STejun Heo } 37051acd92d9STejun Heo } 37061acd92d9STejun Heo 3707fca839c0STejun Heo /** 3708fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3709fca839c0STejun Heo * @target_wq: workqueue being flushed 3710fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3711fca839c0STejun Heo * 3712fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3713fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3714fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3715fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3716fca839c0STejun Heo * a deadlock. 3717fca839c0STejun Heo */ 3718fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3719fca839c0STejun Heo struct work_struct *target_work) 3720fca839c0STejun Heo { 3721fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3722fca839c0STejun Heo struct worker *worker; 3723fca839c0STejun Heo 3724fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3725fca839c0STejun Heo return; 3726fca839c0STejun Heo 3727fca839c0STejun Heo worker = current_wq_worker(); 3728fca839c0STejun Heo 3729fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3730d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3731fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 373223d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 373323d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3734d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3735fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3736fca839c0STejun Heo target_wq->name, target_func); 3737fca839c0STejun Heo } 3738fca839c0STejun Heo 3739fc2e4d70SOleg Nesterov struct wq_barrier { 3740fc2e4d70SOleg Nesterov struct work_struct work; 3741fc2e4d70SOleg Nesterov struct completion done; 37422607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3743fc2e4d70SOleg Nesterov }; 3744fc2e4d70SOleg Nesterov 3745fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3746fc2e4d70SOleg Nesterov { 3747fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3748fc2e4d70SOleg Nesterov complete(&barr->done); 3749fc2e4d70SOleg Nesterov } 3750fc2e4d70SOleg Nesterov 37514690c4abSTejun Heo /** 37524690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3753112202d9STejun Heo * @pwq: pwq to insert barrier into 37544690c4abSTejun Heo * @barr: wq_barrier to insert 3755affee4b2STejun Heo * @target: target work to attach @barr to 3756affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 37574690c4abSTejun Heo * 3758affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3759affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3760affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3761affee4b2STejun Heo * cpu. 3762affee4b2STejun Heo * 3763affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3764affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3765affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3766affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3767affee4b2STejun Heo * after a work with LINKED flag set. 3768affee4b2STejun Heo * 3769affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3770112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 37714690c4abSTejun Heo * 37724690c4abSTejun Heo * CONTEXT: 3773a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 37744690c4abSTejun Heo */ 3775112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3776affee4b2STejun Heo struct wq_barrier *barr, 3777affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3778fc2e4d70SOleg Nesterov { 37794cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3780d812796eSLai Jiangshan unsigned int work_flags = 0; 3781d812796eSLai Jiangshan unsigned int work_color; 3782affee4b2STejun Heo struct list_head *head; 3783affee4b2STejun Heo 3784dc186ad7SThomas Gleixner /* 3785d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3786dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3787dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3788dc186ad7SThomas Gleixner * might deadlock. 37894cb1ef64STejun Heo * 37904cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 37914cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 37924cb1ef64STejun Heo * usage". 3793dc186ad7SThomas Gleixner */ 37944cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 37954cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 379622df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 379752fa5bc5SBoqun Feng 3798fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3799fd1a5b04SByungchul Park 38002607d7a6STejun Heo barr->task = current; 380183c22520SOleg Nesterov 38025797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3803018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3804018f3a13SLai Jiangshan 3805affee4b2STejun Heo /* 3806affee4b2STejun Heo * If @target is currently being executed, schedule the 3807affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3808affee4b2STejun Heo */ 3809d812796eSLai Jiangshan if (worker) { 3810affee4b2STejun Heo head = worker->scheduled.next; 3811d812796eSLai Jiangshan work_color = worker->current_color; 3812d812796eSLai Jiangshan } else { 3813affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3814affee4b2STejun Heo 3815affee4b2STejun Heo head = target->entry.next; 3816affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3817d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3818d812796eSLai Jiangshan work_color = get_work_color(*bits); 3819affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3820affee4b2STejun Heo } 3821affee4b2STejun Heo 3822d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3823d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3824d812796eSLai Jiangshan 3825d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3826fc2e4d70SOleg Nesterov } 3827fc2e4d70SOleg Nesterov 382873f53c4aSTejun Heo /** 3829112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 383073f53c4aSTejun Heo * @wq: workqueue being flushed 383173f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 383273f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 383373f53c4aSTejun Heo * 3834112202d9STejun Heo * Prepare pwqs for workqueue flushing. 383573f53c4aSTejun Heo * 3836112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3837112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3838112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3839112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3840112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 384173f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 384273f53c4aSTejun Heo * 384373f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 384473f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 384573f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 384673f53c4aSTejun Heo * is returned. 384773f53c4aSTejun Heo * 3848112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 384973f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 385073f53c4aSTejun Heo * advanced to @work_color. 385173f53c4aSTejun Heo * 385273f53c4aSTejun Heo * CONTEXT: 38533c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 385473f53c4aSTejun Heo * 3855d185af30SYacine Belkadi * Return: 385673f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 385773f53c4aSTejun Heo * otherwise. 385873f53c4aSTejun Heo */ 3859112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 386073f53c4aSTejun Heo int flush_color, int work_color) 38611da177e4SLinus Torvalds { 386273f53c4aSTejun Heo bool wait = false; 386349e3cf44STejun Heo struct pool_workqueue *pwq; 38641da177e4SLinus Torvalds 386573f53c4aSTejun Heo if (flush_color >= 0) { 38666183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3867112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3868dc186ad7SThomas Gleixner } 386914441960SOleg Nesterov 387049e3cf44STejun Heo for_each_pwq(pwq, wq) { 3871112202d9STejun Heo struct worker_pool *pool = pwq->pool; 38721da177e4SLinus Torvalds 3873a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 387473f53c4aSTejun Heo 387573f53c4aSTejun Heo if (flush_color >= 0) { 38766183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 387773f53c4aSTejun Heo 3878112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3879112202d9STejun Heo pwq->flush_color = flush_color; 3880112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 388173f53c4aSTejun Heo wait = true; 38821da177e4SLinus Torvalds } 388373f53c4aSTejun Heo } 388473f53c4aSTejun Heo 388573f53c4aSTejun Heo if (work_color >= 0) { 38866183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3887112202d9STejun Heo pwq->work_color = work_color; 388873f53c4aSTejun Heo } 388973f53c4aSTejun Heo 3890a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 38911da177e4SLinus Torvalds } 38921da177e4SLinus Torvalds 3893112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 389473f53c4aSTejun Heo complete(&wq->first_flusher->done); 389573f53c4aSTejun Heo 389673f53c4aSTejun Heo return wait; 389783c22520SOleg Nesterov } 38981da177e4SLinus Torvalds 3899c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3900c35aea39STejun Heo { 39014cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 39024cb1ef64STejun Heo if (wq->flags & WQ_BH) 39034cb1ef64STejun Heo local_bh_disable(); 39044cb1ef64STejun Heo 3905c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3906c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 39074cb1ef64STejun Heo 39084cb1ef64STejun Heo if (wq->flags & WQ_BH) 39094cb1ef64STejun Heo local_bh_enable(); 39104cb1ef64STejun Heo #endif 3911c35aea39STejun Heo } 3912c35aea39STejun Heo 3913c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3914c35aea39STejun Heo struct workqueue_struct *wq) 3915c35aea39STejun Heo { 39164cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 39174cb1ef64STejun Heo if (wq->flags & WQ_BH) 39184cb1ef64STejun Heo local_bh_disable(); 39194cb1ef64STejun Heo 3920c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3921c35aea39STejun Heo lock_map_release(&work->lockdep_map); 39224cb1ef64STejun Heo 39234cb1ef64STejun Heo if (wq->flags & WQ_BH) 39244cb1ef64STejun Heo local_bh_enable(); 39254cb1ef64STejun Heo #endif 3926c35aea39STejun Heo } 3927c35aea39STejun Heo 39280fcb78c2SRolf Eike Beer /** 3929c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 39300fcb78c2SRolf Eike Beer * @wq: workqueue to flush 39311da177e4SLinus Torvalds * 3932c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3933c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 39341da177e4SLinus Torvalds */ 3935c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 39361da177e4SLinus Torvalds { 393773f53c4aSTejun Heo struct wq_flusher this_flusher = { 393873f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 393973f53c4aSTejun Heo .flush_color = -1, 3940fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 394173f53c4aSTejun Heo }; 394273f53c4aSTejun Heo int next_color; 3943b1f4ec17SOleg Nesterov 39443347fa09STejun Heo if (WARN_ON(!wq_online)) 39453347fa09STejun Heo return; 39463347fa09STejun Heo 3947c35aea39STejun Heo touch_wq_lockdep_map(wq); 394887915adcSJohannes Berg 39493c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 395073f53c4aSTejun Heo 395173f53c4aSTejun Heo /* 395273f53c4aSTejun Heo * Start-to-wait phase 395373f53c4aSTejun Heo */ 395473f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 395573f53c4aSTejun Heo 395673f53c4aSTejun Heo if (next_color != wq->flush_color) { 395773f53c4aSTejun Heo /* 395873f53c4aSTejun Heo * Color space is not full. The current work_color 395973f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 396073f53c4aSTejun Heo * by one. 396173f53c4aSTejun Heo */ 39626183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 396373f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 396473f53c4aSTejun Heo wq->work_color = next_color; 396573f53c4aSTejun Heo 396673f53c4aSTejun Heo if (!wq->first_flusher) { 396773f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 39686183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 396973f53c4aSTejun Heo 397073f53c4aSTejun Heo wq->first_flusher = &this_flusher; 397173f53c4aSTejun Heo 3972112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 397373f53c4aSTejun Heo wq->work_color)) { 397473f53c4aSTejun Heo /* nothing to flush, done */ 397573f53c4aSTejun Heo wq->flush_color = next_color; 397673f53c4aSTejun Heo wq->first_flusher = NULL; 397773f53c4aSTejun Heo goto out_unlock; 397873f53c4aSTejun Heo } 397973f53c4aSTejun Heo } else { 398073f53c4aSTejun Heo /* wait in queue */ 39816183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 398273f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3983112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 398473f53c4aSTejun Heo } 398573f53c4aSTejun Heo } else { 398673f53c4aSTejun Heo /* 398773f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 398873f53c4aSTejun Heo * The next flush completion will assign us 398973f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 399073f53c4aSTejun Heo */ 399173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 399273f53c4aSTejun Heo } 399373f53c4aSTejun Heo 3994fca839c0STejun Heo check_flush_dependency(wq, NULL); 3995fca839c0STejun Heo 39963c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 399773f53c4aSTejun Heo 399873f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 399973f53c4aSTejun Heo 400073f53c4aSTejun Heo /* 400173f53c4aSTejun Heo * Wake-up-and-cascade phase 400273f53c4aSTejun Heo * 400373f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 400473f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 400573f53c4aSTejun Heo */ 400600d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 400773f53c4aSTejun Heo return; 400873f53c4aSTejun Heo 40093c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 401073f53c4aSTejun Heo 40114ce48b37STejun Heo /* we might have raced, check again with mutex held */ 40124ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 40134ce48b37STejun Heo goto out_unlock; 40144ce48b37STejun Heo 401500d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 401673f53c4aSTejun Heo 40176183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 40186183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 401973f53c4aSTejun Heo 402073f53c4aSTejun Heo while (true) { 402173f53c4aSTejun Heo struct wq_flusher *next, *tmp; 402273f53c4aSTejun Heo 402373f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 402473f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 402573f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 402673f53c4aSTejun Heo break; 402773f53c4aSTejun Heo list_del_init(&next->list); 402873f53c4aSTejun Heo complete(&next->done); 402973f53c4aSTejun Heo } 403073f53c4aSTejun Heo 40316183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 403273f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 403373f53c4aSTejun Heo 403473f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 403573f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 403673f53c4aSTejun Heo 403773f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 403873f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 403973f53c4aSTejun Heo /* 404073f53c4aSTejun Heo * Assign the same color to all overflowed 404173f53c4aSTejun Heo * flushers, advance work_color and append to 404273f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 404373f53c4aSTejun Heo * phase for these overflowed flushers. 404473f53c4aSTejun Heo */ 404573f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 404673f53c4aSTejun Heo tmp->flush_color = wq->work_color; 404773f53c4aSTejun Heo 404873f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 404973f53c4aSTejun Heo 405073f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 405173f53c4aSTejun Heo &wq->flusher_queue); 4052112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 405373f53c4aSTejun Heo } 405473f53c4aSTejun Heo 405573f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 40566183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 405773f53c4aSTejun Heo break; 405873f53c4aSTejun Heo } 405973f53c4aSTejun Heo 406073f53c4aSTejun Heo /* 406173f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 4062112202d9STejun Heo * the new first flusher and arm pwqs. 406373f53c4aSTejun Heo */ 40646183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 40656183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 406673f53c4aSTejun Heo 406773f53c4aSTejun Heo list_del_init(&next->list); 406873f53c4aSTejun Heo wq->first_flusher = next; 406973f53c4aSTejun Heo 4070112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 407173f53c4aSTejun Heo break; 407273f53c4aSTejun Heo 407373f53c4aSTejun Heo /* 407473f53c4aSTejun Heo * Meh... this color is already done, clear first 407573f53c4aSTejun Heo * flusher and repeat cascading. 407673f53c4aSTejun Heo */ 407773f53c4aSTejun Heo wq->first_flusher = NULL; 407873f53c4aSTejun Heo } 407973f53c4aSTejun Heo 408073f53c4aSTejun Heo out_unlock: 40813c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 40821da177e4SLinus Torvalds } 4083c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 40841da177e4SLinus Torvalds 40859c5a2ba7STejun Heo /** 40869c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 40879c5a2ba7STejun Heo * @wq: workqueue to drain 40889c5a2ba7STejun Heo * 40899c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 40909c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 40919c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 4092b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 40939c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 40949c5a2ba7STejun Heo * takes too long. 40959c5a2ba7STejun Heo */ 40969c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 40979c5a2ba7STejun Heo { 40989c5a2ba7STejun Heo unsigned int flush_cnt = 0; 409949e3cf44STejun Heo struct pool_workqueue *pwq; 41009c5a2ba7STejun Heo 41019c5a2ba7STejun Heo /* 41029c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 41039c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 4104618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 41059c5a2ba7STejun Heo */ 410687fc741eSLai Jiangshan mutex_lock(&wq->mutex); 41079c5a2ba7STejun Heo if (!wq->nr_drainers++) 4108618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 410987fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 41109c5a2ba7STejun Heo reflush: 4111c4f135d6STetsuo Handa __flush_workqueue(wq); 41129c5a2ba7STejun Heo 4113b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 411476af4d93STejun Heo 411549e3cf44STejun Heo for_each_pwq(pwq, wq) { 4116fa2563e4SThomas Tuttle bool drained; 41179c5a2ba7STejun Heo 4118a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4119afa87ce8STejun Heo drained = pwq_is_empty(pwq); 4120a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4121fa2563e4SThomas Tuttle 4122fa2563e4SThomas Tuttle if (drained) 41239c5a2ba7STejun Heo continue; 41249c5a2ba7STejun Heo 41259c5a2ba7STejun Heo if (++flush_cnt == 10 || 41269c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 4127e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 4128e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 412976af4d93STejun Heo 4130b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 41319c5a2ba7STejun Heo goto reflush; 41329c5a2ba7STejun Heo } 41339c5a2ba7STejun Heo 41349c5a2ba7STejun Heo if (!--wq->nr_drainers) 4135618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 413687fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 41379c5a2ba7STejun Heo } 41389c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 41399c5a2ba7STejun Heo 4140d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 4141d6e89786SJohannes Berg bool from_cancel) 4142baf59022STejun Heo { 4143baf59022STejun Heo struct worker *worker = NULL; 4144c9e7cf27STejun Heo struct worker_pool *pool; 4145112202d9STejun Heo struct pool_workqueue *pwq; 4146c35aea39STejun Heo struct workqueue_struct *wq; 4147baf59022STejun Heo 4148baf59022STejun Heo might_sleep(); 4149baf59022STejun Heo 415024acfb71SThomas Gleixner rcu_read_lock(); 4151fa1b54e6STejun Heo pool = get_work_pool(work); 4152fa1b54e6STejun Heo if (!pool) { 415324acfb71SThomas Gleixner rcu_read_unlock(); 4154fa1b54e6STejun Heo return false; 4155fa1b54e6STejun Heo } 4156fa1b54e6STejun Heo 4157a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 41580b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 4159112202d9STejun Heo pwq = get_work_pwq(work); 4160112202d9STejun Heo if (pwq) { 4161112202d9STejun Heo if (unlikely(pwq->pool != pool)) 4162baf59022STejun Heo goto already_gone; 4163606a5020STejun Heo } else { 4164c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 4165baf59022STejun Heo if (!worker) 4166baf59022STejun Heo goto already_gone; 4167112202d9STejun Heo pwq = worker->current_pwq; 4168606a5020STejun Heo } 4169baf59022STejun Heo 4170c35aea39STejun Heo wq = pwq->wq; 4171c35aea39STejun Heo check_flush_dependency(wq, work); 4172fca839c0STejun Heo 4173112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 4174a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4175baf59022STejun Heo 4176c35aea39STejun Heo touch_work_lockdep_map(work, wq); 4177c35aea39STejun Heo 4178e159489bSTejun Heo /* 4179a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 4180a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 4181a1d14934SPeter Zijlstra * 4182a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 4183a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 4184a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 4185a1d14934SPeter Zijlstra * forward progress. 4186e159489bSTejun Heo */ 4187c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 4188c35aea39STejun Heo touch_wq_lockdep_map(wq); 4189c35aea39STejun Heo 419024acfb71SThomas Gleixner rcu_read_unlock(); 4191baf59022STejun Heo return true; 4192baf59022STejun Heo already_gone: 4193a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 419424acfb71SThomas Gleixner rcu_read_unlock(); 4195baf59022STejun Heo return false; 4196baf59022STejun Heo } 4197baf59022STejun Heo 4198d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4199d6e89786SJohannes Berg { 4200d6e89786SJohannes Berg struct wq_barrier barr; 4201d6e89786SJohannes Berg 4202d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4203d6e89786SJohannes Berg return false; 4204d6e89786SJohannes Berg 42054d43d395STetsuo Handa if (WARN_ON(!work->func)) 42064d43d395STetsuo Handa return false; 42074d43d395STetsuo Handa 4208d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4209d6e89786SJohannes Berg wait_for_completion(&barr.done); 4210d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4211d6e89786SJohannes Berg return true; 4212d6e89786SJohannes Berg } else { 4213d6e89786SJohannes Berg return false; 4214d6e89786SJohannes Berg } 4215d6e89786SJohannes Berg } 4216d6e89786SJohannes Berg 4217db700897SOleg Nesterov /** 4218401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4219401a8d04STejun Heo * @work: the work to flush 4220db700897SOleg Nesterov * 4221606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4222606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4223401a8d04STejun Heo * 4224d185af30SYacine Belkadi * Return: 4225401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4226401a8d04STejun Heo * %false if it was already idle. 4227db700897SOleg Nesterov */ 4228401a8d04STejun Heo bool flush_work(struct work_struct *work) 4229db700897SOleg Nesterov { 4230d6e89786SJohannes Berg return __flush_work(work, false); 4231606a5020STejun Heo } 4232db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4233db700897SOleg Nesterov 4234cdc6e4b3STejun Heo /** 4235cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4236cdc6e4b3STejun Heo * @dwork: the delayed work to flush 4237cdc6e4b3STejun Heo * 4238cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for 4239cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only 4240cdc6e4b3STejun Heo * considers the last queueing instance of @dwork. 4241cdc6e4b3STejun Heo * 4242cdc6e4b3STejun Heo * Return: 4243cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution, 4244cdc6e4b3STejun Heo * %false if it was already idle. 4245cdc6e4b3STejun Heo */ 4246cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4247cdc6e4b3STejun Heo { 4248cdc6e4b3STejun Heo local_irq_disable(); 4249cdc6e4b3STejun Heo if (del_timer_sync(&dwork->timer)) 4250cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work); 4251cdc6e4b3STejun Heo local_irq_enable(); 4252cdc6e4b3STejun Heo return flush_work(&dwork->work); 4253cdc6e4b3STejun Heo } 4254cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4255cdc6e4b3STejun Heo 4256cdc6e4b3STejun Heo /** 4257cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 4258cdc6e4b3STejun Heo * @rwork: the rcu work to flush 4259cdc6e4b3STejun Heo * 4260cdc6e4b3STejun Heo * Return: 4261cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 4262cdc6e4b3STejun Heo * %false if it was already idle. 4263cdc6e4b3STejun Heo */ 4264cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork) 4265cdc6e4b3STejun Heo { 4266cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 4267cdc6e4b3STejun Heo rcu_barrier(); 4268cdc6e4b3STejun Heo flush_work(&rwork->work); 4269cdc6e4b3STejun Heo return true; 4270cdc6e4b3STejun Heo } else { 4271cdc6e4b3STejun Heo return flush_work(&rwork->work); 4272cdc6e4b3STejun Heo } 4273cdc6e4b3STejun Heo } 4274cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work); 4275cdc6e4b3STejun Heo 4276c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags) 4277cdc6e4b3STejun Heo { 4278c26e2f2eSTejun Heo unsigned long irq_flags; 4279cdc6e4b3STejun Heo int ret; 4280cdc6e4b3STejun Heo 4281cdc6e4b3STejun Heo do { 4282c5f5b942STejun Heo ret = try_to_grab_pending(work, cflags, &irq_flags); 4283cdc6e4b3STejun Heo } while (unlikely(ret == -EAGAIN)); 4284cdc6e4b3STejun Heo 4285cdc6e4b3STejun Heo if (unlikely(ret < 0)) 4286cdc6e4b3STejun Heo return false; 4287cdc6e4b3STejun Heo 4288bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, get_work_pool_id(work), 0); 4289c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4290cdc6e4b3STejun Heo return ret; 4291cdc6e4b3STejun Heo } 4292cdc6e4b3STejun Heo 4293c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags) 4294401a8d04STejun Heo { 4295c26e2f2eSTejun Heo unsigned long irq_flags; 4296978b8409STejun Heo bool ret; 42971f1f642eSOleg Nesterov 4298978b8409STejun Heo /* claim @work and tell other tasks trying to grab @work to back off */ 4299978b8409STejun Heo ret = work_grab_pending(work, cflags, &irq_flags); 4300bbb68dfaSTejun Heo mark_work_canceling(work); 4301c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4302bbb68dfaSTejun Heo 43033347fa09STejun Heo /* 4304c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't 4305c7a40c49STejun Heo * executing. This allows canceling during early boot. 43063347fa09STejun Heo */ 43073347fa09STejun Heo if (wq_online) 4308d6e89786SJohannes Berg __flush_work(work, true); 43093347fa09STejun Heo 43108603e1b3STejun Heo /* 4311afe928c1STejun Heo * smp_mb() at the end of set_work_pool_and_clear_pending() is paired 4312afe928c1STejun Heo * with prepare_to_wait() above so that either waitqueue_active() is 4313afe928c1STejun Heo * visible here or !work_is_canceling() is visible there. 43148603e1b3STejun Heo */ 4315bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, WORK_OFFQ_POOL_NONE, 0); 4316afe928c1STejun Heo 4317978b8409STejun Heo if (waitqueue_active(&wq_cancel_waitq)) 4318978b8409STejun Heo __wake_up(&wq_cancel_waitq, TASK_NORMAL, 1, work); 43198603e1b3STejun Heo 43201f1f642eSOleg Nesterov return ret; 43211f1f642eSOleg Nesterov } 43221f1f642eSOleg Nesterov 4323cdc6e4b3STejun Heo /* 4324cdc6e4b3STejun Heo * See cancel_delayed_work() 4325cdc6e4b3STejun Heo */ 4326cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work) 4327cdc6e4b3STejun Heo { 4328c5f5b942STejun Heo return __cancel_work(work, 0); 4329cdc6e4b3STejun Heo } 4330cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work); 4331cdc6e4b3STejun Heo 43326e84d644SOleg Nesterov /** 4333401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4334401a8d04STejun Heo * @work: the work to cancel 43356e84d644SOleg Nesterov * 4336401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4337401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4338401a8d04STejun Heo * another workqueue. On return from this function, @work is 4339401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 43401f1f642eSOleg Nesterov * 4341401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4342401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 43436e84d644SOleg Nesterov * 4344401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 43456e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4346401a8d04STejun Heo * 4347d185af30SYacine Belkadi * Return: 4348401a8d04STejun Heo * %true if @work was pending, %false otherwise. 43496e84d644SOleg Nesterov */ 4350401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 43516e84d644SOleg Nesterov { 4352c5f5b942STejun Heo return __cancel_work_sync(work, 0); 4353b89deed3SOleg Nesterov } 435428e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4355b89deed3SOleg Nesterov 43566e84d644SOleg Nesterov /** 435757b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 435857b30ae7STejun Heo * @dwork: delayed_work to cancel 435909383498STejun Heo * 4360d185af30SYacine Belkadi * Kill off a pending delayed_work. 4361d185af30SYacine Belkadi * 4362d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4363d185af30SYacine Belkadi * pending. 4364d185af30SYacine Belkadi * 4365d185af30SYacine Belkadi * Note: 4366d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4367d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4368d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 436909383498STejun Heo * 437057b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 437109383498STejun Heo */ 437257b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 437309383498STejun Heo { 4374c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED); 437509383498STejun Heo } 437657b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 437709383498STejun Heo 437809383498STejun Heo /** 4379401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4380401a8d04STejun Heo * @dwork: the delayed work cancel 4381401a8d04STejun Heo * 4382401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4383401a8d04STejun Heo * 4384d185af30SYacine Belkadi * Return: 4385401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4386401a8d04STejun Heo */ 4387401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 43886e84d644SOleg Nesterov { 4389c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED); 43906e84d644SOleg Nesterov } 4391f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 43921da177e4SLinus Torvalds 43930fcb78c2SRolf Eike Beer /** 439431ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4395b6136773SAndrew Morton * @func: the function to call 4396b6136773SAndrew Morton * 439731ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 439831ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4399b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 440031ddd871STejun Heo * 4401d185af30SYacine Belkadi * Return: 440231ddd871STejun Heo * 0 on success, -errno on failure. 4403b6136773SAndrew Morton */ 440465f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 440515316ba8SChristoph Lameter { 440615316ba8SChristoph Lameter int cpu; 440738f51568SNamhyung Kim struct work_struct __percpu *works; 440815316ba8SChristoph Lameter 4409b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4410b6136773SAndrew Morton if (!works) 441115316ba8SChristoph Lameter return -ENOMEM; 4412b6136773SAndrew Morton 4413ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 441493981800STejun Heo 441515316ba8SChristoph Lameter for_each_online_cpu(cpu) { 44169bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 44179bfb1839SIngo Molnar 44189bfb1839SIngo Molnar INIT_WORK(work, func); 44198de6d308SOleg Nesterov schedule_work_on(cpu, work); 442015316ba8SChristoph Lameter } 442193981800STejun Heo 442293981800STejun Heo for_each_online_cpu(cpu) 44238616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 442493981800STejun Heo 4425ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4426b6136773SAndrew Morton free_percpu(works); 442715316ba8SChristoph Lameter return 0; 442815316ba8SChristoph Lameter } 442915316ba8SChristoph Lameter 4430eef6a7d5SAlan Stern /** 44311fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 44321fa44ecaSJames Bottomley * @fn: the function to execute 44331fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 44341fa44ecaSJames Bottomley * be available when the work executes) 44351fa44ecaSJames Bottomley * 44361fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 44371fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 44381fa44ecaSJames Bottomley * 4439d185af30SYacine Belkadi * Return: 0 - function was executed 44401fa44ecaSJames Bottomley * 1 - function was scheduled for execution 44411fa44ecaSJames Bottomley */ 444265f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 44431fa44ecaSJames Bottomley { 44441fa44ecaSJames Bottomley if (!in_interrupt()) { 444565f27f38SDavid Howells fn(&ew->work); 44461fa44ecaSJames Bottomley return 0; 44471fa44ecaSJames Bottomley } 44481fa44ecaSJames Bottomley 444965f27f38SDavid Howells INIT_WORK(&ew->work, fn); 44501fa44ecaSJames Bottomley schedule_work(&ew->work); 44511fa44ecaSJames Bottomley 44521fa44ecaSJames Bottomley return 1; 44531fa44ecaSJames Bottomley } 44541fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 44551fa44ecaSJames Bottomley 44567a4e344cSTejun Heo /** 44577a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 44587a4e344cSTejun Heo * @attrs: workqueue_attrs to free 44597a4e344cSTejun Heo * 44607a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 44617a4e344cSTejun Heo */ 4462513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 44637a4e344cSTejun Heo { 44647a4e344cSTejun Heo if (attrs) { 44657a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 44669546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 44677a4e344cSTejun Heo kfree(attrs); 44687a4e344cSTejun Heo } 44697a4e344cSTejun Heo } 44707a4e344cSTejun Heo 44717a4e344cSTejun Heo /** 44727a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 44737a4e344cSTejun Heo * 44747a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4475d185af30SYacine Belkadi * return it. 4476d185af30SYacine Belkadi * 4477d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 44787a4e344cSTejun Heo */ 4479513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 44807a4e344cSTejun Heo { 44817a4e344cSTejun Heo struct workqueue_attrs *attrs; 44827a4e344cSTejun Heo 4483be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 44847a4e344cSTejun Heo if (!attrs) 44857a4e344cSTejun Heo goto fail; 4486be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 44877a4e344cSTejun Heo goto fail; 44889546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 44899546b29eSTejun Heo goto fail; 44907a4e344cSTejun Heo 449113e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4492523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 44937a4e344cSTejun Heo return attrs; 44947a4e344cSTejun Heo fail: 44957a4e344cSTejun Heo free_workqueue_attrs(attrs); 44967a4e344cSTejun Heo return NULL; 44977a4e344cSTejun Heo } 44987a4e344cSTejun Heo 449929c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 450029c91e99STejun Heo const struct workqueue_attrs *from) 450129c91e99STejun Heo { 450229c91e99STejun Heo to->nice = from->nice; 450329c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 45049546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 45058639ecebSTejun Heo to->affn_strict = from->affn_strict; 450684193c07STejun Heo 45072865a8fbSShaohua Li /* 450884193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 450984193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 451084193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 45112865a8fbSShaohua Li */ 451284193c07STejun Heo to->affn_scope = from->affn_scope; 4513af73f5c9STejun Heo to->ordered = from->ordered; 451429c91e99STejun Heo } 451529c91e99STejun Heo 45165de7a03cSTejun Heo /* 45175de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 45185de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 45195de7a03cSTejun Heo */ 45205de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 45215de7a03cSTejun Heo { 452284193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 45235de7a03cSTejun Heo attrs->ordered = false; 45245de7a03cSTejun Heo } 45255de7a03cSTejun Heo 452629c91e99STejun Heo /* hash value of the content of @attr */ 452729c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 452829c91e99STejun Heo { 452929c91e99STejun Heo u32 hash = 0; 453029c91e99STejun Heo 453129c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 453213e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 453313e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 45349546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 45359546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 45368639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 453729c91e99STejun Heo return hash; 453829c91e99STejun Heo } 453929c91e99STejun Heo 454029c91e99STejun Heo /* content equality test */ 454129c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 454229c91e99STejun Heo const struct workqueue_attrs *b) 454329c91e99STejun Heo { 454429c91e99STejun Heo if (a->nice != b->nice) 454529c91e99STejun Heo return false; 454629c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 454729c91e99STejun Heo return false; 45489546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 45499546b29eSTejun Heo return false; 45508639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 45518639ecebSTejun Heo return false; 455229c91e99STejun Heo return true; 455329c91e99STejun Heo } 455429c91e99STejun Heo 45550f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 45560f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 45570f36ee24STejun Heo const cpumask_t *unbound_cpumask) 45580f36ee24STejun Heo { 45590f36ee24STejun Heo /* 45600f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 45610f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 45620f36ee24STejun Heo * @unbound_cpumask. 45630f36ee24STejun Heo */ 45640f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 45650f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 45660f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 45670f36ee24STejun Heo } 45680f36ee24STejun Heo 456984193c07STejun Heo /* find wq_pod_type to use for @attrs */ 457084193c07STejun Heo static const struct wq_pod_type * 457184193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 457284193c07STejun Heo { 4573523a301eSTejun Heo enum wq_affn_scope scope; 4574523a301eSTejun Heo struct wq_pod_type *pt; 4575523a301eSTejun Heo 4576523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4577523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4578523a301eSTejun Heo 4579523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4580523a301eSTejun Heo scope = wq_affn_dfl; 4581523a301eSTejun Heo else 4582523a301eSTejun Heo scope = attrs->affn_scope; 4583523a301eSTejun Heo 4584523a301eSTejun Heo pt = &wq_pod_types[scope]; 458584193c07STejun Heo 458684193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 458784193c07STejun Heo likely(pt->nr_pods)) 458884193c07STejun Heo return pt; 458984193c07STejun Heo 459084193c07STejun Heo /* 459184193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 459284193c07STejun Heo * initialized in workqueue_init_early(). 459384193c07STejun Heo */ 459484193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 459584193c07STejun Heo BUG_ON(!pt->nr_pods); 459684193c07STejun Heo return pt; 459784193c07STejun Heo } 459884193c07STejun Heo 45997a4e344cSTejun Heo /** 46007a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 46017a4e344cSTejun Heo * @pool: worker_pool to initialize 46027a4e344cSTejun Heo * 4603402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4604d185af30SYacine Belkadi * 4605d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 460629c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 460729c91e99STejun Heo * on @pool safely to release it. 46087a4e344cSTejun Heo */ 46097a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 46104e1a1f9aSTejun Heo { 4611a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 461229c91e99STejun Heo pool->id = -1; 461329c91e99STejun Heo pool->cpu = -1; 4614f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 46154e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 461682607adcSTejun Heo pool->watchdog_ts = jiffies; 46174e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 46184e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 46194e1a1f9aSTejun Heo hash_init(pool->busy_hash); 46204e1a1f9aSTejun Heo 462132a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 46223f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 46234e1a1f9aSTejun Heo 462432a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 46254e1a1f9aSTejun Heo 4626da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4627e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 46287a4e344cSTejun Heo 46297cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 463029c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 463129c91e99STejun Heo pool->refcnt = 1; 463229c91e99STejun Heo 463329c91e99STejun Heo /* shouldn't fail above this point */ 4634be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 46357a4e344cSTejun Heo if (!pool->attrs) 46367a4e344cSTejun Heo return -ENOMEM; 46375de7a03cSTejun Heo 46385de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 46395de7a03cSTejun Heo 46407a4e344cSTejun Heo return 0; 46414e1a1f9aSTejun Heo } 46424e1a1f9aSTejun Heo 4643669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4644669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4645669de8bdSBart Van Assche { 4646669de8bdSBart Van Assche char *lock_name; 4647669de8bdSBart Van Assche 4648669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4649669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4650669de8bdSBart Van Assche if (!lock_name) 4651669de8bdSBart Van Assche lock_name = wq->name; 465269a106c0SQian Cai 465369a106c0SQian Cai wq->lock_name = lock_name; 4654669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4655669de8bdSBart Van Assche } 4656669de8bdSBart Van Assche 4657669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4658669de8bdSBart Van Assche { 4659669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4660669de8bdSBart Van Assche } 4661669de8bdSBart Van Assche 4662669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4663669de8bdSBart Van Assche { 4664669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4665669de8bdSBart Van Assche kfree(wq->lock_name); 4666669de8bdSBart Van Assche } 4667669de8bdSBart Van Assche #else 4668669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4669669de8bdSBart Van Assche { 4670669de8bdSBart Van Assche } 4671669de8bdSBart Van Assche 4672669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4673669de8bdSBart Van Assche { 4674669de8bdSBart Van Assche } 4675669de8bdSBart Van Assche 4676669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4677669de8bdSBart Van Assche { 4678669de8bdSBart Van Assche } 4679669de8bdSBart Van Assche #endif 4680669de8bdSBart Van Assche 468191ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 468291ccc6e7STejun Heo { 468391ccc6e7STejun Heo int node; 468491ccc6e7STejun Heo 468591ccc6e7STejun Heo for_each_node(node) { 468691ccc6e7STejun Heo kfree(nna_ar[node]); 468791ccc6e7STejun Heo nna_ar[node] = NULL; 468891ccc6e7STejun Heo } 468991ccc6e7STejun Heo 469091ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 469191ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 469291ccc6e7STejun Heo } 469391ccc6e7STejun Heo 469491ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 469591ccc6e7STejun Heo { 4696c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 469791ccc6e7STejun Heo atomic_set(&nna->nr, 0); 46985797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 46995797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 470091ccc6e7STejun Heo } 470191ccc6e7STejun Heo 470291ccc6e7STejun Heo /* 470391ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 470491ccc6e7STejun Heo * should be allocated in the node. 470591ccc6e7STejun Heo */ 470691ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 470791ccc6e7STejun Heo { 470891ccc6e7STejun Heo struct wq_node_nr_active *nna; 470991ccc6e7STejun Heo int node; 471091ccc6e7STejun Heo 471191ccc6e7STejun Heo for_each_node(node) { 471291ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 471391ccc6e7STejun Heo if (!nna) 471491ccc6e7STejun Heo goto err_free; 471591ccc6e7STejun Heo init_node_nr_active(nna); 471691ccc6e7STejun Heo nna_ar[node] = nna; 471791ccc6e7STejun Heo } 471891ccc6e7STejun Heo 471991ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 472091ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 472191ccc6e7STejun Heo if (!nna) 472291ccc6e7STejun Heo goto err_free; 472391ccc6e7STejun Heo init_node_nr_active(nna); 472491ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 472591ccc6e7STejun Heo 472691ccc6e7STejun Heo return 0; 472791ccc6e7STejun Heo 472891ccc6e7STejun Heo err_free: 472991ccc6e7STejun Heo free_node_nr_active(nna_ar); 473091ccc6e7STejun Heo return -ENOMEM; 473191ccc6e7STejun Heo } 473291ccc6e7STejun Heo 4733e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4734e2dca7adSTejun Heo { 4735e2dca7adSTejun Heo struct workqueue_struct *wq = 4736e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4737e2dca7adSTejun Heo 473891ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 473991ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 474091ccc6e7STejun Heo 4741669de8bdSBart Van Assche wq_free_lockdep(wq); 4742ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4743e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4744e2dca7adSTejun Heo kfree(wq); 4745e2dca7adSTejun Heo } 4746e2dca7adSTejun Heo 474729c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 474829c91e99STejun Heo { 474929c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 475029c91e99STejun Heo 47517cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 475229c91e99STejun Heo free_workqueue_attrs(pool->attrs); 475329c91e99STejun Heo kfree(pool); 475429c91e99STejun Heo } 475529c91e99STejun Heo 475629c91e99STejun Heo /** 475729c91e99STejun Heo * put_unbound_pool - put a worker_pool 475829c91e99STejun Heo * @pool: worker_pool to put 475929c91e99STejun Heo * 476024acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4761c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4762c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4763c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4764a892caccSTejun Heo * 4765a892caccSTejun Heo * Should be called with wq_pool_mutex held. 476629c91e99STejun Heo */ 476729c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 476829c91e99STejun Heo { 476960f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 477029c91e99STejun Heo struct worker *worker; 47719680540cSYang Yingliang LIST_HEAD(cull_list); 4772e02b9312SValentin Schneider 4773a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4774a892caccSTejun Heo 4775a892caccSTejun Heo if (--pool->refcnt) 477629c91e99STejun Heo return; 477729c91e99STejun Heo 477829c91e99STejun Heo /* sanity checks */ 477961d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4780a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 478129c91e99STejun Heo return; 478229c91e99STejun Heo 478329c91e99STejun Heo /* release id and unhash */ 478429c91e99STejun Heo if (pool->id >= 0) 478529c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 478629c91e99STejun Heo hash_del(&pool->hash_node); 478729c91e99STejun Heo 4788c5aa87bbSTejun Heo /* 4789692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4790692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4791692b4825STejun Heo * manager and @pool gets freed with the flag set. 47929ab03be4SValentin Schneider * 47939ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 47949ab03be4SValentin Schneider * only get here with 47959ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 47969ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 47979ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 47989ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 47999ab03be4SValentin Schneider * drops pool->lock 4800c5aa87bbSTejun Heo */ 48019ab03be4SValentin Schneider while (true) { 48029ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 48039ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4804d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4805e02b9312SValentin Schneider 4806e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 48079ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 48089ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4809692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 48109ab03be4SValentin Schneider break; 48119ab03be4SValentin Schneider } 48129ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4813e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 48149ab03be4SValentin Schneider } 4815692b4825STejun Heo 48161037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4817e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 481829c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4819a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 482060f5a4bcSLai Jiangshan 4821e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4822e02b9312SValentin Schneider 4823e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 482460f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 48251258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 482660f5a4bcSLai Jiangshan 482760f5a4bcSLai Jiangshan if (pool->detach_completion) 482860f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 482960f5a4bcSLai Jiangshan 483029c91e99STejun Heo /* shut down the timers */ 483129c91e99STejun Heo del_timer_sync(&pool->idle_timer); 48323f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 483329c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 483429c91e99STejun Heo 483524acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 483625b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 483729c91e99STejun Heo } 483829c91e99STejun Heo 483929c91e99STejun Heo /** 484029c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 484129c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 484229c91e99STejun Heo * 484329c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 484429c91e99STejun Heo * reference count and return it. If there already is a matching 484529c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4846d185af30SYacine Belkadi * create a new one. 4847a892caccSTejun Heo * 4848a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4849d185af30SYacine Belkadi * 4850d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4851d185af30SYacine Belkadi * On failure, %NULL. 485229c91e99STejun Heo */ 485329c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 485429c91e99STejun Heo { 485584193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 485629c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 485729c91e99STejun Heo struct worker_pool *pool; 485884193c07STejun Heo int pod, node = NUMA_NO_NODE; 485929c91e99STejun Heo 4860a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 486129c91e99STejun Heo 486229c91e99STejun Heo /* do we already have a matching pool? */ 486329c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 486429c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 486529c91e99STejun Heo pool->refcnt++; 48663fb1823cSLai Jiangshan return pool; 486729c91e99STejun Heo } 486829c91e99STejun Heo } 486929c91e99STejun Heo 48709546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 487184193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 48729546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 487384193c07STejun Heo node = pt->pod_node[pod]; 4874e2273584SXunlei Pang break; 4875e2273584SXunlei Pang } 4876e2273584SXunlei Pang } 4877e2273584SXunlei Pang 487829c91e99STejun Heo /* nope, create a new one */ 487984193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 488029c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 488129c91e99STejun Heo goto fail; 488229c91e99STejun Heo 488384193c07STejun Heo pool->node = node; 48845de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 48855de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 48862865a8fbSShaohua Li 488729c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 488829c91e99STejun Heo goto fail; 488929c91e99STejun Heo 489029c91e99STejun Heo /* create and start the initial worker */ 48913347fa09STejun Heo if (wq_online && !create_worker(pool)) 489229c91e99STejun Heo goto fail; 489329c91e99STejun Heo 489429c91e99STejun Heo /* install */ 489529c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 48963fb1823cSLai Jiangshan 489729c91e99STejun Heo return pool; 489829c91e99STejun Heo fail: 489929c91e99STejun Heo if (pool) 490029c91e99STejun Heo put_unbound_pool(pool); 490129c91e99STejun Heo return NULL; 490229c91e99STejun Heo } 490329c91e99STejun Heo 49048864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 49058864b4e5STejun Heo { 49068864b4e5STejun Heo kmem_cache_free(pwq_cache, 49078864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 49088864b4e5STejun Heo } 49098864b4e5STejun Heo 49108864b4e5STejun Heo /* 4911967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4912967b494eSTejun Heo * refcnt and needs to be destroyed. 49138864b4e5STejun Heo */ 4914687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 49158864b4e5STejun Heo { 49168864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4917687a9aa5STejun Heo release_work); 49188864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 49198864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4920b42b0bddSYang Yingliang bool is_last = false; 49218864b4e5STejun Heo 4922b42b0bddSYang Yingliang /* 4923687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4924b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4925b42b0bddSYang Yingliang */ 4926b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 49273c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 49288864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4929bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 49304c065dbcSWaiman Long 49314c065dbcSWaiman Long /* 49324c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 49334c065dbcSWaiman Long */ 49344c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 49354c065dbcSWaiman Long unplug_oldest_pwq(wq); 49364c065dbcSWaiman Long 49373c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4938b42b0bddSYang Yingliang } 49398864b4e5STejun Heo 4940687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4941a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 49428864b4e5STejun Heo put_unbound_pool(pool); 4943a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4944687a9aa5STejun Heo } 4945a892caccSTejun Heo 49465797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 49475797b1c1STejun Heo struct wq_node_nr_active *nna = 49485797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 49495797b1c1STejun Heo 49505797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 49515797b1c1STejun Heo list_del_init(&pwq->pending_node); 49525797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 49535797b1c1STejun Heo } 49545797b1c1STejun Heo 495525b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 49568864b4e5STejun Heo 49578864b4e5STejun Heo /* 49588864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4959e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 49608864b4e5STejun Heo */ 4961669de8bdSBart Van Assche if (is_last) { 4962669de8bdSBart Van Assche wq_unregister_lockdep(wq); 496325b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 49646029a918STejun Heo } 4965669de8bdSBart Van Assche } 49668864b4e5STejun Heo 496767dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4968f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4969f147f29eSTejun Heo struct worker_pool *pool) 4970d2c1d404STejun Heo { 4971e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK); 4972d2c1d404STejun Heo 4973e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4974e50aba9aSTejun Heo 4975d2c1d404STejun Heo pwq->pool = pool; 4976d2c1d404STejun Heo pwq->wq = wq; 4977d2c1d404STejun Heo pwq->flush_color = -1; 49788864b4e5STejun Heo pwq->refcnt = 1; 4979f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 49805797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 49811befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4982d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4983687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4984f147f29eSTejun Heo } 4985d2c1d404STejun Heo 4986f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 49871befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4988f147f29eSTejun Heo { 4989f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4990f147f29eSTejun Heo 4991f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 499275ccf595STejun Heo 49931befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 49941befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 49951befcf30STejun Heo return; 49961befcf30STejun Heo 499729b1cb41SLai Jiangshan /* set the matching work_color */ 499875ccf595STejun Heo pwq->work_color = wq->work_color; 4999983ca25eSTejun Heo 5000983ca25eSTejun Heo /* link in @pwq */ 500126fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 5002df2d5ae4STejun Heo } 50036029a918STejun Heo 5004f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 5005f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 5006f147f29eSTejun Heo const struct workqueue_attrs *attrs) 5007f147f29eSTejun Heo { 5008f147f29eSTejun Heo struct worker_pool *pool; 5009f147f29eSTejun Heo struct pool_workqueue *pwq; 5010f147f29eSTejun Heo 5011f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 5012f147f29eSTejun Heo 5013f147f29eSTejun Heo pool = get_unbound_pool(attrs); 5014f147f29eSTejun Heo if (!pool) 5015f147f29eSTejun Heo return NULL; 5016f147f29eSTejun Heo 5017e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 5018f147f29eSTejun Heo if (!pwq) { 5019f147f29eSTejun Heo put_unbound_pool(pool); 5020f147f29eSTejun Heo return NULL; 5021f147f29eSTejun Heo } 5022f147f29eSTejun Heo 5023f147f29eSTejun Heo init_pwq(pwq, wq, pool); 5024f147f29eSTejun Heo return pwq; 5025d2c1d404STejun Heo } 5026d2c1d404STejun Heo 50274c16bd32STejun Heo /** 5028fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 5029042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 503084193c07STejun Heo * @cpu: the target CPU 50314c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 50324c16bd32STejun Heo * 5033fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 5034fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 50359546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 50364c16bd32STejun Heo * 5037fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 5038fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 5039fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 50404c16bd32STejun Heo * 5041fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 50424c16bd32STejun Heo */ 50439546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 50449546b29eSTejun Heo int cpu_going_down) 50454c16bd32STejun Heo { 504684193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 504784193c07STejun Heo int pod = pt->cpu_pod[cpu]; 50484c16bd32STejun Heo 5049fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 50509546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 50519546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 50524c16bd32STejun Heo if (cpu_going_down >= 0) 50539546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 50544c16bd32STejun Heo 50559546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 50569546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 505784193c07STejun Heo return; 505884193c07STejun Heo } 50594c16bd32STejun Heo 5060fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 50619546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 50621ad0f0a7SMichael Bringmann 50639546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 50641ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 50651ad0f0a7SMichael Bringmann "possible intersect\n"); 50664c16bd32STejun Heo } 50674c16bd32STejun Heo 50689f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 5069636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 5070636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 50711befcf30STejun Heo { 50729f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 50731befcf30STejun Heo struct pool_workqueue *old_pwq; 50741befcf30STejun Heo 50755b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 50761befcf30STejun Heo lockdep_assert_held(&wq->mutex); 50771befcf30STejun Heo 50781befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 50791befcf30STejun Heo link_pwq(pwq); 50801befcf30STejun Heo 50819f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 50829f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 50831befcf30STejun Heo return old_pwq; 50841befcf30STejun Heo } 50851befcf30STejun Heo 50862d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 50872d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 50882d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 50892d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 5090042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 50912d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 50922d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 50932d5f0764SLai Jiangshan }; 50942d5f0764SLai Jiangshan 50952d5f0764SLai Jiangshan /* free the resources after success or abort */ 50962d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 50972d5f0764SLai Jiangshan { 50982d5f0764SLai Jiangshan if (ctx) { 5099636b927eSTejun Heo int cpu; 51002d5f0764SLai Jiangshan 5101636b927eSTejun Heo for_each_possible_cpu(cpu) 5102636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 51032d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 51042d5f0764SLai Jiangshan 51052d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 51062d5f0764SLai Jiangshan 51072d5f0764SLai Jiangshan kfree(ctx); 51082d5f0764SLai Jiangshan } 51092d5f0764SLai Jiangshan } 51102d5f0764SLai Jiangshan 51112d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 51122d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 51132d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 511499c621efSLai Jiangshan const struct workqueue_attrs *attrs, 511599c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 51162d5f0764SLai Jiangshan { 51172d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 51189546b29eSTejun Heo struct workqueue_attrs *new_attrs; 5119636b927eSTejun Heo int cpu; 51202d5f0764SLai Jiangshan 51212d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 51222d5f0764SLai Jiangshan 512384193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 512484193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 512584193c07STejun Heo return ERR_PTR(-EINVAL); 512684193c07STejun Heo 5127636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 51282d5f0764SLai Jiangshan 5129be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 51309546b29eSTejun Heo if (!ctx || !new_attrs) 51312d5f0764SLai Jiangshan goto out_free; 51322d5f0764SLai Jiangshan 5133042f7df1SLai Jiangshan /* 51342d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 51352d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 51362d5f0764SLai Jiangshan * it even if we don't use it immediately. 51372d5f0764SLai Jiangshan */ 51380f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 51390f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 51409546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 51412d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 51422d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 51432d5f0764SLai Jiangshan goto out_free; 51442d5f0764SLai Jiangshan 5145636b927eSTejun Heo for_each_possible_cpu(cpu) { 5146af73f5c9STejun Heo if (new_attrs->ordered) { 51472d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5148636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5149636b927eSTejun Heo } else { 51509546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 51519546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5152636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5153636b927eSTejun Heo goto out_free; 51542d5f0764SLai Jiangshan } 51552d5f0764SLai Jiangshan } 51562d5f0764SLai Jiangshan 5157042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5158042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5159042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 51609546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 51612d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5162042f7df1SLai Jiangshan 51634c065dbcSWaiman Long /* 51644c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 51654c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 51664c065dbcSWaiman Long * of newly queued work items until execution of older work items in 51674c065dbcSWaiman Long * the old pwq's have completed. 51684c065dbcSWaiman Long */ 51694c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 51704c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 51714c065dbcSWaiman Long 51722d5f0764SLai Jiangshan ctx->wq = wq; 51732d5f0764SLai Jiangshan return ctx; 51742d5f0764SLai Jiangshan 51752d5f0764SLai Jiangshan out_free: 51762d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 51772d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 517884193c07STejun Heo return ERR_PTR(-ENOMEM); 51792d5f0764SLai Jiangshan } 51802d5f0764SLai Jiangshan 51812d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 51822d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 51832d5f0764SLai Jiangshan { 5184636b927eSTejun Heo int cpu; 51852d5f0764SLai Jiangshan 51862d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 51872d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 51882d5f0764SLai Jiangshan 51892d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 51902d5f0764SLai Jiangshan 51919f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5192636b927eSTejun Heo for_each_possible_cpu(cpu) 5193636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5194636b927eSTejun Heo ctx->pwq_tbl[cpu]); 51959f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 51962d5f0764SLai Jiangshan 51975797b1c1STejun Heo /* update node_nr_active->max */ 51985797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 51995797b1c1STejun Heo 5200d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5201d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5202d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5203d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5204d64f2fa0SJuri Lelli 52052d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 52062d5f0764SLai Jiangshan } 52072d5f0764SLai Jiangshan 5208a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5209a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5210a0111cf6SLai Jiangshan { 5211a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5212a0111cf6SLai Jiangshan 5213a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5214a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5215a0111cf6SLai Jiangshan return -EINVAL; 5216a0111cf6SLai Jiangshan 521799c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 521884193c07STejun Heo if (IS_ERR(ctx)) 521984193c07STejun Heo return PTR_ERR(ctx); 5220a0111cf6SLai Jiangshan 5221a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5222a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5223a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5224a0111cf6SLai Jiangshan 52256201171eSwanghaibin return 0; 5226a0111cf6SLai Jiangshan } 5227a0111cf6SLai Jiangshan 52289e8cd2f5STejun Heo /** 52299e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 52309e8cd2f5STejun Heo * @wq: the target workqueue 52319e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 52329e8cd2f5STejun Heo * 5233fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5234fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5235fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5236fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5237fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 52389e8cd2f5STejun Heo * 5239d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5240d185af30SYacine Belkadi * 5241ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5242509b3204SDaniel Jordan * 5243d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 52449e8cd2f5STejun Heo */ 5245513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 52469e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 52479e8cd2f5STejun Heo { 5248a0111cf6SLai Jiangshan int ret; 52499e8cd2f5STejun Heo 5250509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5251509b3204SDaniel Jordan 5252509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5253a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5254509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 52552d5f0764SLai Jiangshan 52562d5f0764SLai Jiangshan return ret; 52579e8cd2f5STejun Heo } 52589e8cd2f5STejun Heo 52594c16bd32STejun Heo /** 5260fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 52614c16bd32STejun Heo * @wq: the target workqueue 52624cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 52634cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 52644c16bd32STejun Heo * @online: whether @cpu is coming up or going down 52654c16bd32STejun Heo * 52664c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5267fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 52684c16bd32STejun Heo * @wq accordingly. 52694c16bd32STejun Heo * 52704c16bd32STejun Heo * 5271fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5272fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5273fef59c9cSTejun Heo * 5274fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5275fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5276fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5277fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5278fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5279fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 52804c16bd32STejun Heo */ 5281fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 52824cbfd3deSTejun Heo int hotplug_cpu, bool online) 52834c16bd32STejun Heo { 52844cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 52854c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 52864c16bd32STejun Heo struct workqueue_attrs *target_attrs; 52874c16bd32STejun Heo 52884c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 52894c16bd32STejun Heo 529084193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 52914c16bd32STejun Heo return; 52924c16bd32STejun Heo 52934c16bd32STejun Heo /* 52944c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 52954c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 52964c16bd32STejun Heo * CPU hotplug exclusion. 52974c16bd32STejun Heo */ 5298fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 52994c16bd32STejun Heo 53004c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 53010f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 53024c16bd32STejun Heo 5303636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 53049546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 53059f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5306f7142ed4SLai Jiangshan return; 53074c16bd32STejun Heo 53084c16bd32STejun Heo /* create a new pwq */ 53094c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 53104c16bd32STejun Heo if (!pwq) { 5311fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 53124c16bd32STejun Heo wq->name); 531377f300b1SDaeseok Youn goto use_dfl_pwq; 53144c16bd32STejun Heo } 53154c16bd32STejun Heo 5316f7142ed4SLai Jiangshan /* Install the new pwq. */ 53174c16bd32STejun Heo mutex_lock(&wq->mutex); 5318636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 53194c16bd32STejun Heo goto out_unlock; 53204c16bd32STejun Heo 53214c16bd32STejun Heo use_dfl_pwq: 5322f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 53239f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 53249f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 53259f66cff2STejun Heo get_pwq(pwq); 53269f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 53279f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 53284c16bd32STejun Heo out_unlock: 53294c16bd32STejun Heo mutex_unlock(&wq->mutex); 53304c16bd32STejun Heo put_pwq_unlocked(old_pwq); 53314c16bd32STejun Heo } 53324c16bd32STejun Heo 533330cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 53341da177e4SLinus Torvalds { 533549e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 53368a2b7538STejun Heo int cpu, ret; 5337e1d8aa9fSFrederic Weisbecker 5338687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5339ee1ceef7STejun Heo if (!wq->cpu_pwq) 5340687a9aa5STejun Heo goto enomem; 534130cdf249STejun Heo 5342636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 534330cdf249STejun Heo for_each_possible_cpu(cpu) { 53444cb1ef64STejun Heo struct pool_workqueue **pwq_p; 53454cb1ef64STejun Heo struct worker_pool __percpu *pools; 53464cb1ef64STejun Heo struct worker_pool *pool; 53474cb1ef64STejun Heo 53484cb1ef64STejun Heo if (wq->flags & WQ_BH) 53494cb1ef64STejun Heo pools = bh_worker_pools; 53504cb1ef64STejun Heo else 53514cb1ef64STejun Heo pools = cpu_worker_pools; 53524cb1ef64STejun Heo 53534cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 53544cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 535530cdf249STejun Heo 5356687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5357687a9aa5STejun Heo pool->node); 5358687a9aa5STejun Heo if (!*pwq_p) 5359687a9aa5STejun Heo goto enomem; 5360687a9aa5STejun Heo 5361687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5362f147f29eSTejun Heo 5363f147f29eSTejun Heo mutex_lock(&wq->mutex); 5364687a9aa5STejun Heo link_pwq(*pwq_p); 5365f147f29eSTejun Heo mutex_unlock(&wq->mutex); 536630cdf249STejun Heo } 536730cdf249STejun Heo return 0; 5368509b3204SDaniel Jordan } 5369509b3204SDaniel Jordan 5370ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5371509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 53729f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 53739f66cff2STejun Heo 53748a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 53758a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 53769f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 53779f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 53789f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 53798a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 53809e8cd2f5STejun Heo } else { 5381509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 53829e8cd2f5STejun Heo } 5383ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5384509b3204SDaniel Jordan 538564344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 538664344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 538764344553SZqiang */ 538864344553SZqiang if (ret) 538964344553SZqiang kthread_flush_worker(pwq_release_worker); 539064344553SZqiang 5391509b3204SDaniel Jordan return ret; 5392687a9aa5STejun Heo 5393687a9aa5STejun Heo enomem: 5394687a9aa5STejun Heo if (wq->cpu_pwq) { 53957b42f401SZqiang for_each_possible_cpu(cpu) { 53967b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 53977b42f401SZqiang 53987b42f401SZqiang if (pwq) 53997b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 54007b42f401SZqiang } 5401687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5402687a9aa5STejun Heo wq->cpu_pwq = NULL; 5403687a9aa5STejun Heo } 5404687a9aa5STejun Heo return -ENOMEM; 54050f900049STejun Heo } 54060f900049STejun Heo 5407f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5408f3421797STejun Heo const char *name) 5409b71ab8c2STejun Heo { 5410636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5411044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5412636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5413b71ab8c2STejun Heo 5414636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5415b71ab8c2STejun Heo } 5416b71ab8c2STejun Heo 5417983c7515STejun Heo /* 5418983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5419983c7515STejun Heo * to guarantee forward progress. 5420983c7515STejun Heo */ 5421983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5422983c7515STejun Heo { 5423983c7515STejun Heo struct worker *rescuer; 5424b92b36eaSDan Carpenter int ret; 5425983c7515STejun Heo 5426983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5427983c7515STejun Heo return 0; 5428983c7515STejun Heo 5429983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 54304c0736a7SPetr Mladek if (!rescuer) { 54314c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 54324c0736a7SPetr Mladek wq->name); 5433983c7515STejun Heo return -ENOMEM; 54344c0736a7SPetr Mladek } 5435983c7515STejun Heo 5436983c7515STejun Heo rescuer->rescue_wq = wq; 5437b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5438f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5439b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 54404c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 54414c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5442983c7515STejun Heo kfree(rescuer); 5443b92b36eaSDan Carpenter return ret; 5444983c7515STejun Heo } 5445983c7515STejun Heo 5446983c7515STejun Heo wq->rescuer = rescuer; 544785f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 544849584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 544985f0ab43SJuri Lelli else 5450983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5451983c7515STejun Heo wake_up_process(rescuer->task); 5452983c7515STejun Heo 5453983c7515STejun Heo return 0; 5454983c7515STejun Heo } 5455983c7515STejun Heo 5456a045a272STejun Heo /** 5457a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5458a045a272STejun Heo * @wq: target workqueue 5459a045a272STejun Heo * 5460a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5461a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5462a045a272STejun Heo * @wq->max_active to zero. 5463a045a272STejun Heo */ 5464a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5465a045a272STejun Heo { 5466c5404d4eSTejun Heo bool activated; 54675797b1c1STejun Heo int new_max, new_min; 5468a045a272STejun Heo 5469a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5470a045a272STejun Heo 5471a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 54725797b1c1STejun Heo new_max = 0; 54735797b1c1STejun Heo new_min = 0; 54745797b1c1STejun Heo } else { 54755797b1c1STejun Heo new_max = wq->saved_max_active; 54765797b1c1STejun Heo new_min = wq->saved_min_active; 5477a045a272STejun Heo } 5478a045a272STejun Heo 54795797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5480a045a272STejun Heo return; 5481a045a272STejun Heo 5482a045a272STejun Heo /* 54835797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5484a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5485a045a272STejun Heo * because new work items are always queued behind existing inactive 5486a045a272STejun Heo * work items if there are any. 5487a045a272STejun Heo */ 54885797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 54895797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 54905797b1c1STejun Heo 54915797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 54925797b1c1STejun Heo wq_update_node_max_active(wq, -1); 54935797b1c1STejun Heo 54945797b1c1STejun Heo if (new_max == 0) 54955797b1c1STejun Heo return; 5496a045a272STejun Heo 5497c5404d4eSTejun Heo /* 5498c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5499c5404d4eSTejun Heo * until max_active is filled. 5500c5404d4eSTejun Heo */ 5501c5404d4eSTejun Heo do { 5502c5404d4eSTejun Heo struct pool_workqueue *pwq; 5503c5404d4eSTejun Heo 5504c5404d4eSTejun Heo activated = false; 5505a045a272STejun Heo for_each_pwq(pwq, wq) { 5506c26e2f2eSTejun Heo unsigned long irq_flags; 5507a045a272STejun Heo 5508c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5509c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 55105797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5511c5404d4eSTejun Heo activated = true; 5512a045a272STejun Heo kick_pool(pwq->pool); 5513c5404d4eSTejun Heo } 5514c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 5515a045a272STejun Heo } 5516c5404d4eSTejun Heo } while (activated); 5517a045a272STejun Heo } 5518a045a272STejun Heo 5519a2775bbcSMathieu Malaterre __printf(1, 4) 5520669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 552197e37d7bSTejun Heo unsigned int flags, 5522669de8bdSBart Van Assche int max_active, ...) 55233af24433SOleg Nesterov { 5524ecf6881fSTejun Heo va_list args; 55253af24433SOleg Nesterov struct workqueue_struct *wq; 552691ccc6e7STejun Heo size_t wq_size; 552791ccc6e7STejun Heo int name_len; 5528b196be89STejun Heo 55294cb1ef64STejun Heo if (flags & WQ_BH) { 55304cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 55314cb1ef64STejun Heo return NULL; 55324cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 55334cb1ef64STejun Heo return NULL; 55344cb1ef64STejun Heo } 55354cb1ef64STejun Heo 5536cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5537cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5538cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5539cee22a15SViresh Kumar 5540ecf6881fSTejun Heo /* allocate wq and format name */ 554191ccc6e7STejun Heo if (flags & WQ_UNBOUND) 554291ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 554391ccc6e7STejun Heo else 554491ccc6e7STejun Heo wq_size = sizeof(*wq); 554591ccc6e7STejun Heo 554691ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5547b196be89STejun Heo if (!wq) 5548d2c1d404STejun Heo return NULL; 5549b196be89STejun Heo 55506029a918STejun Heo if (flags & WQ_UNBOUND) { 5551be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 55526029a918STejun Heo if (!wq->unbound_attrs) 55536029a918STejun Heo goto err_free_wq; 55546029a918STejun Heo } 55556029a918STejun Heo 5556669de8bdSBart Van Assche va_start(args, max_active); 555791ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5558b196be89STejun Heo va_end(args); 55593af24433SOleg Nesterov 556091ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 556191ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 556291ccc6e7STejun Heo wq->name); 556331c89007SAudra Mitchell 55644cb1ef64STejun Heo if (flags & WQ_BH) { 55654cb1ef64STejun Heo /* 55664cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 55674cb1ef64STejun Heo * and don't impose any max_active limit. 55684cb1ef64STejun Heo */ 55694cb1ef64STejun Heo max_active = INT_MAX; 55704cb1ef64STejun Heo } else { 5571d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5572b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 55734cb1ef64STejun Heo } 55743af24433SOleg Nesterov 5575b196be89STejun Heo /* init wq */ 557697e37d7bSTejun Heo wq->flags = flags; 5577a045a272STejun Heo wq->max_active = max_active; 55785797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 55795797b1c1STejun Heo wq->saved_max_active = wq->max_active; 55805797b1c1STejun Heo wq->saved_min_active = wq->min_active; 55813c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5582112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 558330cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 558473f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 558573f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5586493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 55873af24433SOleg Nesterov 5588669de8bdSBart Van Assche wq_init_lockdep(wq); 5589cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 55903af24433SOleg Nesterov 559191ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 559291ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 559382efcab3SBart Van Assche goto err_unreg_lockdep; 559491ccc6e7STejun Heo } 559591ccc6e7STejun Heo 559691ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 559791ccc6e7STejun Heo goto err_free_node_nr_active; 55981537663fSTejun Heo 559940c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5600d2c1d404STejun Heo goto err_destroy; 5601e22bee78STejun Heo 5602226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5603226223abSTejun Heo goto err_destroy; 5604226223abSTejun Heo 56056af8bf3dSOleg Nesterov /* 560668e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 560768e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 560868e13a67SLai Jiangshan * list. 56096af8bf3dSOleg Nesterov */ 561068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5611a0a1a5fdSTejun Heo 5612a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5613a045a272STejun Heo wq_adjust_max_active(wq); 5614a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5615a0a1a5fdSTejun Heo 5616e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5617a0a1a5fdSTejun Heo 561868e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 56193af24433SOleg Nesterov 56203af24433SOleg Nesterov return wq; 5621d2c1d404STejun Heo 562291ccc6e7STejun Heo err_free_node_nr_active: 562391ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 562491ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 562582efcab3SBart Van Assche err_unreg_lockdep: 5626009bb421SBart Van Assche wq_unregister_lockdep(wq); 5627009bb421SBart Van Assche wq_free_lockdep(wq); 562882efcab3SBart Van Assche err_free_wq: 56296029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 56304690c4abSTejun Heo kfree(wq); 5631d2c1d404STejun Heo return NULL; 5632d2c1d404STejun Heo err_destroy: 5633d2c1d404STejun Heo destroy_workqueue(wq); 56344690c4abSTejun Heo return NULL; 56351da177e4SLinus Torvalds } 5636669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 56371da177e4SLinus Torvalds 5638c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5639c29eb853STejun Heo { 5640c29eb853STejun Heo int i; 5641c29eb853STejun Heo 5642c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5643c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5644c29eb853STejun Heo return true; 5645c29eb853STejun Heo 56469f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5647c29eb853STejun Heo return true; 5648afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5649c29eb853STejun Heo return true; 5650c29eb853STejun Heo 5651c29eb853STejun Heo return false; 5652c29eb853STejun Heo } 5653c29eb853STejun Heo 56543af24433SOleg Nesterov /** 56553af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 56563af24433SOleg Nesterov * @wq: target workqueue 56573af24433SOleg Nesterov * 56583af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 56593af24433SOleg Nesterov */ 56603af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 56613af24433SOleg Nesterov { 566249e3cf44STejun Heo struct pool_workqueue *pwq; 5663636b927eSTejun Heo int cpu; 56643af24433SOleg Nesterov 5665def98c84STejun Heo /* 5666def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5667def98c84STejun Heo * lead to sysfs name conflicts. 5668def98c84STejun Heo */ 5669def98c84STejun Heo workqueue_sysfs_unregister(wq); 5670def98c84STejun Heo 567133e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 567233e3f0a3SRichard Clark mutex_lock(&wq->mutex); 567333e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 567433e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 567533e3f0a3SRichard Clark 56769c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 56779c5a2ba7STejun Heo drain_workqueue(wq); 5678c8efcc25STejun Heo 5679def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5680def98c84STejun Heo if (wq->rescuer) { 5681def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5682def98c84STejun Heo 5683def98c84STejun Heo /* this prevents new queueing */ 5684a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5685def98c84STejun Heo wq->rescuer = NULL; 5686a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5687def98c84STejun Heo 5688def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5689def98c84STejun Heo kthread_stop(rescuer->task); 56908efe1223STejun Heo kfree(rescuer); 5691def98c84STejun Heo } 5692def98c84STejun Heo 5693c29eb853STejun Heo /* 5694c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5695c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5696c29eb853STejun Heo */ 5697c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5698b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 569949e3cf44STejun Heo for_each_pwq(pwq, wq) { 5700a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5701c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 57021d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5703e66b39afSTejun Heo __func__, wq->name); 5704c29eb853STejun Heo show_pwq(pwq); 5705a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5706b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5707c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 570855df0933SImran Khan show_one_workqueue(wq); 57096183c009STejun Heo return; 571076af4d93STejun Heo } 5711a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 571276af4d93STejun Heo } 5713b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 57146183c009STejun Heo 5715a0a1a5fdSTejun Heo /* 5716a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5717a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5718a0a1a5fdSTejun Heo */ 5719e2dca7adSTejun Heo list_del_rcu(&wq->list); 572068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 57213af24433SOleg Nesterov 57228864b4e5STejun Heo /* 5723636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5724636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5725636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 57268864b4e5STejun Heo */ 5727636b927eSTejun Heo rcu_read_lock(); 5728636b927eSTejun Heo 5729636b927eSTejun Heo for_each_possible_cpu(cpu) { 57309f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 57319f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 57324c16bd32STejun Heo } 57334c16bd32STejun Heo 57349f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 57359f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5736636b927eSTejun Heo 5737636b927eSTejun Heo rcu_read_unlock(); 57383af24433SOleg Nesterov } 57393af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 57403af24433SOleg Nesterov 5741dcd989cbSTejun Heo /** 5742dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5743dcd989cbSTejun Heo * @wq: target workqueue 5744dcd989cbSTejun Heo * @max_active: new max_active value. 5745dcd989cbSTejun Heo * 57465797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 57475797b1c1STejun Heo * comment. 5748dcd989cbSTejun Heo * 5749dcd989cbSTejun Heo * CONTEXT: 5750dcd989cbSTejun Heo * Don't call from IRQ context. 5751dcd989cbSTejun Heo */ 5752dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5753dcd989cbSTejun Heo { 57544cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 57554cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 57564cb1ef64STejun Heo return; 57578719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 57583bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 57598719dceaSTejun Heo return; 57608719dceaSTejun Heo 5761f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5762dcd989cbSTejun Heo 5763a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5764dcd989cbSTejun Heo 5765dcd989cbSTejun Heo wq->saved_max_active = max_active; 57665797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 57675797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 57685797b1c1STejun Heo 5769a045a272STejun Heo wq_adjust_max_active(wq); 5770dcd989cbSTejun Heo 5771a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5772dcd989cbSTejun Heo } 5773dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5774dcd989cbSTejun Heo 5775dcd989cbSTejun Heo /** 57768f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue 57778f172181STejun Heo * @wq: target unbound workqueue 57788f172181STejun Heo * @min_active: new min_active value 57798f172181STejun Heo * 57808f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an 57818f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active 57828f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be 57838f172181STejun Heo * able to process min_active number of interdependent work items which is 57848f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default. 57858f172181STejun Heo * 57868f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current 57878f172181STejun Heo * max_active. 57888f172181STejun Heo */ 57898f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) 57908f172181STejun Heo { 57918f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */ 57928f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != 57938f172181STejun Heo WQ_UNBOUND)) 57948f172181STejun Heo return; 57958f172181STejun Heo 57968f172181STejun Heo mutex_lock(&wq->mutex); 57978f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); 57988f172181STejun Heo wq_adjust_max_active(wq); 57998f172181STejun Heo mutex_unlock(&wq->mutex); 58008f172181STejun Heo } 58018f172181STejun Heo 58028f172181STejun Heo /** 580327d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 580427d4ee03SLukas Wunner * 580527d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 580627d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 580727d4ee03SLukas Wunner * 580827d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 580927d4ee03SLukas Wunner */ 581027d4ee03SLukas Wunner struct work_struct *current_work(void) 581127d4ee03SLukas Wunner { 581227d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 581327d4ee03SLukas Wunner 581427d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 581527d4ee03SLukas Wunner } 581627d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 581727d4ee03SLukas Wunner 581827d4ee03SLukas Wunner /** 5819e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5820e6267616STejun Heo * 5821e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5822e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5823d185af30SYacine Belkadi * 5824d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5825e6267616STejun Heo */ 5826e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5827e6267616STejun Heo { 5828e6267616STejun Heo struct worker *worker = current_wq_worker(); 5829e6267616STejun Heo 58306a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5831e6267616STejun Heo } 5832e6267616STejun Heo 5833e6267616STejun Heo /** 5834dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5835dcd989cbSTejun Heo * @cpu: CPU in question 5836dcd989cbSTejun Heo * @wq: target workqueue 5837dcd989cbSTejun Heo * 5838dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5839dcd989cbSTejun Heo * no synchronization around this function and the test result is 5840dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5841dcd989cbSTejun Heo * 5842d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5843636b927eSTejun Heo * 5844636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5845636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5846636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5847636b927eSTejun Heo * other CPUs. 5848d3251859STejun Heo * 5849d185af30SYacine Belkadi * Return: 5850dcd989cbSTejun Heo * %true if congested, %false otherwise. 5851dcd989cbSTejun Heo */ 5852d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5853dcd989cbSTejun Heo { 58547fb98ea7STejun Heo struct pool_workqueue *pwq; 585576af4d93STejun Heo bool ret; 585676af4d93STejun Heo 585724acfb71SThomas Gleixner rcu_read_lock(); 585824acfb71SThomas Gleixner preempt_disable(); 58597fb98ea7STejun Heo 5860d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5861d3251859STejun Heo cpu = smp_processor_id(); 5862d3251859STejun Heo 5863687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5864f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5865636b927eSTejun Heo 586624acfb71SThomas Gleixner preempt_enable(); 586724acfb71SThomas Gleixner rcu_read_unlock(); 586876af4d93STejun Heo 586976af4d93STejun Heo return ret; 5870dcd989cbSTejun Heo } 5871dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5872dcd989cbSTejun Heo 5873dcd989cbSTejun Heo /** 5874dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5875dcd989cbSTejun Heo * @work: the work to be tested 5876dcd989cbSTejun Heo * 5877dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5878dcd989cbSTejun Heo * synchronization around this function and the test result is 5879dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5880dcd989cbSTejun Heo * 5881d185af30SYacine Belkadi * Return: 5882dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5883dcd989cbSTejun Heo */ 5884dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5885dcd989cbSTejun Heo { 5886fa1b54e6STejun Heo struct worker_pool *pool; 5887c26e2f2eSTejun Heo unsigned long irq_flags; 5888dcd989cbSTejun Heo unsigned int ret = 0; 5889dcd989cbSTejun Heo 5890dcd989cbSTejun Heo if (work_pending(work)) 5891dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5892038366c5SLai Jiangshan 589324acfb71SThomas Gleixner rcu_read_lock(); 5894fa1b54e6STejun Heo pool = get_work_pool(work); 5895038366c5SLai Jiangshan if (pool) { 5896c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 5897c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5898dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5899c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 5900038366c5SLai Jiangshan } 590124acfb71SThomas Gleixner rcu_read_unlock(); 5902dcd989cbSTejun Heo 5903dcd989cbSTejun Heo return ret; 5904dcd989cbSTejun Heo } 5905dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5906dcd989cbSTejun Heo 59073d1cb205STejun Heo /** 59083d1cb205STejun Heo * set_worker_desc - set description for the current work item 59093d1cb205STejun Heo * @fmt: printf-style format string 59103d1cb205STejun Heo * @...: arguments for the format string 59113d1cb205STejun Heo * 59123d1cb205STejun Heo * This function can be called by a running work function to describe what 59133d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 59143d1cb205STejun Heo * information will be printed out together to help debugging. The 59153d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 59163d1cb205STejun Heo */ 59173d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 59183d1cb205STejun Heo { 59193d1cb205STejun Heo struct worker *worker = current_wq_worker(); 59203d1cb205STejun Heo va_list args; 59213d1cb205STejun Heo 59223d1cb205STejun Heo if (worker) { 59233d1cb205STejun Heo va_start(args, fmt); 59243d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 59253d1cb205STejun Heo va_end(args); 59263d1cb205STejun Heo } 59273d1cb205STejun Heo } 59285c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 59293d1cb205STejun Heo 59303d1cb205STejun Heo /** 59313d1cb205STejun Heo * print_worker_info - print out worker information and description 59323d1cb205STejun Heo * @log_lvl: the log level to use when printing 59333d1cb205STejun Heo * @task: target task 59343d1cb205STejun Heo * 59353d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 59363d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 59373d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 59383d1cb205STejun Heo * 59393d1cb205STejun Heo * This function can be safely called on any task as long as the 59403d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 59413d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 59423d1cb205STejun Heo */ 59433d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 59443d1cb205STejun Heo { 59453d1cb205STejun Heo work_func_t *fn = NULL; 59463d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 59473d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 59483d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 59493d1cb205STejun Heo struct workqueue_struct *wq = NULL; 59503d1cb205STejun Heo struct worker *worker; 59513d1cb205STejun Heo 59523d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 59533d1cb205STejun Heo return; 59543d1cb205STejun Heo 59553d1cb205STejun Heo /* 59563d1cb205STejun Heo * This function is called without any synchronization and @task 59573d1cb205STejun Heo * could be in any state. Be careful with dereferences. 59583d1cb205STejun Heo */ 5959e700591aSPetr Mladek worker = kthread_probe_data(task); 59603d1cb205STejun Heo 59613d1cb205STejun Heo /* 59628bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 59638bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 59643d1cb205STejun Heo */ 5965fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5966fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5967fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5968fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5969fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 59703d1cb205STejun Heo 59713d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5972d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 59738bf89593STejun Heo if (strcmp(name, desc)) 59743d1cb205STejun Heo pr_cont(" (%s)", desc); 59753d1cb205STejun Heo pr_cont("\n"); 59763d1cb205STejun Heo } 59773d1cb205STejun Heo } 59783d1cb205STejun Heo 59793494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 59803494fc30STejun Heo { 59813494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 59823494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 59833494fc30STejun Heo pr_cont(" node=%d", pool->node); 59844cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 59854cb1ef64STejun Heo if (pool->flags & POOL_BH) 59864cb1ef64STejun Heo pr_cont(" bh%s", 59874cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 59884cb1ef64STejun Heo else 59894cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 59904cb1ef64STejun Heo } 59914cb1ef64STejun Heo 59924cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 59934cb1ef64STejun Heo { 59944cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 59954cb1ef64STejun Heo 59964cb1ef64STejun Heo if (pool->flags & WQ_BH) 59974cb1ef64STejun Heo pr_cont("bh%s", 59984cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 59994cb1ef64STejun Heo else 60004cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 60014cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 60023494fc30STejun Heo } 60033494fc30STejun Heo 6004c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 6005c76feb0dSPaul E. McKenney bool comma; 6006c76feb0dSPaul E. McKenney work_func_t func; 6007c76feb0dSPaul E. McKenney long ctr; 6008c76feb0dSPaul E. McKenney }; 6009c76feb0dSPaul E. McKenney 6010c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 6011c76feb0dSPaul E. McKenney { 6012c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 6013c76feb0dSPaul E. McKenney goto out_record; 6014c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 6015c76feb0dSPaul E. McKenney pcwsp->ctr++; 6016c76feb0dSPaul E. McKenney return; 6017c76feb0dSPaul E. McKenney } 6018c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 6019c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 6020c76feb0dSPaul E. McKenney else 6021c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 6022c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 6023c76feb0dSPaul E. McKenney out_record: 6024c76feb0dSPaul E. McKenney if ((long)func == -1L) 6025c76feb0dSPaul E. McKenney return; 6026c76feb0dSPaul E. McKenney pcwsp->comma = comma; 6027c76feb0dSPaul E. McKenney pcwsp->func = func; 6028c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 6029c76feb0dSPaul E. McKenney } 6030c76feb0dSPaul E. McKenney 6031c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 60323494fc30STejun Heo { 60333494fc30STejun Heo if (work->func == wq_barrier_func) { 60343494fc30STejun Heo struct wq_barrier *barr; 60353494fc30STejun Heo 60363494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 60373494fc30STejun Heo 6038c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 60393494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 60403494fc30STejun Heo task_pid_nr(barr->task)); 60413494fc30STejun Heo } else { 6042c76feb0dSPaul E. McKenney if (!comma) 6043c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 6044c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 60453494fc30STejun Heo } 60463494fc30STejun Heo } 60473494fc30STejun Heo 60483494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 60493494fc30STejun Heo { 6050c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 60513494fc30STejun Heo struct worker_pool *pool = pwq->pool; 60523494fc30STejun Heo struct work_struct *work; 60533494fc30STejun Heo struct worker *worker; 60543494fc30STejun Heo bool has_in_flight = false, has_pending = false; 60553494fc30STejun Heo int bkt; 60563494fc30STejun Heo 60573494fc30STejun Heo pr_info(" pwq %d:", pool->id); 60583494fc30STejun Heo pr_cont_pool_info(pool); 60593494fc30STejun Heo 6060a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 6061a045a272STejun Heo pwq->nr_active, pwq->refcnt, 60623494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 60633494fc30STejun Heo 60643494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 60653494fc30STejun Heo if (worker->current_pwq == pwq) { 60663494fc30STejun Heo has_in_flight = true; 60673494fc30STejun Heo break; 60683494fc30STejun Heo } 60693494fc30STejun Heo } 60703494fc30STejun Heo if (has_in_flight) { 60713494fc30STejun Heo bool comma = false; 60723494fc30STejun Heo 60733494fc30STejun Heo pr_info(" in-flight:"); 60743494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 60753494fc30STejun Heo if (worker->current_pwq != pwq) 60763494fc30STejun Heo continue; 60773494fc30STejun Heo 60784cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 60794cb1ef64STejun Heo pr_cont_worker_id(worker); 60804cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 60813494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 6082c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 6083c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60843494fc30STejun Heo comma = true; 60853494fc30STejun Heo } 60863494fc30STejun Heo pr_cont("\n"); 60873494fc30STejun Heo } 60883494fc30STejun Heo 60893494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 60903494fc30STejun Heo if (get_work_pwq(work) == pwq) { 60913494fc30STejun Heo has_pending = true; 60923494fc30STejun Heo break; 60933494fc30STejun Heo } 60943494fc30STejun Heo } 60953494fc30STejun Heo if (has_pending) { 60963494fc30STejun Heo bool comma = false; 60973494fc30STejun Heo 60983494fc30STejun Heo pr_info(" pending:"); 60993494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 61003494fc30STejun Heo if (get_work_pwq(work) != pwq) 61013494fc30STejun Heo continue; 61023494fc30STejun Heo 6103c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 61043494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 61053494fc30STejun Heo } 6106c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61073494fc30STejun Heo pr_cont("\n"); 61083494fc30STejun Heo } 61093494fc30STejun Heo 6110f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 61113494fc30STejun Heo bool comma = false; 61123494fc30STejun Heo 6113f97a4a1aSLai Jiangshan pr_info(" inactive:"); 6114f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 6115c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 61163494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 61173494fc30STejun Heo } 6118c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61193494fc30STejun Heo pr_cont("\n"); 61203494fc30STejun Heo } 61213494fc30STejun Heo } 61223494fc30STejun Heo 61233494fc30STejun Heo /** 612455df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 612555df0933SImran Khan * @wq: workqueue whose state will be printed 61263494fc30STejun Heo */ 612755df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 61283494fc30STejun Heo { 61293494fc30STejun Heo struct pool_workqueue *pwq; 61303494fc30STejun Heo bool idle = true; 6131c26e2f2eSTejun Heo unsigned long irq_flags; 61323494fc30STejun Heo 61333494fc30STejun Heo for_each_pwq(pwq, wq) { 6134afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 61353494fc30STejun Heo idle = false; 61363494fc30STejun Heo break; 61373494fc30STejun Heo } 61383494fc30STejun Heo } 613955df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 614055df0933SImran Khan return; 61413494fc30STejun Heo 61423494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 61433494fc30STejun Heo 61443494fc30STejun Heo for_each_pwq(pwq, wq) { 6145c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 6146afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 614757116ce1SJohan Hovold /* 614857116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 614957116ce1SJohan Hovold * drivers that queue work while holding locks 615057116ce1SJohan Hovold * also taken in their write paths. 615157116ce1SJohan Hovold */ 615257116ce1SJohan Hovold printk_deferred_enter(); 61533494fc30STejun Heo show_pwq(pwq); 615457116ce1SJohan Hovold printk_deferred_exit(); 615557116ce1SJohan Hovold } 6156c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 615762635ea8SSergey Senozhatsky /* 615862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 615955df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 616062635ea8SSergey Senozhatsky * hard lockup. 616162635ea8SSergey Senozhatsky */ 616262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 61633494fc30STejun Heo } 616455df0933SImran Khan 61653494fc30STejun Heo } 61663494fc30STejun Heo 616755df0933SImran Khan /** 616855df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 616955df0933SImran Khan * @pool: worker pool whose state will be printed 617055df0933SImran Khan */ 617155df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 617255df0933SImran Khan { 61733494fc30STejun Heo struct worker *worker; 61743494fc30STejun Heo bool first = true; 6175c26e2f2eSTejun Heo unsigned long irq_flags; 6176335a42ebSPetr Mladek unsigned long hung = 0; 61773494fc30STejun Heo 6178c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 61793494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 61803494fc30STejun Heo goto next_pool; 6181335a42ebSPetr Mladek 6182335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6183335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6184335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6185335a42ebSPetr Mladek 618657116ce1SJohan Hovold /* 618757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 618857116ce1SJohan Hovold * queue work while holding locks also taken in their write 618957116ce1SJohan Hovold * paths. 619057116ce1SJohan Hovold */ 619157116ce1SJohan Hovold printk_deferred_enter(); 61923494fc30STejun Heo pr_info("pool %d:", pool->id); 61933494fc30STejun Heo pr_cont_pool_info(pool); 6194335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 61953494fc30STejun Heo if (pool->manager) 61963494fc30STejun Heo pr_cont(" manager: %d", 61973494fc30STejun Heo task_pid_nr(pool->manager->task)); 61983494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 61994cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 62004cb1ef64STejun Heo pr_cont_worker_id(worker); 62013494fc30STejun Heo first = false; 62023494fc30STejun Heo } 62033494fc30STejun Heo pr_cont("\n"); 620457116ce1SJohan Hovold printk_deferred_exit(); 62053494fc30STejun Heo next_pool: 6206c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 620762635ea8SSergey Senozhatsky /* 620862635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 620955df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 621062635ea8SSergey Senozhatsky * hard lockup. 621162635ea8SSergey Senozhatsky */ 621262635ea8SSergey Senozhatsky touch_nmi_watchdog(); 621355df0933SImran Khan 62143494fc30STejun Heo } 62153494fc30STejun Heo 621655df0933SImran Khan /** 621755df0933SImran Khan * show_all_workqueues - dump workqueue state 621855df0933SImran Khan * 6219704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 622055df0933SImran Khan */ 622155df0933SImran Khan void show_all_workqueues(void) 622255df0933SImran Khan { 622355df0933SImran Khan struct workqueue_struct *wq; 622455df0933SImran Khan struct worker_pool *pool; 622555df0933SImran Khan int pi; 622655df0933SImran Khan 622755df0933SImran Khan rcu_read_lock(); 622855df0933SImran Khan 622955df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 623055df0933SImran Khan 623155df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 623255df0933SImran Khan show_one_workqueue(wq); 623355df0933SImran Khan 623455df0933SImran Khan for_each_pool(pool, pi) 623555df0933SImran Khan show_one_worker_pool(pool); 623655df0933SImran Khan 623724acfb71SThomas Gleixner rcu_read_unlock(); 62383494fc30STejun Heo } 62393494fc30STejun Heo 6240704bc669SJungseung Lee /** 6241704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6242704bc669SJungseung Lee * 6243704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6244704bc669SJungseung Lee * still busy. 6245704bc669SJungseung Lee */ 6246704bc669SJungseung Lee void show_freezable_workqueues(void) 6247704bc669SJungseung Lee { 6248704bc669SJungseung Lee struct workqueue_struct *wq; 6249704bc669SJungseung Lee 6250704bc669SJungseung Lee rcu_read_lock(); 6251704bc669SJungseung Lee 6252704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6253704bc669SJungseung Lee 6254704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6255704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6256704bc669SJungseung Lee continue; 6257704bc669SJungseung Lee show_one_workqueue(wq); 6258704bc669SJungseung Lee } 6259704bc669SJungseung Lee 6260704bc669SJungseung Lee rcu_read_unlock(); 6261704bc669SJungseung Lee } 6262704bc669SJungseung Lee 62636b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 62646b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 62656b59808bSTejun Heo { 62666b59808bSTejun Heo int off; 62676b59808bSTejun Heo 62686b59808bSTejun Heo /* always show the actual comm */ 62696b59808bSTejun Heo off = strscpy(buf, task->comm, size); 62706b59808bSTejun Heo if (off < 0) 62716b59808bSTejun Heo return; 62726b59808bSTejun Heo 6273197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 62746b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 62756b59808bSTejun Heo 6276197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6277197f6accSTejun Heo struct worker *worker = kthread_data(task); 6278197f6accSTejun Heo struct worker_pool *pool = worker->pool; 62796b59808bSTejun Heo 62806b59808bSTejun Heo if (pool) { 6281a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 62826b59808bSTejun Heo /* 6283197f6accSTejun Heo * ->desc tracks information (wq name or 6284197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6285197f6accSTejun Heo * current, prepend '+', otherwise '-'. 62866b59808bSTejun Heo */ 62876b59808bSTejun Heo if (worker->desc[0] != '\0') { 62886b59808bSTejun Heo if (worker->current_work) 62896b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 62906b59808bSTejun Heo worker->desc); 62916b59808bSTejun Heo else 62926b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 62936b59808bSTejun Heo worker->desc); 62946b59808bSTejun Heo } 6295a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 62966b59808bSTejun Heo } 6297197f6accSTejun Heo } 62986b59808bSTejun Heo 62996b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 63006b59808bSTejun Heo } 63016b59808bSTejun Heo 630266448bc2SMathieu Malaterre #ifdef CONFIG_SMP 630366448bc2SMathieu Malaterre 6304db7bccf4STejun Heo /* 6305db7bccf4STejun Heo * CPU hotplug. 6306db7bccf4STejun Heo * 6307e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6308112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6309706026c2STejun Heo * pool which make migrating pending and scheduled works very 6310e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 631194cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6312e22bee78STejun Heo * blocked draining impractical. 6313e22bee78STejun Heo * 631424647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6315628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6316628c78e7STejun Heo * cpu comes back online. 6317db7bccf4STejun Heo */ 6318db7bccf4STejun Heo 6319e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6320db7bccf4STejun Heo { 63214ce62e9eSTejun Heo struct worker_pool *pool; 6322db7bccf4STejun Heo struct worker *worker; 6323db7bccf4STejun Heo 6324f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63251258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6326a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6327e22bee78STejun Heo 6328f2d5a0eeSTejun Heo /* 632992f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 633094cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 633111b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6332b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6333b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6334b4ac9384SLai Jiangshan * is on the same cpu. 6335f2d5a0eeSTejun Heo */ 6336da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6337403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6338db7bccf4STejun Heo 633924647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6340f2d5a0eeSTejun Heo 6341e22bee78STejun Heo /* 6342989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6343989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6344989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6345989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6346989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6347eb283428SLai Jiangshan * are served by workers tied to the pool. 6348e22bee78STejun Heo */ 6349bc35f7efSLai Jiangshan pool->nr_running = 0; 6350eb283428SLai Jiangshan 6351eb283428SLai Jiangshan /* 6352eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6353eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6354eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6355eb283428SLai Jiangshan */ 63560219a352STejun Heo kick_pool(pool); 6357989442d7SLai Jiangshan 6358a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6359989442d7SLai Jiangshan 6360793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6361793777bcSValentin Schneider unbind_worker(worker); 6362989442d7SLai Jiangshan 6363989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6364eb283428SLai Jiangshan } 6365db7bccf4STejun Heo } 6366db7bccf4STejun Heo 6367bd7c089eSTejun Heo /** 6368bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6369bd7c089eSTejun Heo * @pool: pool of interest 6370bd7c089eSTejun Heo * 6371a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6372bd7c089eSTejun Heo */ 6373bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6374bd7c089eSTejun Heo { 6375a9ab775bSTejun Heo struct worker *worker; 6376bd7c089eSTejun Heo 63771258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6378bd7c089eSTejun Heo 6379bd7c089eSTejun Heo /* 6380a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6381a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6382402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6383a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6384a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6385bd7c089eSTejun Heo */ 6386c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6387c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6388c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 63899546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6390c63a2e52SValentin Schneider } 6391a9ab775bSTejun Heo 6392a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6393f7c17d26SWanpeng Li 63943de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6395a9ab775bSTejun Heo 6396da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6397a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6398a9ab775bSTejun Heo 6399a9ab775bSTejun Heo /* 6400a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6401a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6402a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6403a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6404a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6405a9ab775bSTejun Heo * concurrency management. Note that when or whether 6406a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6407a9ab775bSTejun Heo * 6408c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6409a9ab775bSTejun Heo * tested without holding any lock in 64106d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6411a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6412a9ab775bSTejun Heo * management operations. 6413bd7c089eSTejun Heo */ 6414a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6415a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6416a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6417c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6418bd7c089eSTejun Heo } 6419a9ab775bSTejun Heo 6420a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6421bd7c089eSTejun Heo } 6422bd7c089eSTejun Heo 64237dbc725eSTejun Heo /** 64247dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 64257dbc725eSTejun Heo * @pool: unbound pool of interest 64267dbc725eSTejun Heo * @cpu: the CPU which is coming up 64277dbc725eSTejun Heo * 64287dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 64297dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 64307dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 64317dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 64327dbc725eSTejun Heo */ 64337dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 64347dbc725eSTejun Heo { 64357dbc725eSTejun Heo static cpumask_t cpumask; 64367dbc725eSTejun Heo struct worker *worker; 64377dbc725eSTejun Heo 64381258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 64397dbc725eSTejun Heo 64407dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 64417dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 64427dbc725eSTejun Heo return; 64437dbc725eSTejun Heo 64447dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 64457dbc725eSTejun Heo 64467dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6447da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6448d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 64497dbc725eSTejun Heo } 64507dbc725eSTejun Heo 64517ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 64521da177e4SLinus Torvalds { 64534ce62e9eSTejun Heo struct worker_pool *pool; 64541da177e4SLinus Torvalds 6455f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 64563ce63377STejun Heo if (pool->nr_workers) 64573ce63377STejun Heo continue; 6458051e1850SLai Jiangshan if (!create_worker(pool)) 64597ee681b2SThomas Gleixner return -ENOMEM; 64603af24433SOleg Nesterov } 64617ee681b2SThomas Gleixner return 0; 64627ee681b2SThomas Gleixner } 64631da177e4SLinus Torvalds 64647ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 64657ee681b2SThomas Gleixner { 64667ee681b2SThomas Gleixner struct worker_pool *pool; 64677ee681b2SThomas Gleixner struct workqueue_struct *wq; 64687ee681b2SThomas Gleixner int pi; 64697ee681b2SThomas Gleixner 647068e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 64717dbc725eSTejun Heo 64727dbc725eSTejun Heo for_each_pool(pool, pi) { 64734cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 64744cb1ef64STejun Heo if (pool->flags & POOL_BH) 64754cb1ef64STejun Heo continue; 647694cf58bbSTejun Heo 64774cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6478f05b558dSLai Jiangshan if (pool->cpu == cpu) 647994cf58bbSTejun Heo rebind_workers(pool); 6480f05b558dSLai Jiangshan else if (pool->cpu < 0) 64817dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 64821258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 648394cf58bbSTejun Heo } 64847dbc725eSTejun Heo 6485fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 64864cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 648784193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 648884193c07STejun Heo 648984193c07STejun Heo if (attrs) { 649084193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 64914cbfd3deSTejun Heo int tcpu; 64924cbfd3deSTejun Heo 649384193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6494fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 64955797b1c1STejun Heo 64965797b1c1STejun Heo mutex_lock(&wq->mutex); 64975797b1c1STejun Heo wq_update_node_max_active(wq, -1); 64985797b1c1STejun Heo mutex_unlock(&wq->mutex); 64994cbfd3deSTejun Heo } 65004cbfd3deSTejun Heo } 65014c16bd32STejun Heo 650268e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 65037ee681b2SThomas Gleixner return 0; 650465758202STejun Heo } 650565758202STejun Heo 65067ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 650765758202STejun Heo { 65084c16bd32STejun Heo struct workqueue_struct *wq; 65098db25e78STejun Heo 65104c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6511e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6512e8b3f8dbSLai Jiangshan return -1; 6513e8b3f8dbSLai Jiangshan 6514e8b3f8dbSLai Jiangshan unbind_workers(cpu); 65154c16bd32STejun Heo 6516fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 65174c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 65184cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 651984193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 652084193c07STejun Heo 652184193c07STejun Heo if (attrs) { 652284193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 65234cbfd3deSTejun Heo int tcpu; 65244cbfd3deSTejun Heo 652584193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6526fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 65275797b1c1STejun Heo 65285797b1c1STejun Heo mutex_lock(&wq->mutex); 65295797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 65305797b1c1STejun Heo mutex_unlock(&wq->mutex); 65314cbfd3deSTejun Heo } 65324cbfd3deSTejun Heo } 65334c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 65344c16bd32STejun Heo 65357ee681b2SThomas Gleixner return 0; 653665758202STejun Heo } 653765758202STejun Heo 65382d3854a3SRusty Russell struct work_for_cpu { 6539ed48ece2STejun Heo struct work_struct work; 65402d3854a3SRusty Russell long (*fn)(void *); 65412d3854a3SRusty Russell void *arg; 65422d3854a3SRusty Russell long ret; 65432d3854a3SRusty Russell }; 65442d3854a3SRusty Russell 6545ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 65462d3854a3SRusty Russell { 6547ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6548ed48ece2STejun Heo 65492d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 65502d3854a3SRusty Russell } 65512d3854a3SRusty Russell 65522d3854a3SRusty Russell /** 6553265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 65542d3854a3SRusty Russell * @cpu: the cpu to run on 65552d3854a3SRusty Russell * @fn: the function to run 65562d3854a3SRusty Russell * @arg: the function arg 6557265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 65582d3854a3SRusty Russell * 655931ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 65606b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6561d185af30SYacine Belkadi * 6562d185af30SYacine Belkadi * Return: The value @fn returns. 65632d3854a3SRusty Russell */ 6564265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6565265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 65662d3854a3SRusty Russell { 6567ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 65682d3854a3SRusty Russell 6569265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6570ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 657112997d1aSBjorn Helgaas flush_work(&wfc.work); 6572440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 65732d3854a3SRusty Russell return wfc.ret; 65742d3854a3SRusty Russell } 6575265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 65760e8d6a93SThomas Gleixner 65770e8d6a93SThomas Gleixner /** 6578265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 65790e8d6a93SThomas Gleixner * @cpu: the cpu to run on 65800e8d6a93SThomas Gleixner * @fn: the function to run 65810e8d6a93SThomas Gleixner * @arg: the function argument 6582265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 65830e8d6a93SThomas Gleixner * 65840e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 65850e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 65860e8d6a93SThomas Gleixner * 65870e8d6a93SThomas Gleixner * Return: The value @fn returns. 65880e8d6a93SThomas Gleixner */ 6589265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6590265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 65910e8d6a93SThomas Gleixner { 65920e8d6a93SThomas Gleixner long ret = -ENODEV; 65930e8d6a93SThomas Gleixner 6594ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 65950e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6596265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6597ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 65980e8d6a93SThomas Gleixner return ret; 65990e8d6a93SThomas Gleixner } 6600265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 66012d3854a3SRusty Russell #endif /* CONFIG_SMP */ 66022d3854a3SRusty Russell 6603a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6604e7577c50SRusty Russell 6605a0a1a5fdSTejun Heo /** 6606a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6607a0a1a5fdSTejun Heo * 660858a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6609f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6610706026c2STejun Heo * pool->worklist. 6611a0a1a5fdSTejun Heo * 6612a0a1a5fdSTejun Heo * CONTEXT: 6613a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6614a0a1a5fdSTejun Heo */ 6615a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6616a0a1a5fdSTejun Heo { 661724b8a847STejun Heo struct workqueue_struct *wq; 6618a0a1a5fdSTejun Heo 661968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6620a0a1a5fdSTejun Heo 66216183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6622a0a1a5fdSTejun Heo workqueue_freezing = true; 6623a0a1a5fdSTejun Heo 662424b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6625a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6626a045a272STejun Heo wq_adjust_max_active(wq); 6627a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6628a1056305STejun Heo } 66295bcab335STejun Heo 663068e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6631a0a1a5fdSTejun Heo } 6632a0a1a5fdSTejun Heo 6633a0a1a5fdSTejun Heo /** 663458a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6635a0a1a5fdSTejun Heo * 6636a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6637a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6638a0a1a5fdSTejun Heo * 6639a0a1a5fdSTejun Heo * CONTEXT: 664068e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6641a0a1a5fdSTejun Heo * 6642d185af30SYacine Belkadi * Return: 664358a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 664458a69cb4STejun Heo * is complete. 6645a0a1a5fdSTejun Heo */ 6646a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6647a0a1a5fdSTejun Heo { 6648a0a1a5fdSTejun Heo bool busy = false; 664924b8a847STejun Heo struct workqueue_struct *wq; 665024b8a847STejun Heo struct pool_workqueue *pwq; 6651a0a1a5fdSTejun Heo 665268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6653a0a1a5fdSTejun Heo 66546183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6655a0a1a5fdSTejun Heo 665624b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 665724b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 665824b8a847STejun Heo continue; 6659a0a1a5fdSTejun Heo /* 6660a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6661a0a1a5fdSTejun Heo * to peek without lock. 6662a0a1a5fdSTejun Heo */ 666324acfb71SThomas Gleixner rcu_read_lock(); 666424b8a847STejun Heo for_each_pwq(pwq, wq) { 66656183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6666112202d9STejun Heo if (pwq->nr_active) { 6667a0a1a5fdSTejun Heo busy = true; 666824acfb71SThomas Gleixner rcu_read_unlock(); 6669a0a1a5fdSTejun Heo goto out_unlock; 6670a0a1a5fdSTejun Heo } 6671a0a1a5fdSTejun Heo } 667224acfb71SThomas Gleixner rcu_read_unlock(); 6673a0a1a5fdSTejun Heo } 6674a0a1a5fdSTejun Heo out_unlock: 667568e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6676a0a1a5fdSTejun Heo return busy; 6677a0a1a5fdSTejun Heo } 6678a0a1a5fdSTejun Heo 6679a0a1a5fdSTejun Heo /** 6680a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6681a0a1a5fdSTejun Heo * 6682a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6683706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6684a0a1a5fdSTejun Heo * 6685a0a1a5fdSTejun Heo * CONTEXT: 6686a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6687a0a1a5fdSTejun Heo */ 6688a0a1a5fdSTejun Heo void thaw_workqueues(void) 6689a0a1a5fdSTejun Heo { 669024b8a847STejun Heo struct workqueue_struct *wq; 6691a0a1a5fdSTejun Heo 669268e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6693a0a1a5fdSTejun Heo 6694a0a1a5fdSTejun Heo if (!workqueue_freezing) 6695a0a1a5fdSTejun Heo goto out_unlock; 6696a0a1a5fdSTejun Heo 669774b414eaSLai Jiangshan workqueue_freezing = false; 669824b8a847STejun Heo 669924b8a847STejun Heo /* restore max_active and repopulate worklist */ 670024b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6701a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6702a045a272STejun Heo wq_adjust_max_active(wq); 6703a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 670424b8a847STejun Heo } 670524b8a847STejun Heo 6706a0a1a5fdSTejun Heo out_unlock: 670768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6708a0a1a5fdSTejun Heo } 6709a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6710a0a1a5fdSTejun Heo 671199c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6712042f7df1SLai Jiangshan { 6713042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6714042f7df1SLai Jiangshan int ret = 0; 6715042f7df1SLai Jiangshan struct workqueue_struct *wq; 6716042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6717042f7df1SLai Jiangshan 6718042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6719042f7df1SLai Jiangshan 6720042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 67218eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6722042f7df1SLai Jiangshan continue; 6723042f7df1SLai Jiangshan 672499c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 672584193c07STejun Heo if (IS_ERR(ctx)) { 672684193c07STejun Heo ret = PTR_ERR(ctx); 6727042f7df1SLai Jiangshan break; 6728042f7df1SLai Jiangshan } 6729042f7df1SLai Jiangshan 6730042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6731042f7df1SLai Jiangshan } 6732042f7df1SLai Jiangshan 6733042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6734042f7df1SLai Jiangshan if (!ret) 6735042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6736042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6737042f7df1SLai Jiangshan } 6738042f7df1SLai Jiangshan 673999c621efSLai Jiangshan if (!ret) { 674099c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 674199c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 674299c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 674399c621efSLai Jiangshan } 6744042f7df1SLai Jiangshan return ret; 6745042f7df1SLai Jiangshan } 6746042f7df1SLai Jiangshan 6747042f7df1SLai Jiangshan /** 6748fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6749fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6750fe28f631SWaiman Long * 6751fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6752fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6753fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6754fe28f631SWaiman Long */ 6755fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6756fe28f631SWaiman Long { 6757fe28f631SWaiman Long cpumask_var_t cpumask; 6758fe28f631SWaiman Long int ret = 0; 6759fe28f631SWaiman Long 6760fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6761fe28f631SWaiman Long return -ENOMEM; 6762fe28f631SWaiman Long 6763fe28f631SWaiman Long lockdep_assert_cpus_held(); 6764fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6765fe28f631SWaiman Long 6766fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6767fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6768fe28f631SWaiman Long 6769fe28f631SWaiman Long /* 6770fe28f631SWaiman Long * If the operation fails, it will fall back to 6771fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6772fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6773fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6774fe28f631SWaiman Long */ 6775fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6776fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6777fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6778fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6779fe28f631SWaiman Long 6780fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6781fe28f631SWaiman Long free_cpumask_var(cpumask); 6782fe28f631SWaiman Long return ret; 6783fe28f631SWaiman Long } 6784fe28f631SWaiman Long 678563c5484eSTejun Heo static int parse_affn_scope(const char *val) 678663c5484eSTejun Heo { 678763c5484eSTejun Heo int i; 678863c5484eSTejun Heo 678963c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 679063c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 679163c5484eSTejun Heo return i; 679263c5484eSTejun Heo } 679363c5484eSTejun Heo return -EINVAL; 679463c5484eSTejun Heo } 679563c5484eSTejun Heo 679663c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 679763c5484eSTejun Heo { 6798523a301eSTejun Heo struct workqueue_struct *wq; 6799523a301eSTejun Heo int affn, cpu; 680063c5484eSTejun Heo 680163c5484eSTejun Heo affn = parse_affn_scope(val); 680263c5484eSTejun Heo if (affn < 0) 680363c5484eSTejun Heo return affn; 6804523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6805523a301eSTejun Heo return -EINVAL; 6806523a301eSTejun Heo 6807523a301eSTejun Heo cpus_read_lock(); 6808523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 680963c5484eSTejun Heo 681063c5484eSTejun Heo wq_affn_dfl = affn; 6811523a301eSTejun Heo 6812523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6813523a301eSTejun Heo for_each_online_cpu(cpu) { 6814523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6815523a301eSTejun Heo } 6816523a301eSTejun Heo } 6817523a301eSTejun Heo 6818523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6819523a301eSTejun Heo cpus_read_unlock(); 6820523a301eSTejun Heo 682163c5484eSTejun Heo return 0; 682263c5484eSTejun Heo } 682363c5484eSTejun Heo 682463c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 682563c5484eSTejun Heo { 682663c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 682763c5484eSTejun Heo } 682863c5484eSTejun Heo 682963c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 683063c5484eSTejun Heo .set = wq_affn_dfl_set, 683163c5484eSTejun Heo .get = wq_affn_dfl_get, 683263c5484eSTejun Heo }; 683363c5484eSTejun Heo 683463c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 683563c5484eSTejun Heo 68366ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 68376ba94429SFrederic Weisbecker /* 68386ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 68396ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 68406ba94429SFrederic Weisbecker * following attributes. 68416ba94429SFrederic Weisbecker * 68426ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 68436ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 68446ba94429SFrederic Weisbecker * 68456ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 68466ba94429SFrederic Weisbecker * 68476ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 68486ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 684963c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 68508639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 68516ba94429SFrederic Weisbecker */ 68526ba94429SFrederic Weisbecker struct wq_device { 68536ba94429SFrederic Weisbecker struct workqueue_struct *wq; 68546ba94429SFrederic Weisbecker struct device dev; 68556ba94429SFrederic Weisbecker }; 68566ba94429SFrederic Weisbecker 68576ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 68586ba94429SFrederic Weisbecker { 68596ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 68606ba94429SFrederic Weisbecker 68616ba94429SFrederic Weisbecker return wq_dev->wq; 68626ba94429SFrederic Weisbecker } 68636ba94429SFrederic Weisbecker 68646ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 68656ba94429SFrederic Weisbecker char *buf) 68666ba94429SFrederic Weisbecker { 68676ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68686ba94429SFrederic Weisbecker 68696ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", (bool)!(wq->flags & WQ_UNBOUND)); 68706ba94429SFrederic Weisbecker } 68716ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 68726ba94429SFrederic Weisbecker 68736ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 68746ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 68756ba94429SFrederic Weisbecker { 68766ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68776ba94429SFrederic Weisbecker 68786ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 68796ba94429SFrederic Weisbecker } 68806ba94429SFrederic Weisbecker 68816ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 68826ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 68836ba94429SFrederic Weisbecker size_t count) 68846ba94429SFrederic Weisbecker { 68856ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68866ba94429SFrederic Weisbecker int val; 68876ba94429SFrederic Weisbecker 68886ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 68896ba94429SFrederic Weisbecker return -EINVAL; 68906ba94429SFrederic Weisbecker 68916ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 68926ba94429SFrederic Weisbecker return count; 68936ba94429SFrederic Weisbecker } 68946ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 68956ba94429SFrederic Weisbecker 68966ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 68976ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 68986ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 68996ba94429SFrederic Weisbecker NULL, 69006ba94429SFrederic Weisbecker }; 69016ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 69026ba94429SFrederic Weisbecker 690349277a5bSWaiman Long static void apply_wqattrs_lock(void) 690449277a5bSWaiman Long { 690549277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 690649277a5bSWaiman Long cpus_read_lock(); 690749277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 690849277a5bSWaiman Long } 690949277a5bSWaiman Long 691049277a5bSWaiman Long static void apply_wqattrs_unlock(void) 691149277a5bSWaiman Long { 691249277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 691349277a5bSWaiman Long cpus_read_unlock(); 691449277a5bSWaiman Long } 691549277a5bSWaiman Long 69166ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 69176ba94429SFrederic Weisbecker char *buf) 69186ba94429SFrederic Weisbecker { 69196ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69206ba94429SFrederic Weisbecker int written; 69216ba94429SFrederic Weisbecker 69226ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 69236ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 69246ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 69256ba94429SFrederic Weisbecker 69266ba94429SFrederic Weisbecker return written; 69276ba94429SFrederic Weisbecker } 69286ba94429SFrederic Weisbecker 69296ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 69306ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 69316ba94429SFrederic Weisbecker { 69326ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 69336ba94429SFrederic Weisbecker 6934899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6935899a94feSLai Jiangshan 6936be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 69376ba94429SFrederic Weisbecker if (!attrs) 69386ba94429SFrederic Weisbecker return NULL; 69396ba94429SFrederic Weisbecker 69406ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 69416ba94429SFrederic Weisbecker return attrs; 69426ba94429SFrederic Weisbecker } 69436ba94429SFrederic Weisbecker 69446ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 69456ba94429SFrederic Weisbecker const char *buf, size_t count) 69466ba94429SFrederic Weisbecker { 69476ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69486ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6949d4d3e257SLai Jiangshan int ret = -ENOMEM; 6950d4d3e257SLai Jiangshan 6951d4d3e257SLai Jiangshan apply_wqattrs_lock(); 69526ba94429SFrederic Weisbecker 69536ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 69546ba94429SFrederic Weisbecker if (!attrs) 6955d4d3e257SLai Jiangshan goto out_unlock; 69566ba94429SFrederic Weisbecker 69576ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 69586ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6959d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 69606ba94429SFrederic Weisbecker else 69616ba94429SFrederic Weisbecker ret = -EINVAL; 69626ba94429SFrederic Weisbecker 6963d4d3e257SLai Jiangshan out_unlock: 6964d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 69656ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 69666ba94429SFrederic Weisbecker return ret ?: count; 69676ba94429SFrederic Weisbecker } 69686ba94429SFrederic Weisbecker 69696ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 69706ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 69716ba94429SFrederic Weisbecker { 69726ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69736ba94429SFrederic Weisbecker int written; 69746ba94429SFrederic Weisbecker 69756ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 69766ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 69776ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 69786ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 69796ba94429SFrederic Weisbecker return written; 69806ba94429SFrederic Weisbecker } 69816ba94429SFrederic Weisbecker 69826ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 69836ba94429SFrederic Weisbecker struct device_attribute *attr, 69846ba94429SFrederic Weisbecker const char *buf, size_t count) 69856ba94429SFrederic Weisbecker { 69866ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69876ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6988d4d3e257SLai Jiangshan int ret = -ENOMEM; 6989d4d3e257SLai Jiangshan 6990d4d3e257SLai Jiangshan apply_wqattrs_lock(); 69916ba94429SFrederic Weisbecker 69926ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 69936ba94429SFrederic Weisbecker if (!attrs) 6994d4d3e257SLai Jiangshan goto out_unlock; 69956ba94429SFrederic Weisbecker 69966ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 69976ba94429SFrederic Weisbecker if (!ret) 6998d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 69996ba94429SFrederic Weisbecker 7000d4d3e257SLai Jiangshan out_unlock: 7001d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 70026ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 70036ba94429SFrederic Weisbecker return ret ?: count; 70046ba94429SFrederic Weisbecker } 70056ba94429SFrederic Weisbecker 700663c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 700763c5484eSTejun Heo struct device_attribute *attr, char *buf) 700863c5484eSTejun Heo { 700963c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 701063c5484eSTejun Heo int written; 701163c5484eSTejun Heo 701263c5484eSTejun Heo mutex_lock(&wq->mutex); 7013523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 7014523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 7015523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 7016523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 7017523a301eSTejun Heo else 701863c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 701963c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 702063c5484eSTejun Heo mutex_unlock(&wq->mutex); 702163c5484eSTejun Heo 702263c5484eSTejun Heo return written; 702363c5484eSTejun Heo } 702463c5484eSTejun Heo 702563c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 702663c5484eSTejun Heo struct device_attribute *attr, 702763c5484eSTejun Heo const char *buf, size_t count) 702863c5484eSTejun Heo { 702963c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 703063c5484eSTejun Heo struct workqueue_attrs *attrs; 703163c5484eSTejun Heo int affn, ret = -ENOMEM; 703263c5484eSTejun Heo 703363c5484eSTejun Heo affn = parse_affn_scope(buf); 703463c5484eSTejun Heo if (affn < 0) 703563c5484eSTejun Heo return affn; 703663c5484eSTejun Heo 703763c5484eSTejun Heo apply_wqattrs_lock(); 703863c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 703963c5484eSTejun Heo if (attrs) { 704063c5484eSTejun Heo attrs->affn_scope = affn; 704163c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 704263c5484eSTejun Heo } 704363c5484eSTejun Heo apply_wqattrs_unlock(); 704463c5484eSTejun Heo free_workqueue_attrs(attrs); 704563c5484eSTejun Heo return ret ?: count; 704663c5484eSTejun Heo } 704763c5484eSTejun Heo 70488639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 70498639ecebSTejun Heo struct device_attribute *attr, char *buf) 70508639ecebSTejun Heo { 70518639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 70528639ecebSTejun Heo 70538639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 70548639ecebSTejun Heo wq->unbound_attrs->affn_strict); 70558639ecebSTejun Heo } 70568639ecebSTejun Heo 70578639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 70588639ecebSTejun Heo struct device_attribute *attr, 70598639ecebSTejun Heo const char *buf, size_t count) 70608639ecebSTejun Heo { 70618639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 70628639ecebSTejun Heo struct workqueue_attrs *attrs; 70638639ecebSTejun Heo int v, ret = -ENOMEM; 70648639ecebSTejun Heo 70658639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 70668639ecebSTejun Heo return -EINVAL; 70678639ecebSTejun Heo 70688639ecebSTejun Heo apply_wqattrs_lock(); 70698639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 70708639ecebSTejun Heo if (attrs) { 70718639ecebSTejun Heo attrs->affn_strict = (bool)v; 70728639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 70738639ecebSTejun Heo } 70748639ecebSTejun Heo apply_wqattrs_unlock(); 70758639ecebSTejun Heo free_workqueue_attrs(attrs); 70768639ecebSTejun Heo return ret ?: count; 70778639ecebSTejun Heo } 70788639ecebSTejun Heo 70796ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 70806ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 70816ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 708263c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 70838639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 70846ba94429SFrederic Weisbecker __ATTR_NULL, 70856ba94429SFrederic Weisbecker }; 70866ba94429SFrederic Weisbecker 70875df9197eSRicardo B. Marliere static const struct bus_type wq_subsys = { 70886ba94429SFrederic Weisbecker .name = "workqueue", 70896ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 70906ba94429SFrederic Weisbecker }; 70916ba94429SFrederic Weisbecker 709249277a5bSWaiman Long /** 709349277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 709449277a5bSWaiman Long * @cpumask: the cpumask to set 709549277a5bSWaiman Long * 709649277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 709749277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 709849277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 709949277a5bSWaiman Long * 710049277a5bSWaiman Long * Return: 0 - Success 710149277a5bSWaiman Long * -EINVAL - Invalid @cpumask 710249277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 710349277a5bSWaiman Long */ 710449277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 710549277a5bSWaiman Long { 710649277a5bSWaiman Long int ret = -EINVAL; 710749277a5bSWaiman Long 710849277a5bSWaiman Long /* 710949277a5bSWaiman Long * Not excluding isolated cpus on purpose. 711049277a5bSWaiman Long * If the user wishes to include them, we allow that. 711149277a5bSWaiman Long */ 711249277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 711349277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 711449277a5bSWaiman Long apply_wqattrs_lock(); 711549277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 711649277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 711749277a5bSWaiman Long ret = 0; 711849277a5bSWaiman Long goto out_unlock; 711949277a5bSWaiman Long } 712049277a5bSWaiman Long 712149277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 712249277a5bSWaiman Long 712349277a5bSWaiman Long out_unlock: 712449277a5bSWaiman Long apply_wqattrs_unlock(); 712549277a5bSWaiman Long } 712649277a5bSWaiman Long 712749277a5bSWaiman Long return ret; 712849277a5bSWaiman Long } 712949277a5bSWaiman Long 7130fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 7131fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 7132b05a7928SFrederic Weisbecker { 7133b05a7928SFrederic Weisbecker int written; 7134b05a7928SFrederic Weisbecker 7135042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 7136fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 7137042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 7138b05a7928SFrederic Weisbecker 7139b05a7928SFrederic Weisbecker return written; 7140b05a7928SFrederic Weisbecker } 7141b05a7928SFrederic Weisbecker 7142fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 7143fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7144fe28f631SWaiman Long { 7145fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 7146fe28f631SWaiman Long } 7147fe28f631SWaiman Long 7148fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 7149fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7150fe28f631SWaiman Long { 7151fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 7152fe28f631SWaiman Long } 7153fe28f631SWaiman Long 7154fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 7155fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7156fe28f631SWaiman Long { 7157fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 7158fe28f631SWaiman Long } 7159fe28f631SWaiman Long 7160042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 7161042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 7162042f7df1SLai Jiangshan { 7163042f7df1SLai Jiangshan cpumask_var_t cpumask; 7164042f7df1SLai Jiangshan int ret; 7165042f7df1SLai Jiangshan 7166042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 7167042f7df1SLai Jiangshan return -ENOMEM; 7168042f7df1SLai Jiangshan 7169042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7170042f7df1SLai Jiangshan if (!ret) 7171042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7172042f7df1SLai Jiangshan 7173042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7174042f7df1SLai Jiangshan return ret ? ret : count; 7175042f7df1SLai Jiangshan } 7176042f7df1SLai Jiangshan 7177fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7178042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7179fe28f631SWaiman Long wq_unbound_cpumask_store), 7180fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7181fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7182fe28f631SWaiman Long __ATTR_NULL, 7183fe28f631SWaiman Long }; 7184b05a7928SFrederic Weisbecker 71856ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 71866ba94429SFrederic Weisbecker { 7187686f6697SGreg Kroah-Hartman struct device *dev_root; 7188b05a7928SFrederic Weisbecker int err; 7189b05a7928SFrederic Weisbecker 7190b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7191b05a7928SFrederic Weisbecker if (err) 7192b05a7928SFrederic Weisbecker return err; 7193b05a7928SFrederic Weisbecker 7194686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7195686f6697SGreg Kroah-Hartman if (dev_root) { 7196fe28f631SWaiman Long struct device_attribute *attr; 7197fe28f631SWaiman Long 7198fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7199fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7200fe28f631SWaiman Long if (err) 7201fe28f631SWaiman Long break; 7202fe28f631SWaiman Long } 7203686f6697SGreg Kroah-Hartman put_device(dev_root); 7204686f6697SGreg Kroah-Hartman } 7205686f6697SGreg Kroah-Hartman return err; 72066ba94429SFrederic Weisbecker } 72076ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 72086ba94429SFrederic Weisbecker 72096ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 72106ba94429SFrederic Weisbecker { 72116ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 72126ba94429SFrederic Weisbecker 72136ba94429SFrederic Weisbecker kfree(wq_dev); 72146ba94429SFrederic Weisbecker } 72156ba94429SFrederic Weisbecker 72166ba94429SFrederic Weisbecker /** 72176ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 72186ba94429SFrederic Weisbecker * @wq: the workqueue to register 72196ba94429SFrederic Weisbecker * 72206ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 72216ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 72226ba94429SFrederic Weisbecker * which is the preferred method. 72236ba94429SFrederic Weisbecker * 72246ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 72256ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 72266ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 72276ba94429SFrederic Weisbecker * attributes. 72286ba94429SFrederic Weisbecker * 72296ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 72306ba94429SFrederic Weisbecker */ 72316ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 72326ba94429SFrederic Weisbecker { 72336ba94429SFrederic Weisbecker struct wq_device *wq_dev; 72346ba94429SFrederic Weisbecker int ret; 72356ba94429SFrederic Weisbecker 72366ba94429SFrederic Weisbecker /* 72374c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 72384c065dbcSWaiman Long * ordered workqueues. 72396ba94429SFrederic Weisbecker */ 72403bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 72416ba94429SFrederic Weisbecker return -EINVAL; 72426ba94429SFrederic Weisbecker 72436ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 72446ba94429SFrederic Weisbecker if (!wq_dev) 72456ba94429SFrederic Weisbecker return -ENOMEM; 72466ba94429SFrederic Weisbecker 72476ba94429SFrederic Weisbecker wq_dev->wq = wq; 72486ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 72496ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 725023217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 72516ba94429SFrederic Weisbecker 72526ba94429SFrederic Weisbecker /* 72536ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 72546ba94429SFrederic Weisbecker * everything is ready. 72556ba94429SFrederic Weisbecker */ 72566ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 72576ba94429SFrederic Weisbecker 72586ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 72596ba94429SFrederic Weisbecker if (ret) { 7260537f4146SArvind Yadav put_device(&wq_dev->dev); 72616ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72626ba94429SFrederic Weisbecker return ret; 72636ba94429SFrederic Weisbecker } 72646ba94429SFrederic Weisbecker 72656ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 72666ba94429SFrederic Weisbecker struct device_attribute *attr; 72676ba94429SFrederic Weisbecker 72686ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 72696ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 72706ba94429SFrederic Weisbecker if (ret) { 72716ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 72726ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72736ba94429SFrederic Weisbecker return ret; 72746ba94429SFrederic Weisbecker } 72756ba94429SFrederic Weisbecker } 72766ba94429SFrederic Weisbecker } 72776ba94429SFrederic Weisbecker 72786ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 72796ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 72806ba94429SFrederic Weisbecker return 0; 72816ba94429SFrederic Weisbecker } 72826ba94429SFrederic Weisbecker 72836ba94429SFrederic Weisbecker /** 72846ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 72856ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 72866ba94429SFrederic Weisbecker * 72876ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 72886ba94429SFrederic Weisbecker */ 72896ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 72906ba94429SFrederic Weisbecker { 72916ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 72926ba94429SFrederic Weisbecker 72936ba94429SFrederic Weisbecker if (!wq->wq_dev) 72946ba94429SFrederic Weisbecker return; 72956ba94429SFrederic Weisbecker 72966ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72976ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 72986ba94429SFrederic Weisbecker } 72996ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 73006ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 73016ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 73026ba94429SFrederic Weisbecker 730382607adcSTejun Heo /* 730482607adcSTejun Heo * Workqueue watchdog. 730582607adcSTejun Heo * 730682607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 730782607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 730882607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 730982607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 731082607adcSTejun Heo * largely opaque. 731182607adcSTejun Heo * 731282607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 731382607adcSTejun Heo * state if some pools failed to make forward progress for a while where 731482607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 731582607adcSTejun Heo * 731682607adcSTejun Heo * This mechanism is controlled through the kernel parameter 731782607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 731882607adcSTejun Heo * corresponding sysfs parameter file. 731982607adcSTejun Heo */ 732082607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 732182607adcSTejun Heo 732282607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 73235cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 732482607adcSTejun Heo 732582607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 732682607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 732782607adcSTejun Heo 7328cd2440d6SPetr Mladek /* 7329cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7330cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7331cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7332cd2440d6SPetr Mladek * in all other situations. 7333cd2440d6SPetr Mladek */ 7334cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7335cd2440d6SPetr Mladek { 7336cd2440d6SPetr Mladek struct worker *worker; 7337c26e2f2eSTejun Heo unsigned long irq_flags; 7338cd2440d6SPetr Mladek int bkt; 7339cd2440d6SPetr Mladek 7340c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 7341cd2440d6SPetr Mladek 7342cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7343cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7344cd2440d6SPetr Mladek /* 7345cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7346cd2440d6SPetr Mladek * drivers that queue work while holding locks 7347cd2440d6SPetr Mladek * also taken in their write paths. 7348cd2440d6SPetr Mladek */ 7349cd2440d6SPetr Mladek printk_deferred_enter(); 7350cd2440d6SPetr Mladek 7351cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7352cd2440d6SPetr Mladek sched_show_task(worker->task); 7353cd2440d6SPetr Mladek 7354cd2440d6SPetr Mladek printk_deferred_exit(); 7355cd2440d6SPetr Mladek } 7356cd2440d6SPetr Mladek } 7357cd2440d6SPetr Mladek 7358c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 7359cd2440d6SPetr Mladek } 7360cd2440d6SPetr Mladek 7361cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7362cd2440d6SPetr Mladek { 7363cd2440d6SPetr Mladek struct worker_pool *pool; 7364cd2440d6SPetr Mladek int pi; 7365cd2440d6SPetr Mladek 7366cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7367cd2440d6SPetr Mladek 7368cd2440d6SPetr Mladek rcu_read_lock(); 7369cd2440d6SPetr Mladek 7370cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7371cd2440d6SPetr Mladek if (pool->cpu_stall) 7372cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7373cd2440d6SPetr Mladek 7374cd2440d6SPetr Mladek } 7375cd2440d6SPetr Mladek 7376cd2440d6SPetr Mladek rcu_read_unlock(); 7377cd2440d6SPetr Mladek } 7378cd2440d6SPetr Mladek 737982607adcSTejun Heo static void wq_watchdog_reset_touched(void) 738082607adcSTejun Heo { 738182607adcSTejun Heo int cpu; 738282607adcSTejun Heo 738382607adcSTejun Heo wq_watchdog_touched = jiffies; 738482607adcSTejun Heo for_each_possible_cpu(cpu) 738582607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 738682607adcSTejun Heo } 738782607adcSTejun Heo 73885cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 738982607adcSTejun Heo { 739082607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 739182607adcSTejun Heo bool lockup_detected = false; 7392cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7393940d71c6SSergey Senozhatsky unsigned long now = jiffies; 739482607adcSTejun Heo struct worker_pool *pool; 739582607adcSTejun Heo int pi; 739682607adcSTejun Heo 739782607adcSTejun Heo if (!thresh) 739882607adcSTejun Heo return; 739982607adcSTejun Heo 740082607adcSTejun Heo rcu_read_lock(); 740182607adcSTejun Heo 740282607adcSTejun Heo for_each_pool(pool, pi) { 740382607adcSTejun Heo unsigned long pool_ts, touched, ts; 740482607adcSTejun Heo 7405cd2440d6SPetr Mladek pool->cpu_stall = false; 740682607adcSTejun Heo if (list_empty(&pool->worklist)) 740782607adcSTejun Heo continue; 740882607adcSTejun Heo 7409940d71c6SSergey Senozhatsky /* 7410940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7411940d71c6SSergey Senozhatsky * the watchdog like a stall. 7412940d71c6SSergey Senozhatsky */ 7413940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7414940d71c6SSergey Senozhatsky 741582607adcSTejun Heo /* get the latest of pool and touched timestamps */ 741689e28ce6SWang Qing if (pool->cpu >= 0) 741789e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 741889e28ce6SWang Qing else 741982607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 742089e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 742182607adcSTejun Heo 742282607adcSTejun Heo if (time_after(pool_ts, touched)) 742382607adcSTejun Heo ts = pool_ts; 742482607adcSTejun Heo else 742582607adcSTejun Heo ts = touched; 742682607adcSTejun Heo 742782607adcSTejun Heo /* did we stall? */ 7428940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 742982607adcSTejun Heo lockup_detected = true; 74304cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7431cd2440d6SPetr Mladek pool->cpu_stall = true; 7432cd2440d6SPetr Mladek cpu_pool_stall = true; 7433cd2440d6SPetr Mladek } 743482607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 743582607adcSTejun Heo pr_cont_pool_info(pool); 743682607adcSTejun Heo pr_cont(" stuck for %us!\n", 7437940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 743882607adcSTejun Heo } 7439cd2440d6SPetr Mladek 7440cd2440d6SPetr Mladek 744182607adcSTejun Heo } 744282607adcSTejun Heo 744382607adcSTejun Heo rcu_read_unlock(); 744482607adcSTejun Heo 744582607adcSTejun Heo if (lockup_detected) 744655df0933SImran Khan show_all_workqueues(); 744782607adcSTejun Heo 7448cd2440d6SPetr Mladek if (cpu_pool_stall) 7449cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7450cd2440d6SPetr Mladek 745182607adcSTejun Heo wq_watchdog_reset_touched(); 745282607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 745382607adcSTejun Heo } 745482607adcSTejun Heo 7455cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 745682607adcSTejun Heo { 745782607adcSTejun Heo if (cpu >= 0) 745882607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 745989e28ce6SWang Qing 746082607adcSTejun Heo wq_watchdog_touched = jiffies; 746182607adcSTejun Heo } 746282607adcSTejun Heo 746382607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 746482607adcSTejun Heo { 746582607adcSTejun Heo wq_watchdog_thresh = 0; 746682607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 746782607adcSTejun Heo 746882607adcSTejun Heo if (thresh) { 746982607adcSTejun Heo wq_watchdog_thresh = thresh; 747082607adcSTejun Heo wq_watchdog_reset_touched(); 747182607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 747282607adcSTejun Heo } 747382607adcSTejun Heo } 747482607adcSTejun Heo 747582607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 747682607adcSTejun Heo const struct kernel_param *kp) 747782607adcSTejun Heo { 747882607adcSTejun Heo unsigned long thresh; 747982607adcSTejun Heo int ret; 748082607adcSTejun Heo 748182607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 748282607adcSTejun Heo if (ret) 748382607adcSTejun Heo return ret; 748482607adcSTejun Heo 748582607adcSTejun Heo if (system_wq) 748682607adcSTejun Heo wq_watchdog_set_thresh(thresh); 748782607adcSTejun Heo else 748882607adcSTejun Heo wq_watchdog_thresh = thresh; 748982607adcSTejun Heo 749082607adcSTejun Heo return 0; 749182607adcSTejun Heo } 749282607adcSTejun Heo 749382607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 749482607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 749582607adcSTejun Heo .get = param_get_ulong, 749682607adcSTejun Heo }; 749782607adcSTejun Heo 749882607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 749982607adcSTejun Heo 0644); 750082607adcSTejun Heo 750182607adcSTejun Heo static void wq_watchdog_init(void) 750282607adcSTejun Heo { 75035cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 750482607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 750582607adcSTejun Heo } 750682607adcSTejun Heo 750782607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 750882607adcSTejun Heo 750982607adcSTejun Heo static inline void wq_watchdog_init(void) { } 751082607adcSTejun Heo 751182607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 751282607adcSTejun Heo 75132f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work) 75142f34d733STejun Heo { 75152f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 75162f34d733STejun Heo } 75172f34d733STejun Heo 75182f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work) 75192f34d733STejun Heo { 75202f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 75212f34d733STejun Heo } 75222f34d733STejun Heo 75234a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 75244a6c5607STejun Heo { 75254a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 75264a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 75274a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 75284a6c5607STejun Heo return; 75294a6c5607STejun Heo } 75304a6c5607STejun Heo 75314a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 75324a6c5607STejun Heo } 75334a6c5607STejun Heo 75342fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 75352fcdb1b4STejun Heo { 75362fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 75372fcdb1b4STejun Heo pool->cpu = cpu; 75382fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 75392fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 75402fcdb1b4STejun Heo pool->attrs->nice = nice; 75412fcdb1b4STejun Heo pool->attrs->affn_strict = true; 75422fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 75432fcdb1b4STejun Heo 75442fcdb1b4STejun Heo /* alloc pool ID */ 75452fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 75462fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 75472fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 75482fcdb1b4STejun Heo } 75492fcdb1b4STejun Heo 75503347fa09STejun Heo /** 75513347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 75523347fa09STejun Heo * 75532930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 75542930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 75552930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 75562930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 75572930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 75582930155bSTejun Heo * before early initcalls. 75593347fa09STejun Heo */ 75602333e829SYu Chen void __init workqueue_init_early(void) 75611da177e4SLinus Torvalds { 756284193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 75637a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 75642f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal, 75652f34d733STejun Heo bh_pool_kick_highpri }; 75667a4e344cSTejun Heo int i, cpu; 7567c34056a3STejun Heo 756810cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7569e904e6c2STejun Heo 7570b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7571fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7572fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7573b05a7928SFrederic Weisbecker 75744a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 75754a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 75764a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7577ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 75784a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7579ace3c549Stiozhang 7580fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 75818b03ae3cSTejun Heo 75828b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7583e22bee78STejun Heo 75842930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 75852930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 75862930155bSTejun Heo 75877bd20b6bSMarcelo Tosatti /* 75887bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 75897bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 75907bd20b6bSMarcelo Tosatti */ 75917bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 75927bd20b6bSMarcelo Tosatti wq_power_efficient = true; 75937bd20b6bSMarcelo Tosatti 759484193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 759584193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 759684193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 759784193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 759884193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 759984193c07STejun Heo 760084193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 760184193c07STejun Heo 760284193c07STejun Heo pt->nr_pods = 1; 760384193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 760484193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 760584193c07STejun Heo pt->cpu_pod[0] = 0; 760684193c07STejun Heo 76074cb1ef64STejun Heo /* initialize BH and CPU pools */ 76081da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 76091da177e4SLinus Torvalds struct worker_pool *pool; 76101da177e4SLinus Torvalds 76111da177e4SLinus Torvalds i = 0; 76124cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 76132f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]); 76144cb1ef64STejun Heo pool->flags |= POOL_BH; 76152f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]); 76162f34d733STejun Heo i++; 76174cb1ef64STejun Heo } 76184cb1ef64STejun Heo 76194cb1ef64STejun Heo i = 0; 76202fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 76212fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 76221da177e4SLinus Torvalds } 76231da177e4SLinus Torvalds 76248a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 762529c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 762629c91e99STejun Heo struct workqueue_attrs *attrs; 762729c91e99STejun Heo 7628be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 762929c91e99STejun Heo attrs->nice = std_nice[i]; 763029c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 76318a2b7538STejun Heo 76328a2b7538STejun Heo /* 76338a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 76348a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 76358a2b7538STejun Heo */ 7636be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 76378a2b7538STejun Heo attrs->nice = std_nice[i]; 7638af73f5c9STejun Heo attrs->ordered = true; 76398a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 764029c91e99STejun Heo } 764129c91e99STejun Heo 7642d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 76431aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7644d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7645f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7646636b927eSTejun Heo WQ_MAX_ACTIVE); 764724d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 764824d51addSTejun Heo WQ_FREEZABLE, 0); 76490668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 76500668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 76518318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 76520668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 76530668106cSViresh Kumar 0); 76544cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 76554cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 76564cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 76571aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 76580668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 76590668106cSViresh Kumar !system_power_efficient_wq || 76604cb1ef64STejun Heo !system_freezable_power_efficient_wq || 76614cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 76623347fa09STejun Heo } 76633347fa09STejun Heo 7664aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7665aa6fde93STejun Heo { 7666aa6fde93STejun Heo unsigned long thresh; 7667aa6fde93STejun Heo unsigned long bogo; 7668aa6fde93STejun Heo 7669dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7670dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7671dd64c873SZqiang 7672aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7673aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7674aa6fde93STejun Heo return; 7675aa6fde93STejun Heo 7676aa6fde93STejun Heo /* 7677aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7678aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7679aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7680aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7681aa6fde93STejun Heo * too low. 7682aa6fde93STejun Heo * 7683aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7684aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7685aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7686aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7687aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7688aa6fde93STejun Heo * usefulness. 7689aa6fde93STejun Heo */ 7690aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7691aa6fde93STejun Heo 7692aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7693aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7694aa6fde93STejun Heo if (bogo < 4000) 7695aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7696aa6fde93STejun Heo 7697aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7698aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7699aa6fde93STejun Heo 7700aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7701aa6fde93STejun Heo } 7702aa6fde93STejun Heo 77033347fa09STejun Heo /** 77043347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 77053347fa09STejun Heo * 77062930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 77072930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 77082930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 77092930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 77102930155bSTejun Heo * workers and enable future kworker creations. 77113347fa09STejun Heo */ 77122333e829SYu Chen void __init workqueue_init(void) 77133347fa09STejun Heo { 77142186d9f9STejun Heo struct workqueue_struct *wq; 77153347fa09STejun Heo struct worker_pool *pool; 77163347fa09STejun Heo int cpu, bkt; 77173347fa09STejun Heo 7718aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7719aa6fde93STejun Heo 77202186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 77212186d9f9STejun Heo 77222930155bSTejun Heo /* 77232930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 77242930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 77252930155bSTejun Heo */ 77262186d9f9STejun Heo for_each_possible_cpu(cpu) { 77274cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 77282186d9f9STejun Heo pool->node = cpu_to_node(cpu); 77294cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 77304cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 77312186d9f9STejun Heo } 77322186d9f9STejun Heo 773340c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 773440c17f75STejun Heo WARN(init_rescuer(wq), 773540c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 773640c17f75STejun Heo wq->name); 773740c17f75STejun Heo } 77382186d9f9STejun Heo 77392186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 77402186d9f9STejun Heo 77414cb1ef64STejun Heo /* 77424cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 77434cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 77444cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 77454cb1ef64STejun Heo * possible CPUs here. 77464cb1ef64STejun Heo */ 77474cb1ef64STejun Heo for_each_possible_cpu(cpu) 77484cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 77494cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 77504cb1ef64STejun Heo 77513347fa09STejun Heo for_each_online_cpu(cpu) { 77523347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 77533347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 77543347fa09STejun Heo BUG_ON(!create_worker(pool)); 77553347fa09STejun Heo } 77563347fa09STejun Heo } 77573347fa09STejun Heo 77583347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 77593347fa09STejun Heo BUG_ON(!create_worker(pool)); 77603347fa09STejun Heo 77613347fa09STejun Heo wq_online = true; 776282607adcSTejun Heo wq_watchdog_init(); 77631da177e4SLinus Torvalds } 7764c4f135d6STetsuo Handa 7765025e1684STejun Heo /* 7766025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7767025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7768025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7769025e1684STejun Heo */ 7770025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7771025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7772025e1684STejun Heo { 7773025e1684STejun Heo int cur, pre, cpu, pod; 7774025e1684STejun Heo 7775025e1684STejun Heo pt->nr_pods = 0; 7776025e1684STejun Heo 7777025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7778025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7779025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7780025e1684STejun Heo 7781025e1684STejun Heo for_each_possible_cpu(cur) { 7782025e1684STejun Heo for_each_possible_cpu(pre) { 7783025e1684STejun Heo if (pre >= cur) { 7784025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7785025e1684STejun Heo break; 7786025e1684STejun Heo } 7787025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7788025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7789025e1684STejun Heo break; 7790025e1684STejun Heo } 7791025e1684STejun Heo } 7792025e1684STejun Heo } 7793025e1684STejun Heo 7794025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7795025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7796025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7797025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7798025e1684STejun Heo 7799025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7800025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7801025e1684STejun Heo 7802025e1684STejun Heo for_each_possible_cpu(cpu) { 7803025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7804025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7805025e1684STejun Heo } 7806025e1684STejun Heo } 7807025e1684STejun Heo 780863c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 780963c5484eSTejun Heo { 781063c5484eSTejun Heo return false; 781163c5484eSTejun Heo } 781263c5484eSTejun Heo 781363c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 781463c5484eSTejun Heo { 781563c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 781663c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 781763c5484eSTejun Heo #else 781863c5484eSTejun Heo return false; 781963c5484eSTejun Heo #endif 782063c5484eSTejun Heo } 782163c5484eSTejun Heo 7822025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7823025e1684STejun Heo { 7824025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7825025e1684STejun Heo } 7826025e1684STejun Heo 78272930155bSTejun Heo /** 78282930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 78292930155bSTejun Heo * 783096068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 78312930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 78322930155bSTejun Heo * initializes the unbound CPU pods accordingly. 78332930155bSTejun Heo */ 78342930155bSTejun Heo void __init workqueue_init_topology(void) 7835a86feae6STejun Heo { 78362930155bSTejun Heo struct workqueue_struct *wq; 7837025e1684STejun Heo int cpu; 7838a86feae6STejun Heo 783963c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 784063c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 784163c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7842025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7843a86feae6STejun Heo 7844c5f8cd6cSTejun Heo wq_topo_initialized = true; 7845c5f8cd6cSTejun Heo 78462930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7847a86feae6STejun Heo 7848a86feae6STejun Heo /* 78492930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 78502930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 78512930155bSTejun Heo * combinations to apply per-pod sharing. 78522930155bSTejun Heo */ 78532930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 78545797b1c1STejun Heo for_each_online_cpu(cpu) 78552930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 78565797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 78575797b1c1STejun Heo mutex_lock(&wq->mutex); 78585797b1c1STejun Heo wq_update_node_max_active(wq, -1); 78595797b1c1STejun Heo mutex_unlock(&wq->mutex); 78602930155bSTejun Heo } 78612930155bSTejun Heo } 78622930155bSTejun Heo 78632930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7864a86feae6STejun Heo } 7865a86feae6STejun Heo 786620bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 786720bdedafSTetsuo Handa { 786820bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 786920bdedafSTetsuo Handa dump_stack(); 787020bdedafSTetsuo Handa } 7871c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7872ace3c549Stiozhang 7873ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7874ace3c549Stiozhang { 7875ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7876ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7877ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7878ace3c549Stiozhang } 7879ace3c549Stiozhang 7880ace3c549Stiozhang return 1; 7881ace3c549Stiozhang } 7882ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7883