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); 128057a01eafSSven Schnelle int wake_cpu = cpumask_any_and_distribute(pool->attrs->__pod_cpumask, 128157a01eafSSven Schnelle cpu_online_mask); 128257a01eafSSven Schnelle if (wake_cpu < nr_cpu_ids) { 128357a01eafSSven Schnelle p->wake_cpu = wake_cpu; 12848639ecebSTejun Heo get_work_pwq(work)->stats[PWQ_STAT_REPATRIATED]++; 12858639ecebSTejun Heo } 128657a01eafSSven 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 1601*91f09870SLai Jiangshan /* If all CPUs of the wq get offline, use the default values */ 1602*91f09870SLai Jiangshan if (unlikely(!total_cpus)) { 1603*91f09870SLai Jiangshan for_each_node(node) 1604*91f09870SLai Jiangshan wq_node_nr_active(wq, node)->max = min_active; 1605*91f09870SLai Jiangshan 1606*91f09870SLai Jiangshan wq_node_nr_active(wq, NUMA_NO_NODE)->max = max_active; 1607*91f09870SLai Jiangshan return; 1608*91f09870SLai Jiangshan } 1609*91f09870SLai Jiangshan 16105797b1c1STejun Heo for_each_node(node) { 16115797b1c1STejun Heo int node_cpus; 16125797b1c1STejun Heo 16135797b1c1STejun Heo node_cpus = cpumask_weight_and(effective, cpumask_of_node(node)); 16145797b1c1STejun Heo if (off_cpu >= 0 && cpu_to_node(off_cpu) == node) 16155797b1c1STejun Heo node_cpus--; 16165797b1c1STejun Heo 16175797b1c1STejun Heo wq_node_nr_active(wq, node)->max = 16185797b1c1STejun Heo clamp(DIV_ROUND_UP(max_active * node_cpus, total_cpus), 16195797b1c1STejun Heo min_active, max_active); 16205797b1c1STejun Heo } 16215797b1c1STejun Heo 1622d40f9202STejun Heo wq_node_nr_active(wq, NUMA_NO_NODE)->max = max_active; 16235797b1c1STejun Heo } 16245797b1c1STejun Heo 16255797b1c1STejun Heo /** 16268864b4e5STejun Heo * get_pwq - get an extra reference on the specified pool_workqueue 16278864b4e5STejun Heo * @pwq: pool_workqueue to get 16288864b4e5STejun Heo * 16298864b4e5STejun Heo * Obtain an extra reference on @pwq. The caller should guarantee that 16308864b4e5STejun Heo * @pwq has positive refcnt and be holding the matching pool->lock. 16318864b4e5STejun Heo */ 16328864b4e5STejun Heo static void get_pwq(struct pool_workqueue *pwq) 16338864b4e5STejun Heo { 16348864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16358864b4e5STejun Heo WARN_ON_ONCE(pwq->refcnt <= 0); 16368864b4e5STejun Heo pwq->refcnt++; 16378864b4e5STejun Heo } 16388864b4e5STejun Heo 16398864b4e5STejun Heo /** 16408864b4e5STejun Heo * put_pwq - put a pool_workqueue reference 16418864b4e5STejun Heo * @pwq: pool_workqueue to put 16428864b4e5STejun Heo * 16438864b4e5STejun Heo * Drop a reference of @pwq. If its refcnt reaches zero, schedule its 16448864b4e5STejun Heo * destruction. The caller should be holding the matching pool->lock. 16458864b4e5STejun Heo */ 16468864b4e5STejun Heo static void put_pwq(struct pool_workqueue *pwq) 16478864b4e5STejun Heo { 16488864b4e5STejun Heo lockdep_assert_held(&pwq->pool->lock); 16498864b4e5STejun Heo if (likely(--pwq->refcnt)) 16508864b4e5STejun Heo return; 16518864b4e5STejun Heo /* 1652967b494eSTejun Heo * @pwq can't be released under pool->lock, bounce to a dedicated 1653967b494eSTejun Heo * kthread_worker to avoid A-A deadlocks. 16548864b4e5STejun Heo */ 1655687a9aa5STejun Heo kthread_queue_work(pwq_release_worker, &pwq->release_work); 16568864b4e5STejun Heo } 16578864b4e5STejun Heo 1658dce90d47STejun Heo /** 1659dce90d47STejun Heo * put_pwq_unlocked - put_pwq() with surrounding pool lock/unlock 1660dce90d47STejun Heo * @pwq: pool_workqueue to put (can be %NULL) 1661dce90d47STejun Heo * 1662dce90d47STejun Heo * put_pwq() with locking. This function also allows %NULL @pwq. 1663dce90d47STejun Heo */ 1664dce90d47STejun Heo static void put_pwq_unlocked(struct pool_workqueue *pwq) 1665dce90d47STejun Heo { 1666dce90d47STejun Heo if (pwq) { 1667dce90d47STejun Heo /* 166824acfb71SThomas Gleixner * As both pwqs and pools are RCU protected, the 1669dce90d47STejun Heo * following lock operations are safe. 1670dce90d47STejun Heo */ 1671a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 1672dce90d47STejun Heo put_pwq(pwq); 1673a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 1674dce90d47STejun Heo } 1675dce90d47STejun Heo } 1676dce90d47STejun Heo 1677afa87ce8STejun Heo static bool pwq_is_empty(struct pool_workqueue *pwq) 1678afa87ce8STejun Heo { 1679afa87ce8STejun Heo return !pwq->nr_active && list_empty(&pwq->inactive_works); 1680afa87ce8STejun Heo } 1681afa87ce8STejun Heo 16824c638030STejun Heo static void __pwq_activate_work(struct pool_workqueue *pwq, 16834c638030STejun Heo struct work_struct *work) 1684bf4ede01STejun Heo { 16851c270b79STejun Heo unsigned long *wdb = work_data_bits(work); 16861c270b79STejun Heo 16871c270b79STejun Heo WARN_ON_ONCE(!(*wdb & WORK_STRUCT_INACTIVE)); 1688bf4ede01STejun Heo trace_workqueue_activate_work(work); 168982607adcSTejun Heo if (list_empty(&pwq->pool->worklist)) 169082607adcSTejun Heo pwq->pool->watchdog_ts = jiffies; 1691112202d9STejun Heo move_linked_works(work, &pwq->pool->worklist, NULL); 16921c270b79STejun Heo __clear_bit(WORK_STRUCT_INACTIVE_BIT, wdb); 16934c638030STejun Heo } 16944c638030STejun Heo 16954c638030STejun Heo /** 16964c638030STejun Heo * pwq_activate_work - Activate a work item if inactive 16974c638030STejun Heo * @pwq: pool_workqueue @work belongs to 16984c638030STejun Heo * @work: work item to activate 16994c638030STejun Heo * 17004c638030STejun Heo * Returns %true if activated. %false if already active. 17014c638030STejun Heo */ 17024c638030STejun Heo static bool pwq_activate_work(struct pool_workqueue *pwq, 17034c638030STejun Heo struct work_struct *work) 17044c638030STejun Heo { 17054c638030STejun Heo struct worker_pool *pool = pwq->pool; 170691ccc6e7STejun Heo struct wq_node_nr_active *nna; 17074c638030STejun Heo 17084c638030STejun Heo lockdep_assert_held(&pool->lock); 17094c638030STejun Heo 17104c638030STejun Heo if (!(*work_data_bits(work) & WORK_STRUCT_INACTIVE)) 17114c638030STejun Heo return false; 17124c638030STejun Heo 171391ccc6e7STejun Heo nna = wq_node_nr_active(pwq->wq, pool->node); 171491ccc6e7STejun Heo if (nna) 171591ccc6e7STejun Heo atomic_inc(&nna->nr); 171691ccc6e7STejun Heo 1717112202d9STejun Heo pwq->nr_active++; 17184c638030STejun Heo __pwq_activate_work(pwq, work); 17194c638030STejun Heo return true; 1720bf4ede01STejun Heo } 1721bf4ede01STejun Heo 17225797b1c1STejun Heo static bool tryinc_node_nr_active(struct wq_node_nr_active *nna) 17235797b1c1STejun Heo { 17245797b1c1STejun Heo int max = READ_ONCE(nna->max); 17255797b1c1STejun Heo 17265797b1c1STejun Heo while (true) { 17275797b1c1STejun Heo int old, tmp; 17285797b1c1STejun Heo 17295797b1c1STejun Heo old = atomic_read(&nna->nr); 17305797b1c1STejun Heo if (old >= max) 17315797b1c1STejun Heo return false; 17325797b1c1STejun Heo tmp = atomic_cmpxchg_relaxed(&nna->nr, old, old + 1); 17335797b1c1STejun Heo if (tmp == old) 17345797b1c1STejun Heo return true; 17355797b1c1STejun Heo } 17365797b1c1STejun Heo } 17375797b1c1STejun Heo 17381c270b79STejun Heo /** 17391c270b79STejun Heo * pwq_tryinc_nr_active - Try to increment nr_active for a pwq 17401c270b79STejun Heo * @pwq: pool_workqueue of interest 17415797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 17421c270b79STejun Heo * 17431c270b79STejun Heo * Try to increment nr_active for @pwq. Returns %true if an nr_active count is 17441c270b79STejun Heo * successfully obtained. %false otherwise. 17451c270b79STejun Heo */ 17465797b1c1STejun Heo static bool pwq_tryinc_nr_active(struct pool_workqueue *pwq, bool fill) 17473aa62497SLai Jiangshan { 17481c270b79STejun Heo struct workqueue_struct *wq = pwq->wq; 17491c270b79STejun Heo struct worker_pool *pool = pwq->pool; 175091ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(wq, pool->node); 17515797b1c1STejun Heo bool obtained = false; 17521c270b79STejun Heo 17531c270b79STejun Heo lockdep_assert_held(&pool->lock); 17541c270b79STejun Heo 17555797b1c1STejun Heo if (!nna) { 17564cb1ef64STejun Heo /* BH or per-cpu workqueue, pwq->nr_active is sufficient */ 17571c270b79STejun Heo obtained = pwq->nr_active < READ_ONCE(wq->max_active); 17585797b1c1STejun Heo goto out; 175991ccc6e7STejun Heo } 17605797b1c1STejun Heo 17614c065dbcSWaiman Long if (unlikely(pwq->plugged)) 17624c065dbcSWaiman Long return false; 17634c065dbcSWaiman Long 17645797b1c1STejun Heo /* 17655797b1c1STejun Heo * Unbound workqueue uses per-node shared nr_active $nna. If @pwq is 17665797b1c1STejun Heo * already waiting on $nna, pwq_dec_nr_active() will maintain the 17675797b1c1STejun Heo * concurrency level. Don't jump the line. 17685797b1c1STejun Heo * 17695797b1c1STejun Heo * We need to ignore the pending test after max_active has increased as 17705797b1c1STejun Heo * pwq_dec_nr_active() can only maintain the concurrency level but not 17715797b1c1STejun Heo * increase it. This is indicated by @fill. 17725797b1c1STejun Heo */ 17735797b1c1STejun Heo if (!list_empty(&pwq->pending_node) && likely(!fill)) 17745797b1c1STejun Heo goto out; 17755797b1c1STejun Heo 17765797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17775797b1c1STejun Heo if (obtained) 17785797b1c1STejun Heo goto out; 17795797b1c1STejun Heo 17805797b1c1STejun Heo /* 17815797b1c1STejun Heo * Lockless acquisition failed. Lock, add ourself to $nna->pending_pwqs 17825797b1c1STejun Heo * and try again. The smp_mb() is paired with the implied memory barrier 17835797b1c1STejun Heo * of atomic_dec_return() in pwq_dec_nr_active() to ensure that either 17845797b1c1STejun Heo * we see the decremented $nna->nr or they see non-empty 17855797b1c1STejun Heo * $nna->pending_pwqs. 17865797b1c1STejun Heo */ 17875797b1c1STejun Heo raw_spin_lock(&nna->lock); 17885797b1c1STejun Heo 17895797b1c1STejun Heo if (list_empty(&pwq->pending_node)) 17905797b1c1STejun Heo list_add_tail(&pwq->pending_node, &nna->pending_pwqs); 17915797b1c1STejun Heo else if (likely(!fill)) 17925797b1c1STejun Heo goto out_unlock; 17935797b1c1STejun Heo 17945797b1c1STejun Heo smp_mb(); 17955797b1c1STejun Heo 17965797b1c1STejun Heo obtained = tryinc_node_nr_active(nna); 17975797b1c1STejun Heo 17985797b1c1STejun Heo /* 17995797b1c1STejun Heo * If @fill, @pwq might have already been pending. Being spuriously 18005797b1c1STejun Heo * pending in cold paths doesn't affect anything. Let's leave it be. 18015797b1c1STejun Heo */ 18025797b1c1STejun Heo if (obtained && likely(!fill)) 18035797b1c1STejun Heo list_del_init(&pwq->pending_node); 18045797b1c1STejun Heo 18055797b1c1STejun Heo out_unlock: 18065797b1c1STejun Heo raw_spin_unlock(&nna->lock); 18075797b1c1STejun Heo out: 18085797b1c1STejun Heo if (obtained) 18095797b1c1STejun Heo pwq->nr_active++; 18101c270b79STejun Heo return obtained; 18111c270b79STejun Heo } 18121c270b79STejun Heo 18131c270b79STejun Heo /** 18141c270b79STejun Heo * pwq_activate_first_inactive - Activate the first inactive work item on a pwq 18151c270b79STejun Heo * @pwq: pool_workqueue of interest 18165797b1c1STejun Heo * @fill: max_active may have increased, try to increase concurrency level 18171c270b79STejun Heo * 18181c270b79STejun Heo * Activate the first inactive work item of @pwq if available and allowed by 18191c270b79STejun Heo * max_active limit. 18201c270b79STejun Heo * 18211c270b79STejun Heo * Returns %true if an inactive work item has been activated. %false if no 18221c270b79STejun Heo * inactive work item is found or max_active limit is reached. 18231c270b79STejun Heo */ 18245797b1c1STejun Heo static bool pwq_activate_first_inactive(struct pool_workqueue *pwq, bool fill) 18251c270b79STejun Heo { 18261c270b79STejun Heo struct work_struct *work = 18271c270b79STejun Heo list_first_entry_or_null(&pwq->inactive_works, 18283aa62497SLai Jiangshan struct work_struct, entry); 18293aa62497SLai Jiangshan 18305797b1c1STejun Heo if (work && pwq_tryinc_nr_active(pwq, fill)) { 18311c270b79STejun Heo __pwq_activate_work(pwq, work); 18321c270b79STejun Heo return true; 18331c270b79STejun Heo } else { 18341c270b79STejun Heo return false; 18351c270b79STejun Heo } 18361c270b79STejun Heo } 18371c270b79STejun Heo 18381c270b79STejun Heo /** 1839516d3dc9SWaiman Long * unplug_oldest_pwq - unplug the oldest pool_workqueue 1840516d3dc9SWaiman Long * @wq: workqueue_struct where its oldest pwq is to be unplugged 18414c065dbcSWaiman Long * 1842516d3dc9SWaiman Long * This function should only be called for ordered workqueues where only the 1843516d3dc9SWaiman Long * oldest pwq is unplugged, the others are plugged to suspend execution to 1844516d3dc9SWaiman Long * ensure proper work item ordering:: 18454c065dbcSWaiman Long * 18464c065dbcSWaiman Long * dfl_pwq --------------+ [P] - plugged 18474c065dbcSWaiman Long * | 18484c065dbcSWaiman Long * v 18494c065dbcSWaiman Long * pwqs -> A -> B [P] -> C [P] (newest) 18504c065dbcSWaiman Long * | | | 18514c065dbcSWaiman Long * 1 3 5 18524c065dbcSWaiman Long * | | | 18534c065dbcSWaiman Long * 2 4 6 1854516d3dc9SWaiman Long * 1855516d3dc9SWaiman Long * When the oldest pwq is drained and removed, this function should be called 1856516d3dc9SWaiman Long * to unplug the next oldest one to start its work item execution. Note that 1857516d3dc9SWaiman Long * pwq's are linked into wq->pwqs with the oldest first, so the first one in 1858516d3dc9SWaiman Long * the list is the oldest. 18594c065dbcSWaiman Long */ 18604c065dbcSWaiman Long static void unplug_oldest_pwq(struct workqueue_struct *wq) 18614c065dbcSWaiman Long { 18624c065dbcSWaiman Long struct pool_workqueue *pwq; 18634c065dbcSWaiman Long 18644c065dbcSWaiman Long lockdep_assert_held(&wq->mutex); 18654c065dbcSWaiman Long 18664c065dbcSWaiman Long /* Caller should make sure that pwqs isn't empty before calling */ 18674c065dbcSWaiman Long pwq = list_first_entry_or_null(&wq->pwqs, struct pool_workqueue, 18684c065dbcSWaiman Long pwqs_node); 18694c065dbcSWaiman Long raw_spin_lock_irq(&pwq->pool->lock); 18704c065dbcSWaiman Long if (pwq->plugged) { 18714c065dbcSWaiman Long pwq->plugged = false; 18724c065dbcSWaiman Long if (pwq_activate_first_inactive(pwq, true)) 18734c065dbcSWaiman Long kick_pool(pwq->pool); 18744c065dbcSWaiman Long } 18754c065dbcSWaiman Long raw_spin_unlock_irq(&pwq->pool->lock); 18764c065dbcSWaiman Long } 18774c065dbcSWaiman Long 18784c065dbcSWaiman Long /** 18795797b1c1STejun Heo * node_activate_pending_pwq - Activate a pending pwq on a wq_node_nr_active 18805797b1c1STejun Heo * @nna: wq_node_nr_active to activate a pending pwq for 18815797b1c1STejun Heo * @caller_pool: worker_pool the caller is locking 18825797b1c1STejun Heo * 18835797b1c1STejun Heo * Activate a pwq in @nna->pending_pwqs. Called with @caller_pool locked. 18845797b1c1STejun Heo * @caller_pool may be unlocked and relocked to lock other worker_pools. 18855797b1c1STejun Heo */ 18865797b1c1STejun Heo static void node_activate_pending_pwq(struct wq_node_nr_active *nna, 18875797b1c1STejun Heo struct worker_pool *caller_pool) 18885797b1c1STejun Heo { 18895797b1c1STejun Heo struct worker_pool *locked_pool = caller_pool; 18905797b1c1STejun Heo struct pool_workqueue *pwq; 18915797b1c1STejun Heo struct work_struct *work; 18925797b1c1STejun Heo 18935797b1c1STejun Heo lockdep_assert_held(&caller_pool->lock); 18945797b1c1STejun Heo 18955797b1c1STejun Heo raw_spin_lock(&nna->lock); 18965797b1c1STejun Heo retry: 18975797b1c1STejun Heo pwq = list_first_entry_or_null(&nna->pending_pwqs, 18985797b1c1STejun Heo struct pool_workqueue, pending_node); 18995797b1c1STejun Heo if (!pwq) 19005797b1c1STejun Heo goto out_unlock; 19015797b1c1STejun Heo 19025797b1c1STejun Heo /* 19035797b1c1STejun Heo * If @pwq is for a different pool than @locked_pool, we need to lock 19045797b1c1STejun Heo * @pwq->pool->lock. Let's trylock first. If unsuccessful, do the unlock 19055797b1c1STejun Heo * / lock dance. For that, we also need to release @nna->lock as it's 19065797b1c1STejun Heo * nested inside pool locks. 19075797b1c1STejun Heo */ 19085797b1c1STejun Heo if (pwq->pool != locked_pool) { 19095797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19105797b1c1STejun Heo locked_pool = pwq->pool; 19115797b1c1STejun Heo if (!raw_spin_trylock(&locked_pool->lock)) { 19125797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19135797b1c1STejun Heo raw_spin_lock(&locked_pool->lock); 19145797b1c1STejun Heo raw_spin_lock(&nna->lock); 19155797b1c1STejun Heo goto retry; 19165797b1c1STejun Heo } 19175797b1c1STejun Heo } 19185797b1c1STejun Heo 19195797b1c1STejun Heo /* 19205797b1c1STejun Heo * $pwq may not have any inactive work items due to e.g. cancellations. 19215797b1c1STejun Heo * Drop it from pending_pwqs and see if there's another one. 19225797b1c1STejun Heo */ 19235797b1c1STejun Heo work = list_first_entry_or_null(&pwq->inactive_works, 19245797b1c1STejun Heo struct work_struct, entry); 19255797b1c1STejun Heo if (!work) { 19265797b1c1STejun Heo list_del_init(&pwq->pending_node); 19275797b1c1STejun Heo goto retry; 19285797b1c1STejun Heo } 19295797b1c1STejun Heo 19305797b1c1STejun Heo /* 19315797b1c1STejun Heo * Acquire an nr_active count and activate the inactive work item. If 19325797b1c1STejun Heo * $pwq still has inactive work items, rotate it to the end of the 19335797b1c1STejun Heo * pending_pwqs so that we round-robin through them. This means that 19345797b1c1STejun Heo * inactive work items are not activated in queueing order which is fine 19355797b1c1STejun Heo * given that there has never been any ordering across different pwqs. 19365797b1c1STejun Heo */ 19375797b1c1STejun Heo if (likely(tryinc_node_nr_active(nna))) { 19385797b1c1STejun Heo pwq->nr_active++; 19395797b1c1STejun Heo __pwq_activate_work(pwq, work); 19405797b1c1STejun Heo 19415797b1c1STejun Heo if (list_empty(&pwq->inactive_works)) 19425797b1c1STejun Heo list_del_init(&pwq->pending_node); 19435797b1c1STejun Heo else 19445797b1c1STejun Heo list_move_tail(&pwq->pending_node, &nna->pending_pwqs); 19455797b1c1STejun Heo 19465797b1c1STejun Heo /* if activating a foreign pool, make sure it's running */ 19475797b1c1STejun Heo if (pwq->pool != caller_pool) 19485797b1c1STejun Heo kick_pool(pwq->pool); 19495797b1c1STejun Heo } 19505797b1c1STejun Heo 19515797b1c1STejun Heo out_unlock: 19525797b1c1STejun Heo raw_spin_unlock(&nna->lock); 19535797b1c1STejun Heo if (locked_pool != caller_pool) { 19545797b1c1STejun Heo raw_spin_unlock(&locked_pool->lock); 19555797b1c1STejun Heo raw_spin_lock(&caller_pool->lock); 19565797b1c1STejun Heo } 19575797b1c1STejun Heo } 19585797b1c1STejun Heo 19595797b1c1STejun Heo /** 19601c270b79STejun Heo * pwq_dec_nr_active - Retire an active count 19611c270b79STejun Heo * @pwq: pool_workqueue of interest 19621c270b79STejun Heo * 19631c270b79STejun Heo * Decrement @pwq's nr_active and try to activate the first inactive work item. 19645797b1c1STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock. 19651c270b79STejun Heo */ 19661c270b79STejun Heo static void pwq_dec_nr_active(struct pool_workqueue *pwq) 19671c270b79STejun Heo { 19681c270b79STejun Heo struct worker_pool *pool = pwq->pool; 196991ccc6e7STejun Heo struct wq_node_nr_active *nna = wq_node_nr_active(pwq->wq, pool->node); 19701c270b79STejun Heo 19711c270b79STejun Heo lockdep_assert_held(&pool->lock); 19721c270b79STejun Heo 197391ccc6e7STejun Heo /* 197491ccc6e7STejun Heo * @pwq->nr_active should be decremented for both percpu and unbound 197591ccc6e7STejun Heo * workqueues. 197691ccc6e7STejun Heo */ 19771c270b79STejun Heo pwq->nr_active--; 197891ccc6e7STejun Heo 197991ccc6e7STejun Heo /* 198091ccc6e7STejun Heo * For a percpu workqueue, it's simple. Just need to kick the first 198191ccc6e7STejun Heo * inactive work item on @pwq itself. 198291ccc6e7STejun Heo */ 198391ccc6e7STejun Heo if (!nna) { 19845797b1c1STejun Heo pwq_activate_first_inactive(pwq, false); 198591ccc6e7STejun Heo return; 198691ccc6e7STejun Heo } 198791ccc6e7STejun Heo 19885797b1c1STejun Heo /* 19895797b1c1STejun Heo * If @pwq is for an unbound workqueue, it's more complicated because 19905797b1c1STejun Heo * multiple pwqs and pools may be sharing the nr_active count. When a 19915797b1c1STejun Heo * pwq needs to wait for an nr_active count, it puts itself on 19925797b1c1STejun Heo * $nna->pending_pwqs. The following atomic_dec_return()'s implied 19935797b1c1STejun Heo * memory barrier is paired with smp_mb() in pwq_tryinc_nr_active() to 19945797b1c1STejun Heo * guarantee that either we see non-empty pending_pwqs or they see 19955797b1c1STejun Heo * decremented $nna->nr. 19965797b1c1STejun Heo * 19975797b1c1STejun Heo * $nna->max may change as CPUs come online/offline and @pwq->wq's 19985797b1c1STejun Heo * max_active gets updated. However, it is guaranteed to be equal to or 19995797b1c1STejun Heo * larger than @pwq->wq->min_active which is above zero unless freezing. 20005797b1c1STejun Heo * This maintains the forward progress guarantee. 20015797b1c1STejun Heo */ 20025797b1c1STejun Heo if (atomic_dec_return(&nna->nr) >= READ_ONCE(nna->max)) 20035797b1c1STejun Heo return; 20045797b1c1STejun Heo 20055797b1c1STejun Heo if (!list_empty(&nna->pending_pwqs)) 20065797b1c1STejun Heo node_activate_pending_pwq(nna, pool); 20073aa62497SLai Jiangshan } 20083aa62497SLai Jiangshan 2009bf4ede01STejun Heo /** 2010112202d9STejun Heo * pwq_dec_nr_in_flight - decrement pwq's nr_in_flight 2011112202d9STejun Heo * @pwq: pwq of interest 2012c4560c2cSLai Jiangshan * @work_data: work_data of work which left the queue 2013bf4ede01STejun Heo * 2014bf4ede01STejun Heo * A work either has completed or is removed from pending queue, 2015112202d9STejun Heo * decrement nr_in_flight of its pwq and handle workqueue flushing. 2016bf4ede01STejun Heo * 2017dd6c3c54STejun Heo * NOTE: 2018dd6c3c54STejun Heo * For unbound workqueues, this function may temporarily drop @pwq->pool->lock 2019dd6c3c54STejun Heo * and thus should be called after all other state updates for the in-flight 2020dd6c3c54STejun Heo * work item is complete. 2021dd6c3c54STejun Heo * 2022bf4ede01STejun Heo * CONTEXT: 2023a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2024bf4ede01STejun Heo */ 2025c4560c2cSLai Jiangshan static void pwq_dec_nr_in_flight(struct pool_workqueue *pwq, unsigned long work_data) 2026bf4ede01STejun Heo { 2027c4560c2cSLai Jiangshan int color = get_work_color(work_data); 2028c4560c2cSLai Jiangshan 20291c270b79STejun Heo if (!(work_data & WORK_STRUCT_INACTIVE)) 20301c270b79STejun Heo pwq_dec_nr_active(pwq); 2031018f3a13SLai Jiangshan 2032018f3a13SLai Jiangshan pwq->nr_in_flight[color]--; 2033bf4ede01STejun Heo 2034bf4ede01STejun Heo /* is flush in progress and are we at the flushing tip? */ 2035112202d9STejun Heo if (likely(pwq->flush_color != color)) 20368864b4e5STejun Heo goto out_put; 2037bf4ede01STejun Heo 2038bf4ede01STejun Heo /* are there still in-flight works? */ 2039112202d9STejun Heo if (pwq->nr_in_flight[color]) 20408864b4e5STejun Heo goto out_put; 2041bf4ede01STejun Heo 2042112202d9STejun Heo /* this pwq is done, clear flush_color */ 2043112202d9STejun Heo pwq->flush_color = -1; 2044bf4ede01STejun Heo 2045bf4ede01STejun Heo /* 2046112202d9STejun Heo * If this was the last pwq, wake up the first flusher. It 2047bf4ede01STejun Heo * will handle the rest. 2048bf4ede01STejun Heo */ 2049112202d9STejun Heo if (atomic_dec_and_test(&pwq->wq->nr_pwqs_to_flush)) 2050112202d9STejun Heo complete(&pwq->wq->first_flusher->done); 20518864b4e5STejun Heo out_put: 20528864b4e5STejun Heo put_pwq(pwq); 2053bf4ede01STejun Heo } 2054bf4ede01STejun Heo 205536e227d2STejun Heo /** 2056bbb68dfaSTejun Heo * try_to_grab_pending - steal work item from worklist and disable irq 205736e227d2STejun Heo * @work: work item to steal 2058c5f5b942STejun Heo * @cflags: %WORK_CANCEL_ flags 2059c26e2f2eSTejun Heo * @irq_flags: place to store irq state 206036e227d2STejun Heo * 206136e227d2STejun Heo * Try to grab PENDING bit of @work. This function can handle @work in any 2062d185af30SYacine Belkadi * stable state - idle, on timer or on worklist. 206336e227d2STejun Heo * 2064d185af30SYacine Belkadi * Return: 20653eb6b31bSMauro Carvalho Chehab * 20663eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 206736e227d2STejun Heo * 1 if @work was pending and we successfully stole PENDING 206836e227d2STejun Heo * 0 if @work was idle and we claimed PENDING 206936e227d2STejun Heo * -EAGAIN if PENDING couldn't be grabbed at the moment, safe to busy-retry 2070bbb68dfaSTejun Heo * -ENOENT if someone else is canceling @work, this state may persist 2071bbb68dfaSTejun Heo * for arbitrarily long 20723eb6b31bSMauro Carvalho Chehab * ======== ================================================================ 207336e227d2STejun Heo * 2074d185af30SYacine Belkadi * Note: 2075bbb68dfaSTejun Heo * On >= 0 return, the caller owns @work's PENDING bit. To avoid getting 2076e0aecdd8STejun Heo * interrupted while holding PENDING and @work off queue, irq must be 2077e0aecdd8STejun Heo * disabled on entry. This, combined with delayed_work->timer being 2078e0aecdd8STejun Heo * irqsafe, ensures that we return -EAGAIN for finite short period of time. 2079bbb68dfaSTejun Heo * 2080bbb68dfaSTejun Heo * On successful return, >= 0, irq is disabled and the caller is 2081c26e2f2eSTejun Heo * responsible for releasing it using local_irq_restore(*@irq_flags). 2082bbb68dfaSTejun Heo * 2083e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 2084bf4ede01STejun Heo */ 2085c5f5b942STejun Heo static int try_to_grab_pending(struct work_struct *work, u32 cflags, 2086c26e2f2eSTejun Heo unsigned long *irq_flags) 2087bf4ede01STejun Heo { 2088d565ed63STejun Heo struct worker_pool *pool; 2089112202d9STejun Heo struct pool_workqueue *pwq; 2090bf4ede01STejun Heo 2091c26e2f2eSTejun Heo local_irq_save(*irq_flags); 2092bbb68dfaSTejun Heo 209336e227d2STejun Heo /* try to steal the timer if it exists */ 2094c5f5b942STejun Heo if (cflags & WORK_CANCEL_DELAYED) { 209536e227d2STejun Heo struct delayed_work *dwork = to_delayed_work(work); 209636e227d2STejun Heo 2097e0aecdd8STejun Heo /* 2098e0aecdd8STejun Heo * dwork->timer is irqsafe. If del_timer() fails, it's 2099e0aecdd8STejun Heo * guaranteed that the timer is not queued anywhere and not 2100e0aecdd8STejun Heo * running on the local CPU. 2101e0aecdd8STejun Heo */ 210236e227d2STejun Heo if (likely(del_timer(&dwork->timer))) 210336e227d2STejun Heo return 1; 210436e227d2STejun Heo } 210536e227d2STejun Heo 210636e227d2STejun Heo /* try to claim PENDING the normal way */ 2107bf4ede01STejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) 2108bf4ede01STejun Heo return 0; 2109bf4ede01STejun Heo 211024acfb71SThomas Gleixner rcu_read_lock(); 2111bf4ede01STejun Heo /* 2112bf4ede01STejun Heo * The queueing is in progress, or it is already queued. Try to 2113bf4ede01STejun Heo * steal it from ->worklist without clearing WORK_STRUCT_PENDING. 2114bf4ede01STejun Heo */ 2115d565ed63STejun Heo pool = get_work_pool(work); 2116d565ed63STejun Heo if (!pool) 2117bbb68dfaSTejun Heo goto fail; 2118bf4ede01STejun Heo 2119a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&pool->lock); 2120bf4ede01STejun Heo /* 2121112202d9STejun Heo * work->data is guaranteed to point to pwq only while the work 2122112202d9STejun Heo * item is queued on pwq->wq, and both updating work->data to point 2123112202d9STejun Heo * to pwq on queueing and to pool on dequeueing are done under 2124112202d9STejun Heo * pwq->pool->lock. This in turn guarantees that, if work->data 2125112202d9STejun Heo * points to pwq which is associated with a locked pool, the work 21260b3dae68SLai Jiangshan * item is currently queued on that pool. 2127bf4ede01STejun Heo */ 2128112202d9STejun Heo pwq = get_work_pwq(work); 2129112202d9STejun Heo if (pwq && pwq->pool == pool) { 2130c70e1779STejun Heo unsigned long work_data; 2131c70e1779STejun Heo 2132bf4ede01STejun Heo debug_work_deactivate(work); 21333aa62497SLai Jiangshan 21343aa62497SLai Jiangshan /* 2135018f3a13SLai Jiangshan * A cancelable inactive work item must be in the 2136018f3a13SLai Jiangshan * pwq->inactive_works since a queued barrier can't be 2137018f3a13SLai Jiangshan * canceled (see the comments in insert_wq_barrier()). 2138018f3a13SLai Jiangshan * 2139f97a4a1aSLai Jiangshan * An inactive work item cannot be grabbed directly because 2140d812796eSLai Jiangshan * it might have linked barrier work items which, if left 2141f97a4a1aSLai Jiangshan * on the inactive_works list, will confuse pwq->nr_active 214216062836STejun Heo * management later on and cause stall. Make sure the work 214316062836STejun Heo * item is activated before grabbing. 21443aa62497SLai Jiangshan */ 21454c638030STejun Heo pwq_activate_work(pwq, work); 21463aa62497SLai Jiangshan 2147bf4ede01STejun Heo list_del_init(&work->entry); 214836e227d2STejun Heo 2149c70e1779STejun Heo /* 2150c70e1779STejun Heo * work->data points to pwq iff queued. Let's point to pool. As 2151c70e1779STejun Heo * this destroys work->data needed by the next step, stash it. 2152c70e1779STejun Heo */ 2153c70e1779STejun Heo work_data = *work_data_bits(work); 2154bccdc1faSTejun Heo set_work_pool_and_keep_pending(work, pool->id, 0); 21554468a00fSLai Jiangshan 2156dd6c3c54STejun Heo /* must be the last step, see the function comment */ 2157c70e1779STejun Heo pwq_dec_nr_in_flight(pwq, work_data); 2158dd6c3c54STejun Heo 2159a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 216024acfb71SThomas Gleixner rcu_read_unlock(); 216136e227d2STejun Heo return 1; 2162bf4ede01STejun Heo } 2163a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&pool->lock); 2164bbb68dfaSTejun Heo fail: 216524acfb71SThomas Gleixner rcu_read_unlock(); 2166c26e2f2eSTejun Heo local_irq_restore(*irq_flags); 2167bbb68dfaSTejun Heo if (work_is_canceling(work)) 2168bbb68dfaSTejun Heo return -ENOENT; 2169bbb68dfaSTejun Heo cpu_relax(); 217036e227d2STejun Heo return -EAGAIN; 2171bf4ede01STejun Heo } 2172bf4ede01STejun Heo 2173978b8409STejun Heo struct cwt_wait { 2174978b8409STejun Heo wait_queue_entry_t wait; 2175978b8409STejun Heo struct work_struct *work; 2176978b8409STejun Heo }; 2177978b8409STejun Heo 2178978b8409STejun Heo static int cwt_wakefn(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 2179978b8409STejun Heo { 2180978b8409STejun Heo struct cwt_wait *cwait = container_of(wait, struct cwt_wait, wait); 2181978b8409STejun Heo 2182978b8409STejun Heo if (cwait->work != key) 2183978b8409STejun Heo return 0; 2184978b8409STejun Heo return autoremove_wake_function(wait, mode, sync, key); 2185978b8409STejun Heo } 2186978b8409STejun Heo 2187978b8409STejun Heo /** 2188978b8409STejun Heo * work_grab_pending - steal work item from worklist and disable irq 2189978b8409STejun Heo * @work: work item to steal 2190978b8409STejun Heo * @cflags: %WORK_CANCEL_ flags 2191978b8409STejun Heo * @irq_flags: place to store IRQ state 2192978b8409STejun Heo * 2193978b8409STejun Heo * Grab PENDING bit of @work. @work can be in any stable state - idle, on timer 2194978b8409STejun Heo * or on worklist. 2195978b8409STejun Heo * 2196978b8409STejun Heo * Must be called in process context. IRQ is disabled on return with IRQ state 2197978b8409STejun Heo * stored in *@irq_flags. The caller is responsible for re-enabling it using 2198978b8409STejun Heo * local_irq_restore(). 2199978b8409STejun Heo * 2200978b8409STejun Heo * Returns %true if @work was pending. %false if idle. 2201978b8409STejun Heo */ 2202978b8409STejun Heo static bool work_grab_pending(struct work_struct *work, u32 cflags, 2203978b8409STejun Heo unsigned long *irq_flags) 2204978b8409STejun Heo { 2205978b8409STejun Heo struct cwt_wait cwait; 2206978b8409STejun Heo int ret; 2207978b8409STejun Heo 2208978b8409STejun Heo might_sleep(); 2209978b8409STejun Heo repeat: 2210978b8409STejun Heo ret = try_to_grab_pending(work, cflags, irq_flags); 2211978b8409STejun Heo if (likely(ret >= 0)) 2212978b8409STejun Heo return ret; 2213978b8409STejun Heo if (ret != -ENOENT) 2214978b8409STejun Heo goto repeat; 2215978b8409STejun Heo 2216978b8409STejun Heo /* 2217978b8409STejun Heo * Someone is already canceling. Wait for it to finish. flush_work() 2218978b8409STejun Heo * doesn't work for PREEMPT_NONE because we may get woken up between 2219978b8409STejun Heo * @work's completion and the other canceling task resuming and clearing 2220978b8409STejun Heo * CANCELING - flush_work() will return false immediately as @work is no 2221978b8409STejun Heo * longer busy, try_to_grab_pending() will return -ENOENT as @work is 2222978b8409STejun Heo * still being canceled and the other canceling task won't be able to 2223978b8409STejun Heo * clear CANCELING as we're hogging the CPU. 2224978b8409STejun Heo * 2225978b8409STejun Heo * Let's wait for completion using a waitqueue. As this may lead to the 2226978b8409STejun Heo * thundering herd problem, use a custom wake function which matches 2227978b8409STejun Heo * @work along with exclusive wait and wakeup. 2228978b8409STejun Heo */ 2229978b8409STejun Heo init_wait(&cwait.wait); 2230978b8409STejun Heo cwait.wait.func = cwt_wakefn; 2231978b8409STejun Heo cwait.work = work; 2232978b8409STejun Heo 2233978b8409STejun Heo prepare_to_wait_exclusive(&wq_cancel_waitq, &cwait.wait, 2234978b8409STejun Heo TASK_UNINTERRUPTIBLE); 2235978b8409STejun Heo if (work_is_canceling(work)) 2236978b8409STejun Heo schedule(); 2237978b8409STejun Heo finish_wait(&wq_cancel_waitq, &cwait.wait); 2238978b8409STejun Heo 2239978b8409STejun Heo goto repeat; 2240978b8409STejun Heo } 2241978b8409STejun Heo 2242bf4ede01STejun Heo /** 2243706026c2STejun Heo * insert_work - insert a work into a pool 2244112202d9STejun Heo * @pwq: pwq @work belongs to 22454690c4abSTejun Heo * @work: work to insert 22464690c4abSTejun Heo * @head: insertion point 22474690c4abSTejun Heo * @extra_flags: extra WORK_STRUCT_* flags to set 22484690c4abSTejun Heo * 2249112202d9STejun Heo * Insert @work which belongs to @pwq after @head. @extra_flags is or'd to 2250706026c2STejun Heo * work_struct flags. 22514690c4abSTejun Heo * 22524690c4abSTejun Heo * CONTEXT: 2253a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2254365970a1SDavid Howells */ 2255112202d9STejun Heo static void insert_work(struct pool_workqueue *pwq, struct work_struct *work, 2256112202d9STejun Heo struct list_head *head, unsigned int extra_flags) 2257b89deed3SOleg Nesterov { 2258fe089f87STejun Heo debug_work_activate(work); 2259e1d8aa9fSFrederic Weisbecker 2260e89a85d6SWalter Wu /* record the work call stack in order to print it in KASAN reports */ 2261f70da745SMarco Elver kasan_record_aux_stack_noalloc(work); 2262e89a85d6SWalter Wu 22634690c4abSTejun Heo /* we own @work, set data and link */ 2264112202d9STejun Heo set_work_pwq(work, pwq, extra_flags); 22651a4d9b0aSOleg Nesterov list_add_tail(&work->entry, head); 22668864b4e5STejun Heo get_pwq(pwq); 2267b89deed3SOleg Nesterov } 2268b89deed3SOleg Nesterov 2269c8efcc25STejun Heo /* 2270c8efcc25STejun Heo * Test whether @work is being queued from another work executing on the 22718d03ecfeSTejun Heo * same workqueue. 2272c8efcc25STejun Heo */ 2273c8efcc25STejun Heo static bool is_chained_work(struct workqueue_struct *wq) 2274c8efcc25STejun Heo { 2275c8efcc25STejun Heo struct worker *worker; 2276c8efcc25STejun Heo 22778d03ecfeSTejun Heo worker = current_wq_worker(); 2278c8efcc25STejun Heo /* 2279bf393fd4SBart Van Assche * Return %true iff I'm a worker executing a work item on @wq. If 22808d03ecfeSTejun Heo * I'm @worker, it's safe to dereference it without locking. 2281c8efcc25STejun Heo */ 2282112202d9STejun Heo return worker && worker->current_pwq->wq == wq; 2283c8efcc25STejun Heo } 2284c8efcc25STejun Heo 2285ef557180SMike Galbraith /* 2286ef557180SMike Galbraith * When queueing an unbound work item to a wq, prefer local CPU if allowed 2287ef557180SMike Galbraith * by wq_unbound_cpumask. Otherwise, round robin among the allowed ones to 2288ef557180SMike Galbraith * avoid perturbing sensitive tasks. 2289ef557180SMike Galbraith */ 2290ef557180SMike Galbraith static int wq_select_unbound_cpu(int cpu) 2291ef557180SMike Galbraith { 2292ef557180SMike Galbraith int new_cpu; 2293ef557180SMike Galbraith 2294f303fccbSTejun Heo if (likely(!wq_debug_force_rr_cpu)) { 2295ef557180SMike Galbraith if (cpumask_test_cpu(cpu, wq_unbound_cpumask)) 2296ef557180SMike Galbraith return cpu; 2297a8ec5880SAmmar Faizi } else { 2298a8ec5880SAmmar Faizi pr_warn_once("workqueue: round-robin CPU selection forced, expect performance impact\n"); 2299f303fccbSTejun Heo } 2300f303fccbSTejun Heo 2301ef557180SMike Galbraith new_cpu = __this_cpu_read(wq_rr_cpu_last); 2302ef557180SMike Galbraith new_cpu = cpumask_next_and(new_cpu, wq_unbound_cpumask, cpu_online_mask); 2303ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) { 2304ef557180SMike Galbraith new_cpu = cpumask_first_and(wq_unbound_cpumask, cpu_online_mask); 2305ef557180SMike Galbraith if (unlikely(new_cpu >= nr_cpu_ids)) 2306ef557180SMike Galbraith return cpu; 2307ef557180SMike Galbraith } 2308ef557180SMike Galbraith __this_cpu_write(wq_rr_cpu_last, new_cpu); 2309ef557180SMike Galbraith 2310ef557180SMike Galbraith return new_cpu; 2311ef557180SMike Galbraith } 2312ef557180SMike Galbraith 2313d84ff051STejun Heo static void __queue_work(int cpu, struct workqueue_struct *wq, 23141da177e4SLinus Torvalds struct work_struct *work) 23151da177e4SLinus Torvalds { 2316112202d9STejun Heo struct pool_workqueue *pwq; 2317fe089f87STejun Heo struct worker_pool *last_pool, *pool; 23188a2e8e5dSTejun Heo unsigned int work_flags; 2319b75cac93SJoonsoo Kim unsigned int req_cpu = cpu; 23208930cabaSTejun Heo 23218930cabaSTejun Heo /* 23228930cabaSTejun Heo * While a work item is PENDING && off queue, a task trying to 23238930cabaSTejun Heo * steal the PENDING will busy-loop waiting for it to either get 23248930cabaSTejun Heo * queued or lose PENDING. Grabbing PENDING and queueing should 23258930cabaSTejun Heo * happen with IRQ disabled. 23268930cabaSTejun Heo */ 23278e8eb730SFrederic Weisbecker lockdep_assert_irqs_disabled(); 23281da177e4SLinus Torvalds 232933e3f0a3SRichard Clark /* 233033e3f0a3SRichard Clark * For a draining wq, only works from the same workqueue are 233133e3f0a3SRichard Clark * allowed. The __WQ_DESTROYING helps to spot the issue that 233233e3f0a3SRichard Clark * queues a new work item to a wq after destroy_workqueue(wq). 233333e3f0a3SRichard Clark */ 233433e3f0a3SRichard Clark if (unlikely(wq->flags & (__WQ_DESTROYING | __WQ_DRAINING) && 233533e3f0a3SRichard Clark WARN_ON_ONCE(!is_chained_work(wq)))) 2336e41e704bSTejun Heo return; 233724acfb71SThomas Gleixner rcu_read_lock(); 23389e8cd2f5STejun Heo retry: 2339aa202f1fSHillf Danton /* pwq which will be used unless @work is executing elsewhere */ 2340636b927eSTejun Heo if (req_cpu == WORK_CPU_UNBOUND) { 2341636b927eSTejun Heo if (wq->flags & WQ_UNBOUND) 2342ef557180SMike Galbraith cpu = wq_select_unbound_cpu(raw_smp_processor_id()); 2343636b927eSTejun Heo else 2344aa202f1fSHillf Danton cpu = raw_smp_processor_id(); 2345aa202f1fSHillf Danton } 2346f3421797STejun Heo 2347636b927eSTejun Heo pwq = rcu_dereference(*per_cpu_ptr(wq->cpu_pwq, cpu)); 2348fe089f87STejun Heo pool = pwq->pool; 2349fe089f87STejun Heo 235018aa9effSTejun Heo /* 2351c9178087STejun Heo * If @work was previously on a different pool, it might still be 2352c9178087STejun Heo * running there, in which case the work needs to be queued on that 2353c9178087STejun Heo * pool to guarantee non-reentrancy. 235418aa9effSTejun Heo */ 2355c9e7cf27STejun Heo last_pool = get_work_pool(work); 2356fe089f87STejun Heo if (last_pool && last_pool != pool) { 235718aa9effSTejun Heo struct worker *worker; 235818aa9effSTejun Heo 2359a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&last_pool->lock); 236018aa9effSTejun Heo 2361c9e7cf27STejun Heo worker = find_worker_executing_work(last_pool, work); 236218aa9effSTejun Heo 2363112202d9STejun Heo if (worker && worker->current_pwq->wq == wq) { 2364c9178087STejun Heo pwq = worker->current_pwq; 2365fe089f87STejun Heo pool = pwq->pool; 2366fe089f87STejun Heo WARN_ON_ONCE(pool != last_pool); 23678594fadeSLai Jiangshan } else { 236818aa9effSTejun Heo /* meh... not running there, queue here */ 2369a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&last_pool->lock); 2370fe089f87STejun Heo raw_spin_lock(&pool->lock); 237118aa9effSTejun Heo } 23728930cabaSTejun Heo } else { 2373fe089f87STejun Heo raw_spin_lock(&pool->lock); 23748930cabaSTejun Heo } 2375502ca9d8STejun Heo 23769e8cd2f5STejun Heo /* 2377636b927eSTejun Heo * pwq is determined and locked. For unbound pools, we could have raced 2378636b927eSTejun Heo * with pwq release and it could already be dead. If its refcnt is zero, 2379636b927eSTejun Heo * repeat pwq selection. Note that unbound pwqs never die without 2380636b927eSTejun Heo * another pwq replacing it in cpu_pwq or while work items are executing 2381636b927eSTejun Heo * on it, so the retrying is guaranteed to make forward-progress. 23829e8cd2f5STejun Heo */ 23839e8cd2f5STejun Heo if (unlikely(!pwq->refcnt)) { 23849e8cd2f5STejun Heo if (wq->flags & WQ_UNBOUND) { 2385fe089f87STejun Heo raw_spin_unlock(&pool->lock); 23869e8cd2f5STejun Heo cpu_relax(); 23879e8cd2f5STejun Heo goto retry; 23889e8cd2f5STejun Heo } 23899e8cd2f5STejun Heo /* oops */ 23909e8cd2f5STejun Heo WARN_ONCE(true, "workqueue: per-cpu pwq for %s on cpu%d has 0 refcnt", 23919e8cd2f5STejun Heo wq->name, cpu); 23929e8cd2f5STejun Heo } 23939e8cd2f5STejun Heo 2394112202d9STejun Heo /* pwq determined, queue */ 2395112202d9STejun Heo trace_workqueue_queue_work(req_cpu, pwq, work); 2396502ca9d8STejun Heo 239724acfb71SThomas Gleixner if (WARN_ON(!list_empty(&work->entry))) 239824acfb71SThomas Gleixner goto out; 23991e19ffc6STejun Heo 2400112202d9STejun Heo pwq->nr_in_flight[pwq->work_color]++; 2401112202d9STejun Heo work_flags = work_color_to_flags(pwq->work_color); 24021e19ffc6STejun Heo 2403a045a272STejun Heo /* 2404a045a272STejun Heo * Limit the number of concurrently active work items to max_active. 2405a045a272STejun Heo * @work must also queue behind existing inactive work items to maintain 2406a045a272STejun Heo * ordering when max_active changes. See wq_adjust_max_active(). 2407a045a272STejun Heo */ 24085797b1c1STejun Heo if (list_empty(&pwq->inactive_works) && pwq_tryinc_nr_active(pwq, false)) { 2409fe089f87STejun Heo if (list_empty(&pool->worklist)) 2410fe089f87STejun Heo pool->watchdog_ts = jiffies; 2411fe089f87STejun Heo 2412cdadf009STejun Heo trace_workqueue_activate_work(work); 2413fe089f87STejun Heo insert_work(pwq, work, &pool->worklist, work_flags); 24140219a352STejun Heo kick_pool(pool); 24158a2e8e5dSTejun Heo } else { 2416f97a4a1aSLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 2417fe089f87STejun Heo insert_work(pwq, work, &pwq->inactive_works, work_flags); 24188a2e8e5dSTejun Heo } 24191e19ffc6STejun Heo 242024acfb71SThomas Gleixner out: 2421fe089f87STejun Heo raw_spin_unlock(&pool->lock); 242224acfb71SThomas Gleixner rcu_read_unlock(); 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 24250fcb78c2SRolf Eike Beer /** 2426c1a220e7SZhang Rui * queue_work_on - queue work on specific cpu 2427c1a220e7SZhang Rui * @cpu: CPU number to execute work on 2428c1a220e7SZhang Rui * @wq: workqueue to use 2429c1a220e7SZhang Rui * @work: work to queue 2430c1a220e7SZhang Rui * 2431c1a220e7SZhang Rui * We queue the work to a specific CPU, the caller must ensure it 2432443378f0SPaul E. McKenney * can't go away. Callers that fail to ensure that the specified 2433443378f0SPaul E. McKenney * CPU cannot go away will execute on a randomly chosen CPU. 2434854f5cc5SPaul E. McKenney * But note well that callers specifying a CPU that never has been 2435854f5cc5SPaul E. McKenney * online will get a splat. 2436d185af30SYacine Belkadi * 2437d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. 2438c1a220e7SZhang Rui */ 2439d4283e93STejun Heo bool queue_work_on(int cpu, struct workqueue_struct *wq, 2440d4283e93STejun Heo struct work_struct *work) 2441c1a220e7SZhang Rui { 2442d4283e93STejun Heo bool ret = false; 2443c26e2f2eSTejun Heo unsigned long irq_flags; 24448930cabaSTejun Heo 2445c26e2f2eSTejun Heo local_irq_save(irq_flags); 2446c1a220e7SZhang Rui 244722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 24484690c4abSTejun Heo __queue_work(cpu, wq, work); 2449d4283e93STejun Heo ret = true; 2450c1a220e7SZhang Rui } 24518930cabaSTejun Heo 2452c26e2f2eSTejun Heo local_irq_restore(irq_flags); 2453c1a220e7SZhang Rui return ret; 2454c1a220e7SZhang Rui } 2455ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_work_on); 2456c1a220e7SZhang Rui 24578204e0c1SAlexander Duyck /** 2458fef59c9cSTejun Heo * select_numa_node_cpu - Select a CPU based on NUMA node 24598204e0c1SAlexander Duyck * @node: NUMA node ID that we want to select a CPU from 24608204e0c1SAlexander Duyck * 24618204e0c1SAlexander Duyck * This function will attempt to find a "random" cpu available on a given 24628204e0c1SAlexander Duyck * node. If there are no CPUs available on the given node it will return 24638204e0c1SAlexander Duyck * WORK_CPU_UNBOUND indicating that we should just schedule to any 24648204e0c1SAlexander Duyck * available CPU if we need to schedule this work. 24658204e0c1SAlexander Duyck */ 2466fef59c9cSTejun Heo static int select_numa_node_cpu(int node) 24678204e0c1SAlexander Duyck { 24688204e0c1SAlexander Duyck int cpu; 24698204e0c1SAlexander Duyck 24708204e0c1SAlexander Duyck /* Delay binding to CPU if node is not valid or online */ 24718204e0c1SAlexander Duyck if (node < 0 || node >= MAX_NUMNODES || !node_online(node)) 24728204e0c1SAlexander Duyck return WORK_CPU_UNBOUND; 24738204e0c1SAlexander Duyck 24748204e0c1SAlexander Duyck /* Use local node/cpu if we are already there */ 24758204e0c1SAlexander Duyck cpu = raw_smp_processor_id(); 24768204e0c1SAlexander Duyck if (node == cpu_to_node(cpu)) 24778204e0c1SAlexander Duyck return cpu; 24788204e0c1SAlexander Duyck 24798204e0c1SAlexander Duyck /* Use "random" otherwise know as "first" online CPU of node */ 24808204e0c1SAlexander Duyck cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask); 24818204e0c1SAlexander Duyck 24828204e0c1SAlexander Duyck /* If CPU is valid return that, otherwise just defer */ 24838204e0c1SAlexander Duyck return cpu < nr_cpu_ids ? cpu : WORK_CPU_UNBOUND; 24848204e0c1SAlexander Duyck } 24858204e0c1SAlexander Duyck 24868204e0c1SAlexander Duyck /** 24878204e0c1SAlexander Duyck * queue_work_node - queue work on a "random" cpu for a given NUMA node 24888204e0c1SAlexander Duyck * @node: NUMA node that we are targeting the work for 24898204e0c1SAlexander Duyck * @wq: workqueue to use 24908204e0c1SAlexander Duyck * @work: work to queue 24918204e0c1SAlexander Duyck * 24928204e0c1SAlexander Duyck * We queue the work to a "random" CPU within a given NUMA node. The basic 24938204e0c1SAlexander Duyck * idea here is to provide a way to somehow associate work with a given 24948204e0c1SAlexander Duyck * NUMA node. 24958204e0c1SAlexander Duyck * 24968204e0c1SAlexander Duyck * This function will only make a best effort attempt at getting this onto 24978204e0c1SAlexander Duyck * the right NUMA node. If no node is requested or the requested node is 24988204e0c1SAlexander Duyck * offline then we just fall back to standard queue_work behavior. 24998204e0c1SAlexander Duyck * 25008204e0c1SAlexander Duyck * Currently the "random" CPU ends up being the first available CPU in the 25018204e0c1SAlexander Duyck * intersection of cpu_online_mask and the cpumask of the node, unless we 25028204e0c1SAlexander Duyck * are running on the node. In that case we just use the current CPU. 25038204e0c1SAlexander Duyck * 25048204e0c1SAlexander Duyck * Return: %false if @work was already on a queue, %true otherwise. 25058204e0c1SAlexander Duyck */ 25068204e0c1SAlexander Duyck bool queue_work_node(int node, struct workqueue_struct *wq, 25078204e0c1SAlexander Duyck struct work_struct *work) 25088204e0c1SAlexander Duyck { 2509c26e2f2eSTejun Heo unsigned long irq_flags; 25108204e0c1SAlexander Duyck bool ret = false; 25118204e0c1SAlexander Duyck 25128204e0c1SAlexander Duyck /* 25138204e0c1SAlexander Duyck * This current implementation is specific to unbound workqueues. 25148204e0c1SAlexander Duyck * Specifically we only return the first available CPU for a given 25158204e0c1SAlexander Duyck * node instead of cycling through individual CPUs within the node. 25168204e0c1SAlexander Duyck * 25178204e0c1SAlexander Duyck * If this is used with a per-cpu workqueue then the logic in 25188204e0c1SAlexander Duyck * workqueue_select_cpu_near would need to be updated to allow for 25198204e0c1SAlexander Duyck * some round robin type logic. 25208204e0c1SAlexander Duyck */ 25218204e0c1SAlexander Duyck WARN_ON_ONCE(!(wq->flags & WQ_UNBOUND)); 25228204e0c1SAlexander Duyck 2523c26e2f2eSTejun Heo local_irq_save(irq_flags); 25248204e0c1SAlexander Duyck 25258204e0c1SAlexander Duyck if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 2526fef59c9cSTejun Heo int cpu = select_numa_node_cpu(node); 25278204e0c1SAlexander Duyck 25288204e0c1SAlexander Duyck __queue_work(cpu, wq, work); 25298204e0c1SAlexander Duyck ret = true; 25308204e0c1SAlexander Duyck } 25318204e0c1SAlexander Duyck 2532c26e2f2eSTejun Heo local_irq_restore(irq_flags); 25338204e0c1SAlexander Duyck return ret; 25348204e0c1SAlexander Duyck } 25358204e0c1SAlexander Duyck EXPORT_SYMBOL_GPL(queue_work_node); 25368204e0c1SAlexander Duyck 25378c20feb6SKees Cook void delayed_work_timer_fn(struct timer_list *t) 25381da177e4SLinus Torvalds { 25398c20feb6SKees Cook struct delayed_work *dwork = from_timer(dwork, t, timer); 25401da177e4SLinus Torvalds 2541e0aecdd8STejun Heo /* should have been called from irqsafe timer with irq already off */ 254260c057bcSLai Jiangshan __queue_work(dwork->cpu, dwork->wq, &dwork->work); 25431da177e4SLinus Torvalds } 25441438ade5SKonstantin Khlebnikov EXPORT_SYMBOL(delayed_work_timer_fn); 25451da177e4SLinus Torvalds 25467beb2edfSTejun Heo static void __queue_delayed_work(int cpu, struct workqueue_struct *wq, 254752bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25481da177e4SLinus Torvalds { 25497beb2edfSTejun Heo struct timer_list *timer = &dwork->timer; 25507beb2edfSTejun Heo struct work_struct *work = &dwork->work; 25511da177e4SLinus Torvalds 2552637fdbaeSTejun Heo WARN_ON_ONCE(!wq); 25534b243563SSami Tolvanen WARN_ON_ONCE(timer->function != delayed_work_timer_fn); 2554fc4b514fSTejun Heo WARN_ON_ONCE(timer_pending(timer)); 2555fc4b514fSTejun Heo WARN_ON_ONCE(!list_empty(&work->entry)); 25567beb2edfSTejun Heo 25578852aac2STejun Heo /* 25588852aac2STejun Heo * If @delay is 0, queue @dwork->work immediately. This is for 25598852aac2STejun Heo * both optimization and correctness. The earliest @timer can 25608852aac2STejun Heo * expire is on the closest next tick and delayed_work users depend 25618852aac2STejun Heo * on that there's no such delay when @delay is 0. 25628852aac2STejun Heo */ 25638852aac2STejun Heo if (!delay) { 25648852aac2STejun Heo __queue_work(cpu, wq, &dwork->work); 25658852aac2STejun Heo return; 25668852aac2STejun Heo } 25678852aac2STejun Heo 256860c057bcSLai Jiangshan dwork->wq = wq; 25691265057fSTejun Heo dwork->cpu = cpu; 25707beb2edfSTejun Heo timer->expires = jiffies + delay; 25717beb2edfSTejun Heo 2572aae17ebbSLeonardo Bras if (housekeeping_enabled(HK_TYPE_TIMER)) { 2573aae17ebbSLeonardo Bras /* If the current cpu is a housekeeping cpu, use it. */ 2574aae17ebbSLeonardo Bras cpu = smp_processor_id(); 2575aae17ebbSLeonardo Bras if (!housekeeping_test_cpu(cpu, HK_TYPE_TIMER)) 2576aae17ebbSLeonardo Bras cpu = housekeeping_any_cpu(HK_TYPE_TIMER); 25777beb2edfSTejun Heo add_timer_on(timer, cpu); 2578aae17ebbSLeonardo Bras } else { 2579aae17ebbSLeonardo Bras if (likely(cpu == WORK_CPU_UNBOUND)) 2580c0e8c5b5SAnna-Maria Behnsen add_timer_global(timer); 2581aae17ebbSLeonardo Bras else 2582aae17ebbSLeonardo Bras add_timer_on(timer, cpu); 2583aae17ebbSLeonardo Bras } 25847beb2edfSTejun Heo } 25851da177e4SLinus Torvalds 25860fcb78c2SRolf Eike Beer /** 25870fcb78c2SRolf Eike Beer * queue_delayed_work_on - queue work on specific CPU after delay 25880fcb78c2SRolf Eike Beer * @cpu: CPU number to execute work on 25890fcb78c2SRolf Eike Beer * @wq: workqueue to use 2590af9997e4SRandy Dunlap * @dwork: work to queue 25910fcb78c2SRolf Eike Beer * @delay: number of jiffies to wait before queueing 25920fcb78c2SRolf Eike Beer * 2593d185af30SYacine Belkadi * Return: %false if @work was already on a queue, %true otherwise. If 2594715f1300STejun Heo * @delay is zero and @dwork is idle, it will be scheduled for immediate 2595715f1300STejun Heo * execution. 25960fcb78c2SRolf Eike Beer */ 2597d4283e93STejun Heo bool queue_delayed_work_on(int cpu, struct workqueue_struct *wq, 259852bad64dSDavid Howells struct delayed_work *dwork, unsigned long delay) 25997a6bc1cdSVenkatesh Pallipadi { 260052bad64dSDavid Howells struct work_struct *work = &dwork->work; 2601d4283e93STejun Heo bool ret = false; 2602c26e2f2eSTejun Heo unsigned long irq_flags; 26038930cabaSTejun Heo 26048930cabaSTejun Heo /* read the comment in __queue_work() */ 2605c26e2f2eSTejun Heo local_irq_save(irq_flags); 26067a6bc1cdSVenkatesh Pallipadi 260722df02bbSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 26087beb2edfSTejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2609d4283e93STejun Heo ret = true; 26107a6bc1cdSVenkatesh Pallipadi } 26118930cabaSTejun Heo 2612c26e2f2eSTejun Heo local_irq_restore(irq_flags); 26137a6bc1cdSVenkatesh Pallipadi return ret; 26147a6bc1cdSVenkatesh Pallipadi } 2615ad7b1f84SMarc Dionne EXPORT_SYMBOL(queue_delayed_work_on); 26161da177e4SLinus Torvalds 2617c8e55f36STejun Heo /** 26188376fe22STejun Heo * mod_delayed_work_on - modify delay of or queue a delayed work on specific CPU 26198376fe22STejun Heo * @cpu: CPU number to execute work on 26208376fe22STejun Heo * @wq: workqueue to use 26218376fe22STejun Heo * @dwork: work to queue 26228376fe22STejun Heo * @delay: number of jiffies to wait before queueing 26238376fe22STejun Heo * 26248376fe22STejun Heo * If @dwork is idle, equivalent to queue_delayed_work_on(); otherwise, 26258376fe22STejun Heo * modify @dwork's timer so that it expires after @delay. If @delay is 26268376fe22STejun Heo * zero, @work is guaranteed to be scheduled immediately regardless of its 26278376fe22STejun Heo * current state. 26288376fe22STejun Heo * 2629d185af30SYacine Belkadi * Return: %false if @dwork was idle and queued, %true if @dwork was 26308376fe22STejun Heo * pending and its timer was modified. 26318376fe22STejun Heo * 2632e0aecdd8STejun Heo * This function is safe to call from any context including IRQ handler. 26338376fe22STejun Heo * See try_to_grab_pending() for details. 26348376fe22STejun Heo */ 26358376fe22STejun Heo bool mod_delayed_work_on(int cpu, struct workqueue_struct *wq, 26368376fe22STejun Heo struct delayed_work *dwork, unsigned long delay) 26378376fe22STejun Heo { 2638c26e2f2eSTejun Heo unsigned long irq_flags; 26398376fe22STejun Heo int ret; 26408376fe22STejun Heo 26418376fe22STejun Heo do { 2642c5f5b942STejun Heo ret = try_to_grab_pending(&dwork->work, WORK_CANCEL_DELAYED, 2643c5f5b942STejun Heo &irq_flags); 26448376fe22STejun Heo } while (unlikely(ret == -EAGAIN)); 26458376fe22STejun Heo 26468376fe22STejun Heo if (likely(ret >= 0)) { 26478376fe22STejun Heo __queue_delayed_work(cpu, wq, dwork, delay); 2648c26e2f2eSTejun Heo local_irq_restore(irq_flags); 26498376fe22STejun Heo } 26508376fe22STejun Heo 26518376fe22STejun Heo /* -ENOENT from try_to_grab_pending() becomes %true */ 26528376fe22STejun Heo return ret; 26538376fe22STejun Heo } 26548376fe22STejun Heo EXPORT_SYMBOL_GPL(mod_delayed_work_on); 26558376fe22STejun Heo 265605f0fe6bSTejun Heo static void rcu_work_rcufn(struct rcu_head *rcu) 265705f0fe6bSTejun Heo { 265805f0fe6bSTejun Heo struct rcu_work *rwork = container_of(rcu, struct rcu_work, rcu); 265905f0fe6bSTejun Heo 266005f0fe6bSTejun Heo /* read the comment in __queue_work() */ 266105f0fe6bSTejun Heo local_irq_disable(); 266205f0fe6bSTejun Heo __queue_work(WORK_CPU_UNBOUND, rwork->wq, &rwork->work); 266305f0fe6bSTejun Heo local_irq_enable(); 266405f0fe6bSTejun Heo } 266505f0fe6bSTejun Heo 266605f0fe6bSTejun Heo /** 266705f0fe6bSTejun Heo * queue_rcu_work - queue work after a RCU grace period 266805f0fe6bSTejun Heo * @wq: workqueue to use 266905f0fe6bSTejun Heo * @rwork: work to queue 267005f0fe6bSTejun Heo * 267105f0fe6bSTejun Heo * Return: %false if @rwork was already pending, %true otherwise. Note 267205f0fe6bSTejun Heo * that a full RCU grace period is guaranteed only after a %true return. 2673bf393fd4SBart Van Assche * While @rwork is guaranteed to be executed after a %false return, the 267405f0fe6bSTejun Heo * execution may happen before a full RCU grace period has passed. 267505f0fe6bSTejun Heo */ 267605f0fe6bSTejun Heo bool queue_rcu_work(struct workqueue_struct *wq, struct rcu_work *rwork) 267705f0fe6bSTejun Heo { 267805f0fe6bSTejun Heo struct work_struct *work = &rwork->work; 267905f0fe6bSTejun Heo 268005f0fe6bSTejun Heo if (!test_and_set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))) { 268105f0fe6bSTejun Heo rwork->wq = wq; 2682a7e30c0eSUladzislau Rezki call_rcu_hurry(&rwork->rcu, rcu_work_rcufn); 268305f0fe6bSTejun Heo return true; 268405f0fe6bSTejun Heo } 268505f0fe6bSTejun Heo 268605f0fe6bSTejun Heo return false; 268705f0fe6bSTejun Heo } 268805f0fe6bSTejun Heo EXPORT_SYMBOL(queue_rcu_work); 268905f0fe6bSTejun Heo 2690f7537df5SLai Jiangshan static struct worker *alloc_worker(int node) 2691c34056a3STejun Heo { 2692c34056a3STejun Heo struct worker *worker; 2693c34056a3STejun Heo 2694f7537df5SLai Jiangshan worker = kzalloc_node(sizeof(*worker), GFP_KERNEL, node); 2695c8e55f36STejun Heo if (worker) { 2696c8e55f36STejun Heo INIT_LIST_HEAD(&worker->entry); 2697affee4b2STejun Heo INIT_LIST_HEAD(&worker->scheduled); 2698da028469SLai Jiangshan INIT_LIST_HEAD(&worker->node); 2699e22bee78STejun Heo /* on creation a worker is in !idle && prep state */ 2700e22bee78STejun Heo worker->flags = WORKER_PREP; 2701c8e55f36STejun Heo } 2702c34056a3STejun Heo return worker; 2703c34056a3STejun Heo } 2704c34056a3STejun Heo 27059546b29eSTejun Heo static cpumask_t *pool_allowed_cpus(struct worker_pool *pool) 27069546b29eSTejun Heo { 27078639ecebSTejun Heo if (pool->cpu < 0 && pool->attrs->affn_strict) 27089546b29eSTejun Heo return pool->attrs->__pod_cpumask; 27098639ecebSTejun Heo else 27108639ecebSTejun Heo return pool->attrs->cpumask; 27119546b29eSTejun Heo } 27129546b29eSTejun Heo 2713c34056a3STejun Heo /** 27144736cbf7SLai Jiangshan * worker_attach_to_pool() - attach a worker to a pool 27154736cbf7SLai Jiangshan * @worker: worker to be attached 27164736cbf7SLai Jiangshan * @pool: the target pool 27174736cbf7SLai Jiangshan * 27184736cbf7SLai Jiangshan * Attach @worker to @pool. Once attached, the %WORKER_UNBOUND flag and 27194736cbf7SLai Jiangshan * cpu-binding of @worker are kept coordinated with the pool across 27204736cbf7SLai Jiangshan * cpu-[un]hotplugs. 27214736cbf7SLai Jiangshan */ 27224736cbf7SLai Jiangshan static void worker_attach_to_pool(struct worker *worker, 27234736cbf7SLai Jiangshan struct worker_pool *pool) 27244736cbf7SLai Jiangshan { 27251258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 27264736cbf7SLai Jiangshan 27274736cbf7SLai Jiangshan /* 27284cb1ef64STejun Heo * The wq_pool_attach_mutex ensures %POOL_DISASSOCIATED remains stable 27294cb1ef64STejun Heo * across this function. See the comments above the flag definition for 27304cb1ef64STejun Heo * details. BH workers are, while per-CPU, always DISASSOCIATED. 27314736cbf7SLai Jiangshan */ 27324cb1ef64STejun Heo if (pool->flags & POOL_DISASSOCIATED) { 27334736cbf7SLai Jiangshan worker->flags |= WORKER_UNBOUND; 27344cb1ef64STejun Heo } else { 27354cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27365c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, pool->cpu); 27374cb1ef64STejun Heo } 27384736cbf7SLai Jiangshan 2739640f17c8SPeter Zijlstra if (worker->rescue_wq) 27409546b29eSTejun Heo set_cpus_allowed_ptr(worker->task, pool_allowed_cpus(pool)); 2741640f17c8SPeter Zijlstra 27424736cbf7SLai Jiangshan list_add_tail(&worker->node, &pool->workers); 2743a2d812a2STejun Heo worker->pool = pool; 27444736cbf7SLai Jiangshan 27451258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 27464736cbf7SLai Jiangshan } 27474736cbf7SLai Jiangshan 27484736cbf7SLai Jiangshan /** 274960f5a4bcSLai Jiangshan * worker_detach_from_pool() - detach a worker from its pool 275060f5a4bcSLai Jiangshan * @worker: worker which is attached to its pool 275160f5a4bcSLai Jiangshan * 27524736cbf7SLai Jiangshan * Undo the attaching which had been done in worker_attach_to_pool(). The 27534736cbf7SLai Jiangshan * caller worker shouldn't access to the pool after detached except it has 27544736cbf7SLai Jiangshan * other reference to the pool. 275560f5a4bcSLai Jiangshan */ 2756a2d812a2STejun Heo static void worker_detach_from_pool(struct worker *worker) 275760f5a4bcSLai Jiangshan { 2758a2d812a2STejun Heo struct worker_pool *pool = worker->pool; 275960f5a4bcSLai Jiangshan struct completion *detach_completion = NULL; 276060f5a4bcSLai Jiangshan 27614cb1ef64STejun Heo /* there is one permanent BH worker per CPU which should never detach */ 27624cb1ef64STejun Heo WARN_ON_ONCE(pool->flags & POOL_BH); 27634cb1ef64STejun Heo 27641258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 2765a2d812a2STejun Heo 27665c25b5ffSPeter Zijlstra kthread_set_per_cpu(worker->task, -1); 2767da028469SLai Jiangshan list_del(&worker->node); 2768a2d812a2STejun Heo worker->pool = NULL; 2769a2d812a2STejun Heo 2770e02b9312SValentin Schneider if (list_empty(&pool->workers) && list_empty(&pool->dying_workers)) 277160f5a4bcSLai Jiangshan detach_completion = pool->detach_completion; 27721258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 277360f5a4bcSLai Jiangshan 2774b62c0751SLai Jiangshan /* clear leftover flags without pool->lock after it is detached */ 2775b62c0751SLai Jiangshan worker->flags &= ~(WORKER_UNBOUND | WORKER_REBOUND); 2776b62c0751SLai Jiangshan 277760f5a4bcSLai Jiangshan if (detach_completion) 277860f5a4bcSLai Jiangshan complete(detach_completion); 277960f5a4bcSLai Jiangshan } 278060f5a4bcSLai Jiangshan 278160f5a4bcSLai Jiangshan /** 2782c34056a3STejun Heo * create_worker - create a new workqueue worker 278363d95a91STejun Heo * @pool: pool the new worker will belong to 2784c34056a3STejun Heo * 2785051e1850SLai Jiangshan * Create and start a new worker which is attached to @pool. 2786c34056a3STejun Heo * 2787c34056a3STejun Heo * CONTEXT: 2788c34056a3STejun Heo * Might sleep. Does GFP_KERNEL allocations. 2789c34056a3STejun Heo * 2790d185af30SYacine Belkadi * Return: 2791c34056a3STejun Heo * Pointer to the newly created worker. 2792c34056a3STejun Heo */ 2793bc2ae0f5STejun Heo static struct worker *create_worker(struct worker_pool *pool) 2794c34056a3STejun Heo { 2795e441b56fSZhen Lei struct worker *worker; 2796e441b56fSZhen Lei int id; 27975d9c7a1eSLucy Mielke char id_buf[23]; 2798c34056a3STejun Heo 27997cda9aaeSLai Jiangshan /* ID is needed to determine kthread name */ 2800e441b56fSZhen Lei id = ida_alloc(&pool->worker_ida, GFP_KERNEL); 28013f0ea0b8SPetr Mladek if (id < 0) { 28023f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker ID: %pe\n", 28033f0ea0b8SPetr Mladek ERR_PTR(id)); 2804e441b56fSZhen Lei return NULL; 28053f0ea0b8SPetr Mladek } 2806c34056a3STejun Heo 2807f7537df5SLai Jiangshan worker = alloc_worker(pool->node); 28083f0ea0b8SPetr Mladek if (!worker) { 28093f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to allocate a worker\n"); 2810c34056a3STejun Heo goto fail; 28113f0ea0b8SPetr Mladek } 2812c34056a3STejun Heo 2813c34056a3STejun Heo worker->id = id; 2814c34056a3STejun Heo 28154cb1ef64STejun Heo if (!(pool->flags & POOL_BH)) { 281629c91e99STejun Heo if (pool->cpu >= 0) 2817e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "%d:%d%s", pool->cpu, id, 2818e3c916a4STejun Heo pool->attrs->nice < 0 ? "H" : ""); 2819f3421797STejun Heo else 2820e3c916a4STejun Heo snprintf(id_buf, sizeof(id_buf), "u%d:%d", pool->id, id); 2821e3c916a4STejun Heo 28224cb1ef64STejun Heo worker->task = kthread_create_on_node(worker_thread, worker, 28234cb1ef64STejun Heo pool->node, "kworker/%s", id_buf); 28243f0ea0b8SPetr Mladek if (IS_ERR(worker->task)) { 282560f54038SPetr Mladek if (PTR_ERR(worker->task) == -EINTR) { 282660f54038SPetr Mladek pr_err("workqueue: Interrupted when creating a worker thread \"kworker/%s\"\n", 282760f54038SPetr Mladek id_buf); 282860f54038SPetr Mladek } else { 28293f0ea0b8SPetr Mladek pr_err_once("workqueue: Failed to create a worker thread: %pe", 28303f0ea0b8SPetr Mladek worker->task); 283160f54038SPetr Mladek } 2832c34056a3STejun Heo goto fail; 28333f0ea0b8SPetr Mladek } 2834c34056a3STejun Heo 283591151228SOleg Nesterov set_user_nice(worker->task, pool->attrs->nice); 28369546b29eSTejun Heo kthread_bind_mask(worker->task, pool_allowed_cpus(pool)); 28374cb1ef64STejun Heo } 283891151228SOleg Nesterov 2839da028469SLai Jiangshan /* successful, attach the worker to the pool */ 28404736cbf7SLai Jiangshan worker_attach_to_pool(worker, pool); 2841822d8405STejun Heo 2842051e1850SLai Jiangshan /* start the newly created worker */ 2843a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 28440219a352STejun Heo 2845051e1850SLai Jiangshan worker->pool->nr_workers++; 2846051e1850SLai Jiangshan worker_enter_idle(worker); 28470219a352STejun Heo 28480219a352STejun Heo /* 28490219a352STejun Heo * @worker is waiting on a completion in kthread() and will trigger hung 28506a229b0eSTejun Heo * check if not woken up soon. As kick_pool() is noop if @pool is empty, 28516a229b0eSTejun Heo * wake it up explicitly. 28520219a352STejun Heo */ 28534cb1ef64STejun Heo if (worker->task) 2854051e1850SLai Jiangshan wake_up_process(worker->task); 28550219a352STejun Heo 2856a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 2857051e1850SLai Jiangshan 2858c34056a3STejun Heo return worker; 2859822d8405STejun Heo 2860c34056a3STejun Heo fail: 2861e441b56fSZhen Lei ida_free(&pool->worker_ida, id); 2862c34056a3STejun Heo kfree(worker); 2863c34056a3STejun Heo return NULL; 2864c34056a3STejun Heo } 2865c34056a3STejun Heo 2866793777bcSValentin Schneider static void unbind_worker(struct worker *worker) 2867793777bcSValentin Schneider { 2868793777bcSValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2869793777bcSValentin Schneider 2870793777bcSValentin Schneider kthread_set_per_cpu(worker->task, -1); 2871793777bcSValentin Schneider if (cpumask_intersects(wq_unbound_cpumask, cpu_active_mask)) 2872793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, wq_unbound_cpumask) < 0); 2873793777bcSValentin Schneider else 2874793777bcSValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, cpu_possible_mask) < 0); 2875793777bcSValentin Schneider } 2876793777bcSValentin Schneider 2877e02b9312SValentin Schneider static void wake_dying_workers(struct list_head *cull_list) 2878e02b9312SValentin Schneider { 2879e02b9312SValentin Schneider struct worker *worker, *tmp; 2880e02b9312SValentin Schneider 2881e02b9312SValentin Schneider list_for_each_entry_safe(worker, tmp, cull_list, entry) { 2882e02b9312SValentin Schneider list_del_init(&worker->entry); 2883e02b9312SValentin Schneider unbind_worker(worker); 2884e02b9312SValentin Schneider /* 2885e02b9312SValentin Schneider * If the worker was somehow already running, then it had to be 2886e02b9312SValentin Schneider * in pool->idle_list when set_worker_dying() happened or we 2887e02b9312SValentin Schneider * wouldn't have gotten here. 2888c34056a3STejun Heo * 2889e02b9312SValentin Schneider * Thus, the worker must either have observed the WORKER_DIE 2890e02b9312SValentin Schneider * flag, or have set its state to TASK_IDLE. Either way, the 2891e02b9312SValentin Schneider * below will be observed by the worker and is safe to do 2892e02b9312SValentin Schneider * outside of pool->lock. 2893e02b9312SValentin Schneider */ 2894e02b9312SValentin Schneider wake_up_process(worker->task); 2895e02b9312SValentin Schneider } 2896e02b9312SValentin Schneider } 2897e02b9312SValentin Schneider 2898e02b9312SValentin Schneider /** 2899e02b9312SValentin Schneider * set_worker_dying - Tag a worker for destruction 2900e02b9312SValentin Schneider * @worker: worker to be destroyed 2901e02b9312SValentin Schneider * @list: transfer worker away from its pool->idle_list and into list 2902e02b9312SValentin Schneider * 2903e02b9312SValentin Schneider * Tag @worker for destruction and adjust @pool stats accordingly. The worker 2904e02b9312SValentin Schneider * should be idle. 2905c8e55f36STejun Heo * 2906c8e55f36STejun Heo * CONTEXT: 2907a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 2908c34056a3STejun Heo */ 2909e02b9312SValentin Schneider static void set_worker_dying(struct worker *worker, struct list_head *list) 2910c34056a3STejun Heo { 2911bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 2912c34056a3STejun Heo 2913cd549687STejun Heo lockdep_assert_held(&pool->lock); 2914e02b9312SValentin Schneider lockdep_assert_held(&wq_pool_attach_mutex); 2915cd549687STejun Heo 2916c34056a3STejun Heo /* sanity check frenzy */ 29176183c009STejun Heo if (WARN_ON(worker->current_work) || 291873eb7fe7SLai Jiangshan WARN_ON(!list_empty(&worker->scheduled)) || 291973eb7fe7SLai Jiangshan WARN_ON(!(worker->flags & WORKER_IDLE))) 29206183c009STejun Heo return; 2921c34056a3STejun Heo 2922bd7bdd43STejun Heo pool->nr_workers--; 2923bd7bdd43STejun Heo pool->nr_idle--; 2924c8e55f36STejun Heo 2925cb444766STejun Heo worker->flags |= WORKER_DIE; 2926e02b9312SValentin Schneider 2927e02b9312SValentin Schneider list_move(&worker->entry, list); 2928e02b9312SValentin Schneider list_move(&worker->node, &pool->dying_workers); 2929c34056a3STejun Heo } 2930c34056a3STejun Heo 29313f959aa3SValentin Schneider /** 29323f959aa3SValentin Schneider * idle_worker_timeout - check if some idle workers can now be deleted. 29333f959aa3SValentin Schneider * @t: The pool's idle_timer that just expired 29343f959aa3SValentin Schneider * 29353f959aa3SValentin Schneider * The timer is armed in worker_enter_idle(). Note that it isn't disarmed in 29363f959aa3SValentin Schneider * worker_leave_idle(), as a worker flicking between idle and active while its 29373f959aa3SValentin Schneider * pool is at the too_many_workers() tipping point would cause too much timer 29383f959aa3SValentin Schneider * housekeeping overhead. Since IDLE_WORKER_TIMEOUT is long enough, we just let 29393f959aa3SValentin Schneider * it expire and re-evaluate things from there. 29403f959aa3SValentin Schneider */ 294132a6c723SKees Cook static void idle_worker_timeout(struct timer_list *t) 2942e22bee78STejun Heo { 294332a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, idle_timer); 29443f959aa3SValentin Schneider bool do_cull = false; 29453f959aa3SValentin Schneider 29463f959aa3SValentin Schneider if (work_pending(&pool->idle_cull_work)) 29473f959aa3SValentin Schneider return; 29483f959aa3SValentin Schneider 29493f959aa3SValentin Schneider raw_spin_lock_irq(&pool->lock); 29503f959aa3SValentin Schneider 29513f959aa3SValentin Schneider if (too_many_workers(pool)) { 29523f959aa3SValentin Schneider struct worker *worker; 29533f959aa3SValentin Schneider unsigned long expires; 29543f959aa3SValentin Schneider 29553f959aa3SValentin Schneider /* idle_list is kept in LIFO order, check the last one */ 29563f959aa3SValentin Schneider worker = list_entry(pool->idle_list.prev, struct worker, entry); 29573f959aa3SValentin Schneider expires = worker->last_active + IDLE_WORKER_TIMEOUT; 29583f959aa3SValentin Schneider do_cull = !time_before(jiffies, expires); 29593f959aa3SValentin Schneider 29603f959aa3SValentin Schneider if (!do_cull) 29613f959aa3SValentin Schneider mod_timer(&pool->idle_timer, expires); 29623f959aa3SValentin Schneider } 29633f959aa3SValentin Schneider raw_spin_unlock_irq(&pool->lock); 29643f959aa3SValentin Schneider 29653f959aa3SValentin Schneider if (do_cull) 29663f959aa3SValentin Schneider queue_work(system_unbound_wq, &pool->idle_cull_work); 29673f959aa3SValentin Schneider } 29683f959aa3SValentin Schneider 29693f959aa3SValentin Schneider /** 29703f959aa3SValentin Schneider * idle_cull_fn - cull workers that have been idle for too long. 29713f959aa3SValentin Schneider * @work: the pool's work for handling these idle workers 29723f959aa3SValentin Schneider * 29733f959aa3SValentin Schneider * This goes through a pool's idle workers and gets rid of those that have been 29743f959aa3SValentin Schneider * idle for at least IDLE_WORKER_TIMEOUT seconds. 2975e02b9312SValentin Schneider * 2976e02b9312SValentin Schneider * We don't want to disturb isolated CPUs because of a pcpu kworker being 2977e02b9312SValentin Schneider * culled, so this also resets worker affinity. This requires a sleepable 2978e02b9312SValentin Schneider * context, hence the split between timer callback and work item. 29793f959aa3SValentin Schneider */ 29803f959aa3SValentin Schneider static void idle_cull_fn(struct work_struct *work) 29813f959aa3SValentin Schneider { 29823f959aa3SValentin Schneider struct worker_pool *pool = container_of(work, struct worker_pool, idle_cull_work); 29839680540cSYang Yingliang LIST_HEAD(cull_list); 2984e22bee78STejun Heo 2985e02b9312SValentin Schneider /* 2986e02b9312SValentin Schneider * Grabbing wq_pool_attach_mutex here ensures an already-running worker 2987e02b9312SValentin Schneider * cannot proceed beyong worker_detach_from_pool() in its self-destruct 2988e02b9312SValentin Schneider * path. This is required as a previously-preempted worker could run after 2989e02b9312SValentin Schneider * set_worker_dying() has happened but before wake_dying_workers() did. 2990e02b9312SValentin Schneider */ 2991e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 2992a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 2993e22bee78STejun Heo 29943347fc9fSLai Jiangshan while (too_many_workers(pool)) { 2995e22bee78STejun Heo struct worker *worker; 2996e22bee78STejun Heo unsigned long expires; 2997e22bee78STejun Heo 299863d95a91STejun Heo worker = list_entry(pool->idle_list.prev, struct worker, entry); 2999e22bee78STejun Heo expires = worker->last_active + IDLE_WORKER_TIMEOUT; 3000e22bee78STejun Heo 30013347fc9fSLai Jiangshan if (time_before(jiffies, expires)) { 300263d95a91STejun Heo mod_timer(&pool->idle_timer, expires); 30033347fc9fSLai Jiangshan break; 3004e22bee78STejun Heo } 30053347fc9fSLai Jiangshan 3006e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 3007e22bee78STejun Heo } 3008e22bee78STejun Heo 3009a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3010e02b9312SValentin Schneider wake_dying_workers(&cull_list); 3011e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 3012e22bee78STejun Heo } 3013e22bee78STejun Heo 3014493a1724STejun Heo static void send_mayday(struct work_struct *work) 3015e22bee78STejun Heo { 3016112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3017112202d9STejun Heo struct workqueue_struct *wq = pwq->wq; 3018493a1724STejun Heo 30192e109a28STejun Heo lockdep_assert_held(&wq_mayday_lock); 3020e22bee78STejun Heo 3021493008a8STejun Heo if (!wq->rescuer) 3022493a1724STejun Heo return; 3023e22bee78STejun Heo 3024e22bee78STejun Heo /* mayday mayday mayday */ 3025493a1724STejun Heo if (list_empty(&pwq->mayday_node)) { 302677668c8bSLai Jiangshan /* 302777668c8bSLai Jiangshan * If @pwq is for an unbound wq, its base ref may be put at 302877668c8bSLai Jiangshan * any time due to an attribute change. Pin @pwq until the 302977668c8bSLai Jiangshan * rescuer is done with it. 303077668c8bSLai Jiangshan */ 303177668c8bSLai Jiangshan get_pwq(pwq); 3032493a1724STejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3033e22bee78STejun Heo wake_up_process(wq->rescuer->task); 3034725e8ec5STejun Heo pwq->stats[PWQ_STAT_MAYDAY]++; 3035493a1724STejun Heo } 3036e22bee78STejun Heo } 3037e22bee78STejun Heo 303832a6c723SKees Cook static void pool_mayday_timeout(struct timer_list *t) 3039e22bee78STejun Heo { 304032a6c723SKees Cook struct worker_pool *pool = from_timer(pool, t, mayday_timer); 3041e22bee78STejun Heo struct work_struct *work; 3042e22bee78STejun Heo 3043a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3044a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); /* for wq->maydays */ 3045e22bee78STejun Heo 304663d95a91STejun Heo if (need_to_create_worker(pool)) { 3047e22bee78STejun Heo /* 3048e22bee78STejun Heo * We've been trying to create a new worker but 3049e22bee78STejun Heo * haven't been successful. We might be hitting an 3050e22bee78STejun Heo * allocation deadlock. Send distress signals to 3051e22bee78STejun Heo * rescuers. 3052e22bee78STejun Heo */ 305363d95a91STejun Heo list_for_each_entry(work, &pool->worklist, entry) 3054e22bee78STejun Heo send_mayday(work); 3055e22bee78STejun Heo } 3056e22bee78STejun Heo 3057a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3058a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3059e22bee78STejun Heo 306063d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INTERVAL); 3061e22bee78STejun Heo } 3062e22bee78STejun Heo 3063e22bee78STejun Heo /** 3064e22bee78STejun Heo * maybe_create_worker - create a new worker if necessary 306563d95a91STejun Heo * @pool: pool to create a new worker for 3066e22bee78STejun Heo * 306763d95a91STejun Heo * Create a new worker for @pool if necessary. @pool is guaranteed to 3068e22bee78STejun Heo * have at least one idle worker on return from this function. If 3069e22bee78STejun Heo * creating a new worker takes longer than MAYDAY_INTERVAL, mayday is 307063d95a91STejun Heo * sent to all rescuers with works scheduled on @pool to resolve 3071e22bee78STejun Heo * possible allocation deadlock. 3072e22bee78STejun Heo * 3073c5aa87bbSTejun Heo * On return, need_to_create_worker() is guaranteed to be %false and 3074c5aa87bbSTejun Heo * may_start_working() %true. 3075e22bee78STejun Heo * 3076e22bee78STejun Heo * LOCKING: 3077a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3078e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. Called only from 3079e22bee78STejun Heo * manager. 3080e22bee78STejun Heo */ 308129187a9eSTejun Heo static void maybe_create_worker(struct worker_pool *pool) 3082d565ed63STejun Heo __releases(&pool->lock) 3083d565ed63STejun Heo __acquires(&pool->lock) 3084e22bee78STejun Heo { 3085e22bee78STejun Heo restart: 3086a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 30879f9c2364STejun Heo 3088e22bee78STejun Heo /* if we don't make progress in MAYDAY_INITIAL_TIMEOUT, call for help */ 308963d95a91STejun Heo mod_timer(&pool->mayday_timer, jiffies + MAYDAY_INITIAL_TIMEOUT); 3090e22bee78STejun Heo 3091e22bee78STejun Heo while (true) { 3092051e1850SLai Jiangshan if (create_worker(pool) || !need_to_create_worker(pool)) 3093e22bee78STejun Heo break; 3094e22bee78STejun Heo 3095e212f361SLai Jiangshan schedule_timeout_interruptible(CREATE_COOLDOWN); 30969f9c2364STejun Heo 309763d95a91STejun Heo if (!need_to_create_worker(pool)) 3098e22bee78STejun Heo break; 3099e22bee78STejun Heo } 3100e22bee78STejun Heo 310163d95a91STejun Heo del_timer_sync(&pool->mayday_timer); 3102a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3103051e1850SLai Jiangshan /* 3104051e1850SLai Jiangshan * This is necessary even after a new worker was just successfully 3105051e1850SLai Jiangshan * created as @pool->lock was dropped and the new worker might have 3106051e1850SLai Jiangshan * already become busy. 3107051e1850SLai Jiangshan */ 310863d95a91STejun Heo if (need_to_create_worker(pool)) 3109e22bee78STejun Heo goto restart; 3110e22bee78STejun Heo } 3111e22bee78STejun Heo 3112e22bee78STejun Heo /** 3113e22bee78STejun Heo * manage_workers - manage worker pool 3114e22bee78STejun Heo * @worker: self 3115e22bee78STejun Heo * 3116706026c2STejun Heo * Assume the manager role and manage the worker pool @worker belongs 3117e22bee78STejun Heo * to. At any given time, there can be only zero or one manager per 3118706026c2STejun Heo * pool. The exclusion is handled automatically by this function. 3119e22bee78STejun Heo * 3120e22bee78STejun Heo * The caller can safely start processing works on false return. On 3121e22bee78STejun Heo * true return, it's guaranteed that need_to_create_worker() is false 3122e22bee78STejun Heo * and may_start_working() is true. 3123e22bee78STejun Heo * 3124e22bee78STejun Heo * CONTEXT: 3125a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3126e22bee78STejun Heo * multiple times. Does GFP_KERNEL allocations. 3127e22bee78STejun Heo * 3128d185af30SYacine Belkadi * Return: 312929187a9eSTejun Heo * %false if the pool doesn't need management and the caller can safely 313029187a9eSTejun Heo * start processing works, %true if management function was performed and 313129187a9eSTejun Heo * the conditions that the caller verified before calling the function may 313229187a9eSTejun Heo * no longer be true. 3133e22bee78STejun Heo */ 3134e22bee78STejun Heo static bool manage_workers(struct worker *worker) 3135e22bee78STejun Heo { 313663d95a91STejun Heo struct worker_pool *pool = worker->pool; 3137e22bee78STejun Heo 3138692b4825STejun Heo if (pool->flags & POOL_MANAGER_ACTIVE) 313929187a9eSTejun Heo return false; 3140692b4825STejun Heo 3141692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 31422607d7a6STejun Heo pool->manager = worker; 3143e22bee78STejun Heo 314429187a9eSTejun Heo maybe_create_worker(pool); 3145e22bee78STejun Heo 31462607d7a6STejun Heo pool->manager = NULL; 3147692b4825STejun Heo pool->flags &= ~POOL_MANAGER_ACTIVE; 3148d8bb65abSSebastian Andrzej Siewior rcuwait_wake_up(&manager_wait); 314929187a9eSTejun Heo return true; 3150e22bee78STejun Heo } 3151e22bee78STejun Heo 3152a62428c0STejun Heo /** 3153a62428c0STejun Heo * process_one_work - process single work 3154c34056a3STejun Heo * @worker: self 3155a62428c0STejun Heo * @work: work to process 3156a62428c0STejun Heo * 3157a62428c0STejun Heo * Process @work. This function contains all the logics necessary to 3158a62428c0STejun Heo * process a single work including synchronization against and 3159a62428c0STejun Heo * interaction with other workers on the same cpu, queueing and 3160a62428c0STejun Heo * flushing. As long as context requirement is met, any worker can 3161a62428c0STejun Heo * call this function to process a work. 3162a62428c0STejun Heo * 3163a62428c0STejun Heo * CONTEXT: 3164a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which is released and regrabbed. 3165a62428c0STejun Heo */ 3166c34056a3STejun Heo static void process_one_work(struct worker *worker, struct work_struct *work) 3167d565ed63STejun Heo __releases(&pool->lock) 3168d565ed63STejun Heo __acquires(&pool->lock) 31691da177e4SLinus Torvalds { 3170112202d9STejun Heo struct pool_workqueue *pwq = get_work_pwq(work); 3171bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 3172c4560c2cSLai Jiangshan unsigned long work_data; 3173c35aea39STejun Heo int lockdep_start_depth, rcu_start_depth; 31741acd92d9STejun Heo bool bh_draining = pool->flags & POOL_BH_DRAINING; 31754e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP 31764e6045f1SJohannes Berg /* 3177a62428c0STejun Heo * It is permissible to free the struct work_struct from 3178a62428c0STejun Heo * inside the function that is called from it, this we need to 3179a62428c0STejun Heo * take into account for lockdep too. To avoid bogus "held 3180a62428c0STejun Heo * lock freed" warnings as well as problems when looking into 3181a62428c0STejun Heo * work->lockdep_map, make a copy and use that here. 31824e6045f1SJohannes Berg */ 31834d82a1deSPeter Zijlstra struct lockdep_map lockdep_map; 31844d82a1deSPeter Zijlstra 31854d82a1deSPeter Zijlstra lockdep_copy_map(&lockdep_map, &work->lockdep_map); 31864e6045f1SJohannes Berg #endif 3187807407c0SLai Jiangshan /* ensure we're on the correct CPU */ 318885327af6SLai Jiangshan WARN_ON_ONCE(!(pool->flags & POOL_DISASSOCIATED) && 3189ec22ca5eSTejun Heo raw_smp_processor_id() != pool->cpu); 319025511a47STejun Heo 31918930cabaSTejun Heo /* claim and dequeue */ 3192dc186ad7SThomas Gleixner debug_work_deactivate(work); 3193c9e7cf27STejun Heo hash_add(pool->busy_hash, &worker->hentry, (unsigned long)work); 3194c34056a3STejun Heo worker->current_work = work; 3195a2c1c57bSTejun Heo worker->current_func = work->func; 3196112202d9STejun Heo worker->current_pwq = pwq; 31974cb1ef64STejun Heo if (worker->task) 3198616db877STejun Heo worker->current_at = worker->task->se.sum_exec_runtime; 3199c4560c2cSLai Jiangshan work_data = *work_data_bits(work); 3200d812796eSLai Jiangshan worker->current_color = get_work_color(work_data); 32017a22ad75STejun Heo 32028bf89593STejun Heo /* 32038bf89593STejun Heo * Record wq name for cmdline and debug reporting, may get 32048bf89593STejun Heo * overridden through set_worker_desc(). 32058bf89593STejun Heo */ 32068bf89593STejun Heo strscpy(worker->desc, pwq->wq->name, WORKER_DESC_LEN); 32078bf89593STejun Heo 3208a62428c0STejun Heo list_del_init(&work->entry); 3209a62428c0STejun Heo 3210649027d7STejun Heo /* 3211228f1d00SLai Jiangshan * CPU intensive works don't participate in concurrency management. 3212228f1d00SLai Jiangshan * They're the scheduler's responsibility. This takes @worker out 3213228f1d00SLai Jiangshan * of concurrency management and the next code block will chain 3214228f1d00SLai Jiangshan * execution of the pending work items. 3215fb0e7bebSTejun Heo */ 3216616db877STejun Heo if (unlikely(pwq->wq->flags & WQ_CPU_INTENSIVE)) 3217228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_CPU_INTENSIVE); 3218fb0e7bebSTejun Heo 3219974271c4STejun Heo /* 32200219a352STejun Heo * Kick @pool if necessary. It's always noop for per-cpu worker pools 32210219a352STejun Heo * since nr_running would always be >= 1 at this point. This is used to 32220219a352STejun Heo * chain execution of the pending work items for WORKER_NOT_RUNNING 32230219a352STejun Heo * workers such as the UNBOUND and CPU_INTENSIVE ones. 3224974271c4STejun Heo */ 32250219a352STejun Heo kick_pool(pool); 3226974271c4STejun Heo 32278930cabaSTejun Heo /* 32287c3eed5cSTejun Heo * Record the last pool and clear PENDING which should be the last 3229d565ed63STejun Heo * update to @work. Also, do this inside @pool->lock so that 323023657bb1STejun Heo * PENDING and queued state changes happen together while IRQ is 323123657bb1STejun Heo * disabled. 32328930cabaSTejun Heo */ 3233bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, pool->id, 0); 32341da177e4SLinus Torvalds 3235fe48ba7dSMirsad Goran Todorovac pwq->stats[PWQ_STAT_STARTED]++; 3236a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3237365970a1SDavid Howells 3238c35aea39STejun Heo rcu_start_depth = rcu_preempt_depth(); 3239c35aea39STejun Heo lockdep_start_depth = lockdep_depth(current); 32401acd92d9STejun Heo /* see drain_dead_softirq_workfn() */ 32411acd92d9STejun Heo if (!bh_draining) 3242a1d14934SPeter Zijlstra lock_map_acquire(&pwq->wq->lockdep_map); 32433295f0efSIngo Molnar lock_map_acquire(&lockdep_map); 3244e6f3faa7SPeter Zijlstra /* 3245f52be570SPeter Zijlstra * Strictly speaking we should mark the invariant state without holding 3246f52be570SPeter Zijlstra * any locks, that is, before these two lock_map_acquire()'s. 3247e6f3faa7SPeter Zijlstra * 3248e6f3faa7SPeter Zijlstra * However, that would result in: 3249e6f3faa7SPeter Zijlstra * 3250e6f3faa7SPeter Zijlstra * A(W1) 3251e6f3faa7SPeter Zijlstra * WFC(C) 3252e6f3faa7SPeter Zijlstra * A(W1) 3253e6f3faa7SPeter Zijlstra * C(C) 3254e6f3faa7SPeter Zijlstra * 3255e6f3faa7SPeter Zijlstra * Which would create W1->C->W1 dependencies, even though there is no 3256e6f3faa7SPeter Zijlstra * actual deadlock possible. There are two solutions, using a 3257e6f3faa7SPeter Zijlstra * read-recursive acquire on the work(queue) 'locks', but this will then 3258f52be570SPeter Zijlstra * hit the lockdep limitation on recursive locks, or simply discard 3259e6f3faa7SPeter Zijlstra * these locks. 3260e6f3faa7SPeter Zijlstra * 3261e6f3faa7SPeter Zijlstra * AFAICT there is no possible deadlock scenario between the 3262e6f3faa7SPeter Zijlstra * flush_work() and complete() primitives (except for single-threaded 3263e6f3faa7SPeter Zijlstra * workqueues), so hiding them isn't a problem. 3264e6f3faa7SPeter Zijlstra */ 3265f52be570SPeter Zijlstra lockdep_invariant_state(true); 3266e36c886aSArjan van de Ven trace_workqueue_execute_start(work); 3267a2c1c57bSTejun Heo worker->current_func(work); 3268e36c886aSArjan van de Ven /* 3269e36c886aSArjan van de Ven * While we must be careful to not use "work" after this, the trace 3270e36c886aSArjan van de Ven * point will only record its address. 3271e36c886aSArjan van de Ven */ 32721c5da0ecSDaniel Jordan trace_workqueue_execute_end(work, worker->current_func); 3273725e8ec5STejun Heo pwq->stats[PWQ_STAT_COMPLETED]++; 32743295f0efSIngo Molnar lock_map_release(&lockdep_map); 32751acd92d9STejun Heo if (!bh_draining) 3276112202d9STejun Heo lock_map_release(&pwq->wq->lockdep_map); 32771da177e4SLinus Torvalds 3278c35aea39STejun Heo if (unlikely((worker->task && in_atomic()) || 3279c35aea39STejun Heo lockdep_depth(current) != lockdep_start_depth || 3280c35aea39STejun Heo rcu_preempt_depth() != rcu_start_depth)) { 3281c35aea39STejun Heo pr_err("BUG: workqueue leaked atomic, lock or RCU: %s[%d]\n" 3282c35aea39STejun Heo " preempt=0x%08x lock=%d->%d RCU=%d->%d workfn=%ps\n", 3283c35aea39STejun Heo current->comm, task_pid_nr(current), preempt_count(), 3284c35aea39STejun Heo lockdep_start_depth, lockdep_depth(current), 3285c35aea39STejun Heo rcu_start_depth, rcu_preempt_depth(), 3286c35aea39STejun Heo worker->current_func); 3287d5abe669SPeter Zijlstra debug_show_held_locks(current); 3288d5abe669SPeter Zijlstra dump_stack(); 3289d5abe669SPeter Zijlstra } 3290d5abe669SPeter Zijlstra 3291b22ce278STejun Heo /* 3292025f50f3SSebastian Andrzej Siewior * The following prevents a kworker from hogging CPU on !PREEMPTION 3293b22ce278STejun Heo * kernels, where a requeueing work item waiting for something to 3294b22ce278STejun Heo * happen could deadlock with stop_machine as such work item could 3295b22ce278STejun Heo * indefinitely requeue itself while all other CPUs are trapped in 3296789cbbecSJoe Lawrence * stop_machine. At the same time, report a quiescent RCU state so 3297789cbbecSJoe Lawrence * the same condition doesn't freeze RCU. 3298b22ce278STejun Heo */ 32994cb1ef64STejun Heo if (worker->task) 3300a7e6425eSPaul E. McKenney cond_resched(); 3301b22ce278STejun Heo 3302a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3303a62428c0STejun Heo 3304616db877STejun Heo /* 3305616db877STejun Heo * In addition to %WQ_CPU_INTENSIVE, @worker may also have been marked 3306616db877STejun Heo * CPU intensive by wq_worker_tick() if @work hogged CPU longer than 3307616db877STejun Heo * wq_cpu_intensive_thresh_us. Clear it. 3308616db877STejun Heo */ 3309fb0e7bebSTejun Heo worker_clr_flags(worker, WORKER_CPU_INTENSIVE); 3310fb0e7bebSTejun Heo 33111b69ac6bSJohannes Weiner /* tag the worker for identification in schedule() */ 33121b69ac6bSJohannes Weiner worker->last_func = worker->current_func; 33131b69ac6bSJohannes Weiner 3314a62428c0STejun Heo /* we're done with it, release */ 331542f8570fSSasha Levin hash_del(&worker->hentry); 3316c34056a3STejun Heo worker->current_work = NULL; 3317a2c1c57bSTejun Heo worker->current_func = NULL; 3318112202d9STejun Heo worker->current_pwq = NULL; 3319d812796eSLai Jiangshan worker->current_color = INT_MAX; 3320dd6c3c54STejun Heo 3321dd6c3c54STejun Heo /* must be the last step, see the function comment */ 3322c4560c2cSLai Jiangshan pwq_dec_nr_in_flight(pwq, work_data); 33231da177e4SLinus Torvalds } 33241da177e4SLinus Torvalds 3325affee4b2STejun Heo /** 3326affee4b2STejun Heo * process_scheduled_works - process scheduled works 3327affee4b2STejun Heo * @worker: self 3328affee4b2STejun Heo * 3329affee4b2STejun Heo * Process all scheduled works. Please note that the scheduled list 3330affee4b2STejun Heo * may change while processing a work, so this function repeatedly 3331affee4b2STejun Heo * fetches a work from the top and executes it. 3332affee4b2STejun Heo * 3333affee4b2STejun Heo * CONTEXT: 3334a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock) which may be released and regrabbed 3335affee4b2STejun Heo * multiple times. 3336affee4b2STejun Heo */ 3337affee4b2STejun Heo static void process_scheduled_works(struct worker *worker) 33381da177e4SLinus Torvalds { 3339c0ab017dSTejun Heo struct work_struct *work; 3340c0ab017dSTejun Heo bool first = true; 3341c0ab017dSTejun Heo 3342c0ab017dSTejun Heo while ((work = list_first_entry_or_null(&worker->scheduled, 3343c0ab017dSTejun Heo struct work_struct, entry))) { 3344c0ab017dSTejun Heo if (first) { 3345c0ab017dSTejun Heo worker->pool->watchdog_ts = jiffies; 3346c0ab017dSTejun Heo first = false; 3347c0ab017dSTejun Heo } 3348c34056a3STejun Heo process_one_work(worker, work); 3349a62428c0STejun Heo } 33501da177e4SLinus Torvalds } 33511da177e4SLinus Torvalds 3352197f6accSTejun Heo static void set_pf_worker(bool val) 3353197f6accSTejun Heo { 3354197f6accSTejun Heo mutex_lock(&wq_pool_attach_mutex); 3355197f6accSTejun Heo if (val) 3356197f6accSTejun Heo current->flags |= PF_WQ_WORKER; 3357197f6accSTejun Heo else 3358197f6accSTejun Heo current->flags &= ~PF_WQ_WORKER; 3359197f6accSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 3360197f6accSTejun Heo } 3361197f6accSTejun Heo 33624690c4abSTejun Heo /** 33634690c4abSTejun Heo * worker_thread - the worker thread function 3364c34056a3STejun Heo * @__worker: self 33654690c4abSTejun Heo * 3366c5aa87bbSTejun Heo * The worker thread function. All workers belong to a worker_pool - 3367c5aa87bbSTejun Heo * either a per-cpu one or dynamic unbound one. These workers process all 3368c5aa87bbSTejun Heo * work items regardless of their specific target workqueue. The only 3369c5aa87bbSTejun Heo * exception is work items which belong to workqueues with a rescuer which 3370c5aa87bbSTejun Heo * will be explained in rescuer_thread(). 3371d185af30SYacine Belkadi * 3372d185af30SYacine Belkadi * Return: 0 33734690c4abSTejun Heo */ 3374c34056a3STejun Heo static int worker_thread(void *__worker) 33751da177e4SLinus Torvalds { 3376c34056a3STejun Heo struct worker *worker = __worker; 3377bd7bdd43STejun Heo struct worker_pool *pool = worker->pool; 33781da177e4SLinus Torvalds 3379e22bee78STejun Heo /* tell the scheduler that this is a workqueue worker */ 3380197f6accSTejun Heo set_pf_worker(true); 3381c8e55f36STejun Heo woke_up: 3382a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3383affee4b2STejun Heo 3384a9ab775bSTejun Heo /* am I supposed to die? */ 3385a9ab775bSTejun Heo if (unlikely(worker->flags & WORKER_DIE)) { 3386a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 3387197f6accSTejun Heo set_pf_worker(false); 338860f5a4bcSLai Jiangshan 338960f5a4bcSLai Jiangshan set_task_comm(worker->task, "kworker/dying"); 3390e441b56fSZhen Lei ida_free(&pool->worker_ida, worker->id); 3391a2d812a2STejun Heo worker_detach_from_pool(worker); 3392e02b9312SValentin Schneider WARN_ON_ONCE(!list_empty(&worker->entry)); 339360f5a4bcSLai Jiangshan kfree(worker); 3394c8e55f36STejun Heo return 0; 3395c8e55f36STejun Heo } 3396c8e55f36STejun Heo 3397c8e55f36STejun Heo worker_leave_idle(worker); 3398db7bccf4STejun Heo recheck: 3399e22bee78STejun Heo /* no more worker necessary? */ 340063d95a91STejun Heo if (!need_more_worker(pool)) 3401e22bee78STejun Heo goto sleep; 3402e22bee78STejun Heo 3403e22bee78STejun Heo /* do we need to manage? */ 340463d95a91STejun Heo if (unlikely(!may_start_working(pool)) && manage_workers(worker)) 3405e22bee78STejun Heo goto recheck; 3406e22bee78STejun Heo 3407c8e55f36STejun Heo /* 3408c8e55f36STejun Heo * ->scheduled list can only be filled while a worker is 3409c8e55f36STejun Heo * preparing to process a work or actually processing it. 3410c8e55f36STejun Heo * Make sure nobody diddled with it while I was sleeping. 3411c8e55f36STejun Heo */ 34126183c009STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 3413c8e55f36STejun Heo 3414e22bee78STejun Heo /* 3415a9ab775bSTejun Heo * Finish PREP stage. We're guaranteed to have at least one idle 3416a9ab775bSTejun Heo * worker or that someone else has already assumed the manager 3417a9ab775bSTejun Heo * role. This is where @worker starts participating in concurrency 3418a9ab775bSTejun Heo * management if applicable and concurrency management is restored 3419a9ab775bSTejun Heo * after being rebound. See rebind_workers() for details. 3420e22bee78STejun Heo */ 3421a9ab775bSTejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 3422e22bee78STejun Heo 3423e22bee78STejun Heo do { 3424affee4b2STejun Heo struct work_struct *work = 3425bd7bdd43STejun Heo list_first_entry(&pool->worklist, 3426affee4b2STejun Heo struct work_struct, entry); 3427affee4b2STejun Heo 3428873eaca6STejun Heo if (assign_work(work, worker, NULL)) 3429affee4b2STejun Heo process_scheduled_works(worker); 343063d95a91STejun Heo } while (keep_working(pool)); 3431affee4b2STejun Heo 3432228f1d00SLai Jiangshan worker_set_flags(worker, WORKER_PREP); 3433d313dd85STejun Heo sleep: 3434c8e55f36STejun Heo /* 3435d565ed63STejun Heo * pool->lock is held and there's no work to process and no need to 3436d565ed63STejun Heo * manage, sleep. Workers are woken up only while holding 3437d565ed63STejun Heo * pool->lock or from local cpu, so setting the current state 3438d565ed63STejun Heo * before releasing pool->lock is enough to prevent losing any 3439d565ed63STejun Heo * event. 3440c8e55f36STejun Heo */ 3441c8e55f36STejun Heo worker_enter_idle(worker); 3442c5a94a61SPeter Zijlstra __set_current_state(TASK_IDLE); 3443a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 34441da177e4SLinus Torvalds schedule(); 3445c8e55f36STejun Heo goto woke_up; 34461da177e4SLinus Torvalds } 34471da177e4SLinus Torvalds 3448e22bee78STejun Heo /** 3449e22bee78STejun Heo * rescuer_thread - the rescuer thread function 3450111c225aSTejun Heo * @__rescuer: self 3451e22bee78STejun Heo * 3452e22bee78STejun Heo * Workqueue rescuer thread function. There's one rescuer for each 3453493008a8STejun Heo * workqueue which has WQ_MEM_RECLAIM set. 3454e22bee78STejun Heo * 3455706026c2STejun Heo * Regular work processing on a pool may block trying to create a new 3456e22bee78STejun Heo * worker which uses GFP_KERNEL allocation which has slight chance of 3457e22bee78STejun Heo * developing into deadlock if some works currently on the same queue 3458e22bee78STejun Heo * need to be processed to satisfy the GFP_KERNEL allocation. This is 3459e22bee78STejun Heo * the problem rescuer solves. 3460e22bee78STejun Heo * 3461706026c2STejun Heo * When such condition is possible, the pool summons rescuers of all 3462706026c2STejun Heo * workqueues which have works queued on the pool and let them process 3463e22bee78STejun Heo * those works so that forward progress can be guaranteed. 3464e22bee78STejun Heo * 3465e22bee78STejun Heo * This should happen rarely. 3466d185af30SYacine Belkadi * 3467d185af30SYacine Belkadi * Return: 0 3468e22bee78STejun Heo */ 3469111c225aSTejun Heo static int rescuer_thread(void *__rescuer) 3470e22bee78STejun Heo { 3471111c225aSTejun Heo struct worker *rescuer = __rescuer; 3472111c225aSTejun Heo struct workqueue_struct *wq = rescuer->rescue_wq; 34734d595b86SLai Jiangshan bool should_stop; 3474e22bee78STejun Heo 3475e22bee78STejun Heo set_user_nice(current, RESCUER_NICE_LEVEL); 3476111c225aSTejun Heo 3477111c225aSTejun Heo /* 3478111c225aSTejun Heo * Mark rescuer as worker too. As WORKER_PREP is never cleared, it 3479111c225aSTejun Heo * doesn't participate in concurrency management. 3480111c225aSTejun Heo */ 3481197f6accSTejun Heo set_pf_worker(true); 3482e22bee78STejun Heo repeat: 3483c5a94a61SPeter Zijlstra set_current_state(TASK_IDLE); 34841da177e4SLinus Torvalds 34854d595b86SLai Jiangshan /* 34864d595b86SLai Jiangshan * By the time the rescuer is requested to stop, the workqueue 34874d595b86SLai Jiangshan * shouldn't have any work pending, but @wq->maydays may still have 34884d595b86SLai Jiangshan * pwq(s) queued. This can happen by non-rescuer workers consuming 34894d595b86SLai Jiangshan * all the work items before the rescuer got to them. Go through 34904d595b86SLai Jiangshan * @wq->maydays processing before acting on should_stop so that the 34914d595b86SLai Jiangshan * list is always empty on exit. 34924d595b86SLai Jiangshan */ 34934d595b86SLai Jiangshan should_stop = kthread_should_stop(); 34941da177e4SLinus Torvalds 3495493a1724STejun Heo /* see whether any pwq is asking for help */ 3496a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 3497493a1724STejun Heo 3498493a1724STejun Heo while (!list_empty(&wq->maydays)) { 3499493a1724STejun Heo struct pool_workqueue *pwq = list_first_entry(&wq->maydays, 3500493a1724STejun Heo struct pool_workqueue, mayday_node); 3501112202d9STejun Heo struct worker_pool *pool = pwq->pool; 3502e22bee78STejun Heo struct work_struct *work, *n; 3503e22bee78STejun Heo 3504e22bee78STejun Heo __set_current_state(TASK_RUNNING); 3505493a1724STejun Heo list_del_init(&pwq->mayday_node); 3506493a1724STejun Heo 3507a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3508e22bee78STejun Heo 350951697d39SLai Jiangshan worker_attach_to_pool(rescuer, pool); 351051697d39SLai Jiangshan 3511a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 3512e22bee78STejun Heo 3513e22bee78STejun Heo /* 3514e22bee78STejun Heo * Slurp in all works issued via this workqueue and 3515e22bee78STejun Heo * process'em. 3516e22bee78STejun Heo */ 3517873eaca6STejun Heo WARN_ON_ONCE(!list_empty(&rescuer->scheduled)); 351882607adcSTejun Heo list_for_each_entry_safe(work, n, &pool->worklist, entry) { 3519873eaca6STejun Heo if (get_work_pwq(work) == pwq && 3520873eaca6STejun Heo assign_work(work, rescuer, &n)) 3521725e8ec5STejun Heo pwq->stats[PWQ_STAT_RESCUED]++; 352282607adcSTejun Heo } 3523e22bee78STejun Heo 3524873eaca6STejun Heo if (!list_empty(&rescuer->scheduled)) { 3525e22bee78STejun Heo process_scheduled_works(rescuer); 35267576958aSTejun Heo 35277576958aSTejun Heo /* 3528008847f6SNeilBrown * The above execution of rescued work items could 3529008847f6SNeilBrown * have created more to rescue through 3530f97a4a1aSLai Jiangshan * pwq_activate_first_inactive() or chained 3531008847f6SNeilBrown * queueing. Let's put @pwq back on mayday list so 3532008847f6SNeilBrown * that such back-to-back work items, which may be 3533008847f6SNeilBrown * being used to relieve memory pressure, don't 3534008847f6SNeilBrown * incur MAYDAY_INTERVAL delay inbetween. 3535008847f6SNeilBrown */ 35364f3f4cf3SLai Jiangshan if (pwq->nr_active && need_to_create_worker(pool)) { 3537a9b8a985SSebastian Andrzej Siewior raw_spin_lock(&wq_mayday_lock); 3538e66b39afSTejun Heo /* 3539e66b39afSTejun Heo * Queue iff we aren't racing destruction 3540e66b39afSTejun Heo * and somebody else hasn't queued it already. 3541e66b39afSTejun Heo */ 3542e66b39afSTejun Heo if (wq->rescuer && list_empty(&pwq->mayday_node)) { 3543008847f6SNeilBrown get_pwq(pwq); 3544e66b39afSTejun Heo list_add_tail(&pwq->mayday_node, &wq->maydays); 3545e66b39afSTejun Heo } 3546a9b8a985SSebastian Andrzej Siewior raw_spin_unlock(&wq_mayday_lock); 3547008847f6SNeilBrown } 3548008847f6SNeilBrown } 3549008847f6SNeilBrown 3550008847f6SNeilBrown /* 355177668c8bSLai Jiangshan * Put the reference grabbed by send_mayday(). @pool won't 355213b1d625SLai Jiangshan * go away while we're still attached to it. 355377668c8bSLai Jiangshan */ 355477668c8bSLai Jiangshan put_pwq(pwq); 355577668c8bSLai Jiangshan 355677668c8bSLai Jiangshan /* 35570219a352STejun Heo * Leave this pool. Notify regular workers; otherwise, we end up 35580219a352STejun Heo * with 0 concurrency and stalling the execution. 35597576958aSTejun Heo */ 35600219a352STejun Heo kick_pool(pool); 35617576958aSTejun Heo 3562a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 356313b1d625SLai Jiangshan 3564a2d812a2STejun Heo worker_detach_from_pool(rescuer); 356513b1d625SLai Jiangshan 3566a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 35671da177e4SLinus Torvalds } 35681da177e4SLinus Torvalds 3569a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 3570493a1724STejun Heo 35714d595b86SLai Jiangshan if (should_stop) { 35724d595b86SLai Jiangshan __set_current_state(TASK_RUNNING); 3573197f6accSTejun Heo set_pf_worker(false); 35744d595b86SLai Jiangshan return 0; 35754d595b86SLai Jiangshan } 35764d595b86SLai Jiangshan 3577111c225aSTejun Heo /* rescuers should never participate in concurrency management */ 3578111c225aSTejun Heo WARN_ON_ONCE(!(rescuer->flags & WORKER_NOT_RUNNING)); 3579e22bee78STejun Heo schedule(); 3580e22bee78STejun Heo goto repeat; 35811da177e4SLinus Torvalds } 35821da177e4SLinus Torvalds 35834cb1ef64STejun Heo static void bh_worker(struct worker *worker) 35844cb1ef64STejun Heo { 35854cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 35864cb1ef64STejun Heo int nr_restarts = BH_WORKER_RESTARTS; 35874cb1ef64STejun Heo unsigned long end = jiffies + BH_WORKER_JIFFIES; 35884cb1ef64STejun Heo 35894cb1ef64STejun Heo raw_spin_lock_irq(&pool->lock); 35904cb1ef64STejun Heo worker_leave_idle(worker); 35914cb1ef64STejun Heo 35924cb1ef64STejun Heo /* 35934cb1ef64STejun Heo * This function follows the structure of worker_thread(). See there for 35944cb1ef64STejun Heo * explanations on each step. 35954cb1ef64STejun Heo */ 35964cb1ef64STejun Heo if (!need_more_worker(pool)) 35974cb1ef64STejun Heo goto done; 35984cb1ef64STejun Heo 35994cb1ef64STejun Heo WARN_ON_ONCE(!list_empty(&worker->scheduled)); 36004cb1ef64STejun Heo worker_clr_flags(worker, WORKER_PREP | WORKER_REBOUND); 36014cb1ef64STejun Heo 36024cb1ef64STejun Heo do { 36034cb1ef64STejun Heo struct work_struct *work = 36044cb1ef64STejun Heo list_first_entry(&pool->worklist, 36054cb1ef64STejun Heo struct work_struct, entry); 36064cb1ef64STejun Heo 36074cb1ef64STejun Heo if (assign_work(work, worker, NULL)) 36084cb1ef64STejun Heo process_scheduled_works(worker); 36094cb1ef64STejun Heo } while (keep_working(pool) && 36104cb1ef64STejun Heo --nr_restarts && time_before(jiffies, end)); 36114cb1ef64STejun Heo 36124cb1ef64STejun Heo worker_set_flags(worker, WORKER_PREP); 36134cb1ef64STejun Heo done: 36144cb1ef64STejun Heo worker_enter_idle(worker); 36154cb1ef64STejun Heo kick_pool(pool); 36164cb1ef64STejun Heo raw_spin_unlock_irq(&pool->lock); 36174cb1ef64STejun Heo } 36184cb1ef64STejun Heo 36194cb1ef64STejun Heo /* 36204cb1ef64STejun Heo * TODO: Convert all tasklet users to workqueue and use softirq directly. 36214cb1ef64STejun Heo * 36224cb1ef64STejun Heo * This is currently called from tasklet[_hi]action() and thus is also called 36234cb1ef64STejun Heo * whenever there are tasklets to run. Let's do an early exit if there's nothing 36244cb1ef64STejun Heo * queued. Once conversion from tasklet is complete, the need_more_worker() test 36254cb1ef64STejun Heo * can be dropped. 36264cb1ef64STejun Heo * 36274cb1ef64STejun Heo * After full conversion, we'll add worker->softirq_action, directly use the 36284cb1ef64STejun Heo * softirq action and obtain the worker pointer from the softirq_action pointer. 36294cb1ef64STejun Heo */ 36304cb1ef64STejun Heo void workqueue_softirq_action(bool highpri) 36314cb1ef64STejun Heo { 36324cb1ef64STejun Heo struct worker_pool *pool = 36334cb1ef64STejun Heo &per_cpu(bh_worker_pools, smp_processor_id())[highpri]; 36344cb1ef64STejun Heo if (need_more_worker(pool)) 36354cb1ef64STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36364cb1ef64STejun Heo } 36374cb1ef64STejun Heo 36381acd92d9STejun Heo struct wq_drain_dead_softirq_work { 36391acd92d9STejun Heo struct work_struct work; 36401acd92d9STejun Heo struct worker_pool *pool; 36411acd92d9STejun Heo struct completion done; 36421acd92d9STejun Heo }; 36431acd92d9STejun Heo 36441acd92d9STejun Heo static void drain_dead_softirq_workfn(struct work_struct *work) 36451acd92d9STejun Heo { 36461acd92d9STejun Heo struct wq_drain_dead_softirq_work *dead_work = 36471acd92d9STejun Heo container_of(work, struct wq_drain_dead_softirq_work, work); 36481acd92d9STejun Heo struct worker_pool *pool = dead_work->pool; 36491acd92d9STejun Heo bool repeat; 36501acd92d9STejun Heo 36511acd92d9STejun Heo /* 36521acd92d9STejun Heo * @pool's CPU is dead and we want to execute its still pending work 36531acd92d9STejun Heo * items from this BH work item which is running on a different CPU. As 36541acd92d9STejun Heo * its CPU is dead, @pool can't be kicked and, as work execution path 36551acd92d9STejun Heo * will be nested, a lockdep annotation needs to be suppressed. Mark 36561acd92d9STejun Heo * @pool with %POOL_BH_DRAINING for the special treatments. 36571acd92d9STejun Heo */ 36581acd92d9STejun Heo raw_spin_lock_irq(&pool->lock); 36591acd92d9STejun Heo pool->flags |= POOL_BH_DRAINING; 36601acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock); 36611acd92d9STejun Heo 36621acd92d9STejun Heo bh_worker(list_first_entry(&pool->workers, struct worker, node)); 36631acd92d9STejun Heo 36641acd92d9STejun Heo raw_spin_lock_irq(&pool->lock); 36651acd92d9STejun Heo pool->flags &= ~POOL_BH_DRAINING; 36661acd92d9STejun Heo repeat = need_more_worker(pool); 36671acd92d9STejun Heo raw_spin_unlock_irq(&pool->lock); 36681acd92d9STejun Heo 36691acd92d9STejun Heo /* 36701acd92d9STejun Heo * bh_worker() might hit consecutive execution limit and bail. If there 36711acd92d9STejun Heo * still are pending work items, reschedule self and return so that we 36721acd92d9STejun Heo * don't hog this CPU's BH. 36731acd92d9STejun Heo */ 36741acd92d9STejun Heo if (repeat) { 36751acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 36761acd92d9STejun Heo queue_work(system_bh_highpri_wq, work); 36771acd92d9STejun Heo else 36781acd92d9STejun Heo queue_work(system_bh_wq, work); 36791acd92d9STejun Heo } else { 36801acd92d9STejun Heo complete(&dead_work->done); 36811acd92d9STejun Heo } 36821acd92d9STejun Heo } 36831acd92d9STejun Heo 36841acd92d9STejun Heo /* 36851acd92d9STejun Heo * @cpu is dead. Drain the remaining BH work items on the current CPU. It's 36861acd92d9STejun Heo * possible to allocate dead_work per CPU and avoid flushing. However, then we 36871acd92d9STejun Heo * have to worry about draining overlapping with CPU coming back online or 36881acd92d9STejun Heo * nesting (one CPU's dead_work queued on another CPU which is also dead and so 36891acd92d9STejun Heo * on). Let's keep it simple and drain them synchronously. These are BH work 36901acd92d9STejun Heo * items which shouldn't be requeued on the same pool. Shouldn't take long. 36911acd92d9STejun Heo */ 36921acd92d9STejun Heo void workqueue_softirq_dead(unsigned int cpu) 36931acd92d9STejun Heo { 36941acd92d9STejun Heo int i; 36951acd92d9STejun Heo 36961acd92d9STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 36971acd92d9STejun Heo struct worker_pool *pool = &per_cpu(bh_worker_pools, cpu)[i]; 36981acd92d9STejun Heo struct wq_drain_dead_softirq_work dead_work; 36991acd92d9STejun Heo 37001acd92d9STejun Heo if (!need_more_worker(pool)) 37011acd92d9STejun Heo continue; 37021acd92d9STejun Heo 37031acd92d9STejun Heo INIT_WORK(&dead_work.work, drain_dead_softirq_workfn); 37041acd92d9STejun Heo dead_work.pool = pool; 37051acd92d9STejun Heo init_completion(&dead_work.done); 37061acd92d9STejun Heo 37071acd92d9STejun Heo if (pool->attrs->nice == HIGHPRI_NICE_LEVEL) 37081acd92d9STejun Heo queue_work(system_bh_highpri_wq, &dead_work.work); 37091acd92d9STejun Heo else 37101acd92d9STejun Heo queue_work(system_bh_wq, &dead_work.work); 37111acd92d9STejun Heo 37121acd92d9STejun Heo wait_for_completion(&dead_work.done); 37131acd92d9STejun Heo } 37141acd92d9STejun Heo } 37151acd92d9STejun Heo 3716fca839c0STejun Heo /** 3717fca839c0STejun Heo * check_flush_dependency - check for flush dependency sanity 3718fca839c0STejun Heo * @target_wq: workqueue being flushed 3719fca839c0STejun Heo * @target_work: work item being flushed (NULL for workqueue flushes) 3720fca839c0STejun Heo * 3721fca839c0STejun Heo * %current is trying to flush the whole @target_wq or @target_work on it. 3722fca839c0STejun Heo * If @target_wq doesn't have %WQ_MEM_RECLAIM, verify that %current is not 3723fca839c0STejun Heo * reclaiming memory or running on a workqueue which doesn't have 3724fca839c0STejun Heo * %WQ_MEM_RECLAIM as that can break forward-progress guarantee leading to 3725fca839c0STejun Heo * a deadlock. 3726fca839c0STejun Heo */ 3727fca839c0STejun Heo static void check_flush_dependency(struct workqueue_struct *target_wq, 3728fca839c0STejun Heo struct work_struct *target_work) 3729fca839c0STejun Heo { 3730fca839c0STejun Heo work_func_t target_func = target_work ? target_work->func : NULL; 3731fca839c0STejun Heo struct worker *worker; 3732fca839c0STejun Heo 3733fca839c0STejun Heo if (target_wq->flags & WQ_MEM_RECLAIM) 3734fca839c0STejun Heo return; 3735fca839c0STejun Heo 3736fca839c0STejun Heo worker = current_wq_worker(); 3737fca839c0STejun Heo 3738fca839c0STejun Heo WARN_ONCE(current->flags & PF_MEMALLOC, 3739d75f773cSSakari Ailus "workqueue: PF_MEMALLOC task %d(%s) is flushing !WQ_MEM_RECLAIM %s:%ps", 3740fca839c0STejun Heo current->pid, current->comm, target_wq->name, target_func); 374123d11a58STejun Heo WARN_ONCE(worker && ((worker->current_pwq->wq->flags & 374223d11a58STejun Heo (WQ_MEM_RECLAIM | __WQ_LEGACY)) == WQ_MEM_RECLAIM), 3743d75f773cSSakari Ailus "workqueue: WQ_MEM_RECLAIM %s:%ps is flushing !WQ_MEM_RECLAIM %s:%ps", 3744fca839c0STejun Heo worker->current_pwq->wq->name, worker->current_func, 3745fca839c0STejun Heo target_wq->name, target_func); 3746fca839c0STejun Heo } 3747fca839c0STejun Heo 3748fc2e4d70SOleg Nesterov struct wq_barrier { 3749fc2e4d70SOleg Nesterov struct work_struct work; 3750fc2e4d70SOleg Nesterov struct completion done; 37512607d7a6STejun Heo struct task_struct *task; /* purely informational */ 3752fc2e4d70SOleg Nesterov }; 3753fc2e4d70SOleg Nesterov 3754fc2e4d70SOleg Nesterov static void wq_barrier_func(struct work_struct *work) 3755fc2e4d70SOleg Nesterov { 3756fc2e4d70SOleg Nesterov struct wq_barrier *barr = container_of(work, struct wq_barrier, work); 3757fc2e4d70SOleg Nesterov complete(&barr->done); 3758fc2e4d70SOleg Nesterov } 3759fc2e4d70SOleg Nesterov 37604690c4abSTejun Heo /** 37614690c4abSTejun Heo * insert_wq_barrier - insert a barrier work 3762112202d9STejun Heo * @pwq: pwq to insert barrier into 37634690c4abSTejun Heo * @barr: wq_barrier to insert 3764affee4b2STejun Heo * @target: target work to attach @barr to 3765affee4b2STejun Heo * @worker: worker currently executing @target, NULL if @target is not executing 37664690c4abSTejun Heo * 3767affee4b2STejun Heo * @barr is linked to @target such that @barr is completed only after 3768affee4b2STejun Heo * @target finishes execution. Please note that the ordering 3769affee4b2STejun Heo * guarantee is observed only with respect to @target and on the local 3770affee4b2STejun Heo * cpu. 3771affee4b2STejun Heo * 3772affee4b2STejun Heo * Currently, a queued barrier can't be canceled. This is because 3773affee4b2STejun Heo * try_to_grab_pending() can't determine whether the work to be 3774affee4b2STejun Heo * grabbed is at the head of the queue and thus can't clear LINKED 3775affee4b2STejun Heo * flag of the previous work while there must be a valid next work 3776affee4b2STejun Heo * after a work with LINKED flag set. 3777affee4b2STejun Heo * 3778affee4b2STejun Heo * Note that when @worker is non-NULL, @target may be modified 3779112202d9STejun Heo * underneath us, so we can't reliably determine pwq from @target. 37804690c4abSTejun Heo * 37814690c4abSTejun Heo * CONTEXT: 3782a9b8a985SSebastian Andrzej Siewior * raw_spin_lock_irq(pool->lock). 37834690c4abSTejun Heo */ 3784112202d9STejun Heo static void insert_wq_barrier(struct pool_workqueue *pwq, 3785affee4b2STejun Heo struct wq_barrier *barr, 3786affee4b2STejun Heo struct work_struct *target, struct worker *worker) 3787fc2e4d70SOleg Nesterov { 37884cb1ef64STejun Heo static __maybe_unused struct lock_class_key bh_key, thr_key; 3789d812796eSLai Jiangshan unsigned int work_flags = 0; 3790d812796eSLai Jiangshan unsigned int work_color; 3791affee4b2STejun Heo struct list_head *head; 3792affee4b2STejun Heo 3793dc186ad7SThomas Gleixner /* 3794d565ed63STejun Heo * debugobject calls are safe here even with pool->lock locked 3795dc186ad7SThomas Gleixner * as we know for sure that this will not trigger any of the 3796dc186ad7SThomas Gleixner * checks and call back into the fixup functions where we 3797dc186ad7SThomas Gleixner * might deadlock. 37984cb1ef64STejun Heo * 37994cb1ef64STejun Heo * BH and threaded workqueues need separate lockdep keys to avoid 38004cb1ef64STejun Heo * spuriously triggering "inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} 38014cb1ef64STejun Heo * usage". 3802dc186ad7SThomas Gleixner */ 38034cb1ef64STejun Heo INIT_WORK_ONSTACK_KEY(&barr->work, wq_barrier_func, 38044cb1ef64STejun Heo (pwq->wq->flags & WQ_BH) ? &bh_key : &thr_key); 380522df02bbSTejun Heo __set_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&barr->work)); 380652fa5bc5SBoqun Feng 3807fd1a5b04SByungchul Park init_completion_map(&barr->done, &target->lockdep_map); 3808fd1a5b04SByungchul Park 38092607d7a6STejun Heo barr->task = current; 381083c22520SOleg Nesterov 38115797b1c1STejun Heo /* The barrier work item does not participate in nr_active. */ 3812018f3a13SLai Jiangshan work_flags |= WORK_STRUCT_INACTIVE; 3813018f3a13SLai Jiangshan 3814affee4b2STejun Heo /* 3815affee4b2STejun Heo * If @target is currently being executed, schedule the 3816affee4b2STejun Heo * barrier to the worker; otherwise, put it after @target. 3817affee4b2STejun Heo */ 3818d812796eSLai Jiangshan if (worker) { 3819affee4b2STejun Heo head = worker->scheduled.next; 3820d812796eSLai Jiangshan work_color = worker->current_color; 3821d812796eSLai Jiangshan } else { 3822affee4b2STejun Heo unsigned long *bits = work_data_bits(target); 3823affee4b2STejun Heo 3824affee4b2STejun Heo head = target->entry.next; 3825affee4b2STejun Heo /* there can already be other linked works, inherit and set */ 3826d21cece0SLai Jiangshan work_flags |= *bits & WORK_STRUCT_LINKED; 3827d812796eSLai Jiangshan work_color = get_work_color(*bits); 3828affee4b2STejun Heo __set_bit(WORK_STRUCT_LINKED_BIT, bits); 3829affee4b2STejun Heo } 3830affee4b2STejun Heo 3831d812796eSLai Jiangshan pwq->nr_in_flight[work_color]++; 3832d812796eSLai Jiangshan work_flags |= work_color_to_flags(work_color); 3833d812796eSLai Jiangshan 3834d21cece0SLai Jiangshan insert_work(pwq, &barr->work, head, work_flags); 3835fc2e4d70SOleg Nesterov } 3836fc2e4d70SOleg Nesterov 383773f53c4aSTejun Heo /** 3838112202d9STejun Heo * flush_workqueue_prep_pwqs - prepare pwqs for workqueue flushing 383973f53c4aSTejun Heo * @wq: workqueue being flushed 384073f53c4aSTejun Heo * @flush_color: new flush color, < 0 for no-op 384173f53c4aSTejun Heo * @work_color: new work color, < 0 for no-op 384273f53c4aSTejun Heo * 3843112202d9STejun Heo * Prepare pwqs for workqueue flushing. 384473f53c4aSTejun Heo * 3845112202d9STejun Heo * If @flush_color is non-negative, flush_color on all pwqs should be 3846112202d9STejun Heo * -1. If no pwq has in-flight commands at the specified color, all 3847112202d9STejun Heo * pwq->flush_color's stay at -1 and %false is returned. If any pwq 3848112202d9STejun Heo * has in flight commands, its pwq->flush_color is set to 3849112202d9STejun Heo * @flush_color, @wq->nr_pwqs_to_flush is updated accordingly, pwq 385073f53c4aSTejun Heo * wakeup logic is armed and %true is returned. 385173f53c4aSTejun Heo * 385273f53c4aSTejun Heo * The caller should have initialized @wq->first_flusher prior to 385373f53c4aSTejun Heo * calling this function with non-negative @flush_color. If 385473f53c4aSTejun Heo * @flush_color is negative, no flush color update is done and %false 385573f53c4aSTejun Heo * is returned. 385673f53c4aSTejun Heo * 3857112202d9STejun Heo * If @work_color is non-negative, all pwqs should have the same 385873f53c4aSTejun Heo * work_color which is previous to @work_color and all will be 385973f53c4aSTejun Heo * advanced to @work_color. 386073f53c4aSTejun Heo * 386173f53c4aSTejun Heo * CONTEXT: 38623c25a55dSLai Jiangshan * mutex_lock(wq->mutex). 386373f53c4aSTejun Heo * 3864d185af30SYacine Belkadi * Return: 386573f53c4aSTejun Heo * %true if @flush_color >= 0 and there's something to flush. %false 386673f53c4aSTejun Heo * otherwise. 386773f53c4aSTejun Heo */ 3868112202d9STejun Heo static bool flush_workqueue_prep_pwqs(struct workqueue_struct *wq, 386973f53c4aSTejun Heo int flush_color, int work_color) 38701da177e4SLinus Torvalds { 387173f53c4aSTejun Heo bool wait = false; 387249e3cf44STejun Heo struct pool_workqueue *pwq; 38731da177e4SLinus Torvalds 387473f53c4aSTejun Heo if (flush_color >= 0) { 38756183c009STejun Heo WARN_ON_ONCE(atomic_read(&wq->nr_pwqs_to_flush)); 3876112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 1); 3877dc186ad7SThomas Gleixner } 387814441960SOleg Nesterov 387949e3cf44STejun Heo for_each_pwq(pwq, wq) { 3880112202d9STejun Heo struct worker_pool *pool = pwq->pool; 38811da177e4SLinus Torvalds 3882a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 388373f53c4aSTejun Heo 388473f53c4aSTejun Heo if (flush_color >= 0) { 38856183c009STejun Heo WARN_ON_ONCE(pwq->flush_color != -1); 388673f53c4aSTejun Heo 3887112202d9STejun Heo if (pwq->nr_in_flight[flush_color]) { 3888112202d9STejun Heo pwq->flush_color = flush_color; 3889112202d9STejun Heo atomic_inc(&wq->nr_pwqs_to_flush); 389073f53c4aSTejun Heo wait = true; 38911da177e4SLinus Torvalds } 389273f53c4aSTejun Heo } 389373f53c4aSTejun Heo 389473f53c4aSTejun Heo if (work_color >= 0) { 38956183c009STejun Heo WARN_ON_ONCE(work_color != work_next_color(pwq->work_color)); 3896112202d9STejun Heo pwq->work_color = work_color; 389773f53c4aSTejun Heo } 389873f53c4aSTejun Heo 3899a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 39001da177e4SLinus Torvalds } 39011da177e4SLinus Torvalds 3902112202d9STejun Heo if (flush_color >= 0 && atomic_dec_and_test(&wq->nr_pwqs_to_flush)) 390373f53c4aSTejun Heo complete(&wq->first_flusher->done); 390473f53c4aSTejun Heo 390573f53c4aSTejun Heo return wait; 390683c22520SOleg Nesterov } 39071da177e4SLinus Torvalds 3908c35aea39STejun Heo static void touch_wq_lockdep_map(struct workqueue_struct *wq) 3909c35aea39STejun Heo { 39104cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 39114cb1ef64STejun Heo if (wq->flags & WQ_BH) 39124cb1ef64STejun Heo local_bh_disable(); 39134cb1ef64STejun Heo 3914c35aea39STejun Heo lock_map_acquire(&wq->lockdep_map); 3915c35aea39STejun Heo lock_map_release(&wq->lockdep_map); 39164cb1ef64STejun Heo 39174cb1ef64STejun Heo if (wq->flags & WQ_BH) 39184cb1ef64STejun Heo local_bh_enable(); 39194cb1ef64STejun Heo #endif 3920c35aea39STejun Heo } 3921c35aea39STejun Heo 3922c35aea39STejun Heo static void touch_work_lockdep_map(struct work_struct *work, 3923c35aea39STejun Heo struct workqueue_struct *wq) 3924c35aea39STejun Heo { 39254cb1ef64STejun Heo #ifdef CONFIG_LOCKDEP 39264cb1ef64STejun Heo if (wq->flags & WQ_BH) 39274cb1ef64STejun Heo local_bh_disable(); 39284cb1ef64STejun Heo 3929c35aea39STejun Heo lock_map_acquire(&work->lockdep_map); 3930c35aea39STejun Heo lock_map_release(&work->lockdep_map); 39314cb1ef64STejun Heo 39324cb1ef64STejun Heo if (wq->flags & WQ_BH) 39334cb1ef64STejun Heo local_bh_enable(); 39344cb1ef64STejun Heo #endif 3935c35aea39STejun Heo } 3936c35aea39STejun Heo 39370fcb78c2SRolf Eike Beer /** 3938c4f135d6STetsuo Handa * __flush_workqueue - ensure that any scheduled work has run to completion. 39390fcb78c2SRolf Eike Beer * @wq: workqueue to flush 39401da177e4SLinus Torvalds * 3941c5aa87bbSTejun Heo * This function sleeps until all work items which were queued on entry 3942c5aa87bbSTejun Heo * have finished execution, but it is not livelocked by new incoming ones. 39431da177e4SLinus Torvalds */ 3944c4f135d6STetsuo Handa void __flush_workqueue(struct workqueue_struct *wq) 39451da177e4SLinus Torvalds { 394673f53c4aSTejun Heo struct wq_flusher this_flusher = { 394773f53c4aSTejun Heo .list = LIST_HEAD_INIT(this_flusher.list), 394873f53c4aSTejun Heo .flush_color = -1, 3949fd1a5b04SByungchul Park .done = COMPLETION_INITIALIZER_ONSTACK_MAP(this_flusher.done, wq->lockdep_map), 395073f53c4aSTejun Heo }; 395173f53c4aSTejun Heo int next_color; 3952b1f4ec17SOleg Nesterov 39533347fa09STejun Heo if (WARN_ON(!wq_online)) 39543347fa09STejun Heo return; 39553347fa09STejun Heo 3956c35aea39STejun Heo touch_wq_lockdep_map(wq); 395787915adcSJohannes Berg 39583c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 395973f53c4aSTejun Heo 396073f53c4aSTejun Heo /* 396173f53c4aSTejun Heo * Start-to-wait phase 396273f53c4aSTejun Heo */ 396373f53c4aSTejun Heo next_color = work_next_color(wq->work_color); 396473f53c4aSTejun Heo 396573f53c4aSTejun Heo if (next_color != wq->flush_color) { 396673f53c4aSTejun Heo /* 396773f53c4aSTejun Heo * Color space is not full. The current work_color 396873f53c4aSTejun Heo * becomes our flush_color and work_color is advanced 396973f53c4aSTejun Heo * by one. 397073f53c4aSTejun Heo */ 39716183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow)); 397273f53c4aSTejun Heo this_flusher.flush_color = wq->work_color; 397373f53c4aSTejun Heo wq->work_color = next_color; 397473f53c4aSTejun Heo 397573f53c4aSTejun Heo if (!wq->first_flusher) { 397673f53c4aSTejun Heo /* no flush in progress, become the first flusher */ 39776183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 397873f53c4aSTejun Heo 397973f53c4aSTejun Heo wq->first_flusher = &this_flusher; 398073f53c4aSTejun Heo 3981112202d9STejun Heo if (!flush_workqueue_prep_pwqs(wq, wq->flush_color, 398273f53c4aSTejun Heo wq->work_color)) { 398373f53c4aSTejun Heo /* nothing to flush, done */ 398473f53c4aSTejun Heo wq->flush_color = next_color; 398573f53c4aSTejun Heo wq->first_flusher = NULL; 398673f53c4aSTejun Heo goto out_unlock; 398773f53c4aSTejun Heo } 398873f53c4aSTejun Heo } else { 398973f53c4aSTejun Heo /* wait in queue */ 39906183c009STejun Heo WARN_ON_ONCE(wq->flush_color == this_flusher.flush_color); 399173f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_queue); 3992112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 399373f53c4aSTejun Heo } 399473f53c4aSTejun Heo } else { 399573f53c4aSTejun Heo /* 399673f53c4aSTejun Heo * Oops, color space is full, wait on overflow queue. 399773f53c4aSTejun Heo * The next flush completion will assign us 399873f53c4aSTejun Heo * flush_color and transfer to flusher_queue. 399973f53c4aSTejun Heo */ 400073f53c4aSTejun Heo list_add_tail(&this_flusher.list, &wq->flusher_overflow); 400173f53c4aSTejun Heo } 400273f53c4aSTejun Heo 4003fca839c0STejun Heo check_flush_dependency(wq, NULL); 4004fca839c0STejun Heo 40053c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 400673f53c4aSTejun Heo 400773f53c4aSTejun Heo wait_for_completion(&this_flusher.done); 400873f53c4aSTejun Heo 400973f53c4aSTejun Heo /* 401073f53c4aSTejun Heo * Wake-up-and-cascade phase 401173f53c4aSTejun Heo * 401273f53c4aSTejun Heo * First flushers are responsible for cascading flushes and 401373f53c4aSTejun Heo * handling overflow. Non-first flushers can simply return. 401473f53c4aSTejun Heo */ 401500d5d15bSChris Wilson if (READ_ONCE(wq->first_flusher) != &this_flusher) 401673f53c4aSTejun Heo return; 401773f53c4aSTejun Heo 40183c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 401973f53c4aSTejun Heo 40204ce48b37STejun Heo /* we might have raced, check again with mutex held */ 40214ce48b37STejun Heo if (wq->first_flusher != &this_flusher) 40224ce48b37STejun Heo goto out_unlock; 40234ce48b37STejun Heo 402400d5d15bSChris Wilson WRITE_ONCE(wq->first_flusher, NULL); 402573f53c4aSTejun Heo 40266183c009STejun Heo WARN_ON_ONCE(!list_empty(&this_flusher.list)); 40276183c009STejun Heo WARN_ON_ONCE(wq->flush_color != this_flusher.flush_color); 402873f53c4aSTejun Heo 402973f53c4aSTejun Heo while (true) { 403073f53c4aSTejun Heo struct wq_flusher *next, *tmp; 403173f53c4aSTejun Heo 403273f53c4aSTejun Heo /* complete all the flushers sharing the current flush color */ 403373f53c4aSTejun Heo list_for_each_entry_safe(next, tmp, &wq->flusher_queue, list) { 403473f53c4aSTejun Heo if (next->flush_color != wq->flush_color) 403573f53c4aSTejun Heo break; 403673f53c4aSTejun Heo list_del_init(&next->list); 403773f53c4aSTejun Heo complete(&next->done); 403873f53c4aSTejun Heo } 403973f53c4aSTejun Heo 40406183c009STejun Heo WARN_ON_ONCE(!list_empty(&wq->flusher_overflow) && 404173f53c4aSTejun Heo wq->flush_color != work_next_color(wq->work_color)); 404273f53c4aSTejun Heo 404373f53c4aSTejun Heo /* this flush_color is finished, advance by one */ 404473f53c4aSTejun Heo wq->flush_color = work_next_color(wq->flush_color); 404573f53c4aSTejun Heo 404673f53c4aSTejun Heo /* one color has been freed, handle overflow queue */ 404773f53c4aSTejun Heo if (!list_empty(&wq->flusher_overflow)) { 404873f53c4aSTejun Heo /* 404973f53c4aSTejun Heo * Assign the same color to all overflowed 405073f53c4aSTejun Heo * flushers, advance work_color and append to 405173f53c4aSTejun Heo * flusher_queue. This is the start-to-wait 405273f53c4aSTejun Heo * phase for these overflowed flushers. 405373f53c4aSTejun Heo */ 405473f53c4aSTejun Heo list_for_each_entry(tmp, &wq->flusher_overflow, list) 405573f53c4aSTejun Heo tmp->flush_color = wq->work_color; 405673f53c4aSTejun Heo 405773f53c4aSTejun Heo wq->work_color = work_next_color(wq->work_color); 405873f53c4aSTejun Heo 405973f53c4aSTejun Heo list_splice_tail_init(&wq->flusher_overflow, 406073f53c4aSTejun Heo &wq->flusher_queue); 4061112202d9STejun Heo flush_workqueue_prep_pwqs(wq, -1, wq->work_color); 406273f53c4aSTejun Heo } 406373f53c4aSTejun Heo 406473f53c4aSTejun Heo if (list_empty(&wq->flusher_queue)) { 40656183c009STejun Heo WARN_ON_ONCE(wq->flush_color != wq->work_color); 406673f53c4aSTejun Heo break; 406773f53c4aSTejun Heo } 406873f53c4aSTejun Heo 406973f53c4aSTejun Heo /* 407073f53c4aSTejun Heo * Need to flush more colors. Make the next flusher 4071112202d9STejun Heo * the new first flusher and arm pwqs. 407273f53c4aSTejun Heo */ 40736183c009STejun Heo WARN_ON_ONCE(wq->flush_color == wq->work_color); 40746183c009STejun Heo WARN_ON_ONCE(wq->flush_color != next->flush_color); 407573f53c4aSTejun Heo 407673f53c4aSTejun Heo list_del_init(&next->list); 407773f53c4aSTejun Heo wq->first_flusher = next; 407873f53c4aSTejun Heo 4079112202d9STejun Heo if (flush_workqueue_prep_pwqs(wq, wq->flush_color, -1)) 408073f53c4aSTejun Heo break; 408173f53c4aSTejun Heo 408273f53c4aSTejun Heo /* 408373f53c4aSTejun Heo * Meh... this color is already done, clear first 408473f53c4aSTejun Heo * flusher and repeat cascading. 408573f53c4aSTejun Heo */ 408673f53c4aSTejun Heo wq->first_flusher = NULL; 408773f53c4aSTejun Heo } 408873f53c4aSTejun Heo 408973f53c4aSTejun Heo out_unlock: 40903c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 40911da177e4SLinus Torvalds } 4092c4f135d6STetsuo Handa EXPORT_SYMBOL(__flush_workqueue); 40931da177e4SLinus Torvalds 40949c5a2ba7STejun Heo /** 40959c5a2ba7STejun Heo * drain_workqueue - drain a workqueue 40969c5a2ba7STejun Heo * @wq: workqueue to drain 40979c5a2ba7STejun Heo * 40989c5a2ba7STejun Heo * Wait until the workqueue becomes empty. While draining is in progress, 40999c5a2ba7STejun Heo * only chain queueing is allowed. IOW, only currently pending or running 41009c5a2ba7STejun Heo * work items on @wq can queue further work items on it. @wq is flushed 4101b749b1b6SChen Hanxiao * repeatedly until it becomes empty. The number of flushing is determined 41029c5a2ba7STejun Heo * by the depth of chaining and should be relatively short. Whine if it 41039c5a2ba7STejun Heo * takes too long. 41049c5a2ba7STejun Heo */ 41059c5a2ba7STejun Heo void drain_workqueue(struct workqueue_struct *wq) 41069c5a2ba7STejun Heo { 41079c5a2ba7STejun Heo unsigned int flush_cnt = 0; 410849e3cf44STejun Heo struct pool_workqueue *pwq; 41099c5a2ba7STejun Heo 41109c5a2ba7STejun Heo /* 41119c5a2ba7STejun Heo * __queue_work() needs to test whether there are drainers, is much 41129c5a2ba7STejun Heo * hotter than drain_workqueue() and already looks at @wq->flags. 4113618b01ebSTejun Heo * Use __WQ_DRAINING so that queue doesn't have to check nr_drainers. 41149c5a2ba7STejun Heo */ 411587fc741eSLai Jiangshan mutex_lock(&wq->mutex); 41169c5a2ba7STejun Heo if (!wq->nr_drainers++) 4117618b01ebSTejun Heo wq->flags |= __WQ_DRAINING; 411887fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 41199c5a2ba7STejun Heo reflush: 4120c4f135d6STetsuo Handa __flush_workqueue(wq); 41219c5a2ba7STejun Heo 4122b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 412376af4d93STejun Heo 412449e3cf44STejun Heo for_each_pwq(pwq, wq) { 4125fa2563e4SThomas Tuttle bool drained; 41269c5a2ba7STejun Heo 4127a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 4128afa87ce8STejun Heo drained = pwq_is_empty(pwq); 4129a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 4130fa2563e4SThomas Tuttle 4131fa2563e4SThomas Tuttle if (drained) 41329c5a2ba7STejun Heo continue; 41339c5a2ba7STejun Heo 41349c5a2ba7STejun Heo if (++flush_cnt == 10 || 41359c5a2ba7STejun Heo (flush_cnt % 100 == 0 && flush_cnt <= 1000)) 4136e9ad2eb3SStephen Zhang pr_warn("workqueue %s: %s() isn't complete after %u tries\n", 4137e9ad2eb3SStephen Zhang wq->name, __func__, flush_cnt); 413876af4d93STejun Heo 4139b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 41409c5a2ba7STejun Heo goto reflush; 41419c5a2ba7STejun Heo } 41429c5a2ba7STejun Heo 41439c5a2ba7STejun Heo if (!--wq->nr_drainers) 4144618b01ebSTejun Heo wq->flags &= ~__WQ_DRAINING; 414587fc741eSLai Jiangshan mutex_unlock(&wq->mutex); 41469c5a2ba7STejun Heo } 41479c5a2ba7STejun Heo EXPORT_SYMBOL_GPL(drain_workqueue); 41489c5a2ba7STejun Heo 4149d6e89786SJohannes Berg static bool start_flush_work(struct work_struct *work, struct wq_barrier *barr, 4150d6e89786SJohannes Berg bool from_cancel) 4151baf59022STejun Heo { 4152baf59022STejun Heo struct worker *worker = NULL; 4153c9e7cf27STejun Heo struct worker_pool *pool; 4154112202d9STejun Heo struct pool_workqueue *pwq; 4155c35aea39STejun Heo struct workqueue_struct *wq; 4156baf59022STejun Heo 4157baf59022STejun Heo might_sleep(); 4158baf59022STejun Heo 415924acfb71SThomas Gleixner rcu_read_lock(); 4160fa1b54e6STejun Heo pool = get_work_pool(work); 4161fa1b54e6STejun Heo if (!pool) { 416224acfb71SThomas Gleixner rcu_read_unlock(); 4163fa1b54e6STejun Heo return false; 4164fa1b54e6STejun Heo } 4165fa1b54e6STejun Heo 4166a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 41670b3dae68SLai Jiangshan /* see the comment in try_to_grab_pending() with the same code */ 4168112202d9STejun Heo pwq = get_work_pwq(work); 4169112202d9STejun Heo if (pwq) { 4170112202d9STejun Heo if (unlikely(pwq->pool != pool)) 4171baf59022STejun Heo goto already_gone; 4172606a5020STejun Heo } else { 4173c9e7cf27STejun Heo worker = find_worker_executing_work(pool, work); 4174baf59022STejun Heo if (!worker) 4175baf59022STejun Heo goto already_gone; 4176112202d9STejun Heo pwq = worker->current_pwq; 4177606a5020STejun Heo } 4178baf59022STejun Heo 4179c35aea39STejun Heo wq = pwq->wq; 4180c35aea39STejun Heo check_flush_dependency(wq, work); 4181fca839c0STejun Heo 4182112202d9STejun Heo insert_wq_barrier(pwq, barr, work, worker); 4183a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 4184baf59022STejun Heo 4185c35aea39STejun Heo touch_work_lockdep_map(work, wq); 4186c35aea39STejun Heo 4187e159489bSTejun Heo /* 4188a1d14934SPeter Zijlstra * Force a lock recursion deadlock when using flush_work() inside a 4189a1d14934SPeter Zijlstra * single-threaded or rescuer equipped workqueue. 4190a1d14934SPeter Zijlstra * 4191a1d14934SPeter Zijlstra * For single threaded workqueues the deadlock happens when the work 4192a1d14934SPeter Zijlstra * is after the work issuing the flush_work(). For rescuer equipped 4193a1d14934SPeter Zijlstra * workqueues the deadlock happens when the rescuer stalls, blocking 4194a1d14934SPeter Zijlstra * forward progress. 4195e159489bSTejun Heo */ 4196c35aea39STejun Heo if (!from_cancel && (wq->saved_max_active == 1 || wq->rescuer)) 4197c35aea39STejun Heo touch_wq_lockdep_map(wq); 4198c35aea39STejun Heo 419924acfb71SThomas Gleixner rcu_read_unlock(); 4200baf59022STejun Heo return true; 4201baf59022STejun Heo already_gone: 4202a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 420324acfb71SThomas Gleixner rcu_read_unlock(); 4204baf59022STejun Heo return false; 4205baf59022STejun Heo } 4206baf59022STejun Heo 4207d6e89786SJohannes Berg static bool __flush_work(struct work_struct *work, bool from_cancel) 4208d6e89786SJohannes Berg { 4209d6e89786SJohannes Berg struct wq_barrier barr; 4210d6e89786SJohannes Berg 4211d6e89786SJohannes Berg if (WARN_ON(!wq_online)) 4212d6e89786SJohannes Berg return false; 4213d6e89786SJohannes Berg 42144d43d395STetsuo Handa if (WARN_ON(!work->func)) 42154d43d395STetsuo Handa return false; 42164d43d395STetsuo Handa 4217d6e89786SJohannes Berg if (start_flush_work(work, &barr, from_cancel)) { 4218d6e89786SJohannes Berg wait_for_completion(&barr.done); 4219d6e89786SJohannes Berg destroy_work_on_stack(&barr.work); 4220d6e89786SJohannes Berg return true; 4221d6e89786SJohannes Berg } else { 4222d6e89786SJohannes Berg return false; 4223d6e89786SJohannes Berg } 4224d6e89786SJohannes Berg } 4225d6e89786SJohannes Berg 4226db700897SOleg Nesterov /** 4227401a8d04STejun Heo * flush_work - wait for a work to finish executing the last queueing instance 4228401a8d04STejun Heo * @work: the work to flush 4229db700897SOleg Nesterov * 4230606a5020STejun Heo * Wait until @work has finished execution. @work is guaranteed to be idle 4231606a5020STejun Heo * on return if it hasn't been requeued since flush started. 4232401a8d04STejun Heo * 4233d185af30SYacine Belkadi * Return: 4234401a8d04STejun Heo * %true if flush_work() waited for the work to finish execution, 4235401a8d04STejun Heo * %false if it was already idle. 4236db700897SOleg Nesterov */ 4237401a8d04STejun Heo bool flush_work(struct work_struct *work) 4238db700897SOleg Nesterov { 4239d6e89786SJohannes Berg return __flush_work(work, false); 4240606a5020STejun Heo } 4241db700897SOleg Nesterov EXPORT_SYMBOL_GPL(flush_work); 4242db700897SOleg Nesterov 4243cdc6e4b3STejun Heo /** 4244cdc6e4b3STejun Heo * flush_delayed_work - wait for a dwork to finish executing the last queueing 4245cdc6e4b3STejun Heo * @dwork: the delayed work to flush 4246cdc6e4b3STejun Heo * 4247cdc6e4b3STejun Heo * Delayed timer is cancelled and the pending work is queued for 4248cdc6e4b3STejun Heo * immediate execution. Like flush_work(), this function only 4249cdc6e4b3STejun Heo * considers the last queueing instance of @dwork. 4250cdc6e4b3STejun Heo * 4251cdc6e4b3STejun Heo * Return: 4252cdc6e4b3STejun Heo * %true if flush_work() waited for the work to finish execution, 4253cdc6e4b3STejun Heo * %false if it was already idle. 4254cdc6e4b3STejun Heo */ 4255cdc6e4b3STejun Heo bool flush_delayed_work(struct delayed_work *dwork) 4256cdc6e4b3STejun Heo { 4257cdc6e4b3STejun Heo local_irq_disable(); 4258cdc6e4b3STejun Heo if (del_timer_sync(&dwork->timer)) 4259cdc6e4b3STejun Heo __queue_work(dwork->cpu, dwork->wq, &dwork->work); 4260cdc6e4b3STejun Heo local_irq_enable(); 4261cdc6e4b3STejun Heo return flush_work(&dwork->work); 4262cdc6e4b3STejun Heo } 4263cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_delayed_work); 4264cdc6e4b3STejun Heo 4265cdc6e4b3STejun Heo /** 4266cdc6e4b3STejun Heo * flush_rcu_work - wait for a rwork to finish executing the last queueing 4267cdc6e4b3STejun Heo * @rwork: the rcu work to flush 4268cdc6e4b3STejun Heo * 4269cdc6e4b3STejun Heo * Return: 4270cdc6e4b3STejun Heo * %true if flush_rcu_work() waited for the work to finish execution, 4271cdc6e4b3STejun Heo * %false if it was already idle. 4272cdc6e4b3STejun Heo */ 4273cdc6e4b3STejun Heo bool flush_rcu_work(struct rcu_work *rwork) 4274cdc6e4b3STejun Heo { 4275cdc6e4b3STejun Heo if (test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(&rwork->work))) { 4276cdc6e4b3STejun Heo rcu_barrier(); 4277cdc6e4b3STejun Heo flush_work(&rwork->work); 4278cdc6e4b3STejun Heo return true; 4279cdc6e4b3STejun Heo } else { 4280cdc6e4b3STejun Heo return flush_work(&rwork->work); 4281cdc6e4b3STejun Heo } 4282cdc6e4b3STejun Heo } 4283cdc6e4b3STejun Heo EXPORT_SYMBOL(flush_rcu_work); 4284cdc6e4b3STejun Heo 4285c5f5b942STejun Heo static bool __cancel_work(struct work_struct *work, u32 cflags) 4286cdc6e4b3STejun Heo { 4287c26e2f2eSTejun Heo unsigned long irq_flags; 4288cdc6e4b3STejun Heo int ret; 4289cdc6e4b3STejun Heo 4290cdc6e4b3STejun Heo do { 4291c5f5b942STejun Heo ret = try_to_grab_pending(work, cflags, &irq_flags); 4292cdc6e4b3STejun Heo } while (unlikely(ret == -EAGAIN)); 4293cdc6e4b3STejun Heo 4294cdc6e4b3STejun Heo if (unlikely(ret < 0)) 4295cdc6e4b3STejun Heo return false; 4296cdc6e4b3STejun Heo 4297bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, get_work_pool_id(work), 0); 4298c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4299cdc6e4b3STejun Heo return ret; 4300cdc6e4b3STejun Heo } 4301cdc6e4b3STejun Heo 4302c5f5b942STejun Heo static bool __cancel_work_sync(struct work_struct *work, u32 cflags) 4303401a8d04STejun Heo { 4304c26e2f2eSTejun Heo unsigned long irq_flags; 4305978b8409STejun Heo bool ret; 43061f1f642eSOleg Nesterov 4307978b8409STejun Heo /* claim @work and tell other tasks trying to grab @work to back off */ 4308978b8409STejun Heo ret = work_grab_pending(work, cflags, &irq_flags); 4309bbb68dfaSTejun Heo mark_work_canceling(work); 4310c26e2f2eSTejun Heo local_irq_restore(irq_flags); 4311bbb68dfaSTejun Heo 43123347fa09STejun Heo /* 4313c7a40c49STejun Heo * Skip __flush_work() during early boot when we know that @work isn't 4314c7a40c49STejun Heo * executing. This allows canceling during early boot. 43153347fa09STejun Heo */ 43163347fa09STejun Heo if (wq_online) 4317d6e89786SJohannes Berg __flush_work(work, true); 43183347fa09STejun Heo 43198603e1b3STejun Heo /* 4320afe928c1STejun Heo * smp_mb() at the end of set_work_pool_and_clear_pending() is paired 4321afe928c1STejun Heo * with prepare_to_wait() above so that either waitqueue_active() is 4322afe928c1STejun Heo * visible here or !work_is_canceling() is visible there. 43238603e1b3STejun Heo */ 4324bccdc1faSTejun Heo set_work_pool_and_clear_pending(work, WORK_OFFQ_POOL_NONE, 0); 4325afe928c1STejun Heo 4326978b8409STejun Heo if (waitqueue_active(&wq_cancel_waitq)) 4327978b8409STejun Heo __wake_up(&wq_cancel_waitq, TASK_NORMAL, 1, work); 43288603e1b3STejun Heo 43291f1f642eSOleg Nesterov return ret; 43301f1f642eSOleg Nesterov } 43311f1f642eSOleg Nesterov 4332cdc6e4b3STejun Heo /* 4333cdc6e4b3STejun Heo * See cancel_delayed_work() 4334cdc6e4b3STejun Heo */ 4335cdc6e4b3STejun Heo bool cancel_work(struct work_struct *work) 4336cdc6e4b3STejun Heo { 4337c5f5b942STejun Heo return __cancel_work(work, 0); 4338cdc6e4b3STejun Heo } 4339cdc6e4b3STejun Heo EXPORT_SYMBOL(cancel_work); 4340cdc6e4b3STejun Heo 43416e84d644SOleg Nesterov /** 4342401a8d04STejun Heo * cancel_work_sync - cancel a work and wait for it to finish 4343401a8d04STejun Heo * @work: the work to cancel 43446e84d644SOleg Nesterov * 4345401a8d04STejun Heo * Cancel @work and wait for its execution to finish. This function 4346401a8d04STejun Heo * can be used even if the work re-queues itself or migrates to 4347401a8d04STejun Heo * another workqueue. On return from this function, @work is 4348401a8d04STejun Heo * guaranteed to be not pending or executing on any CPU. 43491f1f642eSOleg Nesterov * 4350401a8d04STejun Heo * cancel_work_sync(&delayed_work->work) must not be used for 4351401a8d04STejun Heo * delayed_work's. Use cancel_delayed_work_sync() instead. 43526e84d644SOleg Nesterov * 4353401a8d04STejun Heo * The caller must ensure that the workqueue on which @work was last 43546e84d644SOleg Nesterov * queued can't be destroyed before this function returns. 4355401a8d04STejun Heo * 4356d185af30SYacine Belkadi * Return: 4357401a8d04STejun Heo * %true if @work was pending, %false otherwise. 43586e84d644SOleg Nesterov */ 4359401a8d04STejun Heo bool cancel_work_sync(struct work_struct *work) 43606e84d644SOleg Nesterov { 4361c5f5b942STejun Heo return __cancel_work_sync(work, 0); 4362b89deed3SOleg Nesterov } 436328e53bddSOleg Nesterov EXPORT_SYMBOL_GPL(cancel_work_sync); 4364b89deed3SOleg Nesterov 43656e84d644SOleg Nesterov /** 436657b30ae7STejun Heo * cancel_delayed_work - cancel a delayed work 436757b30ae7STejun Heo * @dwork: delayed_work to cancel 436809383498STejun Heo * 4369d185af30SYacine Belkadi * Kill off a pending delayed_work. 4370d185af30SYacine Belkadi * 4371d185af30SYacine Belkadi * Return: %true if @dwork was pending and canceled; %false if it wasn't 4372d185af30SYacine Belkadi * pending. 4373d185af30SYacine Belkadi * 4374d185af30SYacine Belkadi * Note: 4375d185af30SYacine Belkadi * The work callback function may still be running on return, unless 4376d185af30SYacine Belkadi * it returns %true and the work doesn't re-arm itself. Explicitly flush or 4377d185af30SYacine Belkadi * use cancel_delayed_work_sync() to wait on it. 437809383498STejun Heo * 437957b30ae7STejun Heo * This function is safe to call from any context including IRQ handler. 438009383498STejun Heo */ 438157b30ae7STejun Heo bool cancel_delayed_work(struct delayed_work *dwork) 438209383498STejun Heo { 4383c5f5b942STejun Heo return __cancel_work(&dwork->work, WORK_CANCEL_DELAYED); 438409383498STejun Heo } 438557b30ae7STejun Heo EXPORT_SYMBOL(cancel_delayed_work); 438609383498STejun Heo 438709383498STejun Heo /** 4388401a8d04STejun Heo * cancel_delayed_work_sync - cancel a delayed work and wait for it to finish 4389401a8d04STejun Heo * @dwork: the delayed work cancel 4390401a8d04STejun Heo * 4391401a8d04STejun Heo * This is cancel_work_sync() for delayed works. 4392401a8d04STejun Heo * 4393d185af30SYacine Belkadi * Return: 4394401a8d04STejun Heo * %true if @dwork was pending, %false otherwise. 4395401a8d04STejun Heo */ 4396401a8d04STejun Heo bool cancel_delayed_work_sync(struct delayed_work *dwork) 43976e84d644SOleg Nesterov { 4398c5f5b942STejun Heo return __cancel_work_sync(&dwork->work, WORK_CANCEL_DELAYED); 43996e84d644SOleg Nesterov } 4400f5a421a4SOleg Nesterov EXPORT_SYMBOL(cancel_delayed_work_sync); 44011da177e4SLinus Torvalds 44020fcb78c2SRolf Eike Beer /** 440331ddd871STejun Heo * schedule_on_each_cpu - execute a function synchronously on each online CPU 4404b6136773SAndrew Morton * @func: the function to call 4405b6136773SAndrew Morton * 440631ddd871STejun Heo * schedule_on_each_cpu() executes @func on each online CPU using the 440731ddd871STejun Heo * system workqueue and blocks until all CPUs have completed. 4408b6136773SAndrew Morton * schedule_on_each_cpu() is very slow. 440931ddd871STejun Heo * 4410d185af30SYacine Belkadi * Return: 441131ddd871STejun Heo * 0 on success, -errno on failure. 4412b6136773SAndrew Morton */ 441365f27f38SDavid Howells int schedule_on_each_cpu(work_func_t func) 441415316ba8SChristoph Lameter { 441515316ba8SChristoph Lameter int cpu; 441638f51568SNamhyung Kim struct work_struct __percpu *works; 441715316ba8SChristoph Lameter 4418b6136773SAndrew Morton works = alloc_percpu(struct work_struct); 4419b6136773SAndrew Morton if (!works) 442015316ba8SChristoph Lameter return -ENOMEM; 4421b6136773SAndrew Morton 4422ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 442393981800STejun Heo 442415316ba8SChristoph Lameter for_each_online_cpu(cpu) { 44259bfb1839SIngo Molnar struct work_struct *work = per_cpu_ptr(works, cpu); 44269bfb1839SIngo Molnar 44279bfb1839SIngo Molnar INIT_WORK(work, func); 44288de6d308SOleg Nesterov schedule_work_on(cpu, work); 442915316ba8SChristoph Lameter } 443093981800STejun Heo 443193981800STejun Heo for_each_online_cpu(cpu) 44328616a89aSOleg Nesterov flush_work(per_cpu_ptr(works, cpu)); 443393981800STejun Heo 4434ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 4435b6136773SAndrew Morton free_percpu(works); 443615316ba8SChristoph Lameter return 0; 443715316ba8SChristoph Lameter } 443815316ba8SChristoph Lameter 4439eef6a7d5SAlan Stern /** 44401fa44ecaSJames Bottomley * execute_in_process_context - reliably execute the routine with user context 44411fa44ecaSJames Bottomley * @fn: the function to execute 44421fa44ecaSJames Bottomley * @ew: guaranteed storage for the execute work structure (must 44431fa44ecaSJames Bottomley * be available when the work executes) 44441fa44ecaSJames Bottomley * 44451fa44ecaSJames Bottomley * Executes the function immediately if process context is available, 44461fa44ecaSJames Bottomley * otherwise schedules the function for delayed execution. 44471fa44ecaSJames Bottomley * 4448d185af30SYacine Belkadi * Return: 0 - function was executed 44491fa44ecaSJames Bottomley * 1 - function was scheduled for execution 44501fa44ecaSJames Bottomley */ 445165f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *ew) 44521fa44ecaSJames Bottomley { 44531fa44ecaSJames Bottomley if (!in_interrupt()) { 445465f27f38SDavid Howells fn(&ew->work); 44551fa44ecaSJames Bottomley return 0; 44561fa44ecaSJames Bottomley } 44571fa44ecaSJames Bottomley 445865f27f38SDavid Howells INIT_WORK(&ew->work, fn); 44591fa44ecaSJames Bottomley schedule_work(&ew->work); 44601fa44ecaSJames Bottomley 44611fa44ecaSJames Bottomley return 1; 44621fa44ecaSJames Bottomley } 44631fa44ecaSJames Bottomley EXPORT_SYMBOL_GPL(execute_in_process_context); 44641fa44ecaSJames Bottomley 44657a4e344cSTejun Heo /** 44667a4e344cSTejun Heo * free_workqueue_attrs - free a workqueue_attrs 44677a4e344cSTejun Heo * @attrs: workqueue_attrs to free 44687a4e344cSTejun Heo * 44697a4e344cSTejun Heo * Undo alloc_workqueue_attrs(). 44707a4e344cSTejun Heo */ 4471513c98d0SDaniel Jordan void free_workqueue_attrs(struct workqueue_attrs *attrs) 44727a4e344cSTejun Heo { 44737a4e344cSTejun Heo if (attrs) { 44747a4e344cSTejun Heo free_cpumask_var(attrs->cpumask); 44759546b29eSTejun Heo free_cpumask_var(attrs->__pod_cpumask); 44767a4e344cSTejun Heo kfree(attrs); 44777a4e344cSTejun Heo } 44787a4e344cSTejun Heo } 44797a4e344cSTejun Heo 44807a4e344cSTejun Heo /** 44817a4e344cSTejun Heo * alloc_workqueue_attrs - allocate a workqueue_attrs 44827a4e344cSTejun Heo * 44837a4e344cSTejun Heo * Allocate a new workqueue_attrs, initialize with default settings and 4484d185af30SYacine Belkadi * return it. 4485d185af30SYacine Belkadi * 4486d185af30SYacine Belkadi * Return: The allocated new workqueue_attr on success. %NULL on failure. 44877a4e344cSTejun Heo */ 4488513c98d0SDaniel Jordan struct workqueue_attrs *alloc_workqueue_attrs(void) 44897a4e344cSTejun Heo { 44907a4e344cSTejun Heo struct workqueue_attrs *attrs; 44917a4e344cSTejun Heo 4492be69d00dSThomas Gleixner attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); 44937a4e344cSTejun Heo if (!attrs) 44947a4e344cSTejun Heo goto fail; 4495be69d00dSThomas Gleixner if (!alloc_cpumask_var(&attrs->cpumask, GFP_KERNEL)) 44967a4e344cSTejun Heo goto fail; 44979546b29eSTejun Heo if (!alloc_cpumask_var(&attrs->__pod_cpumask, GFP_KERNEL)) 44989546b29eSTejun Heo goto fail; 44997a4e344cSTejun Heo 450013e2e556STejun Heo cpumask_copy(attrs->cpumask, cpu_possible_mask); 4501523a301eSTejun Heo attrs->affn_scope = WQ_AFFN_DFL; 45027a4e344cSTejun Heo return attrs; 45037a4e344cSTejun Heo fail: 45047a4e344cSTejun Heo free_workqueue_attrs(attrs); 45057a4e344cSTejun Heo return NULL; 45067a4e344cSTejun Heo } 45077a4e344cSTejun Heo 450829c91e99STejun Heo static void copy_workqueue_attrs(struct workqueue_attrs *to, 450929c91e99STejun Heo const struct workqueue_attrs *from) 451029c91e99STejun Heo { 451129c91e99STejun Heo to->nice = from->nice; 451229c91e99STejun Heo cpumask_copy(to->cpumask, from->cpumask); 45139546b29eSTejun Heo cpumask_copy(to->__pod_cpumask, from->__pod_cpumask); 45148639ecebSTejun Heo to->affn_strict = from->affn_strict; 451584193c07STejun Heo 45162865a8fbSShaohua Li /* 451784193c07STejun Heo * Unlike hash and equality test, copying shouldn't ignore wq-only 451884193c07STejun Heo * fields as copying is used for both pool and wq attrs. Instead, 451984193c07STejun Heo * get_unbound_pool() explicitly clears the fields. 45202865a8fbSShaohua Li */ 452184193c07STejun Heo to->affn_scope = from->affn_scope; 4522af73f5c9STejun Heo to->ordered = from->ordered; 452329c91e99STejun Heo } 452429c91e99STejun Heo 45255de7a03cSTejun Heo /* 45265de7a03cSTejun Heo * Some attrs fields are workqueue-only. Clear them for worker_pool's. See the 45275de7a03cSTejun Heo * comments in 'struct workqueue_attrs' definition. 45285de7a03cSTejun Heo */ 45295de7a03cSTejun Heo static void wqattrs_clear_for_pool(struct workqueue_attrs *attrs) 45305de7a03cSTejun Heo { 453184193c07STejun Heo attrs->affn_scope = WQ_AFFN_NR_TYPES; 45325de7a03cSTejun Heo attrs->ordered = false; 45335de7a03cSTejun Heo } 45345de7a03cSTejun Heo 453529c91e99STejun Heo /* hash value of the content of @attr */ 453629c91e99STejun Heo static u32 wqattrs_hash(const struct workqueue_attrs *attrs) 453729c91e99STejun Heo { 453829c91e99STejun Heo u32 hash = 0; 453929c91e99STejun Heo 454029c91e99STejun Heo hash = jhash_1word(attrs->nice, hash); 454113e2e556STejun Heo hash = jhash(cpumask_bits(attrs->cpumask), 454213e2e556STejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 45439546b29eSTejun Heo hash = jhash(cpumask_bits(attrs->__pod_cpumask), 45449546b29eSTejun Heo BITS_TO_LONGS(nr_cpumask_bits) * sizeof(long), hash); 45458639ecebSTejun Heo hash = jhash_1word(attrs->affn_strict, hash); 454629c91e99STejun Heo return hash; 454729c91e99STejun Heo } 454829c91e99STejun Heo 454929c91e99STejun Heo /* content equality test */ 455029c91e99STejun Heo static bool wqattrs_equal(const struct workqueue_attrs *a, 455129c91e99STejun Heo const struct workqueue_attrs *b) 455229c91e99STejun Heo { 455329c91e99STejun Heo if (a->nice != b->nice) 455429c91e99STejun Heo return false; 455529c91e99STejun Heo if (!cpumask_equal(a->cpumask, b->cpumask)) 455629c91e99STejun Heo return false; 45579546b29eSTejun Heo if (!cpumask_equal(a->__pod_cpumask, b->__pod_cpumask)) 45589546b29eSTejun Heo return false; 45598639ecebSTejun Heo if (a->affn_strict != b->affn_strict) 45608639ecebSTejun Heo return false; 456129c91e99STejun Heo return true; 456229c91e99STejun Heo } 456329c91e99STejun Heo 45640f36ee24STejun Heo /* Update @attrs with actually available CPUs */ 45650f36ee24STejun Heo static void wqattrs_actualize_cpumask(struct workqueue_attrs *attrs, 45660f36ee24STejun Heo const cpumask_t *unbound_cpumask) 45670f36ee24STejun Heo { 45680f36ee24STejun Heo /* 45690f36ee24STejun Heo * Calculate the effective CPU mask of @attrs given @unbound_cpumask. If 45700f36ee24STejun Heo * @attrs->cpumask doesn't overlap with @unbound_cpumask, we fallback to 45710f36ee24STejun Heo * @unbound_cpumask. 45720f36ee24STejun Heo */ 45730f36ee24STejun Heo cpumask_and(attrs->cpumask, attrs->cpumask, unbound_cpumask); 45740f36ee24STejun Heo if (unlikely(cpumask_empty(attrs->cpumask))) 45750f36ee24STejun Heo cpumask_copy(attrs->cpumask, unbound_cpumask); 45760f36ee24STejun Heo } 45770f36ee24STejun Heo 457884193c07STejun Heo /* find wq_pod_type to use for @attrs */ 457984193c07STejun Heo static const struct wq_pod_type * 458084193c07STejun Heo wqattrs_pod_type(const struct workqueue_attrs *attrs) 458184193c07STejun Heo { 4582523a301eSTejun Heo enum wq_affn_scope scope; 4583523a301eSTejun Heo struct wq_pod_type *pt; 4584523a301eSTejun Heo 4585523a301eSTejun Heo /* to synchronize access to wq_affn_dfl */ 4586523a301eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4587523a301eSTejun Heo 4588523a301eSTejun Heo if (attrs->affn_scope == WQ_AFFN_DFL) 4589523a301eSTejun Heo scope = wq_affn_dfl; 4590523a301eSTejun Heo else 4591523a301eSTejun Heo scope = attrs->affn_scope; 4592523a301eSTejun Heo 4593523a301eSTejun Heo pt = &wq_pod_types[scope]; 459484193c07STejun Heo 459584193c07STejun Heo if (!WARN_ON_ONCE(attrs->affn_scope == WQ_AFFN_NR_TYPES) && 459684193c07STejun Heo likely(pt->nr_pods)) 459784193c07STejun Heo return pt; 459884193c07STejun Heo 459984193c07STejun Heo /* 460084193c07STejun Heo * Before workqueue_init_topology(), only SYSTEM is available which is 460184193c07STejun Heo * initialized in workqueue_init_early(). 460284193c07STejun Heo */ 460384193c07STejun Heo pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 460484193c07STejun Heo BUG_ON(!pt->nr_pods); 460584193c07STejun Heo return pt; 460684193c07STejun Heo } 460784193c07STejun Heo 46087a4e344cSTejun Heo /** 46097a4e344cSTejun Heo * init_worker_pool - initialize a newly zalloc'd worker_pool 46107a4e344cSTejun Heo * @pool: worker_pool to initialize 46117a4e344cSTejun Heo * 4612402dd89dSShailendra Verma * Initialize a newly zalloc'd @pool. It also allocates @pool->attrs. 4613d185af30SYacine Belkadi * 4614d185af30SYacine Belkadi * Return: 0 on success, -errno on failure. Even on failure, all fields 461529c91e99STejun Heo * inside @pool proper are initialized and put_unbound_pool() can be called 461629c91e99STejun Heo * on @pool safely to release it. 46177a4e344cSTejun Heo */ 46187a4e344cSTejun Heo static int init_worker_pool(struct worker_pool *pool) 46194e1a1f9aSTejun Heo { 4620a9b8a985SSebastian Andrzej Siewior raw_spin_lock_init(&pool->lock); 462129c91e99STejun Heo pool->id = -1; 462229c91e99STejun Heo pool->cpu = -1; 4623f3f90ad4STejun Heo pool->node = NUMA_NO_NODE; 46244e1a1f9aSTejun Heo pool->flags |= POOL_DISASSOCIATED; 462582607adcSTejun Heo pool->watchdog_ts = jiffies; 46264e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->worklist); 46274e1a1f9aSTejun Heo INIT_LIST_HEAD(&pool->idle_list); 46284e1a1f9aSTejun Heo hash_init(pool->busy_hash); 46294e1a1f9aSTejun Heo 463032a6c723SKees Cook timer_setup(&pool->idle_timer, idle_worker_timeout, TIMER_DEFERRABLE); 46313f959aa3SValentin Schneider INIT_WORK(&pool->idle_cull_work, idle_cull_fn); 46324e1a1f9aSTejun Heo 463332a6c723SKees Cook timer_setup(&pool->mayday_timer, pool_mayday_timeout, 0); 46344e1a1f9aSTejun Heo 4635da028469SLai Jiangshan INIT_LIST_HEAD(&pool->workers); 4636e02b9312SValentin Schneider INIT_LIST_HEAD(&pool->dying_workers); 46377a4e344cSTejun Heo 46387cda9aaeSLai Jiangshan ida_init(&pool->worker_ida); 463929c91e99STejun Heo INIT_HLIST_NODE(&pool->hash_node); 464029c91e99STejun Heo pool->refcnt = 1; 464129c91e99STejun Heo 464229c91e99STejun Heo /* shouldn't fail above this point */ 4643be69d00dSThomas Gleixner pool->attrs = alloc_workqueue_attrs(); 46447a4e344cSTejun Heo if (!pool->attrs) 46457a4e344cSTejun Heo return -ENOMEM; 46465de7a03cSTejun Heo 46475de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 46485de7a03cSTejun Heo 46497a4e344cSTejun Heo return 0; 46504e1a1f9aSTejun Heo } 46514e1a1f9aSTejun Heo 4652669de8bdSBart Van Assche #ifdef CONFIG_LOCKDEP 4653669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4654669de8bdSBart Van Assche { 4655669de8bdSBart Van Assche char *lock_name; 4656669de8bdSBart Van Assche 4657669de8bdSBart Van Assche lockdep_register_key(&wq->key); 4658669de8bdSBart Van Assche lock_name = kasprintf(GFP_KERNEL, "%s%s", "(wq_completion)", wq->name); 4659669de8bdSBart Van Assche if (!lock_name) 4660669de8bdSBart Van Assche lock_name = wq->name; 466169a106c0SQian Cai 466269a106c0SQian Cai wq->lock_name = lock_name; 4663669de8bdSBart Van Assche lockdep_init_map(&wq->lockdep_map, lock_name, &wq->key, 0); 4664669de8bdSBart Van Assche } 4665669de8bdSBart Van Assche 4666669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4667669de8bdSBart Van Assche { 4668669de8bdSBart Van Assche lockdep_unregister_key(&wq->key); 4669669de8bdSBart Van Assche } 4670669de8bdSBart Van Assche 4671669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4672669de8bdSBart Van Assche { 4673669de8bdSBart Van Assche if (wq->lock_name != wq->name) 4674669de8bdSBart Van Assche kfree(wq->lock_name); 4675669de8bdSBart Van Assche } 4676669de8bdSBart Van Assche #else 4677669de8bdSBart Van Assche static void wq_init_lockdep(struct workqueue_struct *wq) 4678669de8bdSBart Van Assche { 4679669de8bdSBart Van Assche } 4680669de8bdSBart Van Assche 4681669de8bdSBart Van Assche static void wq_unregister_lockdep(struct workqueue_struct *wq) 4682669de8bdSBart Van Assche { 4683669de8bdSBart Van Assche } 4684669de8bdSBart Van Assche 4685669de8bdSBart Van Assche static void wq_free_lockdep(struct workqueue_struct *wq) 4686669de8bdSBart Van Assche { 4687669de8bdSBart Van Assche } 4688669de8bdSBart Van Assche #endif 4689669de8bdSBart Van Assche 469091ccc6e7STejun Heo static void free_node_nr_active(struct wq_node_nr_active **nna_ar) 469191ccc6e7STejun Heo { 469291ccc6e7STejun Heo int node; 469391ccc6e7STejun Heo 469491ccc6e7STejun Heo for_each_node(node) { 469591ccc6e7STejun Heo kfree(nna_ar[node]); 469691ccc6e7STejun Heo nna_ar[node] = NULL; 469791ccc6e7STejun Heo } 469891ccc6e7STejun Heo 469991ccc6e7STejun Heo kfree(nna_ar[nr_node_ids]); 470091ccc6e7STejun Heo nna_ar[nr_node_ids] = NULL; 470191ccc6e7STejun Heo } 470291ccc6e7STejun Heo 470391ccc6e7STejun Heo static void init_node_nr_active(struct wq_node_nr_active *nna) 470491ccc6e7STejun Heo { 4705c5f8cd6cSTejun Heo nna->max = WQ_DFL_MIN_ACTIVE; 470691ccc6e7STejun Heo atomic_set(&nna->nr, 0); 47075797b1c1STejun Heo raw_spin_lock_init(&nna->lock); 47085797b1c1STejun Heo INIT_LIST_HEAD(&nna->pending_pwqs); 470991ccc6e7STejun Heo } 471091ccc6e7STejun Heo 471191ccc6e7STejun Heo /* 471291ccc6e7STejun Heo * Each node's nr_active counter will be accessed mostly from its own node and 471391ccc6e7STejun Heo * should be allocated in the node. 471491ccc6e7STejun Heo */ 471591ccc6e7STejun Heo static int alloc_node_nr_active(struct wq_node_nr_active **nna_ar) 471691ccc6e7STejun Heo { 471791ccc6e7STejun Heo struct wq_node_nr_active *nna; 471891ccc6e7STejun Heo int node; 471991ccc6e7STejun Heo 472091ccc6e7STejun Heo for_each_node(node) { 472191ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, node); 472291ccc6e7STejun Heo if (!nna) 472391ccc6e7STejun Heo goto err_free; 472491ccc6e7STejun Heo init_node_nr_active(nna); 472591ccc6e7STejun Heo nna_ar[node] = nna; 472691ccc6e7STejun Heo } 472791ccc6e7STejun Heo 472891ccc6e7STejun Heo /* [nr_node_ids] is used as the fallback */ 472991ccc6e7STejun Heo nna = kzalloc_node(sizeof(*nna), GFP_KERNEL, NUMA_NO_NODE); 473091ccc6e7STejun Heo if (!nna) 473191ccc6e7STejun Heo goto err_free; 473291ccc6e7STejun Heo init_node_nr_active(nna); 473391ccc6e7STejun Heo nna_ar[nr_node_ids] = nna; 473491ccc6e7STejun Heo 473591ccc6e7STejun Heo return 0; 473691ccc6e7STejun Heo 473791ccc6e7STejun Heo err_free: 473891ccc6e7STejun Heo free_node_nr_active(nna_ar); 473991ccc6e7STejun Heo return -ENOMEM; 474091ccc6e7STejun Heo } 474191ccc6e7STejun Heo 4742e2dca7adSTejun Heo static void rcu_free_wq(struct rcu_head *rcu) 4743e2dca7adSTejun Heo { 4744e2dca7adSTejun Heo struct workqueue_struct *wq = 4745e2dca7adSTejun Heo container_of(rcu, struct workqueue_struct, rcu); 4746e2dca7adSTejun Heo 474791ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 474891ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 474991ccc6e7STejun Heo 4750669de8bdSBart Van Assche wq_free_lockdep(wq); 4751ee1ceef7STejun Heo free_percpu(wq->cpu_pwq); 4752e2dca7adSTejun Heo free_workqueue_attrs(wq->unbound_attrs); 4753e2dca7adSTejun Heo kfree(wq); 4754e2dca7adSTejun Heo } 4755e2dca7adSTejun Heo 475629c91e99STejun Heo static void rcu_free_pool(struct rcu_head *rcu) 475729c91e99STejun Heo { 475829c91e99STejun Heo struct worker_pool *pool = container_of(rcu, struct worker_pool, rcu); 475929c91e99STejun Heo 47607cda9aaeSLai Jiangshan ida_destroy(&pool->worker_ida); 476129c91e99STejun Heo free_workqueue_attrs(pool->attrs); 476229c91e99STejun Heo kfree(pool); 476329c91e99STejun Heo } 476429c91e99STejun Heo 476529c91e99STejun Heo /** 476629c91e99STejun Heo * put_unbound_pool - put a worker_pool 476729c91e99STejun Heo * @pool: worker_pool to put 476829c91e99STejun Heo * 476924acfb71SThomas Gleixner * Put @pool. If its refcnt reaches zero, it gets destroyed in RCU 4770c5aa87bbSTejun Heo * safe manner. get_unbound_pool() calls this function on its failure path 4771c5aa87bbSTejun Heo * and this function should be able to release pools which went through, 4772c5aa87bbSTejun Heo * successfully or not, init_worker_pool(). 4773a892caccSTejun Heo * 4774a892caccSTejun Heo * Should be called with wq_pool_mutex held. 477529c91e99STejun Heo */ 477629c91e99STejun Heo static void put_unbound_pool(struct worker_pool *pool) 477729c91e99STejun Heo { 477860f5a4bcSLai Jiangshan DECLARE_COMPLETION_ONSTACK(detach_completion); 477929c91e99STejun Heo struct worker *worker; 47809680540cSYang Yingliang LIST_HEAD(cull_list); 4781e02b9312SValentin Schneider 4782a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 4783a892caccSTejun Heo 4784a892caccSTejun Heo if (--pool->refcnt) 478529c91e99STejun Heo return; 478629c91e99STejun Heo 478729c91e99STejun Heo /* sanity checks */ 478861d0fbb4SLai Jiangshan if (WARN_ON(!(pool->cpu < 0)) || 4789a892caccSTejun Heo WARN_ON(!list_empty(&pool->worklist))) 479029c91e99STejun Heo return; 479129c91e99STejun Heo 479229c91e99STejun Heo /* release id and unhash */ 479329c91e99STejun Heo if (pool->id >= 0) 479429c91e99STejun Heo idr_remove(&worker_pool_idr, pool->id); 479529c91e99STejun Heo hash_del(&pool->hash_node); 479629c91e99STejun Heo 4797c5aa87bbSTejun Heo /* 4798692b4825STejun Heo * Become the manager and destroy all workers. This prevents 4799692b4825STejun Heo * @pool's workers from blocking on attach_mutex. We're the last 4800692b4825STejun Heo * manager and @pool gets freed with the flag set. 48019ab03be4SValentin Schneider * 48029ab03be4SValentin Schneider * Having a concurrent manager is quite unlikely to happen as we can 48039ab03be4SValentin Schneider * only get here with 48049ab03be4SValentin Schneider * pwq->refcnt == pool->refcnt == 0 48059ab03be4SValentin Schneider * which implies no work queued to the pool, which implies no worker can 48069ab03be4SValentin Schneider * become the manager. However a worker could have taken the role of 48079ab03be4SValentin Schneider * manager before the refcnts dropped to 0, since maybe_create_worker() 48089ab03be4SValentin Schneider * drops pool->lock 4809c5aa87bbSTejun Heo */ 48109ab03be4SValentin Schneider while (true) { 48119ab03be4SValentin Schneider rcuwait_wait_event(&manager_wait, 48129ab03be4SValentin Schneider !(pool->flags & POOL_MANAGER_ACTIVE), 4813d8bb65abSSebastian Andrzej Siewior TASK_UNINTERRUPTIBLE); 4814e02b9312SValentin Schneider 4815e02b9312SValentin Schneider mutex_lock(&wq_pool_attach_mutex); 48169ab03be4SValentin Schneider raw_spin_lock_irq(&pool->lock); 48179ab03be4SValentin Schneider if (!(pool->flags & POOL_MANAGER_ACTIVE)) { 4818692b4825STejun Heo pool->flags |= POOL_MANAGER_ACTIVE; 48199ab03be4SValentin Schneider break; 48209ab03be4SValentin Schneider } 48219ab03be4SValentin Schneider raw_spin_unlock_irq(&pool->lock); 4822e02b9312SValentin Schneider mutex_unlock(&wq_pool_attach_mutex); 48239ab03be4SValentin Schneider } 4824692b4825STejun Heo 48251037de36SLai Jiangshan while ((worker = first_idle_worker(pool))) 4826e02b9312SValentin Schneider set_worker_dying(worker, &cull_list); 482729c91e99STejun Heo WARN_ON(pool->nr_workers || pool->nr_idle); 4828a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 482960f5a4bcSLai Jiangshan 4830e02b9312SValentin Schneider wake_dying_workers(&cull_list); 4831e02b9312SValentin Schneider 4832e02b9312SValentin Schneider if (!list_empty(&pool->workers) || !list_empty(&pool->dying_workers)) 483360f5a4bcSLai Jiangshan pool->detach_completion = &detach_completion; 48341258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 483560f5a4bcSLai Jiangshan 483660f5a4bcSLai Jiangshan if (pool->detach_completion) 483760f5a4bcSLai Jiangshan wait_for_completion(pool->detach_completion); 483860f5a4bcSLai Jiangshan 483929c91e99STejun Heo /* shut down the timers */ 484029c91e99STejun Heo del_timer_sync(&pool->idle_timer); 48413f959aa3SValentin Schneider cancel_work_sync(&pool->idle_cull_work); 484229c91e99STejun Heo del_timer_sync(&pool->mayday_timer); 484329c91e99STejun Heo 484424acfb71SThomas Gleixner /* RCU protected to allow dereferences from get_work_pool() */ 484525b00775SPaul E. McKenney call_rcu(&pool->rcu, rcu_free_pool); 484629c91e99STejun Heo } 484729c91e99STejun Heo 484829c91e99STejun Heo /** 484929c91e99STejun Heo * get_unbound_pool - get a worker_pool with the specified attributes 485029c91e99STejun Heo * @attrs: the attributes of the worker_pool to get 485129c91e99STejun Heo * 485229c91e99STejun Heo * Obtain a worker_pool which has the same attributes as @attrs, bump the 485329c91e99STejun Heo * reference count and return it. If there already is a matching 485429c91e99STejun Heo * worker_pool, it will be used; otherwise, this function attempts to 4855d185af30SYacine Belkadi * create a new one. 4856a892caccSTejun Heo * 4857a892caccSTejun Heo * Should be called with wq_pool_mutex held. 4858d185af30SYacine Belkadi * 4859d185af30SYacine Belkadi * Return: On success, a worker_pool with the same attributes as @attrs. 4860d185af30SYacine Belkadi * On failure, %NULL. 486129c91e99STejun Heo */ 486229c91e99STejun Heo static struct worker_pool *get_unbound_pool(const struct workqueue_attrs *attrs) 486329c91e99STejun Heo { 486484193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_NUMA]; 486529c91e99STejun Heo u32 hash = wqattrs_hash(attrs); 486629c91e99STejun Heo struct worker_pool *pool; 486784193c07STejun Heo int pod, node = NUMA_NO_NODE; 486829c91e99STejun Heo 4869a892caccSTejun Heo lockdep_assert_held(&wq_pool_mutex); 487029c91e99STejun Heo 487129c91e99STejun Heo /* do we already have a matching pool? */ 487229c91e99STejun Heo hash_for_each_possible(unbound_pool_hash, pool, hash_node, hash) { 487329c91e99STejun Heo if (wqattrs_equal(pool->attrs, attrs)) { 487429c91e99STejun Heo pool->refcnt++; 48753fb1823cSLai Jiangshan return pool; 487629c91e99STejun Heo } 487729c91e99STejun Heo } 487829c91e99STejun Heo 48799546b29eSTejun Heo /* If __pod_cpumask is contained inside a NUMA pod, that's our node */ 488084193c07STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) { 48819546b29eSTejun Heo if (cpumask_subset(attrs->__pod_cpumask, pt->pod_cpus[pod])) { 488284193c07STejun Heo node = pt->pod_node[pod]; 4883e2273584SXunlei Pang break; 4884e2273584SXunlei Pang } 4885e2273584SXunlei Pang } 4886e2273584SXunlei Pang 488729c91e99STejun Heo /* nope, create a new one */ 488884193c07STejun Heo pool = kzalloc_node(sizeof(*pool), GFP_KERNEL, node); 488929c91e99STejun Heo if (!pool || init_worker_pool(pool) < 0) 489029c91e99STejun Heo goto fail; 489129c91e99STejun Heo 489284193c07STejun Heo pool->node = node; 48935de7a03cSTejun Heo copy_workqueue_attrs(pool->attrs, attrs); 48945de7a03cSTejun Heo wqattrs_clear_for_pool(pool->attrs); 48952865a8fbSShaohua Li 489629c91e99STejun Heo if (worker_pool_assign_id(pool) < 0) 489729c91e99STejun Heo goto fail; 489829c91e99STejun Heo 489929c91e99STejun Heo /* create and start the initial worker */ 49003347fa09STejun Heo if (wq_online && !create_worker(pool)) 490129c91e99STejun Heo goto fail; 490229c91e99STejun Heo 490329c91e99STejun Heo /* install */ 490429c91e99STejun Heo hash_add(unbound_pool_hash, &pool->hash_node, hash); 49053fb1823cSLai Jiangshan 490629c91e99STejun Heo return pool; 490729c91e99STejun Heo fail: 490829c91e99STejun Heo if (pool) 490929c91e99STejun Heo put_unbound_pool(pool); 491029c91e99STejun Heo return NULL; 491129c91e99STejun Heo } 491229c91e99STejun Heo 49138864b4e5STejun Heo static void rcu_free_pwq(struct rcu_head *rcu) 49148864b4e5STejun Heo { 49158864b4e5STejun Heo kmem_cache_free(pwq_cache, 49168864b4e5STejun Heo container_of(rcu, struct pool_workqueue, rcu)); 49178864b4e5STejun Heo } 49188864b4e5STejun Heo 49198864b4e5STejun Heo /* 4920967b494eSTejun Heo * Scheduled on pwq_release_worker by put_pwq() when an unbound pwq hits zero 4921967b494eSTejun Heo * refcnt and needs to be destroyed. 49228864b4e5STejun Heo */ 4923687a9aa5STejun Heo static void pwq_release_workfn(struct kthread_work *work) 49248864b4e5STejun Heo { 49258864b4e5STejun Heo struct pool_workqueue *pwq = container_of(work, struct pool_workqueue, 4926687a9aa5STejun Heo release_work); 49278864b4e5STejun Heo struct workqueue_struct *wq = pwq->wq; 49288864b4e5STejun Heo struct worker_pool *pool = pwq->pool; 4929b42b0bddSYang Yingliang bool is_last = false; 49308864b4e5STejun Heo 4931b42b0bddSYang Yingliang /* 4932687a9aa5STejun Heo * When @pwq is not linked, it doesn't hold any reference to the 4933b42b0bddSYang Yingliang * @wq, and @wq is invalid to access. 4934b42b0bddSYang Yingliang */ 4935b42b0bddSYang Yingliang if (!list_empty(&pwq->pwqs_node)) { 49363c25a55dSLai Jiangshan mutex_lock(&wq->mutex); 49378864b4e5STejun Heo list_del_rcu(&pwq->pwqs_node); 4938bc0caf09STejun Heo is_last = list_empty(&wq->pwqs); 49394c065dbcSWaiman Long 49404c065dbcSWaiman Long /* 49414c065dbcSWaiman Long * For ordered workqueue with a plugged dfl_pwq, restart it now. 49424c065dbcSWaiman Long */ 49434c065dbcSWaiman Long if (!is_last && (wq->flags & __WQ_ORDERED)) 49444c065dbcSWaiman Long unplug_oldest_pwq(wq); 49454c065dbcSWaiman Long 49463c25a55dSLai Jiangshan mutex_unlock(&wq->mutex); 4947b42b0bddSYang Yingliang } 49488864b4e5STejun Heo 4949687a9aa5STejun Heo if (wq->flags & WQ_UNBOUND) { 4950a892caccSTejun Heo mutex_lock(&wq_pool_mutex); 49518864b4e5STejun Heo put_unbound_pool(pool); 4952a892caccSTejun Heo mutex_unlock(&wq_pool_mutex); 4953687a9aa5STejun Heo } 4954a892caccSTejun Heo 49555797b1c1STejun Heo if (!list_empty(&pwq->pending_node)) { 49565797b1c1STejun Heo struct wq_node_nr_active *nna = 49575797b1c1STejun Heo wq_node_nr_active(pwq->wq, pwq->pool->node); 49585797b1c1STejun Heo 49595797b1c1STejun Heo raw_spin_lock_irq(&nna->lock); 49605797b1c1STejun Heo list_del_init(&pwq->pending_node); 49615797b1c1STejun Heo raw_spin_unlock_irq(&nna->lock); 49625797b1c1STejun Heo } 49635797b1c1STejun Heo 496425b00775SPaul E. McKenney call_rcu(&pwq->rcu, rcu_free_pwq); 49658864b4e5STejun Heo 49668864b4e5STejun Heo /* 49678864b4e5STejun Heo * If we're the last pwq going away, @wq is already dead and no one 4968e2dca7adSTejun Heo * is gonna access it anymore. Schedule RCU free. 49698864b4e5STejun Heo */ 4970669de8bdSBart Van Assche if (is_last) { 4971669de8bdSBart Van Assche wq_unregister_lockdep(wq); 497225b00775SPaul E. McKenney call_rcu(&wq->rcu, rcu_free_wq); 49736029a918STejun Heo } 4974669de8bdSBart Van Assche } 49758864b4e5STejun Heo 497667dc8325SCai Huoqing /* initialize newly allocated @pwq which is associated with @wq and @pool */ 4977f147f29eSTejun Heo static void init_pwq(struct pool_workqueue *pwq, struct workqueue_struct *wq, 4978f147f29eSTejun Heo struct worker_pool *pool) 4979d2c1d404STejun Heo { 4980e9a8e01fSTejun Heo BUG_ON((unsigned long)pwq & ~WORK_STRUCT_PWQ_MASK); 4981d2c1d404STejun Heo 4982e50aba9aSTejun Heo memset(pwq, 0, sizeof(*pwq)); 4983e50aba9aSTejun Heo 4984d2c1d404STejun Heo pwq->pool = pool; 4985d2c1d404STejun Heo pwq->wq = wq; 4986d2c1d404STejun Heo pwq->flush_color = -1; 49878864b4e5STejun Heo pwq->refcnt = 1; 4988f97a4a1aSLai Jiangshan INIT_LIST_HEAD(&pwq->inactive_works); 49895797b1c1STejun Heo INIT_LIST_HEAD(&pwq->pending_node); 49901befcf30STejun Heo INIT_LIST_HEAD(&pwq->pwqs_node); 4991d2c1d404STejun Heo INIT_LIST_HEAD(&pwq->mayday_node); 4992687a9aa5STejun Heo kthread_init_work(&pwq->release_work, pwq_release_workfn); 4993f147f29eSTejun Heo } 4994d2c1d404STejun Heo 4995f147f29eSTejun Heo /* sync @pwq with the current state of its associated wq and link it */ 49961befcf30STejun Heo static void link_pwq(struct pool_workqueue *pwq) 4997f147f29eSTejun Heo { 4998f147f29eSTejun Heo struct workqueue_struct *wq = pwq->wq; 4999f147f29eSTejun Heo 5000f147f29eSTejun Heo lockdep_assert_held(&wq->mutex); 500175ccf595STejun Heo 50021befcf30STejun Heo /* may be called multiple times, ignore if already linked */ 50031befcf30STejun Heo if (!list_empty(&pwq->pwqs_node)) 50041befcf30STejun Heo return; 50051befcf30STejun Heo 500629b1cb41SLai Jiangshan /* set the matching work_color */ 500775ccf595STejun Heo pwq->work_color = wq->work_color; 5008983ca25eSTejun Heo 5009983ca25eSTejun Heo /* link in @pwq */ 501026fb7e3dSWaiman Long list_add_tail_rcu(&pwq->pwqs_node, &wq->pwqs); 5011df2d5ae4STejun Heo } 50126029a918STejun Heo 5013f147f29eSTejun Heo /* obtain a pool matching @attr and create a pwq associating the pool and @wq */ 5014f147f29eSTejun Heo static struct pool_workqueue *alloc_unbound_pwq(struct workqueue_struct *wq, 5015f147f29eSTejun Heo const struct workqueue_attrs *attrs) 5016f147f29eSTejun Heo { 5017f147f29eSTejun Heo struct worker_pool *pool; 5018f147f29eSTejun Heo struct pool_workqueue *pwq; 5019f147f29eSTejun Heo 5020f147f29eSTejun Heo lockdep_assert_held(&wq_pool_mutex); 5021f147f29eSTejun Heo 5022f147f29eSTejun Heo pool = get_unbound_pool(attrs); 5023f147f29eSTejun Heo if (!pool) 5024f147f29eSTejun Heo return NULL; 5025f147f29eSTejun Heo 5026e50aba9aSTejun Heo pwq = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, pool->node); 5027f147f29eSTejun Heo if (!pwq) { 5028f147f29eSTejun Heo put_unbound_pool(pool); 5029f147f29eSTejun Heo return NULL; 5030f147f29eSTejun Heo } 5031f147f29eSTejun Heo 5032f147f29eSTejun Heo init_pwq(pwq, wq, pool); 5033f147f29eSTejun Heo return pwq; 5034d2c1d404STejun Heo } 5035d2c1d404STejun Heo 50364c16bd32STejun Heo /** 5037fef59c9cSTejun Heo * wq_calc_pod_cpumask - calculate a wq_attrs' cpumask for a pod 5038042f7df1SLai Jiangshan * @attrs: the wq_attrs of the default pwq of the target workqueue 503984193c07STejun Heo * @cpu: the target CPU 50404c16bd32STejun Heo * @cpu_going_down: if >= 0, the CPU to consider as offline 50414c16bd32STejun Heo * 5042fef59c9cSTejun Heo * Calculate the cpumask a workqueue with @attrs should use on @pod. If 5043fef59c9cSTejun Heo * @cpu_going_down is >= 0, that cpu is considered offline during calculation. 50449546b29eSTejun Heo * The result is stored in @attrs->__pod_cpumask. 50454c16bd32STejun Heo * 5046fef59c9cSTejun Heo * If pod affinity is not enabled, @attrs->cpumask is always used. If enabled 5047fef59c9cSTejun Heo * and @pod has online CPUs requested by @attrs, the returned cpumask is the 5048fef59c9cSTejun Heo * intersection of the possible CPUs of @pod and @attrs->cpumask. 50494c16bd32STejun Heo * 5050fef59c9cSTejun Heo * The caller is responsible for ensuring that the cpumask of @pod stays stable. 50514c16bd32STejun Heo */ 50529546b29eSTejun Heo static void wq_calc_pod_cpumask(struct workqueue_attrs *attrs, int cpu, 50539546b29eSTejun Heo int cpu_going_down) 50544c16bd32STejun Heo { 505584193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 505684193c07STejun Heo int pod = pt->cpu_pod[cpu]; 50574c16bd32STejun Heo 5058fef59c9cSTejun Heo /* does @pod have any online CPUs @attrs wants? */ 50599546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, pt->pod_cpus[pod], attrs->cpumask); 50609546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->__pod_cpumask, cpu_online_mask); 50614c16bd32STejun Heo if (cpu_going_down >= 0) 50629546b29eSTejun Heo cpumask_clear_cpu(cpu_going_down, attrs->__pod_cpumask); 50634c16bd32STejun Heo 50649546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) { 50659546b29eSTejun Heo cpumask_copy(attrs->__pod_cpumask, attrs->cpumask); 506684193c07STejun Heo return; 506784193c07STejun Heo } 50684c16bd32STejun Heo 5069fef59c9cSTejun Heo /* yeap, return possible CPUs in @pod that @attrs wants */ 50709546b29eSTejun Heo cpumask_and(attrs->__pod_cpumask, attrs->cpumask, pt->pod_cpus[pod]); 50711ad0f0a7SMichael Bringmann 50729546b29eSTejun Heo if (cpumask_empty(attrs->__pod_cpumask)) 50731ad0f0a7SMichael Bringmann pr_warn_once("WARNING: workqueue cpumask: online intersect > " 50741ad0f0a7SMichael Bringmann "possible intersect\n"); 50754c16bd32STejun Heo } 50764c16bd32STejun Heo 50779f66cff2STejun Heo /* install @pwq into @wq and return the old pwq, @cpu < 0 for dfl_pwq */ 5078636b927eSTejun Heo static struct pool_workqueue *install_unbound_pwq(struct workqueue_struct *wq, 5079636b927eSTejun Heo int cpu, struct pool_workqueue *pwq) 50801befcf30STejun Heo { 50819f66cff2STejun Heo struct pool_workqueue __rcu **slot = unbound_pwq_slot(wq, cpu); 50821befcf30STejun Heo struct pool_workqueue *old_pwq; 50831befcf30STejun Heo 50845b95e1afSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 50851befcf30STejun Heo lockdep_assert_held(&wq->mutex); 50861befcf30STejun Heo 50871befcf30STejun Heo /* link_pwq() can handle duplicate calls */ 50881befcf30STejun Heo link_pwq(pwq); 50891befcf30STejun Heo 50909f66cff2STejun Heo old_pwq = rcu_access_pointer(*slot); 50919f66cff2STejun Heo rcu_assign_pointer(*slot, pwq); 50921befcf30STejun Heo return old_pwq; 50931befcf30STejun Heo } 50941befcf30STejun Heo 50952d5f0764SLai Jiangshan /* context to store the prepared attrs & pwqs before applying */ 50962d5f0764SLai Jiangshan struct apply_wqattrs_ctx { 50972d5f0764SLai Jiangshan struct workqueue_struct *wq; /* target workqueue */ 50982d5f0764SLai Jiangshan struct workqueue_attrs *attrs; /* attrs to apply */ 5099042f7df1SLai Jiangshan struct list_head list; /* queued for batching commit */ 51002d5f0764SLai Jiangshan struct pool_workqueue *dfl_pwq; 51012d5f0764SLai Jiangshan struct pool_workqueue *pwq_tbl[]; 51022d5f0764SLai Jiangshan }; 51032d5f0764SLai Jiangshan 51042d5f0764SLai Jiangshan /* free the resources after success or abort */ 51052d5f0764SLai Jiangshan static void apply_wqattrs_cleanup(struct apply_wqattrs_ctx *ctx) 51062d5f0764SLai Jiangshan { 51072d5f0764SLai Jiangshan if (ctx) { 5108636b927eSTejun Heo int cpu; 51092d5f0764SLai Jiangshan 5110636b927eSTejun Heo for_each_possible_cpu(cpu) 5111636b927eSTejun Heo put_pwq_unlocked(ctx->pwq_tbl[cpu]); 51122d5f0764SLai Jiangshan put_pwq_unlocked(ctx->dfl_pwq); 51132d5f0764SLai Jiangshan 51142d5f0764SLai Jiangshan free_workqueue_attrs(ctx->attrs); 51152d5f0764SLai Jiangshan 51162d5f0764SLai Jiangshan kfree(ctx); 51172d5f0764SLai Jiangshan } 51182d5f0764SLai Jiangshan } 51192d5f0764SLai Jiangshan 51202d5f0764SLai Jiangshan /* allocate the attrs and pwqs for later installation */ 51212d5f0764SLai Jiangshan static struct apply_wqattrs_ctx * 51222d5f0764SLai Jiangshan apply_wqattrs_prepare(struct workqueue_struct *wq, 512399c621efSLai Jiangshan const struct workqueue_attrs *attrs, 512499c621efSLai Jiangshan const cpumask_var_t unbound_cpumask) 51252d5f0764SLai Jiangshan { 51262d5f0764SLai Jiangshan struct apply_wqattrs_ctx *ctx; 51279546b29eSTejun Heo struct workqueue_attrs *new_attrs; 5128636b927eSTejun Heo int cpu; 51292d5f0764SLai Jiangshan 51302d5f0764SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 51312d5f0764SLai Jiangshan 513284193c07STejun Heo if (WARN_ON(attrs->affn_scope < 0 || 513384193c07STejun Heo attrs->affn_scope >= WQ_AFFN_NR_TYPES)) 513484193c07STejun Heo return ERR_PTR(-EINVAL); 513584193c07STejun Heo 5136636b927eSTejun Heo ctx = kzalloc(struct_size(ctx, pwq_tbl, nr_cpu_ids), GFP_KERNEL); 51372d5f0764SLai Jiangshan 5138be69d00dSThomas Gleixner new_attrs = alloc_workqueue_attrs(); 51399546b29eSTejun Heo if (!ctx || !new_attrs) 51402d5f0764SLai Jiangshan goto out_free; 51412d5f0764SLai Jiangshan 5142042f7df1SLai Jiangshan /* 51432d5f0764SLai Jiangshan * If something goes wrong during CPU up/down, we'll fall back to 51442d5f0764SLai Jiangshan * the default pwq covering whole @attrs->cpumask. Always create 51452d5f0764SLai Jiangshan * it even if we don't use it immediately. 51462d5f0764SLai Jiangshan */ 51470f36ee24STejun Heo copy_workqueue_attrs(new_attrs, attrs); 51480f36ee24STejun Heo wqattrs_actualize_cpumask(new_attrs, unbound_cpumask); 51499546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 51502d5f0764SLai Jiangshan ctx->dfl_pwq = alloc_unbound_pwq(wq, new_attrs); 51512d5f0764SLai Jiangshan if (!ctx->dfl_pwq) 51522d5f0764SLai Jiangshan goto out_free; 51532d5f0764SLai Jiangshan 5154636b927eSTejun Heo for_each_possible_cpu(cpu) { 5155af73f5c9STejun Heo if (new_attrs->ordered) { 51562d5f0764SLai Jiangshan ctx->dfl_pwq->refcnt++; 5157636b927eSTejun Heo ctx->pwq_tbl[cpu] = ctx->dfl_pwq; 5158636b927eSTejun Heo } else { 51599546b29eSTejun Heo wq_calc_pod_cpumask(new_attrs, cpu, -1); 51609546b29eSTejun Heo ctx->pwq_tbl[cpu] = alloc_unbound_pwq(wq, new_attrs); 5161636b927eSTejun Heo if (!ctx->pwq_tbl[cpu]) 5162636b927eSTejun Heo goto out_free; 51632d5f0764SLai Jiangshan } 51642d5f0764SLai Jiangshan } 51652d5f0764SLai Jiangshan 5166042f7df1SLai Jiangshan /* save the user configured attrs and sanitize it. */ 5167042f7df1SLai Jiangshan copy_workqueue_attrs(new_attrs, attrs); 5168042f7df1SLai Jiangshan cpumask_and(new_attrs->cpumask, new_attrs->cpumask, cpu_possible_mask); 51699546b29eSTejun Heo cpumask_copy(new_attrs->__pod_cpumask, new_attrs->cpumask); 51702d5f0764SLai Jiangshan ctx->attrs = new_attrs; 5171042f7df1SLai Jiangshan 51724c065dbcSWaiman Long /* 51734c065dbcSWaiman Long * For initialized ordered workqueues, there should only be one pwq 51744c065dbcSWaiman Long * (dfl_pwq). Set the plugged flag of ctx->dfl_pwq to suspend execution 51754c065dbcSWaiman Long * of newly queued work items until execution of older work items in 51764c065dbcSWaiman Long * the old pwq's have completed. 51774c065dbcSWaiman Long */ 51784c065dbcSWaiman Long if ((wq->flags & __WQ_ORDERED) && !list_empty(&wq->pwqs)) 51794c065dbcSWaiman Long ctx->dfl_pwq->plugged = true; 51804c065dbcSWaiman Long 51812d5f0764SLai Jiangshan ctx->wq = wq; 51822d5f0764SLai Jiangshan return ctx; 51832d5f0764SLai Jiangshan 51842d5f0764SLai Jiangshan out_free: 51852d5f0764SLai Jiangshan free_workqueue_attrs(new_attrs); 51862d5f0764SLai Jiangshan apply_wqattrs_cleanup(ctx); 518784193c07STejun Heo return ERR_PTR(-ENOMEM); 51882d5f0764SLai Jiangshan } 51892d5f0764SLai Jiangshan 51902d5f0764SLai Jiangshan /* set attrs and install prepared pwqs, @ctx points to old pwqs on return */ 51912d5f0764SLai Jiangshan static void apply_wqattrs_commit(struct apply_wqattrs_ctx *ctx) 51922d5f0764SLai Jiangshan { 5193636b927eSTejun Heo int cpu; 51942d5f0764SLai Jiangshan 51952d5f0764SLai Jiangshan /* all pwqs have been created successfully, let's install'em */ 51962d5f0764SLai Jiangshan mutex_lock(&ctx->wq->mutex); 51972d5f0764SLai Jiangshan 51982d5f0764SLai Jiangshan copy_workqueue_attrs(ctx->wq->unbound_attrs, ctx->attrs); 51992d5f0764SLai Jiangshan 52009f66cff2STejun Heo /* save the previous pwqs and install the new ones */ 5201636b927eSTejun Heo for_each_possible_cpu(cpu) 5202636b927eSTejun Heo ctx->pwq_tbl[cpu] = install_unbound_pwq(ctx->wq, cpu, 5203636b927eSTejun Heo ctx->pwq_tbl[cpu]); 52049f66cff2STejun Heo ctx->dfl_pwq = install_unbound_pwq(ctx->wq, -1, ctx->dfl_pwq); 52052d5f0764SLai Jiangshan 52065797b1c1STejun Heo /* update node_nr_active->max */ 52075797b1c1STejun Heo wq_update_node_max_active(ctx->wq, -1); 52085797b1c1STejun Heo 5209d64f2fa0SJuri Lelli /* rescuer needs to respect wq cpumask changes */ 5210d64f2fa0SJuri Lelli if (ctx->wq->rescuer) 5211d64f2fa0SJuri Lelli set_cpus_allowed_ptr(ctx->wq->rescuer->task, 5212d64f2fa0SJuri Lelli unbound_effective_cpumask(ctx->wq)); 5213d64f2fa0SJuri Lelli 52142d5f0764SLai Jiangshan mutex_unlock(&ctx->wq->mutex); 52152d5f0764SLai Jiangshan } 52162d5f0764SLai Jiangshan 5217a0111cf6SLai Jiangshan static int apply_workqueue_attrs_locked(struct workqueue_struct *wq, 5218a0111cf6SLai Jiangshan const struct workqueue_attrs *attrs) 5219a0111cf6SLai Jiangshan { 5220a0111cf6SLai Jiangshan struct apply_wqattrs_ctx *ctx; 5221a0111cf6SLai Jiangshan 5222a0111cf6SLai Jiangshan /* only unbound workqueues can change attributes */ 5223a0111cf6SLai Jiangshan if (WARN_ON(!(wq->flags & WQ_UNBOUND))) 5224a0111cf6SLai Jiangshan return -EINVAL; 5225a0111cf6SLai Jiangshan 522699c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, attrs, wq_unbound_cpumask); 522784193c07STejun Heo if (IS_ERR(ctx)) 522884193c07STejun Heo return PTR_ERR(ctx); 5229a0111cf6SLai Jiangshan 5230a0111cf6SLai Jiangshan /* the ctx has been prepared successfully, let's commit it */ 5231a0111cf6SLai Jiangshan apply_wqattrs_commit(ctx); 5232a0111cf6SLai Jiangshan apply_wqattrs_cleanup(ctx); 5233a0111cf6SLai Jiangshan 52346201171eSwanghaibin return 0; 5235a0111cf6SLai Jiangshan } 5236a0111cf6SLai Jiangshan 52379e8cd2f5STejun Heo /** 52389e8cd2f5STejun Heo * apply_workqueue_attrs - apply new workqueue_attrs to an unbound workqueue 52399e8cd2f5STejun Heo * @wq: the target workqueue 52409e8cd2f5STejun Heo * @attrs: the workqueue_attrs to apply, allocated with alloc_workqueue_attrs() 52419e8cd2f5STejun Heo * 5242fef59c9cSTejun Heo * Apply @attrs to an unbound workqueue @wq. Unless disabled, this function maps 5243fef59c9cSTejun Heo * a separate pwq to each CPU pod with possibles CPUs in @attrs->cpumask so that 5244fef59c9cSTejun Heo * work items are affine to the pod it was issued on. Older pwqs are released as 5245fef59c9cSTejun Heo * in-flight work items finish. Note that a work item which repeatedly requeues 5246fef59c9cSTejun Heo * itself back-to-back will stay on its current pwq. 52479e8cd2f5STejun Heo * 5248d185af30SYacine Belkadi * Performs GFP_KERNEL allocations. 5249d185af30SYacine Belkadi * 5250ffd8bea8SSebastian Andrzej Siewior * Assumes caller has CPU hotplug read exclusion, i.e. cpus_read_lock(). 5251509b3204SDaniel Jordan * 5252d185af30SYacine Belkadi * Return: 0 on success and -errno on failure. 52539e8cd2f5STejun Heo */ 5254513c98d0SDaniel Jordan int apply_workqueue_attrs(struct workqueue_struct *wq, 52559e8cd2f5STejun Heo const struct workqueue_attrs *attrs) 52569e8cd2f5STejun Heo { 5257a0111cf6SLai Jiangshan int ret; 52589e8cd2f5STejun Heo 5259509b3204SDaniel Jordan lockdep_assert_cpus_held(); 5260509b3204SDaniel Jordan 5261509b3204SDaniel Jordan mutex_lock(&wq_pool_mutex); 5262a0111cf6SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 5263509b3204SDaniel Jordan mutex_unlock(&wq_pool_mutex); 52642d5f0764SLai Jiangshan 52652d5f0764SLai Jiangshan return ret; 52669e8cd2f5STejun Heo } 52679e8cd2f5STejun Heo 52684c16bd32STejun Heo /** 5269fef59c9cSTejun Heo * wq_update_pod - update pod affinity of a wq for CPU hot[un]plug 52704c16bd32STejun Heo * @wq: the target workqueue 52714cbfd3deSTejun Heo * @cpu: the CPU to update pool association for 52724cbfd3deSTejun Heo * @hotplug_cpu: the CPU coming up or going down 52734c16bd32STejun Heo * @online: whether @cpu is coming up or going down 52744c16bd32STejun Heo * 52754c16bd32STejun Heo * This function is to be called from %CPU_DOWN_PREPARE, %CPU_ONLINE and 5276fef59c9cSTejun Heo * %CPU_DOWN_FAILED. @cpu is being hot[un]plugged, update pod affinity of 52774c16bd32STejun Heo * @wq accordingly. 52784c16bd32STejun Heo * 52794c16bd32STejun Heo * 5280fef59c9cSTejun Heo * If pod affinity can't be adjusted due to memory allocation failure, it falls 5281fef59c9cSTejun Heo * back to @wq->dfl_pwq which may not be optimal but is always correct. 5282fef59c9cSTejun Heo * 5283fef59c9cSTejun Heo * Note that when the last allowed CPU of a pod goes offline for a workqueue 5284fef59c9cSTejun Heo * with a cpumask spanning multiple pods, the workers which were already 5285fef59c9cSTejun Heo * executing the work items for the workqueue will lose their CPU affinity and 5286fef59c9cSTejun Heo * may execute on any CPU. This is similar to how per-cpu workqueues behave on 5287fef59c9cSTejun Heo * CPU_DOWN. If a workqueue user wants strict affinity, it's the user's 5288fef59c9cSTejun Heo * responsibility to flush the work item from CPU_DOWN_PREPARE. 52894c16bd32STejun Heo */ 5290fef59c9cSTejun Heo static void wq_update_pod(struct workqueue_struct *wq, int cpu, 52914cbfd3deSTejun Heo int hotplug_cpu, bool online) 52924c16bd32STejun Heo { 52934cbfd3deSTejun Heo int off_cpu = online ? -1 : hotplug_cpu; 52944c16bd32STejun Heo struct pool_workqueue *old_pwq = NULL, *pwq; 52954c16bd32STejun Heo struct workqueue_attrs *target_attrs; 52964c16bd32STejun Heo 52974c16bd32STejun Heo lockdep_assert_held(&wq_pool_mutex); 52984c16bd32STejun Heo 529984193c07STejun Heo if (!(wq->flags & WQ_UNBOUND) || wq->unbound_attrs->ordered) 53004c16bd32STejun Heo return; 53014c16bd32STejun Heo 53024c16bd32STejun Heo /* 53034c16bd32STejun Heo * We don't wanna alloc/free wq_attrs for each wq for each CPU. 53044c16bd32STejun Heo * Let's use a preallocated one. The following buf is protected by 53054c16bd32STejun Heo * CPU hotplug exclusion. 53064c16bd32STejun Heo */ 5307fef59c9cSTejun Heo target_attrs = wq_update_pod_attrs_buf; 53084c16bd32STejun Heo 53094c16bd32STejun Heo copy_workqueue_attrs(target_attrs, wq->unbound_attrs); 53100f36ee24STejun Heo wqattrs_actualize_cpumask(target_attrs, wq_unbound_cpumask); 53114c16bd32STejun Heo 5312636b927eSTejun Heo /* nothing to do if the target cpumask matches the current pwq */ 53139546b29eSTejun Heo wq_calc_pod_cpumask(target_attrs, cpu, off_cpu); 53149f66cff2STejun Heo if (wqattrs_equal(target_attrs, unbound_pwq(wq, cpu)->pool->attrs)) 5315f7142ed4SLai Jiangshan return; 53164c16bd32STejun Heo 53174c16bd32STejun Heo /* create a new pwq */ 53184c16bd32STejun Heo pwq = alloc_unbound_pwq(wq, target_attrs); 53194c16bd32STejun Heo if (!pwq) { 5320fef59c9cSTejun Heo pr_warn("workqueue: allocation failed while updating CPU pod affinity of \"%s\"\n", 53214c16bd32STejun Heo wq->name); 532277f300b1SDaeseok Youn goto use_dfl_pwq; 53234c16bd32STejun Heo } 53244c16bd32STejun Heo 5325f7142ed4SLai Jiangshan /* Install the new pwq. */ 53264c16bd32STejun Heo mutex_lock(&wq->mutex); 5327636b927eSTejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 53284c16bd32STejun Heo goto out_unlock; 53294c16bd32STejun Heo 53304c16bd32STejun Heo use_dfl_pwq: 5331f7142ed4SLai Jiangshan mutex_lock(&wq->mutex); 53329f66cff2STejun Heo pwq = unbound_pwq(wq, -1); 53339f66cff2STejun Heo raw_spin_lock_irq(&pwq->pool->lock); 53349f66cff2STejun Heo get_pwq(pwq); 53359f66cff2STejun Heo raw_spin_unlock_irq(&pwq->pool->lock); 53369f66cff2STejun Heo old_pwq = install_unbound_pwq(wq, cpu, pwq); 53374c16bd32STejun Heo out_unlock: 53384c16bd32STejun Heo mutex_unlock(&wq->mutex); 53394c16bd32STejun Heo put_pwq_unlocked(old_pwq); 53404c16bd32STejun Heo } 53414c16bd32STejun Heo 534230cdf249STejun Heo static int alloc_and_link_pwqs(struct workqueue_struct *wq) 53431da177e4SLinus Torvalds { 534449e3cf44STejun Heo bool highpri = wq->flags & WQ_HIGHPRI; 53458a2b7538STejun Heo int cpu, ret; 5346e1d8aa9fSFrederic Weisbecker 5347687a9aa5STejun Heo wq->cpu_pwq = alloc_percpu(struct pool_workqueue *); 5348ee1ceef7STejun Heo if (!wq->cpu_pwq) 5349687a9aa5STejun Heo goto enomem; 535030cdf249STejun Heo 5351636b927eSTejun Heo if (!(wq->flags & WQ_UNBOUND)) { 535230cdf249STejun Heo for_each_possible_cpu(cpu) { 53534cb1ef64STejun Heo struct pool_workqueue **pwq_p; 53544cb1ef64STejun Heo struct worker_pool __percpu *pools; 53554cb1ef64STejun Heo struct worker_pool *pool; 53564cb1ef64STejun Heo 53574cb1ef64STejun Heo if (wq->flags & WQ_BH) 53584cb1ef64STejun Heo pools = bh_worker_pools; 53594cb1ef64STejun Heo else 53604cb1ef64STejun Heo pools = cpu_worker_pools; 53614cb1ef64STejun Heo 53624cb1ef64STejun Heo pool = &(per_cpu_ptr(pools, cpu)[highpri]); 53634cb1ef64STejun Heo pwq_p = per_cpu_ptr(wq->cpu_pwq, cpu); 536430cdf249STejun Heo 5365687a9aa5STejun Heo *pwq_p = kmem_cache_alloc_node(pwq_cache, GFP_KERNEL, 5366687a9aa5STejun Heo pool->node); 5367687a9aa5STejun Heo if (!*pwq_p) 5368687a9aa5STejun Heo goto enomem; 5369687a9aa5STejun Heo 5370687a9aa5STejun Heo init_pwq(*pwq_p, wq, pool); 5371f147f29eSTejun Heo 5372f147f29eSTejun Heo mutex_lock(&wq->mutex); 5373687a9aa5STejun Heo link_pwq(*pwq_p); 5374f147f29eSTejun Heo mutex_unlock(&wq->mutex); 537530cdf249STejun Heo } 537630cdf249STejun Heo return 0; 5377509b3204SDaniel Jordan } 5378509b3204SDaniel Jordan 5379ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 5380509b3204SDaniel Jordan if (wq->flags & __WQ_ORDERED) { 53819f66cff2STejun Heo struct pool_workqueue *dfl_pwq; 53829f66cff2STejun Heo 53838a2b7538STejun Heo ret = apply_workqueue_attrs(wq, ordered_wq_attrs[highpri]); 53848a2b7538STejun Heo /* there should only be single pwq for ordering guarantee */ 53859f66cff2STejun Heo dfl_pwq = rcu_access_pointer(wq->dfl_pwq); 53869f66cff2STejun Heo WARN(!ret && (wq->pwqs.next != &dfl_pwq->pwqs_node || 53879f66cff2STejun Heo wq->pwqs.prev != &dfl_pwq->pwqs_node), 53888a2b7538STejun Heo "ordering guarantee broken for workqueue %s\n", wq->name); 53899e8cd2f5STejun Heo } else { 5390509b3204SDaniel Jordan ret = apply_workqueue_attrs(wq, unbound_std_wq_attrs[highpri]); 53919e8cd2f5STejun Heo } 5392ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 5393509b3204SDaniel Jordan 539464344553SZqiang /* for unbound pwq, flush the pwq_release_worker ensures that the 539564344553SZqiang * pwq_release_workfn() completes before calling kfree(wq). 539664344553SZqiang */ 539764344553SZqiang if (ret) 539864344553SZqiang kthread_flush_worker(pwq_release_worker); 539964344553SZqiang 5400509b3204SDaniel Jordan return ret; 5401687a9aa5STejun Heo 5402687a9aa5STejun Heo enomem: 5403687a9aa5STejun Heo if (wq->cpu_pwq) { 54047b42f401SZqiang for_each_possible_cpu(cpu) { 54057b42f401SZqiang struct pool_workqueue *pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 54067b42f401SZqiang 54077b42f401SZqiang if (pwq) 54087b42f401SZqiang kmem_cache_free(pwq_cache, pwq); 54097b42f401SZqiang } 5410687a9aa5STejun Heo free_percpu(wq->cpu_pwq); 5411687a9aa5STejun Heo wq->cpu_pwq = NULL; 5412687a9aa5STejun Heo } 5413687a9aa5STejun Heo return -ENOMEM; 54140f900049STejun Heo } 54150f900049STejun Heo 5416f3421797STejun Heo static int wq_clamp_max_active(int max_active, unsigned int flags, 5417f3421797STejun Heo const char *name) 5418b71ab8c2STejun Heo { 5419636b927eSTejun Heo if (max_active < 1 || max_active > WQ_MAX_ACTIVE) 5420044c782cSValentin Ilie pr_warn("workqueue: max_active %d requested for %s is out of range, clamping between %d and %d\n", 5421636b927eSTejun Heo max_active, name, 1, WQ_MAX_ACTIVE); 5422b71ab8c2STejun Heo 5423636b927eSTejun Heo return clamp_val(max_active, 1, WQ_MAX_ACTIVE); 5424b71ab8c2STejun Heo } 5425b71ab8c2STejun Heo 5426983c7515STejun Heo /* 5427983c7515STejun Heo * Workqueues which may be used during memory reclaim should have a rescuer 5428983c7515STejun Heo * to guarantee forward progress. 5429983c7515STejun Heo */ 5430983c7515STejun Heo static int init_rescuer(struct workqueue_struct *wq) 5431983c7515STejun Heo { 5432983c7515STejun Heo struct worker *rescuer; 5433b92b36eaSDan Carpenter int ret; 5434983c7515STejun Heo 5435983c7515STejun Heo if (!(wq->flags & WQ_MEM_RECLAIM)) 5436983c7515STejun Heo return 0; 5437983c7515STejun Heo 5438983c7515STejun Heo rescuer = alloc_worker(NUMA_NO_NODE); 54394c0736a7SPetr Mladek if (!rescuer) { 54404c0736a7SPetr Mladek pr_err("workqueue: Failed to allocate a rescuer for wq \"%s\"\n", 54414c0736a7SPetr Mladek wq->name); 5442983c7515STejun Heo return -ENOMEM; 54434c0736a7SPetr Mladek } 5444983c7515STejun Heo 5445983c7515STejun Heo rescuer->rescue_wq = wq; 5446b6a46f72SAaron Tomlin rescuer->task = kthread_create(rescuer_thread, rescuer, "kworker/R-%s", wq->name); 5447f187b697SSean Fu if (IS_ERR(rescuer->task)) { 5448b92b36eaSDan Carpenter ret = PTR_ERR(rescuer->task); 54494c0736a7SPetr Mladek pr_err("workqueue: Failed to create a rescuer kthread for wq \"%s\": %pe", 54504c0736a7SPetr Mladek wq->name, ERR_PTR(ret)); 5451983c7515STejun Heo kfree(rescuer); 5452b92b36eaSDan Carpenter return ret; 5453983c7515STejun Heo } 5454983c7515STejun Heo 5455983c7515STejun Heo wq->rescuer = rescuer; 545685f0ab43SJuri Lelli if (wq->flags & WQ_UNBOUND) 545749584bb8SWaiman Long kthread_bind_mask(rescuer->task, wq_unbound_cpumask); 545885f0ab43SJuri Lelli else 5459983c7515STejun Heo kthread_bind_mask(rescuer->task, cpu_possible_mask); 5460983c7515STejun Heo wake_up_process(rescuer->task); 5461983c7515STejun Heo 5462983c7515STejun Heo return 0; 5463983c7515STejun Heo } 5464983c7515STejun Heo 5465a045a272STejun Heo /** 5466a045a272STejun Heo * wq_adjust_max_active - update a wq's max_active to the current setting 5467a045a272STejun Heo * @wq: target workqueue 5468a045a272STejun Heo * 5469a045a272STejun Heo * If @wq isn't freezing, set @wq->max_active to the saved_max_active and 5470a045a272STejun Heo * activate inactive work items accordingly. If @wq is freezing, clear 5471a045a272STejun Heo * @wq->max_active to zero. 5472a045a272STejun Heo */ 5473a045a272STejun Heo static void wq_adjust_max_active(struct workqueue_struct *wq) 5474a045a272STejun Heo { 5475c5404d4eSTejun Heo bool activated; 54765797b1c1STejun Heo int new_max, new_min; 5477a045a272STejun Heo 5478a045a272STejun Heo lockdep_assert_held(&wq->mutex); 5479a045a272STejun Heo 5480a045a272STejun Heo if ((wq->flags & WQ_FREEZABLE) && workqueue_freezing) { 54815797b1c1STejun Heo new_max = 0; 54825797b1c1STejun Heo new_min = 0; 54835797b1c1STejun Heo } else { 54845797b1c1STejun Heo new_max = wq->saved_max_active; 54855797b1c1STejun Heo new_min = wq->saved_min_active; 5486a045a272STejun Heo } 5487a045a272STejun Heo 54885797b1c1STejun Heo if (wq->max_active == new_max && wq->min_active == new_min) 5489a045a272STejun Heo return; 5490a045a272STejun Heo 5491a045a272STejun Heo /* 54925797b1c1STejun Heo * Update @wq->max/min_active and then kick inactive work items if more 5493a045a272STejun Heo * active work items are allowed. This doesn't break work item ordering 5494a045a272STejun Heo * because new work items are always queued behind existing inactive 5495a045a272STejun Heo * work items if there are any. 5496a045a272STejun Heo */ 54975797b1c1STejun Heo WRITE_ONCE(wq->max_active, new_max); 54985797b1c1STejun Heo WRITE_ONCE(wq->min_active, new_min); 54995797b1c1STejun Heo 55005797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 55015797b1c1STejun Heo wq_update_node_max_active(wq, -1); 55025797b1c1STejun Heo 55035797b1c1STejun Heo if (new_max == 0) 55045797b1c1STejun Heo return; 5505a045a272STejun Heo 5506c5404d4eSTejun Heo /* 5507c5404d4eSTejun Heo * Round-robin through pwq's activating the first inactive work item 5508c5404d4eSTejun Heo * until max_active is filled. 5509c5404d4eSTejun Heo */ 5510c5404d4eSTejun Heo do { 5511c5404d4eSTejun Heo struct pool_workqueue *pwq; 5512c5404d4eSTejun Heo 5513c5404d4eSTejun Heo activated = false; 5514a045a272STejun Heo for_each_pwq(pwq, wq) { 5515c26e2f2eSTejun Heo unsigned long irq_flags; 5516a045a272STejun Heo 5517c5404d4eSTejun Heo /* can be called during early boot w/ irq disabled */ 5518c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 55195797b1c1STejun Heo if (pwq_activate_first_inactive(pwq, true)) { 5520c5404d4eSTejun Heo activated = true; 5521a045a272STejun Heo kick_pool(pwq->pool); 5522c5404d4eSTejun Heo } 5523c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 5524a045a272STejun Heo } 5525c5404d4eSTejun Heo } while (activated); 5526a045a272STejun Heo } 5527a045a272STejun Heo 5528a2775bbcSMathieu Malaterre __printf(1, 4) 5529669de8bdSBart Van Assche struct workqueue_struct *alloc_workqueue(const char *fmt, 553097e37d7bSTejun Heo unsigned int flags, 5531669de8bdSBart Van Assche int max_active, ...) 55323af24433SOleg Nesterov { 5533ecf6881fSTejun Heo va_list args; 55343af24433SOleg Nesterov struct workqueue_struct *wq; 553591ccc6e7STejun Heo size_t wq_size; 553691ccc6e7STejun Heo int name_len; 5537b196be89STejun Heo 55384cb1ef64STejun Heo if (flags & WQ_BH) { 55394cb1ef64STejun Heo if (WARN_ON_ONCE(flags & ~__WQ_BH_ALLOWS)) 55404cb1ef64STejun Heo return NULL; 55414cb1ef64STejun Heo if (WARN_ON_ONCE(max_active)) 55424cb1ef64STejun Heo return NULL; 55434cb1ef64STejun Heo } 55444cb1ef64STejun Heo 5545cee22a15SViresh Kumar /* see the comment above the definition of WQ_POWER_EFFICIENT */ 5546cee22a15SViresh Kumar if ((flags & WQ_POWER_EFFICIENT) && wq_power_efficient) 5547cee22a15SViresh Kumar flags |= WQ_UNBOUND; 5548cee22a15SViresh Kumar 5549ecf6881fSTejun Heo /* allocate wq and format name */ 555091ccc6e7STejun Heo if (flags & WQ_UNBOUND) 555191ccc6e7STejun Heo wq_size = struct_size(wq, node_nr_active, nr_node_ids + 1); 555291ccc6e7STejun Heo else 555391ccc6e7STejun Heo wq_size = sizeof(*wq); 555491ccc6e7STejun Heo 555591ccc6e7STejun Heo wq = kzalloc(wq_size, GFP_KERNEL); 5556b196be89STejun Heo if (!wq) 5557d2c1d404STejun Heo return NULL; 5558b196be89STejun Heo 55596029a918STejun Heo if (flags & WQ_UNBOUND) { 5560be69d00dSThomas Gleixner wq->unbound_attrs = alloc_workqueue_attrs(); 55616029a918STejun Heo if (!wq->unbound_attrs) 55626029a918STejun Heo goto err_free_wq; 55636029a918STejun Heo } 55646029a918STejun Heo 5565669de8bdSBart Van Assche va_start(args, max_active); 556691ccc6e7STejun Heo name_len = vsnprintf(wq->name, sizeof(wq->name), fmt, args); 5567b196be89STejun Heo va_end(args); 55683af24433SOleg Nesterov 556991ccc6e7STejun Heo if (name_len >= WQ_NAME_LEN) 557091ccc6e7STejun Heo pr_warn_once("workqueue: name exceeds WQ_NAME_LEN. Truncating to: %s\n", 557191ccc6e7STejun Heo wq->name); 557231c89007SAudra Mitchell 55734cb1ef64STejun Heo if (flags & WQ_BH) { 55744cb1ef64STejun Heo /* 55754cb1ef64STejun Heo * BH workqueues always share a single execution context per CPU 55764cb1ef64STejun Heo * and don't impose any max_active limit. 55774cb1ef64STejun Heo */ 55784cb1ef64STejun Heo max_active = INT_MAX; 55794cb1ef64STejun Heo } else { 5580d320c038STejun Heo max_active = max_active ?: WQ_DFL_ACTIVE; 5581b196be89STejun Heo max_active = wq_clamp_max_active(max_active, flags, wq->name); 55824cb1ef64STejun Heo } 55833af24433SOleg Nesterov 5584b196be89STejun Heo /* init wq */ 558597e37d7bSTejun Heo wq->flags = flags; 5586a045a272STejun Heo wq->max_active = max_active; 55875797b1c1STejun Heo wq->min_active = min(max_active, WQ_DFL_MIN_ACTIVE); 55885797b1c1STejun Heo wq->saved_max_active = wq->max_active; 55895797b1c1STejun Heo wq->saved_min_active = wq->min_active; 55903c25a55dSLai Jiangshan mutex_init(&wq->mutex); 5591112202d9STejun Heo atomic_set(&wq->nr_pwqs_to_flush, 0); 559230cdf249STejun Heo INIT_LIST_HEAD(&wq->pwqs); 559373f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_queue); 559473f53c4aSTejun Heo INIT_LIST_HEAD(&wq->flusher_overflow); 5595493a1724STejun Heo INIT_LIST_HEAD(&wq->maydays); 55963af24433SOleg Nesterov 5597669de8bdSBart Van Assche wq_init_lockdep(wq); 5598cce1a165SOleg Nesterov INIT_LIST_HEAD(&wq->list); 55993af24433SOleg Nesterov 560091ccc6e7STejun Heo if (flags & WQ_UNBOUND) { 560191ccc6e7STejun Heo if (alloc_node_nr_active(wq->node_nr_active) < 0) 560282efcab3SBart Van Assche goto err_unreg_lockdep; 560391ccc6e7STejun Heo } 560491ccc6e7STejun Heo 560591ccc6e7STejun Heo if (alloc_and_link_pwqs(wq) < 0) 560691ccc6e7STejun Heo goto err_free_node_nr_active; 56071537663fSTejun Heo 560840c17f75STejun Heo if (wq_online && init_rescuer(wq) < 0) 5609d2c1d404STejun Heo goto err_destroy; 5610e22bee78STejun Heo 5611226223abSTejun Heo if ((wq->flags & WQ_SYSFS) && workqueue_sysfs_register(wq)) 5612226223abSTejun Heo goto err_destroy; 5613226223abSTejun Heo 56146af8bf3dSOleg Nesterov /* 561568e13a67SLai Jiangshan * wq_pool_mutex protects global freeze state and workqueues list. 561668e13a67SLai Jiangshan * Grab it, adjust max_active and add the new @wq to workqueues 561768e13a67SLai Jiangshan * list. 56186af8bf3dSOleg Nesterov */ 561968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 5620a0a1a5fdSTejun Heo 5621a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5622a045a272STejun Heo wq_adjust_max_active(wq); 5623a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5624a0a1a5fdSTejun Heo 5625e2dca7adSTejun Heo list_add_tail_rcu(&wq->list, &workqueues); 5626a0a1a5fdSTejun Heo 562768e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 56283af24433SOleg Nesterov 56293af24433SOleg Nesterov return wq; 5630d2c1d404STejun Heo 563191ccc6e7STejun Heo err_free_node_nr_active: 563291ccc6e7STejun Heo if (wq->flags & WQ_UNBOUND) 563391ccc6e7STejun Heo free_node_nr_active(wq->node_nr_active); 563482efcab3SBart Van Assche err_unreg_lockdep: 5635009bb421SBart Van Assche wq_unregister_lockdep(wq); 5636009bb421SBart Van Assche wq_free_lockdep(wq); 563782efcab3SBart Van Assche err_free_wq: 56386029a918STejun Heo free_workqueue_attrs(wq->unbound_attrs); 56394690c4abSTejun Heo kfree(wq); 5640d2c1d404STejun Heo return NULL; 5641d2c1d404STejun Heo err_destroy: 5642d2c1d404STejun Heo destroy_workqueue(wq); 56434690c4abSTejun Heo return NULL; 56441da177e4SLinus Torvalds } 5645669de8bdSBart Van Assche EXPORT_SYMBOL_GPL(alloc_workqueue); 56461da177e4SLinus Torvalds 5647c29eb853STejun Heo static bool pwq_busy(struct pool_workqueue *pwq) 5648c29eb853STejun Heo { 5649c29eb853STejun Heo int i; 5650c29eb853STejun Heo 5651c29eb853STejun Heo for (i = 0; i < WORK_NR_COLORS; i++) 5652c29eb853STejun Heo if (pwq->nr_in_flight[i]) 5653c29eb853STejun Heo return true; 5654c29eb853STejun Heo 56559f66cff2STejun Heo if ((pwq != rcu_access_pointer(pwq->wq->dfl_pwq)) && (pwq->refcnt > 1)) 5656c29eb853STejun Heo return true; 5657afa87ce8STejun Heo if (!pwq_is_empty(pwq)) 5658c29eb853STejun Heo return true; 5659c29eb853STejun Heo 5660c29eb853STejun Heo return false; 5661c29eb853STejun Heo } 5662c29eb853STejun Heo 56633af24433SOleg Nesterov /** 56643af24433SOleg Nesterov * destroy_workqueue - safely terminate a workqueue 56653af24433SOleg Nesterov * @wq: target workqueue 56663af24433SOleg Nesterov * 56673af24433SOleg Nesterov * Safely destroy a workqueue. All work currently pending will be done first. 56683af24433SOleg Nesterov */ 56693af24433SOleg Nesterov void destroy_workqueue(struct workqueue_struct *wq) 56703af24433SOleg Nesterov { 567149e3cf44STejun Heo struct pool_workqueue *pwq; 5672636b927eSTejun Heo int cpu; 56733af24433SOleg Nesterov 5674def98c84STejun Heo /* 5675def98c84STejun Heo * Remove it from sysfs first so that sanity check failure doesn't 5676def98c84STejun Heo * lead to sysfs name conflicts. 5677def98c84STejun Heo */ 5678def98c84STejun Heo workqueue_sysfs_unregister(wq); 5679def98c84STejun Heo 568033e3f0a3SRichard Clark /* mark the workqueue destruction is in progress */ 568133e3f0a3SRichard Clark mutex_lock(&wq->mutex); 568233e3f0a3SRichard Clark wq->flags |= __WQ_DESTROYING; 568333e3f0a3SRichard Clark mutex_unlock(&wq->mutex); 568433e3f0a3SRichard Clark 56859c5a2ba7STejun Heo /* drain it before proceeding with destruction */ 56869c5a2ba7STejun Heo drain_workqueue(wq); 5687c8efcc25STejun Heo 5688def98c84STejun Heo /* kill rescuer, if sanity checks fail, leave it w/o rescuer */ 5689def98c84STejun Heo if (wq->rescuer) { 5690def98c84STejun Heo struct worker *rescuer = wq->rescuer; 5691def98c84STejun Heo 5692def98c84STejun Heo /* this prevents new queueing */ 5693a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&wq_mayday_lock); 5694def98c84STejun Heo wq->rescuer = NULL; 5695a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&wq_mayday_lock); 5696def98c84STejun Heo 5697def98c84STejun Heo /* rescuer will empty maydays list before exiting */ 5698def98c84STejun Heo kthread_stop(rescuer->task); 56998efe1223STejun Heo kfree(rescuer); 5700def98c84STejun Heo } 5701def98c84STejun Heo 5702c29eb853STejun Heo /* 5703c29eb853STejun Heo * Sanity checks - grab all the locks so that we wait for all 5704c29eb853STejun Heo * in-flight operations which may do put_pwq(). 5705c29eb853STejun Heo */ 5706c29eb853STejun Heo mutex_lock(&wq_pool_mutex); 5707b09f4fd3SLai Jiangshan mutex_lock(&wq->mutex); 570849e3cf44STejun Heo for_each_pwq(pwq, wq) { 5709a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pwq->pool->lock); 5710c29eb853STejun Heo if (WARN_ON(pwq_busy(pwq))) { 57111d9a6159SKefeng Wang pr_warn("%s: %s has the following busy pwq\n", 5712e66b39afSTejun Heo __func__, wq->name); 5713c29eb853STejun Heo show_pwq(pwq); 5714a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 5715b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 5716c29eb853STejun Heo mutex_unlock(&wq_pool_mutex); 571755df0933SImran Khan show_one_workqueue(wq); 57186183c009STejun Heo return; 571976af4d93STejun Heo } 5720a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pwq->pool->lock); 572176af4d93STejun Heo } 5722b09f4fd3SLai Jiangshan mutex_unlock(&wq->mutex); 57236183c009STejun Heo 5724a0a1a5fdSTejun Heo /* 5725a0a1a5fdSTejun Heo * wq list is used to freeze wq, remove from list after 5726a0a1a5fdSTejun Heo * flushing is complete in case freeze races us. 5727a0a1a5fdSTejun Heo */ 5728e2dca7adSTejun Heo list_del_rcu(&wq->list); 572968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 57303af24433SOleg Nesterov 57318864b4e5STejun Heo /* 5732636b927eSTejun Heo * We're the sole accessor of @wq. Directly access cpu_pwq and dfl_pwq 5733636b927eSTejun Heo * to put the base refs. @wq will be auto-destroyed from the last 5734636b927eSTejun Heo * pwq_put. RCU read lock prevents @wq from going away from under us. 57358864b4e5STejun Heo */ 5736636b927eSTejun Heo rcu_read_lock(); 5737636b927eSTejun Heo 5738636b927eSTejun Heo for_each_possible_cpu(cpu) { 57399f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, cpu)); 57409f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, cpu), NULL); 57414c16bd32STejun Heo } 57424c16bd32STejun Heo 57439f66cff2STejun Heo put_pwq_unlocked(unbound_pwq(wq, -1)); 57449f66cff2STejun Heo RCU_INIT_POINTER(*unbound_pwq_slot(wq, -1), NULL); 5745636b927eSTejun Heo 5746636b927eSTejun Heo rcu_read_unlock(); 57473af24433SOleg Nesterov } 57483af24433SOleg Nesterov EXPORT_SYMBOL_GPL(destroy_workqueue); 57493af24433SOleg Nesterov 5750dcd989cbSTejun Heo /** 5751dcd989cbSTejun Heo * workqueue_set_max_active - adjust max_active of a workqueue 5752dcd989cbSTejun Heo * @wq: target workqueue 5753dcd989cbSTejun Heo * @max_active: new max_active value. 5754dcd989cbSTejun Heo * 57555797b1c1STejun Heo * Set max_active of @wq to @max_active. See the alloc_workqueue() function 57565797b1c1STejun Heo * comment. 5757dcd989cbSTejun Heo * 5758dcd989cbSTejun Heo * CONTEXT: 5759dcd989cbSTejun Heo * Don't call from IRQ context. 5760dcd989cbSTejun Heo */ 5761dcd989cbSTejun Heo void workqueue_set_max_active(struct workqueue_struct *wq, int max_active) 5762dcd989cbSTejun Heo { 57634cb1ef64STejun Heo /* max_active doesn't mean anything for BH workqueues */ 57644cb1ef64STejun Heo if (WARN_ON(wq->flags & WQ_BH)) 57654cb1ef64STejun Heo return; 57668719dceaSTejun Heo /* disallow meddling with max_active for ordered workqueues */ 57673bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 57688719dceaSTejun Heo return; 57698719dceaSTejun Heo 5770f3421797STejun Heo max_active = wq_clamp_max_active(max_active, wq->flags, wq->name); 5771dcd989cbSTejun Heo 5772a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 5773dcd989cbSTejun Heo 5774dcd989cbSTejun Heo wq->saved_max_active = max_active; 57755797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) 57765797b1c1STejun Heo wq->saved_min_active = min(wq->saved_min_active, max_active); 57775797b1c1STejun Heo 5778a045a272STejun Heo wq_adjust_max_active(wq); 5779dcd989cbSTejun Heo 5780a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 5781dcd989cbSTejun Heo } 5782dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_set_max_active); 5783dcd989cbSTejun Heo 5784dcd989cbSTejun Heo /** 57858f172181STejun Heo * workqueue_set_min_active - adjust min_active of an unbound workqueue 57868f172181STejun Heo * @wq: target unbound workqueue 57878f172181STejun Heo * @min_active: new min_active value 57888f172181STejun Heo * 57898f172181STejun Heo * Set min_active of an unbound workqueue. Unlike other types of workqueues, an 57908f172181STejun Heo * unbound workqueue is not guaranteed to be able to process max_active 57918f172181STejun Heo * interdependent work items. Instead, an unbound workqueue is guaranteed to be 57928f172181STejun Heo * able to process min_active number of interdependent work items which is 57938f172181STejun Heo * %WQ_DFL_MIN_ACTIVE by default. 57948f172181STejun Heo * 57958f172181STejun Heo * Use this function to adjust the min_active value between 0 and the current 57968f172181STejun Heo * max_active. 57978f172181STejun Heo */ 57988f172181STejun Heo void workqueue_set_min_active(struct workqueue_struct *wq, int min_active) 57998f172181STejun Heo { 58008f172181STejun Heo /* min_active is only meaningful for non-ordered unbound workqueues */ 58018f172181STejun Heo if (WARN_ON((wq->flags & (WQ_BH | WQ_UNBOUND | __WQ_ORDERED)) != 58028f172181STejun Heo WQ_UNBOUND)) 58038f172181STejun Heo return; 58048f172181STejun Heo 58058f172181STejun Heo mutex_lock(&wq->mutex); 58068f172181STejun Heo wq->saved_min_active = clamp(min_active, 0, wq->saved_max_active); 58078f172181STejun Heo wq_adjust_max_active(wq); 58088f172181STejun Heo mutex_unlock(&wq->mutex); 58098f172181STejun Heo } 58108f172181STejun Heo 58118f172181STejun Heo /** 581227d4ee03SLukas Wunner * current_work - retrieve %current task's work struct 581327d4ee03SLukas Wunner * 581427d4ee03SLukas Wunner * Determine if %current task is a workqueue worker and what it's working on. 581527d4ee03SLukas Wunner * Useful to find out the context that the %current task is running in. 581627d4ee03SLukas Wunner * 581727d4ee03SLukas Wunner * Return: work struct if %current task is a workqueue worker, %NULL otherwise. 581827d4ee03SLukas Wunner */ 581927d4ee03SLukas Wunner struct work_struct *current_work(void) 582027d4ee03SLukas Wunner { 582127d4ee03SLukas Wunner struct worker *worker = current_wq_worker(); 582227d4ee03SLukas Wunner 582327d4ee03SLukas Wunner return worker ? worker->current_work : NULL; 582427d4ee03SLukas Wunner } 582527d4ee03SLukas Wunner EXPORT_SYMBOL(current_work); 582627d4ee03SLukas Wunner 582727d4ee03SLukas Wunner /** 5828e6267616STejun Heo * current_is_workqueue_rescuer - is %current workqueue rescuer? 5829e6267616STejun Heo * 5830e6267616STejun Heo * Determine whether %current is a workqueue rescuer. Can be used from 5831e6267616STejun Heo * work functions to determine whether it's being run off the rescuer task. 5832d185af30SYacine Belkadi * 5833d185af30SYacine Belkadi * Return: %true if %current is a workqueue rescuer. %false otherwise. 5834e6267616STejun Heo */ 5835e6267616STejun Heo bool current_is_workqueue_rescuer(void) 5836e6267616STejun Heo { 5837e6267616STejun Heo struct worker *worker = current_wq_worker(); 5838e6267616STejun Heo 58396a092dfdSLai Jiangshan return worker && worker->rescue_wq; 5840e6267616STejun Heo } 5841e6267616STejun Heo 5842e6267616STejun Heo /** 5843dcd989cbSTejun Heo * workqueue_congested - test whether a workqueue is congested 5844dcd989cbSTejun Heo * @cpu: CPU in question 5845dcd989cbSTejun Heo * @wq: target workqueue 5846dcd989cbSTejun Heo * 5847dcd989cbSTejun Heo * Test whether @wq's cpu workqueue for @cpu is congested. There is 5848dcd989cbSTejun Heo * no synchronization around this function and the test result is 5849dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5850dcd989cbSTejun Heo * 5851d3251859STejun Heo * If @cpu is WORK_CPU_UNBOUND, the test is performed on the local CPU. 5852636b927eSTejun Heo * 5853636b927eSTejun Heo * With the exception of ordered workqueues, all workqueues have per-cpu 5854636b927eSTejun Heo * pool_workqueues, each with its own congested state. A workqueue being 5855636b927eSTejun Heo * congested on one CPU doesn't mean that the workqueue is contested on any 5856636b927eSTejun Heo * other CPUs. 5857d3251859STejun Heo * 5858d185af30SYacine Belkadi * Return: 5859dcd989cbSTejun Heo * %true if congested, %false otherwise. 5860dcd989cbSTejun Heo */ 5861d84ff051STejun Heo bool workqueue_congested(int cpu, struct workqueue_struct *wq) 5862dcd989cbSTejun Heo { 58637fb98ea7STejun Heo struct pool_workqueue *pwq; 586476af4d93STejun Heo bool ret; 586576af4d93STejun Heo 586624acfb71SThomas Gleixner rcu_read_lock(); 586724acfb71SThomas Gleixner preempt_disable(); 58687fb98ea7STejun Heo 5869d3251859STejun Heo if (cpu == WORK_CPU_UNBOUND) 5870d3251859STejun Heo cpu = smp_processor_id(); 5871d3251859STejun Heo 5872687a9aa5STejun Heo pwq = *per_cpu_ptr(wq->cpu_pwq, cpu); 5873f97a4a1aSLai Jiangshan ret = !list_empty(&pwq->inactive_works); 5874636b927eSTejun Heo 587524acfb71SThomas Gleixner preempt_enable(); 587624acfb71SThomas Gleixner rcu_read_unlock(); 587776af4d93STejun Heo 587876af4d93STejun Heo return ret; 5879dcd989cbSTejun Heo } 5880dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(workqueue_congested); 5881dcd989cbSTejun Heo 5882dcd989cbSTejun Heo /** 5883dcd989cbSTejun Heo * work_busy - test whether a work is currently pending or running 5884dcd989cbSTejun Heo * @work: the work to be tested 5885dcd989cbSTejun Heo * 5886dcd989cbSTejun Heo * Test whether @work is currently pending or running. There is no 5887dcd989cbSTejun Heo * synchronization around this function and the test result is 5888dcd989cbSTejun Heo * unreliable and only useful as advisory hints or for debugging. 5889dcd989cbSTejun Heo * 5890d185af30SYacine Belkadi * Return: 5891dcd989cbSTejun Heo * OR'd bitmask of WORK_BUSY_* bits. 5892dcd989cbSTejun Heo */ 5893dcd989cbSTejun Heo unsigned int work_busy(struct work_struct *work) 5894dcd989cbSTejun Heo { 5895fa1b54e6STejun Heo struct worker_pool *pool; 5896c26e2f2eSTejun Heo unsigned long irq_flags; 5897dcd989cbSTejun Heo unsigned int ret = 0; 5898dcd989cbSTejun Heo 5899dcd989cbSTejun Heo if (work_pending(work)) 5900dcd989cbSTejun Heo ret |= WORK_BUSY_PENDING; 5901038366c5SLai Jiangshan 590224acfb71SThomas Gleixner rcu_read_lock(); 5903fa1b54e6STejun Heo pool = get_work_pool(work); 5904038366c5SLai Jiangshan if (pool) { 5905c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 5906c9e7cf27STejun Heo if (find_worker_executing_work(pool, work)) 5907dcd989cbSTejun Heo ret |= WORK_BUSY_RUNNING; 5908c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 5909038366c5SLai Jiangshan } 591024acfb71SThomas Gleixner rcu_read_unlock(); 5911dcd989cbSTejun Heo 5912dcd989cbSTejun Heo return ret; 5913dcd989cbSTejun Heo } 5914dcd989cbSTejun Heo EXPORT_SYMBOL_GPL(work_busy); 5915dcd989cbSTejun Heo 59163d1cb205STejun Heo /** 59173d1cb205STejun Heo * set_worker_desc - set description for the current work item 59183d1cb205STejun Heo * @fmt: printf-style format string 59193d1cb205STejun Heo * @...: arguments for the format string 59203d1cb205STejun Heo * 59213d1cb205STejun Heo * This function can be called by a running work function to describe what 59223d1cb205STejun Heo * the work item is about. If the worker task gets dumped, this 59233d1cb205STejun Heo * information will be printed out together to help debugging. The 59243d1cb205STejun Heo * description can be at most WORKER_DESC_LEN including the trailing '\0'. 59253d1cb205STejun Heo */ 59263d1cb205STejun Heo void set_worker_desc(const char *fmt, ...) 59273d1cb205STejun Heo { 59283d1cb205STejun Heo struct worker *worker = current_wq_worker(); 59293d1cb205STejun Heo va_list args; 59303d1cb205STejun Heo 59313d1cb205STejun Heo if (worker) { 59323d1cb205STejun Heo va_start(args, fmt); 59333d1cb205STejun Heo vsnprintf(worker->desc, sizeof(worker->desc), fmt, args); 59343d1cb205STejun Heo va_end(args); 59353d1cb205STejun Heo } 59363d1cb205STejun Heo } 59375c750d58SSteffen Maier EXPORT_SYMBOL_GPL(set_worker_desc); 59383d1cb205STejun Heo 59393d1cb205STejun Heo /** 59403d1cb205STejun Heo * print_worker_info - print out worker information and description 59413d1cb205STejun Heo * @log_lvl: the log level to use when printing 59423d1cb205STejun Heo * @task: target task 59433d1cb205STejun Heo * 59443d1cb205STejun Heo * If @task is a worker and currently executing a work item, print out the 59453d1cb205STejun Heo * name of the workqueue being serviced and worker description set with 59463d1cb205STejun Heo * set_worker_desc() by the currently executing work item. 59473d1cb205STejun Heo * 59483d1cb205STejun Heo * This function can be safely called on any task as long as the 59493d1cb205STejun Heo * task_struct itself is accessible. While safe, this function isn't 59503d1cb205STejun Heo * synchronized and may print out mixups or garbages of limited length. 59513d1cb205STejun Heo */ 59523d1cb205STejun Heo void print_worker_info(const char *log_lvl, struct task_struct *task) 59533d1cb205STejun Heo { 59543d1cb205STejun Heo work_func_t *fn = NULL; 59553d1cb205STejun Heo char name[WQ_NAME_LEN] = { }; 59563d1cb205STejun Heo char desc[WORKER_DESC_LEN] = { }; 59573d1cb205STejun Heo struct pool_workqueue *pwq = NULL; 59583d1cb205STejun Heo struct workqueue_struct *wq = NULL; 59593d1cb205STejun Heo struct worker *worker; 59603d1cb205STejun Heo 59613d1cb205STejun Heo if (!(task->flags & PF_WQ_WORKER)) 59623d1cb205STejun Heo return; 59633d1cb205STejun Heo 59643d1cb205STejun Heo /* 59653d1cb205STejun Heo * This function is called without any synchronization and @task 59663d1cb205STejun Heo * could be in any state. Be careful with dereferences. 59673d1cb205STejun Heo */ 5968e700591aSPetr Mladek worker = kthread_probe_data(task); 59693d1cb205STejun Heo 59703d1cb205STejun Heo /* 59718bf89593STejun Heo * Carefully copy the associated workqueue's workfn, name and desc. 59728bf89593STejun Heo * Keep the original last '\0' in case the original is garbage. 59733d1cb205STejun Heo */ 5974fe557319SChristoph Hellwig copy_from_kernel_nofault(&fn, &worker->current_func, sizeof(fn)); 5975fe557319SChristoph Hellwig copy_from_kernel_nofault(&pwq, &worker->current_pwq, sizeof(pwq)); 5976fe557319SChristoph Hellwig copy_from_kernel_nofault(&wq, &pwq->wq, sizeof(wq)); 5977fe557319SChristoph Hellwig copy_from_kernel_nofault(name, wq->name, sizeof(name) - 1); 5978fe557319SChristoph Hellwig copy_from_kernel_nofault(desc, worker->desc, sizeof(desc) - 1); 59793d1cb205STejun Heo 59803d1cb205STejun Heo if (fn || name[0] || desc[0]) { 5981d75f773cSSakari Ailus printk("%sWorkqueue: %s %ps", log_lvl, name, fn); 59828bf89593STejun Heo if (strcmp(name, desc)) 59833d1cb205STejun Heo pr_cont(" (%s)", desc); 59843d1cb205STejun Heo pr_cont("\n"); 59853d1cb205STejun Heo } 59863d1cb205STejun Heo } 59873d1cb205STejun Heo 59883494fc30STejun Heo static void pr_cont_pool_info(struct worker_pool *pool) 59893494fc30STejun Heo { 59903494fc30STejun Heo pr_cont(" cpus=%*pbl", nr_cpumask_bits, pool->attrs->cpumask); 59913494fc30STejun Heo if (pool->node != NUMA_NO_NODE) 59923494fc30STejun Heo pr_cont(" node=%d", pool->node); 59934cb1ef64STejun Heo pr_cont(" flags=0x%x", pool->flags); 59944cb1ef64STejun Heo if (pool->flags & POOL_BH) 59954cb1ef64STejun Heo pr_cont(" bh%s", 59964cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 59974cb1ef64STejun Heo else 59984cb1ef64STejun Heo pr_cont(" nice=%d", pool->attrs->nice); 59994cb1ef64STejun Heo } 60004cb1ef64STejun Heo 60014cb1ef64STejun Heo static void pr_cont_worker_id(struct worker *worker) 60024cb1ef64STejun Heo { 60034cb1ef64STejun Heo struct worker_pool *pool = worker->pool; 60044cb1ef64STejun Heo 60054cb1ef64STejun Heo if (pool->flags & WQ_BH) 60064cb1ef64STejun Heo pr_cont("bh%s", 60074cb1ef64STejun Heo pool->attrs->nice == HIGHPRI_NICE_LEVEL ? "-hi" : ""); 60084cb1ef64STejun Heo else 60094cb1ef64STejun Heo pr_cont("%d%s", task_pid_nr(worker->task), 60104cb1ef64STejun Heo worker->rescue_wq ? "(RESCUER)" : ""); 60113494fc30STejun Heo } 60123494fc30STejun Heo 6013c76feb0dSPaul E. McKenney struct pr_cont_work_struct { 6014c76feb0dSPaul E. McKenney bool comma; 6015c76feb0dSPaul E. McKenney work_func_t func; 6016c76feb0dSPaul E. McKenney long ctr; 6017c76feb0dSPaul E. McKenney }; 6018c76feb0dSPaul E. McKenney 6019c76feb0dSPaul E. McKenney static void pr_cont_work_flush(bool comma, work_func_t func, struct pr_cont_work_struct *pcwsp) 6020c76feb0dSPaul E. McKenney { 6021c76feb0dSPaul E. McKenney if (!pcwsp->ctr) 6022c76feb0dSPaul E. McKenney goto out_record; 6023c76feb0dSPaul E. McKenney if (func == pcwsp->func) { 6024c76feb0dSPaul E. McKenney pcwsp->ctr++; 6025c76feb0dSPaul E. McKenney return; 6026c76feb0dSPaul E. McKenney } 6027c76feb0dSPaul E. McKenney if (pcwsp->ctr == 1) 6028c76feb0dSPaul E. McKenney pr_cont("%s %ps", pcwsp->comma ? "," : "", pcwsp->func); 6029c76feb0dSPaul E. McKenney else 6030c76feb0dSPaul E. McKenney pr_cont("%s %ld*%ps", pcwsp->comma ? "," : "", pcwsp->ctr, pcwsp->func); 6031c76feb0dSPaul E. McKenney pcwsp->ctr = 0; 6032c76feb0dSPaul E. McKenney out_record: 6033c76feb0dSPaul E. McKenney if ((long)func == -1L) 6034c76feb0dSPaul E. McKenney return; 6035c76feb0dSPaul E. McKenney pcwsp->comma = comma; 6036c76feb0dSPaul E. McKenney pcwsp->func = func; 6037c76feb0dSPaul E. McKenney pcwsp->ctr = 1; 6038c76feb0dSPaul E. McKenney } 6039c76feb0dSPaul E. McKenney 6040c76feb0dSPaul E. McKenney static void pr_cont_work(bool comma, struct work_struct *work, struct pr_cont_work_struct *pcwsp) 60413494fc30STejun Heo { 60423494fc30STejun Heo if (work->func == wq_barrier_func) { 60433494fc30STejun Heo struct wq_barrier *barr; 60443494fc30STejun Heo 60453494fc30STejun Heo barr = container_of(work, struct wq_barrier, work); 60463494fc30STejun Heo 6047c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 60483494fc30STejun Heo pr_cont("%s BAR(%d)", comma ? "," : "", 60493494fc30STejun Heo task_pid_nr(barr->task)); 60503494fc30STejun Heo } else { 6051c76feb0dSPaul E. McKenney if (!comma) 6052c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1, pcwsp); 6053c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, work->func, pcwsp); 60543494fc30STejun Heo } 60553494fc30STejun Heo } 60563494fc30STejun Heo 60573494fc30STejun Heo static void show_pwq(struct pool_workqueue *pwq) 60583494fc30STejun Heo { 6059c76feb0dSPaul E. McKenney struct pr_cont_work_struct pcws = { .ctr = 0, }; 60603494fc30STejun Heo struct worker_pool *pool = pwq->pool; 60613494fc30STejun Heo struct work_struct *work; 60623494fc30STejun Heo struct worker *worker; 60633494fc30STejun Heo bool has_in_flight = false, has_pending = false; 60643494fc30STejun Heo int bkt; 60653494fc30STejun Heo 60663494fc30STejun Heo pr_info(" pwq %d:", pool->id); 60673494fc30STejun Heo pr_cont_pool_info(pool); 60683494fc30STejun Heo 6069a045a272STejun Heo pr_cont(" active=%d refcnt=%d%s\n", 6070a045a272STejun Heo pwq->nr_active, pwq->refcnt, 60713494fc30STejun Heo !list_empty(&pwq->mayday_node) ? " MAYDAY" : ""); 60723494fc30STejun Heo 60733494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 60743494fc30STejun Heo if (worker->current_pwq == pwq) { 60753494fc30STejun Heo has_in_flight = true; 60763494fc30STejun Heo break; 60773494fc30STejun Heo } 60783494fc30STejun Heo } 60793494fc30STejun Heo if (has_in_flight) { 60803494fc30STejun Heo bool comma = false; 60813494fc30STejun Heo 60823494fc30STejun Heo pr_info(" in-flight:"); 60833494fc30STejun Heo hash_for_each(pool->busy_hash, bkt, worker, hentry) { 60843494fc30STejun Heo if (worker->current_pwq != pwq) 60853494fc30STejun Heo continue; 60863494fc30STejun Heo 60874cb1ef64STejun Heo pr_cont(" %s", comma ? "," : ""); 60884cb1ef64STejun Heo pr_cont_worker_id(worker); 60894cb1ef64STejun Heo pr_cont(":%ps", worker->current_func); 60903494fc30STejun Heo list_for_each_entry(work, &worker->scheduled, entry) 6091c76feb0dSPaul E. McKenney pr_cont_work(false, work, &pcws); 6092c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 60933494fc30STejun Heo comma = true; 60943494fc30STejun Heo } 60953494fc30STejun Heo pr_cont("\n"); 60963494fc30STejun Heo } 60973494fc30STejun Heo 60983494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 60993494fc30STejun Heo if (get_work_pwq(work) == pwq) { 61003494fc30STejun Heo has_pending = true; 61013494fc30STejun Heo break; 61023494fc30STejun Heo } 61033494fc30STejun Heo } 61043494fc30STejun Heo if (has_pending) { 61053494fc30STejun Heo bool comma = false; 61063494fc30STejun Heo 61073494fc30STejun Heo pr_info(" pending:"); 61083494fc30STejun Heo list_for_each_entry(work, &pool->worklist, entry) { 61093494fc30STejun Heo if (get_work_pwq(work) != pwq) 61103494fc30STejun Heo continue; 61113494fc30STejun Heo 6112c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 61133494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 61143494fc30STejun Heo } 6115c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61163494fc30STejun Heo pr_cont("\n"); 61173494fc30STejun Heo } 61183494fc30STejun Heo 6119f97a4a1aSLai Jiangshan if (!list_empty(&pwq->inactive_works)) { 61203494fc30STejun Heo bool comma = false; 61213494fc30STejun Heo 6122f97a4a1aSLai Jiangshan pr_info(" inactive:"); 6123f97a4a1aSLai Jiangshan list_for_each_entry(work, &pwq->inactive_works, entry) { 6124c76feb0dSPaul E. McKenney pr_cont_work(comma, work, &pcws); 61253494fc30STejun Heo comma = !(*work_data_bits(work) & WORK_STRUCT_LINKED); 61263494fc30STejun Heo } 6127c76feb0dSPaul E. McKenney pr_cont_work_flush(comma, (work_func_t)-1L, &pcws); 61283494fc30STejun Heo pr_cont("\n"); 61293494fc30STejun Heo } 61303494fc30STejun Heo } 61313494fc30STejun Heo 61323494fc30STejun Heo /** 613355df0933SImran Khan * show_one_workqueue - dump state of specified workqueue 613455df0933SImran Khan * @wq: workqueue whose state will be printed 61353494fc30STejun Heo */ 613655df0933SImran Khan void show_one_workqueue(struct workqueue_struct *wq) 61373494fc30STejun Heo { 61383494fc30STejun Heo struct pool_workqueue *pwq; 61393494fc30STejun Heo bool idle = true; 6140c26e2f2eSTejun Heo unsigned long irq_flags; 61413494fc30STejun Heo 61423494fc30STejun Heo for_each_pwq(pwq, wq) { 6143afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 61443494fc30STejun Heo idle = false; 61453494fc30STejun Heo break; 61463494fc30STejun Heo } 61473494fc30STejun Heo } 614855df0933SImran Khan if (idle) /* Nothing to print for idle workqueue */ 614955df0933SImran Khan return; 61503494fc30STejun Heo 61513494fc30STejun Heo pr_info("workqueue %s: flags=0x%x\n", wq->name, wq->flags); 61523494fc30STejun Heo 61533494fc30STejun Heo for_each_pwq(pwq, wq) { 6154c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pwq->pool->lock, irq_flags); 6155afa87ce8STejun Heo if (!pwq_is_empty(pwq)) { 615657116ce1SJohan Hovold /* 615757116ce1SJohan Hovold * Defer printing to avoid deadlocks in console 615857116ce1SJohan Hovold * drivers that queue work while holding locks 615957116ce1SJohan Hovold * also taken in their write paths. 616057116ce1SJohan Hovold */ 616157116ce1SJohan Hovold printk_deferred_enter(); 61623494fc30STejun Heo show_pwq(pwq); 616357116ce1SJohan Hovold printk_deferred_exit(); 616457116ce1SJohan Hovold } 6165c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pwq->pool->lock, irq_flags); 616662635ea8SSergey Senozhatsky /* 616762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 616855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 616962635ea8SSergey Senozhatsky * hard lockup. 617062635ea8SSergey Senozhatsky */ 617162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 61723494fc30STejun Heo } 617355df0933SImran Khan 61743494fc30STejun Heo } 61753494fc30STejun Heo 617655df0933SImran Khan /** 617755df0933SImran Khan * show_one_worker_pool - dump state of specified worker pool 617855df0933SImran Khan * @pool: worker pool whose state will be printed 617955df0933SImran Khan */ 618055df0933SImran Khan static void show_one_worker_pool(struct worker_pool *pool) 618155df0933SImran Khan { 61823494fc30STejun Heo struct worker *worker; 61833494fc30STejun Heo bool first = true; 6184c26e2f2eSTejun Heo unsigned long irq_flags; 6185335a42ebSPetr Mladek unsigned long hung = 0; 61863494fc30STejun Heo 6187c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 61883494fc30STejun Heo if (pool->nr_workers == pool->nr_idle) 61893494fc30STejun Heo goto next_pool; 6190335a42ebSPetr Mladek 6191335a42ebSPetr Mladek /* How long the first pending work is waiting for a worker. */ 6192335a42ebSPetr Mladek if (!list_empty(&pool->worklist)) 6193335a42ebSPetr Mladek hung = jiffies_to_msecs(jiffies - pool->watchdog_ts) / 1000; 6194335a42ebSPetr Mladek 619557116ce1SJohan Hovold /* 619657116ce1SJohan Hovold * Defer printing to avoid deadlocks in console drivers that 619757116ce1SJohan Hovold * queue work while holding locks also taken in their write 619857116ce1SJohan Hovold * paths. 619957116ce1SJohan Hovold */ 620057116ce1SJohan Hovold printk_deferred_enter(); 62013494fc30STejun Heo pr_info("pool %d:", pool->id); 62023494fc30STejun Heo pr_cont_pool_info(pool); 6203335a42ebSPetr Mladek pr_cont(" hung=%lus workers=%d", hung, pool->nr_workers); 62043494fc30STejun Heo if (pool->manager) 62053494fc30STejun Heo pr_cont(" manager: %d", 62063494fc30STejun Heo task_pid_nr(pool->manager->task)); 62073494fc30STejun Heo list_for_each_entry(worker, &pool->idle_list, entry) { 62084cb1ef64STejun Heo pr_cont(" %s", first ? "idle: " : ""); 62094cb1ef64STejun Heo pr_cont_worker_id(worker); 62103494fc30STejun Heo first = false; 62113494fc30STejun Heo } 62123494fc30STejun Heo pr_cont("\n"); 621357116ce1SJohan Hovold printk_deferred_exit(); 62143494fc30STejun Heo next_pool: 6215c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 621662635ea8SSergey Senozhatsky /* 621762635ea8SSergey Senozhatsky * We could be printing a lot from atomic context, e.g. 621855df0933SImran Khan * sysrq-t -> show_all_workqueues(). Avoid triggering 621962635ea8SSergey Senozhatsky * hard lockup. 622062635ea8SSergey Senozhatsky */ 622162635ea8SSergey Senozhatsky touch_nmi_watchdog(); 622255df0933SImran Khan 62233494fc30STejun Heo } 62243494fc30STejun Heo 622555df0933SImran Khan /** 622655df0933SImran Khan * show_all_workqueues - dump workqueue state 622755df0933SImran Khan * 6228704bc669SJungseung Lee * Called from a sysrq handler and prints out all busy workqueues and pools. 622955df0933SImran Khan */ 623055df0933SImran Khan void show_all_workqueues(void) 623155df0933SImran Khan { 623255df0933SImran Khan struct workqueue_struct *wq; 623355df0933SImran Khan struct worker_pool *pool; 623455df0933SImran Khan int pi; 623555df0933SImran Khan 623655df0933SImran Khan rcu_read_lock(); 623755df0933SImran Khan 623855df0933SImran Khan pr_info("Showing busy workqueues and worker pools:\n"); 623955df0933SImran Khan 624055df0933SImran Khan list_for_each_entry_rcu(wq, &workqueues, list) 624155df0933SImran Khan show_one_workqueue(wq); 624255df0933SImran Khan 624355df0933SImran Khan for_each_pool(pool, pi) 624455df0933SImran Khan show_one_worker_pool(pool); 624555df0933SImran Khan 624624acfb71SThomas Gleixner rcu_read_unlock(); 62473494fc30STejun Heo } 62483494fc30STejun Heo 6249704bc669SJungseung Lee /** 6250704bc669SJungseung Lee * show_freezable_workqueues - dump freezable workqueue state 6251704bc669SJungseung Lee * 6252704bc669SJungseung Lee * Called from try_to_freeze_tasks() and prints out all freezable workqueues 6253704bc669SJungseung Lee * still busy. 6254704bc669SJungseung Lee */ 6255704bc669SJungseung Lee void show_freezable_workqueues(void) 6256704bc669SJungseung Lee { 6257704bc669SJungseung Lee struct workqueue_struct *wq; 6258704bc669SJungseung Lee 6259704bc669SJungseung Lee rcu_read_lock(); 6260704bc669SJungseung Lee 6261704bc669SJungseung Lee pr_info("Showing freezable workqueues that are still busy:\n"); 6262704bc669SJungseung Lee 6263704bc669SJungseung Lee list_for_each_entry_rcu(wq, &workqueues, list) { 6264704bc669SJungseung Lee if (!(wq->flags & WQ_FREEZABLE)) 6265704bc669SJungseung Lee continue; 6266704bc669SJungseung Lee show_one_workqueue(wq); 6267704bc669SJungseung Lee } 6268704bc669SJungseung Lee 6269704bc669SJungseung Lee rcu_read_unlock(); 6270704bc669SJungseung Lee } 6271704bc669SJungseung Lee 62726b59808bSTejun Heo /* used to show worker information through /proc/PID/{comm,stat,status} */ 62736b59808bSTejun Heo void wq_worker_comm(char *buf, size_t size, struct task_struct *task) 62746b59808bSTejun Heo { 62756b59808bSTejun Heo int off; 62766b59808bSTejun Heo 62776b59808bSTejun Heo /* always show the actual comm */ 62786b59808bSTejun Heo off = strscpy(buf, task->comm, size); 62796b59808bSTejun Heo if (off < 0) 62806b59808bSTejun Heo return; 62816b59808bSTejun Heo 6282197f6accSTejun Heo /* stabilize PF_WQ_WORKER and worker pool association */ 62836b59808bSTejun Heo mutex_lock(&wq_pool_attach_mutex); 62846b59808bSTejun Heo 6285197f6accSTejun Heo if (task->flags & PF_WQ_WORKER) { 6286197f6accSTejun Heo struct worker *worker = kthread_data(task); 6287197f6accSTejun Heo struct worker_pool *pool = worker->pool; 62886b59808bSTejun Heo 62896b59808bSTejun Heo if (pool) { 6290a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 62916b59808bSTejun Heo /* 6292197f6accSTejun Heo * ->desc tracks information (wq name or 6293197f6accSTejun Heo * set_worker_desc()) for the latest execution. If 6294197f6accSTejun Heo * current, prepend '+', otherwise '-'. 62956b59808bSTejun Heo */ 62966b59808bSTejun Heo if (worker->desc[0] != '\0') { 62976b59808bSTejun Heo if (worker->current_work) 62986b59808bSTejun Heo scnprintf(buf + off, size - off, "+%s", 62996b59808bSTejun Heo worker->desc); 63006b59808bSTejun Heo else 63016b59808bSTejun Heo scnprintf(buf + off, size - off, "-%s", 63026b59808bSTejun Heo worker->desc); 63036b59808bSTejun Heo } 6304a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 63056b59808bSTejun Heo } 6306197f6accSTejun Heo } 63076b59808bSTejun Heo 63086b59808bSTejun Heo mutex_unlock(&wq_pool_attach_mutex); 63096b59808bSTejun Heo } 63106b59808bSTejun Heo 631166448bc2SMathieu Malaterre #ifdef CONFIG_SMP 631266448bc2SMathieu Malaterre 6313db7bccf4STejun Heo /* 6314db7bccf4STejun Heo * CPU hotplug. 6315db7bccf4STejun Heo * 6316e22bee78STejun Heo * There are two challenges in supporting CPU hotplug. Firstly, there 6317112202d9STejun Heo * are a lot of assumptions on strong associations among work, pwq and 6318706026c2STejun Heo * pool which make migrating pending and scheduled works very 6319e22bee78STejun Heo * difficult to implement without impacting hot paths. Secondly, 632094cf58bbSTejun Heo * worker pools serve mix of short, long and very long running works making 6321e22bee78STejun Heo * blocked draining impractical. 6322e22bee78STejun Heo * 632324647570STejun Heo * This is solved by allowing the pools to be disassociated from the CPU 6324628c78e7STejun Heo * running as an unbound one and allowing it to be reattached later if the 6325628c78e7STejun Heo * cpu comes back online. 6326db7bccf4STejun Heo */ 6327db7bccf4STejun Heo 6328e8b3f8dbSLai Jiangshan static void unbind_workers(int cpu) 6329db7bccf4STejun Heo { 63304ce62e9eSTejun Heo struct worker_pool *pool; 6331db7bccf4STejun Heo struct worker *worker; 6332db7bccf4STejun Heo 6333f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 63341258fae7STejun Heo mutex_lock(&wq_pool_attach_mutex); 6335a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6336e22bee78STejun Heo 6337f2d5a0eeSTejun Heo /* 633892f9c5c4SLai Jiangshan * We've blocked all attach/detach operations. Make all workers 633994cf58bbSTejun Heo * unbound and set DISASSOCIATED. Before this, all workers 634011b45b0bSLai Jiangshan * must be on the cpu. After this, they may become diasporas. 6341b4ac9384SLai Jiangshan * And the preemption disabled section in their sched callbacks 6342b4ac9384SLai Jiangshan * are guaranteed to see WORKER_UNBOUND since the code here 6343b4ac9384SLai Jiangshan * is on the same cpu. 6344f2d5a0eeSTejun Heo */ 6345da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6346403c821dSTejun Heo worker->flags |= WORKER_UNBOUND; 6347db7bccf4STejun Heo 634824647570STejun Heo pool->flags |= POOL_DISASSOCIATED; 6349f2d5a0eeSTejun Heo 6350e22bee78STejun Heo /* 6351989442d7SLai Jiangshan * The handling of nr_running in sched callbacks are disabled 6352989442d7SLai Jiangshan * now. Zap nr_running. After this, nr_running stays zero and 6353989442d7SLai Jiangshan * need_more_worker() and keep_working() are always true as 6354989442d7SLai Jiangshan * long as the worklist is not empty. This pool now behaves as 6355989442d7SLai Jiangshan * an unbound (in terms of concurrency management) pool which 6356eb283428SLai Jiangshan * are served by workers tied to the pool. 6357e22bee78STejun Heo */ 6358bc35f7efSLai Jiangshan pool->nr_running = 0; 6359eb283428SLai Jiangshan 6360eb283428SLai Jiangshan /* 6361eb283428SLai Jiangshan * With concurrency management just turned off, a busy 6362eb283428SLai Jiangshan * worker blocking could lead to lengthy stalls. Kick off 6363eb283428SLai Jiangshan * unbound chain execution of currently pending work items. 6364eb283428SLai Jiangshan */ 63650219a352STejun Heo kick_pool(pool); 6366989442d7SLai Jiangshan 6367a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6368989442d7SLai Jiangshan 6369793777bcSValentin Schneider for_each_pool_worker(worker, pool) 6370793777bcSValentin Schneider unbind_worker(worker); 6371989442d7SLai Jiangshan 6372989442d7SLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 6373eb283428SLai Jiangshan } 6374db7bccf4STejun Heo } 6375db7bccf4STejun Heo 6376bd7c089eSTejun Heo /** 6377bd7c089eSTejun Heo * rebind_workers - rebind all workers of a pool to the associated CPU 6378bd7c089eSTejun Heo * @pool: pool of interest 6379bd7c089eSTejun Heo * 6380a9ab775bSTejun Heo * @pool->cpu is coming online. Rebind all workers to the CPU. 6381bd7c089eSTejun Heo */ 6382bd7c089eSTejun Heo static void rebind_workers(struct worker_pool *pool) 6383bd7c089eSTejun Heo { 6384a9ab775bSTejun Heo struct worker *worker; 6385bd7c089eSTejun Heo 63861258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 6387bd7c089eSTejun Heo 6388bd7c089eSTejun Heo /* 6389a9ab775bSTejun Heo * Restore CPU affinity of all workers. As all idle workers should 6390a9ab775bSTejun Heo * be on the run-queue of the associated CPU before any local 6391402dd89dSShailendra Verma * wake-ups for concurrency management happen, restore CPU affinity 6392a9ab775bSTejun Heo * of all workers first and then clear UNBOUND. As we're called 6393a9ab775bSTejun Heo * from CPU_ONLINE, the following shouldn't fail. 6394bd7c089eSTejun Heo */ 6395c63a2e52SValentin Schneider for_each_pool_worker(worker, pool) { 6396c63a2e52SValentin Schneider kthread_set_per_cpu(worker->task, pool->cpu); 6397c63a2e52SValentin Schneider WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, 63989546b29eSTejun Heo pool_allowed_cpus(pool)) < 0); 6399c63a2e52SValentin Schneider } 6400a9ab775bSTejun Heo 6401a9b8a985SSebastian Andrzej Siewior raw_spin_lock_irq(&pool->lock); 6402f7c17d26SWanpeng Li 64033de5e884SLai Jiangshan pool->flags &= ~POOL_DISASSOCIATED; 6404a9ab775bSTejun Heo 6405da028469SLai Jiangshan for_each_pool_worker(worker, pool) { 6406a9ab775bSTejun Heo unsigned int worker_flags = worker->flags; 6407a9ab775bSTejun Heo 6408a9ab775bSTejun Heo /* 6409a9ab775bSTejun Heo * We want to clear UNBOUND but can't directly call 6410a9ab775bSTejun Heo * worker_clr_flags() or adjust nr_running. Atomically 6411a9ab775bSTejun Heo * replace UNBOUND with another NOT_RUNNING flag REBOUND. 6412a9ab775bSTejun Heo * @worker will clear REBOUND using worker_clr_flags() when 6413a9ab775bSTejun Heo * it initiates the next execution cycle thus restoring 6414a9ab775bSTejun Heo * concurrency management. Note that when or whether 6415a9ab775bSTejun Heo * @worker clears REBOUND doesn't affect correctness. 6416a9ab775bSTejun Heo * 6417c95491edSMark Rutland * WRITE_ONCE() is necessary because @worker->flags may be 6418a9ab775bSTejun Heo * tested without holding any lock in 64196d25be57SThomas Gleixner * wq_worker_running(). Without it, NOT_RUNNING test may 6420a9ab775bSTejun Heo * fail incorrectly leading to premature concurrency 6421a9ab775bSTejun Heo * management operations. 6422bd7c089eSTejun Heo */ 6423a9ab775bSTejun Heo WARN_ON_ONCE(!(worker_flags & WORKER_UNBOUND)); 6424a9ab775bSTejun Heo worker_flags |= WORKER_REBOUND; 6425a9ab775bSTejun Heo worker_flags &= ~WORKER_UNBOUND; 6426c95491edSMark Rutland WRITE_ONCE(worker->flags, worker_flags); 6427bd7c089eSTejun Heo } 6428a9ab775bSTejun Heo 6429a9b8a985SSebastian Andrzej Siewior raw_spin_unlock_irq(&pool->lock); 6430bd7c089eSTejun Heo } 6431bd7c089eSTejun Heo 64327dbc725eSTejun Heo /** 64337dbc725eSTejun Heo * restore_unbound_workers_cpumask - restore cpumask of unbound workers 64347dbc725eSTejun Heo * @pool: unbound pool of interest 64357dbc725eSTejun Heo * @cpu: the CPU which is coming up 64367dbc725eSTejun Heo * 64377dbc725eSTejun Heo * An unbound pool may end up with a cpumask which doesn't have any online 64387dbc725eSTejun Heo * CPUs. When a worker of such pool get scheduled, the scheduler resets 64397dbc725eSTejun Heo * its cpus_allowed. If @cpu is in @pool's cpumask which didn't have any 64407dbc725eSTejun Heo * online CPU before, cpus_allowed of all its workers should be restored. 64417dbc725eSTejun Heo */ 64427dbc725eSTejun Heo static void restore_unbound_workers_cpumask(struct worker_pool *pool, int cpu) 64437dbc725eSTejun Heo { 64447dbc725eSTejun Heo static cpumask_t cpumask; 64457dbc725eSTejun Heo struct worker *worker; 64467dbc725eSTejun Heo 64471258fae7STejun Heo lockdep_assert_held(&wq_pool_attach_mutex); 64487dbc725eSTejun Heo 64497dbc725eSTejun Heo /* is @cpu allowed for @pool? */ 64507dbc725eSTejun Heo if (!cpumask_test_cpu(cpu, pool->attrs->cpumask)) 64517dbc725eSTejun Heo return; 64527dbc725eSTejun Heo 64537dbc725eSTejun Heo cpumask_and(&cpumask, pool->attrs->cpumask, cpu_online_mask); 64547dbc725eSTejun Heo 64557dbc725eSTejun Heo /* as we're called from CPU_ONLINE, the following shouldn't fail */ 6456da028469SLai Jiangshan for_each_pool_worker(worker, pool) 6457d945b5e9SPeter Zijlstra WARN_ON_ONCE(set_cpus_allowed_ptr(worker->task, &cpumask) < 0); 64587dbc725eSTejun Heo } 64597dbc725eSTejun Heo 64607ee681b2SThomas Gleixner int workqueue_prepare_cpu(unsigned int cpu) 64611da177e4SLinus Torvalds { 64624ce62e9eSTejun Heo struct worker_pool *pool; 64631da177e4SLinus Torvalds 6464f02ae73aSTejun Heo for_each_cpu_worker_pool(pool, cpu) { 64653ce63377STejun Heo if (pool->nr_workers) 64663ce63377STejun Heo continue; 6467051e1850SLai Jiangshan if (!create_worker(pool)) 64687ee681b2SThomas Gleixner return -ENOMEM; 64693af24433SOleg Nesterov } 64707ee681b2SThomas Gleixner return 0; 64717ee681b2SThomas Gleixner } 64721da177e4SLinus Torvalds 64737ee681b2SThomas Gleixner int workqueue_online_cpu(unsigned int cpu) 64747ee681b2SThomas Gleixner { 64757ee681b2SThomas Gleixner struct worker_pool *pool; 64767ee681b2SThomas Gleixner struct workqueue_struct *wq; 64777ee681b2SThomas Gleixner int pi; 64787ee681b2SThomas Gleixner 647968e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 64807dbc725eSTejun Heo 64817dbc725eSTejun Heo for_each_pool(pool, pi) { 64824cb1ef64STejun Heo /* BH pools aren't affected by hotplug */ 64834cb1ef64STejun Heo if (pool->flags & POOL_BH) 64844cb1ef64STejun Heo continue; 648594cf58bbSTejun Heo 64864cb1ef64STejun Heo mutex_lock(&wq_pool_attach_mutex); 6487f05b558dSLai Jiangshan if (pool->cpu == cpu) 648894cf58bbSTejun Heo rebind_workers(pool); 6489f05b558dSLai Jiangshan else if (pool->cpu < 0) 64907dbc725eSTejun Heo restore_unbound_workers_cpumask(pool, cpu); 64911258fae7STejun Heo mutex_unlock(&wq_pool_attach_mutex); 649294cf58bbSTejun Heo } 64937dbc725eSTejun Heo 6494fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 64954cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 649684193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 649784193c07STejun Heo 649884193c07STejun Heo if (attrs) { 649984193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 65004cbfd3deSTejun Heo int tcpu; 65014cbfd3deSTejun Heo 650284193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6503fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, true); 65045797b1c1STejun Heo 65055797b1c1STejun Heo mutex_lock(&wq->mutex); 65065797b1c1STejun Heo wq_update_node_max_active(wq, -1); 65075797b1c1STejun Heo mutex_unlock(&wq->mutex); 65084cbfd3deSTejun Heo } 65094cbfd3deSTejun Heo } 65104c16bd32STejun Heo 651168e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 65127ee681b2SThomas Gleixner return 0; 651365758202STejun Heo } 651465758202STejun Heo 65157ee681b2SThomas Gleixner int workqueue_offline_cpu(unsigned int cpu) 651665758202STejun Heo { 65174c16bd32STejun Heo struct workqueue_struct *wq; 65188db25e78STejun Heo 65194c16bd32STejun Heo /* unbinding per-cpu workers should happen on the local CPU */ 6520e8b3f8dbSLai Jiangshan if (WARN_ON(cpu != smp_processor_id())) 6521e8b3f8dbSLai Jiangshan return -1; 6522e8b3f8dbSLai Jiangshan 6523e8b3f8dbSLai Jiangshan unbind_workers(cpu); 65244c16bd32STejun Heo 6525fef59c9cSTejun Heo /* update pod affinity of unbound workqueues */ 65264c16bd32STejun Heo mutex_lock(&wq_pool_mutex); 65274cbfd3deSTejun Heo list_for_each_entry(wq, &workqueues, list) { 652884193c07STejun Heo struct workqueue_attrs *attrs = wq->unbound_attrs; 652984193c07STejun Heo 653084193c07STejun Heo if (attrs) { 653184193c07STejun Heo const struct wq_pod_type *pt = wqattrs_pod_type(attrs); 65324cbfd3deSTejun Heo int tcpu; 65334cbfd3deSTejun Heo 653484193c07STejun Heo for_each_cpu(tcpu, pt->pod_cpus[pt->cpu_pod[cpu]]) 6535fef59c9cSTejun Heo wq_update_pod(wq, tcpu, cpu, false); 65365797b1c1STejun Heo 65375797b1c1STejun Heo mutex_lock(&wq->mutex); 65385797b1c1STejun Heo wq_update_node_max_active(wq, cpu); 65395797b1c1STejun Heo mutex_unlock(&wq->mutex); 65404cbfd3deSTejun Heo } 65414cbfd3deSTejun Heo } 65424c16bd32STejun Heo mutex_unlock(&wq_pool_mutex); 65434c16bd32STejun Heo 65447ee681b2SThomas Gleixner return 0; 654565758202STejun Heo } 654665758202STejun Heo 65472d3854a3SRusty Russell struct work_for_cpu { 6548ed48ece2STejun Heo struct work_struct work; 65492d3854a3SRusty Russell long (*fn)(void *); 65502d3854a3SRusty Russell void *arg; 65512d3854a3SRusty Russell long ret; 65522d3854a3SRusty Russell }; 65532d3854a3SRusty Russell 6554ed48ece2STejun Heo static void work_for_cpu_fn(struct work_struct *work) 65552d3854a3SRusty Russell { 6556ed48ece2STejun Heo struct work_for_cpu *wfc = container_of(work, struct work_for_cpu, work); 6557ed48ece2STejun Heo 65582d3854a3SRusty Russell wfc->ret = wfc->fn(wfc->arg); 65592d3854a3SRusty Russell } 65602d3854a3SRusty Russell 65612d3854a3SRusty Russell /** 6562265f3ed0SFrederic Weisbecker * work_on_cpu_key - run a function in thread context on a particular cpu 65632d3854a3SRusty Russell * @cpu: the cpu to run on 65642d3854a3SRusty Russell * @fn: the function to run 65652d3854a3SRusty Russell * @arg: the function arg 6566265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 65672d3854a3SRusty Russell * 656831ad9081SRusty Russell * It is up to the caller to ensure that the cpu doesn't go offline. 65696b44003eSAndrew Morton * The caller must not hold any locks which would prevent @fn from completing. 6570d185af30SYacine Belkadi * 6571d185af30SYacine Belkadi * Return: The value @fn returns. 65722d3854a3SRusty Russell */ 6573265f3ed0SFrederic Weisbecker long work_on_cpu_key(int cpu, long (*fn)(void *), 6574265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 65752d3854a3SRusty Russell { 6576ed48ece2STejun Heo struct work_for_cpu wfc = { .fn = fn, .arg = arg }; 65772d3854a3SRusty Russell 6578265f3ed0SFrederic Weisbecker INIT_WORK_ONSTACK_KEY(&wfc.work, work_for_cpu_fn, key); 6579ed48ece2STejun Heo schedule_work_on(cpu, &wfc.work); 658012997d1aSBjorn Helgaas flush_work(&wfc.work); 6581440a1136SChuansheng Liu destroy_work_on_stack(&wfc.work); 65822d3854a3SRusty Russell return wfc.ret; 65832d3854a3SRusty Russell } 6584265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_key); 65850e8d6a93SThomas Gleixner 65860e8d6a93SThomas Gleixner /** 6587265f3ed0SFrederic Weisbecker * work_on_cpu_safe_key - run a function in thread context on a particular cpu 65880e8d6a93SThomas Gleixner * @cpu: the cpu to run on 65890e8d6a93SThomas Gleixner * @fn: the function to run 65900e8d6a93SThomas Gleixner * @arg: the function argument 6591265f3ed0SFrederic Weisbecker * @key: The lock class key for lock debugging purposes 65920e8d6a93SThomas Gleixner * 65930e8d6a93SThomas Gleixner * Disables CPU hotplug and calls work_on_cpu(). The caller must not hold 65940e8d6a93SThomas Gleixner * any locks which would prevent @fn from completing. 65950e8d6a93SThomas Gleixner * 65960e8d6a93SThomas Gleixner * Return: The value @fn returns. 65970e8d6a93SThomas Gleixner */ 6598265f3ed0SFrederic Weisbecker long work_on_cpu_safe_key(int cpu, long (*fn)(void *), 6599265f3ed0SFrederic Weisbecker void *arg, struct lock_class_key *key) 66000e8d6a93SThomas Gleixner { 66010e8d6a93SThomas Gleixner long ret = -ENODEV; 66020e8d6a93SThomas Gleixner 6603ffd8bea8SSebastian Andrzej Siewior cpus_read_lock(); 66040e8d6a93SThomas Gleixner if (cpu_online(cpu)) 6605265f3ed0SFrederic Weisbecker ret = work_on_cpu_key(cpu, fn, arg, key); 6606ffd8bea8SSebastian Andrzej Siewior cpus_read_unlock(); 66070e8d6a93SThomas Gleixner return ret; 66080e8d6a93SThomas Gleixner } 6609265f3ed0SFrederic Weisbecker EXPORT_SYMBOL_GPL(work_on_cpu_safe_key); 66102d3854a3SRusty Russell #endif /* CONFIG_SMP */ 66112d3854a3SRusty Russell 6612a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER 6613e7577c50SRusty Russell 6614a0a1a5fdSTejun Heo /** 6615a0a1a5fdSTejun Heo * freeze_workqueues_begin - begin freezing workqueues 6616a0a1a5fdSTejun Heo * 661758a69cb4STejun Heo * Start freezing workqueues. After this function returns, all freezable 6618f97a4a1aSLai Jiangshan * workqueues will queue new works to their inactive_works list instead of 6619706026c2STejun Heo * pool->worklist. 6620a0a1a5fdSTejun Heo * 6621a0a1a5fdSTejun Heo * CONTEXT: 6622a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6623a0a1a5fdSTejun Heo */ 6624a0a1a5fdSTejun Heo void freeze_workqueues_begin(void) 6625a0a1a5fdSTejun Heo { 662624b8a847STejun Heo struct workqueue_struct *wq; 6627a0a1a5fdSTejun Heo 662868e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6629a0a1a5fdSTejun Heo 66306183c009STejun Heo WARN_ON_ONCE(workqueue_freezing); 6631a0a1a5fdSTejun Heo workqueue_freezing = true; 6632a0a1a5fdSTejun Heo 663324b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6634a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6635a045a272STejun Heo wq_adjust_max_active(wq); 6636a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 6637a1056305STejun Heo } 66385bcab335STejun Heo 663968e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6640a0a1a5fdSTejun Heo } 6641a0a1a5fdSTejun Heo 6642a0a1a5fdSTejun Heo /** 664358a69cb4STejun Heo * freeze_workqueues_busy - are freezable workqueues still busy? 6644a0a1a5fdSTejun Heo * 6645a0a1a5fdSTejun Heo * Check whether freezing is complete. This function must be called 6646a0a1a5fdSTejun Heo * between freeze_workqueues_begin() and thaw_workqueues(). 6647a0a1a5fdSTejun Heo * 6648a0a1a5fdSTejun Heo * CONTEXT: 664968e13a67SLai Jiangshan * Grabs and releases wq_pool_mutex. 6650a0a1a5fdSTejun Heo * 6651d185af30SYacine Belkadi * Return: 665258a69cb4STejun Heo * %true if some freezable workqueues are still busy. %false if freezing 665358a69cb4STejun Heo * is complete. 6654a0a1a5fdSTejun Heo */ 6655a0a1a5fdSTejun Heo bool freeze_workqueues_busy(void) 6656a0a1a5fdSTejun Heo { 6657a0a1a5fdSTejun Heo bool busy = false; 665824b8a847STejun Heo struct workqueue_struct *wq; 665924b8a847STejun Heo struct pool_workqueue *pwq; 6660a0a1a5fdSTejun Heo 666168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6662a0a1a5fdSTejun Heo 66636183c009STejun Heo WARN_ON_ONCE(!workqueue_freezing); 6664a0a1a5fdSTejun Heo 666524b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 666624b8a847STejun Heo if (!(wq->flags & WQ_FREEZABLE)) 666724b8a847STejun Heo continue; 6668a0a1a5fdSTejun Heo /* 6669a0a1a5fdSTejun Heo * nr_active is monotonically decreasing. It's safe 6670a0a1a5fdSTejun Heo * to peek without lock. 6671a0a1a5fdSTejun Heo */ 667224acfb71SThomas Gleixner rcu_read_lock(); 667324b8a847STejun Heo for_each_pwq(pwq, wq) { 66746183c009STejun Heo WARN_ON_ONCE(pwq->nr_active < 0); 6675112202d9STejun Heo if (pwq->nr_active) { 6676a0a1a5fdSTejun Heo busy = true; 667724acfb71SThomas Gleixner rcu_read_unlock(); 6678a0a1a5fdSTejun Heo goto out_unlock; 6679a0a1a5fdSTejun Heo } 6680a0a1a5fdSTejun Heo } 668124acfb71SThomas Gleixner rcu_read_unlock(); 6682a0a1a5fdSTejun Heo } 6683a0a1a5fdSTejun Heo out_unlock: 668468e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6685a0a1a5fdSTejun Heo return busy; 6686a0a1a5fdSTejun Heo } 6687a0a1a5fdSTejun Heo 6688a0a1a5fdSTejun Heo /** 6689a0a1a5fdSTejun Heo * thaw_workqueues - thaw workqueues 6690a0a1a5fdSTejun Heo * 6691a0a1a5fdSTejun Heo * Thaw workqueues. Normal queueing is restored and all collected 6692706026c2STejun Heo * frozen works are transferred to their respective pool worklists. 6693a0a1a5fdSTejun Heo * 6694a0a1a5fdSTejun Heo * CONTEXT: 6695a357fc03SLai Jiangshan * Grabs and releases wq_pool_mutex, wq->mutex and pool->lock's. 6696a0a1a5fdSTejun Heo */ 6697a0a1a5fdSTejun Heo void thaw_workqueues(void) 6698a0a1a5fdSTejun Heo { 669924b8a847STejun Heo struct workqueue_struct *wq; 6700a0a1a5fdSTejun Heo 670168e13a67SLai Jiangshan mutex_lock(&wq_pool_mutex); 6702a0a1a5fdSTejun Heo 6703a0a1a5fdSTejun Heo if (!workqueue_freezing) 6704a0a1a5fdSTejun Heo goto out_unlock; 6705a0a1a5fdSTejun Heo 670674b414eaSLai Jiangshan workqueue_freezing = false; 670724b8a847STejun Heo 670824b8a847STejun Heo /* restore max_active and repopulate worklist */ 670924b8a847STejun Heo list_for_each_entry(wq, &workqueues, list) { 6710a357fc03SLai Jiangshan mutex_lock(&wq->mutex); 6711a045a272STejun Heo wq_adjust_max_active(wq); 6712a357fc03SLai Jiangshan mutex_unlock(&wq->mutex); 671324b8a847STejun Heo } 671424b8a847STejun Heo 6715a0a1a5fdSTejun Heo out_unlock: 671668e13a67SLai Jiangshan mutex_unlock(&wq_pool_mutex); 6717a0a1a5fdSTejun Heo } 6718a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */ 6719a0a1a5fdSTejun Heo 672099c621efSLai Jiangshan static int workqueue_apply_unbound_cpumask(const cpumask_var_t unbound_cpumask) 6721042f7df1SLai Jiangshan { 6722042f7df1SLai Jiangshan LIST_HEAD(ctxs); 6723042f7df1SLai Jiangshan int ret = 0; 6724042f7df1SLai Jiangshan struct workqueue_struct *wq; 6725042f7df1SLai Jiangshan struct apply_wqattrs_ctx *ctx, *n; 6726042f7df1SLai Jiangshan 6727042f7df1SLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6728042f7df1SLai Jiangshan 6729042f7df1SLai Jiangshan list_for_each_entry(wq, &workqueues, list) { 67308eb17dc1SWaiman Long if (!(wq->flags & WQ_UNBOUND) || (wq->flags & __WQ_DESTROYING)) 6731042f7df1SLai Jiangshan continue; 6732042f7df1SLai Jiangshan 673399c621efSLai Jiangshan ctx = apply_wqattrs_prepare(wq, wq->unbound_attrs, unbound_cpumask); 673484193c07STejun Heo if (IS_ERR(ctx)) { 673584193c07STejun Heo ret = PTR_ERR(ctx); 6736042f7df1SLai Jiangshan break; 6737042f7df1SLai Jiangshan } 6738042f7df1SLai Jiangshan 6739042f7df1SLai Jiangshan list_add_tail(&ctx->list, &ctxs); 6740042f7df1SLai Jiangshan } 6741042f7df1SLai Jiangshan 6742042f7df1SLai Jiangshan list_for_each_entry_safe(ctx, n, &ctxs, list) { 6743042f7df1SLai Jiangshan if (!ret) 6744042f7df1SLai Jiangshan apply_wqattrs_commit(ctx); 6745042f7df1SLai Jiangshan apply_wqattrs_cleanup(ctx); 6746042f7df1SLai Jiangshan } 6747042f7df1SLai Jiangshan 674899c621efSLai Jiangshan if (!ret) { 674999c621efSLai Jiangshan mutex_lock(&wq_pool_attach_mutex); 675099c621efSLai Jiangshan cpumask_copy(wq_unbound_cpumask, unbound_cpumask); 675199c621efSLai Jiangshan mutex_unlock(&wq_pool_attach_mutex); 675299c621efSLai Jiangshan } 6753042f7df1SLai Jiangshan return ret; 6754042f7df1SLai Jiangshan } 6755042f7df1SLai Jiangshan 6756042f7df1SLai Jiangshan /** 6757fe28f631SWaiman Long * workqueue_unbound_exclude_cpumask - Exclude given CPUs from unbound cpumask 6758fe28f631SWaiman Long * @exclude_cpumask: the cpumask to be excluded from wq_unbound_cpumask 6759fe28f631SWaiman Long * 6760fe28f631SWaiman Long * This function can be called from cpuset code to provide a set of isolated 6761fe28f631SWaiman Long * CPUs that should be excluded from wq_unbound_cpumask. The caller must hold 6762fe28f631SWaiman Long * either cpus_read_lock or cpus_write_lock. 6763fe28f631SWaiman Long */ 6764fe28f631SWaiman Long int workqueue_unbound_exclude_cpumask(cpumask_var_t exclude_cpumask) 6765fe28f631SWaiman Long { 6766fe28f631SWaiman Long cpumask_var_t cpumask; 6767fe28f631SWaiman Long int ret = 0; 6768fe28f631SWaiman Long 6769fe28f631SWaiman Long if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 6770fe28f631SWaiman Long return -ENOMEM; 6771fe28f631SWaiman Long 6772fe28f631SWaiman Long lockdep_assert_cpus_held(); 6773fe28f631SWaiman Long mutex_lock(&wq_pool_mutex); 6774fe28f631SWaiman Long 6775fe28f631SWaiman Long /* Save the current isolated cpumask & export it via sysfs */ 6776fe28f631SWaiman Long cpumask_copy(wq_isolated_cpumask, exclude_cpumask); 6777fe28f631SWaiman Long 6778fe28f631SWaiman Long /* 6779fe28f631SWaiman Long * If the operation fails, it will fall back to 6780fe28f631SWaiman Long * wq_requested_unbound_cpumask which is initially set to 6781fe28f631SWaiman Long * (HK_TYPE_WQ ∩ HK_TYPE_DOMAIN) house keeping mask and rewritten 6782fe28f631SWaiman Long * by any subsequent write to workqueue/cpumask sysfs file. 6783fe28f631SWaiman Long */ 6784fe28f631SWaiman Long if (!cpumask_andnot(cpumask, wq_requested_unbound_cpumask, exclude_cpumask)) 6785fe28f631SWaiman Long cpumask_copy(cpumask, wq_requested_unbound_cpumask); 6786fe28f631SWaiman Long if (!cpumask_equal(cpumask, wq_unbound_cpumask)) 6787fe28f631SWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 6788fe28f631SWaiman Long 6789fe28f631SWaiman Long mutex_unlock(&wq_pool_mutex); 6790fe28f631SWaiman Long free_cpumask_var(cpumask); 6791fe28f631SWaiman Long return ret; 6792fe28f631SWaiman Long } 6793fe28f631SWaiman Long 679463c5484eSTejun Heo static int parse_affn_scope(const char *val) 679563c5484eSTejun Heo { 679663c5484eSTejun Heo int i; 679763c5484eSTejun Heo 679863c5484eSTejun Heo for (i = 0; i < ARRAY_SIZE(wq_affn_names); i++) { 679963c5484eSTejun Heo if (!strncasecmp(val, wq_affn_names[i], strlen(wq_affn_names[i]))) 680063c5484eSTejun Heo return i; 680163c5484eSTejun Heo } 680263c5484eSTejun Heo return -EINVAL; 680363c5484eSTejun Heo } 680463c5484eSTejun Heo 680563c5484eSTejun Heo static int wq_affn_dfl_set(const char *val, const struct kernel_param *kp) 680663c5484eSTejun Heo { 6807523a301eSTejun Heo struct workqueue_struct *wq; 6808523a301eSTejun Heo int affn, cpu; 680963c5484eSTejun Heo 681063c5484eSTejun Heo affn = parse_affn_scope(val); 681163c5484eSTejun Heo if (affn < 0) 681263c5484eSTejun Heo return affn; 6813523a301eSTejun Heo if (affn == WQ_AFFN_DFL) 6814523a301eSTejun Heo return -EINVAL; 6815523a301eSTejun Heo 6816523a301eSTejun Heo cpus_read_lock(); 6817523a301eSTejun Heo mutex_lock(&wq_pool_mutex); 681863c5484eSTejun Heo 681963c5484eSTejun Heo wq_affn_dfl = affn; 6820523a301eSTejun Heo 6821523a301eSTejun Heo list_for_each_entry(wq, &workqueues, list) { 6822523a301eSTejun Heo for_each_online_cpu(cpu) { 6823523a301eSTejun Heo wq_update_pod(wq, cpu, cpu, true); 6824523a301eSTejun Heo } 6825523a301eSTejun Heo } 6826523a301eSTejun Heo 6827523a301eSTejun Heo mutex_unlock(&wq_pool_mutex); 6828523a301eSTejun Heo cpus_read_unlock(); 6829523a301eSTejun Heo 683063c5484eSTejun Heo return 0; 683163c5484eSTejun Heo } 683263c5484eSTejun Heo 683363c5484eSTejun Heo static int wq_affn_dfl_get(char *buffer, const struct kernel_param *kp) 683463c5484eSTejun Heo { 683563c5484eSTejun Heo return scnprintf(buffer, PAGE_SIZE, "%s\n", wq_affn_names[wq_affn_dfl]); 683663c5484eSTejun Heo } 683763c5484eSTejun Heo 683863c5484eSTejun Heo static const struct kernel_param_ops wq_affn_dfl_ops = { 683963c5484eSTejun Heo .set = wq_affn_dfl_set, 684063c5484eSTejun Heo .get = wq_affn_dfl_get, 684163c5484eSTejun Heo }; 684263c5484eSTejun Heo 684363c5484eSTejun Heo module_param_cb(default_affinity_scope, &wq_affn_dfl_ops, NULL, 0644); 684463c5484eSTejun Heo 68456ba94429SFrederic Weisbecker #ifdef CONFIG_SYSFS 68466ba94429SFrederic Weisbecker /* 68476ba94429SFrederic Weisbecker * Workqueues with WQ_SYSFS flag set is visible to userland via 68486ba94429SFrederic Weisbecker * /sys/bus/workqueue/devices/WQ_NAME. All visible workqueues have the 68496ba94429SFrederic Weisbecker * following attributes. 68506ba94429SFrederic Weisbecker * 68516ba94429SFrederic Weisbecker * per_cpu RO bool : whether the workqueue is per-cpu or unbound 68526ba94429SFrederic Weisbecker * max_active RW int : maximum number of in-flight work items 68536ba94429SFrederic Weisbecker * 68546ba94429SFrederic Weisbecker * Unbound workqueues have the following extra attributes. 68556ba94429SFrederic Weisbecker * 68566ba94429SFrederic Weisbecker * nice RW int : nice value of the workers 68576ba94429SFrederic Weisbecker * cpumask RW mask : bitmask of allowed CPUs for the workers 685863c5484eSTejun Heo * affinity_scope RW str : worker CPU affinity scope (cache, numa, none) 68598639ecebSTejun Heo * affinity_strict RW bool : worker CPU affinity is strict 68606ba94429SFrederic Weisbecker */ 68616ba94429SFrederic Weisbecker struct wq_device { 68626ba94429SFrederic Weisbecker struct workqueue_struct *wq; 68636ba94429SFrederic Weisbecker struct device dev; 68646ba94429SFrederic Weisbecker }; 68656ba94429SFrederic Weisbecker 68666ba94429SFrederic Weisbecker static struct workqueue_struct *dev_to_wq(struct device *dev) 68676ba94429SFrederic Weisbecker { 68686ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 68696ba94429SFrederic Weisbecker 68706ba94429SFrederic Weisbecker return wq_dev->wq; 68716ba94429SFrederic Weisbecker } 68726ba94429SFrederic Weisbecker 68736ba94429SFrederic Weisbecker static ssize_t per_cpu_show(struct device *dev, struct device_attribute *attr, 68746ba94429SFrederic Weisbecker 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", (bool)!(wq->flags & WQ_UNBOUND)); 68796ba94429SFrederic Weisbecker } 68806ba94429SFrederic Weisbecker static DEVICE_ATTR_RO(per_cpu); 68816ba94429SFrederic Weisbecker 68826ba94429SFrederic Weisbecker static ssize_t max_active_show(struct device *dev, 68836ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 68846ba94429SFrederic Weisbecker { 68856ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68866ba94429SFrederic Weisbecker 68876ba94429SFrederic Weisbecker return scnprintf(buf, PAGE_SIZE, "%d\n", wq->saved_max_active); 68886ba94429SFrederic Weisbecker } 68896ba94429SFrederic Weisbecker 68906ba94429SFrederic Weisbecker static ssize_t max_active_store(struct device *dev, 68916ba94429SFrederic Weisbecker struct device_attribute *attr, const char *buf, 68926ba94429SFrederic Weisbecker size_t count) 68936ba94429SFrederic Weisbecker { 68946ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 68956ba94429SFrederic Weisbecker int val; 68966ba94429SFrederic Weisbecker 68976ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &val) != 1 || val <= 0) 68986ba94429SFrederic Weisbecker return -EINVAL; 68996ba94429SFrederic Weisbecker 69006ba94429SFrederic Weisbecker workqueue_set_max_active(wq, val); 69016ba94429SFrederic Weisbecker return count; 69026ba94429SFrederic Weisbecker } 69036ba94429SFrederic Weisbecker static DEVICE_ATTR_RW(max_active); 69046ba94429SFrederic Weisbecker 69056ba94429SFrederic Weisbecker static struct attribute *wq_sysfs_attrs[] = { 69066ba94429SFrederic Weisbecker &dev_attr_per_cpu.attr, 69076ba94429SFrederic Weisbecker &dev_attr_max_active.attr, 69086ba94429SFrederic Weisbecker NULL, 69096ba94429SFrederic Weisbecker }; 69106ba94429SFrederic Weisbecker ATTRIBUTE_GROUPS(wq_sysfs); 69116ba94429SFrederic Weisbecker 691249277a5bSWaiman Long static void apply_wqattrs_lock(void) 691349277a5bSWaiman Long { 691449277a5bSWaiman Long /* CPUs should stay stable across pwq creations and installations */ 691549277a5bSWaiman Long cpus_read_lock(); 691649277a5bSWaiman Long mutex_lock(&wq_pool_mutex); 691749277a5bSWaiman Long } 691849277a5bSWaiman Long 691949277a5bSWaiman Long static void apply_wqattrs_unlock(void) 692049277a5bSWaiman Long { 692149277a5bSWaiman Long mutex_unlock(&wq_pool_mutex); 692249277a5bSWaiman Long cpus_read_unlock(); 692349277a5bSWaiman Long } 692449277a5bSWaiman Long 69256ba94429SFrederic Weisbecker static ssize_t wq_nice_show(struct device *dev, struct device_attribute *attr, 69266ba94429SFrederic Weisbecker char *buf) 69276ba94429SFrederic Weisbecker { 69286ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69296ba94429SFrederic Weisbecker int written; 69306ba94429SFrederic Weisbecker 69316ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 69326ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%d\n", wq->unbound_attrs->nice); 69336ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 69346ba94429SFrederic Weisbecker 69356ba94429SFrederic Weisbecker return written; 69366ba94429SFrederic Weisbecker } 69376ba94429SFrederic Weisbecker 69386ba94429SFrederic Weisbecker /* prepare workqueue_attrs for sysfs store operations */ 69396ba94429SFrederic Weisbecker static struct workqueue_attrs *wq_sysfs_prep_attrs(struct workqueue_struct *wq) 69406ba94429SFrederic Weisbecker { 69416ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 69426ba94429SFrederic Weisbecker 6943899a94feSLai Jiangshan lockdep_assert_held(&wq_pool_mutex); 6944899a94feSLai Jiangshan 6945be69d00dSThomas Gleixner attrs = alloc_workqueue_attrs(); 69466ba94429SFrederic Weisbecker if (!attrs) 69476ba94429SFrederic Weisbecker return NULL; 69486ba94429SFrederic Weisbecker 69496ba94429SFrederic Weisbecker copy_workqueue_attrs(attrs, wq->unbound_attrs); 69506ba94429SFrederic Weisbecker return attrs; 69516ba94429SFrederic Weisbecker } 69526ba94429SFrederic Weisbecker 69536ba94429SFrederic Weisbecker static ssize_t wq_nice_store(struct device *dev, struct device_attribute *attr, 69546ba94429SFrederic Weisbecker const char *buf, size_t count) 69556ba94429SFrederic Weisbecker { 69566ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69576ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6958d4d3e257SLai Jiangshan int ret = -ENOMEM; 6959d4d3e257SLai Jiangshan 6960d4d3e257SLai Jiangshan apply_wqattrs_lock(); 69616ba94429SFrederic Weisbecker 69626ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 69636ba94429SFrederic Weisbecker if (!attrs) 6964d4d3e257SLai Jiangshan goto out_unlock; 69656ba94429SFrederic Weisbecker 69666ba94429SFrederic Weisbecker if (sscanf(buf, "%d", &attrs->nice) == 1 && 69676ba94429SFrederic Weisbecker attrs->nice >= MIN_NICE && attrs->nice <= MAX_NICE) 6968d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 69696ba94429SFrederic Weisbecker else 69706ba94429SFrederic Weisbecker ret = -EINVAL; 69716ba94429SFrederic Weisbecker 6972d4d3e257SLai Jiangshan out_unlock: 6973d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 69746ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 69756ba94429SFrederic Weisbecker return ret ?: count; 69766ba94429SFrederic Weisbecker } 69776ba94429SFrederic Weisbecker 69786ba94429SFrederic Weisbecker static ssize_t wq_cpumask_show(struct device *dev, 69796ba94429SFrederic Weisbecker struct device_attribute *attr, char *buf) 69806ba94429SFrederic Weisbecker { 69816ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69826ba94429SFrederic Weisbecker int written; 69836ba94429SFrederic Weisbecker 69846ba94429SFrederic Weisbecker mutex_lock(&wq->mutex); 69856ba94429SFrederic Weisbecker written = scnprintf(buf, PAGE_SIZE, "%*pb\n", 69866ba94429SFrederic Weisbecker cpumask_pr_args(wq->unbound_attrs->cpumask)); 69876ba94429SFrederic Weisbecker mutex_unlock(&wq->mutex); 69886ba94429SFrederic Weisbecker return written; 69896ba94429SFrederic Weisbecker } 69906ba94429SFrederic Weisbecker 69916ba94429SFrederic Weisbecker static ssize_t wq_cpumask_store(struct device *dev, 69926ba94429SFrederic Weisbecker struct device_attribute *attr, 69936ba94429SFrederic Weisbecker const char *buf, size_t count) 69946ba94429SFrederic Weisbecker { 69956ba94429SFrederic Weisbecker struct workqueue_struct *wq = dev_to_wq(dev); 69966ba94429SFrederic Weisbecker struct workqueue_attrs *attrs; 6997d4d3e257SLai Jiangshan int ret = -ENOMEM; 6998d4d3e257SLai Jiangshan 6999d4d3e257SLai Jiangshan apply_wqattrs_lock(); 70006ba94429SFrederic Weisbecker 70016ba94429SFrederic Weisbecker attrs = wq_sysfs_prep_attrs(wq); 70026ba94429SFrederic Weisbecker if (!attrs) 7003d4d3e257SLai Jiangshan goto out_unlock; 70046ba94429SFrederic Weisbecker 70056ba94429SFrederic Weisbecker ret = cpumask_parse(buf, attrs->cpumask); 70066ba94429SFrederic Weisbecker if (!ret) 7007d4d3e257SLai Jiangshan ret = apply_workqueue_attrs_locked(wq, attrs); 70086ba94429SFrederic Weisbecker 7009d4d3e257SLai Jiangshan out_unlock: 7010d4d3e257SLai Jiangshan apply_wqattrs_unlock(); 70116ba94429SFrederic Weisbecker free_workqueue_attrs(attrs); 70126ba94429SFrederic Weisbecker return ret ?: count; 70136ba94429SFrederic Weisbecker } 70146ba94429SFrederic Weisbecker 701563c5484eSTejun Heo static ssize_t wq_affn_scope_show(struct device *dev, 701663c5484eSTejun Heo struct device_attribute *attr, char *buf) 701763c5484eSTejun Heo { 701863c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 701963c5484eSTejun Heo int written; 702063c5484eSTejun Heo 702163c5484eSTejun Heo mutex_lock(&wq->mutex); 7022523a301eSTejun Heo if (wq->unbound_attrs->affn_scope == WQ_AFFN_DFL) 7023523a301eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s (%s)\n", 7024523a301eSTejun Heo wq_affn_names[WQ_AFFN_DFL], 7025523a301eSTejun Heo wq_affn_names[wq_affn_dfl]); 7026523a301eSTejun Heo else 702763c5484eSTejun Heo written = scnprintf(buf, PAGE_SIZE, "%s\n", 702863c5484eSTejun Heo wq_affn_names[wq->unbound_attrs->affn_scope]); 702963c5484eSTejun Heo mutex_unlock(&wq->mutex); 703063c5484eSTejun Heo 703163c5484eSTejun Heo return written; 703263c5484eSTejun Heo } 703363c5484eSTejun Heo 703463c5484eSTejun Heo static ssize_t wq_affn_scope_store(struct device *dev, 703563c5484eSTejun Heo struct device_attribute *attr, 703663c5484eSTejun Heo const char *buf, size_t count) 703763c5484eSTejun Heo { 703863c5484eSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 703963c5484eSTejun Heo struct workqueue_attrs *attrs; 704063c5484eSTejun Heo int affn, ret = -ENOMEM; 704163c5484eSTejun Heo 704263c5484eSTejun Heo affn = parse_affn_scope(buf); 704363c5484eSTejun Heo if (affn < 0) 704463c5484eSTejun Heo return affn; 704563c5484eSTejun Heo 704663c5484eSTejun Heo apply_wqattrs_lock(); 704763c5484eSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 704863c5484eSTejun Heo if (attrs) { 704963c5484eSTejun Heo attrs->affn_scope = affn; 705063c5484eSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 705163c5484eSTejun Heo } 705263c5484eSTejun Heo apply_wqattrs_unlock(); 705363c5484eSTejun Heo free_workqueue_attrs(attrs); 705463c5484eSTejun Heo return ret ?: count; 705563c5484eSTejun Heo } 705663c5484eSTejun Heo 70578639ecebSTejun Heo static ssize_t wq_affinity_strict_show(struct device *dev, 70588639ecebSTejun Heo struct device_attribute *attr, char *buf) 70598639ecebSTejun Heo { 70608639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 70618639ecebSTejun Heo 70628639ecebSTejun Heo return scnprintf(buf, PAGE_SIZE, "%d\n", 70638639ecebSTejun Heo wq->unbound_attrs->affn_strict); 70648639ecebSTejun Heo } 70658639ecebSTejun Heo 70668639ecebSTejun Heo static ssize_t wq_affinity_strict_store(struct device *dev, 70678639ecebSTejun Heo struct device_attribute *attr, 70688639ecebSTejun Heo const char *buf, size_t count) 70698639ecebSTejun Heo { 70708639ecebSTejun Heo struct workqueue_struct *wq = dev_to_wq(dev); 70718639ecebSTejun Heo struct workqueue_attrs *attrs; 70728639ecebSTejun Heo int v, ret = -ENOMEM; 70738639ecebSTejun Heo 70748639ecebSTejun Heo if (sscanf(buf, "%d", &v) != 1) 70758639ecebSTejun Heo return -EINVAL; 70768639ecebSTejun Heo 70778639ecebSTejun Heo apply_wqattrs_lock(); 70788639ecebSTejun Heo attrs = wq_sysfs_prep_attrs(wq); 70798639ecebSTejun Heo if (attrs) { 70808639ecebSTejun Heo attrs->affn_strict = (bool)v; 70818639ecebSTejun Heo ret = apply_workqueue_attrs_locked(wq, attrs); 70828639ecebSTejun Heo } 70838639ecebSTejun Heo apply_wqattrs_unlock(); 70848639ecebSTejun Heo free_workqueue_attrs(attrs); 70858639ecebSTejun Heo return ret ?: count; 70868639ecebSTejun Heo } 70878639ecebSTejun Heo 70886ba94429SFrederic Weisbecker static struct device_attribute wq_sysfs_unbound_attrs[] = { 70896ba94429SFrederic Weisbecker __ATTR(nice, 0644, wq_nice_show, wq_nice_store), 70906ba94429SFrederic Weisbecker __ATTR(cpumask, 0644, wq_cpumask_show, wq_cpumask_store), 709163c5484eSTejun Heo __ATTR(affinity_scope, 0644, wq_affn_scope_show, wq_affn_scope_store), 70928639ecebSTejun Heo __ATTR(affinity_strict, 0644, wq_affinity_strict_show, wq_affinity_strict_store), 70936ba94429SFrederic Weisbecker __ATTR_NULL, 70946ba94429SFrederic Weisbecker }; 70956ba94429SFrederic Weisbecker 70965df9197eSRicardo B. Marliere static const struct bus_type wq_subsys = { 70976ba94429SFrederic Weisbecker .name = "workqueue", 70986ba94429SFrederic Weisbecker .dev_groups = wq_sysfs_groups, 70996ba94429SFrederic Weisbecker }; 71006ba94429SFrederic Weisbecker 710149277a5bSWaiman Long /** 710249277a5bSWaiman Long * workqueue_set_unbound_cpumask - Set the low-level unbound cpumask 710349277a5bSWaiman Long * @cpumask: the cpumask to set 710449277a5bSWaiman Long * 710549277a5bSWaiman Long * The low-level workqueues cpumask is a global cpumask that limits 710649277a5bSWaiman Long * the affinity of all unbound workqueues. This function check the @cpumask 710749277a5bSWaiman Long * and apply it to all unbound workqueues and updates all pwqs of them. 710849277a5bSWaiman Long * 710949277a5bSWaiman Long * Return: 0 - Success 711049277a5bSWaiman Long * -EINVAL - Invalid @cpumask 711149277a5bSWaiman Long * -ENOMEM - Failed to allocate memory for attrs or pwqs. 711249277a5bSWaiman Long */ 711349277a5bSWaiman Long static int workqueue_set_unbound_cpumask(cpumask_var_t cpumask) 711449277a5bSWaiman Long { 711549277a5bSWaiman Long int ret = -EINVAL; 711649277a5bSWaiman Long 711749277a5bSWaiman Long /* 711849277a5bSWaiman Long * Not excluding isolated cpus on purpose. 711949277a5bSWaiman Long * If the user wishes to include them, we allow that. 712049277a5bSWaiman Long */ 712149277a5bSWaiman Long cpumask_and(cpumask, cpumask, cpu_possible_mask); 712249277a5bSWaiman Long if (!cpumask_empty(cpumask)) { 712349277a5bSWaiman Long apply_wqattrs_lock(); 712449277a5bSWaiman Long cpumask_copy(wq_requested_unbound_cpumask, cpumask); 712549277a5bSWaiman Long if (cpumask_equal(cpumask, wq_unbound_cpumask)) { 712649277a5bSWaiman Long ret = 0; 712749277a5bSWaiman Long goto out_unlock; 712849277a5bSWaiman Long } 712949277a5bSWaiman Long 713049277a5bSWaiman Long ret = workqueue_apply_unbound_cpumask(cpumask); 713149277a5bSWaiman Long 713249277a5bSWaiman Long out_unlock: 713349277a5bSWaiman Long apply_wqattrs_unlock(); 713449277a5bSWaiman Long } 713549277a5bSWaiman Long 713649277a5bSWaiman Long return ret; 713749277a5bSWaiman Long } 713849277a5bSWaiman Long 7139fe28f631SWaiman Long static ssize_t __wq_cpumask_show(struct device *dev, 7140fe28f631SWaiman Long struct device_attribute *attr, char *buf, cpumask_var_t mask) 7141b05a7928SFrederic Weisbecker { 7142b05a7928SFrederic Weisbecker int written; 7143b05a7928SFrederic Weisbecker 7144042f7df1SLai Jiangshan mutex_lock(&wq_pool_mutex); 7145fe28f631SWaiman Long written = scnprintf(buf, PAGE_SIZE, "%*pb\n", cpumask_pr_args(mask)); 7146042f7df1SLai Jiangshan mutex_unlock(&wq_pool_mutex); 7147b05a7928SFrederic Weisbecker 7148b05a7928SFrederic Weisbecker return written; 7149b05a7928SFrederic Weisbecker } 7150b05a7928SFrederic Weisbecker 7151fe28f631SWaiman Long static ssize_t wq_unbound_cpumask_show(struct device *dev, 7152fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7153fe28f631SWaiman Long { 7154fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_unbound_cpumask); 7155fe28f631SWaiman Long } 7156fe28f631SWaiman Long 7157fe28f631SWaiman Long static ssize_t wq_requested_cpumask_show(struct device *dev, 7158fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7159fe28f631SWaiman Long { 7160fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_requested_unbound_cpumask); 7161fe28f631SWaiman Long } 7162fe28f631SWaiman Long 7163fe28f631SWaiman Long static ssize_t wq_isolated_cpumask_show(struct device *dev, 7164fe28f631SWaiman Long struct device_attribute *attr, char *buf) 7165fe28f631SWaiman Long { 7166fe28f631SWaiman Long return __wq_cpumask_show(dev, attr, buf, wq_isolated_cpumask); 7167fe28f631SWaiman Long } 7168fe28f631SWaiman Long 7169042f7df1SLai Jiangshan static ssize_t wq_unbound_cpumask_store(struct device *dev, 7170042f7df1SLai Jiangshan struct device_attribute *attr, const char *buf, size_t count) 7171042f7df1SLai Jiangshan { 7172042f7df1SLai Jiangshan cpumask_var_t cpumask; 7173042f7df1SLai Jiangshan int ret; 7174042f7df1SLai Jiangshan 7175042f7df1SLai Jiangshan if (!zalloc_cpumask_var(&cpumask, GFP_KERNEL)) 7176042f7df1SLai Jiangshan return -ENOMEM; 7177042f7df1SLai Jiangshan 7178042f7df1SLai Jiangshan ret = cpumask_parse(buf, cpumask); 7179042f7df1SLai Jiangshan if (!ret) 7180042f7df1SLai Jiangshan ret = workqueue_set_unbound_cpumask(cpumask); 7181042f7df1SLai Jiangshan 7182042f7df1SLai Jiangshan free_cpumask_var(cpumask); 7183042f7df1SLai Jiangshan return ret ? ret : count; 7184042f7df1SLai Jiangshan } 7185042f7df1SLai Jiangshan 7186fe28f631SWaiman Long static struct device_attribute wq_sysfs_cpumask_attrs[] = { 7187042f7df1SLai Jiangshan __ATTR(cpumask, 0644, wq_unbound_cpumask_show, 7188fe28f631SWaiman Long wq_unbound_cpumask_store), 7189fe28f631SWaiman Long __ATTR(cpumask_requested, 0444, wq_requested_cpumask_show, NULL), 7190fe28f631SWaiman Long __ATTR(cpumask_isolated, 0444, wq_isolated_cpumask_show, NULL), 7191fe28f631SWaiman Long __ATTR_NULL, 7192fe28f631SWaiman Long }; 7193b05a7928SFrederic Weisbecker 71946ba94429SFrederic Weisbecker static int __init wq_sysfs_init(void) 71956ba94429SFrederic Weisbecker { 7196686f6697SGreg Kroah-Hartman struct device *dev_root; 7197b05a7928SFrederic Weisbecker int err; 7198b05a7928SFrederic Weisbecker 7199b05a7928SFrederic Weisbecker err = subsys_virtual_register(&wq_subsys, NULL); 7200b05a7928SFrederic Weisbecker if (err) 7201b05a7928SFrederic Weisbecker return err; 7202b05a7928SFrederic Weisbecker 7203686f6697SGreg Kroah-Hartman dev_root = bus_get_dev_root(&wq_subsys); 7204686f6697SGreg Kroah-Hartman if (dev_root) { 7205fe28f631SWaiman Long struct device_attribute *attr; 7206fe28f631SWaiman Long 7207fe28f631SWaiman Long for (attr = wq_sysfs_cpumask_attrs; attr->attr.name; attr++) { 7208fe28f631SWaiman Long err = device_create_file(dev_root, attr); 7209fe28f631SWaiman Long if (err) 7210fe28f631SWaiman Long break; 7211fe28f631SWaiman Long } 7212686f6697SGreg Kroah-Hartman put_device(dev_root); 7213686f6697SGreg Kroah-Hartman } 7214686f6697SGreg Kroah-Hartman return err; 72156ba94429SFrederic Weisbecker } 72166ba94429SFrederic Weisbecker core_initcall(wq_sysfs_init); 72176ba94429SFrederic Weisbecker 72186ba94429SFrederic Weisbecker static void wq_device_release(struct device *dev) 72196ba94429SFrederic Weisbecker { 72206ba94429SFrederic Weisbecker struct wq_device *wq_dev = container_of(dev, struct wq_device, dev); 72216ba94429SFrederic Weisbecker 72226ba94429SFrederic Weisbecker kfree(wq_dev); 72236ba94429SFrederic Weisbecker } 72246ba94429SFrederic Weisbecker 72256ba94429SFrederic Weisbecker /** 72266ba94429SFrederic Weisbecker * workqueue_sysfs_register - make a workqueue visible in sysfs 72276ba94429SFrederic Weisbecker * @wq: the workqueue to register 72286ba94429SFrederic Weisbecker * 72296ba94429SFrederic Weisbecker * Expose @wq in sysfs under /sys/bus/workqueue/devices. 72306ba94429SFrederic Weisbecker * alloc_workqueue*() automatically calls this function if WQ_SYSFS is set 72316ba94429SFrederic Weisbecker * which is the preferred method. 72326ba94429SFrederic Weisbecker * 72336ba94429SFrederic Weisbecker * Workqueue user should use this function directly iff it wants to apply 72346ba94429SFrederic Weisbecker * workqueue_attrs before making the workqueue visible in sysfs; otherwise, 72356ba94429SFrederic Weisbecker * apply_workqueue_attrs() may race against userland updating the 72366ba94429SFrederic Weisbecker * attributes. 72376ba94429SFrederic Weisbecker * 72386ba94429SFrederic Weisbecker * Return: 0 on success, -errno on failure. 72396ba94429SFrederic Weisbecker */ 72406ba94429SFrederic Weisbecker int workqueue_sysfs_register(struct workqueue_struct *wq) 72416ba94429SFrederic Weisbecker { 72426ba94429SFrederic Weisbecker struct wq_device *wq_dev; 72436ba94429SFrederic Weisbecker int ret; 72446ba94429SFrederic Weisbecker 72456ba94429SFrederic Weisbecker /* 72464c065dbcSWaiman Long * Adjusting max_active breaks ordering guarantee. Disallow exposing 72474c065dbcSWaiman Long * ordered workqueues. 72486ba94429SFrederic Weisbecker */ 72493bc1e711STejun Heo if (WARN_ON(wq->flags & __WQ_ORDERED)) 72506ba94429SFrederic Weisbecker return -EINVAL; 72516ba94429SFrederic Weisbecker 72526ba94429SFrederic Weisbecker wq->wq_dev = wq_dev = kzalloc(sizeof(*wq_dev), GFP_KERNEL); 72536ba94429SFrederic Weisbecker if (!wq_dev) 72546ba94429SFrederic Weisbecker return -ENOMEM; 72556ba94429SFrederic Weisbecker 72566ba94429SFrederic Weisbecker wq_dev->wq = wq; 72576ba94429SFrederic Weisbecker wq_dev->dev.bus = &wq_subsys; 72586ba94429SFrederic Weisbecker wq_dev->dev.release = wq_device_release; 725923217b44SLars-Peter Clausen dev_set_name(&wq_dev->dev, "%s", wq->name); 72606ba94429SFrederic Weisbecker 72616ba94429SFrederic Weisbecker /* 72626ba94429SFrederic Weisbecker * unbound_attrs are created separately. Suppress uevent until 72636ba94429SFrederic Weisbecker * everything is ready. 72646ba94429SFrederic Weisbecker */ 72656ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, true); 72666ba94429SFrederic Weisbecker 72676ba94429SFrederic Weisbecker ret = device_register(&wq_dev->dev); 72686ba94429SFrederic Weisbecker if (ret) { 7269537f4146SArvind Yadav put_device(&wq_dev->dev); 72706ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72716ba94429SFrederic Weisbecker return ret; 72726ba94429SFrederic Weisbecker } 72736ba94429SFrederic Weisbecker 72746ba94429SFrederic Weisbecker if (wq->flags & WQ_UNBOUND) { 72756ba94429SFrederic Weisbecker struct device_attribute *attr; 72766ba94429SFrederic Weisbecker 72776ba94429SFrederic Weisbecker for (attr = wq_sysfs_unbound_attrs; attr->attr.name; attr++) { 72786ba94429SFrederic Weisbecker ret = device_create_file(&wq_dev->dev, attr); 72796ba94429SFrederic Weisbecker if (ret) { 72806ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 72816ba94429SFrederic Weisbecker wq->wq_dev = NULL; 72826ba94429SFrederic Weisbecker return ret; 72836ba94429SFrederic Weisbecker } 72846ba94429SFrederic Weisbecker } 72856ba94429SFrederic Weisbecker } 72866ba94429SFrederic Weisbecker 72876ba94429SFrederic Weisbecker dev_set_uevent_suppress(&wq_dev->dev, false); 72886ba94429SFrederic Weisbecker kobject_uevent(&wq_dev->dev.kobj, KOBJ_ADD); 72896ba94429SFrederic Weisbecker return 0; 72906ba94429SFrederic Weisbecker } 72916ba94429SFrederic Weisbecker 72926ba94429SFrederic Weisbecker /** 72936ba94429SFrederic Weisbecker * workqueue_sysfs_unregister - undo workqueue_sysfs_register() 72946ba94429SFrederic Weisbecker * @wq: the workqueue to unregister 72956ba94429SFrederic Weisbecker * 72966ba94429SFrederic Weisbecker * If @wq is registered to sysfs by workqueue_sysfs_register(), unregister. 72976ba94429SFrederic Weisbecker */ 72986ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) 72996ba94429SFrederic Weisbecker { 73006ba94429SFrederic Weisbecker struct wq_device *wq_dev = wq->wq_dev; 73016ba94429SFrederic Weisbecker 73026ba94429SFrederic Weisbecker if (!wq->wq_dev) 73036ba94429SFrederic Weisbecker return; 73046ba94429SFrederic Weisbecker 73056ba94429SFrederic Weisbecker wq->wq_dev = NULL; 73066ba94429SFrederic Weisbecker device_unregister(&wq_dev->dev); 73076ba94429SFrederic Weisbecker } 73086ba94429SFrederic Weisbecker #else /* CONFIG_SYSFS */ 73096ba94429SFrederic Weisbecker static void workqueue_sysfs_unregister(struct workqueue_struct *wq) { } 73106ba94429SFrederic Weisbecker #endif /* CONFIG_SYSFS */ 73116ba94429SFrederic Weisbecker 731282607adcSTejun Heo /* 731382607adcSTejun Heo * Workqueue watchdog. 731482607adcSTejun Heo * 731582607adcSTejun Heo * Stall may be caused by various bugs - missing WQ_MEM_RECLAIM, illegal 731682607adcSTejun Heo * flush dependency, a concurrency managed work item which stays RUNNING 731782607adcSTejun Heo * indefinitely. Workqueue stalls can be very difficult to debug as the 731882607adcSTejun Heo * usual warning mechanisms don't trigger and internal workqueue state is 731982607adcSTejun Heo * largely opaque. 732082607adcSTejun Heo * 732182607adcSTejun Heo * Workqueue watchdog monitors all worker pools periodically and dumps 732282607adcSTejun Heo * state if some pools failed to make forward progress for a while where 732382607adcSTejun Heo * forward progress is defined as the first item on ->worklist changing. 732482607adcSTejun Heo * 732582607adcSTejun Heo * This mechanism is controlled through the kernel parameter 732682607adcSTejun Heo * "workqueue.watchdog_thresh" which can be updated at runtime through the 732782607adcSTejun Heo * corresponding sysfs parameter file. 732882607adcSTejun Heo */ 732982607adcSTejun Heo #ifdef CONFIG_WQ_WATCHDOG 733082607adcSTejun Heo 733182607adcSTejun Heo static unsigned long wq_watchdog_thresh = 30; 73325cd79d6aSKees Cook static struct timer_list wq_watchdog_timer; 733382607adcSTejun Heo 733482607adcSTejun Heo static unsigned long wq_watchdog_touched = INITIAL_JIFFIES; 733582607adcSTejun Heo static DEFINE_PER_CPU(unsigned long, wq_watchdog_touched_cpu) = INITIAL_JIFFIES; 733682607adcSTejun Heo 7337cd2440d6SPetr Mladek /* 7338cd2440d6SPetr Mladek * Show workers that might prevent the processing of pending work items. 7339cd2440d6SPetr Mladek * The only candidates are CPU-bound workers in the running state. 7340cd2440d6SPetr Mladek * Pending work items should be handled by another idle worker 7341cd2440d6SPetr Mladek * in all other situations. 7342cd2440d6SPetr Mladek */ 7343cd2440d6SPetr Mladek static void show_cpu_pool_hog(struct worker_pool *pool) 7344cd2440d6SPetr Mladek { 7345cd2440d6SPetr Mladek struct worker *worker; 7346c26e2f2eSTejun Heo unsigned long irq_flags; 7347cd2440d6SPetr Mladek int bkt; 7348cd2440d6SPetr Mladek 7349c26e2f2eSTejun Heo raw_spin_lock_irqsave(&pool->lock, irq_flags); 7350cd2440d6SPetr Mladek 7351cd2440d6SPetr Mladek hash_for_each(pool->busy_hash, bkt, worker, hentry) { 7352cd2440d6SPetr Mladek if (task_is_running(worker->task)) { 7353cd2440d6SPetr Mladek /* 7354cd2440d6SPetr Mladek * Defer printing to avoid deadlocks in console 7355cd2440d6SPetr Mladek * drivers that queue work while holding locks 7356cd2440d6SPetr Mladek * also taken in their write paths. 7357cd2440d6SPetr Mladek */ 7358cd2440d6SPetr Mladek printk_deferred_enter(); 7359cd2440d6SPetr Mladek 7360cd2440d6SPetr Mladek pr_info("pool %d:\n", pool->id); 7361cd2440d6SPetr Mladek sched_show_task(worker->task); 7362cd2440d6SPetr Mladek 7363cd2440d6SPetr Mladek printk_deferred_exit(); 7364cd2440d6SPetr Mladek } 7365cd2440d6SPetr Mladek } 7366cd2440d6SPetr Mladek 7367c26e2f2eSTejun Heo raw_spin_unlock_irqrestore(&pool->lock, irq_flags); 7368cd2440d6SPetr Mladek } 7369cd2440d6SPetr Mladek 7370cd2440d6SPetr Mladek static void show_cpu_pools_hogs(void) 7371cd2440d6SPetr Mladek { 7372cd2440d6SPetr Mladek struct worker_pool *pool; 7373cd2440d6SPetr Mladek int pi; 7374cd2440d6SPetr Mladek 7375cd2440d6SPetr Mladek pr_info("Showing backtraces of running workers in stalled CPU-bound worker pools:\n"); 7376cd2440d6SPetr Mladek 7377cd2440d6SPetr Mladek rcu_read_lock(); 7378cd2440d6SPetr Mladek 7379cd2440d6SPetr Mladek for_each_pool(pool, pi) { 7380cd2440d6SPetr Mladek if (pool->cpu_stall) 7381cd2440d6SPetr Mladek show_cpu_pool_hog(pool); 7382cd2440d6SPetr Mladek 7383cd2440d6SPetr Mladek } 7384cd2440d6SPetr Mladek 7385cd2440d6SPetr Mladek rcu_read_unlock(); 7386cd2440d6SPetr Mladek } 7387cd2440d6SPetr Mladek 738882607adcSTejun Heo static void wq_watchdog_reset_touched(void) 738982607adcSTejun Heo { 739082607adcSTejun Heo int cpu; 739182607adcSTejun Heo 739282607adcSTejun Heo wq_watchdog_touched = jiffies; 739382607adcSTejun Heo for_each_possible_cpu(cpu) 739482607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 739582607adcSTejun Heo } 739682607adcSTejun Heo 73975cd79d6aSKees Cook static void wq_watchdog_timer_fn(struct timer_list *unused) 739882607adcSTejun Heo { 739982607adcSTejun Heo unsigned long thresh = READ_ONCE(wq_watchdog_thresh) * HZ; 740082607adcSTejun Heo bool lockup_detected = false; 7401cd2440d6SPetr Mladek bool cpu_pool_stall = false; 7402940d71c6SSergey Senozhatsky unsigned long now = jiffies; 740382607adcSTejun Heo struct worker_pool *pool; 740482607adcSTejun Heo int pi; 740582607adcSTejun Heo 740682607adcSTejun Heo if (!thresh) 740782607adcSTejun Heo return; 740882607adcSTejun Heo 740982607adcSTejun Heo rcu_read_lock(); 741082607adcSTejun Heo 741182607adcSTejun Heo for_each_pool(pool, pi) { 741282607adcSTejun Heo unsigned long pool_ts, touched, ts; 741382607adcSTejun Heo 7414cd2440d6SPetr Mladek pool->cpu_stall = false; 741582607adcSTejun Heo if (list_empty(&pool->worklist)) 741682607adcSTejun Heo continue; 741782607adcSTejun Heo 7418940d71c6SSergey Senozhatsky /* 7419940d71c6SSergey Senozhatsky * If a virtual machine is stopped by the host it can look to 7420940d71c6SSergey Senozhatsky * the watchdog like a stall. 7421940d71c6SSergey Senozhatsky */ 7422940d71c6SSergey Senozhatsky kvm_check_and_clear_guest_paused(); 7423940d71c6SSergey Senozhatsky 742482607adcSTejun Heo /* get the latest of pool and touched timestamps */ 742589e28ce6SWang Qing if (pool->cpu >= 0) 742689e28ce6SWang Qing touched = READ_ONCE(per_cpu(wq_watchdog_touched_cpu, pool->cpu)); 742789e28ce6SWang Qing else 742882607adcSTejun Heo touched = READ_ONCE(wq_watchdog_touched); 742989e28ce6SWang Qing pool_ts = READ_ONCE(pool->watchdog_ts); 743082607adcSTejun Heo 743182607adcSTejun Heo if (time_after(pool_ts, touched)) 743282607adcSTejun Heo ts = pool_ts; 743382607adcSTejun Heo else 743482607adcSTejun Heo ts = touched; 743582607adcSTejun Heo 743682607adcSTejun Heo /* did we stall? */ 7437940d71c6SSergey Senozhatsky if (time_after(now, ts + thresh)) { 743882607adcSTejun Heo lockup_detected = true; 74394cb1ef64STejun Heo if (pool->cpu >= 0 && !(pool->flags & POOL_BH)) { 7440cd2440d6SPetr Mladek pool->cpu_stall = true; 7441cd2440d6SPetr Mladek cpu_pool_stall = true; 7442cd2440d6SPetr Mladek } 744382607adcSTejun Heo pr_emerg("BUG: workqueue lockup - pool"); 744482607adcSTejun Heo pr_cont_pool_info(pool); 744582607adcSTejun Heo pr_cont(" stuck for %us!\n", 7446940d71c6SSergey Senozhatsky jiffies_to_msecs(now - pool_ts) / 1000); 744782607adcSTejun Heo } 7448cd2440d6SPetr Mladek 7449cd2440d6SPetr Mladek 745082607adcSTejun Heo } 745182607adcSTejun Heo 745282607adcSTejun Heo rcu_read_unlock(); 745382607adcSTejun Heo 745482607adcSTejun Heo if (lockup_detected) 745555df0933SImran Khan show_all_workqueues(); 745682607adcSTejun Heo 7457cd2440d6SPetr Mladek if (cpu_pool_stall) 7458cd2440d6SPetr Mladek show_cpu_pools_hogs(); 7459cd2440d6SPetr Mladek 746082607adcSTejun Heo wq_watchdog_reset_touched(); 746182607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh); 746282607adcSTejun Heo } 746382607adcSTejun Heo 7464cb9d7fd5SVincent Whitchurch notrace void wq_watchdog_touch(int cpu) 746582607adcSTejun Heo { 746682607adcSTejun Heo if (cpu >= 0) 746782607adcSTejun Heo per_cpu(wq_watchdog_touched_cpu, cpu) = jiffies; 746889e28ce6SWang Qing 746982607adcSTejun Heo wq_watchdog_touched = jiffies; 747082607adcSTejun Heo } 747182607adcSTejun Heo 747282607adcSTejun Heo static void wq_watchdog_set_thresh(unsigned long thresh) 747382607adcSTejun Heo { 747482607adcSTejun Heo wq_watchdog_thresh = 0; 747582607adcSTejun Heo del_timer_sync(&wq_watchdog_timer); 747682607adcSTejun Heo 747782607adcSTejun Heo if (thresh) { 747882607adcSTejun Heo wq_watchdog_thresh = thresh; 747982607adcSTejun Heo wq_watchdog_reset_touched(); 748082607adcSTejun Heo mod_timer(&wq_watchdog_timer, jiffies + thresh * HZ); 748182607adcSTejun Heo } 748282607adcSTejun Heo } 748382607adcSTejun Heo 748482607adcSTejun Heo static int wq_watchdog_param_set_thresh(const char *val, 748582607adcSTejun Heo const struct kernel_param *kp) 748682607adcSTejun Heo { 748782607adcSTejun Heo unsigned long thresh; 748882607adcSTejun Heo int ret; 748982607adcSTejun Heo 749082607adcSTejun Heo ret = kstrtoul(val, 0, &thresh); 749182607adcSTejun Heo if (ret) 749282607adcSTejun Heo return ret; 749382607adcSTejun Heo 749482607adcSTejun Heo if (system_wq) 749582607adcSTejun Heo wq_watchdog_set_thresh(thresh); 749682607adcSTejun Heo else 749782607adcSTejun Heo wq_watchdog_thresh = thresh; 749882607adcSTejun Heo 749982607adcSTejun Heo return 0; 750082607adcSTejun Heo } 750182607adcSTejun Heo 750282607adcSTejun Heo static const struct kernel_param_ops wq_watchdog_thresh_ops = { 750382607adcSTejun Heo .set = wq_watchdog_param_set_thresh, 750482607adcSTejun Heo .get = param_get_ulong, 750582607adcSTejun Heo }; 750682607adcSTejun Heo 750782607adcSTejun Heo module_param_cb(watchdog_thresh, &wq_watchdog_thresh_ops, &wq_watchdog_thresh, 750882607adcSTejun Heo 0644); 750982607adcSTejun Heo 751082607adcSTejun Heo static void wq_watchdog_init(void) 751182607adcSTejun Heo { 75125cd79d6aSKees Cook timer_setup(&wq_watchdog_timer, wq_watchdog_timer_fn, TIMER_DEFERRABLE); 751382607adcSTejun Heo wq_watchdog_set_thresh(wq_watchdog_thresh); 751482607adcSTejun Heo } 751582607adcSTejun Heo 751682607adcSTejun Heo #else /* CONFIG_WQ_WATCHDOG */ 751782607adcSTejun Heo 751882607adcSTejun Heo static inline void wq_watchdog_init(void) { } 751982607adcSTejun Heo 752082607adcSTejun Heo #endif /* CONFIG_WQ_WATCHDOG */ 752182607adcSTejun Heo 75222f34d733STejun Heo static void bh_pool_kick_normal(struct irq_work *irq_work) 75232f34d733STejun Heo { 75242f34d733STejun Heo raise_softirq_irqoff(TASKLET_SOFTIRQ); 75252f34d733STejun Heo } 75262f34d733STejun Heo 75272f34d733STejun Heo static void bh_pool_kick_highpri(struct irq_work *irq_work) 75282f34d733STejun Heo { 75292f34d733STejun Heo raise_softirq_irqoff(HI_SOFTIRQ); 75302f34d733STejun Heo } 75312f34d733STejun Heo 75324a6c5607STejun Heo static void __init restrict_unbound_cpumask(const char *name, const struct cpumask *mask) 75334a6c5607STejun Heo { 75344a6c5607STejun Heo if (!cpumask_intersects(wq_unbound_cpumask, mask)) { 75354a6c5607STejun Heo pr_warn("workqueue: Restricting unbound_cpumask (%*pb) with %s (%*pb) leaves no CPU, ignoring\n", 75364a6c5607STejun Heo cpumask_pr_args(wq_unbound_cpumask), name, cpumask_pr_args(mask)); 75374a6c5607STejun Heo return; 75384a6c5607STejun Heo } 75394a6c5607STejun Heo 75404a6c5607STejun Heo cpumask_and(wq_unbound_cpumask, wq_unbound_cpumask, mask); 75414a6c5607STejun Heo } 75424a6c5607STejun Heo 75432fcdb1b4STejun Heo static void __init init_cpu_worker_pool(struct worker_pool *pool, int cpu, int nice) 75442fcdb1b4STejun Heo { 75452fcdb1b4STejun Heo BUG_ON(init_worker_pool(pool)); 75462fcdb1b4STejun Heo pool->cpu = cpu; 75472fcdb1b4STejun Heo cpumask_copy(pool->attrs->cpumask, cpumask_of(cpu)); 75482fcdb1b4STejun Heo cpumask_copy(pool->attrs->__pod_cpumask, cpumask_of(cpu)); 75492fcdb1b4STejun Heo pool->attrs->nice = nice; 75502fcdb1b4STejun Heo pool->attrs->affn_strict = true; 75512fcdb1b4STejun Heo pool->node = cpu_to_node(cpu); 75522fcdb1b4STejun Heo 75532fcdb1b4STejun Heo /* alloc pool ID */ 75542fcdb1b4STejun Heo mutex_lock(&wq_pool_mutex); 75552fcdb1b4STejun Heo BUG_ON(worker_pool_assign_id(pool)); 75562fcdb1b4STejun Heo mutex_unlock(&wq_pool_mutex); 75572fcdb1b4STejun Heo } 75582fcdb1b4STejun Heo 75593347fa09STejun Heo /** 75603347fa09STejun Heo * workqueue_init_early - early init for workqueue subsystem 75613347fa09STejun Heo * 75622930155bSTejun Heo * This is the first step of three-staged workqueue subsystem initialization and 75632930155bSTejun Heo * invoked as soon as the bare basics - memory allocation, cpumasks and idr are 75642930155bSTejun Heo * up. It sets up all the data structures and system workqueues and allows early 75652930155bSTejun Heo * boot code to create workqueues and queue/cancel work items. Actual work item 75662930155bSTejun Heo * execution starts only after kthreads can be created and scheduled right 75672930155bSTejun Heo * before early initcalls. 75683347fa09STejun Heo */ 75692333e829SYu Chen void __init workqueue_init_early(void) 75701da177e4SLinus Torvalds { 757184193c07STejun Heo struct wq_pod_type *pt = &wq_pod_types[WQ_AFFN_SYSTEM]; 75727a4e344cSTejun Heo int std_nice[NR_STD_WORKER_POOLS] = { 0, HIGHPRI_NICE_LEVEL }; 75732f34d733STejun Heo void (*irq_work_fns[2])(struct irq_work *) = { bh_pool_kick_normal, 75742f34d733STejun Heo bh_pool_kick_highpri }; 75757a4e344cSTejun Heo int i, cpu; 7576c34056a3STejun Heo 757710cdb157SLai Jiangshan BUILD_BUG_ON(__alignof__(struct pool_workqueue) < __alignof__(long long)); 7578e904e6c2STejun Heo 7579b05a7928SFrederic Weisbecker BUG_ON(!alloc_cpumask_var(&wq_unbound_cpumask, GFP_KERNEL)); 7580fe28f631SWaiman Long BUG_ON(!alloc_cpumask_var(&wq_requested_unbound_cpumask, GFP_KERNEL)); 7581fe28f631SWaiman Long BUG_ON(!zalloc_cpumask_var(&wq_isolated_cpumask, GFP_KERNEL)); 7582b05a7928SFrederic Weisbecker 75834a6c5607STejun Heo cpumask_copy(wq_unbound_cpumask, cpu_possible_mask); 75844a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_WQ", housekeeping_cpumask(HK_TYPE_WQ)); 75854a6c5607STejun Heo restrict_unbound_cpumask("HK_TYPE_DOMAIN", housekeeping_cpumask(HK_TYPE_DOMAIN)); 7586ace3c549Stiozhang if (!cpumask_empty(&wq_cmdline_cpumask)) 75874a6c5607STejun Heo restrict_unbound_cpumask("workqueue.unbound_cpus", &wq_cmdline_cpumask); 7588ace3c549Stiozhang 7589fe28f631SWaiman Long cpumask_copy(wq_requested_unbound_cpumask, wq_unbound_cpumask); 75908b03ae3cSTejun Heo 75918b03ae3cSTejun Heo pwq_cache = KMEM_CACHE(pool_workqueue, SLAB_PANIC); 7592e22bee78STejun Heo 75932930155bSTejun Heo wq_update_pod_attrs_buf = alloc_workqueue_attrs(); 75942930155bSTejun Heo BUG_ON(!wq_update_pod_attrs_buf); 75952930155bSTejun Heo 75967bd20b6bSMarcelo Tosatti /* 75977bd20b6bSMarcelo Tosatti * If nohz_full is enabled, set power efficient workqueue as unbound. 75987bd20b6bSMarcelo Tosatti * This allows workqueue items to be moved to HK CPUs. 75997bd20b6bSMarcelo Tosatti */ 76007bd20b6bSMarcelo Tosatti if (housekeeping_enabled(HK_TYPE_TICK)) 76017bd20b6bSMarcelo Tosatti wq_power_efficient = true; 76027bd20b6bSMarcelo Tosatti 760384193c07STejun Heo /* initialize WQ_AFFN_SYSTEM pods */ 760484193c07STejun Heo pt->pod_cpus = kcalloc(1, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 760584193c07STejun Heo pt->pod_node = kcalloc(1, sizeof(pt->pod_node[0]), GFP_KERNEL); 760684193c07STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 760784193c07STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node || !pt->cpu_pod); 760884193c07STejun Heo 760984193c07STejun Heo BUG_ON(!zalloc_cpumask_var_node(&pt->pod_cpus[0], GFP_KERNEL, NUMA_NO_NODE)); 761084193c07STejun Heo 761184193c07STejun Heo pt->nr_pods = 1; 761284193c07STejun Heo cpumask_copy(pt->pod_cpus[0], cpu_possible_mask); 761384193c07STejun Heo pt->pod_node[0] = NUMA_NO_NODE; 761484193c07STejun Heo pt->cpu_pod[0] = 0; 761584193c07STejun Heo 76164cb1ef64STejun Heo /* initialize BH and CPU pools */ 76171da177e4SLinus Torvalds for_each_possible_cpu(cpu) { 76181da177e4SLinus Torvalds struct worker_pool *pool; 76191da177e4SLinus Torvalds 76201da177e4SLinus Torvalds i = 0; 76214cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) { 76222f34d733STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i]); 76234cb1ef64STejun Heo pool->flags |= POOL_BH; 76242f34d733STejun Heo init_irq_work(bh_pool_irq_work(pool), irq_work_fns[i]); 76252f34d733STejun Heo i++; 76264cb1ef64STejun Heo } 76274cb1ef64STejun Heo 76284cb1ef64STejun Heo i = 0; 76292fcdb1b4STejun Heo for_each_cpu_worker_pool(pool, cpu) 76302fcdb1b4STejun Heo init_cpu_worker_pool(pool, cpu, std_nice[i++]); 76311da177e4SLinus Torvalds } 76321da177e4SLinus Torvalds 76338a2b7538STejun Heo /* create default unbound and ordered wq attrs */ 763429c91e99STejun Heo for (i = 0; i < NR_STD_WORKER_POOLS; i++) { 763529c91e99STejun Heo struct workqueue_attrs *attrs; 763629c91e99STejun Heo 7637be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 763829c91e99STejun Heo attrs->nice = std_nice[i]; 763929c91e99STejun Heo unbound_std_wq_attrs[i] = attrs; 76408a2b7538STejun Heo 76418a2b7538STejun Heo /* 76428a2b7538STejun Heo * An ordered wq should have only one pwq as ordering is 76438a2b7538STejun Heo * guaranteed by max_active which is enforced by pwqs. 76448a2b7538STejun Heo */ 7645be69d00dSThomas Gleixner BUG_ON(!(attrs = alloc_workqueue_attrs())); 76468a2b7538STejun Heo attrs->nice = std_nice[i]; 7647af73f5c9STejun Heo attrs->ordered = true; 76488a2b7538STejun Heo ordered_wq_attrs[i] = attrs; 764929c91e99STejun Heo } 765029c91e99STejun Heo 7651d320c038STejun Heo system_wq = alloc_workqueue("events", 0, 0); 76521aabe902SJoonsoo Kim system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0); 7653d320c038STejun Heo system_long_wq = alloc_workqueue("events_long", 0, 0); 7654f3421797STejun Heo system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 7655636b927eSTejun Heo WQ_MAX_ACTIVE); 765624d51addSTejun Heo system_freezable_wq = alloc_workqueue("events_freezable", 765724d51addSTejun Heo WQ_FREEZABLE, 0); 76580668106cSViresh Kumar system_power_efficient_wq = alloc_workqueue("events_power_efficient", 76590668106cSViresh Kumar WQ_POWER_EFFICIENT, 0); 76608318d6a6SAudra Mitchell system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_pwr_efficient", 76610668106cSViresh Kumar WQ_FREEZABLE | WQ_POWER_EFFICIENT, 76620668106cSViresh Kumar 0); 76634cb1ef64STejun Heo system_bh_wq = alloc_workqueue("events_bh", WQ_BH, 0); 76644cb1ef64STejun Heo system_bh_highpri_wq = alloc_workqueue("events_bh_highpri", 76654cb1ef64STejun Heo WQ_BH | WQ_HIGHPRI, 0); 76661aabe902SJoonsoo Kim BUG_ON(!system_wq || !system_highpri_wq || !system_long_wq || 76670668106cSViresh Kumar !system_unbound_wq || !system_freezable_wq || 76680668106cSViresh Kumar !system_power_efficient_wq || 76694cb1ef64STejun Heo !system_freezable_power_efficient_wq || 76704cb1ef64STejun Heo !system_bh_wq || !system_bh_highpri_wq); 76713347fa09STejun Heo } 76723347fa09STejun Heo 7673aa6fde93STejun Heo static void __init wq_cpu_intensive_thresh_init(void) 7674aa6fde93STejun Heo { 7675aa6fde93STejun Heo unsigned long thresh; 7676aa6fde93STejun Heo unsigned long bogo; 7677aa6fde93STejun Heo 7678dd64c873SZqiang pwq_release_worker = kthread_create_worker(0, "pool_workqueue_release"); 7679dd64c873SZqiang BUG_ON(IS_ERR(pwq_release_worker)); 7680dd64c873SZqiang 7681aa6fde93STejun Heo /* if the user set it to a specific value, keep it */ 7682aa6fde93STejun Heo if (wq_cpu_intensive_thresh_us != ULONG_MAX) 7683aa6fde93STejun Heo return; 7684aa6fde93STejun Heo 7685aa6fde93STejun Heo /* 7686aa6fde93STejun Heo * The default of 10ms is derived from the fact that most modern (as of 7687aa6fde93STejun Heo * 2023) processors can do a lot in 10ms and that it's just below what 7688aa6fde93STejun Heo * most consider human-perceivable. However, the kernel also runs on a 7689aa6fde93STejun Heo * lot slower CPUs including microcontrollers where the threshold is way 7690aa6fde93STejun Heo * too low. 7691aa6fde93STejun Heo * 7692aa6fde93STejun Heo * Let's scale up the threshold upto 1 second if BogoMips is below 4000. 7693aa6fde93STejun Heo * This is by no means accurate but it doesn't have to be. The mechanism 7694aa6fde93STejun Heo * is still useful even when the threshold is fully scaled up. Also, as 7695aa6fde93STejun Heo * the reports would usually be applicable to everyone, some machines 7696aa6fde93STejun Heo * operating on longer thresholds won't significantly diminish their 7697aa6fde93STejun Heo * usefulness. 7698aa6fde93STejun Heo */ 7699aa6fde93STejun Heo thresh = 10 * USEC_PER_MSEC; 7700aa6fde93STejun Heo 7701aa6fde93STejun Heo /* see init/calibrate.c for lpj -> BogoMIPS calculation */ 7702aa6fde93STejun Heo bogo = max_t(unsigned long, loops_per_jiffy / 500000 * HZ, 1); 7703aa6fde93STejun Heo if (bogo < 4000) 7704aa6fde93STejun Heo thresh = min_t(unsigned long, thresh * 4000 / bogo, USEC_PER_SEC); 7705aa6fde93STejun Heo 7706aa6fde93STejun Heo pr_debug("wq_cpu_intensive_thresh: lpj=%lu BogoMIPS=%lu thresh_us=%lu\n", 7707aa6fde93STejun Heo loops_per_jiffy, bogo, thresh); 7708aa6fde93STejun Heo 7709aa6fde93STejun Heo wq_cpu_intensive_thresh_us = thresh; 7710aa6fde93STejun Heo } 7711aa6fde93STejun Heo 77123347fa09STejun Heo /** 77133347fa09STejun Heo * workqueue_init - bring workqueue subsystem fully online 77143347fa09STejun Heo * 77152930155bSTejun Heo * This is the second step of three-staged workqueue subsystem initialization 77162930155bSTejun Heo * and invoked as soon as kthreads can be created and scheduled. Workqueues have 77172930155bSTejun Heo * been created and work items queued on them, but there are no kworkers 77182930155bSTejun Heo * executing the work items yet. Populate the worker pools with the initial 77192930155bSTejun Heo * workers and enable future kworker creations. 77203347fa09STejun Heo */ 77212333e829SYu Chen void __init workqueue_init(void) 77223347fa09STejun Heo { 77232186d9f9STejun Heo struct workqueue_struct *wq; 77243347fa09STejun Heo struct worker_pool *pool; 77253347fa09STejun Heo int cpu, bkt; 77263347fa09STejun Heo 7727aa6fde93STejun Heo wq_cpu_intensive_thresh_init(); 7728aa6fde93STejun Heo 77292186d9f9STejun Heo mutex_lock(&wq_pool_mutex); 77302186d9f9STejun Heo 77312930155bSTejun Heo /* 77322930155bSTejun Heo * Per-cpu pools created earlier could be missing node hint. Fix them 77332930155bSTejun Heo * up. Also, create a rescuer for workqueues that requested it. 77342930155bSTejun Heo */ 77352186d9f9STejun Heo for_each_possible_cpu(cpu) { 77364cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 77372186d9f9STejun Heo pool->node = cpu_to_node(cpu); 77384cb1ef64STejun Heo for_each_cpu_worker_pool(pool, cpu) 77394cb1ef64STejun Heo pool->node = cpu_to_node(cpu); 77402186d9f9STejun Heo } 77412186d9f9STejun Heo 774240c17f75STejun Heo list_for_each_entry(wq, &workqueues, list) { 774340c17f75STejun Heo WARN(init_rescuer(wq), 774440c17f75STejun Heo "workqueue: failed to create early rescuer for %s", 774540c17f75STejun Heo wq->name); 774640c17f75STejun Heo } 77472186d9f9STejun Heo 77482186d9f9STejun Heo mutex_unlock(&wq_pool_mutex); 77492186d9f9STejun Heo 77504cb1ef64STejun Heo /* 77514cb1ef64STejun Heo * Create the initial workers. A BH pool has one pseudo worker that 77524cb1ef64STejun Heo * represents the shared BH execution context and thus doesn't get 77534cb1ef64STejun Heo * affected by hotplug events. Create the BH pseudo workers for all 77544cb1ef64STejun Heo * possible CPUs here. 77554cb1ef64STejun Heo */ 77564cb1ef64STejun Heo for_each_possible_cpu(cpu) 77574cb1ef64STejun Heo for_each_bh_worker_pool(pool, cpu) 77584cb1ef64STejun Heo BUG_ON(!create_worker(pool)); 77594cb1ef64STejun Heo 77603347fa09STejun Heo for_each_online_cpu(cpu) { 77613347fa09STejun Heo for_each_cpu_worker_pool(pool, cpu) { 77623347fa09STejun Heo pool->flags &= ~POOL_DISASSOCIATED; 77633347fa09STejun Heo BUG_ON(!create_worker(pool)); 77643347fa09STejun Heo } 77653347fa09STejun Heo } 77663347fa09STejun Heo 77673347fa09STejun Heo hash_for_each(unbound_pool_hash, bkt, pool, hash_node) 77683347fa09STejun Heo BUG_ON(!create_worker(pool)); 77693347fa09STejun Heo 77703347fa09STejun Heo wq_online = true; 777182607adcSTejun Heo wq_watchdog_init(); 77721da177e4SLinus Torvalds } 7773c4f135d6STetsuo Handa 7774025e1684STejun Heo /* 7775025e1684STejun Heo * Initialize @pt by first initializing @pt->cpu_pod[] with pod IDs according to 7776025e1684STejun Heo * @cpu_shares_pod(). Each subset of CPUs that share a pod is assigned a unique 7777025e1684STejun Heo * and consecutive pod ID. The rest of @pt is initialized accordingly. 7778025e1684STejun Heo */ 7779025e1684STejun Heo static void __init init_pod_type(struct wq_pod_type *pt, 7780025e1684STejun Heo bool (*cpus_share_pod)(int, int)) 7781025e1684STejun Heo { 7782025e1684STejun Heo int cur, pre, cpu, pod; 7783025e1684STejun Heo 7784025e1684STejun Heo pt->nr_pods = 0; 7785025e1684STejun Heo 7786025e1684STejun Heo /* init @pt->cpu_pod[] according to @cpus_share_pod() */ 7787025e1684STejun Heo pt->cpu_pod = kcalloc(nr_cpu_ids, sizeof(pt->cpu_pod[0]), GFP_KERNEL); 7788025e1684STejun Heo BUG_ON(!pt->cpu_pod); 7789025e1684STejun Heo 7790025e1684STejun Heo for_each_possible_cpu(cur) { 7791025e1684STejun Heo for_each_possible_cpu(pre) { 7792025e1684STejun Heo if (pre >= cur) { 7793025e1684STejun Heo pt->cpu_pod[cur] = pt->nr_pods++; 7794025e1684STejun Heo break; 7795025e1684STejun Heo } 7796025e1684STejun Heo if (cpus_share_pod(cur, pre)) { 7797025e1684STejun Heo pt->cpu_pod[cur] = pt->cpu_pod[pre]; 7798025e1684STejun Heo break; 7799025e1684STejun Heo } 7800025e1684STejun Heo } 7801025e1684STejun Heo } 7802025e1684STejun Heo 7803025e1684STejun Heo /* init the rest to match @pt->cpu_pod[] */ 7804025e1684STejun Heo pt->pod_cpus = kcalloc(pt->nr_pods, sizeof(pt->pod_cpus[0]), GFP_KERNEL); 7805025e1684STejun Heo pt->pod_node = kcalloc(pt->nr_pods, sizeof(pt->pod_node[0]), GFP_KERNEL); 7806025e1684STejun Heo BUG_ON(!pt->pod_cpus || !pt->pod_node); 7807025e1684STejun Heo 7808025e1684STejun Heo for (pod = 0; pod < pt->nr_pods; pod++) 7809025e1684STejun Heo BUG_ON(!zalloc_cpumask_var(&pt->pod_cpus[pod], GFP_KERNEL)); 7810025e1684STejun Heo 7811025e1684STejun Heo for_each_possible_cpu(cpu) { 7812025e1684STejun Heo cpumask_set_cpu(cpu, pt->pod_cpus[pt->cpu_pod[cpu]]); 7813025e1684STejun Heo pt->pod_node[pt->cpu_pod[cpu]] = cpu_to_node(cpu); 7814025e1684STejun Heo } 7815025e1684STejun Heo } 7816025e1684STejun Heo 781763c5484eSTejun Heo static bool __init cpus_dont_share(int cpu0, int cpu1) 781863c5484eSTejun Heo { 781963c5484eSTejun Heo return false; 782063c5484eSTejun Heo } 782163c5484eSTejun Heo 782263c5484eSTejun Heo static bool __init cpus_share_smt(int cpu0, int cpu1) 782363c5484eSTejun Heo { 782463c5484eSTejun Heo #ifdef CONFIG_SCHED_SMT 782563c5484eSTejun Heo return cpumask_test_cpu(cpu0, cpu_smt_mask(cpu1)); 782663c5484eSTejun Heo #else 782763c5484eSTejun Heo return false; 782863c5484eSTejun Heo #endif 782963c5484eSTejun Heo } 783063c5484eSTejun Heo 7831025e1684STejun Heo static bool __init cpus_share_numa(int cpu0, int cpu1) 7832025e1684STejun Heo { 7833025e1684STejun Heo return cpu_to_node(cpu0) == cpu_to_node(cpu1); 7834025e1684STejun Heo } 7835025e1684STejun Heo 78362930155bSTejun Heo /** 78372930155bSTejun Heo * workqueue_init_topology - initialize CPU pods for unbound workqueues 78382930155bSTejun Heo * 783996068b60SWang Jinchao * This is the third step of three-staged workqueue subsystem initialization and 78402930155bSTejun Heo * invoked after SMP and topology information are fully initialized. It 78412930155bSTejun Heo * initializes the unbound CPU pods accordingly. 78422930155bSTejun Heo */ 78432930155bSTejun Heo void __init workqueue_init_topology(void) 7844a86feae6STejun Heo { 78452930155bSTejun Heo struct workqueue_struct *wq; 7846025e1684STejun Heo int cpu; 7847a86feae6STejun Heo 784863c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CPU], cpus_dont_share); 784963c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_SMT], cpus_share_smt); 785063c5484eSTejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_CACHE], cpus_share_cache); 7851025e1684STejun Heo init_pod_type(&wq_pod_types[WQ_AFFN_NUMA], cpus_share_numa); 7852a86feae6STejun Heo 7853c5f8cd6cSTejun Heo wq_topo_initialized = true; 7854c5f8cd6cSTejun Heo 78552930155bSTejun Heo mutex_lock(&wq_pool_mutex); 7856a86feae6STejun Heo 7857a86feae6STejun Heo /* 78582930155bSTejun Heo * Workqueues allocated earlier would have all CPUs sharing the default 78592930155bSTejun Heo * worker pool. Explicitly call wq_update_pod() on all workqueue and CPU 78602930155bSTejun Heo * combinations to apply per-pod sharing. 78612930155bSTejun Heo */ 78622930155bSTejun Heo list_for_each_entry(wq, &workqueues, list) { 78635797b1c1STejun Heo for_each_online_cpu(cpu) 78642930155bSTejun Heo wq_update_pod(wq, cpu, cpu, true); 78655797b1c1STejun Heo if (wq->flags & WQ_UNBOUND) { 78665797b1c1STejun Heo mutex_lock(&wq->mutex); 78675797b1c1STejun Heo wq_update_node_max_active(wq, -1); 78685797b1c1STejun Heo mutex_unlock(&wq->mutex); 78692930155bSTejun Heo } 78702930155bSTejun Heo } 78712930155bSTejun Heo 78722930155bSTejun Heo mutex_unlock(&wq_pool_mutex); 7873a86feae6STejun Heo } 7874a86feae6STejun Heo 787520bdedafSTetsuo Handa void __warn_flushing_systemwide_wq(void) 787620bdedafSTetsuo Handa { 787720bdedafSTetsuo Handa pr_warn("WARNING: Flushing system-wide workqueues will be prohibited in near future.\n"); 787820bdedafSTetsuo Handa dump_stack(); 787920bdedafSTetsuo Handa } 7880c4f135d6STetsuo Handa EXPORT_SYMBOL(__warn_flushing_systemwide_wq); 7881ace3c549Stiozhang 7882ace3c549Stiozhang static int __init workqueue_unbound_cpus_setup(char *str) 7883ace3c549Stiozhang { 7884ace3c549Stiozhang if (cpulist_parse(str, &wq_cmdline_cpumask) < 0) { 7885ace3c549Stiozhang cpumask_clear(&wq_cmdline_cpumask); 7886ace3c549Stiozhang pr_warn("workqueue.unbound_cpus: incorrect CPU range, using default\n"); 7887ace3c549Stiozhang } 7888ace3c549Stiozhang 7889ace3c549Stiozhang return 1; 7890ace3c549Stiozhang } 7891ace3c549Stiozhang __setup("workqueue.unbound_cpus=", workqueue_unbound_cpus_setup); 7892